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