home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / developm / scnote / cplussmp.014 / TDocument.cp < prev    next >
Encoding:
Text File  |  1989-10-01  |  4.4 KB  |  171 lines

  1. /*------------------------------------------------------------------------------
  2. #
  3. #    Apple Macintosh Developer Technical Support
  4. #
  5. #    MultiFinder-Aware Simple TextEdit Sample Application
  6. #
  7. #    CPlusTESample
  8. #
  9. #    TDocument.cp    -    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. /*
  52. Segmentation strategy:
  53.  
  54.     This program has only one segment, since the issues
  55.     surrounding segmentation within a class's methods have
  56.     not been investigated yet. We DO unload the data
  57.     initialization segment at startup time, which frees up
  58.     some memory 
  59.  
  60. SetPort strategy:
  61.  
  62.     Toolbox routines do not change the current port. In
  63.     spite of this, in this program we use a strategy of
  64.     calling SetPort whenever we want to draw or make calls
  65.     which depend on the current port. This makes us less
  66.     vulnerable to bugs in other software which might alter
  67.     the current port (such as the bug (feature?) in many
  68.     desk accessories which change the port on OpenDeskAcc).
  69.     Hopefully, this also makes the routines from this
  70.     program more self-contained, since they don't depend on
  71.     the current port setting. 
  72.  
  73. Clipboard strategy:
  74.  
  75.     This program does not maintain a private scrap.
  76.     Whenever a cut, copy, or paste occurs, we import/export
  77.     from the public scrap to TextEdit's scrap right away,
  78.     using the TEToScrap and TEFromScrap routines. If we did
  79.     use a private scrap, the import/export would be in the
  80.     activate/deactivate event and suspend/resume event
  81.     routines. 
  82. */
  83.  
  84. // Mac Includes
  85. #include <Types.h>
  86. #include <QuickDraw.h>
  87. #include <Fonts.h>
  88. #include <Events.h>
  89. #include <Controls.h>
  90. #include <Windows.h>
  91. #include <Menus.h>
  92. #include <TextEdit.h>
  93. #include <Dialogs.h>
  94. #include <Desk.h>
  95. #include <Scrap.h>
  96. #include <ToolUtils.h>
  97. #include <Memory.h>
  98. #include <SegLoad.h>
  99. #include <Files.h>
  100. #include <OSUtils.h>
  101. #include <Traps.h>
  102.  
  103. #include "TDocument.h"
  104.  
  105. TDocument::TDocument(short resID)
  106. {
  107.     fDocWindow = GetNewWindow(resID,nil,(WindowPtr) -1);
  108.     SetPort(fDocWindow);
  109. }
  110.  
  111. TDocument::~TDocument()
  112. {
  113.     DisposeWindow(fDocWindow);
  114. }
  115.  
  116. TDocumentLink::TDocumentLink(TDocumentLink* n, TDocument* v)
  117. {
  118.     fNext = n;
  119.     fDoc = v;
  120. }
  121.  
  122. TDocumentList::TDocumentList()
  123. {
  124.     fDocList = nil;
  125.     fNumDocs = 0;
  126. }
  127.  
  128. // find the TDocument associated with the window
  129. TDocument* TDocumentList::FindDoc(WindowPtr window)
  130. {
  131.     TDocumentLink*    temp;
  132.     TDocument*        tDoc;
  133.  
  134.     for (temp = fDocList; temp != nil; temp = temp->GetNext())
  135.       {
  136.         tDoc = temp->GetDoc();
  137.         if (tDoc->GetDocWindow() == window)
  138.           return tDoc;
  139.       }
  140.     return nil;
  141. }
  142.  
  143. // private list management routines
  144. void TDocumentList::AddDoc(TDocument* doc)
  145. {
  146.     TDocumentLink* temp;
  147.  
  148.     temp = new TDocumentLink(fDocList,doc);
  149.     fDocList = temp;
  150.     fNumDocs++;
  151. }
  152.  
  153. void TDocumentList::RemoveDoc(TDocument* doc)
  154. {
  155.     TDocumentLink* temp;
  156.     TDocumentLink* last;
  157.  
  158.     last = nil;
  159.     for (temp = fDocList; temp != nil; temp = temp->GetNext())
  160.       if (temp->GetDoc() == doc)
  161.         {
  162.           if (last == nil)                // if first item in list, just set first
  163.             fDocList = temp->GetNext();
  164.           else last->SetNext(temp->GetNext());
  165.           delete temp;                    // free the TDocumentLink
  166.           fNumDocs--;
  167.           return;
  168.         }
  169.       else last = temp;
  170. }
  171.