/***(PrefPanel.m)**************************************************************
*H* VDT's preference Panel class implmentation                V0.1, 19-MAY-92 *
*C* V0.4 19-MAY-92 Correct Current Preferences actions	    	    	--MDM *
*C* V0.1 26-APR-92 Initial version  	    	    	    	    	--MDM *
******************************************************************************/

#import <defaults.h>

#import <appkit/Button.h>
#import <appkit/Form.h>
#import <appkit/FormCell.h>
#import <appkit/Matrix.h>

#import "PrefPanel.h"
#import "VdtApplication.h"
#import "VdtWindow.h"
#import "VdtView.h"

@implementation PrefPanel

/******************************************************************************
* CLASS METHOD:- init 	    	    	    	    	    	    	      *
*   This method creates an instance of  a PrefPanel object and then loads the *
* NIB module for the Info panel.    	    	    	    	    	      *
******************************************************************************/
- init

{/* BEGIN- init */

/* Create the parent object */
[super init];

/* Load the NIB hierarchy: this also sets the outlet variable IVinfoPanel */
[NXApp loadNibSection:"PrefPanel.nib" owner:self];

/* Set the Preferences panel objects to the default settings */
[self displayCurrentPrefs:self];

return(self);
}/* END- init */



/******************************************************************************
* CLASS METHOD:- currentDefaults:  	    	    	    	    	      *
*   This method replaces certain preference panel fields according to the *
* state of the VDT window.
******************************************************************************/
- currentDefaults:sender

{   /* Local Variables */
    const char *appName;      	/* String pointer application name */
    NXRect  	conFrame;    	/* Frame rectangle of VdtWindow content view */
    char    	defStr[32]; 	/* String space to set default value */
    NXSize  	screenSize; 	/* Size of physical display screen */
    float   	vdtScale;   	/* Current VDT view scalling factor */
    id	        vdtWindow;    	/* ID of VdtWindow object */
    id	    	vdtView;    	/* ID of VdtView object */
    NXRect  	viewRect;    	/* Rectangle of VdtView */
    NXRect  	winFrame;    	/* Frame rectangle of VdtWindow */

/* BEGIN-currentDefaults: */

/* Get the name of the application for setting defaults */
appName = [NXApp appName];

/* Get current X,Y location of VDT window,  set PrefPanel fields */
vdtWindow = [NXApp getVdtWindow];
[vdtWindow getFrame:&winFrame];
[[vdtWindow contentView] getFrame:&conFrame];

sprintf(defStr,"%0.1f", winFrame.origin.x +conFrame.origin.x);
NXSetDefault(appName, VdtKdefNamWinLocX, defStr);
sprintf(defStr,"%0.1f", winFrame.origin.y + conFrame.origin.y);
NXSetDefault(appName, VdtKdefNamWinLocY, defStr);

/* Get the size of the current VDT view in nearest screen sizes */
vdtView = [NXApp getVdtView];
[vdtView getFrame:&viewRect];
[NXApp getDefaultScale:&vdtScale];
[NXApp getScreenSize:&screenSize];

sprintf(defStr,"%d %d",
        (int)(viewRect.size.width*vdtScale/screenSize.width),
    	(int)(viewRect.size.height*vdtScale/screenSize.height));
NXSetDefault(appName, VdtKdefNamSize, defStr);

#ifdef DEBUG
printf("vdtView(w,h): %f %f\n", viewRect.size.width, viewRect.size.height);
printf("vdtScale: %f\n", vdtScale);
#endif

/* Reset all the PrefPanel objects to the current defaults table */
[self displayCurrentPrefs:self];

return(self);
}/* END-currentDefaults: */



/******************************************************************************
* CLASS METHOD:- displayCurrentPrefs:  	    	    	    	    	      *
*   This method sets the  various  Preference panel fields to the current App *
* default values.   	    	    	    	    	    	    	      *
******************************************************************************/
- displayCurrentPrefs:sender

