//----------------------------------------------------------------------------------------------------
//
//	ColorBrowserInspector
//
//	Inherits From:		IBInspector
//
//	Declared In:		ColorBrowserInspector.h
//
//	Disclaimer
//
//		You may freely copy, distribute and reuse this software and its
//		associated documentation. I disclaim any warranty of any kind, 
//		expressed or implied, as to its fitness for any particular use.
//
//----------------------------------------------------------------------------------------------------
#import "ColorBrowserInspector.h"
#import "ColorBrowserCell.h"
#import "ColorBrowser.h"
#import <appkit/appkit.h>


@implementation ColorBrowserInspector

//----------------------------------------------------------------------------------------------------
//	Initialization and Freeing
//----------------------------------------------------------------------------------------------------
- init
{
    	char 	path[MAXPATHLEN+1];
    	id 		bundle;
		
    	[super init];
    	if (! (bundle = [NXBundle bundleForClass:[ColorBrowserInspector class]])) return nil;
    	if (! ([bundle getPath: path forResource:"ColorBrowserInspector" ofType:"nib"])) return nil;
       	[NXApp loadNibFile:path owner:self withNames:NO ]; 
	font = [Font newFont:"Helvetica" size:12 style:0 matrix:NX_FLIPPEDMATRIX];
	[[colorBrowser cellPrototype] setFont: font];
	[colorBrowser loadColumnZero];
    	return self;
}


- free
{
	if (colorBrowser) [colorBrowser free];
	if (colorPanel) [colorPanel free];
	if (horizontalScrolling) [horizontalScrolling free];
	if (verticalScrolling) [verticalScrolling free];
	if (options) [options free];
	if (font) [font free];
	return [super free];
}

	
//----------------------------------------------------------------------------------------------------
//	IB Inspector Methods
//----------------------------------------------------------------------------------------------------
- ok:sender
{
	if (sender == colorPanel)
		{
		[self setTextColor];
		}
	else if (sender == horizontalScrolling)
		{
		[self setHorizontalScrolling];
		}
	else if (sender == verticalScrolling)
		{
		[self setVerticalScrolling];
		}
	else if (sender == options)
		{
		[self setOptions];
		}

	[[NXApp activeDocument] touch];
    	return [super ok:sender];
}


- revert:sender
{
	[self revertTextColor];
	[self revertHorizontalScrolling];
	[self revertVerticalScrolling];
	[self revertOptions];
	return [super revert: sender];
}


- (BOOL)wantsButtons
{
    	return NO;
}


//----------------------------------------------------------------------------------------------------
//	Reverting Attributes
//----------------------------------------------------------------------------------------------------
- revertTextColor
{
	[[colorBrowser cellPrototype] setTextColor: [[object cellPrototype] textColor]];
	[colorPanel setColor: [[colorBrowser cellPrototype] textColor]];
	[colorBrowser loadColumnZero];
	return self;
}


- revertHorizontalScrolling
{
	[horizontalScrolling clearSelectedCell];
	if ([object isHorizontalScrollerEnabled])
		{
		[horizontalScrolling setState: 0  at: 0 :0];
		[horizontalScrolling setState: 1  at: 1 :0];
		[horizontalScrolling setState: 0  at: 2 :0];
		[[verticalScrolling findCellWithTag: 1] setEnabled: NO];
		}
	else if ([object areHorizontalScrollButtonsEnabled])
		{
		[horizontalScrolling setState: 0  at: 0 :0];
		[horizontalScrolling setState: 0  at: 1 :0];
		[horizontalScrolling setState: 1  at: 2 :0];
		[[verticalScrolling findCellWithTag: 1] setEnabled: YES];
		}
	else
		{
		[horizontalScrolling setState: 1  at: 0 :0];
		[horizontalScrolling setState: 0  at: 1 :0];
		[horizontalScrolling setState: 0  at: 2 :0];
		[[verticalScrolling findCellWithTag: 1] setEnabled: YES];
		}

	return self;
}


