]> git.saurik.com Git - wxWidgets.git/blob - src/msw/ole/droptgt.cpp
67825f8569c8e97441581b241547b2e94051209f
[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 license
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // Declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
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_DRAG_AND_DROP
34
35 #include "wx/log.h"
36
37 #ifdef __WIN32__
38 #ifndef __GNUWIN32__
39 #include <shlobj.h> // for DROPFILES structure
40 #endif
41 #else
42 #include <shellapi.h>
43 #endif
44
45 #include "wx/dnd.h"
46
47 #ifndef __WIN32__
48 #include <ole2.h>
49 #include <olestd.h>
50 #endif
51
52 #include "wx/msw/ole/oleutils.h"
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
60 class wxIDropTarget : public IDropTarget
61 {
62 public:
63 wxIDropTarget(wxDropTarget *p);
64 ~wxIDropTarget();
65
66 // accessors for wxDropTarget
67 void SetHwnd(HWND hwnd) { m_hwnd = hwnd; }
68
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;
76
77 protected:
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
82
83 // get default drop effect for given keyboard flags
84 static inline DWORD GetDropEffect(DWORD flags);
85 };
86
87 // ----------------------------------------------------------------------------
88 // private functions
89 // ----------------------------------------------------------------------------
90
91 static wxDragResult ConvertDragEffectToResult(DWORD dwEffect);
92 static DWORD ConvertDragResultToEffect(wxDragResult result);
93
94 // ============================================================================
95 // wxIDropTarget implementation
96 // ============================================================================
97
98 // Name : static wxIDropTarget::GetDropEffect
99 // Purpose : determine the drop operation from keyboard/mouse state.
100 // Returns : DWORD combined from DROPEFFECT_xxx constants
101 // Params : [in] DWORD flags kbd & mouse flags as passed to
102 // IDropTarget methods
103 // Notes : We do "move" normally and "copy" if <Ctrl> is pressed,
104 // which is the standard behaviour (currently there is no
105 // way to redefine it)
106 DWORD wxIDropTarget::GetDropEffect(DWORD flags)
107 {
108 return flags & MK_CONTROL ? DROPEFFECT_COPY : DROPEFFECT_MOVE;
109 }
110
111 wxIDropTarget::wxIDropTarget(wxDropTarget *pTarget)
112 {
113 m_cRef = 0;
114 m_pTarget = pTarget;
115 m_pIDataObject = NULL;
116 }
117
118 wxIDropTarget::~wxIDropTarget()
119 {
120 }
121
122 BEGIN_IID_TABLE(wxIDropTarget)
123 ADD_IID(Unknown)
124 ADD_IID(DropTarget)
125 END_IID_TABLE;
126
127 IMPLEMENT_IUNKNOWN_METHODS(wxIDropTarget)
128
129 // Name : wxIDropTarget::DragEnter
130 // Purpose : Called when the mouse enters the window (dragging something)
131 // Returns : S_OK
132 // Params : [in] IDataObject *pIDataSource : source data
133 // [in] DWORD grfKeyState : kbd & mouse state
134 // [in] POINTL pt : mouse coordinates
135 // [out]DWORD *pdwEffect : effect flag
136 // Notes :
137 STDMETHODIMP wxIDropTarget::DragEnter(IDataObject *pIDataSource,
138 DWORD grfKeyState,
139 POINTL pt,
140 DWORD *pdwEffect)
141 {
142 wxLogDebug(wxT("IDropTarget::DragEnter"));
143
144 wxASSERT( m_pIDataObject == NULL );
145
146 if ( !m_pTarget->IsAcceptedData(pIDataSource) ) {
147 // we don't accept this kind of data
148 *pdwEffect = DROPEFFECT_NONE;
149
150 return S_OK;
151 }
152
153 // get hold of the data object
154 m_pIDataObject = pIDataSource;
155 m_pIDataObject->AddRef();
156
157 // we need client coordinates to pass to wxWin functions
158 if ( !ScreenToClient(m_hwnd, (POINT *)&pt) )
159 {
160 wxLogLastError("ScreenToClient");
161 }
162
163 // give some visual feedback
164 *pdwEffect = ConvertDragResultToEffect(
165 m_pTarget->OnEnter(pt.x, pt.y,
166 ConvertDragEffectToResult(GetDropEffect(grfKeyState))
167 )
168 );
169
170 return S_OK;
171 }
172
173 // Name : wxIDropTarget::DragOver
174 // Purpose : Indicates that the mouse was moved inside the window represented
175 // by this drop target.
176 // Returns : S_OK
177 // Params : [in] DWORD grfKeyState kbd & mouse state
178 // [in] POINTL pt mouse coordinates
179 // [out]LPDWORD pdwEffect effect flag
180 // Notes : We're called on every WM_MOUSEMOVE, so this function should be
181 // very efficient.
182 STDMETHODIMP wxIDropTarget::DragOver(DWORD grfKeyState,
183 POINTL pt,
184 LPDWORD pdwEffect)
185 {
186 // there are too many of them... wxLogDebug("IDropTarget::DragOver");
187
188 wxDragResult result;
189 if ( m_pIDataObject ) {
190 result = ConvertDragEffectToResult(GetDropEffect(grfKeyState));
191 }
192 else {
193 // can't accept data anyhow normally
194 result = wxDragNone;
195 }
196
197 // we need client coordinates to pass to wxWin functions
198 if ( !ScreenToClient(m_hwnd, (POINT *)&pt) )
199 {
200 wxLogLastError("ScreenToClient");
201 }
202
203 *pdwEffect = ConvertDragResultToEffect(
204 m_pTarget->OnDragOver(pt.x, pt.y, result)
205 );
206
207 return S_OK;
208 }
209
210 // Name : wxIDropTarget::DragLeave
211 // Purpose : Informs the drop target that the operation has left its window.
212 // Returns : S_OK
213 // Notes : good place to do any clean-up
214 STDMETHODIMP wxIDropTarget::DragLeave()
215 {
216 wxLogDebug(wxT("IDropTarget::DragLeave"));
217
218 // remove the UI feedback
219 m_pTarget->OnLeave();
220
221 // release the held object
222 RELEASE_AND_NULL(m_pIDataObject);
223
224 return S_OK;
225 }
226
227 // Name : wxIDropTarget::Drop
228 // Purpose : Instructs the drop target to paste data that was just now
229 // dropped on it.
230 // Returns : S_OK
231 // Params : [in] IDataObject *pIDataSource the data to paste
232 // [in] DWORD grfKeyState kbd & mouse state
233 // [in] POINTL pt where the drop occured?
234 // [ouy]DWORD *pdwEffect operation effect
235 // Notes :
236 STDMETHODIMP wxIDropTarget::Drop(IDataObject *pIDataSource,
237 DWORD grfKeyState,
238 POINTL pt,
239 DWORD *pdwEffect)
240 {
241 wxLogDebug(wxT("IDropTarget::Drop"));
242
243 // TODO I don't know why there is this parameter, but so far I assume
244 // that it's the same we've already got in DragEnter
245 wxASSERT( m_pIDataObject == pIDataSource );
246
247 // by default, nothing happens
248 *pdwEffect = DROPEFFECT_NONE;
249
250 // we need client coordinates to pass to wxWin functions
251 if ( !ScreenToClient(m_hwnd, (POINT *)&pt) )
252 {
253 wxLogLastError("ScreenToClient");
254 }
255
256 // first ask the drop target if it wants data
257 if ( m_pTarget->OnDrop(pt.x, pt.y) ) {
258 // it does, so give it the data source
259 m_pTarget->SetDataSource(pIDataSource);
260
261 // and now it has the data
262 wxDragResult rc = ConvertDragEffectToResult(GetDropEffect(grfKeyState));
263 m_pTarget->OnData(pt.x, pt.y);//, rc);
264 /*
265 if ( wxIsDragResultOk(rc) ) {
266 // operation succeeded
267 *pdwEffect = ConvertDragResultToEffect(rc);
268 }
269 */
270
271 //else: *pdwEffect is already DROPEFFECT_NONE
272 }
273 //else: OnDrop() returned FALSE, no need to copy data
274
275 // release the held object
276 RELEASE_AND_NULL(m_pIDataObject);
277
278 return S_OK;
279 }
280
281 // ============================================================================
282 // wxDropTarget implementation
283 // ============================================================================
284
285 // ----------------------------------------------------------------------------
286 // ctor/dtor
287 // ----------------------------------------------------------------------------
288
289 wxDropTarget::wxDropTarget(wxDataObject *dataObj)
290 : wxDropTargetBase(dataObj)
291 {
292 // create an IDropTarget implementation which will notify us about d&d
293 // operations.
294 m_pIDropTarget = new wxIDropTarget(this);
295 m_pIDropTarget->AddRef();
296 }
297
298 wxDropTarget::~wxDropTarget()
299 {
300 ReleaseInterface(m_pIDropTarget);
301 }
302
303 // ----------------------------------------------------------------------------
304 // [un]register drop handler
305 // ----------------------------------------------------------------------------
306
307 bool wxDropTarget::Register(WXHWND hwnd)
308 {
309 HRESULT hr = ::CoLockObjectExternal(m_pIDropTarget, TRUE, FALSE);
310 if ( FAILED(hr) ) {
311 wxLogApiError("CoLockObjectExternal", hr);
312 return FALSE;
313 }
314
315 hr = ::RegisterDragDrop((HWND) hwnd, m_pIDropTarget);
316 if ( FAILED(hr) ) {
317 ::CoLockObjectExternal(m_pIDropTarget, FALSE, FALSE);
318
319 wxLogApiError("RegisterDragDrop", hr);
320 return FALSE;
321 }
322
323 // we will need the window handle for coords transformation later
324 m_pIDropTarget->SetHwnd((HWND)hwnd);
325
326 return TRUE;
327 }
328
329 void wxDropTarget::Revoke(WXHWND hwnd)
330 {
331 HRESULT hr = ::RevokeDragDrop((HWND) hwnd);
332
333 if ( FAILED(hr) ) {
334 wxLogApiError("RevokeDragDrop", hr);
335 }
336
337 ::CoLockObjectExternal(m_pIDropTarget, FALSE, TRUE);
338
339 m_pIDropTarget->SetHwnd(0);
340 }
341
342 // ----------------------------------------------------------------------------
343 // base class pure virtuals
344 // ----------------------------------------------------------------------------
345
346 // OnDrop() is called only if we previously returned TRUE from
347 // IsAcceptedData(), so no need to check anything here
348 bool wxDropTarget::OnDrop(wxCoord WXUNUSED(x), wxCoord WXUNUSED(y))
349 {
350 return TRUE;
351 }
352
353 // copy the data from the data source to the target data object
354 bool wxDropTarget::GetData()
355 {
356 wxDataFormat format = GetSupportedFormat(m_pIDataSource);
357 if ( format == wxDF_INVALID ) {
358 // this is strange because IsAcceptedData() succeeded previously!
359 wxFAIL_MSG(wxT("strange - did supported formats list change?"));
360
361 return FALSE;
362 }
363
364 STGMEDIUM stm;
365 FORMATETC fmtMemory;
366 fmtMemory.cfFormat = format;
367 fmtMemory.ptd = NULL;
368 fmtMemory.dwAspect = DVASPECT_CONTENT;
369 fmtMemory.lindex = -1;
370 fmtMemory.tymed = TYMED_HGLOBAL; // TODO to add other media
371
372 bool rc = FALSE;
373
374 HRESULT hr = m_pIDataSource->GetData(&fmtMemory, &stm);
375 if ( SUCCEEDED(hr) ) {
376 IDataObject *dataObject = m_dataObject->GetInterface();
377
378 hr = dataObject->SetData(&fmtMemory, &stm, TRUE);
379 if ( SUCCEEDED(hr) ) {
380 rc = TRUE;
381 }
382 else {
383 wxLogLastError("IDataObject::SetData()");
384 }
385 }
386 else {
387 wxLogLastError("IDataObject::GetData()");
388 }
389
390 return rc;
391 }
392
393 // ----------------------------------------------------------------------------
394 // callbacks used by wxIDropTarget
395 // ----------------------------------------------------------------------------
396
397 // we need a data source, so wxIDropTarget gives it to us using this function
398 void wxDropTarget::SetDataSource(IDataObject *pIDataSource)
399 {
400 m_pIDataSource = pIDataSource;
401 }
402
403 // determine if we accept data of this type
404 bool wxDropTarget::IsAcceptedData(IDataObject *pIDataSource) const
405 {
406 return GetSupportedFormat(pIDataSource) != wxDF_INVALID;
407 }
408
409 // ----------------------------------------------------------------------------
410 // helper functions
411 // ----------------------------------------------------------------------------
412
413 wxDataFormat wxDropTarget::GetSupportedFormat(IDataObject *pIDataSource) const
414 {
415 // this strucutre describes a data of any type (first field will be
416 // changing) being passed through global memory block.
417 static FORMATETC s_fmtMemory = {
418 0,
419 NULL,
420 DVASPECT_CONTENT,
421 -1,
422 TYMED_HGLOBAL // TODO is it worth supporting other tymeds here?
423 };
424
425 // get the list of supported formats
426 size_t nFormats = m_dataObject->GetFormatCount(wxDataObject::Set);
427 wxDataFormat format, *formats;
428 formats = nFormats == 1 ? &format : new wxDataFormat[nFormats];
429
430 m_dataObject->GetAllFormats(formats, wxDataObject::Set);
431
432 // cycle through all supported formats
433 size_t n;
434 for ( n = 0; n < nFormats; n++ ) {
435 s_fmtMemory.cfFormat = formats[n];
436
437 // NB: don't use SUCCEEDED macro here: QueryGetData returns S_FALSE
438 // for file drag and drop (format == CF_HDROP)
439 if ( pIDataSource->QueryGetData(&s_fmtMemory) == S_OK ) {
440 format = formats[n];
441
442 break;
443 }
444 }
445
446 if ( formats != &format ) {
447 // free memory if we allocated it
448 delete [] formats;
449 }
450
451 return n < nFormats ? format : wxFormatInvalid;
452 }
453
454 // ----------------------------------------------------------------------------
455 // private functions
456 // ----------------------------------------------------------------------------
457
458 static wxDragResult ConvertDragEffectToResult(DWORD dwEffect)
459 {
460 switch ( dwEffect ) {
461 case DROPEFFECT_COPY:
462 return wxDragCopy;
463
464 case DROPEFFECT_MOVE:
465 return wxDragMove;
466
467 default:
468 wxFAIL_MSG(wxT("invalid value in ConvertDragEffectToResult"));
469 // fall through
470
471 case DROPEFFECT_NONE:
472 return wxDragNone;
473 }
474 }
475
476 static DWORD ConvertDragResultToEffect(wxDragResult result)
477 {
478 switch ( result ) {
479 case wxDragCopy:
480 return DROPEFFECT_COPY;
481
482 case wxDragMove:
483 return DROPEFFECT_MOVE;
484
485 default:
486 wxFAIL_MSG(wxT("invalid value in ConvertDragResultToEffect"));
487 // fall through
488
489 case wxDragNone:
490 return DROPEFFECT_NONE;
491 }
492 }
493
494 #endif
495 // wxUSE_DRAG_AND_DROP