/***************************************************************************
                            Range.m -  description
                             -------------------
    begin                : Fri Mar  8 13:55:08 2002
    copyright            : (C) 2000-2002 Lost Highway Ltd. All rights reserved.
    email                : Lost_Highway@tipitina.demon.co.uk
    generated by         : duke@tipitina on Fri Mar  8 13:55:08 2002, using kalyptus 0.9.
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This library is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Library General Public License as       *
 *   published by the Free Software Foundation; either version 2 of the    *
 *   License, or (at your option) any later version.                       *
 *                                                                         *
 ***************************************************************************/

#include <qtc/qt_QString.h>
#include <qtc/qt_QCString.h>
#include <kdec/kde_Range.h>
#include <kdeobjc/KDESupport.h>
#include <kdeobjc/Range.h>
#include <kdeobjc/DOMNode.h>
#include <kdeobjc/DOMDocument.h>


@implementation  Range

- (void *) qt
{
	return _qt;
}

- setQt: (void *) qt
{
	_qt = qt;
	return self;
}

- (BOOL) allocatedInObjcWorld
{
	return _allocatedInObjcWorld;
}

- setAllocatedInObjcWorld: (BOOL) yn
{
	_allocatedInObjcWorld = yn;
	return self;
}


- init
{
	if ([self qt] == 0L) {
		[self setQt: kde_new_Range()];
		[self setAllocatedInObjcWorld: YES];
		[QtSupport setObject: self forQtKey: [self qt]];
	}
	[super init];
	return self;
}

//- initWithDocument: (DOMDocument*)rootContainer
//{
//	if ([self qt] == 0L) {
//		[self setQt: kde_new_Range1((kde_DOMDocument*) [rootContainer qt])];
//		[self setAllocatedInObjcWorld: YES];
//		[QtSupport setObject: self forQtKey: [self qt]];
//	}
//	[super init];
//	return self;
//}

- copyWithZone: (NSZone *) zone
{
	Range * aCopy;
	aCopy = [[(Range *)[[self class] allocWithZone: zone] setQt: kde_new_Range2([self qt])] init];
	[aCopy setAllocatedInObjcWorld: YES];
	[QtSupport setObject: aCopy forQtKey: [aCopy qt]];
	return aCopy;
}

- initWithDOMNode: (DOMNode*)startContainer startOffset: (long)startOffset endContainer: (DOMNode*)endContainer endOffset: (long)endOffset
{
	if ([self qt] == 0L) {
		[self setQt: kde_new_Range3((kde_DOMNode*) [startContainer qt], (long)startOffset, (kde_DOMNode*) [endContainer qt], (long)endOffset)];
		[self setAllocatedInObjcWorld: YES];
		[QtSupport setObject: self forQtKey: [self qt]];
	}
	[super init];
	return self;
}

- (void) dealloc
{
	if ([self qt] != 0L) {
		if ([self allocatedInObjcWorld]) {
			kde_del_Range((kde_Range *)[self qt]);
		}
		[QtSupport removeQtKey: [self qt]];
		[self setQt: 0L];
	}
	[super dealloc];
}

- (DOMNode*) startContainer
{
	return (DOMNode*) kde_Range_startContainer((kde_Range *)[self qt]);
}

- (long) startOffset
{
	return (long) kde_Range_startOffset((kde_Range *)[self qt]);
}

- (DOMNode*) endContainer
{
	return (DOMNode*) kde_Range_endContainer((kde_Range *)[self qt]);
}

- (long) endOffset
{
	return (long) kde_Range_endOffset((kde_Range *)[self qt]);
}

- (BOOL) collapsed
{
	return (BOOL) kde_Range_collapsed((kde_Range *)[self qt]);
}

- (DOMNode*) commonAncestorContainer
{
	return (DOMNode*) kde_Range_commonAncestorContainer((kde_Range *)[self qt]);
}

- setStart: (DOMNode*)refNode offset: (long)offset
{
	kde_Range_setStart((kde_Range *)[self qt], (kde_DOMNode*) [refNode qt], (long)offset);
	return self;
}

