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 / DataSave / Sources / Content.cpp < prev    next >
Encoding:
Text File  |  1996-08-16  |  4.0 KB  |  150 lines  |  [TEXT/CWIE]

  1. //    Release Version:    $ ODF 1 $
  2. //    Copyright:            (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  3.  
  4. //========================================================================================
  5. #include "DataSave.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 DataSave
  30. #endif
  31.  
  32. //========================================================================================
  33. FW_DEFINE_AUTO(CPizzaCollection)
  34. FW_DEFINE_AUTO(CPizzaCollectionIterator)
  35. FW_DEFINE_AUTO(CDataSaveContent)
  36.  
  37. //----------------------------------------------------------------------------------------
  38. CDataSaveContent::CDataSaveContent(Environment* ev, CDataSavePart* 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. CDataSaveContent::~CDataSaveContent()
  50. {
  51.     FW_START_DESTRUCTOR
  52.     CPizza* pizza = NULL;
  53.     while ((pizza = (CPizza*)fPizzaList->First()) != NULL) {
  54.         fPizzaList->Remove(pizza);
  55.         delete pizza;
  56.         }
  57.     delete fPizzaList;
  58. }
  59.  
  60. //----------------------------------------------------------------------------------------
  61. CPizzaCollection*
  62. CDataSaveContent::MyGetPizzaList()
  63. {
  64.     return fPizzaList;
  65. }
  66.  
  67. //----------------------------------------------------------------------------------------
  68. void
  69. CDataSaveContent::MyIncrement(Environment* ev, FW_CPoint& position)
  70. {
  71.     fNumPizzas++;
  72.     FW_CPoint halfSize( FW_IntToFixed(15), FW_IntToFixed(15) );
  73.     FW_CRect bounds(position - halfSize, position + halfSize);
  74.     CPizza* pizza = new CPizza(bounds);
  75.     fPizzaList->AddLast(pizza);
  76.     fPart->Changed(ev);
  77.     fPart->MyInvalidatePresentation(ev, bounds);
  78. }
  79.  
  80. //----------------------------------------------------------------------------------------
  81. void
  82. CDataSaveContent::MyAddPizza(Environment* ev, CPizza* pizza)
  83. {
  84.     fNumPizzas++;
  85.     fPizzaList->AddLast(pizza);
  86.     fPart->MyInvalidatePresentation(ev, pizza->GetBounds());
  87. }
  88.  
  89. //----------------------------------------------------------------------------------------
  90. void
  91. CDataSaveContent::MyRemoveLastPizza(Environment* ev, CPizza* pizza)
  92. {
  93.     fNumPizzas--;
  94.     fPizzaList->RemoveLast();
  95.     fPart->MyInvalidatePresentation(ev, pizza->GetBounds());
  96. }
  97.  
  98. //----------------------------------------------------------------------------------------
  99. void
  100. CDataSaveContent::Externalize(Environment* ev,
  101.                             ODStorageUnit* storageUnit,
  102.                             FW_EStorageKinds storageKind,
  103.                             FW_CCloneInfo* cloneInfo)
  104. {
  105.     FW_UNUSED(cloneInfo);
  106.     FW_UNUSED(storageKind);
  107.     FW_PStorageUnitSink suSink(ev, storageUnit, kODPropContents, fPart->GetPartKind(ev));
  108.     FW_CWritableStream stream(suSink);
  109.  
  110.     // write number
  111.     stream << fNumPizzas;
  112.     
  113.     // write each item
  114.     CPizzaCollectionIterator iter(fPizzaList);
  115.     CPizza* pizza = NULL;
  116.     for (pizza = iter.First(); iter.IsNotComplete(); pizza = iter.Next())
  117.         FW_WRITE_DYNAMIC_OBJECT(stream, pizza, CPizza);
  118.  
  119. #ifdef FW_BUILD_MAC
  120.     if (::FW_IsCommandKeyPressed()) {
  121.         FW_LOG_MESSAGE("Number of pizzas =");
  122.         Str255 numString;
  123.         ::NumToString(fNumPizzas, numString);
  124.         DebugStr(numString);
  125.     }
  126. #endif
  127. }
  128.  
  129. //----------------------------------------------------------------------------------------
  130. FW_Boolean 
  131. CDataSaveContent::Internalize(Environment* ev,
  132.                                             ODStorageUnit* storageUnit, 
  133.                                             FW_EStorageKinds storageKind,
  134.                                             FW_CCloneInfo* cloneInfo)
  135. {
  136.     FW_UNUSED(cloneInfo);
  137.     FW_UNUSED(storageKind);
  138.     FW_PStorageUnitSink suSink(ev, storageUnit, kODPropContents, fPart->GetPartKind(ev));
  139.     FW_CReadableStream stream(suSink);    
  140.     // read number
  141.     stream >> fNumPizzas;
  142.     // read items; make list
  143.     CPizza* pizza = NULL;
  144.     for (long count = 0; count < fNumPizzas; count++) {
  145.         FW_READ_DYNAMIC_OBJECT(stream, &pizza, CPizza);
  146.         fPizzaList->AddLast(pizza);
  147.         }
  148.     return true;
  149. }
  150.