- revertVerticalScrolling
{
	NXRect	browserFrame;
	NXRect	columnFrame;
	NXRect	titleFrame;
	float		titleFudgeFactor = 0.0;

	[verticalScrolling clearSelectedCell];

	[object getFrame: &browserFrame];
	[object getFrame: &columnFrame  ofColumn: 0];
	[object getTitleFrame: &titleFrame  ofColumn: 0];
	if ( NX_HEIGHT(&titleFrame) > 1.0 )
		titleFudgeFactor = 2.0;

	if ([object isHorizontalScrollerEnabled])
		{
		[verticalScrolling setState: 1  at: 0 :0];
		[verticalScrolling setState: 0  at: 1 :0];
		[[horizontalScrolling findCellWithTag: 1] setEnabled: YES];
		}
	else if ( (NX_HEIGHT(&columnFrame) + NX_HEIGHT(&titleFrame) + titleFudgeFactor) == 			NX_HEIGHT(&browserFrame) )
		{
		[verticalScrolling setState: 1  at: 0 :0];
		[verticalScrolling setState: 0  at: 1 :0];
		[[horizontalScrolling findCellWithTag: 1] setEnabled: YES];
		}
	else
		{
		[verticalScrolling setState: 0  at: 0 :0];
		[verticalScrolling setState: 1  at: 1 :0];
		[[horizontalScrolling findCellWithTag: 1] setEnabled: NO];
		}

	return self;
}


- revertOptions
{
	[options setState: [object isMultipleSelectionEnabled] at: 0 :0];
	[options setState: [object isEmptySelectionEnabled] at: 1 :0];
	[options setState: [object isBranchSelectionEnabled] at: 2 :0];
	[options setState: [object columnsAreSeparated] at: 3 :0];
	[options setState: [object isTitled] at: 4 :0];
	[[options findCellWithTag: 3] setEnabled: NO];
	return self;
}


//----------------------------------------------------------------------------------------------------
//	Setting Attributes
//----------------------------------------------------------------------------------------------------
- setTextColor
{
	[colorBrowser setTextColor: [colorPanel color] inColumn:0];
	[(ColorBrowserCell*)[object cellPrototype] setTextColor: [colorPanel color]];
	return self;
}


- setHorizontalScrolling
{
	switch ([[horizontalScrolling selectedCell] tag])
		{
		case 0:
			[object setHorizontalScrollButtonsEnabled: NO];
			[object setHorizontalScrollerEnabled: NO];
			[[verticalScrolling cellAt: 1 :0] setEnabled: YES];
			break;
		case 2:
			[object setHorizontalScrollButtonsEnabled: YES];
			[object setHorizontalScrollerEnabled: NO];
			[[verticalScrolling cellAt: 1 :0] setEnabled: YES];
			break;
		case 1:
		default:
			[object setHorizontalScrollerEnabled: YES];
			[object setHorizontalScrollButtonsEnabled: NO];
			[[verticalScrolling cellAt: 1 :0] setEnabled: NO];
			break;
		}
	
	return self;
}

		
- setVerticalScrolling
{
	switch ([[verticalScrolling selectedCell] tag])
		{
		case 1:
			[object useScrollButtons: YES];
			[object useScrollBars: NO];
			[[horizontalScrolling cellAt: 1 :0] setEnabled: NO];
			break;
		case 0:
		default:
			[object useScrollBars: YES];
			[object useScrollButtons: NO];
			[[horizontalScrolling cellAt: 1 :0] setEnabled: YES];
			break;
		}
	
	return self;
}

	
- setOptions
{
	[object setMultipleSelectionEnabled: ([[options cellAt: 0 : 0] state] ? YES : NO)];
	[object setEmptySelectionEnabled: ([[options cellAt: 1 : 0] state] ? YES : NO)];
	[object setBranchSelectionEnabled: ([[options cellAt: 2 : 0] state] ? YES : NO)];
	[object separateColumns: ([[options cellAt: 3 : 0] state] ? YES : NO)];

	if ([[options cellAt: 4 : 0] state])
		{
		[object setTitled: YES];
		[[options cellAt: 3 :0] setState: 1];
		[[options cellAt: 3 :0] setEnabled: NO];
		}
	else
		{
		[object setTitled: NO];
		[[options cellAt: 3 :0] setEnabled: YES];
		}
		
	return self;
}


//----------------------------------------------------------------------------------------------------
//	Browser Methods
//----------------------------------------------------------------------------------------------------
- browserAction: sender
{		
	colorPanel = [NXColorPanel sharedInstance: YES];
	[[colorPanel setTarget: self] setAction: @selector (ok:)];
	[NXApp orderFrontColorPanel: self];
	[[colorBrowser matrixInColumn:0] selectCellAt:-1 :-1];
	return self;
}
					

- (int) browser: aBrowser  fillMatrix: aMatrix  inColumn: (int) aColumn
{
	[aMatrix renewRows: 1 cols:1];
	[[[aMatrix cellAt: 0 : 0] setStringValue: "Click here to set text color..."] setLeaf: YES];
	return 1;
}
	

@end		