/************************************************/ /* */ /* Snakes.c - Snakes main module. */ /* */ /* Revised 7/7/94 by RMR */ /* */ /************************************************/ #include #define __MAIN__ #include "Snakes.h" static pascal void DoAppleMenu (short item) { (void) SkelAlert( kAboutAlrtRes, SkelDlogFilter(nil, true), skelPositionOnParentDevice); SkelRmveDlogFilter(); } static pascal void DoFileMenu(short item) { switch ((FileItem)item) { case iOpen: OpenPictWindow(); ReportFreeMem("\pPicture opened"); AdjustMenus(true); break; case iClose: ClosePictWindow(); ReportFreeMem("\pPicture closed"); AdjustMenus(true); break; case iSave: SavePictWindow(); break; case iSaveAs: SavePictWindowAs(); break; case iLoadModel: LoadModel(); break; case iSaveModel: SaveModelAs(); break; case iGlobal: OpenGlobalDialog(); break; case iOption: OpenOptionsDialog(); break; case iQuit: SkelStopEventLoop(); } } static pascal void DoImageMenu(short item) { switch ((ImageItem)item) { case iUndo: if (FrontWindow() == gGradWindow) CloseGradWindow(); else if (FrontWindow() == gThrsWindow) CloseThrsWindow(); else UndoFilter(); AdjustMenus(true); break; case iSmooth: DoSmoothingFilter(); AdjustMenus(true); break; case iEqual: DoEqualizeFilter(); AdjustMenus(true); break; case iNoise: DoNoiseFilter(); AdjustMenus(true); break; case iFill: DoFillFilter(); AdjustMenus(true); break; case iHist: OpenHistDialog(); AdjustMenus(true); break; case iThrs: OpenThresholdDialog(); AdjustMenus(false); break; case iBlobs: ExpandBlobs(); AdjustMenus(true); break; case iGrad: OpenGradWindow(); AdjustMenus(true); } } static pascal void DoModelMenu(short item) { switch ((ModelItem)item) { case iRefParams: OpenRefParamDialog(); AdjustMenus(false); break; case iTestParams: OpenTestParamDialog(); AdjustMenus(false); break; case iPlacement: if (gPictArray.valid) OpenPlaceDialog(); AdjustMenus(true); break; case iClearAll: ClearModel(gRefModel); ClearModel(gTestModel); KickPictWindow(); AdjustMenus(true); break; case iExecute: if (gPictArray.valid) OpenExecDialog(); AdjustMenus(true); break; case iRelax: RelaxModel(); AdjustMenus(true); } } static void CheckForQD32(void) { if (SkelQuery(skelQHasGestalt) == 0L) DoError("\pDoesn't have Gestalt!"); if (SkelQuery(skelQHasColorQD) == 0L) DoError("\pDoesn't have Color QuickDraw!"); if (NGetTrapAddress(QD32TRAP, ToolTrap) == NGetTrapAddress(UNIMPL_TRAP, ToolTrap)) DoError("\pDoesn't have 32-bit Color QuickDraw!"); } static void SetUpData(void) { GetIndPattern(gSolidPattern, sysPatListID, 1); gTerminateFlag = false; gModelStatus = sIdle; } static void SetUpCursors(void) { gIBeamCursor = GetCursor(iBeamCursor); HLock((Handle)gIBeamCursor); gCrossCursor = GetCursor(crossCursor); HLock((Handle)gCrossCursor); gWatchCursor = GetCursor(watchCursor); HLock((Handle)gWatchCursor); } static void SetUpMenus(void) { gFileMenu = GetMenu(mFile); gImageMenu = GetMenu(mImage); gModelMenu = GetMenu(mModel); gSmoothKernelMenu = GetMenu(mSmoothKernel); gGradKernelMenu = GetMenu(mGradKernel); SkelApple((StringPtr) "\pAbout Snakes\311", DoAppleMenu); SkelMenu(gFileMenu, DoFileMenu, nil, false, false); SkelMenu(gImageMenu, DoImageMenu, nil, false, false); SkelMenu(gModelMenu, DoModelMenu, nil, false, true); DisableItem(gFileMenu, iClose); DisableItem(gFileMenu, iSave); DisableItem(gFileMenu, iSaveAs); DisableItem(gImageMenu, 0); DisableItem(gModelMenu, iExecute); } static void SetUpWindows(void) { Rect r; gInfoWindow = NewDWindow((Rect *)&kInfoWindRect, "\pInfo Window", false, (WindowPtr) -1L, false, 0L); if (gInfoWindow == (WindowPtr)nil) DoError("\pCould not allocate Info Window!"); SetPalette(gInfoWindow, gCustPalette, true); gPictWindow = GetNewCWindow(kPictWindow, nil, (WindowPtr) -1L); if (gPictWindow == (WindowPtr)nil) DoError("\pCould not allocate Picture Window!"); if (!InitPictWindow()) DoError("\pCould not register Picture Window!"); SetPalette(gPictWindow, gCustPalette, true); gGradWindow = GetNewCWindow(kGradWindow, nil, (WindowPtr) -1L); if (gGradWindow == (WindowPtr)nil) DoError("\pCould not allocate Gradient Window!"); if (!InitGradWindow()) DoError("\pCould not register Gradient Window!"); SetPalette(gGradWindow, gCustPalette, true); gThrsWindow = GetNewCWindow(kThrsWindow, nil, (WindowPtr) -1L); if (gThrsWindow == (WindowPtr)nil) DoError("\pCould not allocate Threshold Window!"); if (!InitThrsWindow()) DoError("\pCould not register Threshold Window!"); SetPalette(gThrsWindow, gCustPalette, true); } static void SetUpDialogs(void) { Handle h; h = GetResource('DITL', kProgressDlog); if (h == nil) DoError("\pCould not fetch Progress DITL!"); gProgressDialog = NewCDialog(nil, (Rect *)&kProgrDlogRect, "\pProgress", false, kMovableModal, (WindowPtr)-1L, false, 0, h); if (gProgressDialog == (DialogPtr)nil) DoError("\pCould not allocate Progress Dialog!"); if (!InitProgressDialog()) DoError("\pCould not register Progress Dialog!"); SetPalette(gProgressDialog, gCustPalette, true); h = GetResource('DITL', kGlobalDlog); if (h == nil) DoError("\pCould not fetch Global DITL!"); gGlobalDialog = NewCDialog(nil, (Rect *)&kGlobalDlogRect, "\pGlobal Parameters", false, kMovableModal, (WindowPtr)-1L, true, 0, h); if (gGlobalDialog == (DialogPtr)nil) DoError("\pCould not allocate Global Dialog!"); if (!InitGlobalDialog()) DoError("\pCould not register Global Dialog!"); SetPalette(gGlobalDialog, gCustPalette, true); h = GetResource('DITL', kRefParamDlog); if (h == nil) DoError("\pCould not fetch Ref Param DITL!"); gRefParamDialog = NewCDialog(nil, (Rect *)&kRefParmDlogRect, "\pRef Model Paraneters", false, kMovableModal, (WindowPtr)-1L, false, 0, h); if (gRefParamDialog == (DialogPtr)nil) DoError("\pCould not allocate Ref Param Dialog!"); if (!InitRefParamDialog()) DoError("\pCould not register Ref Param Dialog!"); SetPalette(gRefParamDialog, gCustPalette, true); h = GetResource('DITL', kTestParamDlog); if (h == nil) DoError("\pCould not fetch Test Param DITL!"); gTestParamDialog = NewCDialog(nil, (Rect *)&kTstParmDlogRect, "\pTest Model Paraneters", false, kMovableModal, (WindowPtr)-1L, false, 0, h); if (gTestParamDialog == (DialogPtr)nil) DoError("\pCould not allocate Test Param Dialog!"); if (!InitTestParamDialog()) DoError("\pCould not register Test Param Dialog!"); SetPalette(gTestParamDialog, gCustPalette, true); h = GetResource('DITL', kThresholdDlog); if (h == nil) DoError("\pCould not fetch Threshold DITL!"); gThresholdDialog = NewCDialog(nil, (Rect *)&kThreshDlogRect, "\pSpecular Threshold", false, kMovableModal, (WindowPtr)-1L, false, 0, h); if (gThresholdDialog == (DialogPtr)nil) DoError("\pCould not allocate Threshold Dialog!"); if (!InitThresholdDialog()) DoError("\pCould not register Threshold Dialog!"); SetPalette(gThresholdDialog, gCustPalette, true); h = GetResource('DITL', kOptionsDlog); if (h == nil) DoError("\pCould not fetch Options DITL!"); gOptionsDialog = NewCDialog(nil, (Rect *)&kOptionsDlogRect, "\pProgram Options", false, noGrowDocProc, (WindowPtr)-1L, true, 0, h); if (gOptionsDialog == (DialogPtr)nil) DoError("\pCould not allocate Options Dialog!"); if (!InitOptionsDialog()) DoError("\pCould not register Options Dialog!"); SetPalette(gOptionsDialog, gCustPalette, true); h = GetResource('DITL', kPlaceDlog); if (h == nil) DoError("\pCould not fetch Placement DITL!"); gPlaceDialog = NewCDialog(nil, (Rect *)&kPlaceDlogRect, "\pModel Placement", false, noGrowDocProc, (WindowPtr)-1L, false, 0, h); if (gPlaceDialog == (DialogPtr)nil) DoError("\pCould not allocate Placement Dialog!"); if (!InitPlaceDialog()) DoError("\pCould not register Placement Dialog!"); SetPalette(gPlaceDialog, gCustPalette, true); h = GetResource('DITL', kExecuteDlog); if (h == nil) DoError("\pCould not fetch Placement DITL!"); gExecDialog = NewCDialog(nil, (Rect *)&kExecuteDlogRect, "\pModel Execution", false, noGrowDocProc, (WindowPtr)-1L, false, 0, h); if (gExecDialog == (DialogPtr)nil) DoError("\pCould not allocate Execution Dialog!"); if (!InitExecDialog()) DoError("\pCould not register Execution Dialog!"); SetPalette(gExecDialog, gCustPalette, true); h = GetResource('DITL', kHistDlog); if (h == nil) DoError("\pCould not fetch Histment DITL!"); gHistDialog = NewCDialog(nil, (Rect *)&kHistDlogRect, "\pImage Histogram", false, noGrowDocProc, (WindowPtr)-1L, false, 0, h); if (gHistDialog == (DialogPtr)nil) DoError("\pCould not allocate Histogram Dialog!"); if (!InitHistDialog()) DoError("\pCould not register Histogram Dialog!"); SetPalette(gHistDialog, gCustPalette, true); } static void LoadPreferences(void) { short myVRef, myRefNum, oldRefNum; long myDirID; FSSpec mySpec; FInfo myInfo; Handle h; oldRefNum = CurResFile(); LoadDefault((Ptr *)&gOptions, (Ptr)&defaultOptions, sizeof(Options)); LoadDefault((Ptr *)&gGlobalParams, (Ptr)&defaultParams, sizeof(GlobalParams)); LoadDefault((Ptr *)&gRefModel, (Ptr)&defaultModel, sizeof(Model)); LoadDefault((Ptr *)&gTestModel, (Ptr)&defaultModel, sizeof(Model)); if (FindFolder(kOnSystemDisk, kPreferencesFolderType, kDontCreateFolder, &myVRef, &myDirID) != noErr) DoError("\pCannot find preferences folder!"); if (FSMakeFSSpec(myVRef, myDirID, PrefFileName, &mySpec) == noErr) { if ((myRefNum = FSpOpenResFile(&mySpec, fsRdWrPerm)) == -1) DoError("\pCannot open preferences file!"); UseResFile(myRefNum); SetWPos(gInfoWindow, kInfoWindow); SetWPos(gPictWindow, kPictWindow); SetWPos(gGradWindow, kGradWindow); SetWPos(gThrsWindow, kThrsWindow); SetWPos(gProgressDialog, kProgressDlog); SetWPos(gGlobalDialog, kGlobalDlog); SetWPos(gRefParamDialog, kRefParamDlog); SetWPos(gTestParamDialog, kTestParamDlog); SetWPos(gThresholdDialog, kThresholdDlog); SetWPos(gOptionsDialog, kOptionsDlog); SetWPos(gPlaceDialog, kPlaceDlog); SetWPos(gExecDialog, kExecuteDlog); SetWPos(gHistDialog, kHistDlog); LoadPreference((Ptr)gOptions, sizeof(Options), kOptionsType, kOptions); LoadPreference((Ptr)gGlobalParams, sizeof(GlobalParams), kGlobalParamType, kGlobalParams); LoadPreference((Ptr)gRefModel, sizeof(Model), kModelType, kRefModel); LoadPreference((Ptr)gTestModel, sizeof(Model), kModelType, kTestModel); CloseResFile(myRefNum); } UseResFile(oldRefNum); ClearModel(gRefModel); ClearModel(gTestModel); if (gOptions->showInfoWindow) ShowWindow(gInfoWindow); else HideWindow(gInfoWindow); } static void SavePreferences(void) { short myVRef, tempRefNum, oldRefNum; long myDirID; FSSpec prefSpec, tempSpec; FInfo myInfo; OSErr err; oldRefNum = CurResFile(); if (FindFolder(kOnSystemDisk, kPreferencesFolderType, kDontCreateFolder, &myVRef, &myDirID) != noErr) DoError("\pCannot find preferences folder!"); err = FSMakeFSSpec(myVRef, myDirID, PrefTempName, &tempSpec); if (err == noErr) FSpDelete(&tempSpec); FSMakeFSSpec(myVRef, myDirID, PrefTempName, &tempSpec); FSpCreateResFile(&tempSpec, kCreatorID, 'rsrc', 0); if ((tempRefNum = FSpOpenResFile(&tempSpec, fsRdWrPerm)) == -1) DoError("\pCannot open new preferences file!"); UseResFile(tempRefNum); /* save window positions */ SaveWPos(gInfoWindow, &gInfoWindowWPos); SaveWPos(gPictWindow, &gPictWindowWPos); SaveWPos(gGradWindow, &gGradWindowWPos); SaveWPos(gThrsWindow, &gThrsWindowWPos); SaveWPos(gProgressDialog, &gProgressDialogWPos); SaveWPos(gGlobalDialog, &gGlobalDialogWPos); SaveWPos(gRefParamDialog, &gRefParamDialogWPos); SaveWPos(gTestParamDialog, &gTestParamDialogWPos); SaveWPos(gThresholdDialog, &gThresholdDialogWPos); SaveWPos(gOptionsDialog, &gOptionsDialogWPos); SaveWPos(gPlaceDialog, &gPlaceDialogWPos); SaveWPos(gHistDialog, &gHistDialogWPos); SaveWPos(gExecDialog, &gExecDialogWPos); /* save global options, global parameters, and models */ SaveResource((Handle)gInfoWindowWPos, kWPosType, kInfoWindow, "\pInfo Window"); SaveResource((Handle)gPictWindowWPos, kWPosType, kPictWindow, "\pPicture Window"); SaveResource((Handle)gGradWindowWPos, kWPosType, kGradWindow, "\pGradient Window"); SaveResource((Handle)gThrsWindowWPos, kWPosType, kThrsWindow, "\pThreshold Window"); SaveResource((Handle)gProgressDialogWPos, kWPosType, kProgressDlog, "\pProgress Dialog"); SaveResource((Handle)gGlobalDialogWPos, kWPosType, kGlobalDlog, "\pGlobal Parameters Dialog"); SaveResource((Handle)gRefParamDialogWPos, kWPosType, kRefParamDlog, "\pRef Model Dialog"); SaveResource((Handle)gTestParamDialogWPos, kWPosType, kTestParamDlog, "\pTest Model Dialog"); SaveResource((Handle)gThresholdDialogWPos, kWPosType, kThresholdDlog, "\pThreshold Dialog"); SaveResource((Handle)gOptionsDialogWPos, kWPosType, kOptionsDlog, "\pGlobal Options Dialog"); SaveResource((Handle)gPlaceDialogWPos, kWPosType, kPlaceDlog, "\pPlacement Dialog"); SaveResource((Handle)gHistDialogWPos, kWPosType, kHistDlog, "\pHistogram Dialog"); SaveResource((Handle)gExecDialogWPos, kWPosType, kExecuteDlog, "\pExecution Dialog"); SavePtrRsrc((Ptr)gOptions, sizeof(Options), kOptionsType, kOptions, "\pGlobal Options"); SavePtrRsrc((Ptr)gGlobalParams, sizeof(GlobalParams), kGlobalParamType, kGlobalParams, "\pGlobal Parameters"); SavePtrRsrc((Ptr)gRefModel, sizeof(Model), kModelType, kRefModel, "\pReference Model"); SavePtrRsrc((Ptr)gTestModel, sizeof(Model), kModelType, kTestModel, "\pTest Model"); UpdateResFile(tempRefNum); CloseResFile(tempRefNum); if (FSMakeFSSpec(myVRef, myDirID, PrefFileName, &prefSpec) == noErr) { if (FSpExchangeFiles(&prefSpec, &tempSpec) != noErr) DoError("\pCannot swap temp and pref files!"); if (FSpDelete(&tempSpec) != noErr) DoError("\pCannot delete old pref file!"); } else if (FSpRename(&tempSpec, PrefFileName) != noErr) DoError("\pCannot rename temp pref file!"); UseResFile(oldRefNum); } int main (void) { SkelInit(nil); CheckForQD32(); CheckFileCompatibility(); SetCustomPalette(); SetUpData(); SetUpCursors(); SetUpMenus(); SetUpWindows(); SetUpDialogs(); LoadPreferences(); AdjustMenus(true); ReportFreeMem("\pAfter initialization"); SkelEventLoop(); SavePreferences(); SkelCleanup(); }