{   /* Local Variables */
    int	    defIntVal1;
    int	    defIntVal2;
    float   defFloatVal;

/* BEGIN-displayCurrentPrefs: */

/* Set BorderGray Text Field to the current default */
[NXApp getDefaultBorderGray:&defFloatVal];
[IVborderGrayFCell setFloatValue:defFloatVal];

/* Set DockHandling radio box to the current default */
[NXApp getDefaultDockHandling:&defIntVal1];
[IVdhRadioBox selectCellAt:defIntVal1 :0];

/* Set Icon Gravity radio box to the current default */
[NXApp  getDefaultIconGravity:&defIntVal1];
[IVigRadioBox selectCellAt:defIntVal1 :0];

/* Set Immobile switch to the current default */
[NXApp  getDefaultImmobile:&defIntVal1];
[IVimmobileSwitch setState:defIntVal1];

/* Set View Port Origin X,Y fields to the current default */
[NXApp getDefaultOrigin:&defIntVal1 :&defIntVal2];
[IVxOriginFCell  setIntValue:defIntVal1];
[IVyOriginFCell  setIntValue:defIntVal2];

/* Set Pan Delta field to the current default */
[NXApp getDefaultPanDelta:&defIntVal1];
[IVpanDeltaFCell setIntValue:defIntVal1];

/* Set Pan Grid field to the current default */
[NXApp getDefaultPanGrid:&defIntVal1];
[IVpanGridFCell setIntValue:defIntVal1];

/* Set Pan Jump switch to the current default */
[NXApp  getDefaultPanJump:&defIntVal1];
[IVpanJumpSwitch setState:defIntVal1];

/* Set Resize Bar switch to the current default */
[NXApp  getDefaultResizeBar:&defIntVal1];
[IVresizeBarSwitch setState:defIntVal1];

/* Set VDT Scale Factor field to the current default */
[NXApp getDefaultScale:&defFloatVal];
[IVscaleFCell setFloatValue:defFloatVal];

/* Set the VDT Size width and height fields to the current default */
[NXApp getDefaultSize:&defIntVal1 :&defIntVal2];
[IVwidthFCell  setIntValue:defIntVal1];
[IVheightFCell setIntValue:defIntVal2];

/* Set VDT Window radio box to the current default */
[NXApp getDefaultWindowLevel:&defIntVal1];
[IVwlRadioBox selectCellAt:defIntVal1 :0];

/* Set the Window Motion radio box to the current default */
[NXApp getDefaultWindowMotion:&defIntVal1];
[IVwmRadioBox selectCellAt:defIntVal1 :0];

/* Set VDT Window X,Y startup location fields to the current default */
[NXApp getDefaultWinLoc:&defIntVal1 :&defIntVal2];
[IVxWinLocFCell setIntValue:defIntVal1];
[IVyWinLocFCell setIntValue:defIntVal2];

return(self);
}/* END-displayCurrentPrefs: */



/******************************************************************************
* INSTANCE METHOD:- ExposeAndFocus   	    	    	    	    	      *
*   This method displays  the  InfoPanel  on  the top of the window stack and *
* gives it the keyboard focus.	    	    	    	    	    	      *
******************************************************************************/
- ExposeAndFocus:sender

{/* BEGIN- ExposeAndFocus */
[IVprefPanel makeKeyAndOrderFront:self];
return(self);
}/* END- ExposeAndFocus */



/******************************************************************************
* INSTANCE METHOD:- revertDefaults:   	    	    	    	    	      *
*   This method replaces all  the  preference  settings  with  the last saved *
* values.   	    	    	    	    	    	    	    	      *
******************************************************************************/
- revertDefaults:sender

