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 / chap18 / cosmo / idataobj.cpp < prev    next >
C/C++ Source or Header  |  1995-05-03  |  9KB  |  363 lines

  1. /*
  2.  * IDATAOBJ.CPP
  3.  * Cosmo Chapter 18
  4.  *
  5.  * Implementation of the IDataObject interface.
  6.  *
  7.  * Copyright (c)1993-1995 Microsoft Corporation, All Rights Reserved
  8.  *
  9.  * Kraig Brockschmidt, Microsoft
  10.  * Internet  :  kraigb@microsoft.com
  11.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  12.  */
  13.  
  14.  
  15. #include "cosmo.h"
  16.  
  17.  
  18. /*
  19.  * CImpIDataObject::CImpIDataObject
  20.  * CImpIDataObject::~CImpIDataObject
  21.  *
  22.  * Parameters (Constructor):
  23.  *  pObj            PCFigure of the object we're in.
  24.  *  pUnkOuter       LPUNKNOWN to which we delegate.
  25.  */
  26.  
  27. CImpIDataObject::CImpIDataObject(PCFigure pObj
  28.     , LPUNKNOWN pUnkOuter)
  29.     {
  30.     m_cRef=0;
  31.     m_pObj=pObj;
  32.     m_pUnkOuter=pUnkOuter;
  33.     return;
  34.     }
  35.  
  36. CImpIDataObject::~CImpIDataObject(void)
  37.     {
  38.     return;
  39.     }
  40.  
  41.  
  42.  
  43.  
  44. /*
  45.  * CImpIDataObject::QueryInterface
  46.  * CImpIDataObject::AddRef
  47.  * CImpIDataObject::Release
  48.  */
  49.  
  50. STDMETHODIMP CImpIDataObject::QueryInterface(REFIID riid, PPVOID ppv)
  51.     {
  52.     return m_pUnkOuter->QueryInterface(riid, ppv);
  53.     }
  54.  
  55. STDMETHODIMP_(ULONG) CImpIDataObject::AddRef(void)
  56.     {
  57.     ++m_cRef;
  58.     return m_pUnkOuter->AddRef();
  59.     }
  60.  
  61. STDMETHODIMP_(ULONG) CImpIDataObject::Release(void)
  62.     {
  63.     --m_cRef;
  64.     return m_pUnkOuter->Release();
  65.     }
  66.  
  67.  
  68.  
  69.  
  70.  
  71. /*
  72.  * CImpIDataObject::GetData
  73.  *
  74.  * Purpose:
  75.  *  Retrieves data described by a specific FormatEtc into a StgMedium
  76.  *  allocated by this function.  Used like GetClipboardData.
  77.  *
  78.  * Parameters:
  79.  *  pFE             LPFORMATETC describing the desired data.
  80.  *  pSTM            LPSTGMEDIUM in which to return the data.
  81.  *
  82.  * Return Value:
  83.  *  HRESULT         NOERROR or a general error value.
  84.  */
  85.  
  86. STDMETHODIMP CImpIDataObject::GetData(LPFORMATETC pFE
  87.     , LPSTGMEDIUM pSTM)
  88.     {
  89.     UINT            cf=pFE->cfFormat;
  90.     BOOL            fRet=FALSE;
  91.  
  92.     //Another part of us already knows if the format is good.
  93.     if (NOERROR!=QueryGetData(pFE))
  94.         return ResultFromScode(DATA_E_FORMATETC);
  95.  
  96.     if (CF_METAFILEPICT==cf || CF_BITMAP==cf || m_pObj->m_cf==cf)
  97.         {
  98.         if (CF_METAFILEPICT==cf)
  99.             {
  100.             pSTM->tymed=TYMED_MFPICT;
  101.             }
  102.         else
  103.             pSTM->tymed=TYMED_HGLOBAL;
  104.  
  105.         pSTM->pUnkForRelease=NULL;
  106.         pSTM->hGlobal=m_pObj->m_pDoc->RenderFormat(cf);
  107.         fRet=(NULL!=pSTM->hGlobal);
  108.         }
  109.     else
  110.         fRet=m_pObj->m_pDoc->RenderMedium(cf, pSTM);
  111.  
  112.     return fRet ? NOERROR : ResultFromScode(DATA_E_FORMATETC);
  113.     }
  114.  
  115.  
  116.  
  117.  
  118. /*
  119.  * CImpIDataObject::GetDataHere
  120.  *
  121.  * Purpose:
  122.  *  Renders the specific FormatEtc into caller-allocated medium
  123.  *  provided in pSTM.
  124.  *
  125.  * Parameters:
  126.  *  pFE             LPFORMATETC describing the desired data.
  127.  *  pSTM            LPSTGMEDIUM providing the medium into which
  128.  *                  wer render the data.
  129.  *
  130.  * Return Value:
  131.  *  HRESULT         NOERROR or a general error value.
  132.  */
  133.  
  134. STDMETHODIMP CImpIDataObject::GetDataHere(LPFORMATETC pFE
  135.     , LPSTGMEDIUM pSTM)
  136.     {
  137.     UINT        cf;
  138.     LONG        lRet;
  139.  
  140.     /*
  141.      * The only reasonable time this is called is for
  142.      * CFSTR_EMBEDSOURCE and TYMED_ISTORAGE (and later for
  143.      * CFSTR_LINKSOURCE).  This means the same as
  144.      * IPersistStorage::Save.
  145.      */
  146.  
  147.     cf=RegisterClipboardFormat(CFSTR_EMBEDSOURCE);
  148.  
  149.     //Aspect is unimportant to us here, as is lindex and ptd.
  150.     if (cf==pFE->cfFormat && (TYMED_ISTORAGE & pFE->tymed))
  151.         {
  152.         //We have an IStorage we can write into.
  153.         pSTM->tymed=TYMED_ISTORAGE;
  154.         pSTM->pUnkForRelease=NULL;
  155.         lRet=m_pObj->m_pPL->WriteToStorage(pSTM->pstg
  156.             , VERSIONCURRENT);
  157.  
  158.         if (lRet >= 0)
  159.             return NOERROR;
  160.  
  161.         return ResultFromScode(STG_E_WRITEFAULT);
  162.         }
  163.  
  164.     return ResultFromScode(DATA_E_FORMATETC);
  165.     }
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172. /*
  173.  * CImpIDataObject::QueryGetData
  174.  *
  175.  * Purpose:
  176.  *  Tests if a call to GetData with this FormatEtc will provide
  177.  *  any rendering; used like IsClipboardFormatAvailable.
  178.  *
  179.  * Parameters:
  180.  *  pFE             LPFORMATETC describing the desired data.
  181.  *
  182.  * Return Value:
  183.  *  HRESULT         NOERROR or a general error value.
  184.  */
  185.  
  186. STDMETHODIMP CImpIDataObject::QueryGetData(LPFORMATETC pFE)
  187.     {
  188.     UINT            cf=pFE->cfFormat;
  189.     UINT            i;
  190.  
  191.     //Check the aspects we support.
  192.     if (!(DVASPECT_CONTENT & pFE->dwAspect))
  193.         return ResultFromScode(S_FALSE);
  194.  
  195.     for (i=0; i < m_pObj->m_cfeGet; i++)
  196.         {
  197.         if (pFE->cfFormat==m_pObj->m_rgfeGet[i].cfFormat
  198.             && pFE->tymed & m_pObj->m_rgfeGet[i].tymed)
  199.             {
  200.             return NOERROR;
  201.             }
  202.         }
  203.  
  204.     return ResultFromScode(S_FALSE);
  205.     }
  206.  
  207.  
  208.  
  209.  
  210.  
  211. /*
  212.  * CImpIDataObject::GetCanonicalFormatEtc
  213.  *
  214.  * Purpose:
  215.  *  Provides the caller with an equivalent FormatEtc to the one
  216.  *  provided when different FormatEtcs will produce exactly the
  217.  *  same renderings.
  218.  *
  219.  * Parameters:
  220.  *  pFEIn            LPFORMATETC of the first description.
  221.  *  pFEOut           LPFORMATETC of the equal description.
  222.  *
  223.  * Return Value:
  224.  *  HRESULT         NOERROR or a general error value.
  225.  */
  226.  
  227. STDMETHODIMP CImpIDataObject::GetCanonicalFormatEtc
  228.     (LPFORMATETC pFEIn, LPFORMATETC pFEOut)
  229.     {
  230.     if (NULL==pFEOut)
  231.         return ResultFromScode(E_INVALIDARG);
  232.  
  233.     pFEOut->ptd=NULL;
  234.     return ResultFromScode(DATA_S_SAMEFORMATETC);
  235.     }
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242. /*
  243.  * CImpIDataObject::SetData
  244.  *
  245.  * Purpose:
  246.  *  Places data described by a FormatEtc and living in a StgMedium
  247.  *  into the object.  The object may be responsible to clean up the
  248.  *  StgMedium before exiting.
  249.  *
  250.  * Parameters:
  251.  *  pFE             LPFORMATETC describing the data to set.
  252.  *  pSTM            LPSTGMEDIUM containing the data.
  253.  *  fRelease        BOOL indicating if this function is responsible
  254.  *                  for freeing the data.
  255.  *
  256.  * Return Value:
  257.  *  HRESULT         NOERROR or a general error value.
  258.  */
  259.  
  260. STDMETHODIMP CImpIDataObject::SetData(LPFORMATETC pFE
  261.     , LPSTGMEDIUM pSTM, BOOL fRelease)
  262.     {
  263.     LONG            lRet;
  264.  
  265.     /*
  266.      * Data can only come from global memory containing a
  267.      * POLYLINEDATA structure that we send to the Polyline's
  268.      * DataSetMem.
  269.      */
  270.     if ((pFE->cfFormat!=m_pObj->m_cf)
  271.         || !(DVASPECT_CONTENT & pFE->dwAspect)
  272.         || (TYMED_HGLOBAL!=pSTM->tymed))
  273.         return ResultFromScode(DATA_E_FORMATETC);
  274.  
  275.     lRet=m_pObj->m_pPL->DataSetMem(pSTM->hGlobal, FALSE, TRUE
  276.         , TRUE);
  277.  
  278.     if (fRelease)
  279.         ReleaseStgMedium(pSTM);
  280.  
  281.     return (POLYLINE_E_NONE==lRet) ?
  282.         NOERROR : ResultFromScode(DATA_E_FORMATETC);
  283.     }
  284.  
  285.  
  286.  
  287.  
  288.  
  289.  
  290. /*
  291.  * CImpIDataObject::EnumFormatEtc
  292.  *
  293.  * Purpose:
  294.  *  Returns an IEnumFORMATETC object through which the caller can
  295.  *  iterate to learn about all the data formats this object can
  296.  *  provide through either GetData[Here] or SetData.
  297.  *
  298.  * Parameters:
  299.  *  dwDir           DWORD describing a data direction, either
  300.  *                  DATADIR_SET or DATADIR_GET.
  301.  *  ppEnum          LPENUMFORMATETC * in which to return the
  302.  *                  pointer to the enumerator.
  303.  *
  304.  * Return Value:
  305.  *  HRESULT         NOERROR or a general error value.
  306.  */
  307.  
  308. STDMETHODIMP CImpIDataObject::EnumFormatEtc(DWORD dwDir
  309.     , LPENUMFORMATETC *ppEnum)
  310.     {
  311.     return ResultFromScode(OLE_S_USEREG);
  312.     }
  313.  
  314.  
  315.  
  316.  
  317.  
  318. /*
  319.  * CImpIDataObject::DAdvise
  320.  * CImpIDataObject::DUnadvise
  321.  * CImpIDataObject::EnumDAdvise
  322.  *
  323.  * Pass-throughs to IDataAdviseHolder.
  324.  */
  325.  
  326. STDMETHODIMP CImpIDataObject::DAdvise(LPFORMATETC pFE, DWORD dwFlags
  327.     , LPADVISESINK pIAdviseSink, LPDWORD pdwConn)
  328.     {
  329.     HRESULT         hr;
  330.  
  331.     if (NULL==m_pObj->m_pIDataAdviseHolder)
  332.         {
  333.         hr=CreateDataAdviseHolder(&m_pObj->m_pIDataAdviseHolder);
  334.  
  335.         if (FAILED(hr))
  336.             return ResultFromScode(E_OUTOFMEMORY);
  337.         }
  338.  
  339.     hr=m_pObj->m_pIDataAdviseHolder->Advise(this, pFE
  340.         , dwFlags, pIAdviseSink, pdwConn);
  341.  
  342.     return hr;
  343.     }
  344.  
  345.  
  346. STDMETHODIMP CImpIDataObject::DUnadvise(DWORD dwConn)
  347.     {
  348.     if (NULL==m_pObj->m_pIDataAdviseHolder)
  349.         return ResultFromScode(E_FAIL);
  350.  
  351.     return m_pObj->m_pIDataAdviseHolder->Unadvise(dwConn);
  352.     }
  353.  
  354.  
  355.  
  356. STDMETHODIMP CImpIDataObject::EnumDAdvise(LPENUMSTATDATA *ppEnum)
  357.     {
  358.     if (NULL==m_pObj->m_pIDataAdviseHolder)
  359.         return ResultFromScode(E_FAIL);
  360.  
  361.     return m_pObj->m_pIDataAdviseHolder->EnumAdvise(ppEnum);
  362.     }
  363.