/****************************************************************************
 *   Copyright (C) 2004 by Arboretum Systems, Inc.                          *
 *   Technical administrator: Alain Mazy, hav@mazy.be                       *
 *   Lead program designer: Georges Jaroslaw, gjaro@arboretum.com           *
 *                                                                          *
 *   HyperEngine-AV is a video, audio and text editor that can be used to   *
 *   record, import, edit, process and arrange together video, sound and    *
 *   text. These are all achieved in the program's free-form document       *
 *   window, with no limitations to the number of tracks.                   *
 *   The purpose of this program is to provide to users at all levels of    *
 *   expertise a readily accessible tool enabling them to create movies,    *
 *   slide shows or presentations.                                          *
 *                                                                          *
 *   HyperEngine-AV itself is free software. However, plug-ins that can be  *
 *   used as add-ons to HyperEngine-AV may or may not be free software, at  *
 *   the discretion of each plug-in developer and as specified by these     *
 *   plug-ins' respective licenses.                                         *
 *   You can redistribute HyperEngine-AV itself and/or modify it under the  *
 *   terms of the GNU General Public License as published by the Free       *
 *   Software Foundation; either version 2 of the License, or (at your      *
 *   option) any later version.                                             *
 *                                                                          *
 *   HyperEngine-AV is distributed in the hope that it will be useful, but  *
 *   WITHOUT ANY WARRANTY; without even the implied warranty of             *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
 *   GNU General Public License for all applicable details.                 *
 *                                                                          *
 *   You should have received a copy of the GNU General Public License      *
 *   along with this program; if not, write to:                             *
 *   Free Software Foundation, Inc.,                                        *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.              *
 ****************************************************************************/
#import "MIBlueWindowView.h"
#import "MIBlueWindowController.h"

@implementation MIBlueWindowView

- (void)awakeFromNib
{
	fCursorImage = [[NSImage alloc] initWithSize:NSMakeSize(16,16)];

	[fCursorImage lockFocus];
		[[NSColor blueColor] set];
		[NSBezierPath fillRect:NSMakeRect(0,0,16,16)];

		[[NSColor whiteColor] set];
		[NSBezierPath strokeLineFromPoint:NSMakePoint(7.5,0) toPoint:NSMakePoint(7.5,16)];
		[NSBezierPath strokeLineFromPoint:NSMakePoint(0, 7.5) toPoint:NSMakePoint(16,7.5)];
	[fCursorImage unlockFocus];
	
	fCursor = [[NSCursor alloc] initWithImage:fCursorImage hotSpot:NSMakePoint(8,8)];
	
	[self addCursorRect:[self bounds] cursor:fCursor];
}

- (void)drawRect:(NSRect)aRect
{
	[[NSColor blueColor] set];
	[NSBezierPath fillRect:[self bounds]];
	[self drawKnob];
}

- (void)drawKnob
{
	[fCursorImage compositeToPoint:fKnobPosition operation:NSCompositeCopy];
}

-(BOOL)acceptsFirstMouse:(NSEvent *)theEvent
{
	// this function is used to tell that the view receives the first 'mousedown' event
	return YES;
}

- (void)mouseDown:(NSEvent*)theEvent
{
	[self sendValuesToController:[self convertPoint:[theEvent locationInWindow] fromView:nil]];
}

- (void)mouseDragged:(NSEvent*)theEvent
{
	[self sendValuesToController:[self convertPoint:[theEvent locationInWindow] fromView:nil]];
}

- (void)mouseUp:(NSEvent*)theEvent
{
	[self sendValuesToController:[self convertPoint:[theEvent locationInWindow] fromView:nil]];
}

- (void)sendValuesToController:(NSPoint)mouseLoc
{
	float 		HPos, VPos;
	NSRect		myBounds;
	NSRect		redrawRect;
	
	myBounds = [self bounds];
	
	if ( mouseLoc.x > NSMaxX(myBounds) )
		mouseLoc.x = NSMaxX(myBounds);
	
	if ( mouseLoc.x < 0 )
		mouseLoc.x = 0;
		
	if ( mouseLoc.y > NSMaxY(myBounds) )
		mouseLoc.y = NSMaxY(myBounds);
	
	if ( mouseLoc.y < 0 )
		mouseLoc.y = 0;
		
	redrawRect = RectWithPoints(mouseLoc, fKnobPosition);
	redrawRect = NSInsetRect(redrawRect,-16, -16);
	[self setNeedsDisplayInRect:redrawRect];
	
	fKnobPosition = TranslateNSPoint(mouseLoc,NSMakePoint( -8, -8));
	
	VPos = mouseLoc.y/NSHeight(myBounds);
	HPos = mouseLoc.x/NSWidth(myBounds);
	
	[fBlueWindowController newBlueWindowVPos:VPos];
	[fBlueWindowController newBlueWindowHPos:HPos];
}

@end
