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 
  33 #include "wx/msw/private.h" 
  41     #if !defined(__GNUWIN32__) || wxUSE_NORLANDER_HEADERS 
  42         #if wxCHECK_W32API_VERSION( 1, 0 ) 
  43             #include "wx/msw/wrapwin.h" 
  45         #include <shlobj.h>            // for DROPFILES structure 
  53 #include "wx/msw/ole/oleutils.h" 
  55 // ---------------------------------------------------------------------------- 
  56 // IDropTarget interface: forward all interesting things to wxDropTarget 
  57 // (the name is unfortunate, but wx_I_DropTarget is not at all the same thing 
  58 //  as wxDropTarget which is 'public' class, while this one is private) 
  59 // ---------------------------------------------------------------------------- 
  61 class wxIDropTarget 
: public IDropTarget
 
  64     wxIDropTarget(wxDropTarget 
*p
); 
  65     virtual ~wxIDropTarget(); 
  67     // accessors for wxDropTarget 
  68     void SetHwnd(HWND hwnd
) { m_hwnd 
= hwnd
; } 
  70     // IDropTarget methods 
  71     STDMETHODIMP 
DragEnter(LPDATAOBJECT
, DWORD
, POINTL
, LPDWORD
); 
  72     STDMETHODIMP 
DragOver(DWORD
, POINTL
, LPDWORD
); 
  73     STDMETHODIMP 
DragLeave(); 
  74     STDMETHODIMP 
Drop(LPDATAOBJECT
, DWORD
, POINTL
, LPDWORD
); 
  76     DECLARE_IUNKNOWN_METHODS
; 
  79     IDataObject  
*m_pIDataObject
; // !NULL between DragEnter and DragLeave/Drop 
  80     wxDropTarget 
*m_pTarget
;      // the real target (we're just a proxy) 
  82     HWND          m_hwnd
;         // window we're associated with 
  84     // get default drop effect for given keyboard flags 
  85     static inline DWORD 
GetDropEffect(DWORD flags
, wxDragResult defaultAction
); 
  87     DECLARE_NO_COPY_CLASS(wxIDropTarget
) 
  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
, wxDragResult defaultAction
) 
 111   if (defaultAction 
== wxDragCopy
) 
 112     return flags 
& MK_SHIFT 
? DROPEFFECT_MOVE 
: DROPEFFECT_COPY
; 
 113   return flags 
& MK_CONTROL 
? DROPEFFECT_COPY 
: DROPEFFECT_MOVE
; 
 116 wxIDropTarget::wxIDropTarget(wxDropTarget 
*pTarget
) 
 119   m_pIDataObject 
= NULL
; 
 122 wxIDropTarget::~wxIDropTarget() 
 126 BEGIN_IID_TABLE(wxIDropTarget
) 
 131 IMPLEMENT_IUNKNOWN_METHODS(wxIDropTarget
) 
 133 // Name    : wxIDropTarget::DragEnter 
 134 // Purpose : Called when the mouse enters the window (dragging something) 
 136 // Params  : [in] IDataObject *pIDataSource : source data 
 137 //           [in] DWORD        grfKeyState  : kbd & mouse state 
 138 //           [in] POINTL       pt           : mouse coordinates 
 139 //           [out]DWORD       *pdwEffect    : effect flag 
 141 STDMETHODIMP 
