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 / chap23 / cosmo / idataobj.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-03  |  8.6 KB  |  368 lines

  1. /*
  2.  * IDATAOBJ.CPP
  3.  * Cosmo Chapter 23
  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.         //These are for creating an embedded object from a file
  159.         WriteClassStg(pSTM->pstg, CLSID_CosmoFigure);
  160.         WriteFmtUserTypeStg(pSTM->pstg, m_pObj->m_cf
  161.             , (*m_pObj->m_pST)[IDS_USERTYPE]);
  162.  
  163.         if (lRet >= 0)
  164.             return NOERROR;
  165.  
  166.         return ResultFromScode(STG_E_WRITEFAULT);
  167.         }
  168.  
  169.     return ResultFromScode(DATA_E_FORMATETC);
  170.     }
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177. /*
  178.  * CImpIDataObject::QueryGetData
  179.  *
  180.  * Purpose:
  181.  *  Tests if a call to GetData with this FormatEtc will provide
  182.  *  any rendering; used like IsClipboardFormatAvailable.
  183.  *
  184.  * Parameters:
  185.  *  pFE             LPFORMATETC describing the desired data.
  186.  *
  187.  * Return Value:
  188.  *  HRESULT         NOERROR or a general error value.
  189.  */
  190.  
  191. STDMETHODIMP CImpIDataObject::QueryGetData(LPFORMATETC pFE)
  192.     {
  193.     UINT            cf=pFE->cfFormat;
  194.     UINT            i;
  195.  
  196.     //Check the aspects we support.
  197.     if (!(DVASPECT_CONTENT & pFE->dwAspect))
  198.         return ResultFromScode(S_FALSE);
  199.  
  200.     for (i=0; i < m_pObj->m_cfeGet; i++)
  201.         {
  202.         if (pFE->cfFormat==m_pObj->m_rgfeGet[i].cfFormat
  203.             && pFE->tymed & m_pObj->m_rgfeGet[i].tymed)
  204.             {
  205.             return NOERROR;
  206.             }
  207.         }
  208.  
  209.     return ResultFromScode(S_FALSE);
  210.     }
  211.  
  212.  
  213.  
  214.  
  215.  
  216. /*
  217.  * CImpIDataObject::GetCanonicalFormatEtc
  218.  *
  219.  * Purpose:
  220.  *  Provides the caller with an equivalent FormatEtc to the one
  221.  *  provided when different FormatEtcs will produce exactly the
  222.  *  same renderings.
  223.  *
  224.  * Parameters:
  225.  *  pFEIn            LPFORMATETC of the first description.
  226.  *  pFEOut           LPFORMATETC of the equal description.
  227.  *
  228.  * Return Value:
  229.  *  HRESULT         NOERROR or a general error value.
  230.  */
  231.  
  232. STDMETHODIMP CImpIDataObject::GetCanonicalFormatEtc
  233.     (LPFORMATETC pFEIn, LPFORMATETC pFEOut)
  234.     {
  235.     if (NULL==pFEOut)
  236.         return ResultFromScode(E_INVALIDARG);
  237.  
  238.     pFEOut->ptd=NULL;
  239.     return ResultFromScode(DATA_S_SAMEFORMATETC);
  240.     }
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247. /*
  248.  * CImpIDataObject::SetData
  249.  *
  250.  * Purpose:
  251.  *  Places data described by a FormatEtc and living in a StgMedium
  252.  *  into the object.  The object may be responsible to clean up the
  253.  *  StgMedium before exiting.
  254.  *
  255.  * Parameters:
  256.  *  pFE             LPFORMATETC describing the data to set.
  257.  *  pSTM            LPSTGMEDIUM containing the data.
  258.  *  fRelease        BOOL indicating if this function is responsible
  259.  *                  for freeing the data.
  260.  *
  261.  * Return Value:
  262.  *  HRESULT         NOERROR or a general error value.
  263.  */
  264.  
  265. STDMETHODIMP CImpIDataObject::SetData(LPFORMATETC pFE
  266.     , LPSTGMEDIUM pSTM, BOOL fRelease)
  267.     {
  268.     LONG            lRet;
  269.  
  270.     /*
  271.      * Data can only come from global memory containing a
  272.      * POLYLINEDATA structure that we send to the Polyline's
  273.      * DataSetMem.
  274.      */
  275.     if ((pFE->cfFormat!=m_pObj->m_cf)
  276.         || !(DVASPECT_CONTENT & pFE->dwAspect)
  277.         || (TYMED_HGLOBAL!=pSTM->tymed))
  278.         return ResultFromScode(DATA_E_FORMATETC);
  279.  
  280.     lRet=m_pObj->m_pPL->DataSetMem(pSTM->hGlobal, FALSE, TRUE
  281.         , TRUE);
  282.  
  283.     if (fRelease)
  284.         ReleaseStgMedium(pSTM);
  285.  
  286.     return (POLYLINE_E_NONE==lRet) ?
  287.         NOERROR : ResultFromScode(DATA_E_FORMATETC);
  288.     }
  289.  
  290.  
  291.  
  292.  
  293.  
  294.  
  295. /*
  296.  * CImpIDataObject::EnumFormatEtc
  297.  *
  298.  * Purpose:
  299.  *  Returns an IEnumFORMATETC object through which the caller can
  300.  *  iterate to learn about all the data formats this object can
  301.  *  provide through either GetData[Here] or SetData.
  302.  *
  303.  * Parameters:
  304.  *  dwDir           DWORD describing a data direction, either
  305.  *                  DATADIR_SET or DATADIR_GET.
  306.  *  ppEnum          LPENUMFORMATETC * in which to return the
  307.  *                  pointer to the enumerator.
  308.  *
  309.  * Return Value:
  310.  *  HRESULT         NOERROR or a general error value.
  311.  */
  312.  
  313. STDMETHODIMP CImpIDataObject::EnumFormatEtc(DWORD dwDir
  314.     , LPENUMFORMATETC *ppEnum)
  315.     {
  316.     return ResultFromScode(OLE_S_USEREG);
  317.     }
  318.  
  319.  
  320.  
  321.  
  322.  
  323. /*
  324.  * CImpIDataObject::DAdvise
  325.  * CImpIDataObject::DUnadvise
  326.  * CImpIDataObject::EnumDAdvise
  327.  *
  328.  * Pass-throughs to IDataAdviseHolder.
  329.  */
  330.  
  331. STDMETHODIMP CImpIDataObject::DAdvise(LPFORMATETC pFE, DWORD dwFlags
  332.     , LPADVISESINK pIAdviseSink, LPDWORD pdwConn)
  333.     {
  334.     HRESULT         hr;
  335.  
  336.     if (NULL==m_pObj->m_pIDataAdviseHolder)
  337.         {
  338.         hr=CreateDataAdviseHolder(&m_pObj->m_pIDataAdviseHolder);
  339.  
  340.         if (FAILED(hr))
  341.             return ResultFromScode(E_OUTOFMEMORY);
  342.         }
  343.  
  344.     hr=m_pObj->m_pIDataAdviseHolder->Advise(this, pFE
  345.         , dwFlags, pIAdviseSink, pdwConn);
  346.  
  347.     return hr;
  348.     }
  349.  
  350.  
  351. STDMETHODIMP CImpIDataObject::DUnadvise(DWORD dwConn)
  352.     {
  353.     if (NULL==m_pObj->m_pIDataAdviseHolder)
  354.         return ResultFromScode(E_FAIL);
  355.  
  356.     return m_pObj->m_pIDataAdviseHolder->Unadvise(dwConn);
  357.     }
  358.  
  359.  
  360.  
  361. STDMETHODIMP CImpIDataObject::EnumDAdvise(LPENUMSTATDATA *ppEnum)
  362.     {
  363.     if (NULL==m_pObj->m_pIDataAdviseHolder)
  364.         return ResultFromScode(E_FAIL);
  365.  
  366.     return m_pObj->m_pIDataAdviseHolder->EnumAdvise(ppEnum);
  367.     }
  368.