]> git.saurik.com Git - wxWidgets.git/blame - src/msw/ole/droptgt.cpp
scrolling to selection
[wxWidgets.git] / src / msw / ole / droptgt.cpp
CommitLineData
bbf1f0e5
KB
1///////////////////////////////////////////////////////////////////////////////
2// Name: ole/droptgt.cpp
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>
6c9a19aa 9// Licence: wxWindows licence
bbf1f0e5
KB
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// Declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
14f355c2 20#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
bbf1f0e5
KB
21#pragma implementation "droptgt.h"
22#endif
23
24// For compilers that support precompilation, includes "wx.h".
bbf1f0e5
KB
25#include "wx/wxprec.h"
26
27#if defined(__BORLANDC__)
28#pragma hdrstop
29#endif
30
3f480da3 31#include "wx/setup.h"
bbf1f0e5 32
21709999 33#if wxUSE_OLE && wxUSE_DRAG_AND_DROP
bbf1f0e5 34
4676948b 35#include "wx/msw/private.h"
3f480da3 36#include "wx/log.h"
bbf1f0e5 37
4676948b
JS
38#ifdef __WXWINCE__
39 #include <winreg.h>
40 #include <ole2.h>
41#endif
42
bbf1f0e5 43#ifdef __WIN32__
b64f0a5f 44 #if !defined(__GNUWIN32__) || wxUSE_NORLANDER_HEADERS
7f017c64 45 #if wxCHECK_W32API_VERSION( 1, 0 )
9ed0d735 46 #include "wx/msw/wrapwin.h"
7f017c64 47 #endif
3f480da3
VZ
48 #include <shlobj.h> // for DROPFILES structure
49 #endif
bbf1f0e5 50#else
3f480da3 51 #include <shellapi.h>
bbf1f0e5
KB
52#endif
53
9e2896e5 54#include "wx/dnd.h"
bbf1f0e5 55
3f480da3 56#include "wx/msw/ole/oleutils.h"
bbf1f0e5
KB
57
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// ----------------------------------------------------------------------------
63
64class wxIDropTarget : public IDropTarget
65{
66public:
8ee9d618 67 wxIDropTarget(wxDropTarget *p);
33ac7e6f 68 virtual ~wxIDropTarget();
bbf1f0e5 69
8ee9d618
VZ
70 // accessors for wxDropTarget
71 void SetHwnd(HWND hwnd) { m_hwnd = hwnd; }
bbf1f0e5 72
8ee9d618
VZ
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);
78
79 DECLARE_IUNKNOWN_METHODS;
bbf1f0e5
KB
80
81protected:
8ee9d618
VZ
82 IDataObject *m_pIDataObject; // !NULL between DragEnter and DragLeave/Drop
83 wxDropTarget *m_pTarget; // the real target (we're just a proxy)
84
85 HWND m_hwnd; // window we're associated with
bbf1f0e5 86
8ee9d618
VZ
87 // get default drop effect for given keyboard flags
88 static inline DWORD GetDropEffect(DWORD flags);
22f3361e
VZ
89
90 DECLARE_NO_COPY_CLASS(wxIDropTarget)
bbf1f0e5
KB
91};
92
c9057ae1
VZ
93// ----------------------------------------------------------------------------
94// private functions
95// ----------------------------------------------------------------------------
96
97static wxDragResult ConvertDragEffectToResult(DWORD dwEffect);
98static DWORD ConvertDragResultToEffect(wxDragResult result);
99
bbf1f0e5
KB
100// ============================================================================
101// wxIDropTarget implementation
102// ============================================================================
103
8ee9d618 104// Name : static wxIDropTarget::GetDropEffect
bbf1f0e5 105// Purpose : determine the drop operation from keyboard/mouse state.
3f480da3 106// Returns : DWORD combined from DROPEFFECT_xxx constants
bbf1f0e5
KB
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,
3f480da3 110// which is the standard behaviour (currently there is no
bbf1f0e5
KB
111// way to redefine it)
112DWORD wxIDropTarget::GetDropEffect(DWORD flags)
113{
114 return flags & MK_CONTROL ? DROPEFFECT_COPY : DROPEFFECT_MOVE;
115}
116
117wxIDropTarget::wxIDropTarget(wxDropTarget *pTarget)
3f480da3 118{
bbf1f0e5 119 m_pTarget = pTarget;
3f480da3 120 m_pIDataObject = NULL;
bbf1f0e5
KB
121}
122
3f480da3
VZ
123wxIDropTarget::~wxIDropTarget()
124{
bbf1f0e5
KB
125}
126
127BEGIN_IID_TABLE(wxIDropTarget)
128 ADD_IID(Unknown)
129 ADD_IID(DropTarget)
130END_IID_TABLE;
131
132IMPLEMENT_IUNKNOWN_METHODS(wxIDropTarget)
133
bbf1f0e5
KB
134// Name : wxIDropTarget::DragEnter
135// Purpose : Called when the mouse enters the window (dragging something)
136// Returns : S_OK
137// Params : [in] IDataObject *pIDataSource : source data
138// [in] DWORD grfKeyState : kbd & mouse state
139// [in] POINTL pt : mouse coordinates
140// [out]DWORD *pdwEffect : effect flag
3f480da3 141// Notes :
bbf1f0e5
KB
142STDMETHODIMP wxIDropTarget::DragEnter(IDataObject *pIDataSource,
143 DWORD grfKeyState,
144 POINTL pt,
145 DWORD *pdwEffect)
146{
c5639a87 147 wxLogTrace(wxTRACE_OleCalls, wxT("IDropTarget::DragEnter"));
bbf1f0e5 148
c5639a87
VZ
149 wxASSERT_MSG( m_pIDataObject == NULL,
150 _T("drop target must have data object") );
151
152 // show the list of formats supported by the source data object for the
444ad3a7 153 // debugging purposes, this is quite useful sometimes - please don't remove
c5639a87
VZ
154#if 0
155 IEnumFORMATETC *penumFmt;
156 if ( SUCCEEDED(pIDataSource->EnumFormatEtc(DATADIR_GET, &penumFmt)) )
157 {
158 FORMATETC fmt;
159 while ( penumFmt->Next(1, &fmt, NULL) == S_OK )
160 {
161 wxLogDebug(_T("Drop source supports format %s"),
162 wxDataObject::GetFormatName(fmt.cfFormat));
163 }
164
165 penumFmt->Release();
166 }
167 else
168 {
169 wxLogLastError(_T("IDataObject::EnumFormatEtc"));
170 }
171#endif // 0
bbf1f0e5 172
8ee9d618
VZ
173 if ( !m_pTarget->IsAcceptedData(pIDataSource) ) {
174 // we don't accept this kind of data
175 *pdwEffect = DROPEFFECT_NONE;
bbf1f0e5 176
8ee9d618
VZ
177 return S_OK;
178 }
bbf1f0e5 179
8ee9d618
VZ
180 // get hold of the data object
181 m_pIDataObject = pIDataSource;
182 m_pIDataObject->AddRef();
bbf1f0e5 183
8ee9d618
VZ
184 // we need client coordinates to pass to wxWin functions
185 if ( !ScreenToClient(m_hwnd, (POINT *)&pt) )
186 {
f6bcfd97 187 wxLogLastError(wxT("ScreenToClient"));
8ee9d618 188 }
bbf1f0e5 189
8ee9d618
VZ
190 // give some visual feedback
191 *pdwEffect = ConvertDragResultToEffect(
192 m_pTarget->OnEnter(pt.x, pt.y,
193 ConvertDragEffectToResult(GetDropEffect(grfKeyState))
194 )
195 );
196
197 return S_OK;
bbf1f0e5
KB
198}
199
200// Name : wxIDropTarget::DragOver
201// Purpose : Indicates that the mouse was moved inside the window represented
202// by this drop target.
203// Returns : S_OK
204// Params : [in] DWORD grfKeyState kbd & mouse state
205// [in] POINTL pt mouse coordinates
206// [out]LPDWORD pdwEffect effect flag
3f480da3 207// Notes : We're called on every WM_MOUSEMOVE, so this function should be
bbf1f0e5
KB
208// very efficient.
209STDMETHODIMP wxIDropTarget::DragOver(DWORD grfKeyState,
210 POINTL pt,
211 LPDWORD pdwEffect)
212{
8ee9d618 213 // there are too many of them... wxLogDebug("IDropTarget::DragOver");
bbf1f0e5 214
8ee9d618
VZ
215 wxDragResult result;
216 if ( m_pIDataObject ) {
217 result = ConvertDragEffectToResult(GetDropEffect(grfKeyState));
218 }
219 else {
220 // can't accept data anyhow normally
221 result = wxDragNone;
222 }
c9057ae1 223
8ee9d618
VZ
224 // we need client coordinates to pass to wxWin functions
225 if ( !ScreenToClient(m_hwnd, (POINT *)&pt) )
226 {
f6bcfd97 227 wxLogLastError(wxT("ScreenToClient"));
8ee9d618 228 }
c9057ae1 229
8ee9d618
VZ
230 *pdwEffect = ConvertDragResultToEffect(
231 m_pTarget->OnDragOver(pt.x, pt.y, result)
232 );
233
234 return S_OK;
bbf1f0e5
KB
235}
236
237// Name : wxIDropTarget::DragLeave
238// Purpose : Informs the drop target that the operation has left its window.
239// Returns : S_OK
240// Notes : good place to do any clean-up
241STDMETHODIMP wxIDropTarget::DragLeave()
242{
c5639a87 243 wxLogTrace(wxTRACE_OleCalls, wxT("IDropTarget::DragLeave"));
bbf1f0e5
KB
244
245 // remove the UI feedback
246 m_pTarget->OnLeave();
247
248 // release the held object
249 RELEASE_AND_NULL(m_pIDataObject);
3f480da3 250
bbf1f0e5
KB
251 return S_OK;
252}
253
254// Name : wxIDropTarget::Drop
3f480da3 255// Purpose : Instructs the drop target to paste data that was just now
bbf1f0e5
KB
256// dropped on it.
257// Returns : S_OK
258// Params : [in] IDataObject *pIDataSource the data to paste
259// [in] DWORD grfKeyState kbd & mouse state
260// [in] POINTL pt where the drop occured?
261// [ouy]DWORD *pdwEffect operation effect
3f480da3
VZ
262// Notes :
263STDMETHODIMP wxIDropTarget::Drop(IDataObject *pIDataSource,
264 DWORD grfKeyState,
265 POINTL pt,
bbf1f0e5
KB
266 DWORD *pdwEffect)
267{
c5639a87 268 wxLogTrace(wxTRACE_OleCalls, wxT("IDropTarget::Drop"));
9e2896e5
VZ
269
270 // TODO I don't know why there is this parameter, but so far I assume
271 // that it's the same we've already got in DragEnter
272 wxASSERT( m_pIDataObject == pIDataSource );
273
274 // by default, nothing happens
275 *pdwEffect = DROPEFFECT_NONE;
276
8ee9d618
VZ
277 // we need client coordinates to pass to wxWin functions
278 if ( !ScreenToClient(m_hwnd, (POINT *)&pt) )
279 {
f6bcfd97 280 wxLogLastError(wxT("ScreenToClient"));
8ee9d618
VZ
281 }
282
9e2896e5
VZ
283 // first ask the drop target if it wants data
284 if ( m_pTarget->OnDrop(pt.x, pt.y) ) {
285 // it does, so give it the data source
286 m_pTarget->SetDataSource(pIDataSource);
287
288 // and now it has the data
8ee9d618 289 wxDragResult rc = ConvertDragEffectToResult(GetDropEffect(grfKeyState));
87a1e308 290 rc = m_pTarget->OnData(pt.x, pt.y, rc);
8ee9d618 291 if ( wxIsDragResultOk(rc) ) {
9e2896e5 292 // operation succeeded
8ee9d618 293 *pdwEffect = ConvertDragResultToEffect(rc);
9e2896e5
VZ
294 }
295 //else: *pdwEffect is already DROPEFFECT_NONE
bbf1f0e5 296 }
9e2896e5 297 //else: OnDrop() returned FALSE, no need to copy data
bbf1f0e5 298
9e2896e5
VZ
299 // release the held object
300 RELEASE_AND_NULL(m_pIDataObject);
bbf1f0e5 301
9e2896e5 302 return S_OK;
bbf1f0e5
KB
303}
304
305// ============================================================================
306// wxDropTarget implementation
307// ============================================================================
308
309// ----------------------------------------------------------------------------
310// ctor/dtor
311// ----------------------------------------------------------------------------
312
9e2896e5
VZ
313wxDropTarget::wxDropTarget(wxDataObject *dataObj)
314 : wxDropTargetBase(dataObj)
bbf1f0e5 315{
9e2896e5
VZ
316 // create an IDropTarget implementation which will notify us about d&d
317 // operations.
318 m_pIDropTarget = new wxIDropTarget(this);
319 m_pIDropTarget->AddRef();
bbf1f0e5
KB
320}
321
322wxDropTarget::~wxDropTarget()
323{
9e2896e5 324 ReleaseInterface(m_pIDropTarget);
bbf1f0e5
KB
325}
326
327// ----------------------------------------------------------------------------
328// [un]register drop handler
329// ----------------------------------------------------------------------------
330
331bool wxDropTarget::Register(WXHWND hwnd)
332{
a40b3f5d
JS
333 // FIXME
334 // RegisterDragDrop not available on Windows CE >= 400?
335 // Or maybe we can dynamically load them from ceshell.dll
336 // or similar.
337#if defined(__WXWINCE__) && _WIN32_WCE >= 400
338 return FALSE;
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);
9e2896e5
VZ
347 return FALSE;
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);
9e2896e5
VZ
358 return FALSE;
359 }
bbf1f0e5 360
8ee9d618
VZ
361 // we will need the window handle for coords transformation later
362 m_pIDropTarget->SetHwnd((HWND)hwnd);
363
9e2896e5 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
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
VZ
391
392// OnDrop() is called only if we previously returned TRUE from
393// IsAcceptedData(), so no need to check anything here
394bool wxDropTarget::OnDrop(wxCoord WXUNUSED(x), wxCoord WXUNUSED(y))
395{
396 return TRUE;
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
407 return FALSE;
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
418 bool rc = FALSE;
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) ) {
426 rc = TRUE;
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