home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / mfc / src / ctlpstg.cpp < prev    next >
C/C++ Source or Header  |  1998-06-16  |  6KB  |  217 lines

  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.  
  11. #include "stdafx.h"
  12.  
  13. #ifdef AFXCTL_PROP_SEG
  14. #pragma code_seg(AFXCTL_PROP_SEG)
  15. #endif
  16.  
  17. #ifdef _DEBUG
  18. #undef THIS_FILE
  19. static char THIS_FILE[] = __FILE__;
  20. #endif
  21.  
  22. #define new DEBUG_NEW
  23.  
  24. /////////////////////////////////////////////////////////////////////////////
  25. // COleControl::XPersistStorage
  26.  
  27. STDMETHODIMP_(ULONG) COleControl::XPersistStorage::AddRef()
  28. {
  29.     METHOD_PROLOGUE_EX_(COleControl, PersistStorage)
  30.     return (ULONG)pThis->ExternalAddRef();
  31. }
  32.  
  33. STDMETHODIMP_(ULONG) COleControl::XPersistStorage::Release()
  34. {
  35.     METHOD_PROLOGUE_EX_(COleControl, PersistStorage)
  36.     return (ULONG)pThis->ExternalRelease();
  37. }
  38.  
  39. STDMETHODIMP COleControl::XPersistStorage::QueryInterface(
  40.     REFIID iid, LPVOID* ppvObj)
  41. {
  42.     METHOD_PROLOGUE_EX_(COleControl, PersistStorage)
  43.     return (HRESULT)pThis->ExternalQueryInterface(&iid, ppvObj);
  44. }
  45.  
  46. STDMETHODIMP COleControl::XPersistStorage::GetClassID(LPCLSID lpClassID)
  47. {
  48.     METHOD_PROLOGUE_EX_(COleControl, PersistStorage)
  49.     return pThis->GetClassID(lpClassID);
  50. }
  51.  
  52. STDMETHODIMP COleControl::XPersistStorage::IsDirty()
  53. {
  54.     // Return S_OK if modified, and S_FALSE otherwise.
  55.     METHOD_PROLOGUE_EX_(COleControl, PersistStorage)
  56.  
  57.     if (pThis->m_pDefIPersistStorage == NULL)
  58.         pThis->m_pDefIPersistStorage =
  59.             (LPPERSISTSTORAGE)pThis->QueryDefHandler(IID_IPersistStorage);
  60.  
  61.     BOOL bDefModified = (pThis->m_pDefIPersistStorage->IsDirty() == S_OK);
  62.     return (bDefModified || pThis->m_bModified) ? S_OK : S_FALSE;
  63. }
  64.  
  65. STDMETHODIMP COleControl::XPersistStorage::InitNew(LPSTORAGE pStg)
  66. {
  67.     METHOD_PROLOGUE_EX(COleControl, PersistStorage)
  68.  
  69.     if (pThis->m_pDefIPersistStorage == NULL)
  70.         pThis->m_pDefIPersistStorage =
  71.             (LPPERSISTSTORAGE)pThis->QueryDefHandler(IID_IPersistStorage);
  72.  
  73.     pThis->m_pDefIPersistStorage->InitNew(pStg);
  74.  
  75.     // Delegate to OnResetState.
  76.     pThis->OnResetState();
  77.  
  78.     // Unless IOleObject::SetClientSite is called after this, we can
  79.     // count on ambient properties being available while loading.
  80.     pThis->m_bCountOnAmbients = TRUE;
  81.  
  82.     // Properties have been initialized
  83.     pThis->m_bInitialized = TRUE;
  84.  
  85.     // Uncache cached ambient properties
  86.     _afxAmbientCache->Cache(NULL);
  87.  
  88.     return S_OK;
  89. }
  90.  
  91. STDMETHODIMP COleControl::XPersistStorage::Load(LPSTORAGE pStg)
  92. {
  93.     ASSERT(pStg != NULL);
  94.     METHOD_PROLOGUE_EX(COleControl, PersistStorage)
  95.  
  96.     CLIPFORMAT cf;
  97.     HRESULT hr;
  98.     CLSID fmtid;
  99.  
  100.     hr = ::ReadFmtUserTypeStg(pStg, &cf, NULL);
  101.  
  102.     if (SUCCEEDED(hr) && _AfxOleMatchPropsetClipFormat(cf, &fmtid))
  103.     {
  104.         // Load the property set data
  105.         FORMATETC formatEtc;
  106.         STGMEDIUM stgMedium;
  107.         formatEtc.cfFormat = cf;
  108.         formatEtc.ptd = NULL;
  109.         formatEtc.dwAspect = DVASPECT_CONTENT;
  110.         formatEtc.lindex = -1;
  111.         formatEtc.tymed = TYMED_ISTORAGE;
  112.         stgMedium.tymed = TYMED_ISTORAGE;
  113.         stgMedium.pstg = pStg;
  114.         stgMedium.pUnkForRelease = NULL;
  115.         hr = pThis->SetPropsetData(&formatEtc, &stgMedium, fmtid) ?
  116.             S_OK : E_FAIL;
  117.     }
  118.     else
  119.     {
  120.         // Open the "Contents" stream of the supplied storage object, and
  121.         // then delegate to same implementation as IPersistStreamInit::Load.
  122.         LPSTREAM pStm = NULL;
  123.         hr = pStg->OpenStream(OLESTR("Contents"), NULL,
  124.             STGM_READ | STGM_SHARE_EXCLUSIVE, 0, &pStm);
  125.  
  126.         ASSERT(FAILED(hr) || pStm != NULL);
  127.  
  128.         if (pStm != NULL)
  129.         {
  130.             // Delegate to LoadState.
  131.             hr = pThis->LoadState(pStm);
  132.             pStm->Release();
  133.         }
  134.     }
  135.  
  136.     if (pThis->m_pDefIPersistStorage == NULL)
  137.         pThis->m_pDefIPersistStorage =
  138.             (LPPERSISTSTORAGE)pThis->QueryDefHandler(IID_IPersistStorage);
  139.  
  140.     // Delegate to default handler (for cache).
  141.     pThis->m_pDefIPersistStorage->Load(pStg);
  142.  
  143.     return hr;
  144. }
  145.  
  146. STDMETHODIMP COleControl::XPersistStorage::Save(LPSTORAGE pStg,
  147.     BOOL fSameAsLoad)
  148. {
  149.     METHOD_PROLOGUE_EX(COleControl, PersistStorage)
  150.     ASSERT(pStg != NULL);
  151.  
  152.     // Create a "Contents" stream on the supplied storage object, and
  153.     // then delegate to the implementation of IPersistStreamInit::Save.
  154.  
  155.     // Don't bother saving if destination is up-to-date.
  156.     if (fSameAsLoad && (IsDirty() != S_OK))
  157.         return S_OK;
  158.  
  159.     LPSTREAM pStm = NULL;
  160.     HRESULT hr = pStg->CreateStream(OLESTR("Contents"),
  161.         STGM_CREATE | STGM_READWRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &pStm);
  162.  
  163.     ASSERT(FAILED(hr) || pStm != NULL);
  164.  
  165.     if (pStm != NULL)
  166.     {
  167.         // Delegate to SaveState.
  168.         hr = pThis->SaveState(pStm);
  169.  
  170.         // Bookkeeping:  Clear the dirty flag, if storage is same.
  171.         if (fSameAsLoad)
  172.             pThis->m_bModified = FALSE;
  173.  
  174.         pStm->Release();
  175.     }
  176.  
  177.     if (pThis->m_pDefIPersistStorage == NULL)
  178.         pThis->m_pDefIPersistStorage =
  179.             (LPPERSISTSTORAGE)pThis->QueryDefHandler(IID_IPersistStorage);
  180.  
  181.     // Delegate to default handler (for cache).
  182.     pThis->m_pDefIPersistStorage->Save(pStg, fSameAsLoad);
  183.     return hr;
  184. }
  185.  
  186. STDMETHODIMP COleControl::XPersistStorage::SaveCompleted(LPSTORAGE pStgSaved)
  187. {
  188.     METHOD_PROLOGUE_EX_(COleControl, PersistStorage)
  189.  
  190.     if (pThis->m_pDefIPersistStorage == NULL)
  191.         pThis->m_pDefIPersistStorage =
  192.             (LPPERSISTSTORAGE)pThis->QueryDefHandler(IID_IPersistStorage);
  193.  
  194.     if (pStgSaved != NULL)
  195.         pThis->m_bModified = FALSE;
  196.  
  197.     return pThis->m_pDefIPersistStorage->SaveCompleted(pStgSaved);
  198. }
  199.  
  200. STDMETHODIMP COleControl::XPersistStorage::HandsOffStorage()
  201. {
  202.     METHOD_PROLOGUE_EX_(COleControl, PersistStorage)
  203.  
  204.     if (pThis->m_pDefIPersistStorage == NULL)
  205.         pThis->m_pDefIPersistStorage =
  206.             (LPPERSISTSTORAGE)pThis->QueryDefHandler(IID_IPersistStorage);
  207.  
  208.     return pThis->m_pDefIPersistStorage->HandsOffStorage();
  209. }
  210.  
  211. /////////////////////////////////////////////////////////////////////////////
  212. // Force any extra compiler-generated code into AFX_INIT_SEG
  213.  
  214. #ifdef AFX_INIT_SEG
  215. #pragma code_seg(AFX_INIT_SEG)
  216. #endif
  217.