]> git.saurik.com Git - wxWidgets.git/blame - src/msw/ole/droptgt.cpp
make wxSemaphore::Post() return wxSEMA_OVERFLOW as documented (patch 1557642)
[wxWidgets.git] / src / msw / ole / droptgt.cpp
CommitLineData
bbf1f0e5 1///////////////////////////////////////////////////////////////////////////////
521bf4ff 2// Name: src/msw/ole/droptgt.cpp
bbf1f0e5
KB
3// Purpose: wxDropTarget implementation
4// Author: Vadim Zeitlin
3f480da3
VZ
5// Modified by:
6// Created:
bbf1f0e5
KB
7// RCS-ID: $Id$
8// Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
65571936 9// Licence: wxWindows licence
bbf1f0e5
KB
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// Declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
bbf1f0e5 20// For compilers that support precompilation, includes "wx.h".
bbf1f0e5
KB
21#include "wx/wxprec.h"
22
23#if defined(__BORLANDC__)
e4db172a 24 #pragma hdrstop
bbf1f0e5
KB
25#endif
26
21709999 27#if wxUSE_OLE && wxUSE_DRAG_AND_DROP
bbf1f0e5 28
e4db172a 29#ifndef WX_PRECOMP
57bd4c60 30 #include "wx/msw/wrapwin.h"
e4db172a
WS
31 #include "wx/log.h"
32#endif
33
4676948b 34#include "wx/msw/private.h"
bbf1f0e5 35
4676948b
JS
36#ifdef __WXWINCE__
37 #include <winreg.h>
38 #include <ole2.h>
39#endif
40
bbf1f0e5 41#ifdef __WIN32__
b64f0a5f 42 #if !defined(__GNUWIN32__) || wxUSE_NORLANDER_HEADERS
3f480da3
VZ
43 #include <shlobj.h> // for DROPFILES structure
44 #endif
bbf1f0e5 45#else
3f480da3 46 #include <shellapi.h>
bbf1f0e5
KB
47#endif
48
9e2896e5 49#include "wx/dnd.h"
bbf1f0e5 50
3f480da3 51#include "wx/msw/ole/oleutils.h"
bbf1f0e5
KB
52
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// ----------------------------------------------------------------------------
58
59class wxIDropTarget : public IDropTarget
60{
61public:
8ee9d618 62 wxIDropTarget(wxDropTarget *p);
33ac7e6f 63 virtual ~wxIDropTarget();
bbf1f0e5 64
8ee9d618
VZ
65 // accessors for wxDropTarget
66 void SetHwnd(HWND hwnd) { m_hwnd = hwnd; }
bbf1f0e5 67
8ee9d618
VZ
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);
73
74 DECLARE_IUNKNOWN_METHODS;
bbf1f0e5
KB
75
76protected:
8ee9d618
VZ
77 IDataObject *m_pIDataObject; // !NULL between DragEnter and DragLeave/Drop
78 wxDropTarget *m_pTarget; // the real target (we're just a proxy)
79
80 HWND m_hwnd; // window we're associated with
bbf1f0e5 81
8ee9d618 82 // get default drop effect for given keyboard flags
917ae499 83 static inline DWORD GetDropEffect(DWORD flags, wxDragResult defaultAction);
22f3361e
VZ
84
85 DECLARE_NO_COPY_CLASS(wxIDropTarget)
bbf1f0e5
KB
86};
87
c9057ae1
VZ
88// ----------------------------------------------------------------------------
89// private functions
90// ----------------------------------------------------------------------------
91
92static wxDragResult ConvertDragEffectToResult(DWORD dwEffect);
93static DWORD ConvertDragResultToEffect(wxDragResult result);
94
bbf1f0e5
KB
95// ============================================================================
96// wxIDropTarget implementation
97// ============================================================================
98
8ee9d618 99// Name : static wxIDropTarget::GetDropEffect
bbf1f0e5 100// Purpose : determine the drop operation from keyboard/mouse state.
3f480da3 101// Returns : DWORD combined from DROPEFFECT_xxx constants
bbf1f0e5
KB
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,
3f480da3 105// which is the standard behaviour (currently there is no
bbf1f0e5 106// way to redefine it)
917ae499 107DWORD wxIDropTarget::GetDropEffect(DWORD flags, wxDragResult defaultAction)
bbf1f0e5 108{
917ae499
RR
109 if (defaultAction == wxDragCopy)
110 return flags & MK_SHIFT ? DROPEFFECT_MOVE : DROPEFFECT_COPY;
bbf1f0e5
KB
111 return flags & MK_CONTROL ? DROPEFFECT_COPY : DROPEFFECT_MOVE;
112}
113
114wxIDropTarget::wxIDropTarget(wxDropTarget *pTarget)
3f480da3 115{
bbf1f0e5 116 m_pTarget = pTarget;
3f480da3 117 m_pIDataObject = NULL;
bbf1f0e5
KB
118}
119
3f480da3
VZ
120wxIDropTarget::~wxIDropTarget()
121{
bbf1f0e5
KB
122}
123
124BEGIN_IID_TABLE(wxIDropTarget)
125 ADD_IID(Unknown)
126 ADD_IID(DropTarget)
127END_IID_TABLE;
128
129IMPLEMENT_IUNKNOWN_METHODS(wxIDropTarget)
130
bbf1f0e5
KB
131// Name : wxIDropTarget::DragEnter
132// Purpose : Called when the mouse enters the window (dragging something)
133// Returns : S_OK
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
3f480da3 138// Notes :
bbf1f0e5
KB
139STDMETHODIMP wxIDropTarget::DragEnter(IDataObject *pIDataSource,
140 DWORD grfKeyState,
141 POINTL pt,
142 DWORD *pdwEffect)
143{
c5639a87 144 wxLogTrace(wxTRACE_OleCalls, wxT("IDropTarget::DragEnter"));
bbf1f0e5 145
c5639a87
VZ
146 wxASSERT_MSG( m_pIDataObject == NULL,
147 _T("drop target must have data object") );
148
149 // show the list of formats supported by the source data object for the
444ad3a7 150 // debugging purposes, this is quite useful sometimes - please don't remove
c5639a87
VZ
151#if 0
152 IEnumFORMATETC *penumFmt;
153 if ( SUCCEEDED(pIDataSource->EnumFormatEtc(DATADIR_GET, &penumFmt)) )
154 {
155 FORMATETC fmt;
156 while ( penumFmt->Next(1, &fmt, NULL) == S_OK )
157 {
158 wxLogDebug(_T("Drop source supports format %s"),
159 wxDataObject::GetFormatName(fmt.cfFormat));
160 }
161
162 penumFmt->Release();
163 }
164 else
165 {
166 wxLogLastError(_T("IDataObject::EnumFormatEtc"));
167 }
168#endif // 0
bbf1f0e5 169
8ee9d618
VZ
170 if ( !m_pTarget->IsAcceptedData(pIDataSource) ) {
171 // we don't accept this kind of data
172 *pdwEffect = DROPEFFECT_NONE;
bbf1f0e5 173
8ee9d618
VZ
174 return S_OK;
175 }
bbf1f0e5 176
8ee9d618
VZ
177 // get hold of the data object
178 m_pIDataObject = pIDataSource;
179 m_pIDataObject->AddRef();
bbf1f0e5 180
8ee9d618
VZ
181 // we need client coordinates to pass to wxWin functions
182 if ( !ScreenToClient(m_hwnd, (POINT *)&pt) )
183 {
f6bcfd97 184 wxLogLastError(wxT("ScreenToClient"));
8ee9d618 185 }
bbf1f0e5 186
8ee9d618
VZ
187 // give some visual feedback
188 *pdwEffect = ConvertDragResultToEffect(
917ae499
RR
189 m_pTarget->OnEnter(pt.x, pt.y, ConvertDragEffectToResult(
190 GetDropEffect(grfKeyState, m_pTarget->GetDefaultAction()))
8ee9d618
VZ
191 )
192 );
193
194 return S_OK;
bbf1f0e5
KB
195}
196
197// Name : wxIDropTarget::DragOver
198// Purpose : Indicates that the mouse was moved inside the window represented
199// by this drop target.
200// Returns : S_OK
201// Params : [in] DWORD grfKeyState kbd & mouse state
202// [in] POINTL pt mouse coordinates
203// [out]LPDWORD pdwEffect effect flag
3f480da3 204// Notes : We're called on every WM_MOUSEMOVE, so this function should be
bbf1f0e5
KB
205// very efficient.
206STDMETHODIMP wxIDropTarget::DragOver(DWORD grfKeyState,
207 POINTL pt,
208 LPDWORD pdwEffect)
209{
8ee9d618 210 // there are too many of them... wxLogDebug("IDropTarget::DragOver");
bbf1f0e5 211
8ee9d618
VZ
212 wxDragResult result;
213 if ( m_pIDataObject ) {
917ae499
RR
214 result = ConvertDragEffectToResult(
215 GetDropEffect(grfKeyState, m_pTarget->GetDefaultAction()));
8ee9d618
VZ
216 }
217 else {
218 // can't accept data anyhow normally
219 result = wxDragNone;
220 }
c9057ae1 221
8ee9d618
VZ
222 // we need client coordinates to pass to wxWin functions
223 if ( !ScreenToClient(m_hwnd, (POINT *)&pt) )
224 {
f6bcfd97 225 wxLogLastError(wxT("ScreenToClient"));
8ee9d618 226 }
c9057ae1 227
8ee9d618
VZ
228 *pdwEffect = ConvertDragResultToEffect(
229 m_pTarget->OnDragOver(pt.x, pt.y, result)
230 );
231
232 return S_OK;
bbf1f0e5
KB
233}
234
235// Name : wxIDropTarget::DragLeave
236// Purpose : Informs the drop target that the operation has left its window.
237// Returns : S_OK
238// Notes : good place to do any clean-up
239STDMETHODIMP wxIDropTarget::DragLeave()
240{
c5639a87 241 wxLogTrace(wxTRACE_OleCalls, wxT("IDropTarget::DragLeave"));
bbf1f0e5
KB
242
243 // remove the UI feedback
244 m_pTarget->OnLeave();
245
246 // release the held object
247 RELEASE_AND_NULL(m_pIDataObject);
3f480da3 248
bbf1f0e5
KB
249 return S_OK;
250}
251
252// Name : wxIDropTarget::Drop
3f480da3 253// Purpose : Instructs the drop target to paste data that was just now
bbf1f0e5
KB
254// dropped on it.
255// Returns : S_OK
256// Params : [in] IDataObject *pIDataSource the data to paste
257// [in] DWORD grfKeyState kbd & mouse state
d6922577 258// [in] POINTL pt where the drop occurred?
bbf1f0e5 259// [ouy]DWORD *pdwEffect operation effect
3f480da3
VZ
260// Notes :
261STDMETHODIMP wxIDropTarget::Drop(IDataObject *pIDataSource,
262 DWORD grfKeyState,
263 POINTL pt,
bbf1f0e5
KB
264 DWORD *pdwEffect)
265{
c5639a87 266 wxLogTrace(wxTRACE_OleCalls, wxT("IDropTarget::Drop"));
9e2896e5
VZ
267
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 );
271
272 // by default, nothing happens
273 *pdwEffect = DROPEFFECT_NONE;
274
8ee9d618
VZ
275 // we need client coordinates to pass to wxWin functions
276 if ( !ScreenToClient(m_hwnd, (POINT *)&pt) )
277 {
f6bcfd97 278 wxLogLastError(wxT("ScreenToClient"));
8ee9d618
VZ
279 }
280
9e2896e5
VZ
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);
285
286 // and now it has the data
917ae499
RR
287 wxDragResult rc = ConvertDragEffectToResult(
288 GetDropEffect(grfKeyState, m_pTarget->GetDefaultAction()));
87a1e308 289 rc = m_pTarget->OnData(pt.x, pt.y, rc);
8ee9d618 290 if ( wxIsDragResultOk(rc) ) {
9e2896e5 291 // operation succeeded
8ee9d618 292 *pdwEffect = ConvertDragResultToEffect(rc);
9e2896e5
VZ
293 }
294 //else: *pdwEffect is already DROPEFFECT_NONE
bbf1f0e5 295 }
0a0e6a5b 296 //else: OnDrop() returned false, no need to copy data
bbf1f0e5 297
9e2896e5
VZ
298 // release the held object
299 RELEASE_AND_NULL(m_pIDataObject);
bbf1f0e5 300
9e2896e5 301 return S_OK;
bbf1f0e5
KB
302}
303
304// ============================================================================
305// wxDropTarget implementation
306// ============================================================================
307
308// ----------------------------------------------------------------------------
309// ctor/dtor
310// ----------------------------------------------------------------------------
311
9e2896e5
VZ
312wxDropTarget::wxDropTarget(wxDataObject *dataObj)
313 : wxDropTargetBase(dataObj)
bbf1f0e5 314{
9e2896e5
VZ
315 // create an IDropTarget implementation which will notify us about d&d
316 // operations.
317 m_pIDropTarget = new wxIDropTarget(this);
318 m_pIDropTarget->AddRef();
bbf1f0e5
KB
319}
320
321wxDropTarget::~wxDropTarget()
322{
9e2896e5 323 ReleaseInterface(m_pIDropTarget);
bbf1f0e5
KB
324}
325
326// ----------------------------------------------------------------------------
327// [un]register drop handler
328// ----------------------------------------------------------------------------
329
330bool wxDropTarget::Register(WXHWND hwnd)
331{
a40b3f5d
JS
332 // FIXME
333 // RegisterDragDrop not available on Windows CE >= 400?
334 // Or maybe we can dynamically load them from ceshell.dll
335 // or similar.
336#if defined(__WXWINCE__) && _WIN32_WCE >= 400
abf912c5 337 wxUnusedVar(hwnd);
0a0e6a5b 338 return false;
a40b3f5d 339#else
4cb88a72
JS
340 HRESULT hr;
341
342 // May exist in later WinCE versions
343#ifndef __WXWINCE__
344 hr = ::CoLockObjectExternal(m_pIDropTarget, TRUE, FALSE);
9e2896e5 345 if ( FAILED(hr) ) {
161f4f73 346 wxLogApiError(wxT("CoLockObjectExternal"), hr);
0a0e6a5b 347 return false;
9e2896e5 348 }
4cb88a72 349#endif
bbf1f0e5 350
9e2896e5
VZ
351 hr = ::RegisterDragDrop((HWND) hwnd, m_pIDropTarget);
352 if ( FAILED(hr) ) {
4cb88a72
JS
353 // May exist in later WinCE versions
354#ifndef __WXWINCE__
9e2896e5 355 ::CoLockObjectExternal(m_pIDropTarget, FALSE, FALSE);
4cb88a72 356#endif
161f4f73 357 wxLogApiError(wxT("RegisterDragDrop"), hr);
0a0e6a5b 358 return false;
9e2896e5 359 }
bbf1f0e5 360
8ee9d618
VZ
361 // we will need the window handle for coords transformation later
362 m_pIDropTarget->SetHwnd((HWND)hwnd);
363
0a0e6a5b 364 return true;
a40b3f5d 365#endif
bbf1f0e5
KB
366}
367
368void wxDropTarget::Revoke(WXHWND hwnd)
369{
a40b3f5d
JS
370#if defined(__WXWINCE__) && _WIN32_WCE >= 400
371 // Not available, see note above
abf912c5 372 wxUnusedVar(hwnd);
a40b3f5d 373#else
9e2896e5 374 HRESULT hr = ::RevokeDragDrop((HWND) hwnd);
bbf1f0e5 375
9e2896e5 376 if ( FAILED(hr) ) {
161f4f73 377 wxLogApiError(wxT("RevokeDragDrop"), hr);
9e2896e5 378 }
bbf1f0e5 379
4cb88a72
JS
380 // May exist in later WinCE versions
381#ifndef __WXWINCE__
9e2896e5 382 ::CoLockObjectExternal(m_pIDropTarget, FALSE, TRUE);
4cb88a72 383#endif
8ee9d618
VZ
384
385 m_pIDropTarget->SetHwnd(0);
a40b3f5d 386#endif
bbf1f0e5
KB
387}
388
389// ----------------------------------------------------------------------------
9e2896e5 390// base class pure virtuals
bbf1f0e5 391// ----------------------------------------------------------------------------
9e2896e5 392
0a0e6a5b 393// OnDrop() is called only if we previously returned true from
9e2896e5
VZ
394// IsAcceptedData(), so no need to check anything here
395bool wxDropTarget::OnDrop(wxCoord WXUNUSED(x), wxCoord WXUNUSED(y))
396{
0a0e6a5b 397 return true;
9e2896e5
VZ
398}
399
400// copy the data from the data source to the target data object
401bool wxDropTarget::GetData()
bbf1f0e5 402{
9e2896e5
VZ
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?"));
407
0a0e6a5b 408 return false;
9e2896e5
VZ
409 }
410
411 STGMEDIUM stm;
412 FORMATETC fmtMemory;
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
418
0a0e6a5b 419 bool rc = false;
9e2896e5
VZ
420
421 HRESULT hr = m_pIDataSource->GetData(&fmtMemory, &stm);
422 if ( SUCCEEDED(hr) ) {
423 IDataObject *dataObject = m_dataObject->GetInterface();
424
425 hr = dataObject->SetData(&fmtMemory, &stm, TRUE);
426 if ( SUCCEEDED(hr) ) {
0a0e6a5b 427 rc = true;
9e2896e5
VZ
428 }
429 else {
444ad3a7 430 wxLogApiError(wxT("IDataObject::SetData()"), hr);
9e2896e5
VZ
431 }
432 }
433 else {
444ad3a7 434 wxLogApiError(wxT("IDataObject::GetData()"), hr);
bbf1f0e5 435 }
bbf1f0e5 436
9e2896e5 437 return rc;
bbf1f0e5
KB
438}
439
9e2896e5
VZ
440// ----------------------------------------------------------------------------
441// callbacks used by wxIDropTarget
442// ----------------------------------------------------------------------------
bbf1f0e5 443
9e2896e5
VZ
444// we need a data source, so wxIDropTarget gives it to us using this function
445void wxDropTarget::SetDataSource(IDataObject *pIDataSource)
bbf1f0e5 446{
9e2896e5 447 m_pIDataSource = pIDataSource;
bbf1f0e5
KB
448}
449
9e2896e5
VZ
450// determine if we accept data of this type
451bool wxDropTarget::IsAcceptedData(IDataObject *pIDataSource) const
bbf1f0e5 452{
9e2896e5 453 return GetSupportedFormat(pIDataSource) != wxDF_INVALID;
bbf1f0e5
KB
454}
455
9e2896e5
VZ
456// ----------------------------------------------------------------------------
457// helper functions
458// ----------------------------------------------------------------------------
459
460wxDataFormat wxDropTarget::GetSupportedFormat(IDataObject *pIDataSource) const
bbf1f0e5 461{
9e2896e5
VZ
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 = {
465 0,
466 NULL,
467 DVASPECT_CONTENT,
468 -1,
469 TYMED_HGLOBAL // TODO is it worth supporting other tymeds here?
470 };
471
472 // get the list of supported formats
473 size_t nFormats = m_dataObject->GetFormatCount(wxDataObject::Set);
33ac7e6f 474 wxDataFormat format;
c5639a87 475 wxDataFormat *formats;
9e2896e5
VZ
476 formats = nFormats == 1 ? &format : new wxDataFormat[nFormats];
477
478 m_dataObject->GetAllFormats(formats, wxDataObject::Set);
479
480 // cycle through all supported formats
481 size_t n;
482 for ( n = 0; n < nFormats; n++ ) {
483 s_fmtMemory.cfFormat = formats[n];
484
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 ) {
488 format = formats[n];
489
490 break;
491 }
492 }
493
494 if ( formats != &format ) {
495 // free memory if we allocated it
496 delete [] formats;
497 }
498
90bc25c7 499 return n < nFormats ? format : wxFormatInvalid;
bbf1f0e5
KB
500}
501
c9057ae1
VZ
502// ----------------------------------------------------------------------------
503// private functions
504// ----------------------------------------------------------------------------
505
506static wxDragResult ConvertDragEffectToResult(DWORD dwEffect)
507{
508 switch ( dwEffect ) {
509 case DROPEFFECT_COPY:
510 return wxDragCopy;
511
e6d318c2
RD
512 case DROPEFFECT_LINK:
513 return wxDragLink;
514
c9057ae1
VZ
515 case DROPEFFECT_MOVE:
516 return wxDragMove;
517
518 default:
519 wxFAIL_MSG(wxT("invalid value in ConvertDragEffectToResult"));
520 // fall through
521
522 case DROPEFFECT_NONE:
523 return wxDragNone;
524 }
525}
526
527static DWORD ConvertDragResultToEffect(wxDragResult result)
528{
529 switch ( result ) {
530 case wxDragCopy:
531 return DROPEFFECT_COPY;
532
e6d318c2
RD
533 case wxDragLink:
534 return DROPEFFECT_LINK;
535
c9057ae1
VZ
536 case wxDragMove:
537 return DROPEFFECT_MOVE;
538
539 default:
540 wxFAIL_MSG(wxT("invalid value in ConvertDragResultToEffect"));
541 // fall through
542
543 case wxDragNone:
544 return DROPEFFECT_NONE;
545 }
546}
547
e4db172a 548#endif // wxUSE_OLE && wxUSE_DRAG_AND_DROP