{   /* Local Variables */
    const char *appName;      	/* String pointer to application name */
    int	    	defaultIndex;
    char       *defaultNames[15] =
    	    	    { VdtKdefNamBorderGray,   VdtKdefNamDockHandling,
    	    	      VdtKdefNamIconGravity,  VdtKdefNamImmobile,
    	    	      VdtKdefNamOrigin,	      VdtKdefNamPanDelta,
    	    	      VdtKdefNamPanGrid,      VdtKdefNamPanJump,
    	    	      VdtKdefNamResizeBar,    VdtKdefNamScale,
    	    	      VdtKdefNamSize,  	      VdtKdefNamWindowLevel,
    	    	      VdtKdefNamWindowMotion, VdtKdefNamWinLocX,
    	    	      VdtKdefNamWinLocY };
    const char *defStr;

/* BEGIN- revertDefaults: */
#ifdef DEBUG
printf("PrefPanel- revertDefaults:\n");
#endif
appName = [NXApp appName];

for (defaultIndex = 0; defaultIndex < 15; defaultIndex++)
    {
    defStr = NXReadDefault(appName, defaultNames[defaultIndex]);
    if ( defStr )
    	NXSetDefault(appName, defaultNames[defaultIndex], defStr);
    }

[NXApp validateAppDefaults];
[self displayCurrentPrefs:self];

return(self);
}/* END- revertDefaults: */



/******************************************************************************
* INSTANCE METHOD:- saveDefaults:   	    	    	    	    	      *
*   This method writes all  the  current  preference settings to the defaults *
* data base.	    	    	    	    	    	    	    	      *
******************************************************************************/
- saveDefaults:sender

{   /* Local Variables */
    int	    	defaultIndex;
    char       *defaultNames[15] =
    	    	    { VdtKdefNamBorderGray,   VdtKdefNamDockHandling,
    	    	      VdtKdefNamIconGravity,  VdtKdefNamImmobile,
    	    	      VdtKdefNamOrigin,	      VdtKdefNamPanDelta,
    	    	      VdtKdefNamPanGrid,      VdtKdefNamPanJump,
    	    	      VdtKdefNamResizeBar,    VdtKdefNamScale,
    	    	      VdtKdefNamSize,  	      VdtKdefNamWindowLevel,
    	    	      VdtKdefNamWindowMotion, VdtKdefNamWinLocX,
    	    	      VdtKdefNamWinLocY };
    const char *defStr;
    const char *appName;

/* BEGIN- saveDefaults: */
#ifdef DEBUG
printf("PrefPanel- saveDefaults:\n");
#endif
appName = [NXApp appName];

for (defaultIndex = 0; defaultIndex < 15; defaultIndex++)
    {
    defStr = NXGetDefaultValue(appName, defaultNames[defaultIndex]);
    NXWriteDefault(appName, defaultNames[defaultIndex], defStr);
    }

return(self);
}/* END- saveDefaults: */



/******************************************************************************
* INSTANCE METHOD:- setBorderGray:   	    	    	    	    	      *
*   This method handles changing the Border Gray Runtime Default.  	      *
******************************************************************************/
- setBorderGray:sender

{   /* Local Variables */
    float       gray;
    static char grayDefault[32];

/* BEGIN- setBorderGray: */
gray = [sender floatValueAt:0];

sprintf(grayDefault, "%0.4f", gray);
NXSetDefault([NXApp appName], VdtKdefNamBorderGray, grayDefault);

[NXApp validateAppDefaults];
[self displayCurrentPrefs:self];

[NXApp getDefaultBorderGray:&gray];
[[NXApp getVdtWindow] setVdtBorderGray:gray];

return(self);
}/* END- setBorderGray: */



/******************************************************************************
* INSTANCE METHOD:- setDockHandling:   	    	    	    	    	      *
*   This method handles changing the DockHandling Runtime Default.  	      *
******************************************************************************/
- setDockHandling:sender

{   /* Local Variables */
    int	dockHandling;

/* BEGIN- setDockHandling: */
dockHandling = [sender selectedTag];
if ( dockHandling == 0 )
    /* Set DockHandling to "Sticky" */
    NXSetDefault([NXApp appName], VdtKdefNamDockHandling, VdtKdefSetSticky);
else
    /* Set DockHandling to "Slippery" */
    NXSetDefault([NXApp appName], VdtKdefNamDockHandling, VdtKdefSetSlippery);

[[NXApp getVdtView] setViewDockHandling:dockHandling];

return(self);
}/* END- setDockHandling: */



