home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 October: Mac OS SDK / Dev.CD Oct 96 SDK / Dev.CD Oct 96 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / Developer University / DUProjects / DataCopy / Sources / Content.cpp < prev    next >
Encoding:
Text File  |  1996-08-16  |  4.4 KB  |  164 lines  |  [TEXT/CWIE]

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