home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 April: Mac OS SDK / Dev.CD Apr 97 SDK1.toast / Development Kits (Disc 1) / Apple Shared Library Manager / ASLM Examples / Inspector / Sources / InspectorMain.cp < prev    next >
Encoding:
Text File  |  1996-11-19  |  10.6 KB  |  459 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        InspectorMain.cp
  3.  
  4.     Contains:    Main program for the Inspector application
  5.  
  6.     Copyright:    © 1991-1994 by Apple Computer, Inc., all rights reserved.
  7.  
  8. */
  9.  
  10. #ifndef __QUICKDRAW__
  11. #include <Quickdraw.h>
  12. #endif
  13. #ifndef __FONTS__
  14. #include <Fonts.h>
  15. #endif
  16. #ifndef __EVENTS__
  17. #include <Events.h>
  18. #endif
  19. #ifndef __WINDOWS__
  20. #include <Windows.h>
  21. #endif
  22. #ifndef __MENUS__
  23. #include <Menus.h>
  24. #endif
  25. #ifndef __DIALOGS__
  26. #include <Dialogs.h>
  27. #endif
  28. #ifndef __TOOLUTILS__
  29. #include <ToolUtils.h>
  30. #endif
  31. #ifndef __MEMORY__
  32. #include <Memory.h>
  33. #endif
  34. #ifndef __RESOURCES__
  35. #include <Resources.h>
  36. #endif
  37. #ifndef __SEGLOAD__
  38. #include <SegLoad.h>
  39. #endif
  40. #ifndef __OSUTILS__
  41. #include <OSUtils.h>
  42. #endif
  43. #ifndef __TRAPS__
  44. #include <Traps.h>
  45. #endif
  46.  
  47. #ifndef __LIBRARYMANAGERUTILITIES__
  48. #include <LibraryManagerUtilities.h>
  49. #endif
  50. #ifndef __REGISTEREDOBJECTSWINDOW__
  51. #include "RegisteredObjectsWindow.h"
  52. #endif
  53. #ifndef __LIST__
  54. #include "List.h"
  55. #endif
  56. #ifndef __APPLICATION__
  57. #include "Application.h"
  58. #endif
  59. #ifndef __DOCUMENT__
  60. #include "Document.h"
  61. #endif
  62. #ifndef __INSPECTOR__
  63. #include "Inspector.h"
  64. #endif
  65. #ifndef __SIMPLECOMMON__
  66. #include "SimpleCommon.h"
  67. #endif
  68.  
  69. #include "TrapAvailable.cp"
  70.  
  71. /* some globals we need for the static methods */
  72.  
  73. TInspector*    gInspector = NULL;
  74. TNotifier*    gDeathWatchNotifier = NULL;
  75. Boolean     gDone = false;
  76. Boolean     gQuit = false;
  77. Boolean     gToolboxInitialized = false;
  78.  
  79. #if GENERATINGPOWERPC
  80.     QDGlobals qd;
  81. #endif
  82.  
  83. int main()
  84. {
  85.     return TInspector::Main();
  86. }
  87.  
  88. /**********************************************************************
  89. **  Inspector static methods
  90. ***********************************************************************/
  91.  
  92. #define optionKeyCode     58
  93. static Boolean isKeyDown(long keyCode);
  94. static void AlertUser(short errResID, short errCode);
  95.  
  96. /**********************************************************************
  97. **  UnloadLibraryManager/LoadLibraryManager
  98. ***********************************************************************/
  99.  
  100. void TInspector::UnloadLibraryManager()
  101. {
  102.     TInspector::Cleanup();        /* get rid of the app and friends */
  103.  
  104.     ::UnloadLibraryManager();
  105. }
  106.  
  107. void TInspector::LoadLibraryManager()
  108. {
  109.     if (!::LoadLibraryManager())
  110.         DebugStr((ConstStr255Param)
  111.                  "\pTInspector::LoadLibraryManager - Could load the ASLM");
  112. }
  113.  
  114. /**********************************************************************
  115. **  DeathNotifierProc
  116. ***********************************************************************/
  117.  
  118. void TInspector::DeathNotifierProc(TInspector* me, EventCode event, OSErrParm, TLibraryManager* libmgr)
  119. {
  120.     if (event != kDeathEvent)
  121.     {
  122.         DebugBreak("Bad event code");
  123.         return;
  124.     }
  125.     
  126.     if (libmgr == GetLocalLibraryManager())        // looks like I'm dying, someone did ExitToShell
  127.     {
  128.         RemoveDeathWatcher(gDeathWatchNotifier);
  129.         delete gDeathWatchNotifier;
  130.         delete me;
  131.     }
  132. }
  133.  
  134. /**********************************************************************
  135. **  Boot/Cleanup
  136. ***********************************************************************/
  137.  
  138. void TInspector::Boot()
  139. {
  140.     while (true)
  141.     {
  142.         ::InitLibraryManager(kInspectorPoolSize);    // see if we can initialize the ASLM
  143.         TLibraryManager* theLibraryManager = GetLocalLibraryManager();    // did we succeed?
  144.         if (theLibraryManager == NULL)
  145.         {
  146.             ::AlertUser(kInspectorMainErrStrings, kInitLibraryManagerFailed);
  147.             TInspector::SimpleProgram();        // try the simple app
  148.             if (gQuit)
  149.                 return;
  150.             continue;                            // Try full app again
  151.         }
  152.         
  153.         TRY
  154.             gInspector = new TInspector(&qd, !gToolboxInitialized);
  155.         CATCH_ALL
  156.             ::AlertUser(kInspectorMainErrStrings, kInspectorLibraryMissing);
  157.         ENDTRY
  158.         
  159.         if (gInspector != NULL)
  160.         {
  161.             gDeathWatchNotifier = new TProcNotifier(NotifyProcPtr(&TInspector::DeathNotifierProc),gInspector);
  162.             if (gDeathWatchNotifier)
  163.                 InstallDeathWatcher(gDeathWatchNotifier);
  164.             gToolboxInitialized = true;
  165.             return;
  166.         }
  167.     
  168.         ::CleanupLibraryManager();
  169.         TInspector::SimpleProgram();
  170.         if (gQuit)
  171.             return;
  172.     }
  173. }
  174.  
  175. void TInspector::Cleanup()
  176. {
  177.     if (gInspector)
  178.     {
  179.         if (gDeathWatchNotifier)
  180.         {
  181.             RemoveDeathWatcher(gDeathWatchNotifier);
  182.             delete gDeathWatchNotifier;
  183.         }
  184.         delete gInspector;
  185.         gInspector = NULL;
  186.         CleanupLibraryManager();
  187.     }
  188. }
  189.  
  190. /**********************************************************************
  191. **  SimpleProgram
  192. ***********************************************************************/
  193.  
  194. void TInspector::SimpleProgram()
  195. {
  196.     // install menus
  197.     Handle menuBar = GetNewMBar(rMenuBar);
  198.     SetMenuBar(menuBar);
  199.     DisposeHandle(menuBar);
  200.  
  201.     // add DA names to Apple menu if we haven't already
  202.     if (CountMItems(GetMenuHandle(mApple)) == 2)
  203.         AppendResMenu(GetMenuHandle(mApple),'DRVR');
  204.  
  205.     DrawMenuBar();
  206.  
  207.     SetCursor(&qd.arrow);
  208.     
  209.     TInspector::SimpleDoEventLoop();
  210. }
  211.  
  212. /**********************************************************************
  213. **  SimpleDoEventLoop
  214. ***********************************************************************/
  215.  
  216. void TInspector::SimpleDoEventLoop()
  217. {
  218.     int            gotEvent;
  219.     EventRecord    theEvent;
  220.     Boolean        hasWaitNextEvent = TrapAvailable(_WaitNextEvent);
  221.     RgnHandle     mouseRgn = NewRgn();
  222.  
  223.     gDone = false;
  224.     gQuit = false;
  225.     while (gDone == false) {
  226.         if (hasWaitNextEvent)
  227.             gotEvent = WaitNextEvent(everyEvent, &theEvent, 20, mouseRgn);
  228.         else {
  229.             SystemTask();
  230.             gotEvent = GetNextEvent(everyEvent, &theEvent);
  231.         }
  232.  
  233.         // make sure we got a real event
  234.         if ( gotEvent ) {
  235.             SetCursor(&qd.arrow);
  236.             switch (theEvent.what) {
  237.                 case mouseDown : {
  238.                     long mResult;
  239.                     short partCode;
  240.                     WindowPtr tWind;
  241.                 
  242.                     partCode = FindWindow(theEvent.where, &tWind);
  243.                     switch (partCode) {
  244.                         case inSysWindow :
  245.                              SystemClick(&theEvent, tWind); 
  246.                              break;
  247.                         case inMenuBar :
  248.                             SimpleAdjustMenus();
  249.                             mResult = MenuSelect(theEvent.where);
  250.                             if (mResult != 0)
  251.                                 TInspector::SimpleDoMenuCommand(HiWrd(mResult),LoWrd(mResult));
  252.                             break;
  253.                     }
  254.                     break;
  255.                 }
  256.                 case keyDown :
  257.                 case autoKey : {
  258.                     char key;
  259.                     long mResult;
  260.                 
  261.                     key = (char) (theEvent.message & charCodeMask);
  262.                     if ((theEvent.modifiers & cmdKey) && (theEvent.what == keyDown)) {
  263.                         // only do command keys if we are not autokeying
  264.                         SimpleAdjustMenus();            // make sure menus are up to date
  265.                         mResult = MenuKey(key);
  266.                         if (mResult != 0)                // if it wasn't a menu key, pass it through
  267.                             TInspector::SimpleDoMenuCommand(HiWrd(mResult), LoWrd(mResult));
  268.                     }
  269.                     break;
  270.                 }
  271.                 default :
  272.                     break;
  273.             }
  274.         }
  275.         SetCursor(&qd.arrow);
  276.     }
  277.     
  278.     DisposeHandle(Handle(mouseRgn));
  279. }
  280.  
  281. /**********************************************************************
  282. **  SimpleAdjustMenus
  283. ***********************************************************************/
  284.     
  285. void TInspector::SimpleAdjustMenus()
  286. {
  287.     MenuHandle    menu;
  288.  
  289.     /* File Menu */
  290.  
  291.     menu = GetMenuHandle(mFile);
  292.     if (::IsLibraryManagerLoaded()) {
  293.         DisableItem(menu,iLoadLibraryManager);
  294.         EnableItem(menu,iUnloadLibraryManager);
  295.         EnableItem(menu,iRealProgram);
  296.     }
  297.     else {
  298.         EnableItem(menu,iLoadLibraryManager);
  299.         DisableItem(menu,iUnloadLibraryManager);
  300.         DisableItem(menu,iRealProgram);
  301.     }
  302. }
  303.  
  304. /**********************************************************************
  305. **  SimpleDoMenuCommand
  306. ***********************************************************************/
  307.  
  308. void TInspector::SimpleDoMenuCommand(short menuID, short menuItem)
  309. {
  310.     short        itemHit;
  311.     Str255        daName;
  312.     short        daRefNum;
  313.     WindowPtr    window;
  314.  
  315.     window = FrontWindow();
  316.     switch (menuID) {
  317.         case mApple:
  318.             switch (menuItem) {
  319.                 case iAbout:        // bring up alert for About 
  320.                     itemHit = Alert(rAboutAlert, NULL);
  321.                     break;
  322.                 default:            // all non-About items in this menu are DAs et al 
  323.                     GetMenuItemText(GetMenuHandle(mApple), menuItem, daName);
  324.                     daRefNum = OpenDeskAcc(daName);
  325.                     break;
  326.             }
  327.             break;
  328.  
  329.         case mFile:
  330.             switch (menuItem) {
  331.                 case iLoadLibraryManager:
  332.                     TInspector::LoadLibraryManager();
  333.                     break;
  334.                 case iUnloadLibraryManager:
  335.                     TInspector::UnloadLibraryManager();
  336.                     break;
  337.                 case iRealProgram:
  338.                     gDone = true;
  339.                     break;
  340.                 case iQuit:
  341.                     gDone = true;
  342.                     gQuit = true;
  343.                     break;
  344.             }
  345.             break;
  346.  
  347.         case mEdit:                    // call SystemEdit for DA editing & MultiFinder 
  348.             if (!SystemEdit(menuItem-1))
  349.                 DebugStr((ConstStr255Param)"\pOops.  SystemEdit returned false.");
  350.             break;
  351.     }
  352.     
  353.     HiliteMenu(0);                    // unhighlight what MenuSelect (or MenuKey) hilited 
  354. }
  355.  
  356. /**********************************************************************
  357. ** isKeyDown
  358. ***********************************************************************/
  359.  
  360. static Boolean isKeyDown(long keyCode)
  361. {
  362.     KeyMap keys;
  363.     Byte *keysBytes;
  364.     
  365.     GetKeys(keys);
  366.     
  367.     keysBytes = (Byte *)keys;
  368.  
  369.     return((keysBytes[keyCode/8] & (1 << keyCode % 8)) != 0);
  370. }
  371.  
  372. /**********************************************************************
  373. **  AlertUser
  374. ***********************************************************************/
  375.  
  376. void AlertUser(short errResID, short errCode)
  377. {
  378.     Str255 message;
  379.  
  380.     GetIndString(message, errResID, errCode);
  381.     #if qDebug
  382.     if (message[0] == 0)
  383.     {
  384.         DebugStr((ConstStr255Param)"\pAlertUser could not get error string.");
  385.         return;
  386.     }
  387.     #endif
  388.     ParamText(message, (ConstStr255Param)"\p", (ConstStr255Param)"\p", (ConstStr255Param)"\p");
  389.     Alert(rUserAlert, NULL);
  390. }
  391.  
  392. /**********************************************************************
  393. ** Main
  394. ***********************************************************************/
  395.  
  396. int TInspector::Main()
  397. {
  398. #if GENERATINGPOWERPC
  399.     if (isKeyDown(optionKeyCode)) {
  400.         DebugStr ((unsigned char *)"\pDebugStr to get us into the Nub debugger");
  401.     }
  402. #endif
  403.     if (!gToolboxInitialized) {
  404.         gToolboxInitialized = true;
  405.         InitGraf(&qd.thePort);
  406.         InitFonts();
  407.         InitWindows();
  408.         InitMenus();
  409.         TEInit();
  410.         InitDialogs(NULL);
  411.         InitCursor();
  412.     }
  413.  
  414.     if (isKeyDown(optionKeyCode)) {
  415.         TInspector::SimpleProgram();
  416.         if (gQuit)
  417.             return 0;
  418.     }
  419.  
  420.     TInspector::Boot();
  421.     
  422.     // Start our main event loop running.
  423.      while (!gQuit && gInspector) {
  424.         gInspector->EventLoop();
  425.         switch (gInspector->GetLibraryManagerCmd()) {
  426.             case kReloadLibraryManagerCmd: {
  427.                 TInspector::UnloadLibraryManager();    /* unload the ASLM */
  428.                 TInspector::LoadLibraryManager();    /* reload the ASLM */
  429.                 TInspector::Boot();                    /* Re-boot the TInspector */
  430.                 break;
  431.             }
  432.                 
  433.             case kUnloadLibraryManagerCmd: {
  434.                 TInspector::UnloadLibraryManager();    /* unload the ASLM */
  435.                 TInspector::SimpleProgram();        /* run the SimpleProgram */
  436.                 if (!gQuit)                    
  437.                     TInspector::Boot();                /* Re-boot the TInspector */
  438.                 break;
  439.             }
  440.             
  441.             case kGotoSimpleProgramCmd: {
  442.                 TInspector::Cleanup();            /* get rid of the app and friends */
  443.                 TInspector::SimpleProgram();    /* run the SimpleProgram */
  444.                 if (!gQuit)                    
  445.                     TInspector::Boot();            /* Re-boot the TInspector */
  446.                 break;
  447.             }
  448.             
  449.             default:
  450.                 gQuit = true;
  451.                 TInspector::Cleanup();
  452.                 break;
  453.         }
  454.     }
  455.     
  456.     // We always return a value, like good little ANSI worshippers
  457.     return 0;
  458. }
  459.