
#import "Rect.h"

@implementation Rect

- initFromX:(float)x y:(float)y width:(float)width height:(float)height
{
	[super init];
	rect.origin.x = x;
	rect.origin.y = y;
	rect.size.width = width;
	rect.size.height = height;
	return self;
}

- initFromOrigin:(NXPoint *)p size:(NXSize *)s
{
	[super init];
	rect.origin = *p;
	rect.size = *s;
	return self;
}

- initFromRect:(NXRect *)aRect
{
	[super init];
	rect = *aRect;
	return self;
}

- setRect:(NXRect *)aRect
{
	rect = *aRect;
	return self;
}

- (NXRect *)asCPointer		{ return (NXRect *)&rect; }

- setOrigin:aPoint			{ rect.origin.x = [aPoint xCoord]; rect.origin.y = [aPoint yCoord]; return self; }
- setX:(float)x				{ rect.origin.x = x; return self; }
- setY:(float)y				{ rect.origin.y = y; return self; }
- (double)xCoord			{ return (double)rect.origin.x; }
- (double)yCoord			{ return (double)rect.origin.y; }
- origin					{ return [[Point alloc] init]; }

- setSize:aSize				{ rect.size.width = [aSize width]; rect.size.height = [aSize height]; return self; }
- setWidth:(float)width		{ rect.size.width = width; return self; }
- setHeight:(float)height	{ rect.size.height = height; return self; }
- (double)width				{ return (double)rect.size.width; }
- (double)height			{ return (double)rect.size.height; }
- size						{ return [[Size alloc] init]; }

- (double)area				{ return (double)(rect.size.height * rect.size.width); }

- (Rect *)intersectionWith:(Rect *)aRect
{
	NXRect	temp;
	
	temp = *(NXRect *)[(Rect *)aRect asCPointer];
	if (NXIntersectionRect(&rect, &temp) != NULL)
		return [[Rect alloc] initFromRect:&temp];		
	return [[Rect alloc] init];
}

- (Rect *)unionWith:(Rect *)aRect
{
	NXRect	temp;
	
	temp = *(NXRect *)[(Rect *)aRect asCPointer];
	if (NXUnionRect(&rect, &temp) != NULL)
		return [[Rect alloc] initFromRect:&temp];		
	return [[Rect alloc] init];
}

- intergral
{
	NXIntegralRect(&rect);
	return self;
}

- insetBy:(Size *)size
{
	NXInsetRect(&rect, [size width], [size height]);
	return self;
}

- offsetBy:(Size *)size
{
	NXOffsetRect(&rect, [size width], [size height]);
	return self;
}

- (BOOL)mouseIn:(Point *)p flipped:(BOOL)aBool
{
	return NXMouseInRect((const NXPoint *)[p asCPointer], &rect, aBool);
}

- (BOOL)pointIn:(Point *)p
{
	return NXPointInRect((const NXPoint *)[p asCPointer], &rect);
}

- (BOOL)intersects:(Rect *)r
{
	return NXIntersectsRect(&rect, (const NXRect *)[r asCPointer]);
}

- (BOOL)contains:(Rect *)r
{
	return NXContainsRect(&rect, (const NXRect *)[r asCPointer]);
}

- (BOOL)equals:(Rect *)r
{
	return NXEqualRect(&rect, (const NXRect *)[r asCPointer]);
}

- (BOOL)isEmpty
{
	NXEmptyRect(&rect);
}

- clip
{
	NXRectClip(&rect);
	return self;
}

- fill
{
	NXRectFill(&rect);
	return self;
}

- erase
{
	NXEraseRect(&rect);
	return self;
}

- highlight
{
	NXHighlightRect(&rect);
	return self;
}

- drawButton:(Rect *)clipRect
{
	return self;
}

- drawGrayBezel:(Rect *)clipRect
{
	NXDrawGrayBezel(&rect, (const NXRect *)[clipRect asCPointer]);
	return self;
}

- drawGroove:(Rect *)clipRect
{
	NXDrawGroove(&rect, (const NXRect *)[clipRect asCPointer]);
	return self;
}

- drawWhiteBezel:(Rect *)clipRect
{
	NXDrawWhiteBezel(&rect, (const NXRect *)[clipRect asCPointer]);
	return self;
}

- drawFrame
{
	NXFrameRect(&rect);
	return self;
}

- drawFrameWithWidth:(float)width
{
	NXFrameRectWithWidth(&rect, width);
	return self;
}

@end
