]> git.saurik.com Git - wxWidgets.git/blob - src/msw/ole/droptgt.cpp
2aee28affe3a5f6002020b6b88b3281a9b461cc3
[wxWidgets.git] / src / msw / ole / droptgt.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "droptgt.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #if defined(__BORLANDC__)
28 #pragma hdrstop
29 #endif
30
31 #include "wx/setup.h"
32
33 #if wxUSE_OLE && wxUSE_DRAG_AND_DROP
34
35 #include "wx/msw/private.h"
36 #include "wx/log.h"
37
38 #ifdef __WXWINCE__
39 #include <winreg.h>
40 #include <ole2.h>
41 #endif
42
43 #ifdef __WIN32__
44 #if !defined(__GNUWIN32__) || wxUSE_NORLANDER_HEADERS
45 #if wxCHECK_W32API_VERSION( 1, 0 )
46 #include "wx/msw/wrapwin.h"
47 #endif
48 #include <shlobj.h> // for DROPFILES structure
49 #endif
50 #else
51 #include <shellapi.h>
52 #endif
53
54 #include "wx/dnd.h"
55
56 #include "wx/msw/ole/oleutils.h"
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
64 class wxIDropTarget : public IDropTarget
65 {
66 public:
67 wxIDropTarget(wxDropTarget *p);
68 virtual ~wxIDropTarget();
69
70 // accessors for wxDropTarget
71 void SetHwnd(HWND hwnd) { m_hwnd = hwnd; }
72
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;
80
81 protected:
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
86
87 // get default drop effect for given keyboard flags
88 static inline DWORD GetDropEffect(DWORD flags);
89
90 DECLARE_NO_COPY_CLASS(wxIDropTarget)
91 };
92
93 // ----------------------------------------------------------------------------
94 // private functions
95 // ----------------------------------------------------------------------------
96
97 static wxDragResult ConvertDragEffectToResult(DWORD dwEffect);
98 static DWORD ConvertDragResultToEffect(wxDragResult result);
99
100 // ============================================================================
101 // wxIDropTarget implementation
102 // ============================================================================
103
104 // Name : static wxIDropTarget::GetDropEffect
105 // Purpose : determine the drop operation from keyboard/mouse state.
106 // Returns : DWORD combined from DROPEFFECT_xxx constants
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,
110 // which is the standard behaviour (currently there is no
111 // way to redefine it)
112 DWORD wxIDropTarget::GetDropEffect(DWORD flags)
113 {
114 return flags & MK_CONTROL ? DROPEFFECT_COPY : DROPEFFECT_MOVE;
115 }
116
117 wxIDropTarget::wxIDropTarget(wxDropTarget *pTarget)
118 {
119 m_pTarget = pTarget;
120 m_pIDataObject = NULL;
121 }
122
123 wxIDropTarget::~wxIDropTarget()
124 {
125 }
126
127 BEGIN_IID_TABLE(wxIDropTarget)
128 ADD_IID(Unknown)
129 ADD_IID(DropTarget)
130 END_IID_TABLE;
131
132 IMPLEMENT_IUNKNOWN_METHODS(wxIDropTarget)
133
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
141 // Notes :
142 STDMETHODIMP wxIDropTarget::DragEnter(IDataObject *pIDataSource,
143 DWORD grfKeyState,
144 POINTL pt,
145 DWORD *pdwEffect)
146 {
147 wxLogTrace(wxTRACE_OleCalls, wxT("IDropTarget::DragEnter"));
148
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
153 // debugging purposes, this is quite useful sometimes - please don't remove
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
172
173 if ( !m_pTarget->IsAcceptedData(pIDataSource) ) {
174 // we don't accept this kind of data
175 *pdwEffect = DROPEFFECT_NONE;
176
177 return S_OK;
178 }
179
180 // get hold of the data object
181 m_pIDataObject = pIDataSource;
182 m_pIDataObject->AddRef();
183
184 // we need client coordinates to pass to wxWin functions
185 if ( !ScreenToClient(m_hwnd, (POINT *)&pt) )
186 {
187 wxLogLastError(wxT("ScreenToClient"));
188 }
189
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;
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
207 // Notes : We're called on every WM_MOUSEMOVE, so this function should be
208 // very efficient.
209 STDMETHODIMP wxIDropTarget::DragOver(DWORD grfKeyState,
210 POINTL pt,
211 LPDWORD pdwEffect)
212 {
213 // there are too many of them... wxLogDebug("IDropTarget::DragOver");
214
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 }
223
224 // we need client coordinates to pass to wxWin functions
225 if ( !ScreenToClient(m_hwnd, (POINT *)&pt) )
226 {
227 wxLogLastError(wxT("ScreenToClient"));
228 }
229
230 *pdwEffect = ConvertDragResultToEffect(
231 m_pTarget->OnDragOver(pt.x, pt.y, result)
232 );
233
234 return S_OK;
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
241 STDMETHODIMP wxIDropTarget::DragLeave()
242 {
243 wxLogTrace(wxTRACE_OleCalls, wxT("IDropTarget::DragLeave"));
244
245 // remove the UI feedback
246 m_pTarget->OnLeave();
247
248 // release the held object
249 RELEASE_AND_NULL(m_pIDataObject);
250
251 return S_OK;
252 }
253
254 // Name : wxIDropTarget::Drop
255 // Purpose : Instructs the drop target to paste data that was just now
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
262 // Notes :
263 STDMETHODIMP wxIDropTarget::Drop(IDataObject *pIDataSource,
264 DWORD grfKeyState,
265 POINTL pt,
266 DWORD *pdwEffect)
267 {
268 wxLogTrace(wxTRACE_OleCalls, wxT("IDropTarget::Drop"));
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
277 // we need client coordinates to pass to wxWin functions
278 if ( !ScreenToClient(m_hwnd, (POINT *)&pt) )
279 {
280 wxLogLastError(wxT("ScreenToClient"));
281 }
282
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
289 wxDragResult rc = ConvertDragEffectToResult(GetDropEffect(grfKeyState));
290 rc = m_pTarget->OnData(pt.x, pt.y, rc);
291 if ( wxIsDragResultOk(rc) ) {
292 // operation succeeded
293 *pdwEffect = ConvertDragResultToEffect(rc);
294 }
295 //else: *pdwEffect is already DROPEFFECT_NONE
296 }
297 //else: OnDrop() returned false, no need to copy data
298
299 // release the held object
300 RELEASE_AND_NULL(m_pIDataObject);
301
302 return S_OK;
303 }
304
305 // ============================================================================
306 // wxDropTarget implementation
307 // ============================================================================
308
309 // ----------------------------------------------------------------------------
310 // ctor/dtor
311 // ----------------------------------------------------------------------------
312
313 wxDropTarget::wxDropTarget(wxDataObject *dataObj)
314 : wxDropTargetBase(dataObj)
315 {
316 // create an IDropTarget implementation which will notify us about d&d
317 // operations.
318 m_pIDropTarget = new wxIDropTarget(this);
319 m_pIDropTarget->AddRef();
320 }
321
322 wxDropTarget::~wxDropTarget()
323 {
324 ReleaseInterface(m_pIDropTarget);
325 }
326
327 // ----------------------------------------------------------------------------
328 // [un]register drop handler
329 // ----------------------------------------------------------------------------
330
331 bool wxDropTarget::Register(WXHWND hwnd)
332 {
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 wxUnusedVar(hwnd);
339 return false;
340 #else
341 HRESULT hr;
342
343 // May exist in later WinCE versions
344 #ifndef __WXWINCE__
345 hr = ::CoLockObjectExternal(m_pIDropTarget, TRUE, FALSE);
346 if ( FAILED(hr) ) {
347 wxLogApiError(wxT("CoLockObjectExternal"), hr);
348 return false;
349 }
350 #endif
351
352 hr = ::RegisterDragDrop((HWND) hwnd, m_pIDropTarget);
353 if ( FAILED(hr) ) {
354 // May exist in later WinCE versions
355 #ifndef __WXWINCE__
356 ::CoLockObjectExternal(m_pIDropTarget, FALSE, FALSE);
357 #endif
358 wxLogApiError(wxT("RegisterDragDrop"), hr);
359 return false;
360 }
361
362 // we will need the window handle for coords transformation later
363 m_pIDropTarget->SetHwnd((HWND)hwnd);
364
365 return true;
366 #endif
367 }
368
369 void wxDropTarget::Revoke(WXHWND hwnd)
370 {
371 #if defined(__WXWINCE__) && _WIN32_WCE >= 400
372 // Not available, see note above
373 wxUnusedVar(hwnd);
374 #else
375 HRESULT hr = ::RevokeDragDrop((HWND) hwnd);
376
377 if ( FAILED(hr) ) {
378 wxLogApiError(wxT("RevokeDragDrop"), hr);
379 }
380
381 // May exist in later WinCE versions
382 #ifndef __WXWINCE__
383 ::CoLockObjectExternal(m_pIDropTarget, FALSE, TRUE);
384 #endif
385
386 m_pIDropTarget->SetHwnd(0);
387 #endif
388 }
389
390 // ----------------------------------------------------------------------------
391 // base class pure virtuals
392 // ----------------------------------------------------------------------------
393
394 // OnDrop() is called only if we previously returned true from
395 // IsAcceptedData(), so no need to check anything here
396 bool wxDropTarget::OnDrop(wxCoord WXUNUSED(x), wxCoord WXUNUSED(y))
397 {
398 return true;
399 }
400
401 // copy the data from the data source to the target data object
402 bool wxDropTarget::GetData()
403 {
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
409 return false;
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
420 bool rc = false;
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) ) {
428 rc = true;
429 }
430 else {
431 wxLogApiError(wxT("IDataObject::SetData()"), hr);
432 }
433 }
434 else {
435 wxLogApiError(wxT("IDataObject::GetData()"), hr);
436 }
437
438 return rc;
439 }
440
441 // ----------------------------------------------------------------------------
442 // callbacks used by wxIDropTarget
443 // ----------------------------------------------------------------------------
444
445 // we need a data source, so wxIDropTarget gives it to us using this function
446 void wxDropTarget::SetDataSource(IDataObject *pIDataSource)
447 {
448 m_pIDataSource = pIDataSource;
449 }
450
451 // determine if we accept data of this type
452 bool wxDropTarget::IsAcceptedData(IDataObject *pIDataSource) const
453 {
454 return GetSupportedFormat(pIDataSource) != wxDF_INVALID;
455 }
456
457 // ----------------------------------------------------------------------------
458 // helper functions
459 // ----------------------------------------------------------------------------
460
461 wxDataFormat wxDropTarget::GetSupportedFormat(IDataObject *pIDataSource) const
462 {
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);
475 wxDataFormat format;
476 wxDataFormat *formats;
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
500 return n < nFormats ? format : wxFormatInvalid;
501 }
502
503 // ----------------------------------------------------------------------------
504 // private functions
505 // ----------------------------------------------------------------------------
506
507 static wxDragResult ConvertDragEffectToResult(DWORD dwEffect)
508 {
509 switch ( dwEffect ) {
510 case DROPEFFECT_COPY:
511 return wxDragCopy;
512
513 case DROPEFFECT_LINK:
514 return wxDragLink;
515
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
528 static DWORD ConvertDragResultToEffect(wxDragResult result)
529 {
530 switch ( result ) {
531 case wxDragCopy:
532 return DROPEFFECT_COPY;
533
534 case wxDragLink:
535 return DROPEFFECT_LINK;
536
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
549 #endif
550 // wxUSE_DRAG_AND_DROP