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 / chap12 / datatran / dataobj.cpp next >
C/C++ Source or Header  |  1995-05-03  |  4KB  |  193 lines

  1. /*
  2.  * DATAOBJ.CPP
  3.  * Data Transfer Object Chapter 12
  4.  *
  5.  * Implementation of the CDataObject class.
  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 "dataobj.h"
  16.  
  17. extern HINSTANCE g_hInst;
  18.  
  19. /*
  20.  * CDataObject::CDataObject
  21.  * CDataObject::~CDataObject
  22.  *
  23.  * Parameters (Constructor):
  24.  *  pUnkOuter       LPUNKNOWN of a controlling unknown.
  25.  *  pfnDestroy      PFNDESTROYED to call when object is destroyed.
  26.  */
  27.  
  28. CDataObject::CDataObject(LPUNKNOWN pUnkOuter
  29.     , PFNDESTROYED pfnDestroy)
  30.     {
  31.     m_cRef=0;
  32.     m_pUnkOuter=pUnkOuter;
  33.     m_pfnDestroy=pfnDestroy;
  34.  
  35.     m_hList=NULL;
  36.     m_pIDataObject=NULL;
  37.  
  38.     return;
  39.     }
  40.  
  41.  
  42. CDataObject::~CDataObject(void)
  43.     {
  44.     if (NULL!=m_pIDataObject)
  45.         delete m_pIDataObject;
  46.  
  47.     Purge();
  48.  
  49.     if (NULL!=m_hList)
  50.         DestroyWindow(m_hList);
  51.  
  52.     return;
  53.     }
  54.  
  55.  
  56.  
  57. /*
  58.  * CDataObject::Init
  59.  *
  60.  * Purpose:
  61.  *  Performs any intiailization of a CDataObject that's prone to
  62.  *  failure that we also use internally before exposing the object
  63.  *  outside.
  64.  *
  65.  * Parameters:
  66.  *  None
  67.  *
  68.  * Return Value:
  69.  *  BOOL            TRUE if the function is successful,
  70.  *                  FALSE otherwise.
  71.  */
  72.  
  73. BOOL CDataObject::Init(void)
  74.     {
  75.     LPUNKNOWN       pIUnknown=this;
  76.  
  77.     if (NULL!=m_pUnkOuter)
  78.         pIUnknown=m_pUnkOuter;
  79.  
  80.     //Allocate contained interfaces.
  81.     m_pIDataObject=new CImpIDataObject(this, pIUnknown);
  82.  
  83.     if (NULL==m_pIDataObject)
  84.         return FALSE;
  85.  
  86.     m_hList=CreateWindow(TEXT("listbox"), TEXT("renderings")
  87.         , WS_POPUP | LBS_OWNERDRAWFIXED, 0, 0, 100, 100
  88.         , HWND_DESKTOP, NULL, g_hInst, NULL);
  89.  
  90.     if (NULL==m_hList)
  91.         return FALSE;
  92.  
  93.     return TRUE;
  94.     }
  95.  
  96.  
  97.  
  98. /*
  99.  * CDataObject::Purge
  100.  *
  101.  * Purpose:
  102.  *  Cleans out all entries in our listbox.
  103.  *
  104.  * Parameters:
  105.  *  None
  106.  *
  107.  * Return Value:
  108.  *  None
  109.  */
  110.  
  111. void CDataObject::Purge(void)
  112.     {
  113.     UINT        i, cItems;
  114.     PRENDERING  pRen;
  115.     DWORD       cb;
  116.  
  117.     if (NULL==m_hList)
  118.         return;
  119.  
  120.     cItems=(UINT)SendMessage(m_hList, LB_GETCOUNT, 0, 0L);
  121.  
  122.     for (i=0; i < cItems; i++)
  123.         {
  124.         cb=SendMessage(m_hList, LB_GETTEXT, i, (LPARAM)&pRen);
  125.  
  126.         if (LB_ERR!=cb)
  127.             {
  128.             /*
  129.              * Release the data completely being sure to reinstate
  130.              * the original pUnkForRelease.
  131.              */
  132.             pRen->stm.pUnkForRelease=pRen->pUnkOrg;
  133.             ReleaseStgMedium(&pRen->stm);
  134.             delete pRen;
  135.             }
  136.         }
  137.  
  138.     SendMessage(m_hList, LB_RESETCONTENT, 0, 0L);
  139.     return;
  140.     }
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147. /*
  148.  * CDataObject::QueryInterface
  149.  * CDataObject::AddRef
  150.  * CDataObject::Release
  151.  *
  152.  * Purpose:
  153.  *  IUnknown members for CDataObject object.
  154.  */
  155.  
  156. STDMETHODIMP CDataObject::QueryInterface(REFIID riid, PPVOID ppv)
  157.     {
  158.     *ppv=NULL;
  159.  
  160.     if (IID_IUnknown==riid)
  161.         *ppv=this;
  162.  
  163.     if (IID_IDataObject==riid)
  164.         *ppv=m_pIDataObject;
  165.  
  166.     if (NULL!=*ppv)
  167.         {
  168.         ((LPUNKNOWN)*ppv)->AddRef();
  169.         return NOERROR;
  170.         }
  171.  
  172.     return ResultFromScode(E_NOINTERFACE);
  173.     }
  174.  
  175.  
  176. STDMETHODIMP_(ULONG) CDataObject::AddRef(void)
  177.     {
  178.     return ++m_cRef;
  179.     }
  180.  
  181.  
  182. STDMETHODIMP_(ULONG) CDataObject::Release(void)
  183.     {
  184.     if (0!=--m_cRef)
  185.         return m_cRef;
  186.  
  187.     if (NULL!=m_pfnDestroy)
  188.         (*m_pfnDestroy)();
  189.  
  190.     delete this;
  191.     return 0;
  192.     }
  193.