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
30 #include "wx/msw/wrapwin.h"
34 #include "wx/msw/private.h"
42 #if !defined(__GNUWIN32__) || wxUSE_NORLANDER_HEADERS
43 #include <shlobj.h> // for DROPFILES structure
51 #include "wx/msw/ole/oleutils.h"
53 // ----------------------------------------------------------------------------
54 // IDropTarget interface: forward all interesting things to wxDropTarget
55 // (the name is unfortunate, but wx_I_DropTarget is not at all the same thing
56 // as wxDropTarget which is 'public' class, while this one is private)
57 // ----------------------------------------------------------------------------
59 class wxIDropTarget
: public IDropTarget
62 wxIDropTarget(wxDropTarget
*p
);
63 virtual ~wxIDropTarget();
65 // accessors for wxDropTarget
66 void SetHwnd(HWND hwnd
) { m_hwnd
= hwnd
; }
68 // IDropTarget methods
69 STDMETHODIMP
DragEnter(LPDATAOBJECT
, DWORD
, POINTL
, LPDWORD
);
70 STDMETHODIMP
DragOver(DWORD
, POINTL
, LPDWORD
);
71 STDMETHODIMP
DragLeave();
72 STDMETHODIMP
Drop(LPDATAOBJECT
, DWORD
, POINTL
, LPDWORD
);
74 DECLARE_IUNKNOWN_METHODS
;
77 IDataObject
*m_pIDataObject
; // !NULL between DragEnter and DragLeave/Drop
78 wxDropTarget
*m_pTarget
; // the real target (we're just a proxy)
80 HWND m_hwnd
; // window we're associated with
82 // get default drop effect for given keyboard flags
83 static DWORD
GetDropEffect(DWORD flags
, wxDragResult defaultAction
, DWORD pdwEffect
);
85 DECLARE_NO_COPY_CLASS(wxIDropTarget
)
88 // ----------------------------------------------------------------------------
90 // ----------------------------------------------------------------------------
92 static wxDragResult
ConvertDragEffectToResult(DWORD dwEffect
);
93 static DWORD
ConvertDragResultToEffect(wxDragResult result
);
95 // ============================================================================
96 // wxIDropTarget implementation
97 // ============================================================================
99 // Name : static wxIDropTarget::GetDropEffect
100 // Purpose : determine the drop operation from keyboard/mouse state.
101 // Returns : DWORD combined from DROPEFFECT_xxx constants
102 // Params : [in] DWORD flags kbd & mouse flags as passed to
103 // IDropTarget methods
104 // Notes : We do "move" normally and "copy" if <Ctrl> is pressed,
105 // which is the standard behaviour (currently there is no
106 // way to redefine it)
107 DWORD
wxIDropTarget::GetDropEffect(DWORD flags
,
108 wxDragResult defaultAction
,
111 DWORD effectiveAction
;
112 if ( defaultAction
== wxDragCopy
)
113 effectiveAction
= flags
& MK_SHIFT
? DROPEFFECT_MOVE
: DROPEFFECT_COPY
;
115 effectiveAction
= flags
& MK_CONTROL
? DROPEFFECT_COPY
: DROPEFFECT_MOVE
;
117 if ( !(effectiveAction
& pdwEffect
) )
119 // the action is not supported by drag source, fall back to something
120 // that it does support
121 if ( pdwEffect
& DROPEFFECT_MOVE
)
122 effectiveAction
= DROPEFFECT_MOVE
;
123 else if ( pdwEffect
& DROPEFFECT_COPY
)
124 effectiveAction
= DROPEFFECT_COPY
;
125 else if ( pdwEffect
& DROPEFFECT_LINK
)
126 effectiveAction
= DROPEFFECT_LINK
;
128 effectiveAction
= DROPEFFECT_NONE
;
131 return effectiveAction
;
134 wxIDropTarget::wxIDropTarget(wxDropTarget
*pTarget
)
137 m_pIDataObject
= NULL
;
140 wxIDropTarget::~wxIDropTarget()
144 BEGIN_IID_TABLE(wxIDropTarget
)
149 IMPLEMENT_IUNKNOWN_METHODS(wxIDropTarget
)
151 // Name : wxIDropTarget::DragEnter
152 // Purpose : Called when the mouse enters the window (dragging something)
154 // Params : [in] IDataObject *pIDataSource : source data
155 // [in] DWORD grfKeyState : kbd & mouse state
156 // [in] POINTL pt : mouse coordinates
157 // [out]DWORD *pdwEffect : effect flag
159 STDMETHODIMP
wxIDropTarget::DragEnter(IDataObject
*pIDataSource
,
164 wxLogTrace(wxTRACE_OleCalls
, wxT("IDropTarget::DragEnter"));
166 wxASSERT_MSG( m_pIDataObject
== NULL
,
167 _T("drop target must have data object") );
169 // show the list of formats supported by the source data object for the
170 // debugging purposes, this is quite useful sometimes - please don't remove
172 IEnumFORMATETC
*penumFmt
;
173 if ( SUCCEEDED(pIDataSource
->EnumFormatEtc(DATADIR_GET
, &penumFmt
)) )
176 while ( penumFmt
->Next(1, &fmt
, NULL
) == S_OK
)
178 wxLogDebug(_T("Drop source supports format %s"),
179 wxDataObject::GetFormatName(fmt
.cfFormat
));
186 wxLogLastError(_T("IDataObject::EnumFormatEtc"));
190 if ( !m_pTarget
->IsAcceptedData(pIDataSource
) ) {
191 // we don't accept this kind of data
192 *pdwEffect
= DROPEFFECT_NONE
;
197 // get hold of the data object
198 m_pIDataObject
= pIDataSource
;
199 m_pIDataObject
->AddRef();
201 // we need client coordinates to pass to wxWin functions
202 if ( !ScreenToClient(m_hwnd
, (POINT
*)&pt
) )
204 wxLogLastError(wxT("ScreenToClient"));
207 // give some visual feedback
208 *pdwEffect
= ConvertDragResultToEffect(
209 m_pTarget
->OnEnter(pt
.x
, pt
.y
, ConvertDragEffectToResult(
210 GetDropEffect(grfKeyState
, m_pTarget
->GetDefaultAction(), *pdwEffect
))
217 // Name : wxIDropTarget::DragOver
218 // Purpose : Indicates that the mouse was moved inside the window represented
219 // by this drop target.
221 // Params : [in] DWORD grfKeyState kbd & mouse state
222 // [in] POINTL pt mouse coordinates
223 // [out]LPDWORD pdwEffect effect flag
224 // Notes : We're called on every WM_MOUSEMOVE, so this function should be
226 STDMETHODIMP
wxIDropTarget::DragOver(DWORD grfKeyState
,
230 // there are too many of them... wxLogDebug("IDropTarget::DragOver");
233 if ( m_pIDataObject
) {
234 result
= ConvertDragEffectToResult(
235 GetDropEffect(grfKeyState
, m_pTarget
->GetDefaultAction(), *pdwEffect
));
238 // can't accept data anyhow normally
242 // we need client coordinates to pass to wxWin functions
243 if ( !ScreenToClient(m_hwnd
, (POINT
*)&pt
) )
245 wxLogLastError(wxT("ScreenToClient"));
248 *pdwEffect
= ConvertDragResultToEffect(
249 m_pTarget
->OnDragOver(pt
.x
, pt
.y
, result
)
255 // Name : wxIDropTarget::DragLeave
256 // Purpose : Informs the drop target that the operation has left its window.
258 // Notes : good place to do any clean-up
259 STDMETHODIMP
wxIDropTarget::DragLeave()
261 wxLogTrace(wxTRACE_OleCalls
, wxT("IDropTarget::DragLeave"));
263 // remove the UI feedback
264 m_pTarget
->OnLeave();
266 // release the held object
267 RELEASE_AND_NULL(m_pIDataObject
);
272 // Name : wxIDropTarget::Drop
273 // Purpose : Instructs the drop target to paste data that was just now
276 // Params : [in] IDataObject *pIDataSource the data to paste
277 // [in] DWORD grfKeyState kbd & mouse state
278 // [in] POINTL pt where the drop occurred?
279 // [ouy]DWORD *pdwEffect operation effect
281 STDMETHODIMP
wxIDropTarget::Drop(IDataObject
*pIDataSource
,
286 wxLogTrace(wxTRACE_OleCalls
, wxT("IDropTarget::Drop"));
288 // TODO I don't know why there is this parameter, but so far I assume
289 // that it's the same we've already got in DragEnter
290 wxASSERT( m_pIDataObject
== pIDataSource
);
292 // we need client coordinates to pass to wxWin functions
293 if ( !ScreenToClient(m_hwnd
, (POINT
*)&pt
) )
295 wxLogLastError(wxT("ScreenToClient"));
298 // first ask the drop target if it wants data
299 if ( m_pTarget
->OnDrop(pt
.x
, pt
.y
) ) {
300 // it does, so give it the data source
301 m_pTarget
->SetDataSource(pIDataSource
);
303 // and now it has the data
304 wxDragResult rc
= ConvertDragEffectToResult(
305 GetDropEffect(grfKeyState
, m_pTarget
->GetDefaultAction(), *pdwEffect
));
306 rc
= m_pTarget
->OnData(pt
.x
, pt
.y
, rc
);
307 if ( wxIsDragResultOk(rc
) ) {
308 // operation succeeded
309 *pdwEffect
= ConvertDragResultToEffect(rc
);
312 *pdwEffect
= DROPEFFECT_NONE
;
316 // OnDrop() returned false, no need to copy data
317 *pdwEffect
= DROPEFFECT_NONE
;
320 // release the held object
321 RELEASE_AND_NULL(m_pIDataObject
);
326 // ============================================================================
327 // wxDropTarget implementation
328 // ============================================================================
330 // ----------------------------------------------------------------------------
332 // ----------------------------------------------------------------------------
334 wxDropTarget::wxDropTarget(wxDataObject
*dataObj
)
335 : wxDropTargetBase(dataObj
)
337 // create an IDropTarget implementation which will notify us about d&d
339 m_pIDropTarget
= new wxIDropTarget(this);
340 m_pIDropTarget
->AddRef();
343 wxDropTarget::~wxDropTarget()
345 ReleaseInterface(m_pIDropTarget
);
348 // ----------------------------------------------------------------------------
349 // [un]register drop handler
350 // ----------------------------------------------------------------------------
352 bool wxDropTarget::Register(WXHWND hwnd
)
355 // RegisterDragDrop not available on Windows CE >= 400?
356 // Or maybe we can dynamically load them from ceshell.dll
358 #if defined(__WXWINCE__) && _WIN32_WCE >= 400
364 // May exist in later WinCE versions
366 hr
= ::CoLockObjectExternal(m_pIDropTarget
, TRUE
, FALSE
);
368 wxLogApiError(wxT("CoLockObjectExternal"), hr
);
373 hr
= ::RegisterDragDrop((HWND
) hwnd
, m_pIDropTarget
);
375 // May exist in later WinCE versions
377 ::CoLockObjectExternal(m_pIDropTarget
, FALSE
, FALSE
);
379 wxLogApiError(wxT("RegisterDragDrop"), hr
);
383 // we will need the window handle for coords transformation later
384 m_pIDropTarget
->SetHwnd((HWND
)hwnd
);
390 void wxDropTarget::Revoke(WXHWND hwnd
)
392 #if defined(__WXWINCE__) && _WIN32_WCE >= 400
393 // Not available, see note above
396 HRESULT hr
= ::RevokeDragDrop((HWND
) hwnd
);
399 wxLogApiError(wxT("RevokeDragDrop"), hr
);
402 // May exist in later WinCE versions
404 ::CoLockObjectExternal(m_pIDropTarget
, FALSE
, TRUE
);
407 m_pIDropTarget
->SetHwnd(0);
411 // ----------------------------------------------------------------------------
412 // base class pure virtuals
413 // ----------------------------------------------------------------------------
415 // OnDrop() is called only if we previously returned true from
416 // IsAcceptedData(), so no need to check anything here
417 bool wxDropTarget::OnDrop(wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
))
422 // copy the data from the data source to the target data object
423 bool wxDropTarget::GetData()
425 wxDataFormat format
= GetSupportedFormat(m_pIDataSource
);
426 if ( format
== wxDF_INVALID
) {
427 // this is strange because IsAcceptedData() succeeded previously!
428 wxFAIL_MSG(wxT("strange - did supported formats list change?"));
435 fmtMemory
.cfFormat
= format
;
436 fmtMemory
.ptd
= NULL
;
437 fmtMemory
.dwAspect
= DVASPECT_CONTENT
;
438 fmtMemory
.lindex
= -1;
439 fmtMemory
.tymed
= TYMED_HGLOBAL
; // TODO to add other media
443 HRESULT hr
= m_pIDataSource
->GetData(&fmtMemory
, &stm
);
444 if ( SUCCEEDED(hr
) ) {
445 IDataObject
*dataObject
= m_dataObject
->GetInterface();
447 hr
= dataObject
->SetData(&fmtMemory
, &stm
, TRUE
);
448 if ( SUCCEEDED(hr
) ) {
452 wxLogApiError(wxT("IDataObject::SetData()"), hr
);
456 wxLogApiError(wxT("IDataObject::GetData()"), hr
);
462 // ----------------------------------------------------------------------------
463 // callbacks used by wxIDropTarget
464 // ----------------------------------------------------------------------------
466 // we need a data source, so wxIDropTarget gives it to us using this function
467 void wxDropTarget::SetDataSource(IDataObject
*pIDataSource
)
469 m_pIDataSource
= pIDataSource
;
472 // determine if we accept data of this type
473 bool wxDropTarget::IsAcceptedData(IDataObject
*pIDataSource
) const
475 return GetSupportedFormat(pIDataSource
) != wxDF_INVALID
;
478 // ----------------------------------------------------------------------------
480 // ----------------------------------------------------------------------------
482 wxDataFormat
wxDropTarget::GetSupportedFormat(IDataObject
*pIDataSource
) const
484 // this strucutre describes a data of any type (first field will be
485 // changing) being passed through global memory block.
486 static FORMATETC s_fmtMemory
= {
491 TYMED_HGLOBAL
// TODO is it worth supporting other tymeds here?
494 // get the list of supported formats
495 size_t nFormats
= m_dataObject
->GetFormatCount(wxDataObject::Set
);
497 wxDataFormat
*formats
;
498 formats
= nFormats
== 1 ? &format
: new wxDataFormat
[nFormats
];
500 m_dataObject
->GetAllFormats(formats
, wxDataObject::Set
);
502 // cycle through all supported formats
504 for ( n
= 0; n
< nFormats
; n
++ ) {
505 s_fmtMemory
.cfFormat
= formats
[n
];
507 // NB: don't use SUCCEEDED macro here: QueryGetData returns S_FALSE
508 // for file drag and drop (format == CF_HDROP)
509 if ( pIDataSource
->QueryGetData(&s_fmtMemory
) == S_OK
) {
516 if ( formats
!= &format
) {
517 // free memory if we allocated it
521 return n
< nFormats
? format
: wxFormatInvalid
;
524 // ----------------------------------------------------------------------------
526 // ----------------------------------------------------------------------------
528 static wxDragResult
ConvertDragEffectToResult(DWORD dwEffect
)
530 switch ( dwEffect
) {
531 case DROPEFFECT_COPY
:
534 case DROPEFFECT_LINK
:
537 case DROPEFFECT_MOVE
:
541 wxFAIL_MSG(wxT("invalid value in ConvertDragEffectToResult"));
544 case DROPEFFECT_NONE
:
549 static DWORD
ConvertDragResultToEffect(wxDragResult result
)
553 return DROPEFFECT_COPY
;
556 return DROPEFFECT_LINK
;
559 return DROPEFFECT_MOVE
;
562 wxFAIL_MSG(wxT("invalid value in ConvertDragResultToEffect"));
566 return DROPEFFECT_NONE
;
570 #endif // wxUSE_OLE && wxUSE_DRAG_AND_DROP