/* Generated by Project Builder */

#import <appkit/appkit.h>
#import "ShapeView.h"

@implementation ShapeView

- initFrame:(NXRect *)frameRect
{
	[super initFrame:frameRect];
	angle = 360;
	xScale = 1;
	yScale = 1;
	x = 50;
	y = 50;
	return self;
}

- translate:sender
{
	// sender is a matrix: thus, obtain
	// cell by asking the matrix
	// for the selected cell
	id cell = [sender selectedCell];
	int tag = [cell tag];

	switch(tag)
		{
		case 0:
			x = [cell floatValue];
			break;
		case 1:
			y = [cell floatValue];
			break;
		}
	// display view with updated values
	[self display];
	return self;
}

- scale:sender
{
	// sender is a matrix: thus, obtain
	// cell by asking the matrix
	// for the selected cell
	id cell = [sender selectedCell];
	int tag = [cell tag];

	switch(tag)
		{
		case 0:
			xScale = [cell floatValue];
			break;
		case 1:
			yScale = [cell floatValue];
			break;
		}
	// display view with updated values
	[self display];
	return self;
}

- rotate:sender
{
	angle = [sender floatValue];
	[self display];
	return self;
}

- erase
{
	// &bounds contains the view's location
	// and size expressed in its own coordinate
	// system: by drawing in bounds, the view
	// avoids unnecessary drawing
	PSsetgray(NX_DKGRAY);
	NXRectFill(&bounds);
	return self;
}

- mouseDown:(NXEvent *)theEvent
{
	char buffer[30];

	// round co-ordinates to 1 digit
	// after decimal point
	// print co-ordinates in window co-ordinates
	sprintf(buffer, "%.1f", theEvent->location.x);
	[[downWindowMatrix cellAt:0 :0]
		setStringValue:buffer];
	sprintf(buffer, "%.1f", theEvent->location.y);
	[[downWindowMatrix cellAt:1 :0]
		setStringValue:buffer];

	// convert from window co-ordinates to
	// view co-ordinates
	[self convertPoint:&theEvent->location
		fromView:nil];

	// lock focus so graphics commands
	// are applied to this view
	[self lockFocus];
	// erase old instance drawing
	PSnewinstance();
	// start new instance drawing to draw directly
	// to the window
	PSsetinstance(YES);
		// select font otherwise output
		// will be upside down
		PSselectfont("Times-Roman", 16.0);
		// print co-ordinates on view
		PSmoveto(theEvent->location.x,
			theEvent->location.y);
		sprintf(buffer, "%.1f, %.1f",
			theEvent->location.x,
			theEvent->location.y);
		// set color to light gray
		PSsetgray(NX_LTGRAY);
		// show the string
		PSshow(buffer);		
	PSsetinstance(NO);

	// balance lockFocus with unlockFocus
	[self unlockFocus];
	return self;
}

- mouseUp:(NXEvent *)theEvent
{
	char buffer[40];
	sprintf(buffer, "%.1f", theEvent->location.x);
	[[upWindowMatrix cellAt:0 :0]
		setStringValue:buffer];
	sprintf(buffer, "%.1f", theEvent->location.y);
	[[upWindowMatrix cellAt:1 :0]
		setStringValue:buffer];

	// convert from window co-ordinates to
	// view co-ordinates
	[self lockFocus];
	[self convertPoint:&theEvent->location
		fromView:nil];

	// don't use PSnewinstance(), since we don't
	// want to erase the drawing from mouseDown:
	// start new instance drawing to draw directly
	// to the window
	PSsetinstance(YES);
		// select font otherwise output
		// will be upside down
		PSselectfont("Times-Roman", 16.0);
		// print co-ordinates on view
		PSmoveto(theEvent->location.x,
			theEvent->location.y);
		sprintf(buffer, "%.1f, %.1f",
			theEvent->location.x,
			theEvent->location.y);
		// set color to black
		PSsetgray(NX_BLACK);
		// show the string
		PSshow(buffer);		
	PSsetinstance(NO);

	// balance lockFocus with unlockFocus
	[self unlockFocus];

	return self;
}

-(BOOL)acceptsFirstMouse
{
	// allows the view to use the first 
	// mouse click that activates the window
	return YES;
}

@end
