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 / chap13 / cocosmo / droptgt.cpp < prev    next >
C/C++ Source or Header  |  1995-05-03  |  5KB  |  252 lines

  1. /*
  2.  * DROPTGT.CPP
  3.  * Component Cosmo Chapter 13
  4.  *
  5.  * Implementation of the DropTarget object.
  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 "cocosmo.h"
  16.  
  17.  
  18.  
  19. /*
  20.  * CDropTarget::CDropTarget
  21.  * CDropTarget::~CDropTarget
  22.  *
  23.  * Constructor Parameters:
  24.  *  pDoc            PCCosmoDoc of the document containing us.
  25.  */
  26.  
  27. CDropTarget::CDropTarget(PCCosmoDoc pDoc)
  28.     {
  29.     m_cRef=0;
  30.     m_pDoc=pDoc;
  31.  
  32.     m_pIDataObject=NULL;
  33.     return;
  34.     }
  35.  
  36.  
  37. CDropTarget::~CDropTarget(void)
  38.     {
  39.     return;
  40.     }
  41.  
  42.  
  43.  
  44.  
  45. /*
  46.  * CDropTarget::QueryInterface
  47.  * CDropTarget::AddRef
  48.  * CDropTarget::Release
  49.  *
  50.  * Purpose:
  51.  *  IUnknown members for CDropTarget object.
  52.  */
  53.  
  54. STDMETHODIMP CDropTarget::QueryInterface(REFIID riid, PPVOID ppv)
  55.     {
  56.     *ppv=NULL;
  57.  
  58.     if (IID_IUnknown==riid || IID_IDropTarget==riid)
  59.         *ppv=this;
  60.  
  61.     if (NULL!=*ppv)
  62.         {
  63.         ((LPUNKNOWN)*ppv)->AddRef();
  64.         return NOERROR;
  65.         }
  66.  
  67.     return ResultFromScode(E_NOINTERFACE);
  68.     }
  69.  
  70.  
  71. STDMETHODIMP_(ULONG) CDropTarget::AddRef(void)
  72.     {
  73.     return ++m_cRef;
  74.     }
  75.  
  76. STDMETHODIMP_(ULONG) CDropTarget::Release(void)
  77.     {
  78.      if (0!=--m_cRef)
  79.         return m_cRef;
  80.  
  81.     delete this;
  82.     return 0;
  83.     }
  84.  
  85.  
  86.  
  87.  
  88.  
  89. /*
  90.  * CDropTarget::DragEnter
  91.  *
  92.  * Purpose:
  93.  *  Indicates that data in a drag operation has been dragged over
  94.  *  our window that's a potential target.  We are to decide if it's
  95.  *  something in which we're interested.
  96.  *
  97.  * Parameters:
  98.  *  pIDataSource    LPDATAOBJECT providing the source data.
  99.  *  grfKeyState     DWORD flags: states of keys and mouse buttons.
  100.  *  pt              POINTL coordinates in the client space of
  101.  *                  the document.
  102.  *  pdwEffect       LPDWORD into which we'll place the appropriate
  103.  *                  effect flag for this point.
  104.  *
  105.  * Return Value:
  106.  *  HRESULT         NOERROR
  107.  */
  108.  
  109. STDMETHODIMP CDropTarget::DragEnter(LPDATAOBJECT pIDataSource
  110.     , DWORD grfKeyState, POINTL pt, LPDWORD pdwEffect)
  111.     {
  112.     HWND        hWnd;
  113.  
  114.     m_pIDataObject=NULL;
  115.  
  116.     if (!m_pDoc->FQueryPasteFromData(pIDataSource))
  117.         {
  118.         *pdwEffect=DROPEFFECT_NONE;
  119.         return NOERROR;
  120.         }
  121.  
  122.     *pdwEffect=DROPEFFECT_MOVE;
  123.  
  124.     if (grfKeyState & MK_CONTROL)
  125.         *pdwEffect=DROPEFFECT_COPY;
  126.  
  127.     m_pIDataObject=pIDataSource;
  128.     m_pIDataObject->AddRef();
  129.  
  130.     hWnd=m_pDoc->Window();
  131.     BringWindowToTop(hWnd);
  132.     UpdateWindow(hWnd);
  133.     m_pDoc->DropSelectTargetWindow();
  134.  
  135.     return NOERROR;
  136.     }
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143. /*
  144.  * CDropTarget::DragOver
  145.  *
  146.  * Purpose:
  147.  *  Indicates that the mouse was moved inside the window represented
  148.  *  by this drop target.  This happens on every WM_MOUSEMOVE, so
  149.  *  this function should be very efficient.
  150.  *
  151.  * Parameters:
  152.  *  grfKeyState     DWORD providing the current keyboard and
  153.  *                  mouse states
  154.  *  pt              POINTL where the mouse currently is.
  155.  *  pdwEffect       LPDWORD in which to store the effect flag
  156.  *                  for this point.
  157.  *
  158.  * Return Value:
  159.  *  HRESULT         NOERROR
  160.  */
  161.  
  162. STDMETHODIMP CDropTarget::DragOver(DWORD grfKeyState, POINTL pt
  163.     , LPDWORD pdwEffect)
  164.     {
  165.     if (NULL==m_pIDataObject)
  166.         {
  167.         *pdwEffect=DROPEFFECT_NONE;
  168.         return NOERROR;
  169.         }
  170.  
  171.     *pdwEffect=DROPEFFECT_MOVE;
  172.  
  173.     if (grfKeyState & MK_CONTROL)
  174.         *pdwEffect=DROPEFFECT_COPY;
  175.  
  176.     return NOERROR;
  177.     }
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184. /*
  185.  * CDropTarget::DragLeave
  186.  *
  187.  * Purpose:
  188.  *  Informs the drop target that the operation has left its window.
  189.  *
  190.  * Parameters:
  191.  *  None
  192.  *
  193.  * Return Value:
  194.  *  HRESULT         NOERROR
  195.  */
  196.  
  197. STDMETHODIMP CDropTarget::DragLeave(void)
  198.     {
  199.     m_pDoc->DropSelectTargetWindow();
  200.     ReleaseInterface(m_pIDataObject);
  201.     return NOERROR;
  202.     }
  203.  
  204.  
  205.  
  206.  
  207.  
  208. /*
  209.  * CDropTarget::Drop
  210.  *
  211.  * Purpose:
  212.  *  Instructs the drop target to paste the data that was just now
  213.  *  dropped on it.
  214.  *
  215.  * Parameters:
  216.  *  pIDataSource    LPDATAOBJECT from which we'll paste.
  217.  *  grfKeyState     DWORD providing current keyboard/mouse state.
  218.  *  pt              POINTL at which the drop occurred.
  219.  *  pdwEffect       LPDWORD in which to store what you did.
  220.  *
  221.  * Return Value:
  222.  *  HRESULT         NOERROR
  223.  */
  224.  
  225. STDMETHODIMP CDropTarget::Drop(LPDATAOBJECT pIDataSource
  226.     , DWORD grfKeyState, POINTL pt, LPDWORD pdwEffect)
  227.     {
  228.     BOOL        fRet=TRUE;
  229.  
  230.     *pdwEffect=DROPEFFECT_NONE;
  231.  
  232.     if (NULL==m_pIDataObject)
  233.         return ResultFromScode(E_FAIL);
  234.  
  235.     DragLeave();
  236.  
  237.     if (m_pDoc->m_fDragSource)
  238.         return ResultFromScode(E_FAIL);
  239.  
  240.     fRet=m_pDoc->PasteFromData(pIDataSource);
  241.  
  242.     if (!fRet)
  243.         return ResultFromScode(E_FAIL);
  244.  
  245.     *pdwEffect=DROPEFFECT_MOVE;
  246.  
  247.     if (grfKeyState & MK_CONTROL)
  248.         *pdwEffect=DROPEFFECT_COPY;
  249.  
  250.     return NOERROR;
  251.     }
  252.