#ifdef lint
static char *SCCSid = "%W%	(NCSA)	%G%";
#endif
/*
*    prefs.c
*	 by Gaige B. Paulsen
****************************************************************************
*	Part of NCSA Telnet for the Macintosh								   *
*                                                                          *
*      Uses	:															   *
*      TCP/IP kernel for NCSA Telnet                                       *
*      by Tim Krauskopf                                                    *
*	   with Macintosh code by Gaige B. Paulsen						  	   *
*                                                                          *
*      National Center for Supercomputing Applications                     *
*      152 Computing Applications Building                                 *
*      605 E. Springfield Ave.                                             *
*      Champaign, IL  61820                                                *
*                                                                          *
*                                                                          *
****************************************************************************
*
*    Preferences reading,writing, and manipulating code.
*
*	Called by:
*		menu.c
*		maclook.c
*/

#include <StdIO.h>
#include <String.h>

#include <Controls.h>
#include <Dialogs.h>
#include <Memory.h>
#include <Resources.h>

#include "prefs.h"
#include "configrec.h"
#include "maclook.h"
#include "menu.h"
#include "event.h"

#ifdef MPW
#include "mpw.h"
#endif MPW

#define	DLOGOk		1
#define DLOGCancel	2

#define	PrefDLOG	700		/* The Preferences Dialog		---------------- */
#define PrefOutline 3		/*   outline for OK */
#define PrefCTt		5		/*   Copy table threshold */
#define PrefCMDkey	6		/*   Command keys */
#define PrefSave	7		/*	 Save the Preferences */
#define PrefBinary	8		/*   MacBinary active by default */
#define PrefBReset	9		/*   Reset MacBinary to default at Beginning of FTP? */
#define PrefOPTkey  10		/*	 Remap option key to control */
#define PrefTitle	11		/* 	 This is just a title */
#define PrefDClose	12		/* 	 Don't close the window on "Close" */
#define PrefTMap	13		/*	 Shall we map the tilde? */
#define PrefStag    14		/* Staggered Windows? */
#define PrefBlink   15		/*   BYU 2.4.11 - Blink the cursor? */
#define PrefBlockCursor   		16	/* BYU 2.4.11 - Display a block cursor? */
#define PrefUnderscoreCursor	17	/* BYU 2.4.11 - Display an underscore cursor? */
#define PrefVerticalCursor		18	/* BYU 2.4.11 - Display a vertical line cursor? */
#define PrefsysKCHR 			19	/*   BYU 2.4.18 - Use system KCHR? */

extern SysEnvRec theWorld;		/* BYU 2.4.18 - System Environment record */

extern int whichMenus;		/* From Menu.c - which menu set is active */
extern int MacBinary;		/* From Menu.c - is MacBinary active ? */
extern int SettingsFile;	/* From Maclook.c - where is our resource file */
extern int mapTilde;		/* From Event.c - are we currently mapping the tilde */
extern short cursorBlink;	/* BYU 2.4.11 - From Event.c, are we blinking? */
extern short cursorType;	/* BYU 2.4.11 - From Event.c, what kind of cursor? */

pascal void
	VersionNumber(),		/* Version number display UI code */
	OutlineItem();			/* Outline Item UI code */
pascal int
	modalProc();			/* Keep-alive proc */

/*
 *  SavePrefs()	- puts the preferences record into the current resource file
 *
 */
void SavePrefs
  (
	void
  )
{
	NCSAPrefs *ppref, **hpref;

	if (SettingsFile == -1)
		return;				/* Can't get it if there is no settings file */

	UseResFile(SettingsFile);

	hpref = (NCSAPrefs **) Get1Resource('NPRF',1);

	if (hpref==0L) {
		(Handle) hpref = NewHandle( (long) sizeof(NCSAPrefs));
		addresource((Handle) hpref, 'NPRF', 1, "Preferences");
		}
	else		/* The current handle may be too small, so fix it */
		if ( ((int) GetHandleSize((Handle) hpref)) < sizeof(NCSAPrefs))
			ReallocHandle((Handle) hpref, (long) sizeof(NCSAPrefs));

	HLock((Handle) hpref);
		ppref = *hpref;
		*ppref = Prefs;
	HUnlock((Handle) hpref);

	ChangedResource((Handle) hpref);
	UpdateResFile(SettingsFile);
}

