home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1998 / MacHack 1998.toast / Papers / C++ Exceptions / µShell / Toolbox / Toolbox.cp < prev    next >
Encoding:
Text File  |  1998-06-15  |  7.9 KB  |  370 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        ToolboxModules.cp
  3.  
  4.     Contains:    stuff
  5.  
  6. */
  7.  
  8. #include "Toolbox.h"
  9. #include "Exceptions.h"
  10.  
  11. #include <Gestalt.h>
  12. #include <Fonts.h>
  13. #include <Menus.h>
  14. #include <TextEdit.h>
  15. #include <Dialogs.h>
  16. #include <Threads.h>
  17.  
  18. //--------------------------------------------------------------------------------
  19.  
  20. ApplicationHeap*    ApplicationHeap::gApplicationHeap    = nil;
  21. Toolbox*            Toolbox::gToolbox                    = nil;
  22. bool                Toolbox::gInitialized                = false;
  23. bool                Toolbox::gCanInteract                = false;
  24. bool                Toolbox::gHasThreads                = false;
  25.  
  26. //--------------------------------------------------------------------------------
  27. // ApplicationHeap
  28. //--------------------------------------------------------------------------------
  29.  
  30. DefineClassSingle(ApplicationHeap);
  31.  
  32. #pragma segment AppInit
  33. //--------------------------------------------------------------------------------
  34. ApplicationHeap::ApplicationHeap(long stackSize) : TModule(true)
  35. {
  36.     WarnIf(gApplicationHeap != nil, "Instantiating a second ApplicationHeap");
  37.  
  38.     gApplicationHeap    = this;
  39.     fStackSize            = stackSize;
  40. }
  41.  
  42. #pragma segment AppCleanup
  43. //--------------------------------------------------------------------------------
  44. ApplicationHeap::~ApplicationHeap()
  45. {
  46.     gApplicationHeap = nil;
  47. }
  48.  
  49. #pragma segment AppInit
  50. //--------------------------------------------------------------------------------
  51. void ApplicationHeap::Initialize(void)
  52. {
  53.     Inherited::Initialize();
  54.  
  55.     // We'll need to set up the stack later
  56.  
  57.     MaxApplZone();
  58.  
  59.     fInitialized = true;
  60. }
  61.  
  62. #pragma segment AppInit
  63. //--------------------------------------------------------------------------------
  64. bool ApplicationHeap::Initialized(void)
  65. {
  66.     return fInitialized;
  67. }
  68.  
  69. #pragma segment AppInit
  70. //--------------------------------------------------------------------------------
  71. #if qDebug
  72. ApplicationHeap& ApplicationHeap::GetApplicationHeap()
  73. {
  74.     if (!gApplicationHeap)
  75.     {
  76.         Debugger();
  77.     }
  78.  
  79.     return *gApplicationHeap;
  80. }
  81. #endif
  82.  
  83. //--------------------------------------------------------------------------------
  84. // ApplicationHeap
  85. //--------------------------------------------------------------------------------
  86.  
  87. DefineClassSingle(Toolbox);
  88.  
  89. #pragma segment Main
  90. //--------------------------------------------------------------------------------
  91. Toolbox::Toolbox() : TModule(true)
  92. {
  93.     WarnIf(gToolbox != nil, "Instantiating a second ApplicationHeap");
  94.  
  95.     gToolbox = this;
  96. }
  97.  
  98. #pragma segment AppCleanup
  99. //--------------------------------------------------------------------------------
  100. Toolbox::~Toolbox()
  101. {
  102.     gToolbox = nil;
  103. }
  104.  
  105. #pragma segment AppInit
  106. //--------------------------------------------------------------------------------
  107. bool Toolbox::Validate(void)
  108. {
  109.     if (HasGestalt(gestaltThreadMgrAttr, 1 << gestaltThreadMgrPresent))
  110.     {
  111.         gHasThreads = HasCFMSymbol(NewThread);
  112.     }
  113.     
  114.     return true;
  115. }
  116.  
  117. #pragma segment AppInit
  118. //--------------------------------------------------------------------------------
  119. void Toolbox::Initialize(void)
  120. {
  121.     this->InitializeAfter(ApplicationHeap::GetApplicationHeap());
  122.  
  123.     Inherited::Initialize();
  124.  
  125.     InitGraf(&qd.thePort);
  126.     InitFonts();
  127.     
  128.     if (!qBackgroundOnly || qDebug)
  129.     {
  130.     #if qBackgroundOnly
  131.         ProcessSerialNumber psn = {0, kCurrentProcess};
  132.         ProcessInfoRec        info;
  133.         
  134.         info.processInfoLength    = sizeof(info);
  135.         info.processName        = nil;
  136.         info.processAppSpec        = nil;
  137.         
  138.         if ((GetProcessInformation(&psn, &info) == noErr)
  139.         &&     (info.processType == 'APPL))
  140.         {
  141.             // If we're debugging a background application, then initialize the
  142.             // managers anyway and create a menu bar with a quit item
  143.     #endif
  144.  
  145.             InitWindows();
  146.             InitMenus();
  147.             TEInit();
  148.             InitDialogs(nil);
  149.             InitCursor();
  150.             
  151.             gCanInteract = true;
  152.  
  153.     #if qBackgroundOnly
  154.             Handle mbar = GetResource('MBAR', 128);
  155.             
  156.             if (mbar != nil)
  157.             {
  158.                 mbar = GetNewMBar(128);
  159.             }
  160.             
  161.             if (mbar != nil)
  162.             {
  163.                 // if there's an MBAR resource, use it
  164.             
  165.                 SetMenuBar(mbar);
  166.             }
  167.             else
  168.             {
  169.                 // otherwise, build one
  170.             
  171.                 MenuHandle menu = NewMenu(1, "\p\024");
  172.                 
  173.                 if (menu != nil)
  174.                 {
  175.                     AddResMenu(menu, 'DRVR');
  176.                     InsertMenu(menu, 0);
  177.                 }
  178.                 
  179.                 menu = NewMenu(2, "File");
  180.  
  181.                 if (menu != nil)
  182.                 {
  183.                     AppendMenu(menu, "\pQuit/Q");
  184.                     InsertMenu(menu, 0);
  185.                 }
  186.             }
  187.     
  188.             DrawMenuBar();
  189.         }
  190.     #endif
  191.     }
  192.     
  193.     gInitialized    = true;
  194.  
  195. //    fHasColorQuickdraw = HasGestalt(gestaltQuickdrawFeatures, gestaltHasColor);
  196. //    fHasDisplayManager = HasGestalt(gestaltDisplayMgrAttr, gestaltDisplayMgrPresent);
  197. }
  198.  
  199. #pragma segment AppInit
  200. //--------------------------------------------------------------------------------
  201. bool Toolbox::Initialized(void)
  202. {
  203.     return gInitialized;
  204. }
  205.  
  206. //--------------------------------------------------------------------------------
  207.  
  208. #if qDebug
  209. Toolbox& Toolbox::GetToolbox()
  210. {
  211.     Assert(gToolbox != nil);
  212.  
  213.     return *gToolbox;
  214. }
  215. #endif
  216.  
  217. //--------------------------------------------------------------------------------
  218.  
  219. bool Toolbox::HasGestalt(OSType selector, long mask)
  220. {
  221.     long result;
  222.     
  223.     OSErr err = Gestalt(selector, &result);
  224.     
  225.     return (err == noErr) && ((result & mask) == mask);
  226. }
  227.  
  228. //--------------------------------------------------------------------------------
  229.  
  230. short Toolbox::FindWindow(Point where, WindowPtr& win)
  231. {
  232.     // Don't call the window manager if InitWindows() hasn't been called
  233.  
  234.     return gCanInteract ? ::FindWindow(where, &win) : inNoWindow;
  235. }
  236.  
  237. //--------------------------------------------------------------------------------
  238. // ToolboxManager
  239. //--------------------------------------------------------------------------------
  240.  
  241. #pragma segment Main
  242. //--------------------------------------------------------------------------------
  243. ToolboxManager::ToolboxManager(
  244.     OSType        gestaltSelector,
  245.     short        /*gestaltBit*/,
  246. #if GENERATINGCFM
  247.     ProcPtr        entryPoint,
  248. #else
  249.     ProcPtr        /*entryPoint*/,
  250. #endif
  251.     bool        required)
  252. :    fGestaltSelector(gestaltSelector),
  253.     fGestaltValue(0),
  254. #if GENERATINGCFM
  255.     fLibraryEntry(entryPoint),
  256. #endif
  257.     fGestaltCalled(false),
  258.     fGestaltValid(false),
  259.     fLibraryChecked(false),
  260.     fLibraryPresent(false),
  261. //    fRuntimeChecked(false),
  262. //    fRuntimeResult(false),
  263.     fInitialized(false),
  264.     fInitializeCalled(false),
  265.     fAvailable(false),
  266.     fRequired(required)
  267. {
  268. }
  269.  
  270. #pragma segment AppInit
  271. //--------------------------------------------------------------------------------
  272. void ToolboxManager::RuntimeCheck(void)
  273. {
  274.     bool fAvailable = true;
  275.  
  276.     // first do the Gestalt call
  277.     if (!fGestaltCalled)
  278.     {
  279.         OSErr err = Gestalt(fGestaltSelector, &fGestaltValue);
  280.     
  281.         if (err == noErr)
  282.         {
  283.             fGestaltValid = true;
  284.             
  285.             if (fGestaltRequired)
  286.             {
  287.                 if (!(fGestaltValue & (1 << fGestaltBit)))
  288.                     fAvailable = false;
  289.             }
  290.             else
  291.             {
  292.                 fAvailable = true;
  293.             }
  294.         }
  295.         else
  296.         {
  297.             fGestaltValue = 0;
  298.         }
  299.         
  300.         fGestaltCalled = true;
  301.     }
  302.     
  303.     // second look for a shared library
  304. #if GENERATINGCFM
  305.     if (fLibraryEntry != nil)
  306.     {
  307.         // was if (entryPoint != (ProcPtr) kUnresolvedSymbolAddress)
  308.         DebugStr("\pNot Yet Implement - PPC Compiler Quirk");
  309. //        if (fLibraryEntry != (ProcPtr) kUnresolvedSymbolAddress)
  310. //            fLibraryPresent = true;
  311.     }
  312.     else
  313.     {
  314.         fLibraryPresent = true;
  315.     }
  316.     
  317.     fLibraryChecked    = true;
  318. #endif
  319.     
  320.     if (fLibraryRequired)
  321.         fAvailable = fAvailable && fLibraryPresent;
  322. }
  323.  
  324.  
  325. #pragma segment AppInit
  326. //--------------------------------------------------------------------------------
  327. bool ToolboxManager::Validate(void)
  328. {
  329.     return !fRequired || fAvailable;
  330. }
  331.  
  332.  
  333. #pragma segment AppInit
  334. //--------------------------------------------------------------------------------
  335. bool ToolboxManager::Initialized(void)
  336. {
  337.     return fInitialized;
  338. }
  339.     
  340. #pragma segment AppInit
  341. //--------------------------------------------------------------------------------
  342. bool ToolboxManager::InitializeCalled(void)
  343. {
  344.     return fInitializeCalled;
  345. }
  346.  
  347.  
  348. #pragma segment AppInit
  349. //--------------------------------------------------------------------------------
  350. void ToolboxManager::Initialize(void)
  351. {
  352.     Inherited::Initialize();
  353.  
  354.     if (fAvailable && !fInitializeCalled)
  355.     {
  356.  
  357.         this->DoInitialization();
  358.         fInitializeCalled = true;
  359.     }
  360. }
  361.  
  362.  
  363. #pragma segment AppInit
  364. //--------------------------------------------------------------------------------
  365. void ToolboxManager::DoInitialization(void)
  366. {
  367.     fInitialized = true;
  368. }
  369.  
  370.