]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/ole/droptgt.cpp
   1 /////////////////////////////////////////////////////////////////////////////// 
   2 // Name:        ole/droptgt.cpp 
   3 // Purpose:     wxDropTarget implementation 
   4 // Author:      Vadim Zeitlin 
   8 // Copyright:   (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> 
   9 // Licence:     wxWindows license 
  10 /////////////////////////////////////////////////////////////////////////////// 
  12 // ============================================================================ 
  14 // ============================================================================ 
  16 // ---------------------------------------------------------------------------- 
  18 // ---------------------------------------------------------------------------- 
  21 #pragma implementation "droptgt.h" 
  24 // For compilers that support precompilation, includes "wx.h". 
  25 #include "wx/wxprec.h" 
  27 #if defined(__BORLANDC__) 
  33 #if wxUSE_OLE && wxUSE_DRAG_AND_DROP 
  38     #if !defined(__GNUWIN32__) || wxUSE_NORLANDER_HEADERS 
  39         #if wxCHECK_W32API_VERSION( 1, 0 ) 
  42         #include <shlobj.h>            // for DROPFILES structure 
  55 #include "wx/msw/ole/oleutils.h" 
  57 // ---------------------------------------------------------------------------- 
  58 // IDropTarget interface: forward all interesting things to wxDropTarget 
  59 // (the name is unfortunate, but wx_I_DropTarget is not at all the same thing 
  60 //  as wxDropTarget which is 'public' class, while this one is private) 
  61 // ---------------------------------------------------------------------------- 
  63 class wxIDropTarget 
: public IDropTarget
 
  66     wxIDropTarget(wxDropTarget 
*p
); 
  67     virtual ~wxIDropTarget(); 
  69     // accessors for wxDropTarget 
  70     void SetHwnd(HWND hwnd
) { m_hwnd 
= hwnd
; } 
  72     // IDropTarget methods 
  73     STDMETHODIMP 
DragEnter(LPDATAOBJECT
, DWORD
, POINTL
, LPDWORD
); 
  74     STDMETHODIMP 
DragOver(DWORD
, POINTL
, LPDWORD
); 
  75     STDMETHODIMP 
DragLeave(); 
  76     STDMETHODIMP 
Drop(LPDATAOBJECT
, DWORD
, POINTL
, LPDWORD
); 
  78     DECLARE_IUNKNOWN_METHODS
; 
  81     IDataObject  
*m_pIDataObject
; // !NULL between DragEnter and DragLeave/Drop 
  82     wxDropTarget 
*m_pTarget
;      // the real target (we're just a proxy) 
  84     HWND          m_hwnd
;         // window we're associated with 
  86     // get default drop effect for given keyboard flags 
  87     static inline DWORD 
GetDropEffect(DWORD flags
); 
  90 // ---------------------------------------------------------------------------- 
  92 // ---------------------------------------------------------------------------- 
  94 static wxDragResult 
ConvertDragEffectToResult(DWORD dwEffect
); 
  95 static DWORD 
ConvertDragResultToEffect(wxDragResult result
); 
  97 // ============================================================================ 
  98 // wxIDropTarget implementation 
  99 // ============================================================================ 
 101 // Name    : static wxIDropTarget::GetDropEffect 
 102 // Purpose : determine the drop operation from keyboard/mouse state. 
 103 // Returns : DWORD combined from DROPEFFECT_xxx constants 
 104 // Params  : [in] DWORD flags       kbd & mouse flags as passed to 
 105 //                                  IDropTarget methods 
 106 // Notes   : We do "move" normally and "copy" if <Ctrl> is pressed, 
 107 //           which is the standard behaviour (currently there is no 
 108 //           way to redefine it) 
 109 DWORD 
wxIDropTarget::GetDropEffect(DWORD flags
) 
 111   return flags 
& MK_CONTROL 
? DROPEFFECT_COPY 
: DROPEFFECT_MOVE
; 
 114 wxIDropTarget::wxIDropTarget(wxDropTarget 
*pTarget
) 
 118   m_pIDataObject 
