home *** CD-ROM | disk | FTP | other *** search
- /*------------------------------------------------------------------------------
- #
- # Apple Macintosh Developer Technical Support
- #
- # MultiFinder-Aware Simple TextEdit Sample Application
- #
- # CPlusTESample
- #
- # TDocument.h - C++ source
- #
- # Copyright ⌐ 1989 Apple Computer, Inc.
- # All rights reserved.
- #
- # Versions:
- # 1.20 10/89
- # 1.10 07/89
- # 1.00 04/89
- #
- # Components:
- # CPlusTESample.make October 1, 1989
- # TApplicationCommon.h October 1, 1989
- # TApplication.h October 1, 1989
- # TDocument.h October 1, 1989
- # TECommon.h October 1, 1989
- # TESample.h October 1, 1989
- # TEDocument.h October 1, 1989
- # TApplication.cp October 1, 1989
- # TDocument.cp October 1, 1989
- # TESample.cp October 1, 1989
- # TEDocument.cp October 1, 1989
- # TESampleGlue.a October 1, 1989
- # TApplication.r October 1, 1989
- # TESample.r October 1, 1989
- #
- # CPlusTESample is an example application that demonstrates
- # how to initialize the commonly used toolbox managers,
- # operate successfully under MultiFinder, handle desk
- # accessories and create, grow, and zoom windows. The
- # fundamental TextEdit toolbox calls and TextEdit autoscroll
- # are demonstrated. It also shows how to create and maintain
- # scrollbar controls.
- #
- # This version of TESample has been substantially reworked in
- # C++ to show how a "typical" object oriented program could
- # be written. To this end, what was once a single source code
- # file has been restructured into a set of classes which
- # demonstrate the advantages of object-oriented programming.
- #
- ------------------------------------------------------------------------------*/
-
- #ifndef __TDOCUMENT__
- #define __TDOCUMENT__
-
- // Include necessary interface files
- #include <Types.h>
- #include <QuickDraw.h>
-
- // Define HiWrd and LoWrd macros for efficiency
- #define HiWrd(aLong) ((short) (((aLong) >> 16) & 0xFFFF))
- #define LoWrd(aLong) ((short) ((aLong) & 0xFFFF))
-
- // Define TopLeft and BotRight macros for convenience. Notice the implicit
- // dependency on the ordering of fields within a Rect
- #define TopLeft(aRect) (* (Point *) &(aRect).top)
- #define BotRight(aRect) (* (Point *) &(aRect).bottom)
-
- const long kMaxSleepTime = 60; // 1 second worth of ticks
-
- // we derive from handle object to prevent fragmentation
- class TDocument : HandleObject {
- protected:
- WindowPtr fDocWindow;
-
- public:
- TDocument(short resID); // our constructor - creates window using resID as template
- virtual ~TDocument(); // our destructor - disposes of window
-
- // you will need to override these in your subclasses,
- // since they are do-nothing routines by default...
- virtual void DoZoom(short partCode) {};
- virtual void DoGrow(EventRecord* theEvent) {};
- virtual void DoContent(EventRecord* theEvent) {};
- virtual void DoKeyDown(EventRecord* theEvent) {};
- virtual void DoActivate(Boolean becomingActive) {};
- virtual void DoUpdate() {};
- // file handling routines
- virtual void DoOpen() {};
- virtual void DoClose() { delete this; }; // by default, we just delete ourself & let destructor do cleanup
- virtual void DoSave() {};
- virtual void DoSaveAs() {};
- virtual void DoRevert() {};
- virtual void DoPrint() {};
- // do standard edit menu actions
- virtual void DoUndo() {};
- virtual void DoCut() {};
- virtual void DoCopy() {};
- virtual void DoPaste() {};
- virtual void DoClear() {};
- virtual void DoSelectAll() {};
-
- // idle time routines: you can use these to do cursor handling,
- // TE caret blinking, marquee effects, etc...
- virtual void DoIdle() {};
- virtual unsigned long CalcIdle() { return kMaxSleepTime; }; // by default, we don't need idle
- virtual void AdjustCursor(Point where) {}; // where is in local coords
-
- // query state of document - useful for adjusting menu state
- virtual Boolean HaveUndo() { return false; };
- virtual Boolean HaveSelection() { return false; };
- virtual Boolean HavePaste() { return false; };
- virtual Boolean CanClose() { return true; };
- virtual Boolean CanSave() { return false; };
- virtual Boolean CanSaveAs() { return true; };
- virtual Boolean CanRevert() { return false; };
- virtual Boolean CanPrint() { return false; };
-
- // utility routine to get window pointer for document
- inline WindowPtr GetDocWindow() { return fDocWindow; };
- };
-
- // TDocumentLink is a simple utility class which is used by
- // the TDocumentList class below. You cannot allocate
- // objects of this type yourself, since its constructor
- // is private. We make TDocumentList a "friend" of this class,
- // so it is the only class that can allocate objects of
- // this class. This is a useful technique for ensuring that
- // a class library "knows" about all objects of a certain
- // class.
- class TDocumentLink {
- private:
- friend class TDocumentList;
-
- TDocumentLink* fNext; // the link to the next document
- TDocument* fDoc; // the document this link refers to
-
- // our constructor. Note that it can take args for convenience,
- // but that they default to nil.
- TDocumentLink(TDocumentLink* n = nil, TDocument* v = nil);
-
- // implementation of our TDocumentLink routines is done inline, for speed
- inline TDocumentLink* GetNext() { return fNext; };
- inline TDocument* GetDoc() { return fDoc; };
- inline void SetNext(TDocumentLink* aLink) { fNext = aLink; };
- inline void SetDoc(TDocument* aDoc) { fDoc = aDoc; };
- };
-
- // TDocumentList is a simple linked list of documents, implemented C++
- // style. I could have made a general linked list class & just made
- // this a subclass. This would have been a more general (and more
- // object-oriented) solution, but I did it from scratch in a futile
- // attempt at keeping the size of this program at a reasonable level.
- class TDocumentList {
- private:
- TDocumentLink* fDocList; // the first link in our list
- int fNumDocs; // the number of elements in the list
-
- public:
- TDocumentList(); // our constructor
-
- virtual void AddDoc(TDocument* doc);
- virtual void RemoveDoc(TDocument* doc);
- // find the TDocument associated with the window
- virtual TDocument* FindDoc(WindowPtr window);
- // return number of active documents
- inline int NumDocs() { return fNumDocs; }
- };
-
- #endif __TDOCUMENT__
-