]> git.saurik.com Git - wxWidgets.git/blame - src/msw/ole/droptgt.cpp
return begining of the selection for GetInsertionPoint to match what
[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>
65571936 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 87 // get default drop effect for given keyboard flags
917ae499 88 static inline DWORD GetDropEffect(DWORD flags, wxDragResult defaultAction);
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 111// way to redefine it)
917ae499 112DWORD wxIDropTarget::GetDropEffect(DWORD flags, wxDragResult defaultAction)
bbf1f0e5 113{
917ae499
RR
114 if (defaultAction == wxDragCopy)
115 return flags & MK_SHIFT ? DROPEFFECT_MOVE : DROPEFFECT_COPY;
bbf1f0e5
KB
116 return flags & MK_CONTROL ? DROPEFFECT_COPY : DROPEFFECT_MOVE;
117}
118
119wxIDropTarget::wxIDropTarget(wxDropTarget *pTarget)
3f480da3 120{
bbf1f0e5 121 m_pTarget = pTarget;
3f480da3 122 m_pIDataObject = NULL;
bbf1f0e5
KB
123}
124
3f480da3
VZ
125wxIDropTarget::~wxIDropTarget()
126{
bbf1f0e5
KB
127}
128
129BEGIN_IID_TABLE(wxIDropTarget)
130 ADD_IID(Unknown)
131 ADD_IID(DropTarget)
132END_IID_TABLE;
133
134IMPLEMENT_IUNKNOWN_METHODS(wxIDropTarget)
135
bbf1f0e5
KB
136// Name : wxIDropTarget::DragEnter
137// Purpose : Called when the mouse enters the window (dragging something)
138// Returns : S_OK
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
3f480da3 143// Notes :
bbf1f0e5
KB
144STDMETHODIMP wxIDropTarget::DragEnter(IDataObject *pIDataSource,
145 DWORD grfKeyState,
146 POINTL pt,
147 DWORD *pdwEffect)
148{
c5639a87 149 wxLogTrace(wxTRACE_OleCalls, wxT("IDropTarget::DragEnter"));
bbf1f0e5 150
c5639a87
VZ
151 wxASSERT_MSG( m_pIDataObject == NULL,
152 _T("drop target must have data object") );
153
154 // show the list of formats supported by the source data object for the
444ad3a7 155 // debugging purposes, this is quite useful sometimes - please don't remove
c5639a87
VZ
156#if 0
157 IEnumFORMATETC *penumFmt;
158 if ( SUCCEEDED(pIDataSource->EnumFormatEtc(DATADIR_GET, &penumFmt)) )
159 {
160 FORMATETC fmt;
161 while ( penumFmt->Next(1, &fmt, NULL) == S_OK )
162 {
163 wxLogDebug(_T("Drop source supports format %s"),
164 wxDataObject::GetFormatName(fmt.cfFormat));
165 }
166
167 penumFmt->Release();
168 }
169 else
170 {
171 wxLogLastError(_T("IDataObject::EnumFormatEtc"));
172 }
173#endif // 0
bbf1f0e5 174
8ee9d618
VZ
175 if ( !m_pTarget->IsAcceptedData(pIDataSource) ) {
176 // we don't accept this kind of data
177 *pdwEffect = DROPEFFECT_NONE;
bbf1f0e5 178
8ee9d618
VZ
179 return S_OK;
180 }
bbf1f0e5 181
8ee9d618
VZ
182 // get hold of the data object
183 m_pIDataObject = pIDataSource;
184 m_pIDataObject->AddRef();
bbf1f0e5 185
8ee9d618
VZ
186 // we need client coordinates to pass to wxWin functions
187 if ( !ScreenToClient(m_hwnd, (POINT *)&pt) )
188 {
f6bcfd97 189 wxLogLastError(wxT("ScreenToClient"));
8ee9d618 190 }
bbf1f0e5 191
8ee9d618
VZ
192 // give some visual feedback
193 *pdwEffect = ConvertDragResultToEffect(
917ae499
RR
194 m_pTarget->OnEnter(pt.x, pt.y, ConvertDragEffectToResult(
195 GetDropEffect(grfKeyState, m_pTarget->GetDefaultAction()))
8ee9d618
VZ
196 )
197 );
198
199 return S_OK;
bbf1f0e5
KB
200}
201
202// Name : wxIDropTarget::DragOver
203// Purpose : Indicates that the mouse was moved inside the window represented
204// by this drop target.
205// Returns : S_OK
206// Params : [in] DWORD grfKeyState kbd & mouse state
207// [in] POINTL pt mouse coordinates
208// [out]LPDWORD pdwEffect effect flag
3f480da3 209// Notes : We're called on every WM_MOUSEMOVE, so this function should be
bbf1f0e5
KB
210// very efficient.
211STDMETHODIMP wxIDropTarget::DragOver(DWORD grfKeyState,
212 POINTL pt,
213 LPDWORD pdwEffect)
214{
8ee9d618 215 // there are too many of them... wxLogDebug("IDropTarget::DragOver");
bbf1f0e5 216
8ee9d618
VZ
217 wxDragResult result;
218 if ( m_pIDataObject ) {
917ae499
RR
219 result = ConvertDragEffectToResult(
220 GetDropEffect(grfKeyState, m_pTarget->GetDefaultAction()));
8ee9d618
VZ
221 }
222 else {
223 // can't accept data anyhow normally
224 result = wxDragNone;
225 }
c9057ae1 226
8ee9d618
VZ
227 // we need client coordinates to pass to wxWin functions
228 if ( !ScreenToClient(m_hwnd, (POINT *)&pt) )
229 {
f6bcfd97 230 wxLogLastError(wxT("ScreenToClient"));
8ee9d618 231 }
c9057ae1 232
8ee9d618
VZ
233 *pdwEffect = ConvertDragResultToEffect(
234 m_pTarget->OnDragOver(pt.x, pt.y, result)
235 );
236
237 return S_OK;
bbf1f0e5
KB
238}
239
240// Name : wxIDropTarget::DragLeave
241// Purpose : Informs the drop target that the operation has left its window.
242// Returns : S_OK
243// Notes : good place to do any clean-up
244STDMETHODIMP wxIDropTarget::DragLeave()
245{
c5639a87 246 wxLogTrace(wxTRACE_OleCalls, wxT("IDropTarget::DragLeave"));
bbf1f0e5
KB
247
248 // remove the UI feedback
249 m_pTarget->OnLeave();
250
251 // release the held object
252 RELEASE_AND_NULL(m_pIDataObject);
3f480da3 253
bbf1f0e5
KB
254 return S_OK;
255}
256
257// Name : wxIDropTarget::Drop
3f480da3 258// Purpose : Instructs the drop target to paste data that was just now
bbf1f0e5
KB
259// dropped on it.
260// Returns : S_OK
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
3f480da3
VZ
265// Notes :
266STDMETHODIMP wxIDropTarget::Drop(IDataObject *pIDataSource,
267 DWORD grfKeyState,
268 POINTL pt,
bbf1f0e5
KB
269 DWORD *pdwEffect)
270{
c5639a87 271 wxLogTrace(wxTRACE_OleCalls, wxT("IDropTarget::Drop"));
9e2896e5
VZ
272
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 );
276
277 // by default, nothing happens
278 *pdwEffect = DROPEFFECT_NONE;
279
8ee9d618
VZ
280 // we need client coordinates to pass to wxWin functions
281 if ( !ScreenToClient(m_hwnd, (POINT *)&pt) )
282 {
f6bcfd97 283 wxLogLastError(wxT("ScreenToClient"));
8ee9d618
VZ
284 }
285
9e2896e5
VZ
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);
290
291 // and now it has the data
917ae499
RR
292 wxDragResult rc = ConvertDragEffectToResult(
293 GetDropEffect(grfKeyState, m_pTarget->GetDefaultAction()));
87a1e308 294 rc = m_pTarget->OnData(pt.x, pt.y, rc);
8ee9d618 295 if ( wxIsDragResultOk(rc) ) {
9e2896e5 296 // operation succeeded
8ee9d618 297 *pdwEffect = ConvertDragResultToEffect(rc);
9e2896e5
VZ
298 }
299 //else: *pdwEffect is already DROPEFFECT_NONE
bbf1f0e5 300 }
0a0e6a5b 301 //else: OnDrop() returned false, no need to copy data
bbf1f0e5 302
9e2896e5
VZ
303 // release the held object
304 RELEASE_AND_NULL(m_pIDataObject);
bbf1f0e5 305
9e2896e5 306 return S_OK;
bbf1f0e5
KB
307}
308
309// ============================================================================
310// wxDropTarget implementation
311// ============================================================================
312
313// ----------------------------------------------------------------------------
314// ctor/dtor
315// ----------------------------------------------------------------------------
316
9e2896e5
VZ
317wxDropTarget::wxDropTarget(wxDataObject *dataObj)
318 : wxDropTargetBase(dataObj)
bbf1f0e5 319{
9e2896e5
VZ
320 // create an IDropTarget implementation which will notify us about d&d
321 // operations.
322 m_pIDropTarget = new wxIDropTarget(this);
323 m_pIDropTarget->AddRef();
bbf1f0e5
KB
324}
325
326wxDropTarget::~wxDropTarget()
327{
9e2896e5 328 ReleaseInterface(m_pIDropTarget);
bbf1f0e5
KB
329}
330
331// ----------------------------------------------------------------------------
332// [un]register drop handler
333// ----------------------------------------------------------------------------
334
335bool wxDropTarget::Register(WXHWND hwnd)
336{
a40b3f5d
JS
337 // FIXME
338 // RegisterDragDrop not available on Windows CE >= 400?
339 // Or maybe we can dynamically load them from ceshell.dll
340 // or similar.
341#if defined(__WXWINCE__) && _WIN32_WCE >= 400
abf912c5 342 wxUnusedVar(hwnd);
0a0e6a5b 343 return false;
a40b3f5d 344#else
4cb88a72
JS
345 HRESULT hr;
346
347 // May exist in later WinCE versions
348#ifndef __WXWINCE__
349 hr = ::CoLockObjectExternal(m_pIDropTarget, TRUE, FALSE);
9e2896e5 350 if ( FAILED(hr) ) {
161f4f73 351 wxLogApiError(wxT("CoLockObjectExternal"), hr);
0a0e6a5b 352 return false;
9e2896e5 353 }
4cb88a72 354#endif
bbf1f0e5 355
9e2896e5
VZ
356 hr = ::RegisterDragDrop((HWND) hwnd, m_pIDropTarget);
357 if ( FAILED(hr) ) {
4cb88a72
JS
358 // May exist in later WinCE versions
359#ifndef __WXWINCE__
9e2896e5 360 ::CoLockObjectExternal(m_pIDropTarget, FALSE, FALSE);
4cb88a72 361#endif
161f4f73 362 wxLogApiError(wxT("RegisterDragDrop"), hr);
0a0e6a5b 363 return false;
9e2896e5 364 }
bbf1f0e5 365
8ee9d618
VZ
366 // we will need the window handle for coords transformation later
367 m_pIDropTarget->SetHwnd((HWND)hwnd);
368
0a0e6a5b 369 return true;
a40b3f5d 370#endif
bbf1f0e5
KB
371}
372
373void wxDropTarget::Revoke(WXHWND hwnd)
374{
a40b3f5d
JS
375#if defined(__WXWINCE__) && _WIN32_WCE >= 400
376 // Not available, see note above
abf912c5 377 wxUnusedVar(hwnd);
a40b3f5d 378#else
9e2896e5 379 HRESULT hr = ::RevokeDragDrop((HWND) hwnd);
bbf1f0e5 380
9e2896e5 381 if ( FAILED(hr) ) {
161f4f73 382 wxLogApiError(wxT("RevokeDragDrop"), hr);
9e2896e5 383 }
bbf1f0e5 384
4cb88a72
JS
385 // May exist in later WinCE versions
386#ifndef __WXWINCE__
9e2896e5 387 ::CoLockObjectExternal(m_pIDropTarget, FALSE, TRUE);
4cb88a72 388#endif
8ee9d618
VZ
389
390 m_pIDropTarget->SetHwnd(0);
a40b3f5d 391#endif
bbf1f0e5
KB
392}
393
394// ----------------------------------------------------------------------------
9e2896e5 395// base class pure virtuals
bbf1f0e5 396// ----------------------------------------------------------------------------
9e2896e5 397
0a0e6a5b 398// OnDrop() is called only if we previously returned true from
9e2896e5
VZ
399// IsAcceptedData(), so no need to check anything here
400bool wxDropTarget::OnDrop(wxCoord WXUNUSED(x), wxCoord WXUNUSED(y))
401{
0a0e6a5b 402 return true;
9e2896e5
VZ
403}
404
405// copy the data from the data source to the target data object
406bool wxDropTarget::GetData()
bbf1f0e5 407{
9e2896e5
VZ
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?"));
412
0a0e6a5b 413 return false;
9e2896e5
VZ
414 }
415
416 STGMEDIUM stm;
417 FORMATETC fmtMemory;
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
423
0a0e6a5b 424 bool rc = false;
9e2896e5
VZ
425
426 HRESULT hr = m_pIDataSource->GetData(&fmtMemory, &stm);
427 if ( SUCCEEDED(hr) ) {
428 IDataObject *dataObject = m_dataObject->GetInterface();
429
430 hr = dataObject->SetData(&fmtMemory, &stm, TRUE);
431 if ( SUCCEEDED(hr) ) {
0a0e6a5b 432 rc = true;
9e2896e5
VZ
433 }
434 else {
444ad3a7 435 wxLogApiError(wxT("IDataObject::SetData()"), hr);
9e2896e5
VZ
436 }
437 }
438 else {
444ad3a7 439 wxLogApiError(wxT("IDataObject::GetData()"), hr);
bbf1f0e5 440 }
bbf1f0e5 441
9e2896e5 442 return rc;
bbf1f0e5
KB
443}
444
9e2896e5
VZ
445// ----------------------------------------------------------------------------
446// callbacks used by wxIDropTarget
447// ----------------------------------------------------------------------------
bbf1f0e5 448
9e2896e5
VZ
449// we need a data source, so wxIDropTarget gives it to us using this function
450void wxDropTarget::SetDataSource(IDataObject *pIDataSource)
bbf1f0e5 451{
9e2896e5 452 m_pIDataSource = pIDataSource;
bbf1f0e5
KB
453}
454
9e2896e5
VZ
455// determine if we accept data of this type
456bool wxDropTarget::IsAcceptedData(IDataObject *pIDataSource) const
bbf1f0e5 457{
9e2896e5 458 return GetSupportedFormat(pIDataSource) != wxDF_INVALID;
bbf1f0e5
KB
459}
460
9e2896e5
VZ
461// ----------------------------------------------------------------------------
462// helper functions
463// ----------------------------------------------------------------------------
464
465wxDataFormat wxDropTarget::GetSupportedFormat(IDataObject *pIDataSource) const
bbf1f0e5 466{
9e2896e5
VZ
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 = {
470 0,
471 NULL,
472 DVASPECT_CONTENT,
473 -1,
474 TYMED_HGLOBAL // TODO is it worth supporting other tymeds here?
475 };
476
477 // get the list of supported formats
478 size_t nFormats = m_dataObject->GetFormatCount(wxDataObject::Set);
33ac7e6f 479 wxDataFormat format;
c5639a87 480 wxDataFormat *formats;
9e2896e5
VZ
481 formats = nFormats == 1 ? &format : new wxDataFormat[nFormats];
482
483 m_dataObject->GetAllFormats(formats, wxDataObject::Set);
484
485 // cycle through all supported formats
486 size_t n;
487 for ( n = 0; n < nFormats; n++ ) {
488 s_fmtMemory.cfFormat = formats[n];
489
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 ) {
493 format = formats[n];
494
495 break;
496 }
497 }
498
499 if ( formats != &format ) {
500 // free memory if we allocated it
501 delete [] formats;
502 }
503
90bc25c7 504 return n < nFormats ? format : wxFormatInvalid;
bbf1f0e5
KB
505}
506
c9057ae1
VZ
507// ----------------------------------------------------------------------------
508// private functions
509// ----------------------------------------------------------------------------
510
511static wxDragResult ConvertDragEffectToResult(DWORD dwEffect)
512{
513 switch ( dwEffect ) {
514 case DROPEFFECT_COPY:
515 return wxDragCopy;
516
e6d318c2
RD
517 case DROPEFFECT_LINK:
518 return wxDragLink;
519
c9057ae1
VZ
520 case DROPEFFECT_MOVE:
521 return wxDragMove;
522
523 default:
524 wxFAIL_MSG(wxT("invalid value in ConvertDragEffectToResult"));
525 // fall through
526
527 case DROPEFFECT_NONE:
528 return wxDragNone;
529 }
530}
531
532static DWORD ConvertDragResultToEffect(wxDragResult result)
533{
534 switch ( result ) {
535 case wxDragCopy:
536 return DROPEFFECT_COPY;
537
e6d318c2
RD
538 case wxDragLink:
539 return DROPEFFECT_LINK;
540
c9057ae1
VZ
541 case wxDragMove:
542 return DROPEFFECT_MOVE;
543
544 default:
545 wxFAIL_MSG(wxT("invalid value in ConvertDragResultToEffect"));
546 // fall through
547
548 case wxDragNone:
549 return DROPEFFECT_NONE;
550 }
551}
552
bbf1f0e5 553#endif
47d67540 554 // wxUSE_DRAG_AND_DROP