/****************************/
/*			PREFS.C			*/
/****************************/

/*	PangeaVR - 3D accelerated plug-in for viewing panoramas.

	©2005 Pangea Software, Inc.  All rights reserved.
	Pangea Software, PangeaVR and various logos are trademarks of Pangea Software, Inc.
	
	Written by Brian Greenstone,   brian@pangeasoft.net	
	
	This program and source code is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
	
	IMPORTANT:  The purpose in releasing this source code is to encourage others
				To port this program to other platforms, and to make improvments to it.
				However, to prevent things from getting out of control with lots of
				different versions of PangeaVR floating around the internet,
				Pangea Software, Inc. prohibits the public distribution of any versions
				of PangeaVR without prior approval from Pangea Software, Inc.
				
				Should you make any improvements to this source code and wish to
				have those changes rolled into an official release then you'll
				need to contact Pangea Software to have your modifications adapted.				
*/


/****************************/
/*    EXTERNALS             */
/****************************/

extern	MyInstanceType		gMyInstances[];
extern	IBNibRef 			gNibs;


/****************************/
/*    PROTOTYPES            */
/****************************/

static void ExtractPrefsData(void);
static pascal OSStatus PrefsDialogEventHandler(EventHandlerCallRef myHandler, EventRef event, void* userData);


/****************************/
/*    CONSTANTS             */
/****************************/


/*********************/
/*    VARIABLES      */
/*********************/

#ifdef TARGET_OS_MAC
short	gPrefsFolderVRefNum;
long	gPrefsFolderDirID;

static WindowRef		gPrefsWindow = nil;
static EventHandlerUPP	gPrefsWinEvtHandler;
#endif


PrefsType				gPrefs;


/************************* INIT DEFAULT PREFS **************************/

void InitDefaultPrefs(void)
{
short	i;

	gPrefs.compressionMode = TEXTURE_COMP_MODE_HTML;
	gPrefs.dragFrictionMode = DRAGFRIC_MODE_HTML;

	for (i = 0; i < 100; i++)
		gPrefs.unused[i] = 0;
}



#ifdef TARGET_OS_MAC


/******************** LOAD PREFS **********************/
//
// Load in standard preferences
//

OSErr LoadPrefs(void)
{
OSErr		iErr;
short		refNum;
FSSpec		file;
long		count;
				
					
				/* OPEN THE PREFS FILE */
				
	FSMakeFSSpec(gPrefsFolderVRefNum, gPrefsFolderDirID, "\p:PangeaVR:Preferences", &file);
	iErr = FSpOpenDF(&file, fsRdPerm, &refNum);	
	if (iErr)											// if no prefs file then just bail
		return(iErr);

				/* LOAD THE PREFS DATA */
				
	count = sizeof(gPrefs);
	iErr = FSRead(refNum, &count, &gPrefs);				// read data from file
	if (iErr)
	{
		FSClose(refNum);			
		return(iErr);
	}
	
	FSClose(refNum);			
	
	return(noErr);	
}



/******************** SAVE PREFS **********************/

void SavePrefs(void)
{
FSSpec				file;
OSErr				iErr;
short				refNum;
long				count;
		
				/* CREATE BLANK FILE */
				
	FSMakeFSSpec(gPrefsFolderVRefNum, gPrefsFolderDirID, "\p:PangeaVR:Preferences", &file);
	FSpDelete(&file);															// delete any existing file
	iErr = FSpCreate(&file, '    ', 'Pref', smSystemScript);					// create blank file
	if (iErr)
		return;

				/* OPEN FILE */
					
	iErr = FSpOpenDF(&file, fsRdWrPerm, &refNum);
	if (iErr)
	{
		FSpDelete(&file);
		return;
	}
		
				/* WRITE DATA */

	count = sizeof(gPrefs);
	FSWrite(refNum, &count, &gPrefs);	
	FSClose(refNum);
	
}




#pragma mark -



/*********************** DO PREFERENCES DIALOG ********************************/
//
//

