//************************************************************************
//
//	OICNSTableHeaderView.m
//
//	by Juergen Moellenhoff <jm@oic.de>		
//
//
//	This code is supplied "as is" the author makes no warranty as to its 
//	suitability for any purpose.  This code is free and may be distributed 
//	in accordance with the terms of the:
//		
//			GNU GENERAL PUBLIC LICENSE
//			Version 2, June 1991
//			copyright (C) 1989, 1991 Free Software Foundation, Inc.
// 			675 Mass Ave, Cambridge, MA 02139, USA
//
//************************************************************************

#import "OICNSTableHeaderView.h"
#import "OICNSTableHeaderCell.h"
#import "OICNSTableColumn.h"
#import "OICNSTableView.h"

#import	<AppKit/AppKit.h>

@implementation OICNSTableHeaderView

//
// Private Methods
//
- (NSTableColumn *)_columnAtIndex:(unsigned int)index
{
	NSTableColumn	*column;

	NS_DURING
		column = [[[self tableView] tableColumns] objectAtIndex:index];
	NS_HANDLER
		column = nil;
	NS_ENDHANDLER

	return column;
}	

//
// Method from NSTableHeaderView. Catch the mouseDown event.
//
- (void)mouseDown:(NSEvent *)theEvent 
{
	NSPoint			point;
	int				aColumnPosition;
	NSRect			rect;
	NSTableColumn	*column;

	// Is it a resize operation?
	if (![[NSCursor currentCursor] isEqual:_resizeCursor])
	{
		// It it not a resize operation
		point = [theEvent locationInWindow];
		point = [self convertPoint:point fromView:nil];

		// Get the column
		if (((aColumnPosition = [self columnAtPoint:point]) >= 0) && 
			((column = [self _columnAtIndex:aColumnPosition]) != nil))
		{
			// Is the column and the headerCell a kind of my own classes?
			if ([column isKindOfClass:[OICNSTableColumn class]] && 
				[[column headerCell] isKindOfClass:[OICNSTableHeaderCell class]])
			{
				// Yes, it is...
                rect = [self headerRectOfColumn:aColumnPosition];

                rect.origin.x = rect.origin.x + rect.size.width - rect.size.height;
                rect.size.width = rect.size.height;

				// Mouse click on the NSButtonCell?
                if (NSPointInRect(point, rect))
                {
					// Yes, so switch the sort order...
                    NSComparisonResult	order;

                    order = [(OICNSTableColumn *)column ordering];
                    switch(order)
                    {
                    case NSOrderedAscending:
                        order = NSOrderedDescending;
                        break;
                    case NSOrderedDescending:
                        order = NSOrderedSame;
                        break;
                    case NSOrderedSame:
                    default:
                        order = NSOrderedAscending;
                        break;
                    }

					// Set the new sort order (force a refresh of the display)
                    [(OICNSTableColumn *)column setOrdering:order];

                    return;
                }
			}
		}
	}

	// Don't catch the mouseDown event...
	[super mouseDown:theEvent];
}


@end
