//  FmsDefaultController
//
//  Copyright 1994  Flying Monkey Software
//                  8424 Todd Creek Cr. WestChester OH 45069
//
//  This code is distributed as shareware. If you like it or use it, please
//  send an appropriate amount of money (ten dollars is suggested) to the
//  above address.
//
//  Modification is cool by us, as well as inclusion of this code in a
//  for-profit product in compiled form. However, Flying Monkey Software
//  retains all copyrights; you are hereby not given permission to resell
//  this code in code form without written consent of Flying Monkey.

#import <defaults/defaults.h>

#import "FmsDefaultController.h"
#import "FmsStringPair.h"

@implementation FmsDefaultController

- init
	{
	[super init];
	defaultList = [[List alloc] init];
	return self;
	}
	
	
- free
	{
	[[defaultList freeObjects] free];
	if (database != NULL)
		{
		free(database);
		database = NULL;
		}
	return [super free];
	}


- registerDefaults
	{
	struct _NXDefault *vector;
	int i, I;
	id element;
	
	I = [defaultList count];
	vector = (struct _NXDefault *)malloc(sizeof(struct _NXDefault)*(I+1));
	for (i = 0; i < I; i++)
		{
		element = [defaultList objectAt:i];
		vector[i].name = [element first];
		vector[i].value = [element second];
		}
	vector[i].name = NULL;
	vector[i].value = NULL;

	NXRegisterDefaults(database,vector);
	
	free(vector);
	return self;
	}
	
	
- remove:(char *)name
	{
	NXRemoveDefault(database,name);
	return self;
	}


- (const char *)database
	{
	return (const char *)database;
	}


- setDatabase:(const char *)theDatabase
	{
	if (database != NULL)
		{
		free(database);
		database = NULL;
		}
	strcpy((database = malloc(strlen(theDatabase)+1)),theDatabase);
	return self;
	}


// strings

- default:(char *)name toStringValue:(char *)theString
	{
	[defaultList addObject:[[FmsStringPair alloc] initFirst:name
			second:theString]];
	return self;
	}


- (const char *)stringValue:(char *)name
	{
	return NXGetDefaultValue(database,name);
	}


- setStringValue:(char *)name to:(const char *)theString
	{
	NXWriteDefault(database,name,theString);
	return self;
	}


// ints

- default:(char *)name toIntValue:(int)theInt
	{
	char value[64];
	[defaultList addObject:[[FmsStringPair alloc] initFirst:name
			second:[self convertIntValue:theInt to:value]]];
	return self;
	}


- (int)intValue:(char *)name
	{
	int value;
	sscanf(NXGetDefaultValue(database,name),"%d",&value);
	return value;
	}


- (char *)convertIntValue:(int)theInt to:(char *)value
	{
	sprintf(value,"%d",theInt);
	return value;
	}
	
	
- setIntValue:(char *)name to:(int)theInt
	{
	char value[64];
	[self convertIntValue:theInt to:value];
	NXWriteDefault(database,name,value);
	return self;
	}


// floats

- default:(char *)name toFloatValue:(float)theFloat
	{
	char value[64];
	[defaultList addObject:[[FmsStringPair alloc] initFirst:name
			second:[self convertFloatValue:theFloat to:value]]];
	return self;
	}


- (float)floatValue:(char *)name
	{
	float value;
	sscanf(NXGetDefaultValue(database,name),"%f",&value);
	return value;
	}


- (char *)convertFloatValue:(float)theFloat to:(char *)value
	{
	sprintf(value,"%f",theFloat);
	return value;
	}


- setFloatValue:(char *)name to:(float)theFloat
	{
	char value[64];
	[self convertFloatValue:theFloat to:value];
	NXWriteDefault(database,name,value);
	return self;
	}


// points

- default:(char *)name toPointValue:(NXPoint *)thePoint
	{
	char value[64];
	[defaultList addObject:[[FmsStringPair alloc] initFirst:name
			second:[self convertPointValue:thePoint to:value]]];
	return self;
	}


- (NXPoint *)get:(NXPoint *)point pointValue:(char *)name
	{
	if (point != NULL)
		sscanf(NXGetDefaultValue(database,name),"%f %f",&(point->x),&(point->y));
	return point;
	}


- (char *)convertPointValue:(NXPoint *)thePoint to:(char *)value
	{
	sprintf(value,"%f %f",thePoint->x,thePoint->y);
	return value;
	}


- setPointValue:(char *)name to:(NXPoint *)thePoint
	{
	char value[64];
	[self convertPointValue:thePoint to:value];
	NXWriteDefault(database,name,value);
	return self;
	}


// sizes

- default:(char *)name toSizeValue:(NXSize *)theSize
	{
	char value[64];
	[defaultList addObject:[[FmsStringPair alloc] initFirst:name
			second:[self convertSizeValue:theSize to:value]]];
	return self;
	}


- (NXSize *)get:(NXSize *)size sizeValue:(char *)name
	{
	if (size != NULL)
		sscanf(NXGetDefaultValue(database,name),"%f %f",
				&(size->width),&(size->height));
	return size;
	}


- (char *)convertSizeValue:(NXSize *)theSize to:(char *)value
	{
	sprintf(value,"%f %f",theSize->width,theSize->height);
	return value;
	}


- setSizeValue:(char *)name to:(NXSize *)theSize
	{
	char value[64];
	[self convertSizeValue:theSize to:value];
	NXWriteDefault(database,name,value);
	return self;
	}


// rects

- default:(char *)name toRectValue:(NXRect *)theRect
	{
	char value[128];
	[defaultList addObject:[[FmsStringPair alloc] initFirst:name
			second:[self convertRectValue:theRect to:value]]];
	return self;
	}


- (NXRect *)get:(NXRect *)rect rectValue:(char *)name
	{
	if (rect != NULL)
		sscanf(NXGetDefaultValue(database,name),"%f %f %f %f",
				&(rect->origin.x),&(rect->origin.y),
				&(rect->size.width),&(rect->size.height));
	return rect;
	}


- (char *)convertRectValue:(NXRect *)theRect to:(char *)value
	{
	sprintf(value,"%f %f %f %f",theRect->origin.x,theRect->origin.y,
			theRect->size.width,theRect->size.height);
	return value;
	}


- setRectValue:(char *)name to:(NXRect *)theRect
	{
	char value[64];
	[self convertRectValue:theRect to:value];
	NXWriteDefault(database,name,value);
	return self;
	}


// colors

- default:(char *)name toColorValue:(NXColor)theColor
	{
	char value[64];
	[defaultList addObject:[[FmsStringPair alloc] initFirst:name
			second:[self convertColorValue:theColor to:value]]];
	return self;
	}


- (NXColor)colorValue:(char *)name
	{
	float red, green, blue, alpha;
	sscanf(NXGetDefaultValue(database,name),
			"%f %f %f %f",&red,&green,&blue,&alpha);
	return NXConvertRGBAToColor(red,green,blue,alpha);
	}

	
- (char *)convertColorValue:(NXColor)theColor to:(char *)value
	{
	float red, green, blue, alpha;
	NXConvertColorToRGBA(theColor,&red,&green,&blue,&alpha);
	sprintf(value,"%f %f %f %f",red,green,blue,alpha);
	return value;
	}
	

- setColorValue:(char *)name to:(NXColor)theColor
	{
	char value[64];
	[self convertColorValue:theColor to:value];
	NXWriteDefault(database,name,value);
	return self;
	}

@end
