
#import "MiscFormFieldInspector.h"
#import "MiscFormFieldPalette.subproj/MiscFormField.h"
#import "MiscFormFieldPalette.subproj/MiscFormFieldCell.h"

int indexForVal(float arr[],int cnt,float val)
{	int i;
	for(i=0;i < cnt;i++) if(ABS(arr[i]-val)< 0.01) return i;
	return -1;
}

@implementation MiscFormFieldInspector

- init
{	char buf[MAXPATHLEN+1];
	[super init];
	[[NXBundle bundleForClass:[MiscFormFieldInspector class]]
					  getPath:buf 
				  forResource:"FormFieldInspector" 
					   ofType:"nib"];
	[NXApp loadNibFile:buf 
				 owner:self 
			 withNames:NO 
			  fromZone:[self zone]];
	return self;
}

- (BOOL)wantsButtons
{	return NO;
}

float grayTab[]={NX_BLACK,NX_DKGRAY,NX_LTGRAY,NX_WHITE,-1};


- ok:sender
{	MiscFormFieldCell *cell=[object cell];
	if(sender == theStringMatrix)
	{	switch([sender selectedIndex])
		{	case 0:
				[cell setMask:[sender stringValueAt:0]];
			break;
			case 1:
				[cell setModifyString:[sender stringValueAt:1]];
			break;
			case 2:
				[cell setValidString:[sender stringValueAt:2]];
			break;
		}
	} else if(sender == blankchar)
	{	[cell setBlank:[blankchar stringValue][0]];
	} else if(sender == completionChar)
	{	[object setCompletionChar:[completionChar stringValue][0]];
	} else if(sender == textGray)
	{	[cell setTextGray:grayTab[[[textGray selectedCell] tag]]];
    } else if(sender == backGray)
	{	[cell setBackgroundGray:grayTab[[[backGray selectedCell] tag]]];
    } else if(sender == border)
	{	switch([[border selectedCell] tag])
		{	case 0: [[cell setBordered:NO]  setBezeled:NO];
			break;
			case 1: [[cell setBezeled:NO] setBordered:YES];
			break;
			case 2: [[cell setBordered:NO] setBezeled:YES];
			break;
		}
    } else if(sender==options)	//[sender isKindOf:[ButtonCell class]])
	{
		switch([[sender selectedCell] tag])
		{	case 0:
				[object setEditable:[[sender selectedCell] state]];
				[self revert:sender];
			break;
			case 1:
				[object setSelectable:[[sender selectedCell] state]];
			break;
			case 2:
				[[object cell] setOverwrite:[[sender selectedCell] state]];
			break;
			case 3:
				[[object cell] setSpecialKeybindings:[[sender selectedCell] state]];
			break;
			case 4:
				[[object cell] setBeepWhenRight:[[sender selectedCell] state]];
			break;
			case 6:
				[[object cell] setOverwrite:[[sender selectedCell] state]];
			break;
		}
	} return [super ok:sender];
}
- revert:sender
{	char buf[2]={[[object cell] blank],0},completion[2]={[object completionChar],0};
	MiscFormFieldCell *cell=[object cell];
	[window disableFlushWindow];
	[theStringMatrix setStringValue:[cell mask] at:0];
	[theStringMatrix setStringValue:[cell modifyString] at:1];
	[theStringMatrix setStringValue:[cell validString] at:2];

	[blankchar setStringValue:buf];
	[completionChar setStringValue:completion];
	[textGray selectCellWithTag:indexForVal(grayTab,5,[cell textGray])];
	[backGray selectCellWithTag:indexForVal(grayTab,5,[cell backgroundGray])];

	if([cell isBezeled]) [border selectCellWithTag:2];
	else if([cell isBordered]) [border selectCellWithTag:1];
	else [border selectCellWithTag:0];

	[options clearSelectedCell],[options display];
	if([object isEditable])
		[ [options findCellWithTag:1] setEnabled:NO],
		[options selectCellWithTag:0];
	else [[options findCellWithTag:1] setEnabled:YES];
	if([object isSelectable]) [options selectCellWithTag:1];
	if([[object cell] overwrite]) [options selectCellWithTag:2];
	if([[object cell] specialKeybindings]) [options selectCellWithTag:3];
	if([[object cell] beepWhenRight]) [options selectCellWithTag:4];
	if([[object cell] escClearLine]) [options selectCellWithTag:6];
	[window reenableFlushWindow];
	return [super revert:sender];
}

@end