wxIDropTarget::DragEnter(IDataObject 
*pIDataSource
, 
 146     wxLogTrace(wxTRACE_OleCalls
, wxT("IDropTarget::DragEnter")); 
 148     wxASSERT_MSG( m_pIDataObject 
== NULL
, 
 149                   _T("drop target must have data object") ); 
 151     // show the list of formats supported by the source data object for the 
 152     // debugging purposes, this is quite useful sometimes - please don't remove 
 154     IEnumFORMATETC 
*penumFmt
; 
 155     if ( SUCCEEDED(pIDataSource
->EnumFormatEtc(DATADIR_GET
, &penumFmt
)) ) 
 158         while ( penumFmt
->Next(1, &fmt
, NULL
) == S_OK 
) 
 160             wxLogDebug(_T("Drop source supports format %s"), 
 161                        wxDataObject::GetFormatName(fmt
.cfFormat
)); 
 168         wxLogLastError(_T("IDataObject::EnumFormatEtc")); 
 172     if ( !m_pTarget
->IsAcceptedData(pIDataSource
) ) { 
 173         // we don't accept this kind of data 
 174         *pdwEffect 
= DROPEFFECT_NONE
; 
 179     // get hold of the data object 
 180     m_pIDataObject 
= pIDataSource
; 
 181     m_pIDataObject
->AddRef(); 
 183     // we need client coordinates to pass to wxWin functions 
 184     if ( !ScreenToClient(m_hwnd
, (POINT 
*)&pt
) ) 
 186         wxLogLastError(wxT("ScreenToClient")); 
 189     // give some visual feedback 
 190     *pdwEffect 
= ConvertDragResultToEffect( 
 191         m_pTarget
->OnEnter(pt
.x
, pt
.y
, ConvertDragEffectToResult( 
 192             GetDropEffect(grfKeyState
, m_pTarget
->GetDefaultAction())) 
 199 // Name    : wxIDropTarget::DragOver 
 200 // Purpose : Indicates that the mouse was moved inside the window represented 
 201 //           by this drop target. 
 203 // Params  : [in] DWORD   grfKeyState     kbd & mouse state 
 204 //           [in] POINTL  pt              mouse coordinates 
 205 //           [out]LPDWORD pdwEffect       effect flag 
 206 // Notes   : We're called on every WM_MOUSEMOVE, so this function should be 
 208 STDMETHODIMP 
wxIDropTarget::DragOver(DWORD   grfKeyState
, 
 212     // there are too many of them... wxLogDebug("IDropTarget::DragOver"); 
 215     if ( m_pIDataObject 
) { 
 216         result 
= ConvertDragEffectToResult( 
 217             GetDropEffect(grfKeyState
, m_pTarget
->GetDefaultAction())); 
 220         // can't accept data anyhow normally 
 224     // we need client coordinates to pass to wxWin functions 
 225     if ( !ScreenToClient(m_hwnd
, (POINT 
*)&pt
) ) 
 227         wxLogLastError(wxT("ScreenToClient")); 
 230     *pdwEffect 
= ConvertDragResultToEffect( 
 231                     m_pTarget
->OnDragOver(pt
.x
, pt
.y
, result
) 
 237 // Name    : wxIDropTarget::DragLeave 
 238 // Purpose : Informs the drop target that the operation has left its window. 
 240 // Notes   : good place to do any clean-up 
 241 STDMETHODIMP 
wxIDropTarget::DragLeave() 
 243   wxLogTrace(wxTRACE_OleCalls
, wxT("IDropTarget::DragLeave")); 
 245   // remove the UI feedback 
 246   m_pTarget
->OnLeave(); 
 248   // release the held object 
 249   RELEASE_AND_NULL(m_pIDataObject
); 
 254 // Name    : wxIDropTarget::Drop 
 255 // Purpose : Instructs the drop target to paste data that was just now 
 258 // Params  : [in] IDataObject *pIDataSource     the data to paste 
 259 //           [in] DWORD        grfKeyState      kbd & mouse state 
 260 //           [in] POINTL       pt               where the drop occurred? 
 261 //           [ouy]DWORD       *pdwEffect        operation effect 
 263 STDMETHODIMP 
wxIDropTarget::Drop(IDataObject 
*pIDataSource
, 
 268     wxLogTrace(wxTRACE_OleCalls
, wxT("IDropTarget::Drop")); 
 270     // TODO I don't know why there is this parameter, but so far I assume 
 271     //      that it's the same we've already got in DragEnter 
 272     wxASSERT( m_pIDataObject 
== pIDataSource 
); 
 274     // by default, nothing happens 
 275     *pdwEffect 
= DROPEFFECT_NONE
; 
 277     // we need client coordinates to pass to wxWin functions 
 278     if ( !ScreenToClient(m_hwnd
, (POINT 
*)&pt
) ) 
 280         wxLogLastError(wxT("ScreenToClient")); 
 283     // first ask the drop target if it wants data 
 284     if ( m_pTarget
->OnDrop(pt
.x
, pt
.y
) ) { 
 285         // it does, so give it the data source 
 286         m_pTarget
->SetDataSource(pIDataSource
); 
 288         // and now it has the data 
 289         wxDragResult rc 
= ConvertDragEffectToResult( 
 290             GetDropEffect(grfKeyState
, m_pTarget
->GetDefaultAction())); 
 291         rc 
= m_pTarget
->OnData(pt
.x
, pt
.y
, rc
); 
 292         if ( wxIsDragResultOk(rc
) ) { 
 293             // operation succeeded 
 294             *pdwEffect 
= ConvertDragResultToEffect(rc
); 
 296         //else: *pdwEffect is already DROPEFFECT_NONE 
 298     //else: OnDrop() returned false, no need to copy data 
 300     // release the held object 
 301     RELEASE_AND_NULL(m_pIDataObject
); 
 306 // ============================================================================ 
 307 // wxDropTarget implementation 
 308 // ============================================================================ 
 310 // ---------------------------------------------------------------------------- 
 312 // ---------------------------------------------------------------------------- 
 314 wxDropTarget::wxDropTarget(wxDataObject 
*dataObj
) 
 315             : wxDropTargetBase(dataObj
) 
 317     // create an IDropTarget implementation which will notify us about d&d 
 319     m_pIDropTarget 
= new wxIDropTarget(this); 
 320     m_pIDropTarget
->AddRef(); 
 323 wxDropTarget::~wxDropTarget() 
 325     ReleaseInterface(m_pIDropTarget
); 
 328 // ---------------------------------------------------------------------------- 
 329 // [un]register drop handler 
 330 // ---------------------------------------------------------------------------- 
 332 bool wxDropTarget::Register(WXHWND hwnd
) 
 335     // RegisterDragDrop not available on Windows CE >= 400? 
 336     // Or maybe we can dynamically load them from ceshell.dll 
 338 #if defined(__WXWINCE__) && _WIN32_WCE >= 400 
 344     // May exist in later WinCE versions 
 346     hr 
= ::CoLockObjectExternal(m_pIDropTarget
, TRUE
, FALSE
); 
 348         wxLogApiError(wxT("CoLockObjectExternal"), hr
); 
 353     hr 
= ::RegisterDragDrop((HWND
) hwnd
, m_pIDropTarget
); 
 355     // May exist in later WinCE versions 
 357         ::CoLockObjectExternal(m_pIDropTarget
, FALSE
, FALSE
); 
 359         wxLogApiError(wxT("RegisterDragDrop"), hr
); 
 363     // we will need the window handle for coords transformation later 
 364     m_pIDropTarget
->SetHwnd((HWND
)hwnd
); 
 370 void wxDropTarget::Revoke(WXHWND hwnd
) 
 372 #if defined(__WXWINCE__) && _WIN32_WCE >= 400 
 373     // Not available, see note above 
 376     HRESULT hr 
= ::RevokeDragDrop((HWND
) hwnd
); 
 379         wxLogApiError(wxT("RevokeDragDrop"), hr
); 
 382     // May exist in later WinCE versions 
 384     ::CoLockObjectExternal(m_pIDropTarget
, FALSE
, TRUE
); 
 387     m_pIDropTarget
->SetHwnd(0); 
 391 // ---------------------------------------------------------------------------- 
 392 // base class pure virtuals 
 393 // ---------------------------------------------------------------------------- 
 395 // OnDrop() is called only if we previously returned true from 
 396 // IsAcceptedData(), so no need to check anything here 
 397 bool wxDropTarget::OnDrop(wxCoord 
WXUNUSED(x
), wxCoord 
WXUNUSED(y
)) 
 402 // copy the data from the data source to the target data object 
 403 bool wxDropTarget::GetData() 
 405     wxDataFormat format 
= GetSupportedFormat(m_pIDataSource
); 
 406     if ( format 
== wxDF_INVALID 
) { 
 407         // this is strange because IsAcceptedData() succeeded previously! 
 408         wxFAIL_MSG(wxT("strange - did supported formats list change?")); 
 415     fmtMemory
.cfFormat  
= format
; 
 416     fmtMemory
.ptd       
= NULL
; 
 417     fmtMemory
.dwAspect  
= DVASPECT_CONTENT
; 
 418     fmtMemory
.lindex    
= -1; 
 419     fmtMemory
.tymed     
= TYMED_HGLOBAL
;  // TODO to add other media 
 423     HRESULT hr 
= m_pIDataSource
->GetData(&fmtMemory
, &stm
); 
 424     if ( SUCCEEDED(hr
) ) { 
 425         IDataObject 
*dataObject 
= m_dataObject
->GetInterface(); 
 427         hr 
= dataObject
->SetData(&fmtMemory
, &stm
, TRUE
); 
 428         if ( SUCCEEDED(hr
) ) { 
 432             wxLogApiError(wxT("IDataObject::SetData()"), hr
); 
 436         wxLogApiError(wxT("IDataObject::GetData()"), hr
); 
 442 // ---------------------------------------------------------------------------- 
 443 // callbacks used by wxIDropTarget 
 444 // ---------------------------------------------------------------------------- 
 446 // we need a data source, so wxIDropTarget gives it to us using this function 
 447 void wxDropTarget::SetDataSource(IDataObject 
*pIDataSource
) 
 449     m_pIDataSource 
= pIDataSource
; 
 452 // determine if we accept data of this type 
 453 bool wxDropTarget::IsAcceptedData(IDataObject 
*pIDataSource
) const 
 455     return GetSupportedFormat(pIDataSource
) != wxDF_INVALID
; 
 458 // ---------------------------------------------------------------------------- 
 460 // ---------------------------------------------------------------------------- 
 462 wxDataFormat 
wxDropTarget::GetSupportedFormat(IDataObject 
*pIDataSource
) const 
 464     // this strucutre describes a data of any type (first field will be 
 465     // changing) being passed through global memory block. 
 466     static FORMATETC s_fmtMemory 
= { 
 471         TYMED_HGLOBAL       
// TODO is it worth supporting other tymeds here? 
 474     // get the list of supported formats 
 475     size_t nFormats 
= m_dataObject
->GetFormatCount(wxDataObject::Set
); 
 477     wxDataFormat 
*formats
; 
 478     formats 
= nFormats 
== 1 ? &format 
:  new wxDataFormat
[nFormats
]; 
 480     m_dataObject
->GetAllFormats(formats
, wxDataObject::Set
); 
 482     // cycle through all supported formats 
 484     for ( n 
= 0; n 
< nFormats
; n
++ ) { 
 485         s_fmtMemory
.cfFormat 
= formats
[n
]; 
 487         // NB: don't use SUCCEEDED macro here: QueryGetData returns S_FALSE 
 488         //     for file drag and drop (format == CF_HDROP) 
 489         if ( pIDataSource
->QueryGetData(&s_fmtMemory
) == S_OK 
) { 
 496     if ( formats 
!= &format 
) { 
 497         // free memory if we allocated it 
 501     return n 
< nFormats 
? format 
: wxFormatInvalid
; 
 504 // ---------------------------------------------------------------------------- 
 506 // ---------------------------------------------------------------------------- 
 508 static wxDragResult 
ConvertDragEffectToResult(DWORD dwEffect
) 
 510     switch ( dwEffect 
) { 
 511         case DROPEFFECT_COPY
: 
 514         case DROPEFFECT_LINK
: 
 517         case DROPEFFECT_MOVE
: 
 521             wxFAIL_MSG(wxT("invalid value in ConvertDragEffectToResult")); 
 524         case DROPEFFECT_NONE
: 
 529 static DWORD 
ConvertDragResultToEffect(wxDragResult result
) 
 533             return DROPEFFECT_COPY
; 
 536             return DROPEFFECT_LINK
; 
 539             return DROPEFFECT_MOVE
; 
 542             wxFAIL_MSG(wxT("invalid value in ConvertDragResultToEffect")); 
 546             return DROPEFFECT_NONE
; 
 550 #endif // wxUSE_OLE && wxUSE_DRAG_AND_DROP