•Mac_Classes/TBackGroundApp.cp

//  TBackGroundApp.cp -  Macintosh Background Application class object
// 
// Apple Macintosh Developer Technical Support
// Written by:  Vinne Moscaritolo
//
//  Copyright (work in progress)  Apple Computer, Inc All rights reserved.
//
// You may incorporate this sample code into your applications without
// restriction, though the sample code has been provided "AS IS" and the
// responsibility for its operation is 100% yours.  However, what you are
// not permitted to do is to redistribute the source as "DSC Sample Code"
// after having made changes. If you're going to re-distribute the source,
// we require that you make it clear in the source that the code was
// descended from Apple Sample Code, but that you've made changes.
// 
 
#include "TBackGroundApp.h" 
#include "TMacException.h"
 
// Global Functions
pascal OSErr AEDispatcher(AppleEvent* in,  AppleEvent* out, long Command);
 
// ---------------------------------------------------------------------------
//   TBackGroundApp::fgApplication
// ---------------------------------------------------------------------------
//  pointer to BackGround App object
//
TBackGroundApp*  TBackGroundApp::fgApplication; 
 
 
// ---------------------------------------------------------------------------
//   TBackGroundApp
// ---------------------------------------------------------------------------
//  Default Constructor
 
TBackGroundApp::TBackGroundApp()
{
    TBackGroundApp::fgApplication = this;
    
    this->SetState(TBackGroundApp::kSInit);             // set out first state
    fSleepTime =    kSleep_1_Sec;                       // default sleep value (ticks)
 
    this->InitializeMemory();                           // initialize memory
    this->InstallAEHandler();                           // install our AE handler
}
 
 
// ---------------------------------------------------------------------------
//   ~TBackGroundApp
// ---------------------------------------------------------------------------
//  Destructor
 
TBackGroundApp::~TBackGroundApp()
{
}
 
// ---------------------------------------------------------------------------
//   TBackGroundApp::InitializeMemory
// ---------------------------------------------------------------------------
// initialize memory
void TBackGroundApp::InitializeMemory()
{
    const unsigned long kStackIncrementSize =  (24 * 1024);
    
 // Increase the space allocated for the application stack by lowering the heap limit.
    SetApplLimit((Ptr) ((unsigned long) GetApplLimit() - kStackIncrementSize));
    ThrowIfOSErr( MemError() );
    MaxApplZone();
}
 
// ---------------------------------------------------------------------------
//   TBackGroundApp::InitializeToolbox
// ---------------------------------------------------------------------------
// initialize Required mac toolbox
void TBackGroundApp::InitializeToolbox()
{
 
// Intialize sections need to support Notification Manager
    InitGraf((Ptr) &qd.thePort);
 
// _DON'T_ flush disk-inserted or os events or you'll be sorry! 
    ::FlushEvents(everyEvent - diskMask - osMask, 0);
}
 
// ---------------------------------------------------------------------------
//   TBackGroundApp::DoNextEvent
// ---------------------------------------------------------------------------
//   Handle incoming events
 
 
void TBackGroundApp::DoNextEvent()
{
    if (!::WaitNextEvent(everyEvent, &fEventRecord, fSleepTime, 0L))
        fEventRecord.what = nullEvent;          // security issue
}
 
// ---------------------------------------------------------------------------
//   TBackGroundApp::DoEventLoop
// ---------------------------------------------------------------------------
// event loop swith statement functio
 
void TBackGroundApp::DoEventLoop()
{
    this->DoNextEvent();                        // get the next event record
 
    switch (fEventRecord.what)
    {
        case nullEvent:                         // we got a periodic null event
            this->DoIdle();                     // call idle handler
            break;
 
        case kHighLevelEvent:
            this->DoHighLevelEvent();           // handle our high level events (as AEs)
            break;
 
        default:
            break;
    }
};
 
// ---------------------------------------------------------------------------
//   TBackGroundApp::DoIdle
// ---------------------------------------------------------------------------
// Handle idle time
 
void TBackGroundApp::DoIdle()
{
}
 
 
// ---------------------------------------------------------------------------
//   TBackGroundApp::DoHighLevelEvent
// ---------------------------------------------------------------------------
// Handle high level events.
 
void TBackGroundApp::DoHighLevelEvent()
{
    ::AEProcessAppleEvent(&fEventRecord);
}
 
 
// ---------------------------------------------------------------------------
//   TBackGroundApp::Start
// ---------------------------------------------------------------------------
// Start the application and let it run until we change the state to Quit.
 
