
/* 	PreferenceManager.m
	created by Robert Vasvari - July 1994
*/

#import "STSwapBoxManager.h"
#import "STUtil.h"

#import "PreferenceManager.h"


#define RESIZE_CONSTR_WARNING "The change in size constraints will take effect next time a document is opened or resized"
 
@implementation PreferenceManager

- awakeFromNib
{	
	[super awakeFromNib];
	
	/* for all matrices, turn off autodisplays here */
	[openEmptyMatrix setAutodisplay:NO];

	[panel display];
	return self;
}

- loadDataFor:(int)boxTag
{
const char *p;
/* 	general purpose method to display a box specified by the tag
   	in the preferences panel 
	for each control in the box:
		   1: get the current value from the defaults into
		      an instance variable
		   2: make the control display that value
 */
	switch(boxTag)
	{ case 0:
		p=getDefaultWithErrorCheck("WindowSize");
		if(p)
		 { sscanf(p,"%d %d", &winWidth, &winHeight);
		   [windowWidthText setIntValue:winWidth];
		   [windowHeightText setIntValue:winHeight];
		 }
		debugfile=(char *)getDefaultWithErrorCheck("DEBUGFILE");
		if(debugfile)
  		 { [debugFileText setStringValue:debugfile];
		 }

	    break;
	  case 1:
	    openEmptyTag=BoolValueForDefault("NewEmptyDocument");
		[openEmptyMatrix selectCellWithTag:openEmptyTag];
	    break;

	  default:fprintf(stderr,"loadDataFor: bad boxTag:%d\n",boxTag);
	}
	return self;
}

- dataChanged:sender
{
BOOL change=NO;
	
	/* this method figures out if there is any change in the currently
	   selected box and enables/disables the set/revert buttons.
	   change=(currentvalueOfControl==storedValueOfControl)
	 */
	[panel disableFlushWindow];
	switch([self currentBoxTag])
	 { case 0: 
	     change=( (winWidth!=[windowWidthText intValue]) ||
		          (winHeight!=[windowHeightText intValue]) ||
				  strcmp(debugfile,[debugFileText stringValue]) );
		 break;
	   case 1:  
	     change=(openEmptyTag!=[[openEmptyMatrix selectedCell] tag]);  
		 break;
	   default: break;
	 }

	if(change)
	  { if(![revertButton isEnabled]) [revertButton setEnabled:YES];
		if(![setButton isEnabled]) [setButton setEnabled:YES];
		[panel setDocEdited:YES];
	  }
	else
 	  {	if([revertButton isEnabled]) [revertButton setEnabled:NO];
		if([setButton isEnabled]) [setButton setEnabled:NO];
		[panel setDocEdited:NO];
	  }
	[panel reenableFlushWindow];
	[panel flushWindow];
	return self;
}

- set:sender
{
char buf[BUFSIZE]="";
register const char *a=[NXApp appName];
	/* for each control:
		1: set instance variable to current value.
		2: save current value to the defaults database.
	 */
	[panel disableFlushWindow];
	switch([self currentBoxTag])
	 { case 0: 
		   winWidth=[windowWidthText intValue];
		   winHeight=[windowHeightText intValue];
		   sprintf(buf,"%d %d", winWidth, winHeight);
		   NXWriteDefault(a,"WindowSize",buf);
		   
		   NXWriteDefault(a,"DEBUGFILE",[debugFileText stringValue]);
		   debugfile=(char *)getDefaultWithErrorCheck("DEBUGFILE");
		   break;
	   case 1: 
	       openEmptyTag=[[openEmptyMatrix selectedCell] tag];
		   NXWriteDefault(a,"NewEmptyDocument",
		     openEmptyTag ? "YES" : "NO");
			   break;
	   default:fprintf(stderr,"set: bad tag:%d\n", [self currentBoxTag]);
	 }
 	if([revertButton isEnabled]) [revertButton setEnabled:NO];
	if([setButton isEnabled]) [setButton setEnabled:NO];
	[panel setDocEdited:NO];
	[panel reenableFlushWindow];
	[panel flushWindow];
	return self;
}


@end

