#import "DrawView.h"
#import "IconCell.h"
#import "RectangleCell.h"

@implementation DrawView

- initFrame:(const NXRect *) newFrame
	{
	id returnval=[super initFrame:newFrame];
	// initialize the cell list
	cell_list=[[List alloc] init];
	
	// Make four members of cell list, two icons, two rectangles
	[cell_list addObject:[[[IconCell alloc] init] setView:self]];
	[cell_list addObject:[[[IconCell alloc] init] setView:self]];
	[cell_list addObject:[[[RectangleCell alloc] init] setView:self]];
	[cell_list addObject:[[[RectangleCell alloc] init] setView:self]];

	return returnval;
	}

- drawSelf:(const NXRect*) rects:(int) rectCount
	{
	int x;
	int max=[cell_list count];
	
	// erase entire view
	PSsetgray(NX_LTGRAY);
	NXRectFill(&bounds);
	
	// tell each cell to draw itself
	for (x=0;x<max;x++)
		{
		[[cell_list objectAt:x] drawSelf:rects:rectCount];
		}
	return self;
	}

- mouseDown:(NXEvent*) theEvent
	{
	int x;
	int max=[cell_list count];
	
	// add mouseDragged as a valid item in the view
	
	[window addToEventMask:NX_MOUSEDRAGGEDMASK];
	
	// deselect old object(s)
	for (x=0;x<max;x++)
		{
		[[cell_list objectAt:x] deselect];		
		}	
	
	// tell each cell to select itself if hit
	for (x=0;x<max;x++)
		{
		if ([[cell_list objectAt:x] hit:theEvent])
			{
			[[cell_list objectAt:x] select];
			[[cell_list objectAt:x] mouseDown:theEvent];
			break;
			}
		}
	// redisplay view
	[self display];
	return self;
	}

- mouseDragged:(NXEvent*) theEvent
	{
	int x;
	int max=[cell_list count];
	
	// tell each cell to move if selected
	for (x=0;x<max;x++)
		{
		if ([[cell_list objectAt:x] isSelected])
			{
			[[cell_list objectAt:x] mouseDragged:(NXEvent*) theEvent];
			break;
			}
		}
	// redisplay view
	[self display];
	return self;
	}

- mouseUp:(NXEvent*) theEvent
	{
	int x;
	int max=[cell_list count];
	
	// tell each cell to stop moving itself if selected
	for (x=0;x<max;x++)
		{
		if ([[cell_list objectAt:x] isSelected])
			{
			[[cell_list objectAt:x] mouseUp:(NXEvent*) theEvent];
			break;
			}
		}
	// redisplay view
	[self display];
	return self;
	}

@end