Application.h

/*------------------------------------------------------------------------------------------
 
    Program:    CPlusTESample 2.0
    File:       Application.h
    Uses:       AppLib.h
 
    by Andrew Shebanow
    of Apple Macintosh Developer Technical Support
 
    Copyright © 1989-1990 Apple Computer, Inc.
    All rights reserved.
 
------------------------------------------------------------------------------------------*/
 
#ifndef __APPLICATION__
#define __APPLICATION__
 
// Include necessary interface files
#ifndef __STRING__
#include <String.h>
#endif
 
#ifndef __TYPES__
#include <Types.h>
#endif
#ifndef __MENUS__
#include <Menus.h>
#endif
#ifndef __DEVICES__
#include <Devices.h>
#endif
#ifndef __EVENTS__
#include <Events.h>
#endif
#ifndef __OSUTILS__
#include <OSUtils.h>
#endif
 
// we need resource ids
#ifndef __APPLIB__
#include "AppLib.h"
#endif
 
// we need the exceptions code
#ifndef __EXCEPTIONS__
#include "Exceptions.h"
#endif
 
class TDocument;
class TDocumentList;
 
// YNCResult is a type used to indicate a result from
// a user operation. YNC means yes/no/cancel, but yes
// and no can also have synonyms like Save and Discard.
enum YNCResult { yesResult, noResult, cancelResult };
 
/*
    TApplication:
 
    This is our class which implements a basic Macintosh style program,
    including a MultiFinder-aware event loop.
*/
 
// we derive from handle object to prevent fragmentation
class TApplication : public HandleObject {
private:
    static OSType fCreator;
 
public:
    // Our constructor
    TApplication(OSType creator);
 
    // process finder arguments
    virtual void    ProcessArgs();
 
    // Call this routine to start event loop running
    // It will not return until the user quits the application (via a call
    // to DoQuit, below)
    virtual void    EventLoop();
 
    // Utility routines you can use
    static Boolean          TrapAvailable(short tNumber,TrapType tType);
        // Is trap implemented???
    inline TDocumentList*   DocList() { return fDocList; };
    static OSType           GetCreator() { return fCreator; };
    static void             WDToDirID(short wdRefNum, short& vRefNum, long& dirID);
        // utility routine to convert a working directory to a vRefNum/dirID pair
 
protected:
    virtual long    StackNeeded() { return 0; };
        // Returns total stack space required in bytes.
        // Returns 0 by default, which tells the initialization code
        // to use the default stack size.
    virtual long    HeapNeeded() { return 0; };
        // Returns total heap space required in bytes.
        // Returns 0 by default, which tells the initialization code
        // to use whatever heap size is given.
 
    // called by EventLoop to process an individual event
    virtual void    ProcessEvent();
 
    // Loop control methods you may need to override
    virtual void    SetUp() {};             // Run before event loop starts
    virtual void    DoIdle() {};            // idle time handler (blink caret, background tasks)
    virtual void    AdjustMenus() {};       // menu updater routine
 
    // event handlers you shouldn't need to override in a typical application
    virtual void    DoOSEvent();
        // Calls DoSuspend, DoResume and DoIdle as apropos
    virtual void    DoMouseDown();
        // Calls DoContent, DoGrow, DoZoom, etc
    virtual void    DoKeyDown();
        // also called for autokey events
    virtual void    DoActivateEvt();
        // handles setup, and calls DoActivate (below)
    virtual void    DoUpdateEvt();
        // handles setup, and calls DoUpdate (below)
    virtual void    DoMouseInSysWindow() { SystemClick(&fTheEvent, fWhichWindow); };
    virtual void    DoDrag();
    virtual void    DoGoAway();
        // handles setup, and calls TDocument::DoClose
    virtual void    DoQuit(Boolean askUser, YNCResult defaultResult);
 
    // handlers you MUST override for functionality:
    virtual void    AdjustCursor() = 0;
        // cursor adjust routine, should setup mouseRgn
    virtual void    DoMenuCommand(short menuID, short menuItem) = 0;
    virtual void    DoNew() = 0;
    virtual void    DoOpen() = 0;
    virtual void    OpenADoc(short vRefNum, long dirID,
                             StringPtr fName, OSType fType) = 0;
 
    // called by OSEvent (just calls DoActivate by default, so no clip conversion
    // is done). If you want to convert clipboard, override these routines
    virtual void    DoSuspend(Boolean doClipConvert);
    virtual void    DoResume(Boolean doClipConvert);
 
    // If you have an app that needs to know about these, override them
    virtual void    DoMouseUp() {};
    virtual void    DoDiskEvt() {};
 
    // Utility routines you need to provide to do MultiFinder stuff
    virtual unsigned long SleepVal() { return 0; };
        // how long to sleep in WaitNextEvent
 
    // useful variables
    Boolean         fHaveWaitNextEvent;     // true if we have WaitNextEvent trap
    Boolean         fDone;                  // set to true when we are ready to quit
    Boolean         fOnSystem7;             // running system 7 or better
    EventRecord     fTheEvent;              // our event record
    WindowPtr       fWhichWindow;           // currently active window
    Boolean         fInBackground;          // true if our app is suspended
    Boolean         fWantFrontClicks;       // true if we want front clicks
    RgnHandle       fMouseRgn;              // mouse moved region (set it in your DoIdle)
    TDocument*      fCurDoc;                // currently active document (if any)
    TDocumentList*  fDocList;               // the list of documents
};
 
// some other handy utility routines, not actually a part of application class
 
// display alert, using specified error STR# resource and error code as index
void AlertUser(short errResID, short errCode);
// call AlertUser to display error message, then quit...
void BigBadError(short errResID, short errCode);
Boolean LookupErrorString(short value, short resID, StringPtr str);
 
// useful state checking Booleans (mostly for System 7 compatibility)
extern Boolean gHaveColorQD;
 
inline void CopyPString(StringPtr to, StringPtr from)
{
    memcpy(to,from,(size_t) (from[0] & 0x0ff) + 1);
}
 
#endif