home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 July: Mac OS SDK / Dev.CD Jul 96 SDK / Dev.CD Jul 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc Development Framework / ODFDev / Hello / Sources / Content.cpp < prev    next >
Encoding:
Text File  |  1996-04-25  |  7.5 KB  |  236 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                Content.cpp
  4. //    Release Version:    $ ODF 1 $
  5. //
  6. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "Hello.hpp"
  11.  
  12. #ifndef CONTENT_H
  13. #include "Content.h"
  14. #endif
  15.  
  16. #ifndef PART_H
  17. #include "Part.h"
  18. #endif
  19.  
  20. #ifndef FWUTIL_H
  21. #include "FWUtil.h"
  22. #endif
  23.  
  24. //========================================================================================
  25. //    Runtime info
  26. //========================================================================================
  27.  
  28. #ifdef FW_BUILD_MAC
  29. #pragma segment odfhello
  30. #endif
  31.  
  32. FW_DEFINE_AUTO(CHelloContent)
  33.  
  34. //========================================================================================
  35. //    CHelloContent class
  36. //========================================================================================
  37.  
  38. //----------------------------------------------------------------------------------------
  39. // CHelloContent constructor
  40. //----------------------------------------------------------------------------------------
  41.  
  42. CHelloContent::CHelloContent(Environment* ev, CHelloPart* part) :
  43.     FW_CContent(ev, part),
  44.     fTextData(""),
  45.     fCentered(FALSE),
  46.     fHelloPart(part)
  47. {
  48.     // ----- Initialize the text data strings -----
  49.     FW_CSharedLibraryResourceFile resFile(ev);
  50.  
  51.     FW_CString32 platformString;
  52.     ::FW_LoadStringByID(ev, resFile, kHelloPartStrings, MULTISTRINGRES, kFirstString, fTextData);
  53.     ::FW_LoadStringByID(ev, resFile, kHelloPartStrings, MULTISTRINGRES, kPlatformString, platformString);
  54.     fTextData += platformString;
  55. }
  56.  
  57. //----------------------------------------------------------------------------------------
  58. // CHelloContent destructor
  59. //----------------------------------------------------------------------------------------
  60.  
  61. CHelloContent::~CHelloContent()
  62. {
  63. }
  64.  
  65. //----------------------------------------------------------------------------------------
  66. // CHelloContent::GetTextData
  67. //----------------------------------------------------------------------------------------
  68.  
  69. const FW_CString& CHelloContent::GetTextData()
  70. {
  71.     return fTextData;
  72. }
  73.  
  74. //----------------------------------------------------------------------------------------
  75. // CHelloContent::SetTextData
  76. //----------------------------------------------------------------------------------------
  77.  
  78. void CHelloContent::SetTextData(Environment* ev, const FW_CString& newText)
  79. {
  80.     fTextData = newText;
  81.     fHelloPart->PartChanged(ev);
  82. }
  83.  
  84. //----------------------------------------------------------------------------------------
  85. // CHelloContent::ClearTextData
  86. //----------------------------------------------------------------------------------------
  87.  
  88. void CHelloContent::ClearTextData(Environment* ev)
  89. {
  90. /*    FW_CString32 newString;    [MEB] Jim - reveals bug in Strings when trying to ReplaceAll with a string that's too big */
  91.     FW_CString newString;
  92.     FW_CSharedLibraryResourceFile resFile(ev);
  93.     ::FW_LoadStringByID(ev, resFile, kHelloPartStrings, MULTISTRINGRES, kBlankString, newString);
  94.  
  95.     fTextData = newString;
  96.     fHelloPart->PartChanged(ev);                    // Notify the proper authorities
  97. }
  98.  
  99. //----------------------------------------------------------------------------------------
  100. // CHelloContent::Externalize
  101. //----------------------------------------------------------------------------------------
  102.  
  103. void CHelloContent::Externalize(Environment* ev,
  104.                                  ODStorageUnit* storageUnit,
  105.                                  FW_EStorageKinds storageKind,
  106.                                  FW_CCloneInfo* cloneInfo)
  107. {
  108.     FW_UNUSED(cloneInfo);
  109.  
  110.     ExternalizePartKind(ev, storageUnit);
  111.     
  112.     if (storageKind != FW_kPartStorage)
  113.         ExternalizeText(ev, storageUnit);
  114. }
  115.  
  116. //----------------------------------------------------------------------------------------
  117. // CHelloContent::ExternalizePartKind
  118. //----------------------------------------------------------------------------------------
  119.  
  120. void CHelloContent::ExternalizePartKind(Environment* ev, ODStorageUnit* storageUnit)
  121. {
  122.     // Write the two text data strings to storage
  123.     FW_PStorageUnitSink suSink(ev, storageUnit, kODPropContents, fHelloPart->GetPartKind(ev));
  124.     FW_CWritableStream archive(suSink);
  125.     unsigned long stringCount = 1;
  126.  
  127.     archive << stringCount;
  128.     archive << fCentered;
  129.     archive << fTextData;
  130. }
  131.  
  132. //----------------------------------------------------------------------------------------
  133. // CHelloContent::ExternalizeText
  134. //----------------------------------------------------------------------------------------
  135.  
  136. void CHelloContent::ExternalizeText(Environment* ev, ODStorageUnit* destinationSU)
  137. {
  138. #ifdef FW_BUILD_MAC
  139.     // Also write the string in text format
  140.     FW_SUAddPropValue(ev, destinationSU, kODPropContents, FW_CPart::gMacTEXTDataType);
  141.     FW_PStorageUnitSink suSink(ev, destinationSU, kODPropContents, FW_CPart::gMacTEXTDataType);
  142.     FW_CWritableStream stream(suSink);
  143.     stream.Write(fTextData.RevealBuffer(), fTextData.GetByteLength());
  144. #endif
  145. }
  146.  
  147. //----------------------------------------------------------------------------------------
  148. // CHelloContent::Internalize
  149. //----------------------------------------------------------------------------------------
  150.  
  151. FW_Boolean CHelloContent::Internalize(Environment* ev,
  152.                                      ODStorageUnit* sourceSU, 
  153.                                      FW_EStorageKinds storageKind,
  154.                                      FW_CCloneInfo* cloneInfo)
  155. {
  156.     FW_UNUSED(cloneInfo);
  157.     FW_Boolean internalized = FALSE;
  158.     
  159.     if (FW_SUExistsThenFocus(ev, sourceSU, kODPropContents, fHelloPart->GetPartKind(ev)))
  160.         internalized = this->InternalizePartKind(ev, sourceSU);
  161. #ifdef FW_BUILD_MAC
  162.     else if (FW_SUExistsThenFocus(ev, sourceSU, kODPropContents, FW_CPart::gMacTEXTDataType))    // 'TEXT'
  163.         internalized = this->InternalizeText(ev, sourceSU);
  164. #endif
  165.  
  166.     if (internalized && storageKind != FW_kPartStorage)
  167.         fHelloPart->PartChanged(ev);
  168.         
  169.     return internalized;
  170. }
  171.  
  172. //----------------------------------------------------------------------------------------
  173. //    CHelloContent::InternalizeText
  174. //----------------------------------------------------------------------------------------
  175.  
  176. FW_Boolean CHelloContent::InternalizeText(Environment* ev, ODStorageUnit* storageUnit)
  177. {
  178.     FW_Boolean result = FALSE;
  179.  
  180. #ifdef FW_BUILD_MAC
  181.     unsigned long textSize = storageUnit->GetSize(ev);
  182.     if (textSize > 0)
  183.     {
  184.         char buffer[256];
  185.         if (textSize > 255) 
  186.             textSize = 255;
  187.         FW_CByteArray byteArray;
  188.         storageUnit->GetValue(ev, textSize, byteArray);
  189.         byteArray.CopyBuffer(&buffer, textSize);
  190.         
  191.         fTextData = "";
  192.         fTextData.Append((const FW_Char*) &buffer, textSize);
  193.         
  194.         result = TRUE;
  195.     }
  196. #endif
  197.  
  198.     return result;
  199. }
  200.  
  201. //----------------------------------------------------------------------------------------
  202. //    CHelloContent::InternalizePartKind
  203. //----------------------------------------------------------------------------------------
  204.  
  205. FW_Boolean CHelloContent::InternalizePartKind(Environment* ev, ODStorageUnit* storageUnit)
  206. {
  207.     FW_Boolean internalized = TRUE;
  208.  
  209.     if (storageUnit->GetSize(ev) != 0)
  210.     {
  211.         // Read the text data strings from storage
  212.         FW_PStorageUnitSink suSink(ev, storageUnit, kODPropContents, fHelloPart->GetPartKind(ev));
  213.         FW_PBufferedSink sink(ev, suSink);
  214.         FW_CReadableStream archive(sink);
  215.         
  216.         unsigned long stringCount;
  217.         archive >> stringCount;
  218.         archive >> fCentered;
  219.         if (stringCount > 0)
  220.             archive >> fTextData;
  221.     }
  222.         
  223.     return internalized;
  224. }
  225.  
  226. //----------------------------------------------------------------------------------------
  227. // CHelloContent::CenterText
  228. //----------------------------------------------------------------------------------------
  229.  
  230. void CHelloContent::CenterText(Environment* ev, FW_Boolean state)
  231. {
  232.     fCentered = state;
  233.     fHelloPart->PartChanged(ev);
  234. }
  235.  
  236.