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