/******************************************************************************
* INSTANCE METHOD:- setIconGravity:   	    	    	    	    	      *
*   This method handles changing the Icon Gravity Runtime Default.  	      *
******************************************************************************/
- setIconGravity:sender

{   /* Local Variables */
    const char *defValue;
    int	        gravity;

/* BEGIN- setIconGravity: */
gravity = [sender selectedTag];
if ( gravity == 0 )
    /* Set IconGravity to "TopLeft" */
    defValue = VdtKdefSetTopLeft;
else if ( gravity == 1 )
    /* Set IconGravity to "TopRight" */
    defValue = VdtKdefSetTopRight;
else if ( gravity == 2 )
    /* Set IconGravity to "BottomLeft" */
    defValue = VdtKdefSetBottomLeft;
else if ( gravity == 3 )
    /* Set IconGravity to "BottomRight" */
    defValue = VdtKdefSetBottomRight;
else
    /* Set icon gravity to registered default value  */
    defValue = VdtKdefValIconGravity;
NXSetDefault([NXApp appName], VdtKdefNamIconGravity, defValue);

return(self);
}/* END- setIconGravity: */



/******************************************************************************
* INSTANCE METHOD:- setImmobile:   	    	    	    	    	      *
*   This method handles changing the Immobile Startup Default.	    	      *
******************************************************************************/
- setImmobile:sender

{   /* Local Variables */
    int	state;

/* BEGIN- setImmobile: */
state = [sender state];

if ( state )
    /* The Immobile default has been toggled to "Yes" */
    NXSetDefault([NXApp appName], VdtKdefNamImmobile, VdtKdefSetYes);
else
    /* The Immobile default has been toggled to "No" */
    NXSetDefault([NXApp appName], VdtKdefNamImmobile, VdtKdefSetNo);

return(self);
}/* END- setImmobile: */



/******************************************************************************
* INSTANCE METHOD:- setOriginX:   	    	    	    	    	      *
*   This method handles the new entries in the Origin X Form Cell.  	      *
******************************************************************************/
- setOriginX:sender

{   /* Local Variables */
    static char     originDefault[32];
    int	    	    originX;
    int	    	    originY;

/* BEGIN- setOriginX: */
[NXApp getDefaultOrigin:&originX :&originY];
originX = [sender intValueAt:0];

sprintf(originDefault, "%d %d", originX, originY);
NXSetDefault([NXApp appName], VdtKdefNamOrigin, originDefault);

[NXApp validateAppDefaults];
[self displayCurrentPrefs:self];

return(self);
}/* END- setOriginX: */



/******************************************************************************
* INSTANCE METHOD:- setOriginY:   	    	    	    	    	      *
*   This method handles the new entries in the Origin Y Form Cell.  	      *
******************************************************************************/
- setOriginY:sender

{   /* Local Variables */
    static char     originDefault[32];
    int	    	    originX;
    int	    	    originY;

/* BEGIN- setOriginY: */
[NXApp getDefaultOrigin:&originX :&originY];
originY = [sender intValueAt:0];

sprintf(originDefault, "%d %d", originX, originY);
NXSetDefault([NXApp appName], VdtKdefNamOrigin, originDefault);

[NXApp validateAppDefaults];
[self displayCurrentPrefs:self];

return(self);
}/* END- setOriginY: */



/******************************************************************************
* INSTANCE METHOD:- setPanDelta:   	    	    	    	    	      *
*   This method handles changes to the Pan Delta Form Cell. 	    	      *
******************************************************************************/
- setPanDelta:sender

