/* $Id: PrefsController.m,v 1.1 1996/10/12 14:30:34 vkyr Exp vkyr $ */

#import <appkit/appkit.h>
#import "PrefsController.h"
#import "SwitchView.h"

@implementation PrefsController

#define UNIT 0
#define WINDOW 1
#define OFF 0
#define ON 1

+ initialize
{
	// Make sure that self is a PrefsController class before setting
	// class initialization. This prevents subclasses from performing
	// reinitialization.
	if (self == [PrefsController class])
	{
		const char *appName = [NXApp appName];

		static NXDefaultsVector theDefaults =
			{
				{"Unit", "Cms"},
				{"Measure", "Round"},
				{"Window", "NO"},
				{NULL, NULL}
			};

		NXRegisterDefaults(appName, theDefaults);
	}
	return self;
}

- awakeFromNib
{
	const char *appName = [NXApp appName];
	const char *unitFlag, *measureFlag, *winFlag;

	unitFlag    = NXGetDefaultValue(appName, "Unit");
	measureFlag = NXGetDefaultValue(appName, "Measure");
	winFlag     = NXGetDefaultValue(appName, "Window");
		
	// Make sure Flags are not NULL before comparing them.
	
	if (unitFlag)
	{
		if (strcasecmp(unitFlag, "Inches") == 0)
			[unitPopUpButton setTitle:"Inches"];
		if (strcasecmp(unitFlag, "Cms") == 0)
			[unitPopUpButton setTitle:"Cms"];
		if (strcasecmp(unitFlag, "Points") == 0)
			[unitPopUpButton setTitle:"Points"];
		if (strcasecmp(measureFlag, "Picas") == 0)
			[unitPopUpButton setTitle:"Picas"];
	}
	
	if (measureFlag)
	{
		if (strcasecmp(measureFlag, "Round") == 0)
			[measureRadioButton selectCellWithTag:0];
		else
			[measureRadioButton selectCellWithTag:1];
	}
	
	if (winFlag)
	{
		if (strcasecmp(unitFlag, "YES") == 0)
			[windowRadioButton selectCellWithTag:0];
		else
			[windowRadioButton selectCellWithTag:1];
	}
	return self;
}


- setInches:sender
{
	const char *appName = [NXApp appName];
	NXWriteDefault(appName, "Unit", "Inches");
	
	return self;
}

- setCms:sender
{
	const char *appName = [NXApp appName];
	NXWriteDefault(appName, "Unit", "Cms");
	
	return self;
}

- setPoints:sender
{
	const char *appName = [NXApp appName];
	NXWriteDefault(appName, "Unit", "Points");
	
	return self;
}

- setPicas:sender
{
	const char *appName = [NXApp appName];
	NXWriteDefault(appName, "Unit", "Picas");
	
	return self;
}

- setWindow:sender
{
	const char *appName = [NXApp appName];
	switch ([[windowRadioButton selectedCell] tag])
	{
		case 0:	NXWriteDefault(appName, "Window", "YES");
				break;
		case 1:	NXWriteDefault(appName, "Window", "NO");
				break;
	}
	return self;
}

- setMeasurement:sender
{
	const char *appName = [NXApp appName];
	switch ([[measureRadioButton selectedCell] tag])
	{
		case 0:	NXWriteDefault(appName, "Measure", "Round");
				break;
		case 1:	NXWriteDefault(appName, "Measure", "Exact");
				break;
	}
	return self;
}


- (const char *)whatUnit
{
	const char *unitFlag;
	const char *appName = [NXApp appName];

	// The value in the database may be newer than the value in the 
	// registration table because of the Prefs panel; thus, update 
	// the value in the table to match the value in the database.
	NXUpdateDefault(appName, "Unit");
	
	// Get value from registration table now that it's been updated.
	unitFlag = NXGetDefaultValue(appName, "Unit");
	return (unitFlag);
}

- (BOOL)shouldRoundUnit
{
	const char *measureFlag;
	const char *appName = [NXApp appName];

	// The value in the database may be newer than the value in the 
	// registration table because of the Prefs panel; thus, update 
	// the value in the table to match the value in the database.
	NXUpdateDefault(appName, "Measure");
	
	// Get value from registration table now that it's been updated.
	measureFlag = NXGetDefaultValue(appName, "Measure");
	
	// Make sure measureFlag is not NULL before comparing it.
	if (measureFlag)
	{
		if (strcasecmp(measureFlag, "Round") == 0)
			return YES;
	}
	return NO;
}

- (BOOL)shouldShowWin
{
	const char *winFlag;
	const char *appName = [NXApp appName];

	// The value in the database may be newer than the value in the 
	// registration table because of the Prefs panel; thus, update 
	// the value in the table to match the value in the database.
	NXUpdateDefault(appName, "Window");
	
	// Get value from registration table now that it's been updated.
	winFlag = NXGetDefaultValue(appName, "Window");
	
	// Make sure winFlag is not NULL before comparing it.
	if (winFlag)
	{
		if (strcasecmp(winFlag, "YES") == 0)
			return YES;
	}
	return NO;
}


- displayPrefsPanel:sender
{
	if (!prefsPanel)
	{
		[NXApp loadNibSection:"Prefs.nib" owner:self withNames:NO];
		[switchView switchToView:unitOptGroup];
	}
	[prefsPanel makeKeyAndOrderFront:nil];
	return self;
}

- showAccessoryView:sender
{
	int index;
	id popUpList;

	// The popuplist is the button's target.
	popUpList = [popUpButton target];
	
	// Get the title of the selected item and then get the index 
	// of the title in the popuplist.
	index = [popUpList indexOfItem: [popUpList selectedItem]];
	switch(index) {
		case UNIT:
			[switchView switchToView:unitOptGroup];
			break;
		case WINDOW:
			[switchView switchToView:winOptGroup];
			break;
	}
	return self;
}

@end;
