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