//
//  ICeCoffEEMenuOutlineView.m
//  ICeCoffEE
//
//  Created by Nicholas Riley on 3/2/08.
//  Copyright 2008 Nicholas Riley. All rights reserved.
//

#import "ICeCoffEEMenuOutlineView.h"
#import <Carbon/Carbon.h>

@implementation ICeCoffEEMenuOutlineView

// undocumented, Leopard-only, only way I can figure out to bypass inter-cell navigation
- (BOOL)canFocusCell:(NSCell *)cell atTableColumn:(NSTableColumn *)tableColumn row:(int)row;
{
    return NO;
}

- (void)highlightSelectionInClipRect:(NSRect)clipRect;
{
    NSRange rows = [self rowsInRect: clipRect];
    unsigned maxRow = NSMaxRange(rows);

    HIThemeMenuItemDrawInfo drawInfo = {0, kThemeMenuItemHierBackground | kThemeMenuItemPopUpBackground, kThemeMenuSelected};
    if ([[self window] firstResponder] != self || [NSApp keyWindow] != [self window]) {
	NSLog(@"firstResponder %@ key %@ main %@ this %@", [[self window] firstResponder], [NSApp keyWindow], [NSApp mainWindow], [self window]);
	[super highlightSelectionInClipRect: clipRect];
	return;
    }
    
    for (unsigned row = rows.location ; row < maxRow ; ++row) {
	if (![self isRowSelected: row])
	    continue;
	
	NSRect rowRect = [self rectOfRow: row];
	rowRect.size.height += 2;
	if (NSIntersectsRect(rowRect, clipRect)) {
	    OSStatus err = HIThemeDrawMenuItem((HIRect *)&clipRect,
					       (HIRect *)&rowRect,
					       &drawInfo,  
					       (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort],
					       kHIThemeOrientationInverted,
					       (HIRect *)&rowRect);
	    if (err != noErr) {
		[super highlightSelectionInClipRect: clipRect];
		return;
	    }
	}
    }
}

@end
