/* 
   IMModelView.m

   Model view used for moving, sizing, and editing of visual components.

   Copyright (C) 1996, 1997 Free Software Foundation, Inc.

   Author: Scott Christley <scottc@net-community.com>
   Date: October 1996
   
   This file is part of the mySTEP Library and is provided
   under the terms of the GNU Library General Public License.
*/ 

#include <AppKit/AppKit.h>

#include "IMModelWindow.h"
#include "IMModelView.h"


@implementation IMModelView

- (void) display
{
id e = [[(IMModelWindow *)window selectedElements] objectEnumerator];
NSView *o;
NSColor *c = [NSColor blackColor];

	[super display];

	NSLog(@"IMModelView display");

	[self lockFocus];

	while ((o = (NSView *)[e nextObject]))
		{
		NSRect r = [o frame];
		NSRect h = {{0,0},{6,6}};
		float mid;

		[c set];								// Display handles on corners

		h.origin.x = r.origin.x - 6;						// Bottom left
		h.origin.y = r.origin.y - 6;
		NSRectFill(h);

		h.origin.y += r.size.height + 6;					// Top left
		NSRectFill(h);

		h.origin.x += r.size.width + 7;						// Top right
		NSRectFill(h);

		h.origin.y -= r.size.height + 6;					// Bottom right
		NSRectFill(h);

		mid = r.size.width / 2;								// Bottom middle
		h.origin.x -= mid + 3;
		NSRectFill(h);

		h.origin.y += r.size.height + 6;					// Top middle
		NSRectFill(h);

		mid = r.size.height / 2;							// Left middle
		h.origin.x = r.origin.x - 6;
		h.origin.y = r.origin.y + mid - 3;
		NSRectFill(h);

		h.origin.x += r.size.width + 7;						// Right middle
		NSRectFill(h);
		}

	[self unlockFocus];
}

- (id) hitTestForHandle:(IMHandleType *)handle atPoint:(NSPoint)p
{
id e = [[(IMModelWindow *)window selectedElements] objectEnumerator];
NSView *o;
											// Loop through selected elements
	while ((o = (NSView *)[e nextObject]))	// which will have handles attached
		{
		NSRect r = [o frame];
		NSRect h = {{0,0},{4,4}};
		float mid;							// Determine if mouse point is 
											// within a handle
		h.origin.x = r.origin.x - 6;							// Bottom left
		h.origin.y = r.origin.y - 6;
		if ([self mouse: p inRect: h])
			{
			*handle = IMBottomLeftHandle;
			return o;
			}

		h.origin.y += r.size.height + 6;
		if ([self mouse: p inRect: h])							// Top left
			{
			*handle = IMTopLeftHandle;
			return o;
			}

		h.origin.x += r.size.width + 7;							// Top right
		if ([self mouse: p inRect: h])
			{
			*handle = IMTopRightHandle;
			return o;
			}

		h.origin.y -= r.size.height + 6;						// Bottom right
		if ([self mouse: p inRect: h])
			{
			*handle = IMBottomRightHandle;
			return o;
			}

		mid = r.size.width / 2;									// Bottom middl
		h.origin.x -= mid + 3;
		if ([self mouse: p inRect: h])
			{
			*handle = IMBottomMiddleHandle;
			return o;
			}

		h.origin.y += r.size.height + 6;						// Top middle
		if ([self mouse: p inRect: h])
			{
			*handle = IMTopMiddleHandle;
			return o;
			}

		mid = r.size.height / 2;								// Left middle
		h.origin.x = r.origin.x - 6;
		h.origin.y = r.origin.y + mid - 3;
		if ([self mouse: p inRect: h])
			{
			*handle = IMLeftMiddleHandle;
			return o;
			}

		h.origin.x += r.size.width + 7;							// Right middle
		if ([self mouse: p inRect: h])
			{
			*handle = IMRightMiddleHandle;
			return o;
		}	}

	*handle = IMNoHandle;

	return nil;
}

@end
