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 licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
23 #if defined(__BORLANDC__)
29 #if wxUSE_OLE && wxUSE_DRAG_AND_DROP
31 #include "wx/msw/private.h"
40 #if !defined(__GNUWIN32__) || wxUSE_NORLANDER_HEADERS
41 #if wxCHECK_W32API_VERSION( 1, 0 )
42 #include "wx/msw/wrapwin.h"
44 #include <shlobj.h> // for DROPFILES structure
52 #include "wx/msw/ole/oleutils.h"
54 // ----------------------------------------------------------------------------
55 // IDropTarget interface: forward all interesting things to wxDropTarget
56 // (the name is unfortunate, but wx_I_DropTarget is not at all the same thing
57 // as wxDropTarget which is 'public' class, while this one is private)
58 // ----------------------------------------------------------------------------
60 class wxIDropTarget
: public IDropTarget
63 wxIDropTarget(wxDropTarget
*p
);
64 virtual ~wxIDropTarget();
66 // accessors for wxDropTarget
67 void SetHwnd(HWND hwnd
) { m_hwnd
= hwnd
; }
69 // IDropTarget methods
70 STDMETHODIMP
DragEnter(LPDATAOBJECT
, DWORD
, POINTL
, LPDWORD
);
71 STDMETHODIMP
DragOver(DWORD
, POINTL
, LPDWORD
);
72 STDMETHODIMP
DragLeave();
73 STDMETHODIMP
Drop(LPDATAOBJECT
, DWORD
, POINTL
, LPDWORD
);
75 DECLARE_IUNKNOWN_METHODS
;
78 IDataObject
*m_pIDataObject
; // !NULL between DragEnter and DragLeave/Drop
79 wxDropTarget
*m_pTarget
; // the real target (we're just a proxy)
81 HWND m_hwnd
; // window we're associated with
83 // get default drop effect for given keyboard flags
84 static inline DWORD
GetDropEffect(DWORD flags
, wxDragResult defaultAction
);
86 DECLARE_NO_COPY_CLASS(wxIDropTarget
)
89 // ----------------------------------------------------------------------------
91 // ----------------------------------------------------------------------------
93 static wxDragResult
ConvertDragEffectToResult(DWORD dwEffect
);
94 static DWORD
ConvertDragResultToEffect(wxDragResult result
);
96 // ============================================================================
97 // wxIDropTarget implementation
98 // ============================================================================
100 // Name : static wxIDropTarget::GetDropEffect
101 // Purpose : determine the drop operation from keyboard/mouse state.
102 // Returns : DWORD combined from DROPEFFECT_xxx constants
103 // Params : [in] DWORD flags kbd & mouse flags as passed to
104 // IDropTarget methods
105 // Notes : We do "move" normally and "copy" if <Ctrl> is pressed,
106 // which is the standard behaviour (currently there is no
107 // way to redefine it)
108 DWORD
wxIDropTarget::GetDropEffect(DWORD flags
, wxDragResult defaultAction
)
110 if (defaultAction
== wxDragCopy
)
111 return flags
& MK_SHIFT
? DROPEFFECT_MOVE
: DROPEFFECT_COPY
;
112 return flags
& MK_CONTROL
? DROPEFFECT_COPY
: DROPEFFECT_MOVE
;
115 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
, ConvertDragEffectToResult(
191 GetDropEffect(grfKeyState
, m_pTarget
->GetDefaultAction()))
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(
216 GetDropEffect(grfKeyState
, m_pTarget
->GetDefaultAction()));
219 // can't accept data anyhow normally
223 // we need client coordinates to pass to wxWin functions
224 if ( !ScreenToClient(m_hwnd
, (POINT
*)&pt
) )
226 wxLogLastError(wxT("ScreenToClient"));
229 *pdwEffect
= ConvertDragResultToEffect(
230 m_pTarget
->OnDragOver(pt
.x
, pt
.y
, result
)
236 // Name : wxIDropTarget::DragLeave
237 // Purpose : Informs the drop target that the operation has left its window.
239 // Notes : good place to do any clean-up
240 STDMETHODIMP
wxIDropTarget::DragLeave()
242 wxLogTrace(wxTRACE_OleCalls
, wxT("IDropTarget::DragLeave"));
244 // remove the UI feedback
245 m_pTarget
->OnLeave();
247 // release the held object
248 RELEASE_AND_NULL(m_pIDataObject
);
253 // Name : wxIDropTarget::Drop
254 // Purpose : Instructs the drop target to paste data that was just now
257 // Params : [in] IDataObject *pIDataSource the data to paste
258 // [in] DWORD grfKeyState kbd & mouse state
259 // [in] POINTL pt where the drop occurred?
260 // [ouy]DWORD *pdwEffect operation effect
262 STDMETHODIMP
wxIDropTarget::Drop(IDataObject
*pIDataSource
,
267 wxLogTrace(wxTRACE_OleCalls
, wxT("IDropTarget::Drop"));
269 // TODO I don't know why there is this parameter, but so far I assume
270 // that it's the same we've already got in DragEnter
271 wxASSERT( m_pIDataObject
== pIDataSource
);
273 // by default, nothing happens
274 *pdwEffect
= DROPEFFECT_NONE
;
276 // we need client coordinates to pass to wxWin functions
277 if ( !ScreenToClient(m_hwnd
, (POINT
*)&pt
) )
279 wxLogLastError(wxT("ScreenToClient"));
282 // first ask the drop target if it wants data
283 if ( m_pTarget
->OnDrop(pt
.x
, pt
.y
) ) {
284 // it does, so give it the data source
285 m_pTarget
->SetDataSource(pIDataSource
);
287 // and now it has the data
288 wxDragResult rc
= ConvertDragEffectToResult(
289 GetDropEffect(grfKeyState
, m_pTarget
->GetDefaultAction()));
290 rc
= m_pTarget
->OnData(pt
.x
, pt
.y
, rc
);
291 if ( wxIsDragResultOk(rc
) ) {
292 // operation succeeded
293 *pdwEffect
= ConvertDragResultToEffect(rc
);
295 //else: *pdwEffect is already DROPEFFECT_NONE
297 //else: OnDrop() returned false, no need to copy data
299 // release the held object
300 RELEASE_AND_NULL(m_pIDataObject
);
305 // ============================================================================
306 // wxDropTarget implementation
307 // ============================================================================
309 // ----------------------------------------------------------------------------
311 // ----------------------------------------------------------------------------
313 wxDropTarget::wxDropTarget(wxDataObject
*dataObj
)
314 : wxDropTargetBase(dataObj
)
316 // create an IDropTarget implementation which will notify us about d&d
318 m_pIDropTarget
= new wxIDropTarget(this);
319 m_pIDropTarget
->AddRef();
322 wxDropTarget::~wxDropTarget()
324 ReleaseInterface(m_pIDropTarget
);
327 // ----------------------------------------------------------------------------
328 // [un]register drop handler
329 // ----------------------------------------------------------------------------
331 bool wxDropTarget::Register(WXHWND hwnd
)
334 // RegisterDragDrop not available on Windows CE >= 400?
335 // Or maybe we can dynamically load them from ceshell.dll
337 #if defined(__WXWINCE__) && _WIN32_WCE >= 400
343 // May exist in later WinCE versions
345 hr
= ::CoLockObjectExternal(m_pIDropTarget
, TRUE
, FALSE
);
347 wxLogApiError(wxT("CoLockObjectExternal"), hr
);
352 hr
= ::RegisterDragDrop((HWND
) hwnd
, m_pIDropTarget
);
354 // May exist in later WinCE versions
356 ::CoLockObjectExternal(m_pIDropTarget
, FALSE
, FALSE
);
358 wxLogApiError(wxT("RegisterDragDrop"), hr
);
362 // we will need the window handle for coords transformation later
363 m_pIDropTarget
->SetHwnd((HWND
)hwnd
);
369 void wxDropTarget::Revoke(WXHWND hwnd
)
371 #if defined(__WXWINCE__) && _WIN32_WCE >= 400
372 // Not available, see note above
375 HRESULT hr
= ::RevokeDragDrop((HWND
) hwnd
);
378 wxLogApiError(wxT("RevokeDragDrop"), hr
);
381 // May exist in later WinCE versions
383 ::CoLockObjectExternal(m_pIDropTarget
, FALSE
, TRUE
);
386 m_pIDropTarget
->SetHwnd(0);
390 // ----------------------------------------------------------------------------
391 // base class pure virtuals
392 // ----------------------------------------------------------------------------
394 // OnDrop() is called only if we previously returned true from
395 // IsAcceptedData(), so no need to check anything here
396 bool wxDropTarget::OnDrop(wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
))
401 // copy the data from the data source to the target data object
402 bool wxDropTarget::GetData()
404 wxDataFormat format
= GetSupportedFormat(m_pIDataSource
);
405 if ( format
== wxDF_INVALID
) {
406 // this is strange because IsAcceptedData() succeeded previously!
407 wxFAIL_MSG(wxT("strange - did supported formats list change?"));
414 fmtMemory
.cfFormat
= format
;
415 fmtMemory
.ptd
= NULL
;
416 fmtMemory
.dwAspect
= DVASPECT_CONTENT
;
417 fmtMemory
.lindex
= -1;
418 fmtMemory
.tymed
= TYMED_HGLOBAL
; // TODO to add other media
422 HRESULT hr
= m_pIDataSource
->GetData(&fmtMemory
, &stm
);
423 if ( SUCCEEDED(hr
) ) {
424 IDataObject
*dataObject
= m_dataObject
->GetInterface();
426 hr
= dataObject
->SetData(&fmtMemory
, &stm
, TRUE
);
427 if ( SUCCEEDED(hr
) ) {
431 wxLogApiError(wxT("IDataObject::SetData()"), hr
);
435 wxLogApiError(wxT("IDataObject::GetData()"), hr
);
441 // ----------------------------------------------------------------------------
442 // callbacks used by wxIDropTarget
443 // ----------------------------------------------------------------------------
445 // we need a data source, so wxIDropTarget gives it to us using this function
446 void wxDropTarget::SetDataSource(IDataObject
*pIDataSource
)
448 m_pIDataSource
= pIDataSource
;
451 // determine if we accept data of this type
452 bool wxDropTarget::IsAcceptedData(IDataObject
*pIDataSource
) const
454 return GetSupportedFormat(pIDataSource
) != wxDF_INVALID
;
457 // ----------------------------------------------------------------------------
459 // ----------------------------------------------------------------------------
461 wxDataFormat
wxDropTarget::GetSupportedFormat(IDataObject
*pIDataSource
) const
463 // this strucutre describes a data of any type (first field will be
464 // changing) being passed through global memory block.
465 static FORMATETC s_fmtMemory
= {
470 TYMED_HGLOBAL
// TODO is it worth supporting other tymeds here?
473 // get the list of supported formats
474 size_t nFormats
= m_dataObject
->GetFormatCount(wxDataObject::Set
);
476 wxDataFormat
*formats
;
477 formats
= nFormats
== 1 ? &format
: new wxDataFormat
[nFormats
];
479 m_dataObject
->GetAllFormats(formats
, wxDataObject::Set
);
481 // cycle through all supported formats
483 for ( n
= 0; n
< nFormats
; n
++ ) {
484 s_fmtMemory
.cfFormat
= formats
[n
];
486 // NB: don't use SUCCEEDED macro here: QueryGetData returns S_FALSE
487 // for file drag and drop (format == CF_HDROP)
488 if ( pIDataSource
->QueryGetData(&s_fmtMemory
) == S_OK
) {
495 if ( formats
!= &format
) {
496 // free memory if we allocated it
500 return n
< nFormats
? format
: wxFormatInvalid
;
503 // ----------------------------------------------------------------------------
505 // ----------------------------------------------------------------------------
507 static wxDragResult
ConvertDragEffectToResult(DWORD dwEffect
)
509 switch ( dwEffect
) {
510 case DROPEFFECT_COPY
:
513 case DROPEFFECT_LINK
:
516 case DROPEFFECT_MOVE
:
520 wxFAIL_MSG(wxT("invalid value in ConvertDragEffectToResult"));
523 case DROPEFFECT_NONE
:
528 static DWORD
ConvertDragResultToEffect(wxDragResult result
)
532 return DROPEFFECT_COPY
;
535 return DROPEFFECT_LINK
;
538 return DROPEFFECT_MOVE
;
541 wxFAIL_MSG(wxT("invalid value in ConvertDragResultToEffect"));
545 return DROPEFFECT_NONE
;
550 // wxUSE_DRAG_AND_DROP