
#import "CornerView.h"
#import "Thinker.h"

@implementation CornerView

- initFrame:(const NXRect *)frameRect
{
	[super initFrame:frameRect];
	image = [NXImage findImageNamed:"corner"];
	check = [NXImage findImageNamed:"x"];
	return self;
}

static NXPoint z[] = {{5,9},{32,9},{32,30},{5,30}};

- drawSelf:(const NXRect *)rects :(int)rectCount
{
	NXPoint zero = {0,0};
	[image composite:NX_SOVER toPoint:&zero];
	if (state) [check composite:NX_SOVER toPoint:&z[state-1]];
	return self;
}

- setState:(int)val
{
	state = val;
	[self display];
	return self;
}

- mouseDown:(NXEvent *)theEvent
{
	int i, tstate = 0;
	NXPoint p;
	NXRect r = {{0,0},{11,11}};
	int inCorner = 0;

	p = theEvent->location;
	[self convertPoint:&p fromView:nil];

	for (i=0; i<4; i++)
	{
		r.origin = z[i];
		if (NXPointInRect(&p,&r))
		{
			tstate = i+1;
			inCorner = 1;
			break;
		}
	}
	if (tstate == state) state = 0;
	else if (inCorner) state = tstate;

	[self display];
	[BSThinker() setSleepCorner:state];

	if (inCorner)
	{
		[self perform:@selector(saveState:)
			with:self
			afterDelay:5000
			cancelPrevious:YES];
	}

	return self;
}

- saveState:sender
{
	char str[10];
	sprintf(str,"%1d", state);
	NXWriteDefault("BackSpace", "hotCorner", str);
	return self;
}

@end















