home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / developm / scnote / cplussmp.014 / TDocument.h < prev    next >
Encoding:
C/C++ Source or Header  |  1989-10-01  |  6.0 KB  |  169 lines

  1. /*------------------------------------------------------------------------------
  2. #
  3. #    Apple Macintosh Developer Technical Support
  4. #
  5. #    MultiFinder-Aware Simple TextEdit Sample Application
  6. #
  7. #    CPlusTESample
  8. #
  9. #    TDocument.h        -    C++ source
  10. #
  11. #    Copyright ⌐ 1989 Apple Computer, Inc.
  12. #    All rights reserved.
  13. #
  14. #    Versions:    
  15. #            1.20                    10/89
  16. #            1.10                     07/89
  17. #            1.00                     04/89
  18. #    
  19. #    Components:
  20. #            CPlusTESample.make        October 1, 1989
  21. #            TApplicationCommon.h    October 1, 1989
  22. #            TApplication.h            October 1, 1989
  23. #            TDocument.h                October 1, 1989
  24. #            TECommon.h                October 1, 1989
  25. #            TESample.h                October 1, 1989
  26. #            TEDocument.h            October 1, 1989
  27. #            TApplication.cp            October 1, 1989
  28. #            TDocument.cp            October 1, 1989
  29. #            TESample.cp                October 1, 1989
  30. #            TEDocument.cp            October 1, 1989
  31. #            TESampleGlue.a            October 1, 1989
  32. #            TApplication.r            October 1, 1989
  33. #            TESample.r                October 1, 1989
  34. #
  35. #    CPlusTESample is an example application that demonstrates
  36. #    how to initialize the commonly used toolbox managers,
  37. #    operate successfully under MultiFinder, handle desk
  38. #    accessories and create, grow, and zoom windows. The
  39. #    fundamental TextEdit toolbox calls and TextEdit autoscroll
  40. #    are demonstrated. It also shows how to create and maintain
  41. #    scrollbar controls. 
  42. #
  43. #    This version of TESample has been substantially reworked in
  44. #    C++ to show how a "typical" object oriented program could
  45. #    be written. To this end, what was once a single source code
  46. #    file has been restructured into a set of classes which
  47. #    demonstrate the advantages of object-oriented programming.
  48. #
  49. ------------------------------------------------------------------------------*/
  50.  
  51. #ifndef __TDOCUMENT__
  52. #define __TDOCUMENT__
  53.  
  54. // Include necessary interface files
  55. #include <Types.h>
  56. #include <QuickDraw.h>
  57.  
  58. // Define HiWrd and LoWrd macros for efficiency
  59. #define HiWrd(aLong)    ((short) (((aLong) >> 16) & 0xFFFF))
  60. #define LoWrd(aLong)    ((short) ((aLong) & 0xFFFF))
  61.  
  62. // Define TopLeft and BotRight macros for convenience. Notice the implicit
  63. // dependency on the ordering of fields within a Rect
  64. #define TopLeft(aRect)    (* (Point *) &(aRect).top)
  65. #define BotRight(aRect)    (* (Point *) &(aRect).bottom)
  66.  
  67. const long kMaxSleepTime = 60;    // 1 second worth of ticks
  68.  
  69. // we derive from handle object to prevent fragmentation
  70. class TDocument : HandleObject {
  71. protected:
  72.     WindowPtr fDocWindow;
  73.  
  74. public:
  75.     TDocument(short resID);        // our constructor - creates window using resID as template
  76.     virtual ~TDocument();        // our destructor - disposes of window
  77.  
  78.     // you will need to override these in your subclasses,
  79.     // since they are do-nothing routines by default...
  80.     virtual void DoZoom(short partCode) {};
  81.     virtual void DoGrow(EventRecord* theEvent) {};
  82.     virtual void DoContent(EventRecord* theEvent) {};
  83.     virtual void DoKeyDown(EventRecord* theEvent) {};
  84.     virtual void DoActivate(Boolean becomingActive) {};
  85.     virtual void DoUpdate() {};
  86.     // file handling routines
  87.     virtual void DoOpen() {};
  88.     virtual void DoClose() { delete this; };    // by default, we just delete ourself & let destructor do cleanup
  89.     virtual void DoSave() {};
  90.     virtual void DoSaveAs() {};
  91.     virtual void DoRevert() {};
  92.     virtual void DoPrint() {};
  93.     // do standard edit menu actions
  94.     virtual void DoUndo() {};
  95.     virtual void DoCut() {};
  96.     virtual void DoCopy() {};
  97.     virtual void DoPaste() {};
  98.     virtual void DoClear() {};
  99.     virtual void DoSelectAll() {};
  100.  
  101.     // idle time routines: you can use these to do cursor handling,
  102.     // TE caret blinking, marquee effects, etc...
  103.     virtual void DoIdle() {};
  104.     virtual unsigned long CalcIdle() { return kMaxSleepTime; };    // by default, we don't need idle
  105.     virtual void AdjustCursor(Point where) {};            // where is in local coords
  106.  
  107.     // query state of document - useful for adjusting menu state
  108.     virtual Boolean HaveUndo() { return false; };
  109.     virtual Boolean HaveSelection() { return false; };
  110.     virtual Boolean HavePaste() { return false; };
  111.     virtual Boolean CanClose() { return true; };
  112.     virtual Boolean CanSave() { return false; };
  113.     virtual Boolean CanSaveAs() { return true; };
  114.     virtual Boolean CanRevert() { return false; };
  115.     virtual Boolean CanPrint() { return false; };
  116.  
  117.     // utility routine to get window pointer for document
  118.     inline WindowPtr GetDocWindow() { return fDocWindow; };
  119. };
  120.  
  121. // TDocumentLink is a simple utility class which is used by
  122. // the TDocumentList class below. You cannot allocate
  123. // objects of this type yourself, since its constructor
  124. // is private. We make TDocumentList a "friend" of this class,
  125. // so it is the only class that can allocate objects of
  126. // this class. This is a useful technique for ensuring that
  127. // a class library "knows" about all objects of a certain
  128. // class.
  129. class TDocumentLink {
  130. private:
  131.     friend class TDocumentList;
  132.  
  133.     TDocumentLink*            fNext;    // the link to the next document
  134.     TDocument*                fDoc;    // the document this link refers to
  135.  
  136.     // our constructor. Note that it can take args for convenience,
  137.     // but that they default to nil.
  138.     TDocumentLink(TDocumentLink* n = nil, TDocument* v = nil);
  139.  
  140.     // implementation of our TDocumentLink routines is done inline, for speed
  141.     inline TDocumentLink*    GetNext() { return fNext; };
  142.     inline TDocument*         GetDoc() { return fDoc; };
  143.     inline void                SetNext(TDocumentLink* aLink) { fNext = aLink; };
  144.     inline void             SetDoc(TDocument* aDoc) { fDoc = aDoc; };
  145. };
  146.  
  147. // TDocumentList is a simple linked list of documents, implemented C++
  148. // style. I could have made a general linked list class & just made
  149. // this a subclass. This would have been a more general (and more
  150. // object-oriented) solution, but I did it from scratch in a futile
  151. // attempt at keeping the size of this program at a reasonable level.
  152. class TDocumentList {
  153. private:
  154.     TDocumentLink*    fDocList;    // the first link in our list
  155.     int                fNumDocs;    // the number of elements in the list
  156.  
  157. public:
  158.     TDocumentList();    // our constructor
  159.  
  160.     virtual void        AddDoc(TDocument* doc);
  161.     virtual void        RemoveDoc(TDocument* doc);
  162.     // find the TDocument associated with the window
  163.     virtual TDocument*    FindDoc(WindowPtr window);
  164.     // return number of active documents
  165.     inline int            NumDocs() { return fNumDocs; }
  166. };
  167.  
  168. #endif __TDOCUMENT__
  169.