home *** CD-ROM | disk | FTP | other *** search
Text File | 1998-06-15 | 7.9 KB | 370 lines | [TEXT/CWIE] |
- /*
- File: ToolboxModules.cp
-
- Contains: stuff
-
- */
-
- #include "Toolbox.h"
- #include "Exceptions.h"
-
- #include <Gestalt.h>
- #include <Fonts.h>
- #include <Menus.h>
- #include <TextEdit.h>
- #include <Dialogs.h>
- #include <Threads.h>
-
- //--------------------------------------------------------------------------------
-
- ApplicationHeap* ApplicationHeap::gApplicationHeap = nil;
- Toolbox* Toolbox::gToolbox = nil;
- bool Toolbox::gInitialized = false;
- bool Toolbox::gCanInteract = false;
- bool Toolbox::gHasThreads = false;
-
- //--------------------------------------------------------------------------------
- // ApplicationHeap
- //--------------------------------------------------------------------------------
-
- DefineClassSingle(ApplicationHeap);
-
- #pragma segment AppInit
- //--------------------------------------------------------------------------------
- ApplicationHeap::ApplicationHeap(long stackSize) : TModule(true)
- {
- WarnIf(gApplicationHeap != nil, "Instantiating a second ApplicationHeap");
-
- gApplicationHeap = this;
- fStackSize = stackSize;
- }
-
- #pragma segment AppCleanup
- //--------------------------------------------------------------------------------
- ApplicationHeap::~ApplicationHeap()
- {
- gApplicationHeap = nil;
- }
-
- #pragma segment AppInit
- //--------------------------------------------------------------------------------
- void ApplicationHeap::Initialize(void)
- {
- Inherited::Initialize();
-
- // We'll need to set up the stack later
-
- MaxApplZone();
-
- fInitialized = true;
- }
-
- #pragma segment AppInit
- //--------------------------------------------------------------------------------
- bool ApplicationHeap::Initialized(void)
- {
- return fInitialized;
- }
-
- #pragma segment AppInit
- //--------------------------------------------------------------------------------
- #if qDebug
- ApplicationHeap& ApplicationHeap::GetApplicationHeap()
- {
- if (!gApplicationHeap)
- {
- Debugger();
- }
-
- return *gApplicationHeap;
- }
- #endif
-
- //--------------------------------------------------------------------------------
- // ApplicationHeap
- //--------------------------------------------------------------------------------
-
- DefineClassSingle(Toolbox);
-
- #pragma segment Main
- //--------------------------------------------------------------------------------
- Toolbox::Toolbox() : TModule(true)
- {
- WarnIf(gToolbox != nil, "Instantiating a second ApplicationHeap");
-
- gToolbox = this;
- }
-
- #pragma segment AppCleanup
- //--------------------------------------------------------------------------------
- Toolbox::~Toolbox()
- {
- gToolbox = nil;
- }
-
- #pragma segment AppInit
- //--------------------------------------------------------------------------------
- bool Toolbox::Validate(void)
- {
- if (HasGestalt(gestaltThreadMgrAttr, 1 << gestaltThreadMgrPresent))
- {
- gHasThreads = HasCFMSymbol(NewThread);
- }
-
- return true;
- }
-
- #pragma segment AppInit
- //--------------------------------------------------------------------------------
- void Toolbox::Initialize(void)
- {
- this->InitializeAfter(ApplicationHeap::GetApplicationHeap());
-
- Inherited::Initialize();
-
- InitGraf(&qd.thePort);
- InitFonts();
-
- if (!qBackgroundOnly || qDebug)
- {
- #if qBackgroundOnly
- ProcessSerialNumber psn = {0, kCurrentProcess};
- ProcessInfoRec info;
-
- info.processInfoLength = sizeof(info);
- info.processName = nil;
- info.processAppSpec = nil;
-
- if ((GetProcessInformation(&psn, &info) == noErr)
- && (info.processType == 'APPL))
- {
- // If we're debugging a background application, then initialize the
- // managers anyway and create a menu bar with a quit item
- #endif
-
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(nil);
- InitCursor();
-
- gCanInteract = true;
-
- #if qBackgroundOnly
- Handle mbar = GetResource('MBAR', 128);
-
- if (mbar != nil)
- {
- mbar = GetNewMBar(128);
- }
-
- if (mbar != nil)
- {
- // if there's an MBAR resource, use it
-
- SetMenuBar(mbar);
- }
- else
- {
- // otherwise, build one
-
- MenuHandle menu = NewMenu(1, "\p\024");
-
- if (menu != nil)
- {
- AddResMenu(menu, 'DRVR');
- InsertMenu(menu, 0);
- }
-
- menu = NewMenu(2, "File");
-
- if (menu != nil)
- {
- AppendMenu(menu, "\pQuit/Q");
- InsertMenu(menu, 0);
- }
- }
-
- DrawMenuBar();
- }
- #endif
- }
-
- gInitialized = true;
-
- // fHasColorQuickdraw = HasGestalt(gestaltQuickdrawFeatures, gestaltHasColor);
- // fHasDisplayManager = HasGestalt(gestaltDisplayMgrAttr, gestaltDisplayMgrPresent);
- }
-
- #pragma segment AppInit
- //--------------------------------------------------------------------------------
- bool Toolbox::Initialized(void)
- {
- return gInitialized;
- }
-
- //--------------------------------------------------------------------------------
-
- #if qDebug
- Toolbox& Toolbox::GetToolbox()
- {
- Assert(gToolbox != nil);
-
- return *gToolbox;
- }
- #endif
-
- //--------------------------------------------------------------------------------
-
- bool Toolbox::HasGestalt(OSType selector, long mask)
- {
- long result;
-
- OSErr err = Gestalt(selector, &result);
-
- return (err == noErr) && ((result & mask) == mask);
- }
-
- //--------------------------------------------------------------------------------
-
- short Toolbox::FindWindow(Point where, WindowPtr& win)
- {
- // Don't call the window manager if InitWindows() hasn't been called
-
- return gCanInteract ? ::FindWindow(where, &win) : inNoWindow;
- }
-
- //--------------------------------------------------------------------------------
- // ToolboxManager
- //--------------------------------------------------------------------------------
-
- #pragma segment Main
- //--------------------------------------------------------------------------------
- ToolboxManager::ToolboxManager(
- OSType gestaltSelector,
- short /*gestaltBit*/,
- #if GENERATINGCFM
- ProcPtr entryPoint,
- #else
- ProcPtr /*entryPoint*/,
- #endif
- bool required)
- : fGestaltSelector(gestaltSelector),
- fGestaltValue(0),
- #if GENERATINGCFM
- fLibraryEntry(entryPoint),
- #endif
- fGestaltCalled(false),
- fGestaltValid(false),
- fLibraryChecked(false),
- fLibraryPresent(false),
- // fRuntimeChecked(false),
- // fRuntimeResult(false),
- fInitialized(false),
- fInitializeCalled(false),
- fAvailable(false),
- fRequired(required)
- {
- }
-
- #pragma segment AppInit
- //--------------------------------------------------------------------------------
- void ToolboxManager::RuntimeCheck(void)
- {
- bool fAvailable = true;
-
- // first do the Gestalt call
- if (!fGestaltCalled)
- {
- OSErr err = Gestalt(fGestaltSelector, &fGestaltValue);
-
- if (err == noErr)
- {
- fGestaltValid = true;
-
- if (fGestaltRequired)
- {
- if (!(fGestaltValue & (1 << fGestaltBit)))
- fAvailable = false;
- }
- else
- {
- fAvailable = true;
- }
- }
- else
- {
- fGestaltValue = 0;
- }
-
- fGestaltCalled = true;
- }
-
- // second look for a shared library
- #if GENERATINGCFM
- if (fLibraryEntry != nil)
- {
- // was if (entryPoint != (ProcPtr) kUnresolvedSymbolAddress)
- DebugStr("\pNot Yet Implement - PPC Compiler Quirk");
- // if (fLibraryEntry != (ProcPtr) kUnresolvedSymbolAddress)
- // fLibraryPresent = true;
- }
- else
- {
- fLibraryPresent = true;
- }
-
- fLibraryChecked = true;
- #endif
-
- if (fLibraryRequired)
- fAvailable = fAvailable && fLibraryPresent;
- }
-
-
- #pragma segment AppInit
- //--------------------------------------------------------------------------------
- bool ToolboxManager::Validate(void)
- {
- return !fRequired || fAvailable;
- }
-
-
- #pragma segment AppInit
- //--------------------------------------------------------------------------------
- bool ToolboxManager::Initialized(void)
- {
- return fInitialized;
- }
-
- #pragma segment AppInit
- //--------------------------------------------------------------------------------
- bool ToolboxManager::InitializeCalled(void)
- {
- return fInitializeCalled;
- }
-
-
- #pragma segment AppInit
- //--------------------------------------------------------------------------------
- void ToolboxManager::Initialize(void)
- {
- Inherited::Initialize();
-
- if (fAvailable && !fInitializeCalled)
- {
-
- this->DoInitialization();
- fInitializeCalled = true;
- }
- }
-
-
- #pragma segment AppInit
- //--------------------------------------------------------------------------------
- void ToolboxManager::DoInitialization(void)
- {
- fInitialized = true;
- }
-
-