home *** CD-ROM | disk | FTP | other *** search
/ Supercompiler 1997 / SUPERCOMPILER97.iso / MS_VC.50 / VC / MFC / SRC / CTLPSTG.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-30  |  6.0 KB  |  219 lines

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