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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "droptgt.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
27 #if defined(__BORLANDC__)
33 #if wxUSE_OLE && wxUSE_DRAG_AND_DROP
35 #include "wx/msw/private.h"
44 #if !defined(__GNUWIN32__) || wxUSE_NORLANDER_HEADERS
45 #if wxCHECK_W32API_VERSION( 1, 0 )
46 #include "wx/msw/wrapwin.h"
48 #include <shlobj.h> // for DROPFILES structure
56 #include "wx/msw/ole/oleutils.h"
58 // ----------------------------------------------------------------------------
59 // IDropTarget interface: forward all interesting things to wxDropTarget
60 // (the name is unfortunate, but wx_I_DropTarget is not at all the same thing
61 // as wxDropTarget which is 'public' class, while this one is private)
62 // ----------------------------------------------------------------------------
64 class wxIDropTarget
: public IDropTarget
67 wxIDropTarget(wxDropTarget
*p
);
68 virtual ~wxIDropTarget();
70 // accessors for wxDropTarget
71 void SetHwnd(HWND hwnd
) { m_hwnd
= hwnd
; }
73 // IDropTarget methods
74 STDMETHODIMP
DragEnter(LPDATAOBJECT
, DWORD
, POINTL
, LPDWORD
);
75 STDMETHODIMP
DragOver(DWORD
, POINTL
, LPDWORD
);
76 STDMETHODIMP
DragLeave();
77 STDMETHODIMP
Drop(LPDATAOBJECT
, DWORD
, POINTL
, LPDWORD
);
79 DECLARE_IUNKNOWN_METHODS
;
82 IDataObject
*m_pIDataObject
; // !NULL between DragEnter and DragLeave/Drop
83 wxDropTarget
*m_pTarget
; // the real target (we're just a proxy)
85 HWND m_hwnd
; // window we're associated with
87 // get default drop effect for given keyboard flags
88 static inline DWORD
GetDropEffect(DWORD flags
, wxDragResult defaultAction
);
90 DECLARE_NO_COPY_CLASS(wxIDropTarget
)
93 // ----------------------------------------------------------------------------
95 // ----------------------------------------------------------------------------
97 static wxDragResult
ConvertDragEffectToResult(DWORD dwEffect
);
98 static DWORD
ConvertDragResultToEffect(wxDragResult result
);
100 // ============================================================================
101 // wxIDropTarget implementation
102 // ============================================================================
104 // Name : static wxIDropTarget::GetDropEffect
105 // Purpose : determine the drop operation from keyboard/mouse state.
106 // Returns : DWORD combined from DROPEFFECT_xxx constants
107 // Params : [in] DWORD flags kbd & mouse flags as passed to
108 // IDropTarget methods
109 // Notes : We do "move" normally and "copy" if <Ctrl> is pressed,
110 // which is the standard behaviour (currently there is no
111 // way to redefine it)
112 DWORD
wxIDropTarget::GetDropEffect(DWORD flags
, wxDragResult defaultAction
)
114 if (defaultAction
== wxDragCopy
)
115 return flags
& MK_SHIFT
? DROPEFFECT_MOVE
: DROPEFFECT_COPY
;
116 return flags
& MK_CONTROL
? DROPEFFECT_COPY
: DROPEFFECT_MOVE
;
119 wxIDropTarget::wxIDropTarget(wxDropTarget
*pTarget
)
122 m_pIDataObject
= NULL
;
125 wxIDropTarget::~wxIDropTarget()
129 BEGIN_IID_TABLE(wxIDropTarget
)
134 IMPLEMENT_IUNKNOWN_METHODS(wxIDropTarget
)
136 // Name : wxIDropTarget::DragEnter
137 // Purpose : Called when the mouse enters the window (dragging something)
139 // Params : [in] IDataObject *pIDataSource : source data
140 // [in] DWORD grfKeyState : kbd & mouse state
141 // [in] POINTL pt : mouse coordinates
142 // [out]DWORD *pdwEffect : effect flag
144 STDMETHODIMP
wxIDropTarget::DragEnter(IDataObject
*pIDataSource
,
149 wxLogTrace(wxTRACE_OleCalls
, wxT("IDropTarget::DragEnter"));
151 wxASSERT_MSG( m_pIDataObject
== NULL
,
152 _T("drop target must have data object") );
154 // show the list of formats supported by the source data object for the
155 // debugging purposes, this is quite useful sometimes - please don't remove
157 IEnumFORMATETC
*penumFmt
;
158 if ( SUCCEEDED(pIDataSource
->EnumFormatEtc(DATADIR_GET
, &penumFmt
)) )
161 while ( penumFmt
->Next(1, &fmt
, NULL
) == S_OK
)
163 wxLogDebug(_T("Drop source supports format %s"),
164 wxDataObject::GetFormatName(fmt
.cfFormat
));
171 wxLogLastError(_T("IDataObject::EnumFormatEtc"));
175 if ( !m_pTarget
->IsAcceptedData(pIDataSource
) ) {
176 // we don't accept this kind of data
177 *pdwEffect
= DROPEFFECT_NONE
;
182 // get hold of the data object
183 m_pIDataObject
= pIDataSource
;
184 m_pIDataObject
->AddRef();
186 // we need client coordinates to pass to wxWin functions
187 if ( !ScreenToClient(m_hwnd
, (POINT
*)&pt
) )
189 wxLogLastError(wxT("ScreenToClient"));
192 // give some visual feedback
193 *pdwEffect
= ConvertDragResultToEffect(
194 m_pTarget
->OnEnter(pt
.x
, pt
.y
, ConvertDragEffectToResult(
195 GetDropEffect(grfKeyState
, m_pTarget
->GetDefaultAction()))
202 // Name : wxIDropTarget::DragOver
203 // Purpose : Indicates that the mouse was moved inside the window represented
204 // by this drop target.
206 // Params : [in] DWORD grfKeyState kbd & mouse state
207 // [in] POINTL pt mouse coordinates
208 // [out]LPDWORD pdwEffect effect flag
209 // Notes : We're called on every WM_MOUSEMOVE, so this function should be
211 STDMETHODIMP
wxIDropTarget::DragOver(DWORD grfKeyState
,
215 // there are too many of them... wxLogDebug("IDropTarget::DragOver");
218 if ( m_pIDataObject
) {
219 result
= ConvertDragEffectToResult(
220 GetDropEffect(grfKeyState
, m_pTarget
->GetDefaultAction()));
223 // can't accept data anyhow normally
227 // we need client coordinates to pass to wxWin functions
228 if ( !ScreenToClient(m_hwnd
, (POINT
*)&pt
) )
230 wxLogLastError(wxT("ScreenToClient"));
233 *pdwEffect
= ConvertDragResultToEffect(
234 m_pTarget
->OnDragOver(pt
.x
, pt
.y
, result
)
240 // Name : wxIDropTarget::DragLeave
241 // Purpose : Informs the drop target that the operation has left its window.
243 // Notes : good place to do any clean-up
244 STDMETHODIMP
wxIDropTarget::DragLeave()
246 wxLogTrace(wxTRACE_OleCalls
, wxT("IDropTarget::DragLeave"));
248 // remove the UI feedback
249 m_pTarget
->OnLeave();
251 // release the held object
252 RELEASE_AND_NULL(m_pIDataObject
);
257 // Name : wxIDropTarget::Drop
258 // Purpose : Instructs the drop target to paste data that was just now
261 // Params : [in] IDataObject *pIDataSource the data to paste
262 // [in] DWORD grfKeyState kbd & mouse state
263 // [in] POINTL pt where the drop occured?
264 // [ouy]DWORD *pdwEffect operation effect
266 STDMETHODIMP
wxIDropTarget::Drop(IDataObject
*pIDataSource
,
271 wxLogTrace(wxTRACE_OleCalls
, wxT("IDropTarget::Drop"));
273 // TODO I don't know why there is this parameter, but so far I assume
274 // that it's the same we've already got in DragEnter
275 wxASSERT( m_pIDataObject
== pIDataSource
);
277 // by default, nothing happens
278 *pdwEffect
= DROPEFFECT_NONE
;
280 // we need client coordinates to pass to wxWin functions
281 if ( !ScreenToClient(m_hwnd
, (POINT
*)&pt
) )
283 wxLogLastError(wxT("ScreenToClient"));
286 // first ask the drop target if it wants data
287 if ( m_pTarget
->OnDrop(pt
.x
, pt
.y
) ) {
288 // it does, so give it the data source
289 m_pTarget
->SetDataSource(pIDataSource
);
291 // and now it has the data
292 wxDragResult rc
= ConvertDragEffectToResult(
293 GetDropEffect(grfKeyState
, m_pTarget
->GetDefaultAction()));
294 rc
= m_pTarget
->OnData(pt
.x
, pt
.y
, rc
);
295 if ( wxIsDragResultOk(rc
) ) {
296 // operation succeeded
297 *pdwEffect
= ConvertDragResultToEffect(rc
);
299 //else: *pdwEffect is already DROPEFFECT_NONE
301 //else: OnDrop() returned false, no need to copy data
303 // release the held object
304 RELEASE_AND_NULL(m_pIDataObject
);
309 // ============================================================================
310 // wxDropTarget implementation
311 // ============================================================================
313 // ----------------------------------------------------------------------------
315 // ----------------------------------------------------------------------------
317 wxDropTarget::wxDropTarget(wxDataObject
*dataObj
)
318 : wxDropTargetBase(dataObj
)
320 // create an IDropTarget implementation which will notify us about d&d
322 m_pIDropTarget
= new wxIDropTarget(this);
323 m_pIDropTarget
->AddRef();
326 wxDropTarget::~wxDropTarget()
328 ReleaseInterface(m_pIDropTarget
);
331 // ----------------------------------------------------------------------------
332 // [un]register drop handler
333 // ----------------------------------------------------------------------------
335 bool wxDropTarget::Register(WXHWND hwnd
)
338 // RegisterDragDrop not available on Windows CE >= 400?
339 // Or maybe we can dynamically load them from ceshell.dll
341 #if defined(__WXWINCE__) && _WIN32_WCE >= 400
347 // May exist in later WinCE versions
349 hr
= ::CoLockObjectExternal(m_pIDropTarget
, TRUE
, FALSE
);
351 wxLogApiError(wxT("CoLockObjectExternal"), hr
);
356 hr
= ::RegisterDragDrop((HWND
) hwnd
, m_pIDropTarget
);
358 // May exist in later WinCE versions
360 ::CoLockObjectExternal(m_pIDropTarget
, FALSE
, FALSE
);
362 wxLogApiError(wxT("RegisterDragDrop"), hr
);
366 // we will need the window handle for coords transformation later
367 m_pIDropTarget
->SetHwnd((HWND
)hwnd
);
373 void wxDropTarget::Revoke(WXHWND hwnd
)
375 #if defined(__WXWINCE__) && _WIN32_WCE >= 400
376 // Not available, see note above
379 HRESULT hr
= ::RevokeDragDrop((HWND
) hwnd
);
382 wxLogApiError(wxT("RevokeDragDrop"), hr
);
385 // May exist in later WinCE versions
387 ::CoLockObjectExternal(m_pIDropTarget
, FALSE
, TRUE
);
390 m_pIDropTarget
->SetHwnd(0);
394 // ----------------------------------------------------------------------------
395 // base class pure virtuals
396 // ----------------------------------------------------------------------------
398 // OnDrop() is called only if we previously returned true from
399 // IsAcceptedData(), so no need to check anything here
400 bool wxDropTarget::OnDrop(wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
))
405 // copy the data from the data source to the target data object
406 bool wxDropTarget::GetData()
408 wxDataFormat format
= GetSupportedFormat(m_pIDataSource
);
409 if ( format
== wxDF_INVALID
) {
410 // this is strange because IsAcceptedData() succeeded previously!
411 wxFAIL_MSG(wxT("strange - did supported formats list change?"));
418 fmtMemory
.cfFormat
= format
;
419 fmtMemory
.ptd
= NULL
;
420 fmtMemory
.dwAspect
= DVASPECT_CONTENT
;
421 fmtMemory
.lindex
= -1;
422 fmtMemory
.tymed
= TYMED_HGLOBAL
; // TODO to add other media
426 HRESULT hr
= m_pIDataSource
->GetData(&fmtMemory
, &stm
);
427 if ( SUCCEEDED(hr
) ) {
428 IDataObject
*dataObject
= m_dataObject
->GetInterface();
430 hr
= dataObject
->SetData(&fmtMemory
, &stm
, TRUE
);
431 if ( SUCCEEDED(hr
) ) {
435 wxLogApiError(wxT("IDataObject::SetData()"), hr
);
439 wxLogApiError(wxT("IDataObject::GetData()"), hr
);
445 // ----------------------------------------------------------------------------
446 // callbacks used by wxIDropTarget
447 // ----------------------------------------------------------------------------
449 // we need a data source, so wxIDropTarget gives it to us using this function
450 void wxDropTarget::SetDataSource(IDataObject
*pIDataSource
)
452 m_pIDataSource
= pIDataSource
;
455 // determine if we accept data of this type
456 bool wxDropTarget::IsAcceptedData(IDataObject
*pIDataSource
) const
458 return GetSupportedFormat(pIDataSource
) != wxDF_INVALID
;
461 // ----------------------------------------------------------------------------
463 // ----------------------------------------------------------------------------
465 wxDataFormat
wxDropTarget::GetSupportedFormat(IDataObject
*pIDataSource
) const
467 // this strucutre describes a data of any type (first field will be
468 // changing) being passed through global memory block.
469 static FORMATETC s_fmtMemory
= {
474 TYMED_HGLOBAL
// TODO is it worth supporting other tymeds here?
477 // get the list of supported formats
478 size_t nFormats
= m_dataObject
->GetFormatCount(wxDataObject::Set
);
480 wxDataFormat
*formats
;
481 formats
= nFormats
== 1 ? &format
: new wxDataFormat
[nFormats
];
483 m_dataObject
->GetAllFormats(formats
, wxDataObject::Set
);
485 // cycle through all supported formats
487 for ( n
= 0; n
< nFormats
; n
++ ) {
488 s_fmtMemory
.cfFormat
= formats
[n
];
490 // NB: don't use SUCCEEDED macro here: QueryGetData returns S_FALSE
491 // for file drag and drop (format == CF_HDROP)
492 if ( pIDataSource
->QueryGetData(&s_fmtMemory
) == S_OK
) {
499 if ( formats
!= &format
) {
500 // free memory if we allocated it
504 return n
< nFormats
? format
: wxFormatInvalid
;
507 // ----------------------------------------------------------------------------
509 // ----------------------------------------------------------------------------
511 static wxDragResult
ConvertDragEffectToResult(DWORD dwEffect
)
513 switch ( dwEffect
) {
514 case DROPEFFECT_COPY
:
517 case DROPEFFECT_LINK
:
520 case DROPEFFECT_MOVE
:
524 wxFAIL_MSG(wxT("invalid value in ConvertDragEffectToResult"));
527 case DROPEFFECT_NONE
:
532 static DWORD
ConvertDragResultToEffect(wxDragResult result
)
536 return DROPEFFECT_COPY
;
539 return DROPEFFECT_LINK
;
542 return DROPEFFECT_MOVE
;
545 wxFAIL_MSG(wxT("invalid value in ConvertDragResultToEffect"));
549 return DROPEFFECT_NONE
;
554 // wxUSE_DRAG_AND_DROP