- setEnd: (DOMNode*)refNode offset: (long)offset
{
	kde_Range_setEnd((kde_Range *)[self qt], (kde_DOMNode*) [refNode qt], (long)offset);
	return self;
}

- setStartBefore: (DOMNode*)refNode
{
	kde_Range_setStartBefore((kde_Range *)[self qt], (kde_DOMNode*) [refNode qt]);
	return self;
}

- setStartAfter: (DOMNode*)refNode
{
	kde_Range_setStartAfter((kde_Range *)[self qt], (kde_DOMNode*) [refNode qt]);
	return self;
}

- setEndBefore: (DOMNode*)refNode
{
	kde_Range_setEndBefore((kde_Range *)[self qt], (kde_DOMNode*) [refNode qt]);
	return self;
}

- setEndAfter: (DOMNode*)refNode
{
	kde_Range_setEndAfter((kde_Range *)[self qt], (kde_DOMNode*) [refNode qt]);
	return self;
}

- collapse: (BOOL)toStart
{
	kde_Range_collapse((kde_Range *)[self qt], (int)toStart);
	return self;
}

- selectNode: (DOMNode*)refNode
{
	kde_Range_selectNode((kde_Range *)[self qt], (kde_DOMNode*) [refNode qt]);
	return self;
}

- selectNodeContents: (DOMNode*)refNode
{
	kde_Range_selectNodeContents((kde_Range *)[self qt], (kde_DOMNode*) [refNode qt]);
	return self;
}

- (short) compareBoundaryPoints: (int)how sourceRange: (Range*)sourceRange
{
	return (short) kde_Range_compareBoundaryPoints((kde_Range *)[self qt], (int)how, (kde_Range*) [sourceRange qt]);
}

- (BOOL) boundaryPointsValid
{
	return (BOOL) kde_Range_boundaryPointsValid((kde_Range *)[self qt]);
}

- deleteContents
{
	kde_Range_deleteContents((kde_Range *)[self qt]);
	return self;
}

- (DocumentFragment*) extractContents
{
	return (DocumentFragment*) [QtSupport objectForQtKey: kde_Range_extractContents((kde_Range *)[self qt]) withClass: [DocumentFragment class] allocatedInObjcWorld: YES];
}

- (DocumentFragment*) cloneContents
{
	return (DocumentFragment*) [QtSupport objectForQtKey: kde_Range_cloneContents((kde_Range *)[self qt]) withClass: [DocumentFragment class] allocatedInObjcWorld: YES];
}

- insertNode: (DOMNode*)newNode
{
	kde_Range_insertNode((kde_Range *)[self qt], (kde_DOMNode*) [newNode qt]);
	return self;
}

- surroundContents: (DOMNode*)newParent
{
	kde_Range_surroundContents((kde_Range *)[self qt], (kde_DOMNode*) [newParent qt]);
	return self;
}

- (Range*) cloneRange
{
	return (Range*) [QtSupport objectForQtKey: kde_Range_cloneRange((kde_Range *)[self qt]) withClass: [Range class] allocatedInObjcWorld: YES];
}

- (NSString *) toString
{
static kde_DOMString * _domstring = 0L;
	_domstring = kde_Range_toString((kde_Range *)[self qt]);
	return [NSString fromDOMString: _domstring];
}

- (NSString *) toHTML
{
static kde_DOMString * _domstring = 0L;
	_domstring = kde_Range_toHTML((kde_Range *)[self qt]);
	return [NSString fromDOMString: _domstring];
}

- detach
{
	kde_Range_detach((kde_Range *)[self qt]);
	return self;
}

- (BOOL) isDetached
{
	return (BOOL) kde_Range_isDetached((kde_Range *)[self qt]);
}

- (BOOL) isNull
{
	return (BOOL) kde_Range_isNull((kde_Range *)[self qt]);
}

+ (int) START_TO_START
{
	return (int) RANGE_START_TO_START;
}

+ (int) START_TO_END
{
	return (int) RANGE_START_TO_END;
}

+ (int) END_TO_END
{
	return (int) RANGE_END_TO_END;
}

+ (int) END_TO_START
{
	return (int) RANGE_END_TO_START;
}

@end
