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

  1. //    Release Version:    $ ODF 2 $
  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 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 DataSave
  36. #endif
  37.  
  38. //========================================================================================
  39. FW_DEFINE_AUTO(CPizzaCollection)
  40. FW_DEFINE_AUTO(CPizzaCollectionIterator)
  41. FW_DEFINE_AUTO(CDataSaveContent)
  42.  
  43. //----------------------------------------------------------------------------------------
  44. CDataSaveContent::CDataSaveContent(Environment* ev, CDataSavePart* 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. CDataSaveContent::~CDataSaveContent()
  56. {
  57.     FW_START_DESTRUCTOR
  58.     CPizza* pizza = NULL;
  59.     while ((pizza = fPizzaList->First()) != NULL) {
  60.         fPizzaList->Remove(pizza);
  61.         delete pizza;
  62.         }
  63.     delete fPizzaList;
  64. }
  65.  
  66. //----------------------------------------------------------------------------------------
  67. CPizzaCollection*
  68. CDataSaveContent::MyGetPizzaList()
  69. {
  70.     return fPizzaList;
  71. }
  72.  
  73. //----------------------------------------------------------------------------------------
  74. void
  75. CDataSaveContent::MyIncrement(Environment* ev, FW_CPoint& position)
  76. {
  77.     fNumPizzas++;
  78.     FW_CPoint halfSize( FW_IntToFixed(15), FW_IntToFixed(15) );
  79.     FW_CRect bounds(position - halfSize, position + halfSize);
  80.     CPizza* pizza = new CPizza(bounds);
  81.     fPizzaList->AddLast(pizza);
  82.     fPart->Changed(ev);
  83.     fPart->MyInvalidatePresentation(ev, bounds);
  84. }
  85.  
  86. //----------------------------------------------------------------------------------------
  87. void
  88. CDataSaveContent::MyAddPizza(Environment* ev, CPizza* pizza)
  89. {
  90.     fNumPizzas++;
  91.     fPizzaList->AddLast(pizza);
  92.     fPart->MyInvalidatePresentation(ev, pizza->GetBounds());
  93. }
  94.  
  95. //----------------------------------------------------------------------------------------
  96. void
  97. CDataSaveContent::MyRemoveLastPizza(Environment* ev, CPizza* pizza)
  98. {
  99.     fNumPizzas--;
  100.     fPizzaList->RemoveLast();
  101.     fPart->MyInvalidatePresentation(ev, pizza->GetBounds());
  102. }
  103.  
  104. //----------------------------------------------------------------------------------------
  105. void
  106. CDataSaveContent::ExternalizeKind(Environment* ev,
  107.                                 ODStorageUnit* storageUnit,
  108.                                 FW_CKind* kind,
  109.                                 FW_StorageKinds storageKind,
  110.                                 FW_CPromise* promise,
  111.                                 FW_CCloneInfo* cloneInfo)
  112. {
  113.     FW_UNUSED(cloneInfo);
  114.     FW_UNUSED(storageKind);
  115.     FW_UNUSED(promise);
  116.     FW_PStorageUnitSink suSink(ev, storageUnit, kODPropContents, kind->GetType(ev));
  117.     FW_CWritableStream stream(suSink);
  118.  
  119.     // write number
  120.     stream << fNumPizzas;
  121.     
  122.     // write each item
  123.     CPizzaCollectionIterator iter(fPizzaList);
  124.     CPizza* pizza = NULL;
  125.     for (pizza = iter.First(); iter.IsNotComplete(); pizza = iter.Next())
  126.         FW_WRITE_DYNAMIC_OBJECT(stream, pizza, CPizza);
  127.  
  128. #ifdef FW_BUILD_MAC
  129.     if (::FW_IsCommandKeyPressed()) {
  130.         FW_LOG_MESSAGE("Number of pizzas =");
  131.         Str255 numString;
  132.         ::NumToString(fNumPizzas, numString);
  133.         DebugStr(numString);
  134.     }
  135. #endif
  136. }
  137.  
  138. //----------------------------------------------------------------------------------------
  139. FW_Boolean 
  140. CDataSaveContent::InternalizeKind(Environment* ev,
  141.                                     ODStorageUnit* storageUnit, 
  142.                                     FW_CKind* kind,
  143.                                     FW_StorageKinds storageKind,
  144.                                     FW_CCloneInfo* cloneInfo)
  145. {
  146.     FW_UNUSED(cloneInfo);
  147.     FW_UNUSED(storageKind);
  148.     FW_PStorageUnitSink suSink(ev, storageUnit, kODPropContents, kind->GetType(ev));
  149.     FW_CReadableStream stream(suSink);    
  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.     return true;
  159. }
  160.