1 /////////////////////////////////////////////////////////////////////////////// 
   2 // Name:        src/msw/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 licence 
  10 /////////////////////////////////////////////////////////////////////////////// 
  12 // ============================================================================ 
  14 // ============================================================================ 
  16 // ---------------------------------------------------------------------------- 
  18 // ---------------------------------------------------------------------------- 
  20 // For compilers that support precompilation, includes "wx.h". 
  21 #include "wx/wxprec.h" 
  23 #if defined(__BORLANDC__) 
  27 #if wxUSE_OLE && wxUSE_DRAG_AND_DROP 
  29 #include "wx/msw/private.h" 
  38     #if !defined(__GNUWIN32__) || wxUSE_NORLANDER_HEADERS 
  39         #if wxCHECK_W32API_VERSION( 1, 0 ) 
  40             #include "wx/msw/wrapwin.h" 
  42         #include <shlobj.h>            // for DROPFILES structure 
  50 #include "wx/msw/ole/oleutils.h" 
  52 // ---------------------------------------------------------------------------- 
  53 // IDropTarget interface: forward all interesting things to wxDropTarget 
  54 // (the name is unfortunate, but wx_I_DropTarget is not at all the same thing 
  55 //  as wxDropTarget which is 'public' class, while this one is private) 
  56 // ---------------------------------------------------------------------------- 
  58 class wxIDropTarget 
: public IDropTarget
 
  61     wxIDropTarget(wxDropTarget 
*p
); 
  62     virtual ~wxIDropTarget(); 
  64     // accessors for wxDropTarget 
  65     void SetHwnd(HWND hwnd
) { m_hwnd 
= hwnd
; } 
  67     // IDropTarget methods 
  68     STDMETHODIMP 
DragEnter(LPDATAOBJECT
, DWORD
, POINTL
, LPDWORD
); 
  69     STDMETHODIMP 
DragOver(DWORD
, POINTL
, LPDWORD
); 
  70     STDMETHODIMP 
DragLeave(); 
  71     STDMETHODIMP 
Drop(LPDATAOBJECT
, DWORD
, POINTL
, LPDWORD
); 
  73     DECLARE_IUNKNOWN_METHODS
; 
  76     IDataObject  
*m_pIDataObject
; // !NULL between DragEnter and DragLeave/Drop 
  77     wxDropTarget 
*m_pTarget
;      // the real target (we're just a proxy) 
  79     HWND          m_hwnd
;         // window we're associated with 
  81     // get default drop effect for given keyboard flags 
  82     static inline DWORD 
GetDropEffect(DWORD flags
, wxDragResult defaultAction
); 
  84     DECLARE_NO_COPY_CLASS(wxIDropTarget
) 
  87 // ---------------------------------------------------------------------------- 
  89 // ---------------------------------------------------------------------------- 
  91 static wxDragResult 
ConvertDragEffectToResult(DWORD dwEffect
); 
  92 static DWORD 
ConvertDragResultToEffect(wxDragResult result
); 
  94 // ============================================================================ 
  95 // wxIDropTarget implementation 
  96 // ============================================================================ 
  98 // Name    : static wxIDropTarget::GetDropEffect 
  99 // Purpose : determine the drop operation from keyboard/mouse state. 
 100 // Returns : DWORD combined from DROPEFFECT_xxx constants 
 101 // Params  : [in] DWORD flags       kbd & mouse flags as passed to 
 102 //                                  IDropTarget methods 
 103 // Notes   : We do "move" normally and "copy" if <Ctrl> is pressed, 
 104 //           which is the standard behaviour (currently there is no 
 105 //           way to redefine it) 
 106 DWORD 
wxIDropTarget::GetDropEffect(DWORD flags
, wxDragResult defaultAction
) 
 108   if (defaultAction 
== wxDragCopy
) 
 109     return flags 
& MK_SHIFT 
? DROPEFFECT_MOVE 
: DROPEFFECT_COPY
; 
 110   return flags 
& MK_CONTROL 
? DROPEFFECT_COPY 
: DROPEFFECT_MOVE
; 
 113 wxIDropTarget::wxIDropTarget(wxDropTarget 
*pTarget
) 
 116   m_pIDataObject 
