home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 November: Tool Chest / Dev.CD Nov 94.toast / Sample Code / Macintosh Sample Code / SC.014.CPlusTESample / Document.h < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-11  |  7.7 KB  |  255 lines  |  [TEXT/MPS ]

  1. /*------------------------------------------------------------------------------------------
  2.  
  3.     Program:    CPlusTESample 2.0
  4.     File:        Document.h
  5.     Uses:       Application.h
  6.                 List.h
  7.  
  8.     by Andrew Shebanow
  9.     of Apple Macintosh Developer Technical Support
  10.  
  11.     Copyright © 1989-1990 Apple Computer, Inc.
  12.     All rights reserved.
  13.  
  14. ------------------------------------------------------------------------------------------*/
  15.  
  16.  
  17. #ifndef __DOCUMENT__
  18. #define __DOCUMENT__
  19.  
  20. // Include necessary interface files
  21. #ifndef __TYPES__
  22. #include <Types.h>
  23. #endif
  24. #ifndef __QUICKDRAW__
  25. #include <QuickDraw.h>
  26. #endif
  27. #ifndef __WINDOWS__
  28. #include <Windows.h>
  29. #endif
  30. #ifndef __FILES__
  31. #include <Files.h>
  32. #endif
  33.  
  34.     /* canonical file specification */
  35.     struct CanonicalFileSpec
  36.     {
  37.         short        vRefNum;    /* volume reference number */
  38.         long        dirID;        /* directory ID */
  39.         Str63        fileName;    /* file name */
  40.     };
  41.  
  42. #ifndef __APPLICATION__
  43. #include "Application.h"
  44. #endif
  45. #ifndef __LIST__
  46. #include "List.h"
  47. #endif
  48.  
  49. // Define HiWord and LoWord inlines for efficiency.
  50. inline unsigned short HiWord(unsigned long val)
  51. {
  52.     return (unsigned short) ((val >> 16) & 0x0000FFFF);
  53. }
  54.  
  55. inline unsigned short LoWord(unsigned long val)
  56. {
  57.     return (unsigned short) (val & 0x0000FFFF);
  58. }
  59.  
  60. // max and min inlines
  61. inline max(long a, long b)
  62. {
  63.     return a > b ? a : b;
  64. }
  65.  
  66. inline min(long a, long b)
  67. {
  68.     return a < b ? a : b;
  69. }
  70.  
  71. // Define inlines to convert between Points & Rects for convenience.
  72. inline Point TopLeft(Rect r)
  73. {
  74.     Point pt;
  75.  
  76.     pt.v = r.top;
  77.     pt.h = r.left;
  78.     return pt;
  79. }
  80.  
  81. inline Point TopRight(Rect r)
  82. {
  83.     Point pt;
  84.  
  85.     pt.v = r.top;
  86.     pt.h = r.right;
  87.     return pt;
  88. }
  89.  
  90. inline Point BotLeft(Rect r)
  91. {
  92.     Point pt;
  93.  
  94.     pt.v = r.bottom;
  95.     pt.h = r.left;
  96.     return pt;
  97. }
  98.  
  99. inline Point BotRight(Rect r)
  100. {
  101.     Point pt;
  102.  
  103.     pt.v = r.bottom;
  104.     pt.h = r.right;
  105.     return pt;
  106. }
  107.  
  108. const long kMaxSleepTime = 60;    // 1 second worth of ticks
  109.  
  110. // we derive from handle object to prevent fragmentation
  111. class TDocument : public HandleObject {
  112. protected:
  113.     WindowPtr            fDocWindow;
  114.     CanonicalFileSpec    fFile;
  115.     short                fFileRefNum;
  116.     OSType                fFileType;
  117.     Boolean                fNewDoc;
  118.     Boolean                fReadOnly;
  119.     Boolean                fDirty;
  120.     Boolean                fActive;
  121.  
  122.     // internal file i/o routines - these are the guys that do the
  123.     // actual work of manipulating files
  124.     virtual void        OpenFile(Boolean readOnly, Boolean createIfNecessary);
  125.         // OpenFile opens the file referred to by fFile. If createIfNecessary
  126.         // is true and the file doesn't exist, it will be created.
  127.         // If readOnly is true, the fReadOnly field will be set correctly
  128.         // and the file will be opened with a read-only access path.
  129.         // You shouldn't need to override this routine.
  130.     virtual void        CloseFile();
  131.         // close file just closes the file specified by fFileRefNum
  132.         // You shouldn't need to override this routine.
  133.     virtual void        ReadFromFile(short refNum) = 0;
  134.         // this routine reads the file's data from the data fork
  135.         // specified by refNum. The file will already be open,
  136.         // and in the correct position for reading.
  137.         // You MUST override this routine.
  138.     virtual void        WriteToFile(short refNum) = 0;
  139.         // this routine writes the files data to the data fork
  140.         // specified by refNum. The file will already be open,
  141.         // and in the correct position for writing.
  142.         // You MUST override this routine.
  143.  
  144.     virtual YNCResult    PresentSaveDialog(Boolean quitting);
  145.         // this routine puts up the standard Save dialog,
  146.         // using the filename specified by fFile. The quitting
  147.         // parameter is used to make sure we have the right
  148.         // phrasing in the dialog box - either
  149.         // 'Save "x" before quitting?' or 'Save "x" before closing?'
  150.  
  151. public:
  152.     // Constructor & Destructor
  153.                         TDocument(short resID, OSType theFileType);
  154.         // creates new, untitled document using resID as window template
  155.     virtual                ~TDocument();
  156.         // our destructor - disposes of window
  157.  
  158.     // Routines to handle basic user interface events
  159.     // you MUST override these in your subclasses!!!
  160.     virtual void        DoZoom(short partCode) = 0;
  161.     virtual void        DoGrow(EventRecord* theEvent) = 0;
  162.     virtual void        DoContent(EventRecord* theEvent) = 0;
  163.     virtual void        DoKeyDown(EventRecord* theEvent) = 0;
  164.     virtual void        DoUpdate() = 0;
  165.  
  166.     // called when activating/deactivating document.
  167.     // default version just sets fActive variable, so
  168.     // you will need to override this to hilite your selection
  169.     // and such
  170.     virtual void        DoActivate(Boolean becomingActive);
  171.  
  172.     // these routines are called after a TDocument object has been
  173.     // created to initialize its contents. You probably won't need to
  174.     // override these, since they are fairly generic.
  175.     virtual void        OpenNewDoc() {};
  176.         // OpenNewDoc sets up a new, untitled document - by default, this
  177.         // has already been done in the TDocument constructor, so this routine
  178.         // is just a placeholder.
  179.     virtual void        OpenOldDoc(CanonicalFileSpec theFile, Boolean readOnly);
  180.         // OpenOldDoc opens a document, reads its contents, sets the window
  181.         // title, and so on.
  182.  
  183.     // high level, user-oriented file handling routines
  184.     // You probably won't need to override these
  185.     virtual YNCResult    DoClose(Boolean askUserToSave,
  186.                                 YNCResult defaultAnswer,
  187.                                 Boolean quitting);
  188.         // DoClose closes the document. If askUserToSave is true,
  189.         // the user is asked whether or not he wants to save the documents
  190.         // contents. If it is false, the document will be saved if defaultAnswer
  191.         // is yesResult.
  192.     virtual void        DoSave();
  193.         // DoSave saves the documents contents. If the document is a new,
  194.         // untitled document, DoSave will call DoSaveAs (below). Otherwise,
  195.         // it sets up the file for writing and calls WriteToFile to save the
  196.         // actual data.
  197.     virtual void        DoSaveAs();
  198.         // DoSaveAs asks the user for a file to save the document into.
  199.         // It saves the data in the same manner as DoSave, and sets up
  200.         // the file-related data members (fFile, fFileRefNum, etc).
  201.     virtual void        DoRevert() {};
  202.         // DoRevert is a hook for a revert routine. Currently, this
  203.         // isn't implemented, but it is here for future enhancement.
  204.     virtual void        DoPrint() {};
  205.         // we don't support printing yet either
  206.  
  207.     // standard edit menu actions
  208.     // with the exception of DoUndo, you MUST override these routines
  209.     virtual void        DoUndo() {};
  210.         // by default, undo is unimplemented, so you don't have to
  211.         // override this method
  212.     virtual void        DoCut() = 0;
  213.     virtual void        DoCopy() = 0;
  214.     virtual void        DoPaste() = 0;
  215.     virtual void        DoClear() = 0;
  216.     virtual void        DoSelectAll() = 0;
  217.  
  218.     // idle time routines: you can override these to do cursor handling,
  219.     // TE caret blinking, marquee effects, etc...
  220.     virtual void        DoIdle() {};
  221.     virtual unsigned long CalcIdle() { return kMaxSleepTime; };
  222.         // we never need idle in typical applications, so return a very large number
  223.     virtual void        AdjustCursor() {};
  224.         // where is in local coords
  225.  
  226.     // query state of document - useful for adjusting menu state
  227.     // You will probably need to override at least a few of
  228.     // these to accurately reflect the state of your document
  229.     virtual Boolean        HaveUndo() { return false; };
  230.     virtual Boolean        HaveSelection() { return false; };
  231.     virtual Boolean        HavePaste() { return false; };
  232.     virtual Boolean        CanClose() { return (FrontWindow() != nil); };
  233.     virtual Boolean        CanSave() { return fDirty; };
  234.     virtual Boolean        CanSaveAs() { return true; };
  235.     virtual Boolean        CanRevert() { return false; };    // not implemented
  236.     virtual Boolean        CanPrint() { return false; };    // not implemented
  237.  
  238.     // utility routine to get window pointer for document
  239.     inline WindowPtr    GetDocWindow() { return fDocWindow; }
  240. };
  241.  
  242. // TDocumentList is a simple linked list of documents,
  243. // implemented C++ style.
  244. class TDocumentList : public TList {
  245. public:
  246.     virtual void        AddDoc(TDocument* doc);
  247.     virtual void        RemoveDoc(TDocument* doc);
  248.     // find the TDocument associated with the window
  249.     virtual TDocument*    FindDoc(WindowPtr window);
  250.     // return number of active documents
  251.     inline int            NumDocs() { return Count(); }
  252. };
  253.  
  254. #endif
  255.