void TBackGroundApp::Start()
{
    this->SetState(TBackGroundApp::kSRun);          // set TBackGroundApp to run state
 
    while (fState != TBackGroundApp::kSQuit)
        this->DoEventLoop();
    this->Quit();                                   // out from the state, we will quit
}
 
// ---------------------------------------------------------------------------
//   TBackGroundApp::Quit
// ---------------------------------------------------------------------------
// Quit the application, do any possible cleanup.
void TBackGroundApp::Quit()
{
}
 
// ---------------------------------------------------------------------------
//   TBackGroundApp::InstallAEHandler
// ---------------------------------------------------------------------------
// install Apple event handlers
void TBackGroundApp::InstallAEHandler()
{
    
    ThrowIfOSErr( ::AEInstallEventHandler( kCoreEventClass, kAEOpenApplication,
                    NewAEEventHandlerProc(AEDispatcher), kAppOpen, false));
 
    ThrowIfOSErr( ::AEInstallEventHandler( kCoreEventClass, kAEOpenDocuments,
                    NewAEEventHandlerProc(AEDispatcher), kAppOpenDocuments, false));
 
    ThrowIfOSErr( ::AEInstallEventHandler( kCoreEventClass, kAEPrintDocuments,
                    NewAEEventHandlerProc(AEDispatcher), kAppPrint, false));
 
    ThrowIfOSErr( ::AEInstallEventHandler( kCoreEventClass, kAEQuitApplication,
                    NewAEEventHandlerProc(AEDispatcher), kAppQuit, false));
 
}
 
// ---------------------------------------------------------------------------
//   TBackGroundApp::SetState
// ---------------------------------------------------------------------------
//  STATE CHANGE METHODS
void TBackGroundApp::SetState(TBackGroundApp::EState theState)
{
    fState = theState;
}
 
// ---------------------------------------------------------------------------
//   AEDispatcher
// ---------------------------------------------------------------------------
//  AppleEvent Dispatcher Wrapper
 
pascal OSErr AEDispatcher(AppleEvent* in,  AppleEvent* out, long Command)
{
    return TBackGroundApp::fgApplication->DispatchAppleEvents(in, out, Command);        // dispatch event back to the framework
}
 
// ---------------------------------------------------------------------------
//   TBackGroundApp::DispatchAppleEvents
// ---------------------------------------------------------------------------
// Dispatch to the right Handler based on the command in the AE (refCon).
 
OSErr TBackGroundApp::DispatchAppleEvents(AppleEvent* in, AppleEvent* out, long command)
{
    switch (command)
    {
        case kAppQuit:                          // a quit AE
            return HandleQuit(in, out, command);
 
        case kAppOpen:                          // a  new AE
            return HandleOpen(in, out, command);
 
        case kAppOpenDocuments:                 // an open document AE
            return HandleOpenDocuments(in, out, command);
 
        case kAppPrint:                         // a print AE
            return HandlePrint(in, out, command);
 
        default:                                                // hmm, something else thenÉ
            ThrowMsg("Problem: We are dealing with an AE command that we have no handler for");
            break;
    }
    return errAEEventNotHandled;
}
 
// ---------------------------------------------------------------------------
//   TBackGroundApp::HandleQuit
// ---------------------------------------------------------------------------
// Our General Quit Handler. Set state to Quit and return.
 
OSErr TBackGroundApp::HandleQuit(AppleEvent*,AppleEvent*,long)
{
    this->SetState(TBackGroundApp::kSQuit);     // set state to Quit
    return noErr;                               // need to have this due to AEHandler prototype
}
 
// ---------------------------------------------------------------------------
//   TBackGroundApp::HandleOpen
// ---------------------------------------------------------------------------
// handle the open AE
 
OSErr TBackGroundApp::HandleOpen(AppleEvent*,AppleEvent*,long)
{
    return errAEEventNotHandled;                
}
 
// ---------------------------------------------------------------------------
//   TBackGroundApp::HandleOpenDocuments
// ---------------------------------------------------------------------------
// handle the OpenDocuments AE
 
OSErr TBackGroundApp::HandleOpenDocuments(AppleEvent*,AppleEvent*,long)
{
    return errAEEventNotHandled;                
}
 
// ---------------------------------------------------------------------------
//   TBackGroundApp::HandlePrint
// ---------------------------------------------------------------------------
// handle the Print AE
 
OSErr TBackGroundApp::HandlePrint(AppleEvent*,AppleEvent*,long)
{
    return errAEEventNotHandled;                
}