= NULL
; 
 119 wxIDropTarget::~wxIDropTarget() 
 123 BEGIN_IID_TABLE(wxIDropTarget
) 
 128 IMPLEMENT_IUNKNOWN_METHODS(wxIDropTarget
) 
 130 // Name    : wxIDropTarget::DragEnter 
 131 // Purpose : Called when the mouse enters the window (dragging something) 
 133 // Params  : [in] IDataObject *pIDataSource : source data 
 134 //           [in] DWORD        grfKeyState  : kbd & mouse state 
 135 //           [in] POINTL       pt           : mouse coordinates 
 136 //           [out]DWORD       *pdwEffect    : effect flag 
 138 STDMETHODIMP 
wxIDropTarget::DragEnter(IDataObject 
*pIDataSource
, 
 143     wxLogTrace(wxTRACE_OleCalls
, wxT("IDropTarget::DragEnter")); 
 145     wxASSERT_MSG( m_pIDataObject 
== NULL
, 
 146                   _T("drop target must have data object") ); 
 148     // show the list of formats supported by the source data object for the 
 149     // debugging purposes, this is quite useful sometimes - please don't remove 
 151     IEnumFORMATETC 
*penumFmt
; 
 152     if ( SUCCEEDED(pIDataSource
->EnumFormatEtc(DATADIR_GET
, &penumFmt
)) ) 
 155         while ( penumFmt
->Next(1, &fmt
, NULL
) == S_OK 
) 
 157             wxLogDebug(_T("Drop source supports format %s"), 
 158                        wxDataObject::GetFormatName(fmt
.cfFormat
)); 
 165         wxLogLastError(_T("IDataObject::EnumFormatEtc")); 
 169     if ( !m_pTarget
->IsAcceptedData(pIDataSource
) ) { 
 170         // we don't accept this kind of data 
 171         *pdwEffect 
= DROPEFFECT_NONE
; 
 176     // get hold of the data object 
 177     m_pIDataObject 
= pIDataSource
; 
 178     m_pIDataObject
->AddRef(); 
 180     // we need client coordinates to pass to wxWin functions 
 181     if ( !ScreenToClient(m_hwnd
, (POINT 
*)&pt
) ) 
 183         wxLogLastError(wxT("ScreenToClient")); 
 186     // give some visual feedback 
 187     *pdwEffect 
= ConvertDragResultToEffect( 
 188         m_pTarget
->OnEnter(pt
.x
, pt
.y
, ConvertDragEffectToResult( 
 189             GetDropEffect(grfKeyState
, m_pTarget
->GetDefaultAction())) 
 196 // Name    : wxIDropTarget::DragOver 
 197 // Purpose : Indicates that the mouse was moved inside the window represented 
 198 //           by this drop target. 
 200 // Params  : [in] DWORD   grfKeyState     kbd & mouse state 
 201 //           [in] POINTL  pt              mouse coordinates 
 202 //           [out]LPDWORD pdwEffect       effect flag 
 203 // Notes   : We're called on every WM_MOUSEMOVE, so this function should be 
 205 STDMETHODIMP 
wxIDropTarget::DragOver(DWORD   grfKeyState
, 
 209     // there are too many of them... wxLogDebug("IDropTarget::DragOver"); 
 212     if ( m_pIDataObject 
) { 
 213         result 
= ConvertDragEffectToResult( 
 214             GetDropEffect(grfKeyState
, m_pTarget
->GetDefaultAction())); 
 217         // can't accept data anyhow normally 
 221     // we need client coordinates to pass to wxWin functions 
 222     if ( !ScreenToClient(m_hwnd
, (POINT 
*)&pt
) ) 
 224         wxLogLastError(wxT("ScreenToClient")); 
 227     *pdwEffect 
= ConvertDragResultToEffect( 
 228                     m_pTarget
->OnDragOver(pt
.x
, pt
.y
, result
) 
 234 // Name    : wxIDropTarget::DragLeave 
 235 // Purpose : Informs the drop target that the operation has left its window. 
 237 // Notes   : good place to do any clean-up 
 238 STDMETHODIMP 
wxIDropTarget::DragLeave() 
 240   wxLogTrace(wxTRACE_OleCalls
, wxT("IDropTarget::DragLeave")); 
 242   // remove the UI feedback 
 243   m_pTarget
->OnLeave(); 
 245   // release the held object 
 246   RELEASE_AND_NULL(m_pIDataObject
); 
 251 // Name    : wxIDropTarget::Drop 
 252 // Purpose : Instructs the drop target to paste data that was just now 
 255 // Params  : [in] IDataObject *pIDataSource     the data to paste 
 256 //           [in] DWORD        grfKeyState      kbd & mouse state 
 257 //           [in] POINTL       pt               where the drop occurred? 
 258 //           [ouy]DWORD       *pdwEffect        operation effect 
 260 STDMETHODIMP 
wxIDropTarget::Drop(IDataObject 
*pIDataSource
, 
 265     wxLogTrace(wxTRACE_OleCalls
, wxT("IDropTarget::Drop")); 
 267     // TODO I don't know why there is this parameter, but so far I assume 
 268     //      that it's the same we've already got in DragEnter 
 269     wxASSERT( m_pIDataObject 
== pIDataSource 
); 
 271     // by default, nothing happens 
 272     *pdwEffect 
= DROPEFFECT_NONE
; 
 274     // we need client coordinates to pass to wxWin functions 
 275     if ( !ScreenToClient(m_hwnd
, (POINT 
*)&pt
) ) 
 277         wxLogLastError(wxT("ScreenToClient")); 
 280     // first ask the drop target if it wants data 
 281     if ( m_pTarget
->OnDrop(pt
.x
, pt
.y
) ) { 
 282         // it does, so give it the data source 
 283         m_pTarget
->SetDataSource(pIDataSource
); 
 285         // and now it has the data 
 286         wxDragResult rc 
= ConvertDragEffectToResult( 
 287             GetDropEffect(grfKeyState
, m_pTarget
->GetDefaultAction())); 
 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
) 
 332     // RegisterDragDrop not available on Windows CE >= 400? 
 333     // Or maybe we can dynamically load them from ceshell.dll 
 335 #if defined(__WXWINCE__) && _WIN32_WCE >= 400 
 341     // May exist in later WinCE versions 
 343     hr 
= ::CoLockObjectExternal(m_pIDropTarget
, TRUE
, FALSE
); 
 345         wxLogApiError(wxT("CoLockObjectExternal"), hr
); 
 350     hr 
= ::RegisterDragDrop((HWND
) hwnd
, m_pIDropTarget
); 
 352     // May exist in later WinCE versions 
 354         ::CoLockObjectExternal(m_pIDropTarget
, FALSE
, FALSE
); 
 356         wxLogApiError(wxT("RegisterDragDrop"), hr
); 
 360     // we will need the window handle for coords transformation later 
 361     m_pIDropTarget
->SetHwnd((HWND
)hwnd
); 
 367 void wxDropTarget::Revoke(WXHWND hwnd
) 
 369 #if defined(__WXWINCE__) && _WIN32_WCE >= 400 
 370     // Not available, see note above 
 373     HRESULT hr 
= ::RevokeDragDrop((HWND
) hwnd
); 
 376         wxLogApiError(wxT("RevokeDragDrop"), hr
); 
 379     // May exist in later WinCE versions 
 381     ::CoLockObjectExternal(m_pIDropTarget
, FALSE
, TRUE
); 
 384     m_pIDropTarget
->SetHwnd(0); 
 388 // ---------------------------------------------------------------------------- 
 389 // base class pure virtuals 
 390 // ---------------------------------------------------------------------------- 
 392 // OnDrop() is called only if we previously returned true from 
 393 // IsAcceptedData(), so no need to check anything here 
 394 bool wxDropTarget::OnDrop(wxCoord 
WXUNUSED(x
), wxCoord 
WXUNUSED(y
)) 
 399 // copy the data from the data source to the target data object 
 400 bool wxDropTarget::GetData() 
 402     wxDataFormat format 
= GetSupportedFormat(m_pIDataSource
); 
 403     if ( format 
== wxDF_INVALID 
) { 
 404         // this is strange because IsAcceptedData() succeeded previously! 
 405         wxFAIL_MSG(wxT("strange - did supported formats list change?")); 
 412     fmtMemory
.cfFormat  
= format
; 
 413     fmtMemory
.ptd       
= NULL
; 
 414     fmtMemory
.dwAspect  
= DVASPECT_CONTENT
; 
 415     fmtMemory
.lindex    
= -1; 
 416     fmtMemory
.tymed     
= TYMED_HGLOBAL
;  // TODO to add other media 
 420     HRESULT hr 
= m_pIDataSource
->GetData(&fmtMemory
, &stm
); 
 421     if ( SUCCEEDED(hr
) ) { 
 422         IDataObject 
*dataObject 
= m_dataObject
->GetInterface(); 
 424         hr 
= dataObject
->SetData(&fmtMemory
, &stm
, TRUE
); 
 425         if ( SUCCEEDED(hr
) ) { 
 429             wxLogApiError(wxT("IDataObject::SetData()"), hr
); 
 433         wxLogApiError(wxT("IDataObject::GetData()"), hr
); 
 439 // ---------------------------------------------------------------------------- 
 440 // callbacks used by wxIDropTarget 
 441 // ---------------------------------------------------------------------------- 
 443 // we need a data source, so wxIDropTarget gives it to us using this function 
 444 void wxDropTarget::SetDataSource(IDataObject 
*pIDataSource
) 
 446     m_pIDataSource 
= pIDataSource
; 
 449 // determine if we accept data of this type 
 450 bool wxDropTarget::IsAcceptedData(IDataObject 
*pIDataSource
) const 
 452     return GetSupportedFormat(pIDataSource
) != wxDF_INVALID
; 
 455 // ---------------------------------------------------------------------------- 
 457 // ---------------------------------------------------------------------------- 
 459 wxDataFormat 
wxDropTarget::GetSupportedFormat(IDataObject 
*pIDataSource
) const 
 461     // this strucutre describes a data of any type (first field will be 
 462     // changing) being passed through global memory block. 
 463     static FORMATETC s_fmtMemory 
= { 
 468         TYMED_HGLOBAL       
// TODO is it worth supporting other tymeds here? 
 471     // get the list of supported formats 
 472     size_t nFormats 
= m_dataObject
->GetFormatCount(wxDataObject::Set
); 
 474     wxDataFormat 
*formats
; 
 475     formats 
= nFormats 
== 1 ? &format 
:  new wxDataFormat
[nFormats
]; 
 477     m_dataObject
->GetAllFormats(formats
, wxDataObject::Set
); 
 479     // cycle through all supported formats 
 481     for ( n 
= 0; n 
< nFormats
; n
++ ) { 
 482         s_fmtMemory
.cfFormat 
= formats
[n
]; 
 484         // NB: don't use SUCCEEDED macro here: QueryGetData returns S_FALSE 
 485         //     for file drag and drop (format == CF_HDROP) 
 486         if ( pIDataSource
->QueryGetData(&s_fmtMemory
) == S_OK 
) { 
 493     if ( formats 
!= &format 
) { 
 494         // free memory if we allocated it 
 498     return n 
< nFormats 
? format 
: wxFormatInvalid
; 
 501 // ---------------------------------------------------------------------------- 
 503 // ---------------------------------------------------------------------------- 
 505 static wxDragResult 
ConvertDragEffectToResult(DWORD dwEffect
) 
 507     switch ( dwEffect 
) { 
 508         case DROPEFFECT_COPY
: 
 511         case DROPEFFECT_LINK
: 
 514         case DROPEFFECT_MOVE
: 
 518             wxFAIL_MSG(wxT("invalid value in ConvertDragEffectToResult")); 
 521         case DROPEFFECT_NONE
: 
 526 static DWORD 
ConvertDragResultToEffect(wxDragResult result
) 
 530             return DROPEFFECT_COPY
; 
 533             return DROPEFFECT_LINK
; 
 536             return DROPEFFECT_MOVE
; 
 539             wxFAIL_MSG(wxT("invalid value in ConvertDragResultToEffect")); 
 543             return DROPEFFECT_NONE
; 
 548  // wxUSE_DRAG_AND_DROP