ctlpstg.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1998 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Microsoft Foundation Classes Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Microsoft Foundation Classes product.
  10. #include "stdafx.h"
  11. #ifdef AFXCTL_PROP_SEG
  12. #pragma code_seg(AFXCTL_PROP_SEG)
  13. #endif
  14. #ifdef _DEBUG
  15. #undef THIS_FILE
  16. static char THIS_FILE[] = __FILE__;
  17. #endif
  18. #define new DEBUG_NEW
  19. /////////////////////////////////////////////////////////////////////////////
  20. // COleControl::XPersistStorage
  21. STDMETHODIMP_(ULONG) COleControl::XPersistStorage::AddRef()
  22. {
  23. METHOD_PROLOGUE_EX_(COleControl, PersistStorage)
  24. return (ULONG)pThis->ExternalAddRef();
  25. }
  26. STDMETHODIMP_(ULONG) COleControl::XPersistStorage::Release()
  27. {
  28. METHOD_PROLOGUE_EX_(COleControl, PersistStorage)
  29. return (ULONG)pThis->ExternalRelease();
  30. }
  31. STDMETHODIMP COleControl::XPersistStorage::QueryInterface(
  32. REFIID iid, LPVOID* ppvObj)
  33. {
  34. METHOD_PROLOGUE_EX_(COleControl, PersistStorage)
  35. return (HRESULT)pThis->ExternalQueryInterface(&iid, ppvObj);
  36. }
  37. STDMETHODIMP COleControl::XPersistStorage::GetClassID(LPCLSID lpClassID)
  38. {
  39. METHOD_PROLOGUE_EX_(COleControl, PersistStorage)
  40. return pThis->GetClassID(lpClassID);
  41. }
  42. STDMETHODIMP COleControl::XPersistStorage::IsDirty()
  43. {
  44. // Return S_OK if modified, and S_FALSE otherwise.
  45. METHOD_PROLOGUE_EX_(COleControl, PersistStorage)
  46. if (pThis->m_pDefIPersistStorage == NULL)
  47. pThis->m_pDefIPersistStorage =
  48. (LPPERSISTSTORAGE)pThis->QueryDefHandler(IID_IPersistStorage);
  49. BOOL bDefModified = (pThis->m_pDefIPersistStorage->IsDirty() == S_OK);
  50. return (bDefModified || pThis->m_bModified) ? S_OK : S_FALSE;
  51. }
  52. STDMETHODIMP COleControl::XPersistStorage::InitNew(LPSTORAGE pStg)
  53. {
  54. METHOD_PROLOGUE_EX(COleControl, PersistStorage)
  55. if (pThis->m_pDefIPersistStorage == NULL)
  56. pThis->m_pDefIPersistStorage =
  57. (LPPERSISTSTORAGE)pThis->QueryDefHandler(IID_IPersistStorage);
  58. pThis->m_pDefIPersistStorage->InitNew(pStg);
  59. // Delegate to OnResetState.
  60. pThis->OnResetState();
  61. // Unless IOleObject::SetClientSite is called after this, we can
  62. // count on ambient properties being available while loading.
  63. pThis->m_bCountOnAmbients = TRUE;
  64. // Properties have been initialized
  65. pThis->m_bInitialized = TRUE;
  66. // Uncache cached ambient properties
  67. _afxAmbientCache->Cache(NULL);
  68. return S_OK;
  69. }
  70. STDMETHODIMP COleControl::XPersistStorage::Load(LPSTORAGE pStg)
  71. {
  72. ASSERT(pStg != NULL);
  73. METHOD_PROLOGUE_EX(COleControl, PersistStorage)
  74. CLIPFORMAT cf;
  75. HRESULT hr;
  76. CLSID fmtid;
  77. hr = ::ReadFmtUserTypeStg(pStg, &cf, NULL);
  78. if (SUCCEEDED(hr) && _AfxOleMatchPropsetClipFormat(cf, &fmtid))
  79. {
  80. // Load the property set data
  81. FORMATETC formatEtc;
  82. STGMEDIUM stgMedium;
  83. formatEtc.cfFormat = cf;
  84. formatEtc.ptd = NULL;
  85. formatEtc.dwAspect = DVASPECT_CONTENT;
  86. formatEtc.lindex = -1;
  87. formatEtc.tymed = TYMED_ISTORAGE;
  88. stgMedium.tymed = TYMED_ISTORAGE;
  89. stgMedium.pstg = pStg;
  90. stgMedium.pUnkForRelease = NULL;
  91. hr = pThis->SetPropsetData(&formatEtc, &stgMedium, fmtid) ?
  92. S_OK : E_FAIL;
  93. }
  94. else
  95. {
  96. // Open the "Contents" stream of the supplied storage object, and
  97. // then delegate to same implementation as IPersistStreamInit::Load.
  98. LPSTREAM pStm = NULL;
  99. hr = pStg->OpenStream(OLESTR("Contents"), NULL,
  100. STGM_READ | STGM_SHARE_EXCLUSIVE, 0, &pStm);
  101. ASSERT(FAILED(hr) || pStm != NULL);
  102. if (pStm != NULL)
  103. {
  104. // Delegate to LoadState.
  105. hr = pThis->LoadState(pStm);
  106. pStm->Release();
  107. }
  108. }
  109. if (pThis->m_pDefIPersistStorage == NULL)
  110. pThis->m_pDefIPersistStorage =
  111. (LPPERSISTSTORAGE)pThis->QueryDefHandler(IID_IPersistStorage);
  112. // Delegate to default handler (for cache).
  113. pThis->m_pDefIPersistStorage->Load(pStg);
  114. return hr;
  115. }
  116. STDMETHODIMP COleControl::XPersistStorage::Save(LPSTORAGE pStg,
  117. BOOL fSameAsLoad)
  118. {
  119. METHOD_PROLOGUE_EX(COleControl, PersistStorage)
  120. ASSERT(pStg != NULL);
  121. // Create a "Contents" stream on the supplied storage object, and
  122. // then delegate to the implementation of IPersistStreamInit::Save.
  123. // Don't bother saving if destination is up-to-date.
  124. if (fSameAsLoad && (IsDirty() != S_OK))
  125. return S_OK;
  126. LPSTREAM pStm = NULL;
  127. HRESULT hr = pStg->CreateStream(OLESTR("Contents"),
  128. STGM_CREATE | STGM_READWRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &pStm);
  129. ASSERT(FAILED(hr) || pStm != NULL);
  130. if (pStm != NULL)
  131. {
  132. // Delegate to SaveState.
  133. hr = pThis->SaveState(pStm);
  134. // Bookkeeping: Clear the dirty flag, if storage is same.
  135. if (fSameAsLoad)
  136. pThis->m_bModified = FALSE;
  137. pStm->Release();
  138. }
  139. if (pThis->m_pDefIPersistStorage == NULL)
  140. pThis->m_pDefIPersistStorage =
  141. (LPPERSISTSTORAGE)pThis->QueryDefHandler(IID_IPersistStorage);
  142. // Delegate to default handler (for cache).
  143. pThis->m_pDefIPersistStorage->Save(pStg, fSameAsLoad);
  144. return hr;
  145. }
  146. STDMETHODIMP COleControl::XPersistStorage::SaveCompleted(LPSTORAGE pStgSaved)
  147. {
  148. METHOD_PROLOGUE_EX_(COleControl, PersistStorage)
  149. if (pThis->m_pDefIPersistStorage == NULL)
  150. pThis->m_pDefIPersistStorage =
  151. (LPPERSISTSTORAGE)pThis->QueryDefHandler(IID_IPersistStorage);
  152. if (pStgSaved != NULL)
  153. pThis->m_bModified = FALSE;
  154. return pThis->m_pDefIPersistStorage->SaveCompleted(pStgSaved);
  155. }
  156. STDMETHODIMP COleControl::XPersistStorage::HandsOffStorage()
  157. {
  158. METHOD_PROLOGUE_EX_(COleControl, PersistStorage)
  159. if (pThis->m_pDefIPersistStorage == NULL)
  160. pThis->m_pDefIPersistStorage =
  161. (LPPERSISTSTORAGE)pThis->QueryDefHandler(IID_IPersistStorage);
  162. return pThis->m_pDefIPersistStorage->HandsOffStorage();
  163. }
  164. /////////////////////////////////////////////////////////////////////////////
  165. // Force any extra compiler-generated code into AFX_INIT_SEG
  166. #ifdef AFX_INIT_SEG
  167. #pragma code_seg(AFX_INIT_SEG)
  168. #endif