home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-11-19 | 10.6 KB | 459 lines | [TEXT/MPS ] |
- /*
- File: InspectorMain.cp
-
- Contains: Main program for the Inspector application
-
- Copyright: © 1991-1994 by Apple Computer, Inc., all rights reserved.
-
- */
-
- #ifndef __QUICKDRAW__
- #include <Quickdraw.h>
- #endif
- #ifndef __FONTS__
- #include <Fonts.h>
- #endif
- #ifndef __EVENTS__
- #include <Events.h>
- #endif
- #ifndef __WINDOWS__
- #include <Windows.h>
- #endif
- #ifndef __MENUS__
- #include <Menus.h>
- #endif
- #ifndef __DIALOGS__
- #include <Dialogs.h>
- #endif
- #ifndef __TOOLUTILS__
- #include <ToolUtils.h>
- #endif
- #ifndef __MEMORY__
- #include <Memory.h>
- #endif
- #ifndef __RESOURCES__
- #include <Resources.h>
- #endif
- #ifndef __SEGLOAD__
- #include <SegLoad.h>
- #endif
- #ifndef __OSUTILS__
- #include <OSUtils.h>
- #endif
- #ifndef __TRAPS__
- #include <Traps.h>
- #endif
-
- #ifndef __LIBRARYMANAGERUTILITIES__
- #include <LibraryManagerUtilities.h>
- #endif
- #ifndef __REGISTEREDOBJECTSWINDOW__
- #include "RegisteredObjectsWindow.h"
- #endif
- #ifndef __LIST__
- #include "List.h"
- #endif
- #ifndef __APPLICATION__
- #include "Application.h"
- #endif
- #ifndef __DOCUMENT__
- #include "Document.h"
- #endif
- #ifndef __INSPECTOR__
- #include "Inspector.h"
- #endif
- #ifndef __SIMPLECOMMON__
- #include "SimpleCommon.h"
- #endif
-
- #include "TrapAvailable.cp"
-
- /* some globals we need for the static methods */
-
- TInspector* gInspector = NULL;
- TNotifier* gDeathWatchNotifier = NULL;
- Boolean gDone = false;
- Boolean gQuit = false;
- Boolean gToolboxInitialized = false;
-
- #if GENERATINGPOWERPC
- QDGlobals qd;
- #endif
-
- int main()
- {
- return TInspector::Main();
- }
-
- /**********************************************************************
- ** Inspector static methods
- ***********************************************************************/
-
- #define optionKeyCode 58
- static Boolean isKeyDown(long keyCode);
- static void AlertUser(short errResID, short errCode);
-
- /**********************************************************************
- ** UnloadLibraryManager/LoadLibraryManager
- ***********************************************************************/
-
- void TInspector::UnloadLibraryManager()
- {
- TInspector::Cleanup(); /* get rid of the app and friends */
-
- ::UnloadLibraryManager();
- }
-
- void TInspector::LoadLibraryManager()
- {
- if (!::LoadLibraryManager())
- DebugStr((ConstStr255Param)
- "\pTInspector::LoadLibraryManager - Could load the ASLM");
- }
-
- /**********************************************************************
- ** DeathNotifierProc
- ***********************************************************************/
-
- void TInspector::DeathNotifierProc(TInspector* me, EventCode event, OSErrParm, TLibraryManager* libmgr)
- {
- if (event != kDeathEvent)
- {
- DebugBreak("Bad event code");
- return;
- }
-
- if (libmgr == GetLocalLibraryManager()) // looks like I'm dying, someone did ExitToShell
- {
- RemoveDeathWatcher(gDeathWatchNotifier);
- delete gDeathWatchNotifier;
- delete me;
- }
- }
-
- /**********************************************************************
- ** Boot/Cleanup
- ***********************************************************************/
-
- void TInspector::Boot()
- {
- while (true)
- {
- ::InitLibraryManager(kInspectorPoolSize); // see if we can initialize the ASLM
- TLibraryManager* theLibraryManager = GetLocalLibraryManager(); // did we succeed?
- if (theLibraryManager == NULL)
- {
- ::AlertUser(kInspectorMainErrStrings, kInitLibraryManagerFailed);
- TInspector::SimpleProgram(); // try the simple app
- if (gQuit)
- return;
- continue; // Try full app again
- }
-
- TRY
- gInspector = new TInspector(&qd, !gToolboxInitialized);
- CATCH_ALL
- ::AlertUser(kInspectorMainErrStrings, kInspectorLibraryMissing);
- ENDTRY
-
- if (gInspector != NULL)
- {
- gDeathWatchNotifier = new TProcNotifier(NotifyProcPtr(&TInspector::DeathNotifierProc),gInspector);
- if (gDeathWatchNotifier)
- InstallDeathWatcher(gDeathWatchNotifier);
- gToolboxInitialized = true;
- return;
- }
-
- ::CleanupLibraryManager();
- TInspector::SimpleProgram();
- if (gQuit)
- return;
- }
- }
-
- void TInspector::Cleanup()
- {
- if (gInspector)
- {
- if (gDeathWatchNotifier)
- {
- RemoveDeathWatcher(gDeathWatchNotifier);
- delete gDeathWatchNotifier;
- }
- delete gInspector;
- gInspector = NULL;
- CleanupLibraryManager();
- }
- }
-
- /**********************************************************************
- ** SimpleProgram
- ***********************************************************************/
-
- void TInspector::SimpleProgram()
- {
- // install menus
- Handle menuBar = GetNewMBar(rMenuBar);
- SetMenuBar(menuBar);
- DisposeHandle(menuBar);
-
- // add DA names to Apple menu if we haven't already
- if (CountMItems(GetMenuHandle(mApple)) == 2)
- AppendResMenu(GetMenuHandle(mApple),'DRVR');
-
- DrawMenuBar();
-
- SetCursor(&qd.arrow);
-
- TInspector::SimpleDoEventLoop();
- }
-
- /**********************************************************************
- ** SimpleDoEventLoop
- ***********************************************************************/
-
- void TInspector::SimpleDoEventLoop()
- {
- int gotEvent;
- EventRecord theEvent;
- Boolean hasWaitNextEvent = TrapAvailable(_WaitNextEvent);
- RgnHandle mouseRgn = NewRgn();
-
- gDone = false;
- gQuit = false;
- while (gDone == false) {
- if (hasWaitNextEvent)
- gotEvent = WaitNextEvent(everyEvent, &theEvent, 20, mouseRgn);
- else {
- SystemTask();
- gotEvent = GetNextEvent(everyEvent, &theEvent);
- }
-
- // make sure we got a real event
- if ( gotEvent ) {
- SetCursor(&qd.arrow);
- switch (theEvent.what) {
- case mouseDown : {
- long mResult;
- short partCode;
- WindowPtr tWind;
-
- partCode = FindWindow(theEvent.where, &tWind);
- switch (partCode) {
- case inSysWindow :
- SystemClick(&theEvent, tWind);
- break;
- case inMenuBar :
- SimpleAdjustMenus();
- mResult = MenuSelect(theEvent.where);
- if (mResult != 0)
- TInspector::SimpleDoMenuCommand(HiWrd(mResult),LoWrd(mResult));
- break;
- }
- break;
- }
- case keyDown :
- case autoKey : {
- char key;
- long mResult;
-
- key = (char) (theEvent.message & charCodeMask);
- if ((theEvent.modifiers & cmdKey) && (theEvent.what == keyDown)) {
- // only do command keys if we are not autokeying
- SimpleAdjustMenus(); // make sure menus are up to date
- mResult = MenuKey(key);
- if (mResult != 0) // if it wasn't a menu key, pass it through
- TInspector::SimpleDoMenuCommand(HiWrd(mResult), LoWrd(mResult));
- }
- break;
- }
- default :
- break;
- }
- }
- SetCursor(&qd.arrow);
- }
-
- DisposeHandle(Handle(mouseRgn));
- }
-
- /**********************************************************************
- ** SimpleAdjustMenus
- ***********************************************************************/
-
- void TInspector::SimpleAdjustMenus()
- {
- MenuHandle menu;
-
- /* File Menu */
-
- menu = GetMenuHandle(mFile);
- if (::IsLibraryManagerLoaded()) {
- DisableItem(menu,iLoadLibraryManager);
- EnableItem(menu,iUnloadLibraryManager);
- EnableItem(menu,iRealProgram);
- }
- else {
- EnableItem(menu,iLoadLibraryManager);
- DisableItem(menu,iUnloadLibraryManager);
- DisableItem(menu,iRealProgram);
- }
- }
-
- /**********************************************************************
- ** SimpleDoMenuCommand
- ***********************************************************************/
-
- void TInspector::SimpleDoMenuCommand(short menuID, short menuItem)
- {
- short itemHit;
- Str255 daName;
- short daRefNum;
- WindowPtr window;
-
- window = FrontWindow();
- switch (menuID) {
- case mApple:
- switch (menuItem) {
- case iAbout: // bring up alert for About
- itemHit = Alert(rAboutAlert, NULL);
- break;
- default: // all non-About items in this menu are DAs et al
- GetMenuItemText(GetMenuHandle(mApple), menuItem, daName);
- daRefNum = OpenDeskAcc(daName);
- break;
- }
- break;
-
- case mFile:
- switch (menuItem) {
- case iLoadLibraryManager:
- TInspector::LoadLibraryManager();
- break;
- case iUnloadLibraryManager:
- TInspector::UnloadLibraryManager();
- break;
- case iRealProgram:
- gDone = true;
- break;
- case iQuit:
- gDone = true;
- gQuit = true;
- break;
- }
- break;
-
- case mEdit: // call SystemEdit for DA editing & MultiFinder
- if (!SystemEdit(menuItem-1))
- DebugStr((ConstStr255Param)"\pOops. SystemEdit returned false.");
- break;
- }
-
- HiliteMenu(0); // unhighlight what MenuSelect (or MenuKey) hilited
- }
-
- /**********************************************************************
- ** isKeyDown
- ***********************************************************************/
-
- static Boolean isKeyDown(long keyCode)
- {
- KeyMap keys;
- Byte *keysBytes;
-
- GetKeys(keys);
-
- keysBytes = (Byte *)keys;
-
- return((keysBytes[keyCode/8] & (1 << keyCode % 8)) != 0);
- }
-
- /**********************************************************************
- ** AlertUser
- ***********************************************************************/
-
- void AlertUser(short errResID, short errCode)
- {
- Str255 message;
-
- GetIndString(message, errResID, errCode);
- #if qDebug
- if (message[0] == 0)
- {
- DebugStr((ConstStr255Param)"\pAlertUser could not get error string.");
- return;
- }
- #endif
- ParamText(message, (ConstStr255Param)"\p", (ConstStr255Param)"\p", (ConstStr255Param)"\p");
- Alert(rUserAlert, NULL);
- }
-
- /**********************************************************************
- ** Main
- ***********************************************************************/
-
- int TInspector::Main()
- {
- #if GENERATINGPOWERPC
- if (isKeyDown(optionKeyCode)) {
- DebugStr ((unsigned char *)"\pDebugStr to get us into the Nub debugger");
- }
- #endif
- if (!gToolboxInitialized) {
- gToolboxInitialized = true;
- InitGraf(&qd.thePort);
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(NULL);
- InitCursor();
- }
-
- if (isKeyDown(optionKeyCode)) {
- TInspector::SimpleProgram();
- if (gQuit)
- return 0;
- }
-
- TInspector::Boot();
-
- // Start our main event loop running.
- while (!gQuit && gInspector) {
- gInspector->EventLoop();
- switch (gInspector->GetLibraryManagerCmd()) {
- case kReloadLibraryManagerCmd: {
- TInspector::UnloadLibraryManager(); /* unload the ASLM */
- TInspector::LoadLibraryManager(); /* reload the ASLM */
- TInspector::Boot(); /* Re-boot the TInspector */
- break;
- }
-
- case kUnloadLibraryManagerCmd: {
- TInspector::UnloadLibraryManager(); /* unload the ASLM */
- TInspector::SimpleProgram(); /* run the SimpleProgram */
- if (!gQuit)
- TInspector::Boot(); /* Re-boot the TInspector */
- break;
- }
-
- case kGotoSimpleProgramCmd: {
- TInspector::Cleanup(); /* get rid of the app and friends */
- TInspector::SimpleProgram(); /* run the SimpleProgram */
- if (!gQuit)
- TInspector::Boot(); /* Re-boot the TInspector */
- break;
- }
-
- default:
- gQuit = true;
- TInspector::Cleanup();
- break;
- }
- }
-
- // We always return a value, like good little ANSI worshippers
- return 0;
- }
-