{   /* Local Variables */
    static char panDefault[32];
    int	    	panDelta;

/* BEGIN- setPanDelta: */
panDelta = [sender intValueAt:0];

sprintf(panDefault, "%d", panDelta);
NXSetDefault([NXApp appName], VdtKdefNamPanDelta, panDefault);

[NXApp validateAppDefaults];
[self displayCurrentPrefs:self];

[NXApp getDefaultPanDelta:&panDelta];
[[NXApp getVdtWindow] setVdtPanDelta:panDelta];

return(self);
}/* END- setPanDelta: */


/******************************************************************************
* INSTANCE METHOD:- setPanGrid:   	    	    	    	    	      *
*   This method handles changes to the Pan Grid Form Cell. 	    	      *
******************************************************************************/
- setPanGrid:sender

{   /* Local Variables */
    static char panDefault[32];
    int	    	panGrid;

/* BEGIN- setPanGrid: */
panGrid = [sender intValueAt:0];

sprintf(panDefault, "%d", panGrid);
NXSetDefault([NXApp appName], VdtKdefNamPanGrid, panDefault);

[NXApp validateAppDefaults];
[self displayCurrentPrefs:self];

[NXApp getDefaultPanGrid:&panGrid];
[[NXApp getVdtView] setVdtPanGrid:panGrid];

return(self);
}/* END- setPanGrid: */



/******************************************************************************
* INSTANCE METHOD:- setPanJump:   	    	    	    	    	      *
*   This method handles changes to the Pan Jump toggle switch. 	    	      *
******************************************************************************/
- setPanJump:sender

{   /* Local Variables */
    int	state;

/* BEGIN- setPanJump: */
state = [sender state];
if ( state )
    /* The PanJump default has been toggled to "Yes" */
    NXSetDefault([NXApp appName], VdtKdefNamPanJump, VdtKdefSetYes);
else
    /* The ResizeBar default has been toggled to "No" */
    NXSetDefault([NXApp appName], VdtKdefNamPanJump, VdtKdefSetNo);

[[NXApp getVdtView] setVdtPanJump:state];
return(self);
}/* END- setPanJump: */



/******************************************************************************
* INSTANCE METHOD:- setResizeBar:   	    	    	    	    	      *
*   This method handles changes to the Resize Bar toggle button.    	      *
******************************************************************************/
- setResizeBar:sender

{   /* Local Variables */
    int	state;

/* BEGIN- setResizeBar: */
state = [sender state];
if ( state )
    /* The ResizeBar default has been toggled to "Yes" */
    NXSetDefault([NXApp appName], VdtKdefNamResizeBar, VdtKdefSetYes);
else
    /* The ResizeBar default has been toggled to "No" */
    NXSetDefault([NXApp appName], VdtKdefNamResizeBar, VdtKdefSetNo);

return(self);
}/* END- setResizeBar: */



/******************************************************************************
* INSTANCE METHOD:- setScale:   	    	    	    	    	      *
*   This method handles changes to the Scale Startup Default setting.	      *
******************************************************************************/
- setScale:sender

{   /* Local Variables */
    float       scale;
    static char scaleDefault[32];

/* BEGIN- setScale: */
scale = [sender floatValueAt:0];

sprintf(scaleDefault, "%0.2f", scale);
NXSetDefault([NXApp appName], VdtKdefNamScale, scaleDefault);

[NXApp validateAppDefaults];
[self displayCurrentPrefs:self];

return(self);
}/* END- setScale: */



/******************************************************************************
* INSTANCE METHOD:- setSizeHeight:   	    	    	    	    	      *
*   This method 
******************************************************************************/
- setSizeHeight:sender

{   /* Local Variables */
    static char sizeDefault[32];
    int	    	sizeHeight;
    int	    	sizeWidth;

/* BEGIN- setSizeHeight: */
[NXApp getDefaultSize:&sizeWidth :&sizeHeight];
sizeHeight = [sender intValueAt:0];

sprintf(sizeDefault, "%d %d", sizeWidth, sizeHeight);
NXSetDefault([NXApp appName], VdtKdefNamSize, sizeDefault);

[NXApp validateAppDefaults];
[self displayCurrentPrefs:self];

return(self);
}/* END- setSizeHeight: */



