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