home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / com / inole2 / chap22 / patron / iperfile.cpp < prev    next >
C/C++ Source or Header  |  1995-05-03  |  6KB  |  281 lines

  1. /*
  2.  * IPERFILE.CPP
  3.  * Patron Chapter 22
  4.  *
  5.  * Implementation of the IPersistFile interface for Patron's
  6.  * documents.
  7.  *
  8.  * Copyright (c)1993-1995 Microsoft Corporation, All Rights Reserved
  9.  *
  10.  * Kraig Brockschmidt, Microsoft
  11.  * Internet  :  kraigb@microsoft.com
  12.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  13.  */
  14.  
  15.  
  16. #include "patron.h"
  17.  
  18.  
  19. /*
  20.  * CImpIPersistFile:CImpIPersistFile
  21.  * CImpIPersistFile::~CImpIPersistFile
  22.  *
  23.  * Constructor Parameters:
  24.  *  pDoc            PCPatronDoc associated with this interface
  25.  *  pUnkOuter       LPUNKNOWN of the controlling unknown.
  26.  */
  27.  
  28. CImpIPersistFile::CImpIPersistFile(PCPatronDoc pDoc
  29.     , LPUNKNOWN pUnkOuter)
  30.     {
  31.     m_cRef=0;
  32.     m_pDoc=pDoc;
  33.     m_pUnkOuter=pUnkOuter;
  34.     return;
  35.     }
  36.  
  37.  
  38. CImpIPersistFile::~CImpIPersistFile(void)
  39.     {
  40.     return;
  41.     }
  42.  
  43.  
  44.  
  45.  
  46. /*
  47.  * CImpIPersistFile::QueryInterface
  48.  * CImpIPersistFile::AddRef
  49.  * CImpIPersistFile::Release
  50.  *
  51.  * Purpose:
  52.  *  Standard set of IUnknown members for this interface
  53.  */
  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.     ++m_cRef;
  64.     return m_pUnkOuter->AddRef();
  65.     }
  66.  
  67. STDMETHODIMP_(ULONG) CImpIPersistFile::Release(void)
  68.     {
  69.     --m_cRef;
  70.     return m_pUnkOuter->Release();
  71.     }
  72.  
  73.  
  74.  
  75.  
  76. /*
  77.  * CImpIPersistFile::GetClassID
  78.  *
  79.  * Purpose:
  80.  *  Returns the CLSID of the file represented by this interface.
  81.  *
  82.  * Parameters:
  83.  *  pClsID          LPCLSID in which to store our CLSID.
  84.  *
  85.  * Return Value:
  86.  *  HRESULT         NOERROR or a general error value.
  87.  */
  88.  
  89. STDMETHODIMP CImpIPersistFile::GetClassID(LPCLSID pClsID)
  90.     {
  91.     *pClsID=CLSID_PatronPages;
  92.     return NOERROR;
  93.     }
  94.  
  95.  
  96.  
  97.  
  98.  
  99. /*
  100.  * CImpIPersistFile::IsDirty
  101.  *
  102.  * Purpose:
  103.  *  Tells the caller if we have made changes to this file since
  104.  *  it was loaded or initialized new.
  105.  *
  106.  * Parameters:
  107.  *  None
  108.  *
  109.  * Return Value:
  110.  *  HRESULT         Contains S_OK if we ARE dirty, S_FALSE if
  111.  *                  NOT dirty.
  112.  */
  113.  
  114. STDMETHODIMP CImpIPersistFile::IsDirty(void)
  115.     {
  116.     return ResultFromScode(m_pDoc->FDirtyGet() ? S_OK : S_FALSE);
  117.     }
  118.  
  119.  
  120.  
  121.  
  122.  
  123. /*
  124.  * IPersistFile::Load
  125.  *
  126.  * Purpose:
  127.  *  Asks the server to load the document for the given filename.
  128.  *
  129.  * Parameters:
  130.  *  pszFile         LPCOLESTR to the filename to load.
  131.  *  grfMode         DWORD containing open flags requested from the
  132.  *                  caller.  Currently these are safely ignored.
  133.  *
  134.  * Return Value:
  135.  *  HRESULT         NOERROR or a general error value.
  136.  */
  137.  
  138. STDMETHODIMP CImpIPersistFile::Load(LPCOLESTR pszFile, DWORD grfMode)
  139.     {
  140.     UINT        uRet;
  141.  
  142.    #ifdef WIN32ANSI
  143.     char        szTemp[CCHPATHMAX];
  144.  
  145.     WideCharToMultiByte(CP_ACP, 0, pszFile, -1, szTemp, CCHPATHMAX
  146.        , NULL, NULL);
  147.     uRet=m_pDoc->Load(TRUE, szTemp);
  148.    #else
  149.     uRet=m_pDoc->Load(TRUE, (LPTSTR)pszFile);
  150.    #endif
  151.     return (DOCERR_NONE==uRet) ? NOERROR
  152.         : ResultFromScode(STG_E_READFAULT);
  153.     }
  154.  
  155.  
  156.  
  157.  
  158.  
  159. /*
  160.  * IPersistFile::Save
  161.  *
  162.  * Purpose:
  163.  *  Instructs the server to write the current file into a new
  164.  *  filename, possibly then using that filename as the current one.
  165.  *
  166.  * Parameters:
  167.  *  pszFile         LPCOLESTR of the file into which we save.  If NULL,
  168.  *                  this means save the current file.
  169.  *  fRemember       BOOL indicating if we're to use this filename as
  170.  *                  the current file now (Save As instead of Save
  171.  *                  Copy As).
  172.  *
  173.  * Return Value:
  174.  *  HRESULT         NOERROR or a general error value.
  175.  */
  176.  
  177. STDMETHODIMP CImpIPersistFile::Save(LPCOLESTR pszFile, BOOL fRemember)
  178.     {
  179.     UINT        uRet;
  180.  
  181.     /*
  182.      * Since we don't want to mess with changing Save (which would
  183.      * require changes to CLASSLIB, urk) we instead save fRemember
  184.      * in the document before calling Save which supresses the call
  185.      * to CPatronDoc::Rename if FALSE.
  186.      */
  187.  
  188.     m_pDoc->m_fRename=fRemember;
  189.    #ifdef WIN32ANSI
  190.     char        szTemp[CCHPATHMAX];
  191.  
  192.     WideCharToMultiByte(CP_ACP, 0, pszFile, -1, szTemp, CCHPATHMAX
  193.        , NULL, NULL);
  194.     uRet=m_pDoc->Save(0, szTemp);
  195.    #else
  196.     uRet=m_pDoc->Save(0, (LPTSTR)pszFile);
  197.    #endif
  198.     m_pDoc->m_fRename=TRUE;
  199.  
  200.     return (DOCERR_NONE==uRet) ? NOERROR
  201.         : ResultFromScode(STG_E_WRITEFAULT);
  202.     }
  203.  
  204.  
  205.  
  206.  
  207.  
  208. /*
  209.  * IPersistFile::SaveCompleted
  210.  *
  211.  * Purpose:
  212.  *  Informs us that the operation that called Save is now finished
  213.  *  and we can access the file again.
  214.  *
  215.  * Parameters:
  216.  *  pszFile         LPCOLESTR of the file in which we can start
  217.  *                  writing again.
  218.  *
  219.  * Return Value:
  220.  *  HRESULT         NOERROR or a general error value.
  221.  */
  222.  
  223. STDMETHODIMP CImpIPersistFile::SaveCompleted(LPCOLESTR pszFile)
  224.     {
  225.     return NOERROR;
  226.     }
  227.  
  228.  
  229.  
  230.  
  231. /*
  232.  * IPersistFile::GetCurFile
  233.  *
  234.  * Purpose:
  235.  *  Retrieves the name of the current file.
  236.  *
  237.  * Parameters:
  238.  *  ppszFile        LPOLESTR * into which we store a pointer to
  239.  *                  the filename that should be allocated with the
  240.  *                  shared IMalloc.
  241.  *
  242.  * Return Value:
  243.  *  HRESULT         NOERROR or a general error value.
  244.  */
  245.  
  246. STDMETHODIMP CImpIPersistFile::GetCurFile(LPOLESTR *ppszFile)
  247.     {
  248.     LPMALLOC    pIMalloc;
  249.     LPOLESTR    psz;
  250.     UINT        uRet;
  251.  
  252.     *ppszFile=NULL;
  253.  
  254.     if (FAILED(CoGetMalloc(MEMCTX_TASK, &pIMalloc)))
  255.         return ResultFromScode(E_FAIL);
  256.  
  257.     psz=(LPOLESTR)pIMalloc->Alloc(CCHPATHMAX*sizeof(OLECHAR));
  258.     pIMalloc->Release();
  259.  
  260.    #ifdef WIN32ANSI
  261.     char    szTemp[CCHPATHMAX];
  262.  
  263.     uRet=m_pDoc->FilenameGet(szTemp, CCHPATHMAX);
  264.     MultiByteToWideChar(CP_ACP, 0, szTemp, -1, psz, CCHPATHMAX);
  265.    #else
  266.     uRet=m_pDoc->FilenameGet(psz, CCHPATHMAX);
  267.    #endif
  268.  
  269.     //If we have no filename, return the prompt for File Open/Save.
  270.     if (0==uRet)
  271.        #ifdef WIN32ANSI
  272.         MultiByteToWideChar(CP_ACP, 0
  273.             , (*m_pDoc->m_pST)[IDS_EXTENSION], -1, psz, CCHPATHMAX);
  274.        #else
  275.         lstrcpy(psz, (*m_pDoc->m_pST)[IDS_EXTENSION]);
  276.        #endif
  277.  
  278.     *ppszFile=psz;
  279.     return (0==uRet) ? ResultFromScode(S_FALSE) : NOERROR;
  280.     }
  281.