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 inline DWORD
GetDropEffect(DWORD flags
, wxDragResult defaultAction
);
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
, wxDragResult defaultAction
)
109 if (defaultAction
== wxDragCopy
)
110 return flags
& MK_SHIFT
? DROPEFFECT_MOVE
: DROPEFFECT_COPY
;
111 return flags
& MK_CONTROL
? DROPEFFECT_COPY
: DROPEFFECT_MOVE
;
114 wxIDropTarget::wxIDropTarget(wxDropTarget
*pTarget
)
117 m_pIDataObject
= NULL
;
120 wxIDropTarget::~wxIDropTarget()
124 BEGIN_IID_TABLE(wxIDropTarget
)
129 IMPLEMENT_IUNKNOWN_METHODS(wxIDropTarget
)
131 // Name : wxIDropTarget::DragEnter
132 // Purpose : Called when the mouse enters the window (dragging something)
134 // Params : [in] IDataObject *pIDataSource : source data
135 // [in] DWORD grfKeyState : kbd & mouse state
136 // [in] POINTL pt : mouse coordinates
137 // [out]DWORD *pdwEffect : effect flag
139 STDMETHODIMP
wxIDropTarget::DragEnter(IDataObject
*pIDataSource
,
144 wxLogTrace(wxTRACE_OleCalls
, wxT("IDropTarget::DragEnter"));
146 wxASSERT_MSG( m_pIDataObject
== NULL
,
147 _T("drop target must have data object") );
149 // show the list of formats supported by the source data object for the
150 // debugging purposes, this is quite useful sometimes - please don't remove
152 IEnumFORMATETC
*penumFmt
;
153 if ( SUCCEEDED(pIDataSource
->EnumFormatEtc(DATADIR_GET
, &penumFmt
)) )
156 while ( penumFmt
->Next(1, &fmt
, NULL
) == S_OK
)
158 wxLogDebug(_T("Drop source supports format %s"),
159 wxDataObject::GetFormatName(fmt
.cfFormat
));
166 wxLogLastError(_T("IDataObject::EnumFormatEtc"));
170 if ( !m_pTarget
->IsAcceptedData(pIDataSource
) ) {
171 // we don't accept this kind of data
172 *pdwEffect
= DROPEFFECT_NONE
;
177 // get hold of the data object
178 m_pIDataObject
= pIDataSource
;
179 m_pIDataObject
->AddRef();
181 // we need client coordinates to pass to wxWin functions
182 if ( !ScreenToClient(m_hwnd
, (POINT
*)&pt
) )
184 wxLogLastError(wxT("ScreenToClient"));
187 // give some visual feedback
188 *pdwEffect
= ConvertDragResultToEffect(
189 m_pTarget
->OnEnter(pt
.x
, pt
.y
, ConvertDragEffectToResult(
190 GetDropEffect(grfKeyState
, m_pTarget
->GetDefaultAction()))
197 // Name : wxIDropTarget::DragOver
198 // Purpose : Indicates that the mouse was moved inside the window represented
199 // by this drop target.
201 // Params : [in] DWORD grfKeyState kbd & mouse state
202 // [in] POINTL pt mouse coordinates
203 // [out]LPDWORD pdwEffect effect flag
204 // Notes : We're called on every WM_MOUSEMOVE, so this function should be
206 STDMETHODIMP
wxIDropTarget::DragOver(DWORD grfKeyState
,
210 // there are too many of them... wxLogDebug("IDropTarget::DragOver");
213 if ( m_pIDataObject
) {
214 result
= ConvertDragEffectToResult(
215 GetDropEffect(grfKeyState
, m_pTarget
->GetDefaultAction()));
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 occurred?
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(
288 GetDropEffect(grfKeyState
, m_pTarget
->GetDefaultAction()));
289 rc
= m_pTarget
->OnData(pt
.x
, pt
.y
, rc
);
290 if ( wxIsDragResultOk(rc
) ) {
291 // operation succeeded
292 *pdwEffect
= ConvertDragResultToEffect(rc
);
294 //else: *pdwEffect is already DROPEFFECT_NONE
296 //else: OnDrop() returned false, no need to copy data
298 // release the held object
299 RELEASE_AND_NULL(m_pIDataObject
);
304 // ============================================================================
305 // wxDropTarget implementation
306 // ============================================================================
308 // ----------------------------------------------------------------------------
310 // ----------------------------------------------------------------------------
312 wxDropTarget::wxDropTarget(wxDataObject
*dataObj
)
313 : wxDropTargetBase(dataObj
)
315 // create an IDropTarget implementation which will notify us about d&d
317 m_pIDropTarget
= new wxIDropTarget(this);
318 m_pIDropTarget
->AddRef();
321 wxDropTarget::~wxDropTarget()
323 ReleaseInterface(m_pIDropTarget
);
326 // ----------------------------------------------------------------------------
327 // [un]register drop handler
328 // ----------------------------------------------------------------------------
330 bool wxDropTarget::Register(WXHWND hwnd
)
333 // RegisterDragDrop not available on Windows CE >= 400?
334 // Or maybe we can dynamically load them from ceshell.dll
336 #if defined(__WXWINCE__) && _WIN32_WCE >= 400
342 // May exist in later WinCE versions
344 hr
= ::CoLockObjectExternal(m_pIDropTarget
, TRUE
, FALSE
);
346 wxLogApiError(wxT("CoLockObjectExternal"), hr
);
351 hr
= ::RegisterDragDrop((HWND
) hwnd
, m_pIDropTarget
);
353 // May exist in later WinCE versions
355 ::CoLockObjectExternal(m_pIDropTarget
, FALSE
, FALSE
);
357 wxLogApiError(wxT("RegisterDragDrop"), hr
);
361 // we will need the window handle for coords transformation later
362 m_pIDropTarget
->SetHwnd((HWND
)hwnd
);
368 void wxDropTarget::Revoke(WXHWND hwnd
)
370 #if defined(__WXWINCE__) && _WIN32_WCE >= 400
371 // Not available, see note above
374 HRESULT hr
= ::RevokeDragDrop((HWND
) hwnd
);
377 wxLogApiError(wxT("RevokeDragDrop"), hr
);
380 // May exist in later WinCE versions
382 ::CoLockObjectExternal(m_pIDropTarget
, FALSE
, TRUE
);
385 m_pIDropTarget
->SetHwnd(0);
389 // ----------------------------------------------------------------------------
390 // base class pure virtuals
391 // ----------------------------------------------------------------------------
393 // OnDrop() is called only if we previously returned true from
394 // IsAcceptedData(), so no need to check anything here
395 bool wxDropTarget::OnDrop(wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
))
400 // copy the data from the data source to the target data object
401 bool wxDropTarget::GetData()
403 wxDataFormat format
= GetSupportedFormat(m_pIDataSource
);
404 if ( format
== wxDF_INVALID
) {
405 // this is strange because IsAcceptedData() succeeded previously!
406 wxFAIL_MSG(wxT("strange - did supported formats list change?"));
413 fmtMemory
.cfFormat
= format
;
414 fmtMemory
.ptd
= NULL
;
415 fmtMemory
.dwAspect
= DVASPECT_CONTENT
;
416 fmtMemory
.lindex
= -1;
417 fmtMemory
.tymed
= TYMED_HGLOBAL
; // TODO to add other media
421 HRESULT hr
= m_pIDataSource
->GetData(&fmtMemory
, &stm
);
422 if ( SUCCEEDED(hr
) ) {
423 IDataObject
*dataObject
= m_dataObject
->GetInterface();
425 hr
= dataObject
->SetData(&fmtMemory
, &stm
, TRUE
);
426 if ( SUCCEEDED(hr
) ) {
430 wxLogApiError(wxT("IDataObject::SetData()"), hr
);
434 wxLogApiError(wxT("IDataObject::GetData()"), hr
);
440 // ----------------------------------------------------------------------------
441 // callbacks used by wxIDropTarget
442 // ----------------------------------------------------------------------------
444 // we need a data source, so wxIDropTarget gives it to us using this function
445 void wxDropTarget::SetDataSource(IDataObject
*pIDataSource
)
447 m_pIDataSource
= pIDataSource
;
450 // determine if we accept data of this type
451 bool wxDropTarget::IsAcceptedData(IDataObject
*pIDataSource
) const
453 return GetSupportedFormat(pIDataSource
) != wxDF_INVALID
;
456 // ----------------------------------------------------------------------------
458 // ----------------------------------------------------------------------------
460 wxDataFormat
wxDropTarget::GetSupportedFormat(IDataObject
*pIDataSource
) const
462 // this strucutre describes a data of any type (first field will be
463 // changing) being passed through global memory block.
464 static FORMATETC s_fmtMemory
= {
469 TYMED_HGLOBAL
// TODO is it worth supporting other tymeds here?
472 // get the list of supported formats
473 size_t nFormats
= m_dataObject
->GetFormatCount(wxDataObject::Set
);
475 wxDataFormat
*formats
;
476 formats
= nFormats
== 1 ? &format
: new wxDataFormat
[nFormats
];
478 m_dataObject
->GetAllFormats(formats
, wxDataObject::Set
);
480 // cycle through all supported formats
482 for ( n
= 0; n
< nFormats
; n
++ ) {
483 s_fmtMemory
.cfFormat
= formats
[n
];
485 // NB: don't use SUCCEEDED macro here: QueryGetData returns S_FALSE
486 // for file drag and drop (format == CF_HDROP)
487 if ( pIDataSource
->QueryGetData(&s_fmtMemory
) == S_OK
) {
494 if ( formats
!= &format
) {
495 // free memory if we allocated it
499 return n
< nFormats
? format
: wxFormatInvalid
;
502 // ----------------------------------------------------------------------------
504 // ----------------------------------------------------------------------------
506 static wxDragResult
ConvertDragEffectToResult(DWORD dwEffect
)
508 switch ( dwEffect
) {
509 case DROPEFFECT_COPY
:
512 case DROPEFFECT_LINK
:
515 case DROPEFFECT_MOVE
:
519 wxFAIL_MSG(wxT("invalid value in ConvertDragEffectToResult"));
522 case DROPEFFECT_NONE
:
527 static DWORD
ConvertDragResultToEffect(wxDragResult result
)
531 return DROPEFFECT_COPY
;
534 return DROPEFFECT_LINK
;
537 return DROPEFFECT_MOVE
;
540 wxFAIL_MSG(wxT("invalid value in ConvertDragResultToEffect"));
544 return DROPEFFECT_NONE
;
548 #endif // wxUSE_OLE && wxUSE_DRAG_AND_DROP