Retired Document
Important: This sample code may not represent best practices for current development. The project may use deprecated symbols and illustrate technologies and techniques that are no longer recommended.
source/Preferences.c
/* |
File: Preferences.c |
Description:Code that displays the preferences dialog and reads and writes the preferences |
file. |
Author: MC |
Copyright: © Copyright 1999-2000 Apple Computer, Inc. All rights reserved. |
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Computer, Inc. |
("Apple") in consideration of your agreement to the following terms, and your |
use, installation, modification or redistribution of this Apple software |
constitutes acceptance of these terms. If you do not agree with these terms, |
please do not use, install, modify or redistribute this Apple software. |
In consideration of your agreement to abide by the following terms, and subject |
to these terms, Apple grants you a personal, non-exclusive license, under AppleÕs |
copyrights in this original Apple software (the "Apple Software"), to use, |
reproduce, modify and redistribute the Apple Software, with or without |
modifications, in source and/or binary forms; provided that if you redistribute |
the Apple Software in its entirety and without modifications, you must retain |
this notice and the following text and disclaimers in all such redistributions of |
the Apple Software. Neither the name, trademarks, service marks or logos of |
Apple Computer, Inc. may be used to endorse or promote products derived from the |
Apple Software without specific prior written permission from Apple. Except as |
expressly stated in this notice, no other rights or licenses, express or implied, |
are granted by Apple herein, including but not limited to any patent rights that |
may be infringed by your derivative works or by other works in which the Apple |
Software may be incorporated. |
The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO |
WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED |
WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN |
COMBINATION WITH YOUR PRODUCTS. |
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR |
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION |
OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT |
(INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN |
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Change History (most recent first): |
*/ |
#define TARGET_API_MAC_CARBON 1 |
#include <Resources.h> |
#include <Folders.h> |
#include <TextUtils.h> |
#include <Errors.h> |
#include <LowMem.h> |
#include <Controls.h> |
#include <fp.h> |
#include "Preferences.h" |
#include "WindowCode.h" |
#include "Structs.h" |
#include "Defines.h" |
/* |
Preferences file is a 'pref' resource |
first long (byte 0-3) is delay during slide show in tics |
next byte (byte 4) is flag for if to display files in reverse order |
if 0 then first file in 'odoc' event is opened first (so it's at the bottom of the window list) |
if 1 then last file in 'odoc' event is opened first (so it's at the bottom of the window list) |
*/ |
extern PreferencesHandle gPreferences; |
extern Boolean gPrefsChanged; |
static OSErr FindPrefsFile (FSSpec * theSpec) { |
OSErr err; |
long foundDirID; |
short foundVRefNum; |
Str255 prefsFileName; |
err = FindFolder (kOnSystemDisk, kPreferencesFolderType, kCreateFolder, &foundVRefNum, &foundDirID); |
if (err == noErr) { |
GetIndString (prefsFileName, kPrefsFileNameResID, kPrefsFileNameIndex); |
err = ResError (); |
if (prefsFileName == nil) { |
err = resNotFound; |
} |
} |
if (err == noErr) { |
err = FSMakeFSSpec (foundVRefNum, foundDirID, prefsFileName, theSpec); |
} |
return err; |
} |
OSErr CreatePrefs (void) { |
FSSpec theSpec; |
OSErr err; |
FInfo finderInfo; |
err = FindPrefsFile (&theSpec); |
if (err == fnfErr) { |
FSpCreateResFile (&theSpec, kCreatorType, kPrefResType, smSystemScript); |
err = ResError (); |
} else { |
FSpGetFInfo (&theSpec, &finderInfo); |
if (finderInfo.fdCreator == kCreatorType) { |
err = FSpDelete (&theSpec); |
if (err == noErr) { |
FSpCreateResFile (&theSpec, kCreatorType, kPrefResType, smSystemScript); |
err = ResError (); |
} |
} |
} |
return err; |
} |
OSErr InitPrefs (void) { |
OSErr err; |
gPreferences = (PreferencesHandle)NewHandle (sizeof (Preferences)); |
err = MemError (); |
if (err == noErr) { |
(**gPreferences).slideShowPause = 120; // two seconds in ticks |
(**gPreferences).quality = codecNormalQuality; |
(**gPreferences).openInReverseOrder = false; |
(**gPreferences).scanSubFolders = false; |
(**gPreferences).randomSlideShow = false; |
} |
return err; |
} |
OSErr ReadPrefs (void) { |
FSSpec theSpec; |
OSErr err; |
short resRef; |
err = FindPrefsFile (&theSpec); |
if (err == noErr) { |
resRef = FSpOpenResFile (&theSpec, fsRdPerm); |
if (resRef == -1) { |
err = ResError (); |
} |
} else { |
resRef = -1; |
} |
if (err == noErr) { |
gPreferences = (PreferencesHandle)Get1Resource (kPrefResType, kPrefResID); |
err = ResError (); |
if (gPreferences == nil) { |
err = resNotFound; |
} |
} |
if (err == noErr) { |
DetachResource ((Handle)gPreferences); |
} |
if (resRef != -1) { |
CloseResFile (resRef); |
} |
return err; |
} |
OSErr WritePrefs (void) { |
Handle temp; |
FSSpec theSpec; |
OSErr err; |
short resRef; |
err = FindPrefsFile (&theSpec); |
if (err == noErr) { |
resRef = FSpOpenResFile (&theSpec, fsRdWrPerm); |
if (resRef == -1) { |
err = ResError (); |
} |
} |
if (err == noErr) { |
temp = Get1Resource (kPrefResType, kPrefResID); |
err = ResError (); |
if (temp == nil) { |
err = resNotFound; |
} |
} |
if (err == noErr) { |
RemoveResource (temp); |
} else if (err == resNotFound) { |
err = noErr; |
} |
if (err == noErr) { |
AddResource ((Handle)gPreferences, kPrefResType, kPrefResID, nil); |
} |
if (resRef != -1) { |
CloseResFile (resRef); |
} |
DisposeHandle (temp); |
return err; |
} |
static pascal Boolean ModalDialogFilter (DialogPtr theDialog, EventRecord *theEvent, short *itemHit) { |
Boolean result; |
OSErr err; |
ModalFilterUPP standardProc; |
result = false; |
if ((theEvent->what == updateEvt) && (DialogPtr)theEvent->message != theDialog) { |
err = DispatchWindowUpdate ((WindowPtr)theEvent->message); |
} else if ((theEvent->what == activateEvt) && (DialogPtr)theEvent->message != theDialog) { |
DoActivate (theEvent, true); |
} else { |
err = GetStdFilterProc (&standardProc); |
if (err == noErr) { |
result = (Boolean)CallModalFilterProc (standardProc, theDialog, theEvent, itemHit); |
} |
} |
return result; |
} |
static void SetRadioGroup (DialogPtr theDialog, short firstRadio, short lastRadio, short onRadio) { |
short i; |
DialogItemType itemType; |
ControlHandle item; |
Rect box; |
for (i = firstRadio; i <= lastRadio; i++) { |
if (i != onRadio) { |
GetDialogItem (theDialog, i, &itemType, (Handle*)&item, &box); |
SetControlValue (item, 0); |
} else { |
GetDialogItem (theDialog, i, &itemType, (Handle*)&item, &box); |
SetControlValue (item, 1); |
} |
} |
} |
OSErr DoPreferencesDialog (void) { |
OSErr err; |
DialogPtr prefsDialog; |
short itemHit; |
DialogItemType itemType; |
ControlHandle item; |
Rect box; |
Str255 pauseAsString; |
Boolean done; |
double pauseTime; |
CodecQ origQuality; |
WindowInfoHandle winInfo; |
ModalFilterUPP filterProcUPP; |
Handle itlHandle; |
unsigned char* inString; |
NumFormatString outString; |
NumberParts numParts; |
FormatResultType formatStatus; |
long offset, |
length; |
extended80 x80; |
Rect winRect; |
done = false; |
inString = "\p###,###.#######"; |
err = noErr; |
prefsDialog = GetNewDialog (kPrefsDialogResID, nil, (WindowPtr)-1L); |
if (prefsDialog == nil) { |
err = resNotFound; |
} |
if (err == noErr) { |
err = SetDialogDefaultItem (prefsDialog, dOKButton); |
} |
if (err == noErr) { |
err = SetDialogCancelItem (prefsDialog, dCancelButton); |
} |
if (err == noErr) { |
err = SetDialogTracksCursor (prefsDialog, true); |
} |
if (err == noErr) { |
GetDialogItem (prefsDialog, dReverseOrderPref, &itemType, (Handle*)&item, &box); |
SetControlValue (item, (**gPreferences).openInReverseOrder); |
GetDialogItem (prefsDialog, dScanSubFolders, &itemType, (Handle*)&item, &box); |
SetControlValue (item, (**gPreferences).scanSubFolders); |
GetDialogItem (prefsDialog, dRandomSlideShow, &itemType, (Handle*)&item, &box); |
SetControlValue (item, (**gPreferences).randomSlideShow); |
origQuality = (**gPreferences).quality; |
switch (origQuality) { |
case codecMinQuality: |
GetDialogItem (prefsDialog, dMinimumQuality, &itemType, (Handle*)&item, &box); |
SetControlValue (item, 1); |
break; |
case codecLowQuality: |
GetDialogItem (prefsDialog, dLowQuality, &itemType, (Handle*)&item, &box); |
SetControlValue (item, 1); |
break; |
case codecNormalQuality: |
GetDialogItem (prefsDialog, dNormalQuality, &itemType, (Handle*)&item, &box); |
SetControlValue (item, 1); |
break; |
case codecHighQuality: |
GetDialogItem (prefsDialog, dHighQuality, &itemType, (Handle*)&item, &box); |
SetControlValue (item, 1); |
break; |
case codecMaxQuality: |
GetDialogItem (prefsDialog, dMaxQuality, &itemType, (Handle*)&item, &box); |
SetControlValue (item, 1); |
break; |
} |
GetDialogItem (prefsDialog, dSlideShowTime, &itemType, (Handle*)&item, &box); |
GetIntlResourceTable (smCurrentScript, smNumberPartsTable, &itlHandle, &offset, &length); |
if (itlHandle != nil) { |
pauseTime = (**gPreferences).slideShowPause / 60.0; |
dtox80 (&pauseTime, &x80); |
BlockMoveData (((char*)*itlHandle) + offset, &numParts, length); |
formatStatus = (FormatResultType)StringToFormatRec (inString, &numParts, &outString); |
if (formatStatus == fFormatOK) { |
formatStatus = (FormatResultType)ExtendedToString (&x80, &outString, &numParts, pauseAsString); |
} |
ReleaseResource (itlHandle); |
} |
SetDialogItemText ((Handle)item, pauseAsString); |
SelectDialogItemText (prefsDialog, dSlideShowTime, 0, 32767); |
ShowWindow (GetDialogWindow(prefsDialog)); |
SetPort (GetWindowPort(GetDialogWindow(prefsDialog))); |
} |
filterProcUPP = NewModalFilterProc (ModalDialogFilter); |
if (err == noErr) { |
do { |
ModalDialog (filterProcUPP, &itemHit); |
switch (itemHit) { |
case dOKButton: |
GetDialogItem (prefsDialog, dSlideShowTime, &itemType, (Handle*)&item, &box); |
GetDialogItemText ((Handle)item, pauseAsString); |
GetIntlResourceTable (smCurrentScript, smNumberPartsTable, &itlHandle, &offset, &length); |
if (itlHandle != nil) { |
BlockMoveData (((char*)*itlHandle) + offset, &numParts, length); |
formatStatus = (FormatResultType)StringToFormatRec (inString, &numParts, &outString); |
if (formatStatus == fFormatOK) { |
formatStatus = (FormatResultType)StringToExtended (pauseAsString, &outString, &numParts, &x80); |
} |
ReleaseResource (itlHandle); |
pauseTime = x80tod (&x80); |
} else { |
pauseTime = 3.0; |
} |
(**gPreferences).slideShowPause = (unsigned long)(pauseTime * 60.0); |
GetDialogItem (prefsDialog, dReverseOrderPref, &itemType, (Handle*)&item, &box); |
(**gPreferences).openInReverseOrder = (Boolean)GetControlValue (item); |
GetDialogItem (prefsDialog, dScanSubFolders, &itemType, (Handle*)&item, &box); |
(**gPreferences).scanSubFolders = (Boolean)GetControlValue (item); |
GetDialogItem (prefsDialog, dRandomSlideShow, &itemType, (Handle*)&item, &box); |
(**gPreferences).randomSlideShow = (Boolean)GetControlValue (item); |
GetDialogItem (prefsDialog, dMaxQuality, &itemType, (Handle*)&item, &box); |
if (GetControlValue (item)) |
(**gPreferences).quality = codecMaxQuality; |
GetDialogItem (prefsDialog, dHighQuality, &itemType, (Handle*)&item, &box); |
if (GetControlValue (item)) |
(**gPreferences).quality = codecHighQuality; |
GetDialogItem (prefsDialog, dNormalQuality, &itemType, (Handle*)&item, &box); |
if (GetControlValue (item)) |
(**gPreferences).quality = codecNormalQuality; |
GetDialogItem (prefsDialog, dLowQuality, &itemType, (Handle*)&item, &box); |
if (GetControlValue (item)) |
(**gPreferences).quality = codecLowQuality; |
GetDialogItem (prefsDialog, dMinimumQuality, &itemType, (Handle*)&item, &box); |
if (GetControlValue (item)) |
(**gPreferences).quality = codecMinQuality; |
gPrefsChanged = true; |
case dCancelButton: |
done = true; |
break; |
case dMaxQuality: |
case dHighQuality: |
case dNormalQuality: |
case dLowQuality: |
case dMinimumQuality: |
SetRadioGroup (prefsDialog, dMaxQuality, dMinimumQuality, itemHit); |
break; |
default: |
GetDialogItem (prefsDialog, itemHit, &itemType, (Handle*)&item, &box); |
if (itemType == chkCtrl + ctrlItem) { |
SetControlValue (item, !GetControlValue (item)); |
} |
} |
} while (done == false); |
if (origQuality != (**gPreferences).quality) { |
// The user changed the quality settings so redraw all the windows. |
WindowPtr theWindow; |
theWindow = FrontWindow (); |
while (theWindow != nil) { |
winInfo = (WindowInfoHandle)GetWRefCon (theWindow); |
if (winInfo != nil && (**winInfo).sig == kMySig && (**winInfo).winType == kGraphPicWinType) { |
GetWindowPortBounds (theWindow, &winRect); |
InvalWindowRect (theWindow, &winRect); |
} |
theWindow = GetNextWindow (theWindow); |
} |
} |
DisposeDialog (prefsDialog); |
} |
DisposeModalFilterUPP (filterProcUPP); |
return err; |
} |
Copyright © 2003 Apple Computer, Inc. All Rights Reserved. Terms of Use | Privacy Policy | Updated: 2003-01-14