home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / Developer University / DU Projects / DataCopy / Sources / Content.cpp < prev    next >
Encoding:
Text File  |  1996-08-22  |  4.7 KB  |  175 lines  |  [TEXT/CWIE]

  1. //    Release Version:    $ ODF 2 $
  2. //    Copyright:            (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  3.  
  4. //========================================================================================
  5. #include "DataCopy.hpp"
  6.  
  7. #ifndef CONTENT_H
  8. #include "Content.h"
  9. #endif
  10.  
  11. #ifndef PART_H
  12. #include "Part.h"
  13. #endif
  14.  
  15. #ifndef PIZZA_H
  16. #include "Pizza.h"            //  CPizza
  17. #endif
  18.  
  19. // ----- Framework Layer -----
  20. #ifndef FWEVENTU_H
  21. #include "FWEventU.h"        // FW_IsCommandKeyPressed
  22. #endif
  23.  
  24. #ifndef FWKIND_H            // FW_CKind
  25. #include "FWKind.h"
  26. #endif
  27.  
  28. //--- OpenDoc ------------------------------------------------------------------
  29. #ifndef SOM_Module_OpenDoc_StdProps_defined
  30. #include <StdProps.xh>        // kODPropContents
  31. #endif
  32.  
  33. //========================================================================================
  34. #ifdef FW_BUILD_MAC
  35. #pragma segment DataCopy
  36. #endif
  37.  
  38. //========================================================================================
  39. FW_DEFINE_AUTO(CPizzaCollection)
  40. FW_DEFINE_AUTO(CPizzaCollectionIterator)
  41. FW_DEFINE_AUTO(CDataCopyContent)
  42.  
  43. //----------------------------------------------------------------------------------------
  44. CDataCopyContent::CDataCopyContent(Environment* ev, CDataCopyPart* part)
  45.  :    FW_CContent(ev, part),
  46.     fPart(part),
  47.     fNumPizzas(0),
  48.     fPizzaList(NULL)
  49. {
  50.     fPizzaList = FW_NEW(CPizzaCollection, ());
  51.     FW_END_CONSTRUCTOR
  52. }
  53.  
  54. //----------------------------------------------------------------------------------------
  55. CDataCopyContent::~CDataCopyContent()
  56. {
  57.     FW_START_DESTRUCTOR
  58.     this->MyRemoveAllPizzas();
  59.     delete fPizzaList;
  60. }
  61.  
  62. //----------------------------------------------------------------------------------------
  63. CPizzaCollection*
  64. CDataCopyContent::MyGetPizzaList()
  65. {
  66.     return fPizzaList;
  67. }
  68.  
  69. //----------------------------------------------------------------------------------------
  70. void
  71. CDataCopyContent::MyIncrement(Environment* ev, FW_CPoint& position)
  72. {
  73.     fNumPizzas++;
  74.     FW_CPoint halfSize( FW_IntToFixed(15), FW_IntToFixed(15) );
  75.     FW_CRect bounds(position - halfSize, position + halfSize);
  76.     CPizza* pizza = new CPizza(bounds);
  77.     fPizzaList->AddLast(pizza);
  78.     fPart->Changed(ev);
  79.     fPart->MyInvalidatePresentation(ev, bounds);
  80. }
  81.  
  82. //----------------------------------------------------------------------------------------
  83. void
  84. CDataCopyContent::MyAddPizza(Environment* ev, CPizza* pizza)
  85. {
  86.     fNumPizzas++;
  87.     fPizzaList->AddLast(pizza);
  88.     fPart->MyInvalidatePresentation(ev, pizza->GetBounds());
  89. }
  90.  
  91. //----------------------------------------------------------------------------------------
  92. void
  93. CDataCopyContent::MyRemoveLastPizza(Environment* ev, CPizza* pizza)
  94. {
  95.     fNumPizzas--;
  96.     fPizzaList->RemoveLast();
  97.     fPart->MyInvalidatePresentation(ev, pizza->GetBounds());
  98. }
  99.  
  100. //----------------------------------------------------------------------------------------
  101. void
  102. CDataCopyContent::MyRemoveAllPizzas()
  103. {
  104.     CPizza* pizza = NULL;
  105.     while ((pizza = (CPizza*)fPizzaList->First()) != NULL) {
  106.         fPizzaList->Remove(pizza);
  107.         delete pizza;
  108.         }
  109.     fNumPizzas = 0;
  110. }
  111.  
  112. //----------------------------------------------------------------------------------------
  113. void 
  114. CDataCopyContent::ExternalizeKind(Environment* ev,
  115.                                 ODStorageUnit* storageUnit,
  116.                                 FW_CKind* kind,
  117.                                 FW_StorageKinds storageKind,
  118.                                 FW_CPromise* promise,
  119.                                 FW_CCloneInfo* cloneInfo)
  120. {
  121.     FW_UNUSED(cloneInfo);
  122.     FW_UNUSED(kind);
  123.     FW_UNUSED(storageKind);
  124.     FW_UNUSED(promise);
  125.     FW_PStorageUnitSink suSink(ev, storageUnit, kODPropContents, kind->GetType(ev));
  126.     FW_CWritableStream stream(suSink);
  127.  
  128.     // write number
  129.     stream << fNumPizzas;
  130.     
  131.     // write each item
  132.     CPizzaCollectionIterator iter(fPizzaList);
  133.     CPizza* pizza = NULL;
  134.     for (pizza = iter.First(); iter.IsNotComplete(); pizza = iter.Next())
  135.         FW_WRITE_DYNAMIC_OBJECT(stream, pizza, CPizza);
  136.  
  137. #ifdef FW_BUILD_MAC
  138.     if (::FW_IsCommandKeyPressed()) {
  139.         FW_LOG_MESSAGE("Number of pizzas =");
  140.         Str255 numString;
  141.         ::NumToString(fNumPizzas, numString);
  142.         DebugStr(numString);
  143.     }
  144. #endif
  145. }
  146.  
  147. //----------------------------------------------------------------------------------------
  148. FW_Boolean 
  149. CDataCopyContent::InternalizeKind(Environment* ev,
  150.                                     ODStorageUnit* storageUnit, 
  151.                                     FW_CKind* kind,
  152.                                     FW_StorageKinds storageKind,
  153.                                     FW_CCloneInfo* cloneInfo)
  154. {
  155.     FW_UNUSED(cloneInfo);
  156.     FW_PStorageUnitSink suSink(ev, storageUnit, kODPropContents, kind->GetType(ev));
  157.     FW_CReadableStream stream(suSink);    
  158.  
  159.     this->MyRemoveAllPizzas();    // remove current pizzas in case of Paste
  160.  
  161.     // read number
  162.     stream >> fNumPizzas;
  163.     // read items; make list
  164.     CPizza* pizza = NULL;
  165.     for (long count = 0; count < fNumPizzas; count++) {
  166.         FW_READ_DYNAMIC_OBJECT(stream, &pizza, CPizza);
  167.         fPizzaList->AddLast(pizza);
  168.         }
  169.     if (storageKind != FW_kPartStorage) {
  170.         fPart->MyInvalidatePresentation(ev);
  171. //        fPart->Changed(ev);
  172.         }
  173.     return true;
  174. }
  175.