void PromptPrefs
  (
	void
  )
{
	DialogPtr dtemp;
	short dItem;
	int tildeMap,dontclose, optKeys, cmdKeys,tempi,stag,sysKCHR,blink,curs;	/* BYU 2.4.18 */
	Rect dBox;
	Str255 temp, *tempp;
	Handle CTtString, saveButton, CMDkeyCtrl, OPTkeyCtrl,
			DCloseCtrl, MacBResetCtrl, MacBinaryCtrl, TMapCtrl, stagHndl,	/* BYU 2.4.11 */
			sysKCHRCtrl,blinkCtrl, curs1Ctrl, curs2Ctrl, curs3Ctrl;			/* BYU 2.4.18 */
	int MacBReset;

	tempp = (Str255 *) &temp;

	dtemp=GetNewDialog( PrefDLOG, (Ptr) 0L, (WindowPtr) -1L);	/* Bring on the box */
	UItemAssign( dtemp, PrefOutline , (int (*)()) OutlineItem);

	if (SettingsFile == -1) {
		GetDItem( dtemp, PrefSave, &dItem, &saveButton, &dBox);
		HiliteControl((ControlHandle) saveButton, 255);
		}
	GetDItem( dtemp, PrefCTt, &dItem, &CTtString, &dBox);
	GetDItem( dtemp, PrefCMDkey, &dItem, &CMDkeyCtrl, &dBox);
	GetDItem( dtemp, PrefBinary, &dItem, &MacBinaryCtrl, &dBox);
	GetDItem( dtemp, PrefBReset, &dItem, &MacBResetCtrl, &dBox);
	GetDItem( dtemp, PrefOPTkey, &dItem, &OPTkeyCtrl, &dBox);
	GetDItem( dtemp, PrefDClose, &dItem, &DCloseCtrl, &dBox);
	GetDItem( dtemp, PrefTMap,   &dItem, &TMapCtrl, &dBox);
	GetDItem( dtemp, PrefStag,   &dItem, &stagHndl, &dBox);
	GetDItem( dtemp, PrefsysKCHR,  &dItem, &sysKCHRCtrl, &dBox);		/* BYU 2.4.18 */
	GetDItem( dtemp, PrefBlink,  &dItem, &blinkCtrl, &dBox);			/* BYU 2.4.11 */
	GetDItem( dtemp, PrefBlockCursor,      &dItem, &curs1Ctrl, &dBox);	/* BYU 2.4.11 */
	GetDItem( dtemp, PrefUnderscoreCursor, &dItem, &curs2Ctrl, &dBox);	/* BYU 2.4.11 */
	GetDItem( dtemp, PrefVerticalCursor,   &dItem, &curs3Ctrl, &dBox);	/* BYU 2.4.11 */

	sprintf((char *) tempp, "%d", Prefs.CTthresh);	/* Make it editable */
	setitext( CTtString, (char *) tempp );	/* Install it in the dialog */
	SelIText( dtemp, PrefCTt, 0, 32767);

	if (theWorld.systemVersion < 0x0700)						/* BYU 2.4.18 */
		SetCtlValue((ControlHandle) OPTkeyCtrl, Prefs.optKeys);	/* BYU 2.4.18 - Set up the Check box */
	else {														/* BYU 2.4.18 */
		SetCtlValue((ControlHandle) OPTkeyCtrl, 0);				/* BYU 2.4.18 - Set up the Check box */
		HiliteControl((ControlHandle) OPTkeyCtrl, 255);			/* BYU 2.4.18 */
	}															/* BYU 2.4.18 */

	optKeys = Prefs.optKeys;
	SetCtlValue((ControlHandle) CMDkeyCtrl, Prefs.cmdKeys);/* Set up the Check box */
	cmdKeys = Prefs.cmdKeys;
	SetCtlValue((ControlHandle) MacBinaryCtrl, Prefs.MacBinary);/* Set up the Check box */
	MacBinary = Prefs.MacBinary;
	SetCtlValue((ControlHandle) MacBResetCtrl, Prefs.MacBReset);/* Set up the Check box */
	MacBReset = Prefs.MacBReset;
	SetCtlValue((ControlHandle) DCloseCtrl, Prefs.dontclose);/* Set up the Check box */
	dontclose = Prefs.dontclose;
	SetCtlValue((ControlHandle) TMapCtrl, Prefs.mapTilde);/* Set up the Check box */
	tildeMap = Prefs.mapTilde;
	SetCtlValue((ControlHandle) stagHndl, Prefs.stag);/* Set up the Check box */
	stag = Prefs.stag;
	SetCtlValue((ControlHandle) sysKCHRCtrl, Prefs.systemKCHR);	/* BYU 2.4.18 */
	sysKCHR = Prefs.systemKCHR;									/* BYU 2.4.18 */
	SetCtlValue((ControlHandle) blinkCtrl, Prefs.cursorBlink);	/* BYU 2.4.11 */
	blink = Prefs.cursorBlink;									/* BYU 2.4.11 */
	switch (Prefs.cursorType) {									/* BYU 2.4.11 */
		case 0:													/* BYU 2.4.11 */
			SetCtlValue((ControlHandle) curs1Ctrl, 1);			/* BYU 2.4.11 */
			curs = 0;											/* BYU 2.4.11 */
			break;												/* BYU 2.4.11 */
		case 1:													/* BYU 2.4.11 */
			SetCtlValue((ControlHandle) curs2Ctrl, 1);			/* BYU 2.4.11 */
			curs = 1;											/* BYU 2.4.11 */
			break;												/* BYU 2.4.11 */
		case 2:													/* BYU 2.4.11 */
			SetCtlValue((ControlHandle) curs3Ctrl, 1);			/* BYU 2.4.11 */
			curs = 2;											/* BYU 2.4.11 */
			break;												/* BYU 2.4.11 */
		default:												/* BYU 2.4.11 */
			SetCtlValue((ControlHandle) curs1Ctrl, 1);			/* BYU 2.4.11 */
			curs = 0;											/* BYU 2.4.11 */
			break;												/* BYU 2.4.11 */
	}															/* BYU 2.4.11 */


	dItem=0;								/* initially no hits */
	while((dItem>2 && dItem !=PrefSave)
				   || (dItem==0)) {					/* While we are in the loop */
		ModalDialog((ModalFilterProcPtr) modalProc,&dItem); /* and do our duty to the box */
		if (dItem == PrefBinary) {
			MacBinary=!MacBinary;
			SetCtlValue((ControlHandle) MacBinaryCtrl, MacBinary);
			}
		if (dItem == PrefTMap) {
			tildeMap=!tildeMap;
			SetCtlValue((ControlHandle) TMapCtrl, tildeMap);
			}
		if (dItem == PrefDClose) {
			dontclose=!dontclose;
			SetCtlValue((ControlHandle) DCloseCtrl, dontclose);
			}
		if (dItem == PrefBReset) {
			MacBReset=!MacBReset;
			SetCtlValue((ControlHandle) MacBResetCtrl, MacBReset);
			}
		if ((dItem == PrefOPTkey) && (theWorld.systemVersion < 0x0700)) {	/* BYU 2.4.18 */
			optKeys=!optKeys;
			SetCtlValue((ControlHandle) OPTkeyCtrl, optKeys);
			}
		if (dItem == PrefCMDkey) {
			cmdKeys=!cmdKeys;
			SetCtlValue((ControlHandle) CMDkeyCtrl, cmdKeys);
			}
		if (dItem == PrefStag) {
			stag =!stag;
			SetCtlValue((ControlHandle) stagHndl, stag);
			}
		if (dItem == PrefsysKCHR) {									/* BYU 2.4.18 */
			sysKCHR =!sysKCHR;										/* BYU 2.4.18 */
			SetCtlValue((ControlHandle) sysKCHRCtrl, sysKCHR);		/* BYU 2.4.18 */
			}														/* BYU 2.4.18 */
		if (dItem == PrefBlink) {									/* BYU 2.4.11 */
			blink =!blink;											/* BYU 2.4.11 */
			SetCtlValue((ControlHandle) blinkCtrl, blink);			/* BYU 2.4.11 */
			}														/* BYU 2.4.11 */
		if (dItem == PrefBlockCursor) {								/* BYU 2.4.11 */
			curs = 0;												/* BYU 2.4.11 */
			SetCtlValue((ControlHandle) curs1Ctrl, 1);				/* BYU 2.4.11 */
			SetCtlValue((ControlHandle) curs2Ctrl, 0);				/* BYU 2.4.11 */
			SetCtlValue((ControlHandle) curs3Ctrl, 0);				/* BYU 2.4.11 */
			}														/* BYU 2.4.11 */
		if (dItem == PrefUnderscoreCursor) {						/* BYU 2.4.11 */
			curs = 1;												/* BYU 2.4.11 */
			SetCtlValue((ControlHandle) curs1Ctrl, 0);				/* BYU 2.4.11 */
			SetCtlValue((ControlHandle) curs2Ctrl, 1);				/* BYU 2.4.11 */
			SetCtlValue((ControlHandle) curs3Ctrl, 0);				/* BYU 2.4.11 */
			}														/* BYU 2.4.11 */
		if (dItem == PrefVerticalCursor) {							/* BYU 2.4.11 */
			curs = 2;												/* BYU 2.4.11 */
			SetCtlValue((ControlHandle) curs1Ctrl, 0);				/* BYU 2.4.11 */
			SetCtlValue((ControlHandle) curs2Ctrl, 0);				/* BYU 2.4.11 */
			SetCtlValue((ControlHandle) curs3Ctrl, 1);				/* BYU 2.4.11 */
			}														/* BYU 2.4.11 */

		}

	if (dItem==DLOGCancel) {
			DisposDialog( dtemp);
			return;
			}

	getitext( CTtString, (char *) tempp);
	sscanf((char *) tempp, "%d", &tempi);
	Prefs.CTthresh  = tempi;

	Prefs.cmdKeys=cmdKeys;					
	Prefs.MacBinary=MacBinary;
	Prefs.MacBReset=MacBReset;
	Prefs.optKeys=optKeys;
	Prefs.dontclose=dontclose;
	Prefs.mapTilde =tildeMap;
	Prefs.stag = stag;
	Prefs.systemKCHR = sysKCHR;		/* BYU 2.4.18 */
	Prefs.cursorBlink = blink;		/* BYU 2.4.11 */
    Prefs.cursorType = curs;		/* BYU 2.4.11 */

	if (theWorld.systemVersion < 0x0700) {	/* BYU 2.4.18 */
		if (optKeys)
			optionKeys();				/* Turn it on.... */
		else 
			optionKeysOff();			/* Turn it off.... */
	}										/* BYU 2.4.18 */

	mapTilde = tildeMap;			/* Change the real variable */
	cursorBlink = blink;			/* BYU 2.4.11 */
	cursorType = curs;				/* BYU 2.4.11 */

	switchMenus( cmdKeys);
	whichMenus=cmdKeys;

	if (dItem == PrefSave)
		SavePrefs();
	DisposDialog( dtemp);
}

