home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / com / activedocument / doserver / dataobj.cpp < prev    next >
C/C++ Source or Header  |  1997-05-28  |  13KB  |  487 lines

  1. /**************************************************************************
  2.    THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  3.    ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  4.    THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  5.    PARTICULAR PURPOSE.
  6.  
  7.    Copyright 1997 Microsoft Corporation.  All Rights Reserved.
  8. **************************************************************************/
  9.  
  10. /**************************************************************************
  11.  
  12.    File:          DataObj.cpp
  13.    
  14.    Description:   CDataObject and CEnumForamtEtc implementation.
  15.  
  16. **************************************************************************/
  17.  
  18. /**************************************************************************
  19.    #include statements
  20. **************************************************************************/
  21.  
  22. #include "DataObj.h"
  23.  
  24. /**************************************************************************
  25.  
  26.    CDataObject::CDataObject()
  27.    
  28. **************************************************************************/
  29.  
  30. CDataObject::CDataObject(COleDocument *pOleDoc)
  31. {
  32. OutputDebugString(TEXT("CDataObject's constructor\n"));
  33.    
  34. m_pOleDoc = pOleDoc;
  35.  
  36. m_pDataAdviseHolder = NULL;
  37.  
  38. m_cfEmbedSource = RegisterClipboardFormat(CF_EMBEDSOURCE);
  39. }
  40.  
  41. /**************************************************************************
  42.  
  43.    CDataObject::~CDataObject()
  44.    
  45. **************************************************************************/
  46.  
  47. CDataObject::~CDataObject(void) 
  48. {
  49. if(m_pDataAdviseHolder)
  50.    m_pDataAdviseHolder->Release();
  51.  
  52. OutputDebugString(TEXT("CDataObject's destructor\n"));
  53. }
  54.  
  55. /**************************************************************************
  56.  
  57.    CDataObject::QueryInterface()
  58.    
  59. **************************************************************************/
  60.  
  61. STDMETHODIMP CDataObject::QueryInterface(REFIID riid, LPVOID *ppReturn)
  62. {
  63. OutputDebugString(TEXT("CDataObject::QueryInterface\n"));
  64.  
  65. return m_pOleDoc->QueryInterface(riid, ppReturn);
  66. }
  67.  
  68. /**************************************************************************
  69.  
  70.    CDataObject::AddRef()
  71.    
  72. **************************************************************************/
  73.  
  74. STDMETHODIMP_(ULONG) CDataObject::AddRef()
  75. {
  76. OutputDebugString(TEXT("CDataObject::AddRef\n"));
  77.  
  78. return m_pOleDoc->AddRef();
  79. }
  80.  
  81. /**************************************************************************
  82.  
  83.    CDataObject::Release()
  84.    
  85. **************************************************************************/
  86.  
  87. STDMETHODIMP_(ULONG) CDataObject::Release()
  88. {
  89. OutputDebugString(TEXT("CDataObject::Release\n"));
  90.    
  91. return m_pOleDoc->Release();
  92. }
  93.  
  94. /**************************************************************************
  95.  
  96.    CDataObject::QueryGetData()
  97.    
  98. **************************************************************************/
  99.  
  100. STDMETHODIMP CDataObject::QueryGetData(LPFORMATETC pformatetc)
  101. {
  102. OutputDebugString(TEXT("CDataObject::QueryGetData\n"));
  103.  
  104. HRESULT  hr = DATA_E_FORMATETC;
  105.  
  106. // check the validity of the formatetc.
  107. if( (pformatetc->cfFormat == m_cfEmbedSource)  &&
  108.     (pformatetc->dwAspect == DVASPECT_CONTENT) &&
  109.     (pformatetc->tymed == TYMED_ISTORAGE))
  110.    {
  111.    hr = S_OK;
  112.    }
  113.  
  114. return hr;
  115. }
  116.  
  117. /**************************************************************************
  118.  
  119.    CDataObject::DAdvise()
  120.    
  121. **************************************************************************/
  122.  
  123. STDMETHODIMP CDataObject::DAdvise(  LPFORMATETC pFormatetc, 
  124.                                     DWORD advf,
  125.                                     LPADVISESINK pAdvSink, 
  126.                                     LPDWORD pdwConnection)
  127. {
  128. OutputDebugString(TEXT("CDataObject::DAdvise\n"));
  129.  
  130. // if no DataAdviseHolder has been created, then create one.
  131. if(!m_pDataAdviseHolder)
  132.    CreateDataAdviseHolder(&m_pDataAdviseHolder);
  133.  
  134. if(m_pDataAdviseHolder)
  135.    {
  136.    // pass on to the DataAdviseHolder
  137.    return m_pDataAdviseHolder->Advise(this, pFormatetc, advf, pAdvSink, pdwConnection);
  138.    }
  139.  
  140. return E_FAIL;
  141. }
  142.  
  143. /**************************************************************************
  144.  
  145.    CDataObject::DUnadvise()
  146.    
  147. **************************************************************************/
  148.  
  149. STDMETHODIMP CDataObject::DUnadvise(DWORD dwConnection)
  150. {
  151. OutputDebugString(TEXT("CDataObject::DUnadvise\n"));
  152.  
  153. if(m_pDataAdviseHolder)
  154.    {
  155.    // pass on to the DataAdviseHolder
  156.    return m_pDataAdviseHolder->Unadvise(dwConnection);
  157.    }
  158.  
  159. return E_FAIL;
  160. }
  161.  
  162. /**************************************************************************
  163.  
  164.    CDataObject::GetData()
  165.    
  166. **************************************************************************/
  167.  
  168. STDMETHODIMP CDataObject::GetData(LPFORMATETC pFE, LPSTGMEDIUM pStgMedium)
  169. {
  170. OutputDebugString(TEXT("CDataObject::GetData\n"));
  171.  
  172. HRESULT  hr = E_NOTIMPL;
  173.  
  174. return hr;
  175. }
  176.  
  177. /**************************************************************************
  178.  
  179.    CDataObject::GetDataHere()
  180.    
  181. **************************************************************************/
  182.  
  183. STDMETHODIMP CDataObject::GetDataHere(LPFORMATETC pFormatEtc, LPSTGMEDIUM pStgMedium)
  184. {
  185. OutputDebugString(TEXT("CDataObject::GetDataHere\n"));
  186.  
  187. HRESULT  hr = DATA_E_FORMATETC;
  188.  
  189. if(pFormatEtc->cfFormat == m_cfEmbedSource)
  190.    {
  191.    hr = GetOleObjectDataHere( m_pOleDoc->m_pPersistStorage,
  192.                               pFormatEtc,
  193.                               pStgMedium);
  194.    }
  195.  
  196. return hr;
  197. }
  198.  
  199. /**************************************************************************
  200.  
  201.    CDataObject::GetCanonicalFormatEtc()
  202.    
  203. **************************************************************************/
  204.  
  205. STDMETHODIMP CDataObject::GetCanonicalFormatEtc(LPFORMATETC pformatetc, LPFORMATETC pformatetcOut)
  206. {
  207. OutputDebugString(TEXT("CDataObject::GetCanonicalFormatEtc\n"));
  208.  
  209. HRESULT hr;
  210.  
  211. if(!pformatetcOut)
  212.    return E_INVALIDARG;
  213.  
  214. //set all out parameters to NULL.
  215. pformatetcOut->ptd = NULL;
  216.  
  217. if(!pformatetc)
  218.    return E_INVALIDARG;
  219.  
  220. // OLE2NOTE: we must validate that the format requested is supported
  221. if(NOERROR != (hr = QueryGetData(pformatetc)))
  222.    return hr;
  223.  
  224. /* OLE2NOTE: an app that is insensitive to target device (as
  225. **    SimpSvr is) should fill in the lpformatOut parameter
  226. **    but NULL out the "ptd" field; it should return NOERROR if the
  227. **    input formatetc->ptd what non-NULL. this tells the caller
  228. **    that it is NOT necessary to maintain a separate screen
  229. **    rendering and printer rendering. if should return
  230. **    DATA_S_SAMEFORMATETC if the input and output formatetc's are
  231. **    identical.
  232. */
  233.  
  234. *pformatetcOut = *pformatetc;
  235.  
  236. if(NULL == pformatetc->ptd)
  237.    return DATA_S_SAMEFORMATETC;
  238.  
  239. pformatetcOut->ptd = NULL;
  240.  
  241. return NOERROR;
  242. }
  243.  
  244. /**************************************************************************
  245.  
  246.    CDataObject::SetData()
  247.    
  248. **************************************************************************/
  249.  
  250. STDMETHODIMP CDataObject::SetData(  LPFORMATETC pformatetc, 
  251.                                     LPSTGMEDIUM pmedium,
  252.                                     BOOL fRelease)
  253. {
  254. OutputDebugString(TEXT("CDataObject::SetData\n"));
  255.  
  256. return DATA_E_FORMATETC;
  257. }
  258.  
  259. /**************************************************************************
  260.  
  261.    CDataObject::EnumFormatEtc()
  262.    
  263. **************************************************************************/
  264.  
  265. STDMETHODIMP CDataObject::EnumFormatEtc(  DWORD dwDirection,
  266.                                           LPENUMFORMATETC *ppenumFormatEtc)
  267. {
  268. OutputDebugString(TEXT("CDataObject::EnumFormatEtc\n"));
  269.  
  270. // need to NULL the out parameter
  271. *ppenumFormatEtc = NULL;
  272.  
  273. if(DATADIR_SET == dwDirection)
  274.    return E_NOTIMPL;
  275.  
  276. *ppenumFormatEtc = new CEnumFormatEtc();
  277.  
  278. if(*ppenumFormatEtc)
  279.    {
  280.    (*ppenumFormatEtc)->AddRef();
  281.    return S_OK;
  282.    }
  283.  
  284. return E_OUTOFMEMORY;
  285. }
  286.  
  287. /**************************************************************************
  288.  
  289.    CDataObject::EnumDAdvise()
  290.    
  291. **************************************************************************/
  292.  
  293. STDMETHODIMP CDataObject::EnumDAdvise(LPENUMSTATDATA *ppenumAdvise)
  294. {
  295. OutputDebugString(TEXT("CDataObject::EnumDAdvise\n"));
  296.  
  297. // need to NULL the out parameter
  298. *ppenumAdvise = NULL;
  299.  
  300. return m_pDataAdviseHolder->EnumAdvise(ppenumAdvise);
  301. }
  302.  
  303. //////////////////////////////////////////////////////////////////////////////
  304. //
  305. // CEnumFormatEtc Implementation
  306. //
  307.  
  308. /**************************************************************************
  309.  
  310.    CEnumFormatEtc::CEnumFormatEtc()
  311.    
  312. **************************************************************************/
  313.  
  314. CEnumFormatEtc::CEnumFormatEtc()
  315. {
  316. OutputDebugString(TEXT("CEnumFormatEtc's constructor\n"));
  317.  
  318. m_ObjRefCount = 0;
  319.  
  320. m_iCur = 0;
  321.  
  322. m_cfEmbedSource = RegisterClipboardFormat(CF_EMBEDSOURCE);
  323. }
  324.  
  325. /**************************************************************************
  326.  
  327.    CEnumFormatEtc::~CEnumFormatEtc()
  328.    
  329. **************************************************************************/
  330.  
  331. CEnumFormatEtc::~CEnumFormatEtc(void)
  332. {
  333. OutputDebugString(TEXT("CEnumFormatEtc's destructor\n"));
  334. }
  335.  
  336. /**************************************************************************
  337.  
  338.    CEnumFormatEtc::QueryInterface()
  339.    
  340. **************************************************************************/
  341.  
  342. STDMETHODIMP CEnumFormatEtc::QueryInterface(REFIID riid, LPVOID *ppReturn)
  343. {
  344. OutputDebugString(TEXT("CEnumFormatEtc::QueryInterface\n"));
  345.  
  346. *ppReturn = NULL;
  347.  
  348. if(IsEqualIID(riid, IID_IUnknown))
  349.    {
  350.    *ppReturn = (LPVOID)this;
  351.    }
  352. else if(IsEqualIID(riid, IID_IEnumFORMATETC))
  353.    {
  354.    *ppReturn = (LPVOID)this;
  355.    }
  356.  
  357. if(NULL != *ppReturn)
  358.    {
  359.    ((LPUNKNOWN)*ppReturn)->AddRef();
  360.    return NOERROR;
  361.    }
  362.  
  363. return E_NOINTERFACE;
  364. }
  365.  
  366. /**************************************************************************
  367.  
  368.    CEnumFormatEtc::AddRef()
  369.    
  370. **************************************************************************/
  371.  
  372. STDMETHODIMP_(ULONG) CEnumFormatEtc::AddRef(void)
  373. {
  374. OutputDebugString(TEXT("CEnumFormatEtc::AddRef\n"));
  375.  
  376. return ++m_ObjRefCount;
  377. }
  378.  
  379. /**************************************************************************
  380.  
  381.    CEnumFormatEtc::Release()
  382.    
  383. **************************************************************************/
  384.  
  385. STDMETHODIMP_(ULONG) CEnumFormatEtc::Release(void)
  386. {
  387. TCHAR szText[MAX_PATH];
  388. wsprintf(szText, TEXT("CEnumFormatEtc::Release - ref count will be %d\n"), m_ObjRefCount - 1);
  389. OutputDebugString(szText);
  390.    
  391. if(0 == --m_ObjRefCount)
  392.    {
  393.    delete this;
  394.    return 0;
  395.    }
  396.  
  397. return m_ObjRefCount;
  398. }
  399.  
  400. /**************************************************************************
  401.  
  402.    CEnumFormatEtc::Next()
  403.    
  404. **************************************************************************/
  405.  
  406. STDMETHODIMP CEnumFormatEtc::Next(ULONG cRequested, LPFORMATETC pFE, ULONG* puFetched)
  407. {
  408. OutputDebugString(TEXT("CEnumFormatEtc::Next\n"));
  409.  
  410. if(m_iCur > 0)
  411.    {
  412.    *puFetched = 0;
  413.    return S_FALSE;
  414.    }
  415.  
  416. pFE->cfFormat = m_cfEmbedSource;
  417. pFE->dwAspect = DVASPECT_CONTENT;
  418. pFE->tymed == TYMED_ISTORAGE;
  419.  
  420. *puFetched = 1;
  421.  
  422. m_iCur++;
  423.  
  424. return S_OK;
  425. }
  426.  
  427. /**************************************************************************
  428.  
  429.    CEnumFormatEtc::Skip()
  430.    
  431. **************************************************************************/
  432.  
  433. STDMETHODIMP CEnumFormatEtc::Skip(ULONG cSkip)
  434. {
  435. OutputDebugString(TEXT("CEnumFormatEtc::Skip\n"));
  436.  
  437. if((m_iCur + cSkip) > 1)
  438.    return S_FALSE;
  439.  
  440. m_iCur += cSkip;
  441.  
  442. return S_OK;
  443. }
  444.  
  445. /**************************************************************************
  446.  
  447.    CEnumFormatEtc::Reset()
  448.    
  449. **************************************************************************/
  450.  
  451. STDMETHODIMP CEnumFormatEtc::Reset(void)
  452. {
  453. OutputDebugString(TEXT("CEnumFormatEtc::Reset\n"));
  454.  
  455. m_iCur = 0;
  456.  
  457. return S_OK;
  458. }
  459.  
  460. /**************************************************************************
  461.  
  462.    CEnumFormatEtc::Clone()
  463.    
  464. **************************************************************************/
  465.  
  466. STDMETHODIMP CEnumFormatEtc::Clone(LPENUMFORMATETC* ppEnum)
  467. {
  468. OutputDebugString(TEXT("CEnumFormatEtc::Clone\n"));
  469.  
  470. CEnumFormatEtc* pNew;
  471.  
  472. *ppEnum = NULL;
  473.  
  474. // Create the clone.
  475. pNew = new CEnumFormatEtc();
  476. if (NULL == pNew)
  477.    return (ResultFromScode(E_OUTOFMEMORY));
  478.  
  479. pNew->AddRef();
  480. pNew->m_iCur = m_iCur;
  481.  
  482. *ppEnum = pNew;
  483.  
  484. return S_OK;
  485. }
  486.  
  487.