void DoPrefsDialog(int instanceNum)
{
ControlID 			idControl;
ControlRef 			control;
OSStatus		iErr;
EventHandlerRef	ref;
EventTypeSpec	list[] = {   { kEventClassWindow, kEventWindowClose },
                             { kEventClassWindow, kEventWindowDrawContent },
                             { kEventClassControl, kEventControlClick },
                             { kEventClassCommand,  kEventProcessCommand } };
    
    
			/* DRAW SCENE WITH CONTROLL BUTTON HILITED */
			
	gMyInstances[instanceNum].hilitePrefsIcon = true;
	OGL_DrawScene(instanceNum);
	gMyInstances[instanceNum].hilitePrefsIcon = false;
	
	
    		/***************/
    		/* INIT DIALOG */
    		/***************/
    
		/* CREATE WINDOW FROM THE NIB */
				
    iErr = CreateWindowFromNib(gNibs, CFSTR("Prefs"),	&gPrefsWindow);
	if (iErr)
	{
		printf("DoPrefsDialog: CreateWindowFromNib failed!");
		return;
	}


		/****************/
		/* SET CONTROLS */
		/****************/
		
		
			/* GET TEXTURE COMPRESSION SETTING */

	idControl.signature = 'Tcom';
	idControl.id 		= 0; 
	GetControlByID(gPrefsWindow, &idControl, &control);
	SetControlValue(control, gPrefs.compressionMode);
	
	
			/* GET DRAG FRICTION SETTING */

	idControl.signature = 'Dfri';
	idControl.id 		= 0; 
	GetControlByID(gPrefsWindow, &idControl, &control);
	SetControlValue(control, gPrefs.dragFrictionMode);


			/*****************/
			/* HANDLE EVENTS */
			/*****************/
			
			/* CREATE NEW WINDOW EVENT HANDLER */
				
    gPrefsWinEvtHandler = NewEventHandlerUPP(PrefsDialogEventHandler);
    InstallWindowEventHandler(gPrefsWindow, gPrefsWinEvtHandler, GetEventTypeCount (list), list, 0, &ref);
    ShowWindow(gPrefsWindow);



			/* START THE EVENT LOOP */
			
    RunAppModalLoopForWindow(gPrefsWindow);


			/***********/
			/* CLEANUP */
			/***********/
			
	DisposeWindow(gPrefsWindow);						// dispose of the window
	gPrefsWindow = nil;
	DisposeEventHandlerUPP(gPrefsWinEvtHandler);
	
	OGL_DrawScene(instanceNum);							// refresh view
	
}


/********************** PREFS DIALOG EVENT HANDLER ************************/

static pascal OSStatus PrefsDialogEventHandler(EventHandlerCallRef myHandler, EventRef event, void* userData)
{
#pragma unused (myHandler, userData)
OSStatus			result = eventNotHandledErr;
HICommand 			command;


	switch(GetEventKind(event))
	{				
				/* PROCESS COMMAND */
				
		case	kEventProcessCommand:
				GetEventParameter (event, kEventParamDirectObject, kEventParamHICommand, NULL, sizeof(command), NULL, &command);
				switch(command.commandID)
				{							
							/* HIT CANCEL BUTTON */
							
					case	kHICommandCancel:
							QuitAppModalLoopForWindow(gPrefsWindow);
							break;
							
							/* HIT OK BUTTON */
							
					case	kHICommandOK:
							ExtractPrefsData();
							QuitAppModalLoopForWindow(gPrefsWindow);
							break;	
							
				}
				break;
    }
    return (result);
}



/****************************** EXTRACT PREFS DATA **********************************/
//
// This checks the dialog settings and sets them in our prefs
//

static void ExtractPrefsData(void)
{
ControlID 			idControl;
ControlRef 			control;


			/* GET TEXTURE COMPRESSION SETTING */

	idControl.signature = 'Tcom';
	idControl.id 		= 0; 
	GetControlByID(gPrefsWindow, &idControl, &control);
	gPrefs.compressionMode = GetControlValue(control);
	
	
			/* GET DRAG FRICTION SETTING */

	idControl.signature = 'Dfri';
	idControl.id 		= 0; 
	GetControlByID(gPrefsWindow, &idControl, &control);
	gPrefs.dragFrictionMode = GetControlValue(control);
	
	
	
				/* SAVE THESE PREFS */
				
	SavePrefs();
}

#endif   // TARGET_OS_MAC





#pragma mark -