/******************************************************************************
* INSTANCE METHOD:- setSizeWidth:   	    	    	    	    	      *
*   This method 
******************************************************************************/
- setSizeWidth:sender

{   /* Local Variables */
    static char sizeDefault[32];
    int	    	sizeHeight;
    int	    	sizeWidth;

/* BEGIN- setSizeWidth: */
[NXApp getDefaultSize:&sizeWidth :&sizeHeight];
sizeWidth = [sender intValueAt:0];

sprintf(sizeDefault, "%d %d", sizeWidth, sizeHeight);
NXSetDefault([NXApp appName], VdtKdefNamSize, sizeDefault);

[NXApp validateAppDefaults];
[self displayCurrentPrefs:self];

return(self);
}/* END- setSizeWidth: */



/******************************************************************************
* INSTANCE METHOD:- setWindowLevel:   	    	    	    	    	      *
*   This method changes the window tier of the VDT Window.
******************************************************************************/
- setWindowLevel:sender

{   /* Local Variables */
    char *defVal;
    int	  windowLevel;

/* BEGIN- setWindowLevel: */
windowLevel = [sender selectedTag];

if ( windowLevel == 0 )
    defVal = VdtKdefSetNormal;
else if ( windowLevel == 1 )
    defVal = VdtKdefSetFloating;
else if ( windowLevel == 2 )
    defVal = VdtKdefSetDock;
else if ( windowLevel == 3 )
    defVal = VdtKdefSetSubMenu;
else if ( windowLevel == 4 )
    defVal = VdtKdefSetMainMenu;
else
    defVal = VdtKdefValWindowLevel;

NXSetDefault([NXApp appName], VdtKdefNamWindowLevel, defVal);
[[NXApp getVdtWindow] setVdtWindowLevel:sender];

return(self);
}/* END- setWindowLevel: */



/******************************************************************************
* INSTANCE METHOD:- setWindowMotion:   	    	    	    	    	      *
*   This method 
******************************************************************************/
- setWindowMotion:sender

{   /* Local Variables */
    const char *defVal;
    int	    	windowMotion;

/* BEGIN- setWindowMotion: */
windowMotion = [sender selectedTag];
if ( windowMotion == 0 )
    defVal = VdtKdefSetDeferred;
else if ( windowMotion == 1 )
    defVal = VdtKdefSetImmediate;
else
    defVal = VdtKdefValWindowMotion;

NXSetDefault([NXApp appName], VdtKdefNamWindowMotion, defVal);
[[NXApp getVdtView] setViewWindowMotion:windowMotion];

return(self);
}/* END- setWindowMotion: */



/******************************************************************************
* INSTANCE METHOD:- setWinLocX:   	    	    	    	    	      *
*   This method 
******************************************************************************/
- setWinLocX:sender

{   /* Local Variables */
    static char xLocDefault[32];
    int	    	winLocX;

/* BEGIN-setWinLocX: */
winLocX = [sender intValueAt:0];

sprintf(xLocDefault, "%d", winLocX);
NXSetDefault([NXApp appName], VdtKdefNamWinLocX, xLocDefault);

[NXApp validateAppDefaults];
[self displayCurrentPrefs:self];

return(self);
}/* END-setWinLocX: */



/******************************************************************************
* INSTANCE METHOD:- setWinLocY:   	    	    	    	    	      *
*   This method 
******************************************************************************/
- setWinLocY:sender

{   /* Local Variables */
    static char yLocDefault[32];
    int	        winLocY;

/* BEGIN-setWinLocY: */
winLocY = [sender intValueAt:0];

sprintf(yLocDefault, "%d", winLocY);
NXSetDefault([NXApp appName], VdtKdefNamWinLocY, yLocDefault);

[NXApp validateAppDefaults];
[self displayCurrentPrefs:self];

return(self);
}/* END- setWinLocY: */

@end
