/* DockBackgroundView.m created by fred on Wed 22-Sep-1999 */

#import "DockBackgroundView.h"

#import "MiniDockApplication.h"
#import "DockWindow.h"	//	Stupid

#define noVERBOSE

//	----------------------------------------------------------------------------

@implementation DockBackgroundView

- initWithFrame:(NSRect)aFrame
{	[super initWithFrame:aFrame];
	[self registerForDraggedTypes:[NSArray arrayWithObject:NSFilenamesPboardType]];
	return self;
}

//	----------------------------------------------------------------------------

- (void)dealloc
{	[self stopTracking];
	[super dealloc];
}

//	----------------------------------------------------------------------------

- (void)startTracking
{	if (itsfTracking)
		[self stopTracking];

	itsTrackingTag = [self addTrackingRect:[self bounds] owner:self userData:NULL assumeInside:itsfMouseInside];
	itsfTracking = YES;
}

//	----------------------------------------------------------------------------

- (void)stopTracking
{	[self removeTrackingRect:itsTrackingTag];
	itsfTracking = NO;
}

//	----------------------------------------------------------------------------

- (void)startCheckingForControl
{	[[self window] makeFirstResponder:self];
	[[self window] makeKeyWindow];
	[NSApp startTrackingModifierFlags:0];		//	Zero, so we get an event if flaga are already pressed
}

//	----------------------------------------------------------------------------

- (void)stopCheckingForControl
{	[NSApp stopTrackingModifierFlags];
}

@end

//	----------------------------------------------------------------------------

@implementation DockBackgroundView(NSDraggingDestination)

- (unsigned int)draggingEntered:(id <NSDraggingInfo>)sender
{
#ifdef VERBOSE
	NSLog( @"DRAGGING ENTERED" );
#endif
	[self doCommandBySelector:@selector(wantUncollapse:)];
	return NSDragOperationNone;
}

//	----------------------------------------------------------------------------

- (void)draggingExited:(id <NSDraggingInfo>)sender
{
#ifdef VERBOSE
	NSLog( @"DRAGGING EXITED" );
#endif
	[self doCommandBySelector:@selector(backgroundExited:)];
}

@end

//	----------------------------------------------------------------------------

@implementation DockBackgroundView(NSResponder_Overrides)

- (void)mouseEntered:(NSEvent *)anEvent
{
#ifdef VERBOSE
	NSLog( @"MOUSE ENTERED" );
#endif
	itsfMouseInside = YES;

	[self doCommandBySelector:@selector(backgroundEntered:)];
}

//	----------------------------------------------------------------------------

- (void)mouseExited:(NSEvent *)anEvent
{
#ifdef VERBOSE
	NSLog( @"MOUSE EXITED" );
#endif
	itsfMouseInside = NO;
	
	[self doCommandBySelector:@selector(backgroundExited:)];
}

//	----------------------------------------------------------------------------

- (void)flagsChanged:(NSEvent *)theEvent 
{
	#ifdef VERBOSE
		NSLog( @"FLAGS CHANGED" );
	#endif
		//	We don't want to send the events that we get when we are key, when the app is active and the user changes the flag while mouse is not in the view
	if (itsfMouseInside)
	{
		#ifdef VERBOSE
			NSLog( @"  AND MOUSE IS INSIDE" );
		#endif
		if ([theEvent modifierFlags] & NSControlKeyMask)
		{
		#ifdef VERBOSE
			NSLog( @"  SENDING CONTROL PRESSED" );
		#endif
			[self doCommandBySelector:@selector(controlPressed:)];
		}
	}
}

//	----------------------------------------------------------------------------

- (void)mouseDown:(NSEvent *)anEvent
{	[(DockWindow *)[self window] dragWindowFromEvent:anEvent view:self];
}

@end

//	----------------------------------------------------------------------------

@implementation DockBackgroundView(NSView_Overrides)

- (void)drawRect:(NSRect)aRect
{
	static NSRectEdge theSides[] = { NSMinXEdge, NSMaxYEdge, NSMaxXEdge, NSMinYEdge, NSMinXEdge, NSMaxYEdge, NSMaxXEdge, NSMinYEdge };
	static float theGrays[sizeof( theSides )/sizeof( theSides[0] )] = { 0, 0, 0, 0, 1, 1, 0.333, 0.333 };

//	[[NSColor orangeColor] set];
//	[[NSColor lightGrayColor] set];
//	NSRectFill( aRect );

	NSDrawTiledRects( [self bounds], aRect, theSides, theGrays, sizeof( theSides )/sizeof( theSides[0] ));
}

@end
