
/*
	NSMovieView subclass to put an NSMovieView in an NSDrawer
	Original Source: <http://cocoa.karelia.com/AppKit_Classes/DrawerMovieView__Ov.m>
	(See copyright notice at <http://cocoa.karelia.com>)
*/

@interface DrawerMovieView : NSMovieView

// IMPORTANT: Call the method below when window activates/deactivates.  SEE WAY BELOW

- (void)parentWindowDidBecomeMain:(BOOL)didBecomeMain;

@end

@interface NSMovieView ( _superPrivate_ )

- (Rect) _convertToQDRect:(NSRect)rect;

@end


@implementation DrawerMovieView

/*"	Draw a background and frame.  But ignore the aRect parameter, which is the clipping rect, and just draw the whole bounds.
"*/

- (void)drawRect:(NSRect)aRect
{
	[[NSColor windowBackgroundColor] set];		// If I could get drawer background color, that would be better!
	[NSBezierPath fillRect:[self bounds]];
	[[NSColor grayColor] set];
	[NSBezierPath strokeRect:[self bounds]];
	[super drawRect:aRect];
}


// Override to handle 10.2.

- (Rect) _convertToQDRect:(NSRect)rect
{
	if (NSAppKitVersionNumber > NSAppKitVersionNumber10_1)
	{
		NSWindow *window = [self window];

		NSRect contentRect = [[window contentView] frame];
		Rect   qdRect;

		rect = [self convertRect:rect toView:[window contentView]];
		rect.origin.y = NSHeight(contentRect) - NSMaxY(rect);

		SetRect(&qdRect, (SInt16)NSMinX(rect), (SInt16)NSMinY(rect),
		  (SInt16)NSMaxX(rect), (SInt16)NSMaxY(rect));

		return qdRect;
	}
	else
	{
		return [super _convertToQDRect:rect];
	}
}

// Call from windowDidBecomeMain or windowDidResignMain.  Only does anything for 10.2.

- (void)parentWindowDidBecomeMain:(BOOL)didBecomeMain;
{
	if (NSAppKitVersionNumber > NSAppKitVersionNumber10_1)
	{
		WindowRef windowRef = [[self window] windowRef];  // windowRef for drawer window
	
		HiliteWindow(windowRef, didBecomeMain ? true : false);
	
		MCActivate([self movieController], windowRef, IsWindowHilited(windowRef));
		[self setNeedsDisplay:YES];
	}
}

@end


// In your window delegate, you should invoke parentWindowDidBecomeMain: to pass along the activation message.  For example:

- (void)windowDidBecomeMain:(NSNotification *)aNotification	// NSWindowDidBecomeMainNotification
{
	[myDrawerMovieView parentWindowDidBecomeMain:YES];
}

- (void)windowDidResignMain:(NSNotification *)aNotification	 // NSWindowDidResignMainNotification
{
	[myDrawerMovieView parentWindowDidBecomeMain:NO];
}