home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / winui / shell / fileview / iperfile.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-12  |  6.9 KB  |  240 lines

  1. //THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF 
  2. //ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO 
  3. //THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A 
  4. // PARTICULAR PURPOSE.
  5. //
  6. // Copyright  1994-1996  Microsoft Corporation.  All Rights Reserved.
  7. //
  8. //    PROGRAM: IPERFILE.CPP        
  9. //
  10. //    PURPOSE:   IPersistFile interface implementation for a FileViewer.
  11. // This interface is written to not generally require modifications
  12. // for a custom FileViewer but is written to interact with the
  13. // definition for CFileViewer in FVTEXT.H.
  14. //
  15. //    PLATFORMS:    Windows 95
  16. //
  17. //    SPECIAL INSTRUCTIONS: N/A
  18. //
  19.  
  20. #include "fileview.h"
  21.  
  22. //
  23. //   FUNCTION:CImpIPersistFile::CImpIPersistFile     
  24. //
  25. //   PURPOSE:  Constructor 
  26. //
  27. //   PARAMETERS: 
  28. //    pObj            PCFileViewer of the object we're in.
  29. //  pUnkOuter       LPUNKNOWN to which we delegate.
  30. //
  31. CImpIPersistFile::CImpIPersistFile(PCFileViewer pObj
  32.     , LPUNKNOWN pUnkOuter)
  33.     {
  34.     m_pObj=pObj;
  35.     m_pUnkOuter=pUnkOuter;
  36.     return;
  37.     }
  38. //
  39. //   FUNCTION:      CImpIPersistFile::~CImpIPersistFile
  40. //
  41. //   PURPOSE:   Destructor
  42. //
  43. CImpIPersistFile::~CImpIPersistFile(void)
  44.     {
  45.     return;
  46.     }
  47. //
  48. //   FUNCTIONS: CImpIPersistFile::QueryInterface    
  49. //                              CImpIPersistFile::AddRef
  50. //                              CImpIPersistFile::Release
  51. //
  52. //   PURPOSE:   
  53. //        IUnknown members for CImpIPersistFile object that only delegate.
  54. //
  55. STDMETHODIMP CImpIPersistFile::QueryInterface(REFIID riid
  56.     , PPVOID ppv)
  57.     {
  58.     return m_pUnkOuter->QueryInterface(riid, ppv);
  59.     }
  60.  
  61. STDMETHODIMP_(ULONG) CImpIPersistFile::AddRef(void)
  62.     {
  63.     return m_pUnkOuter->AddRef();
  64.     }
  65.  
  66. STDMETHODIMP_(ULONG) CImpIPersistFile::Release(void)
  67.     {
  68.     return m_pUnkOuter->Release();
  69.     }
  70. //
  71. //   FUNCTION:      CImpIPersistFile::GetClassID
  72. //
  73. //   PURPOSE:   Returns the Class ID of this object.
  74. //
  75. //   PARAMETERS: 
  76. //         pClsID          LPCLSID in which to store our class ID.
  77. //
  78. //   RETURN VALUE:
  79. //         HRESULT         NOERROR always.    
  80. //
  81. STDMETHODIMP CImpIPersistFile::GetClassID(LPCLSID pClsID)
  82.     {
  83.     *pClsID=m_pObj->m_clsID;
  84.     return NOERROR;
  85.     }
  86. //
  87. //   FUNCTION:     CImpIPersistFile::IsDirty
  88. //
  89. //   PURPOSE:  Always returns ResultFromScode(S_FALSE) for a FileViewer which
  90. //   never makes any changes to the file. 
  91. //
  92. //   RETURN VALUE:
  93. //       HRESULT         S_FALSE always
  94. //
  95. STDMETHODIMP CImpIPersistFile::IsDirty(void)
  96.     {
  97.     return ResultFromScode(S_FALSE);
  98.     }
  99. //
  100. //   FUNCTION:     CImpIPersistFile::Load
  101. //
  102. //   PURPOSE:  Receives the filename of the path to show in this FileViewer.
  103. //  The object need do nothing more than store this path for later
  104. //  use in IFileViewer::Show. 
  105. //
  106. //   PARAMETERS: 
  107. //       pszFile         LPCOLESTR of the filename to load.
  108. //     grfMode         DWORD flags to use when opening the file.
  109. //
  110. //   RETURN VALUE:
  111. //       HRESULT         NOERROR or a general error value.
  112. //
  113. STDMETHODIMP CImpIPersistFile::Load(LPCOLESTR pszFile, DWORD grfMode)
  114.     {
  115.     char        szFile[MAX_PATH];
  116.     LPSTR       psz;
  117.  
  118.      // No modifications are necessary to this code:  it simply
  119.      // copies the parameters into the CFileViewer::m_pszPath and
  120.      // m_grfMode members for use in IFileViewer::ShowInitialize
  121.      // and IFileViewer::Show later on.
  122.  
  123.     //We should never be called twice
  124.     if (NULL!=m_pObj->m_pszPath)
  125.     {
  126.         // Review:: Make sure everything else is handled correctly
  127.         m_pObj->m_fLoadCalled = FALSE;  // handle error case
  128.         m_pObj->MemFree(m_pObj->m_pszPath);
  129.         m_pObj->m_pszPath = NULL;
  130.  
  131.     }
  132.  
  133.     if (NULL==pszFile)
  134.         {
  135.         ODS("IPersistFile::Load called with NULL pointer");
  136.         return ResultFromScode(E_INVALIDARG);
  137.         }
  138.  
  139.     //Convert Unicode filename to ANSI
  140.     wcstombs(szFile, pszFile, sizeof(szFile));
  141.  
  142.     psz=(LPSTR)m_pObj->MemAlloc(lstrlen(szFile)+1);
  143.  
  144.     if (NULL==psz)
  145.         {
  146.         ODS("IPersistFile::Load failed to allocate duplicate pathname");
  147.         return ResultFromScode(E_OUTOFMEMORY);
  148.         }
  149.  
  150.     //Copy the ANSI filename and the mode to use in opening it.
  151.     lstrcpy(psz, szFile);
  152.     m_pObj->m_pszPath=psz;
  153.     m_pObj->m_grfMode=grfMode;
  154.  
  155.     //Remember that this function has been called.
  156.     m_pObj->m_fLoadCalled=TRUE;
  157.     return NOERROR;
  158.     }
  159. //
  160. //   FUNCTION:     CImpIPersistFile::Save
  161. //
  162. //   PURPOSE:  Not implemented in a FileViewer: since FileViewer objects never
  163. //  make changes to a file there is nothing to save.  Parameters
  164. //  are irrelevant. 
  165. //
  166. //   RETURN VALUE:
  167. //      HRESULT         Always contains E_NOTIMPL.
  168. //
  169. STDMETHODIMP CImpIPersistFile::Save(LPCOLESTR pszFile, BOOL fRemember)
  170.     {
  171.     ODS("IPersistFile::Save called...unexpected");
  172.     return ResultFromScode(E_NOTIMPL);
  173.     }
  174. //
  175. //   FUNCTION:      CImpIPersistFile::SaveCompleted
  176. //
  177. //   PURPOSE:  Not implemented in a FileViewer.  Parameters are irrelevant. 
  178. //
  179. //   RETURN VALUE:
  180. //       HRESULT         Always contains E_NOTIMPL.
  181. //
  182. STDMETHODIMP CImpIPersistFile::SaveCompleted(LPCOLESTR pszFile)
  183.     {
  184.     ODS("IPersistFile::SaveCompleted called...unexpected");
  185.     return ResultFromScode(E_NOTIMPL);
  186.     }
  187. //
  188. //   FUNCTION: CImpIPersistFile::GetCurFile    
  189. //
  190. //   PURPOSE:   Not implemented in a FileViewer.  Normally this function
  191. //  would return a copy of the pathname from IPersistFile::Load
  192. //  in a piece of memory allocated with the shared allocator
  193. //  and stored in *ppszFile.  However, this function will not
  194. //  be called in a FileViewer and can be left unimplemented.
  195. //
  196. //   PARAMETERS: 
  197. //      ppszFile        LPOLESTR * into which we store a pointer to
  198. //                  the filename that should be allocated with the shared IMalloc.
  199. //
  200. //   RETURN VALUE:
  201. //  HRESULT         NOERROR or a general error value.
  202. //
  203. STDMETHODIMP CImpIPersistFile::GetCurFile(LPOLESTR *ppszFile)
  204.     {
  205.     LPOLESTR    psz;
  206.     ULONG       cb;
  207.  
  208.      // No modifications are necessary to this code:  it simply
  209.      // copies the CFileViewer::m_pszPath string into a piece
  210.      // of memory and stores the pointer at *ppszFile.
  211.     //Load must be called, of course.
  212.     if (m_pObj->m_fLoadCalled)
  213.         {
  214.         ODS("IPersistFile::GetCurFile called without IPersistFile::Load");
  215.         return ResultFromScode(E_UNEXPECTED);
  216.         }
  217.  
  218.     if (NULL==ppszFile)
  219.         {
  220.         ODS("IPersistFile::GetCurFile called with NULL pointer");
  221.         return ResultFromScode(E_INVALIDARG);
  222.         }
  223.  
  224.     cb=(lstrlen(m_pObj->m_pszPath)+1)*sizeof(OLECHAR);
  225.     psz=(LPOLESTR)m_pObj->MemAlloc(cb);
  226.  
  227.     if (NULL==psz)
  228.         {
  229.         ODS("IPersistFile::GetCurFile failed to allocate duplicate pathname");
  230.         return ResultFromScode(E_OUTOFMEMORY);
  231.         }
  232.  
  233.     //Copy the ANSI filename to the new memory, converting to Unicode
  234.     mbstowcs(psz, m_pObj->m_pszPath, cb);
  235.  
  236.     //Save the pointer which is not caller's responsibility
  237.     *ppszFile=psz;
  238.     return NOERROR;
  239.     }
  240.