= NULL
; 
 121 wxIDropTarget::~wxIDropTarget() 
 125 BEGIN_IID_TABLE(wxIDropTarget
) 
 130 IMPLEMENT_IUNKNOWN_METHODS(wxIDropTarget
) 
 132 // Name    : wxIDropTarget::DragEnter 
 133 // Purpose : Called when the mouse enters the window (dragging something) 
 135 // Params  : [in] IDataObject *pIDataSource : source data 
 136 //           [in] DWORD        grfKeyState  : kbd & mouse state 
 137 //           [in] POINTL       pt           : mouse coordinates 
 138 //           [out]DWORD       *pdwEffect    : effect flag 
 140 STDMETHODIMP 
wxIDropTarget::DragEnter(IDataObject 
*pIDataSource
, 
 145     wxLogTrace(wxTRACE_OleCalls
, wxT("IDropTarget::DragEnter")); 
 147     wxASSERT_MSG( m_pIDataObject 
== NULL
, 
 148                   _T("drop target must have data object") ); 
 150     // show the list of formats supported by the source data object for the 
 151     // debugging purposes, this is quite useful sometimes - please don't remove 
 153     IEnumFORMATETC 
*penumFmt
; 
 154     if ( SUCCEEDED(pIDataSource
->EnumFormatEtc(DATADIR_GET
, &penumFmt
)) ) 
 157         while ( penumFmt
->Next(1, &fmt
, NULL
) == S_OK 
) 
 159             wxLogDebug(_T("Drop source supports format %s"), 
 160                        wxDataObject::GetFormatName(fmt
.cfFormat
)); 
 167         wxLogLastError(_T("IDataObject::EnumFormatEtc")); 
 171     if ( !m_pTarget
->IsAcceptedData(pIDataSource
) ) { 
 172         // we don't accept this kind of data 
 173         *pdwEffect 
= DROPEFFECT_NONE
; 
 178     // get hold of the data object 
 179     m_pIDataObject 
= pIDataSource
; 
 180     m_pIDataObject
->AddRef(); 
 182     // we need client coordinates to pass to wxWin functions 
 183     if ( !ScreenToClient(m_hwnd
, (POINT 
*)&pt
) ) 
 185         wxLogLastError(wxT("ScreenToClient")); 
 188     // give some visual feedback 
 189     *pdwEffect 
= ConvertDragResultToEffect( 
 190                     m_pTarget
->OnEnter(pt
.x
, pt
.y
, 
 191                         ConvertDragEffectToResult(GetDropEffect(grfKeyState
)) 
 198 // Name    : wxIDropTarget::DragOver 
 199 // Purpose : Indicates that the mouse was moved inside the window represented 
 200 //           by this drop target. 
 202 // Params  : [in] DWORD   grfKeyState     kbd & mouse state 
 203 //           [in] POINTL  pt              mouse coordinates 
 204 //           [out]LPDWORD pdwEffect       effect flag 
 205 // Notes   : We're called on every WM_MOUSEMOVE, so this function should be 
 207 STDMETHODIMP 
wxIDropTarget::DragOver(DWORD   grfKeyState
, 
 211     // there are too many of them... wxLogDebug("IDropTarget::DragOver"); 
 214     if ( m_pIDataObject 
) { 
 215         result 
= ConvertDragEffectToResult(GetDropEffect(grfKeyState
)); 
 218         // can't accept data anyhow normally 
 222     // we need client coordinates to pass to wxWin functions 
 223     if ( !ScreenToClient(m_hwnd
, (POINT 
*)&pt
) ) 
 225         wxLogLastError(wxT("ScreenToClient")); 
 228     *pdwEffect 
= ConvertDragResultToEffect( 
 229                     m_pTarget
->OnDragOver(pt
.x
, pt
.y
, result
) 
 235 // Name    : wxIDropTarget::DragLeave 
 236 // Purpose : Informs the drop target that the operation has left its window. 
 238 // Notes   : good place to do any clean-up 
 239 STDMETHODIMP 
wxIDropTarget::DragLeave() 
 241   wxLogTrace(wxTRACE_OleCalls
, wxT("IDropTarget::DragLeave")); 
 243   // remove the UI feedback 
 244   m_pTarget
->OnLeave(); 
 246   // release the held object 
 247   RELEASE_AND_NULL(m_pIDataObject
); 
 252 // Name    : wxIDropTarget::Drop 
 253 // Purpose : Instructs the drop target to paste data that was just now 
 256 // Params  : [in] IDataObject *pIDataSource     the data to paste 
 257 //           [in] DWORD        grfKeyState      kbd & mouse state 
 258 //           [in] POINTL       pt               where the drop occured? 
 259 //           [ouy]DWORD       *pdwEffect        operation effect 
 261 STDMETHODIMP 
wxIDropTarget::Drop(IDataObject 
*pIDataSource
, 
 266     wxLogTrace(wxTRACE_OleCalls
, wxT("IDropTarget::Drop")); 
 268     // TODO I don't know why there is this parameter, but so far I assume 
 269     //      that it's the same we've already got in DragEnter 
 270     wxASSERT( m_pIDataObject 
== pIDataSource 
); 
 272     // by default, nothing happens 
 273     *pdwEffect 
= DROPEFFECT_NONE
; 
 275     // we need client coordinates to pass to wxWin functions 
 276     if ( !ScreenToClient(m_hwnd
, (POINT 
*)&pt
) ) 
 278         wxLogLastError(wxT("ScreenToClient")); 
 281     // first ask the drop target if it wants data 
 282     if ( m_pTarget
->OnDrop(pt
.x
, pt
.y
) ) { 
 283         // it does, so give it the data source 
 284         m_pTarget
->SetDataSource(pIDataSource
); 
 286         // and now it has the data 
 287         wxDragResult rc 
= ConvertDragEffectToResult(GetDropEffect(grfKeyState
)); 
 288         rc 
= m_pTarget
->OnData(pt
.x
, pt
.y
, rc
); 
 289         if ( wxIsDragResultOk(rc
) ) { 
 290             // operation succeeded 
 291             *pdwEffect 
= ConvertDragResultToEffect(rc
); 
 293         //else: *pdwEffect is already DROPEFFECT_NONE 
 295     //else: OnDrop() returned FALSE, no need to copy data 
 297     // release the held object 
 298     RELEASE_AND_NULL(m_pIDataObject
); 
 303 // ============================================================================ 
 304 // wxDropTarget implementation 
 305 // ============================================================================ 
 307 // ---------------------------------------------------------------------------- 
 309 // ---------------------------------------------------------------------------- 
 311 wxDropTarget::wxDropTarget(wxDataObject 
*dataObj
) 
 312             : wxDropTargetBase(dataObj
) 
 314     // create an IDropTarget implementation which will notify us about d&d 
 316     m_pIDropTarget 
= new wxIDropTarget(this); 
 317     m_pIDropTarget
->AddRef(); 
 320 wxDropTarget::~wxDropTarget() 
 322     ReleaseInterface(m_pIDropTarget
); 
 325 // ---------------------------------------------------------------------------- 
 326 // [un]register drop handler 
 327 // ---------------------------------------------------------------------------- 
 329 bool wxDropTarget::Register(WXHWND hwnd
) 
 331     HRESULT hr 
= ::CoLockObjectExternal(m_pIDropTarget
, TRUE
, FALSE
); 
 333         wxLogApiError(wxT("CoLockObjectExternal"), hr
); 
 337     hr 
= ::RegisterDragDrop((HWND
) hwnd
, m_pIDropTarget
); 
 339         ::CoLockObjectExternal(m_pIDropTarget
, FALSE
, FALSE
); 
 341         wxLogApiError(wxT("RegisterDragDrop"), hr
); 
 345     // we will need the window handle for coords transformation later 
 346     m_pIDropTarget
->SetHwnd((HWND
)hwnd
); 
 351 void wxDropTarget::Revoke(WXHWND hwnd
) 
 353     HRESULT hr 
= ::RevokeDragDrop((HWND
) hwnd
); 
 356         wxLogApiError(wxT("RevokeDragDrop"), hr
); 
 359     ::CoLockObjectExternal(m_pIDropTarget
, FALSE
, TRUE
); 
 361     m_pIDropTarget
->SetHwnd(0); 
 364 // ---------------------------------------------------------------------------- 
 365 // base class pure virtuals 
 366 // ---------------------------------------------------------------------------- 
 368 // OnDrop() is called only if we previously returned TRUE from 
 369 // IsAcceptedData(), so no need to check anything here 
 370 bool wxDropTarget::OnDrop(wxCoord 
WXUNUSED(x
), wxCoord 
WXUNUSED(y
)) 
 375 // copy the data from the data source to the target data object 
 376 bool wxDropTarget::GetData() 
 378     wxDataFormat format 
= GetSupportedFormat(m_pIDataSource
); 
 379     if ( format 
== wxDF_INVALID 
) { 
 380         // this is strange because IsAcceptedData() succeeded previously! 
 381         wxFAIL_MSG(wxT("strange - did supported formats list change?")); 
 388     fmtMemory
.cfFormat  
= format
; 
 389     fmtMemory
.ptd       
= NULL
; 
 390     fmtMemory
.dwAspect  
= DVASPECT_CONTENT
; 
 391     fmtMemory
.lindex    
= -1; 
 392     fmtMemory
.tymed     
= TYMED_HGLOBAL
;  // TODO to add other media 
 396     HRESULT hr 
= m_pIDataSource
->GetData(&fmtMemory
, &stm
); 
 397     if ( SUCCEEDED(hr
) ) { 
 398         IDataObject 
*dataObject 
= m_dataObject
->GetInterface(); 
 400         hr 
= dataObject
->SetData(&fmtMemory
, &stm
, TRUE
); 
 401         if ( SUCCEEDED(hr
) ) { 
 405             wxLogApiError(wxT("IDataObject::SetData()"), hr
); 
 409         wxLogApiError(wxT("IDataObject::GetData()"), hr
); 
 415 // ---------------------------------------------------------------------------- 
 416 // callbacks used by wxIDropTarget 
 417 // ---------------------------------------------------------------------------- 
 419 // we need a data source, so wxIDropTarget gives it to us using this function 
 420 void wxDropTarget::SetDataSource(IDataObject 
*pIDataSource
) 
 422     m_pIDataSource 
= pIDataSource
; 
 425 // determine if we accept data of this type 
 426 bool wxDropTarget::IsAcceptedData(IDataObject 
*pIDataSource
) const 
 428     return GetSupportedFormat(pIDataSource
) != wxDF_INVALID
; 
 431 // ---------------------------------------------------------------------------- 
 433 // ---------------------------------------------------------------------------- 
 435 wxDataFormat 
wxDropTarget::GetSupportedFormat(IDataObject 
*pIDataSource
) const 
 437     // this strucutre describes a data of any type (first field will be 
 438     // changing) being passed through global memory block. 
 439     static FORMATETC s_fmtMemory 
= { 
 444         TYMED_HGLOBAL       
// TODO is it worth supporting other tymeds here? 
 447     // get the list of supported formats 
 448     size_t nFormats 
= m_dataObject
->GetFormatCount(wxDataObject::Set
); 
 450     wxDataFormat 
*formats
; 
 451     formats 
= nFormats 
== 1 ? &format 
:  new wxDataFormat
[nFormats
]; 
 453     m_dataObject
->GetAllFormats(formats
, wxDataObject::Set
); 
 455     // cycle through all supported formats 
 457     for ( n 
= 0; n 
< nFormats
; n
++ ) { 
 458         s_fmtMemory
.cfFormat 
= formats
[n
]; 
 460         // NB: don't use SUCCEEDED macro here: QueryGetData returns S_FALSE 
 461         //     for file drag and drop (format == CF_HDROP) 
 462         if ( pIDataSource
->QueryGetData(&s_fmtMemory
) == S_OK 
) { 
 469     if ( formats 
!= &format 
) { 
 470         // free memory if we allocated it 
 474     return n 
< nFormats 
? format 
: wxFormatInvalid
; 
 477 // ---------------------------------------------------------------------------- 
 479 // ---------------------------------------------------------------------------- 
 481 static wxDragResult 
ConvertDragEffectToResult(DWORD dwEffect
) 
 483     switch ( dwEffect 
) { 
 484         case DROPEFFECT_COPY
: 
 487         case DROPEFFECT_LINK
: 
 490         case DROPEFFECT_MOVE
: 
 494             wxFAIL_MSG(wxT("invalid value in ConvertDragEffectToResult")); 
 497         case DROPEFFECT_NONE
: 
 502 static DWORD 
ConvertDragResultToEffect(wxDragResult result
) 
 506             return DROPEFFECT_COPY
; 
 509             return DROPEFFECT_LINK
; 
 512             return DROPEFFECT_MOVE
; 
 515             wxFAIL_MSG(wxT("invalid value in ConvertDragResultToEffect")); 
 519             return DROPEFFECT_NONE
; 
 524  // wxUSE_DRAG_AND_DROP