home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / com / activedocument / doserver / perstor.cpp < prev    next >
C/C++ Source or Header  |  1997-05-28  |  9KB  |  382 lines

  1. /**************************************************************************
  2.    THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  3.    ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  4.    THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  5.    PARTICULAR PURPOSE.
  6.  
  7.    Copyright 1997 Microsoft Corporation.  All Rights Reserved.
  8. **************************************************************************/
  9.  
  10. /**************************************************************************
  11.  
  12.    File:          PerStor.cpp
  13.    
  14.    Description:   CPersistStorage implementation.
  15.  
  16. **************************************************************************/
  17.  
  18. /**************************************************************************
  19.    #include statements
  20. **************************************************************************/
  21.  
  22. #include "PerStor.h"
  23.  
  24. /**************************************************************************
  25.  
  26.    CPersistStorage::CPersistStorage()
  27.    
  28. **************************************************************************/
  29.  
  30. CPersistStorage::CPersistStorage(COleDocument *pOleDoc)
  31. {
  32. OutputDebugString(TEXT("CPersistStorage's constructor\n"));
  33.  
  34. m_pOleDoc = pOleDoc;
  35.  
  36. m_pStorage = NULL;
  37. m_pColorStream = NULL;
  38. m_fSameAsLoad = FALSE;
  39. }
  40.  
  41. /**************************************************************************
  42.  
  43.    CPersistStorage::~CPersistStorage()
  44.    
  45. **************************************************************************/
  46.  
  47. CPersistStorage::~CPersistStorage()
  48. {
  49. OutputDebugString(TEXT("CPersistStorage's destructor\n"));
  50. }
  51.  
  52. /**************************************************************************
  53.  
  54.    CPersistStorage::QueryInterface()
  55.    
  56. **************************************************************************/
  57.  
  58. STDMETHODIMP CPersistStorage::QueryInterface(REFIID riid, LPVOID *ppReturn)
  59. {
  60. OutputDebugString(TEXT("CPersistStorage::QueryInterface\n"));
  61.  
  62. return m_pOleDoc->QueryInterface(riid, ppReturn);
  63. }
  64.  
  65. /**************************************************************************
  66.  
  67.    CPersistStorage::AddRef()
  68.    
  69. **************************************************************************/
  70.  
  71. STDMETHODIMP_(ULONG) CPersistStorage::AddRef()
  72. {
  73. OutputDebugString(TEXT("CPersistStorage::AddRef\n"));
  74.  
  75. return m_pOleDoc->AddRef();
  76. }
  77.  
  78. /**************************************************************************
  79.  
  80.    CPersistStorage::Release()
  81.    
  82. **************************************************************************/
  83.  
  84. STDMETHODIMP_(ULONG) CPersistStorage::Release()
  85. {
  86. OutputDebugString(TEXT("CPersistStorage::Release\n"));
  87.  
  88. return m_pOleDoc->Release();
  89. }
  90.  
  91. /**************************************************************************
  92.  
  93.    CPersistStorage::InitNew()
  94.    
  95. **************************************************************************/
  96.  
  97. STDMETHODIMP CPersistStorage::InitNew(LPSTORAGE pStg)
  98. {
  99. OutputDebugString(TEXT("CPersistStorage::InitNew\n"));
  100.  
  101. // release any streams and storages that may be open
  102. ReleaseStreamsAndStorage();
  103.  
  104. m_pStorage = pStg;
  105.  
  106. // AddRef the new Storage
  107. if (m_pStorage)
  108.    m_pStorage->AddRef();
  109.  
  110. CreateStreams(m_pStorage);
  111.  
  112. return S_OK;
  113. }
  114.  
  115. /**************************************************************************
  116.  
  117.    CPersistStorage::GetClassID()
  118.    
  119. **************************************************************************/
  120.  
  121. STDMETHODIMP CPersistStorage::GetClassID(LPCLSID pClassID)
  122. {
  123. OutputDebugString(TEXT("CPersistStorage::GetClassID\n"));
  124.  
  125. *pClassID = CLSID_SimpleDocObject;
  126.  
  127. return S_OK;
  128. }
  129.  
  130. /**************************************************************************
  131.  
  132.    CPersistStorage::Save()
  133.    
  134. **************************************************************************/
  135.  
  136. STDMETHODIMP CPersistStorage::Save(LPSTORAGE pStg, BOOL fSameAsLoad)
  137. {
  138. OutputDebugString(TEXT("CPersistStorage::Save\n"));
  139.  
  140. m_pOleDoc->m_fNoScribbleMode = TRUE;
  141.  
  142. // save the data
  143. LPSTREAM pTempColor;
  144.  
  145. if(!fSameAsLoad)
  146.    {
  147.    if(!pStg)
  148.       return E_FAIL;
  149.  
  150.    CreateStreams(pStg, &pTempColor);
  151.    }
  152. else
  153.    {
  154.    pStg = m_pStorage;
  155.  
  156.    if(!pStg)
  157.       return E_FAIL;
  158.    
  159.    pTempColor = m_pColorStream;
  160.    pTempColor->AddRef();
  161.    }
  162.  
  163. //write our class ID into the storage
  164. CLSID clsid;
  165. GetClassID(&clsid);
  166. WriteClassStg(pStg, clsid);
  167.  
  168. ULARGE_INTEGER uli;
  169.  
  170. uli.LowPart = 0;
  171. uli.HighPart = 0;
  172.  
  173. pTempColor->SetSize(uli);
  174.  
  175. LARGE_INTEGER li;
  176.  
  177. li.LowPart = 0;
  178. li.HighPart = 0;
  179.  
  180. pTempColor->Seek(li, STREAM_SEEK_SET, NULL);
  181.  
  182. // write the color to the stream
  183. pTempColor->Write(&m_pOleDoc->m_Color, sizeof(m_pOleDoc->m_Color), NULL);
  184.  
  185. pTempColor->Release();
  186.  
  187. m_fSameAsLoad = fSameAsLoad;
  188.  
  189. m_pOleDoc->m_fDirty = FALSE;
  190.  
  191. return S_OK;
  192. }
  193.  
  194. /**************************************************************************
  195.  
  196.    CPersistStorage::SaveCompleted()
  197.    
  198. **************************************************************************/
  199.  
  200. STDMETHODIMP CPersistStorage::SaveCompleted(LPSTORAGE pStgNew)
  201. {
  202. OutputDebugString(TEXT("CPersistStorage::SaveCompleted\n"));
  203.  
  204. if(pStgNew)
  205.    {
  206.    ReleaseStreamsAndStorage();
  207.    m_pStorage = pStgNew;
  208.    m_pStorage->AddRef();
  209.    OpenStreams(pStgNew);
  210.    }
  211.  
  212. if(pStgNew || m_fSameAsLoad)
  213.    {
  214.    if(m_pOleDoc->m_fNoScribbleMode)
  215.       {
  216.       if(m_pOleDoc->m_pOleAdviseHolder)
  217.          m_pOleDoc->m_pOleAdviseHolder->SendOnSave();
  218.       }
  219.  
  220.    m_fSameAsLoad = FALSE;
  221.    }
  222.  
  223. m_pOleDoc->m_fNoScribbleMode = FALSE;
  224.  
  225. return S_OK;
  226. }
  227.  
  228. /**************************************************************************
  229.  
  230.    CPersistStorage::Load()
  231.    
  232. **************************************************************************/
  233.  
  234. STDMETHODIMP CPersistStorage::Load(LPSTORAGE pStg)
  235. {
  236. OutputDebugString(TEXT("CPersistStorage::Load\n"));
  237.  
  238. // remember the storage
  239. if(m_pStorage)
  240.    {
  241.    m_pStorage->Release();
  242.    m_pStorage = NULL;
  243.    }
  244.  
  245. m_pStorage = pStg;
  246.  
  247. m_pStorage->AddRef();
  248.  
  249. //read our class ID from the storage
  250. CLSID clsidStg;
  251. ReadClassStg(m_pStorage, &clsidStg);
  252.  
  253. OpenStreams(m_pStorage);
  254.  
  255. // read the color
  256. if(FAILED(m_pColorStream->Read(&m_pOleDoc->m_Color, sizeof(m_pOleDoc->m_Color), NULL)))
  257.    {
  258.    m_pOleDoc->m_Color = DEFAULT_COLOR;
  259.    }
  260.  
  261. m_pOleDoc->m_fDirty = FALSE;
  262.  
  263. return S_OK;
  264. }
  265.  
  266. /**************************************************************************
  267.  
  268.    CPersistStorage::IsDirty()
  269.    
  270. **************************************************************************/
  271.  
  272. STDMETHODIMP CPersistStorage::IsDirty()
  273. {
  274. OutputDebugString(TEXT("CPersistStorage::IsDirty\n"));
  275.  
  276. return (m_pOleDoc->m_fDirty ? S_OK : S_FALSE);
  277. }
  278.  
  279. /**************************************************************************
  280.  
  281.    CPersistStorage::HandsOffStorage()
  282.    
  283. **************************************************************************/
  284.  
  285. STDMETHODIMP CPersistStorage::HandsOffStorage()
  286. {
  287. OutputDebugString(TEXT("CPersistStorage::HandsOffStorage\n"));
  288.  
  289. ReleaseStreamsAndStorage();
  290.  
  291. return S_OK;
  292. }
  293.  
  294. /**************************************************************************
  295.  
  296.    CPersistStorage::CreateStreams()
  297.    
  298. **************************************************************************/
  299.  
  300. void CPersistStorage::CreateStreams(LPSTORAGE pStg)
  301. {
  302. if(m_pColorStream)
  303.    m_pColorStream->Release();
  304.  
  305. // create a stream to save the color
  306. pStg->CreateStream(  OLESTR("Color"),
  307.                      STGM_READWRITE | STGM_SHARE_EXCLUSIVE | STGM_CREATE,
  308.                      0,
  309.                      0,
  310.                      &m_pColorStream);
  311.  
  312. }
  313.  
  314. /**************************************************************************
  315.  
  316.    CPersistStorage::CreateStreams()
  317.    
  318. **************************************************************************/
  319.  
  320. void CPersistStorage::CreateStreams(   LPSTORAGE pStg, 
  321.                                        LPSTREAM* ppNewColorStream)
  322. {
  323. LPSTREAM pTempColorStream;
  324.  
  325. //save the member streams
  326. pTempColorStream = m_pColorStream;
  327.  
  328. //initialize the streams
  329. m_pColorStream = *ppNewColorStream = NULL;
  330.  
  331. //create the streams
  332. CreateStreams(pStg);
  333.  
  334. //copy the new streams
  335. *ppNewColorStream = m_pColorStream;
  336.  
  337. //restore the member streams
  338. m_pColorStream = pTempColorStream;
  339. }
  340.  
  341. /**************************************************************************
  342.  
  343.    CPersistStorage::OpenStreams()
  344.    
  345. **************************************************************************/
  346.  
  347. void CPersistStorage::OpenStreams(LPSTORAGE pStg)
  348. {
  349. if(m_pColorStream)
  350.    m_pColorStream->Release();
  351.  
  352. // open the color stream
  353. pStg->OpenStream( OLESTR("Color"),
  354.                   0,
  355.                   STGM_READWRITE | STGM_SHARE_EXCLUSIVE,
  356.                   0,
  357.                   &m_pColorStream);
  358.  
  359. }
  360.  
  361. /**************************************************************************
  362.  
  363.    CPersistStorage::ReleaseStreamsAndStorage()
  364.    
  365. **************************************************************************/
  366.  
  367. void CPersistStorage::ReleaseStreamsAndStorage()
  368. {
  369. if(m_pColorStream)
  370.    {
  371.    m_pColorStream->Release();
  372.    m_pColorStream = NULL;
  373.    }
  374.  
  375. if(m_pStorage)
  376.    {
  377.    m_pStorage->Release();
  378.    m_pStorage = NULL;
  379.    }
  380. }
  381.  
  382.