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 / chap19 / polyline / idataobj.cpp < prev    next >
C/C++ Source or Header  |  1995-05-03  |  10KB  |  406 lines

  1. /*
  2.  * IDATAOBJ.CPP
  3.  * Polyline Component Chapter 19
  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 "polyline.h"
  16.  
  17.  
  18. /*
  19.  * CImpIDataObject::CImpIDataObject
  20.  * CImpIDataObject::~CImpIDataObject
  21.  *
  22.  * Parameters (Constructor):
  23.  *  pObj            PCPolyline of the object we're in.
  24.  *  pUnkOuter       LPUNKNOWN to which we delegate.
  25.  */
  26.  
  27. CImpIDataObject::CImpIDataObject(PCPolyline 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.  
  56. STDMETHODIMP_(ULONG) CImpIDataObject::AddRef(void)
  57.     {
  58.     ++m_cRef;
  59.     return m_pUnkOuter->AddRef();
  60.     }
  61.  
  62. STDMETHODIMP_(ULONG) CImpIDataObject::Release(void)
  63.     {
  64.     --m_cRef;
  65.     return m_pUnkOuter->Release();
  66.     }
  67.  
  68.  
  69.  
  70.  
  71.  
  72. /*
  73.  * CImpIDataObject::GetData
  74.  *
  75.  * Purpose:
  76.  *  Retrieves data described by a specific FormatEtc into a StgMedium
  77.  *  allocated by this function.  Used like GetClipboardData.
  78.  *
  79.  * Parameters:
  80.  *  pFE             LPFORMATETC describing the desired data.
  81.  *  pSTM            LPSTGMEDIUM in which to return the data.
  82.  *
  83.  * Return Value:
  84.  *  HRESULT         NOERROR or a general error value.
  85.  */
  86.  
  87. STDMETHODIMP CImpIDataObject::GetData(LPFORMATETC pFE
  88.     , LPSTGMEDIUM pSTM)
  89.     {
  90.     UINT            cf=pFE->cfFormat;
  91.  
  92.     //Check the aspects we support.
  93.     if (!(DVASPECT_CONTENT & pFE->dwAspect))
  94.         return ResultFromScode(DATA_E_FORMATETC);
  95.  
  96.     pSTM->pUnkForRelease=NULL;
  97.  
  98.     //Run creates the window to use as a basis for extents
  99.     m_pObj->m_pImpIRunnableObject->Run(NULL);
  100.  
  101.     //Go render the appropriate data for the format.
  102.     switch (cf)
  103.         {
  104.         case CF_METAFILEPICT:
  105.             pSTM->tymed=TYMED_MFPICT;
  106.             return m_pObj->RenderMetafilePict(&pSTM->hGlobal);
  107.  
  108.         case CF_BITMAP:
  109.             pSTM->tymed=TYMED_GDI;
  110.             return m_pObj->RenderBitmap((HBITMAP *)&pSTM->hGlobal);
  111.  
  112.         default:
  113.             if (cf==m_pObj->m_cf)
  114.                 {
  115.                 pSTM->tymed=TYMED_HGLOBAL;
  116.                 return m_pObj->RenderNative(&pSTM->hGlobal);
  117.                 }
  118.  
  119.             break;
  120.         }
  121.  
  122.     return ResultFromScode(DATA_E_FORMATETC);
  123.     }
  124.  
  125.  
  126.  
  127.  
  128. /*
  129.  * CImpIDataObject::GetDataHere
  130.  *
  131.  * Purpose:
  132.  *  Renders the specific FormatEtc into caller-allocated medium
  133.  *  provided in pSTM.
  134.  *
  135.  * Parameters:
  136.  *  pFE             LPFORMATETC describing the desired data.
  137.  *  pSTM            LPSTGMEDIUM providing the medium into which
  138.  *                  wer render the data.
  139.  *
  140.  * Return Value:
  141.  *  HRESULT         NOERROR or a general error value.
  142.  */
  143.  
  144. STDMETHODIMP CImpIDataObject::GetDataHere(LPFORMATETC pFE
  145.     , LPSTGMEDIUM pSTM)
  146.     {
  147.     //CHAPTER19MOD
  148.     //We can provide CFSTR_EMBEDSOURCE now.
  149.  
  150.     UINT        cf;
  151.     HRESULT     hr;
  152.  
  153.     /*
  154.      * The only reasonable time this is called is for
  155.      * CFSTR_EMBEDSOURCE and TYMED_ISTORAGE (and later for
  156.      * CFSTR_LINKSOURCE).  This means the same as
  157.      * IPersistStorage::Save.
  158.      */
  159.  
  160.     cf=RegisterClipboardFormat(CFSTR_EMBEDSOURCE);
  161.  
  162.     //Aspect is unimportant to us here, as is lindex and ptd.
  163.     if (cf==pFE->cfFormat && (TYMED_ISTORAGE & pFE->tymed))
  164.         {
  165.         //We have an IStorage we can write into.
  166.         pSTM->tymed=TYMED_ISTORAGE;
  167.         pSTM->pUnkForRelease=NULL;
  168.  
  169.         hr=m_pObj->m_pImpIPersistStorage->Save(pSTM->pstg, FALSE);
  170.         m_pObj->m_pImpIPersistStorage->SaveCompleted(NULL);
  171.         return hr;
  172.         }
  173.     //End CHAPTER19MOD
  174.  
  175.     return ResultFromScode(DATA_E_FORMATETC);
  176.     }
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183. /*
  184.  * CImpIDataObject::QueryGetData
  185.  *
  186.  * Purpose:
  187.  *  Tests if a call to GetData with this FormatEtc will provide
  188.  *  any rendering; used like IsClipboardFormatAvailable.
  189.  *
  190.  * Parameters:
  191.  *  pFE             LPFORMATETC describing the desired data.
  192.  *
  193.  * Return Value:
  194.  *  HRESULT         NOERROR or a general error value.
  195.  */
  196.  
  197. STDMETHODIMP CImpIDataObject::QueryGetData(LPFORMATETC pFE)
  198.     {
  199.     UINT            cf=pFE->cfFormat;
  200.     BOOL            fRet=FALSE;
  201.  
  202.     //Check the aspects we support.
  203.     if (!(DVASPECT_CONTENT & pFE->dwAspect))
  204.         return ResultFromScode(DATA_E_FORMATETC);
  205.  
  206.     switch (cf)
  207.         {
  208.         case CF_METAFILEPICT:
  209.             fRet=(BOOL)(pFE->tymed & TYMED_MFPICT);
  210.             break;
  211.  
  212.         case CF_BITMAP:
  213.             fRet=(BOOL)(pFE->tymed & TYMED_GDI);
  214.             break;
  215.  
  216.         default:
  217.             //Check our own format.
  218.             fRet=((cf==m_pObj->m_cf)
  219.                 && (BOOL)(pFE->tymed & TYMED_HGLOBAL));
  220.             break;
  221.         }
  222.  
  223.     return fRet ? NOERROR : ResultFromScode(S_FALSE);
  224.     }
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231. /*
  232.  * CImpIDataObject::GetCanonicalFormatEtc
  233.  *
  234.  * Purpose:
  235.  *  Provides the caller with an equivalent FormatEtc to the one
  236.  *  provided when different FormatEtcs will produce exactly the
  237.  *  same renderings.
  238.  *
  239.  * Parameters:
  240.  *  pFEIn            LPFORMATETC of the first description.
  241.  *  pFEOut           LPFORMATETC of the equal description.
  242.  *
  243.  * Return Value:
  244.  *  HRESULT         NOERROR or a general error value.
  245.  */
  246.  
  247. STDMETHODIMP CImpIDataObject::GetCanonicalFormatEtc
  248.     (LPFORMATETC pFEIn, LPFORMATETC pFEOut)
  249.     {
  250.     if (NULL==pFEOut)
  251.         return ResultFromScode(E_INVALIDARG);
  252.  
  253.     pFEOut->ptd=NULL;
  254.     return ResultFromScode(DATA_S_SAMEFORMATETC);
  255.     }
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262. /*
  263.  * CImpIDataObject::SetData
  264.  *
  265.  * Purpose:
  266.  *  Places data described by a FormatEtc and living in a StgMedium
  267.  *  into the object.  The object may be responsible to clean up the
  268.  *  StgMedium before exiting.
  269.  *
  270.  * Parameters:
  271.  *  pFE             LPFORMATETC describing the data to set.
  272.  *  pSTM            LPSTGMEDIUM containing the data.
  273.  *  fRelease        BOOL indicating if this function is responsible
  274.  *                  for freeing the data.
  275.  *
  276.  * Return Value:
  277.  *  HRESULT         NOERROR or a general error value.
  278.  */
  279.  
  280. STDMETHODIMP CImpIDataObject::SetData(LPFORMATETC pFE
  281.     , LPSTGMEDIUM pSTM, BOOL fRelease)
  282.     {
  283.     UINT            cf=pFE->cfFormat;
  284.     BOOL            fRet=FALSE;
  285.     PPOLYLINEDATA   ppl;
  286.  
  287.     //Check for our own clipboard format and DVASPECT_CONTENT
  288.     if ((cf!=m_pObj->m_cf) || !(DVASPECT_CONTENT & pFE->dwAspect))
  289.         return ResultFromScode(DATA_E_FORMATETC);
  290.  
  291.     /*
  292.      * Data can only come from global memory containing a
  293.      * POLYLINEDATA structure that we send to the Polyline's
  294.      * DataSet, a now internal function used from here and
  295.      * from IPersistStorage::Load.
  296.      */
  297.  
  298.     if (TYMED_HGLOBAL!=pSTM->tymed)
  299.         return ResultFromScode(DATA_E_FORMATETC);
  300.  
  301.     ppl=(PPOLYLINEDATA)GlobalLock(pSTM->hGlobal);
  302.  
  303.     if (NULL!=ppl)
  304.         {
  305.         m_pObj->DataSet(ppl, TRUE, TRUE);
  306.         GlobalUnlock(pSTM->hGlobal);
  307.         fRet=TRUE;
  308.         }
  309.  
  310.     if (fRelease)
  311.         ReleaseStgMedium(pSTM);
  312.  
  313.     return fRet ? NOERROR : ResultFromScode(DATA_E_FORMATETC);
  314.     }
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321. /*
  322.  * CImpIDataObject::EnumFormatEtc
  323.  *
  324.  * Purpose:
  325.  *  Returns an IEnumFORMATETC object through which the caller can
  326.  *  iterate to learn about all the data formats this object can
  327.  *  provide through either GetData[Here] or SetData.
  328.  *
  329.  * Parameters:
  330.  *  dwDir           DWORD describing a data direction, either
  331.  *                  DATADIR_SET or DATADIR_GET.
  332.  *  ppEnum          LPENUMFORMATETC * in which to return the
  333.  *                  pointer to the enumerator.
  334.  *
  335.  * Return Value:
  336.  *  HRESULT         NOERROR or a general error value.
  337.  */
  338.  
  339. STDMETHODIMP CImpIDataObject::EnumFormatEtc(DWORD dwDir
  340.     , LPENUMFORMATETC *ppEnum)
  341.     {
  342.     //CHAPTER19MOD
  343.     //Can now just tell the default handler to do this busy work.
  344.     return m_pObj->m_pDefIDataObject->EnumFormatEtc(dwDir, ppEnum);
  345.     //CHAPTER19MOD
  346.     }
  347.  
  348.  
  349.  
  350.  
  351. //CHAPTER19MOD
  352. //Removed some comments from this stuff.
  353.  
  354. /*
  355.  * CImpIDataObject::DAdvise
  356.  * CImpIDataObject::DUnadvise
  357.  * CImpIDataObject::EnumDAdvise
  358.  */
  359.  
  360. STDMETHODIMP CImpIDataObject::DAdvise(LPFORMATETC pFE, DWORD dwFlags
  361.     , LPADVISESINK pIAdviseSink, LPDWORD pdwConn)
  362.     {
  363.     HRESULT         hr;
  364.  
  365.     if (NULL==m_pObj->m_pIDataAdviseHolder)
  366.         {
  367.         hr=CreateDataAdviseHolder(&m_pObj->m_pIDataAdviseHolder);
  368.  
  369.         if (FAILED(hr))
  370.             return ResultFromScode(E_OUTOFMEMORY);
  371.         }
  372.  
  373.     hr=m_pObj->m_pIDataAdviseHolder->Advise(this, pFE
  374.         , dwFlags, pIAdviseSink, pdwConn);
  375.  
  376.     return hr;
  377.     }
  378.  
  379.  
  380. STDMETHODIMP CImpIDataObject::DUnadvise(DWORD dwConn)
  381.     {
  382.     HRESULT         hr;
  383.  
  384.     if (NULL==m_pObj->m_pIDataAdviseHolder)
  385.         return ResultFromScode(E_FAIL);
  386.  
  387.     hr=m_pObj->m_pIDataAdviseHolder->Unadvise(dwConn);
  388.  
  389.     return hr;
  390.     }
  391.  
  392.  
  393.  
  394. STDMETHODIMP CImpIDataObject::EnumDAdvise(LPENUMSTATDATA *ppEnum)
  395.     {
  396.     HRESULT         hr;
  397.  
  398.     if (NULL==m_pObj->m_pIDataAdviseHolder)
  399.         return ResultFromScode(E_FAIL);
  400.  
  401.     hr=m_pObj->m_pIDataAdviseHolder->EnumAdvise(ppEnum);
  402.     return hr;
  403.     }
  404.  
  405. //End CHAPTER19MOD
  406.