/*
 *	GetPrefs()	- gets the current preferences from the 'NPRF' 1 resource.
 *			assumes RSRC file has already been opened.  Returns 0 if everything
 *			is wonderful and 1 if the resource doesn't exist or isn't the right
 *			version.
 *
 */
int GetPrefs
  (
	void
  )
{
	NCSAPrefs *ppref, **hpref;
	
	Prefs.cmdKeys = whichMenus;	/* when we have a blank file, override default with config.tel */

	if (SettingsFile == -1)
		return(-1);				/* Can't get it if there is no settings file */

	UseResFile( SettingsFile);
	hpref=(NCSAPrefs **)GetResource('NPRF',1);		/* Get the resource */

	if (hpref==0L)
		return(1);									/* Doesn't exist */

	ppref = *hpref;									/* BYU 2.4.20 - Get the pointer */
	if (ppref->Version < PREF_Version)								/* BYU 2.4.18 */
		SetHandleSize((Handle) hpref,(size_t) PrefLength[PREF_Version]);	/* BYU 2.4.18 MPW */

	HLock((Handle) hpref);
		if (ppref->Version < PREF_Version)
			movmem( ppref, &Prefs, PrefLength[ ppref->Version]);	/* Coerce by using known lengths */
		else 	
			Prefs = *ppref;											/* Get the data */

		Prefs.Version = PREF_Version;					/* We are now current */
	HUnlock((Handle) hpref);

	/* Set everything up that way.... */

	if (theWorld.systemVersion < 0x0700) {	/* BYU 2.4.18 */
		if (Prefs.optKeys)
			optionKeys();				/* Turn it on.... */
		else 
			optionKeysOff();			/* Turn it off.... */
	} else									/* BYU 2.4.19 */
		Prefs.optKeys = 0;					/* BYU 2.4.19 */

	switchMenus( Prefs.cmdKeys);
	whichMenus=Prefs.cmdKeys;		/* Exported Variables */
	MacBinary=Prefs.MacBinary;
	mapTilde = Prefs.mapTilde;			/* Change the real variable */
	cursorBlink = Prefs.cursorBlink;	/* BYU 2.4.11 */
	cursorType = Prefs.cursorType;		/* BYU 2.4.11 */

	return(0);
}