]>
Commit | Line | Data |
---|---|---|
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> | |
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". | |
bbf1f0e5 KB |
25 | #include "wx/wxprec.h" |
26 | ||
27 | #if defined(__BORLANDC__) | |
28 | #pragma hdrstop | |
29 | #endif | |
30 | ||
3f480da3 | 31 | #include "wx/setup.h" |
bbf1f0e5 | 32 | |
47d67540 | 33 | #if wxUSE_DRAG_AND_DROP |
bbf1f0e5 | 34 | |
3f480da3 | 35 | #include "wx/log.h" |
bbf1f0e5 KB |
36 | |
37 | #ifdef __WIN32__ | |
b64f0a5f | 38 | #if !defined(__GNUWIN32__) || wxUSE_NORLANDER_HEADERS |
3f480da3 VZ |
39 | #include <shlobj.h> // for DROPFILES structure |
40 | #endif | |
bbf1f0e5 | 41 | #else |
3f480da3 | 42 | #include <shellapi.h> |
bbf1f0e5 KB |
43 | #endif |
44 | ||
9e2896e5 | 45 | #include "wx/dnd.h" |
bbf1f0e5 KB |
46 | |
47 | #ifndef __WIN32__ | |
3f480da3 VZ |
48 | #include <ole2.h> |
49 | #include <olestd.h> | |
bbf1f0e5 KB |
50 | #endif |
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 | ||
60 | class wxIDropTarget : public IDropTarget | |
61 | { | |
62 | public: | |
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 | |
77 | protected: | |
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 VZ |
83 | // get default drop effect for given keyboard flags |
84 | static inline DWORD GetDropEffect(DWORD flags); | |
bbf1f0e5 KB |
85 | }; |
86 | ||
c9057ae1 VZ |
87 | // ---------------------------------------------------------------------------- |
88 | // private functions | |
89 | // ---------------------------------------------------------------------------- | |
90 | ||
91 | static wxDragResult ConvertDragEffectToResult(DWORD dwEffect); | |
92 | static DWORD ConvertDragResultToEffect(wxDragResult result); | |
93 | ||
bbf1f0e5 KB |
94 | // ============================================================================ |
95 | // wxIDropTarget implementation | |
96 | // ============================================================================ | |
97 | ||
8ee9d618 | 98 | // Name : static wxIDropTarget::GetDropEffect |
bbf1f0e5 | 99 | // Purpose : determine the drop operation from keyboard/mouse state. |
3f480da3 | 100 | // Returns : DWORD combined from DROPEFFECT_xxx constants |
bbf1f0e5 KB |
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, | |
3f480da3 | 104 | // which is the standard behaviour (currently there is no |
bbf1f0e5 KB |
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) | |
3f480da3 VZ |
112 | { |
113 | m_cRef = 0; | |
bbf1f0e5 | 114 | m_pTarget = pTarget; |
3f480da3 | 115 | m_pIDataObject = NULL; |
bbf1f0e5 KB |
116 | } |
117 | ||
3f480da3 VZ |
118 | wxIDropTarget::~wxIDropTarget() |
119 | { | |
bbf1f0e5 KB |
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 | ||
bbf1f0e5 KB |
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 | |
3f480da3 | 136 | // Notes : |
bbf1f0e5 KB |
137 | STDMETHODIMP wxIDropTarget::DragEnter(IDataObject *pIDataSource, |
138 | DWORD grfKeyState, | |
139 | POINTL pt, | |
140 | DWORD *pdwEffect) | |
141 | { | |
8ee9d618 | 142 | wxLogDebug(wxT("IDropTarget::DragEnter")); |
bbf1f0e5 | 143 | |
8ee9d618 | 144 | wxASSERT( m_pIDataObject == NULL ); |
bbf1f0e5 | 145 | |
8ee9d618 VZ |
146 | if ( !m_pTarget->IsAcceptedData(pIDataSource) ) { |
147 | // we don't accept this kind of data | |
148 | *pdwEffect = DROPEFFECT_NONE; | |
bbf1f0e5 | 149 | |
8ee9d618 VZ |
150 | return S_OK; |
151 | } | |
bbf1f0e5 | 152 | |
8ee9d618 VZ |
153 | // get hold of the data object |
154 | m_pIDataObject = pIDataSource; | |
155 | m_pIDataObject->AddRef(); | |
bbf1f0e5 | 156 | |
8ee9d618 VZ |
157 | // we need client coordinates to pass to wxWin functions |
158 | if ( !ScreenToClient(m_hwnd, (POINT *)&pt) ) | |
159 | { | |
f6bcfd97 | 160 | wxLogLastError(wxT("ScreenToClient")); |
8ee9d618 | 161 | } |
bbf1f0e5 | 162 | |
8ee9d618 VZ |
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; | |
bbf1f0e5 KB |
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 | |
3f480da3 | 180 | // Notes : We're called on every WM_MOUSEMOVE, so this function should be |
bbf1f0e5 KB |
181 | // very efficient. |
182 | STDMETHODIMP wxIDropTarget::DragOver(DWORD grfKeyState, | |
183 | POINTL pt, | |
184 | LPDWORD pdwEffect) | |
185 | { | |
8ee9d618 | 186 | // there are too many of them... wxLogDebug("IDropTarget::DragOver"); |
bbf1f0e5 | 187 | |
8ee9d618 VZ |
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 | } | |
c9057ae1 | 196 | |
8ee9d618 VZ |
197 | // we need client coordinates to pass to wxWin functions |
198 | if ( !ScreenToClient(m_hwnd, (POINT *)&pt) ) | |
199 | { | |
f6bcfd97 | 200 | wxLogLastError(wxT("ScreenToClient")); |
8ee9d618 | 201 | } |
c9057ae1 | 202 | |
8ee9d618 VZ |
203 | *pdwEffect = ConvertDragResultToEffect( |
204 | m_pTarget->OnDragOver(pt.x, pt.y, result) | |
205 | ); | |
206 | ||
207 | return S_OK; | |
bbf1f0e5 KB |
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 | { | |
223d09f6 | 216 | wxLogDebug(wxT("IDropTarget::DragLeave")); |
bbf1f0e5 KB |
217 | |
218 | // remove the UI feedback | |
219 | m_pTarget->OnLeave(); | |
220 | ||
221 | // release the held object | |
222 | RELEASE_AND_NULL(m_pIDataObject); | |
3f480da3 | 223 | |
bbf1f0e5 KB |
224 | return S_OK; |
225 | } | |
226 | ||
227 | // Name : wxIDropTarget::Drop | |
3f480da3 | 228 | // Purpose : Instructs the drop target to paste data that was just now |
bbf1f0e5 KB |
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 | |
3f480da3 VZ |
235 | // Notes : |
236 | STDMETHODIMP wxIDropTarget::Drop(IDataObject *pIDataSource, | |
237 | DWORD grfKeyState, | |
238 | POINTL pt, | |
bbf1f0e5 KB |
239 | DWORD *pdwEffect) |
240 | { | |
9e2896e5 VZ |
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 | ||
8ee9d618 VZ |
250 | // we need client coordinates to pass to wxWin functions |
251 | if ( !ScreenToClient(m_hwnd, (POINT *)&pt) ) | |
252 | { | |
f6bcfd97 | 253 | wxLogLastError(wxT("ScreenToClient")); |
8ee9d618 VZ |
254 | } |
255 | ||
9e2896e5 VZ |
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 | |
8ee9d618 | 262 | wxDragResult rc = ConvertDragEffectToResult(GetDropEffect(grfKeyState)); |
87a1e308 | 263 | rc = m_pTarget->OnData(pt.x, pt.y, rc); |
8ee9d618 | 264 | if ( wxIsDragResultOk(rc) ) { |
9e2896e5 | 265 | // operation succeeded |
8ee9d618 | 266 | *pdwEffect = ConvertDragResultToEffect(rc); |
9e2896e5 VZ |
267 | } |
268 | //else: *pdwEffect is already DROPEFFECT_NONE | |
bbf1f0e5 | 269 | } |
9e2896e5 | 270 | //else: OnDrop() returned FALSE, no need to copy data |
bbf1f0e5 | 271 | |
9e2896e5 VZ |
272 | // release the held object |
273 | RELEASE_AND_NULL(m_pIDataObject); | |
bbf1f0e5 | 274 | |
9e2896e5 | 275 | return S_OK; |
bbf1f0e5 KB |
276 | } |
277 | ||
278 | // ============================================================================ | |
279 | // wxDropTarget implementation | |
280 | // ============================================================================ | |
281 | ||
282 | // ---------------------------------------------------------------------------- | |
283 | // ctor/dtor | |
284 | // ---------------------------------------------------------------------------- | |
285 | ||
9e2896e5 VZ |
286 | wxDropTarget::wxDropTarget(wxDataObject *dataObj) |
287 | : wxDropTargetBase(dataObj) | |
bbf1f0e5 | 288 | { |
9e2896e5 VZ |
289 | // create an IDropTarget implementation which will notify us about d&d |
290 | // operations. | |
291 | m_pIDropTarget = new wxIDropTarget(this); | |
292 | m_pIDropTarget->AddRef(); | |
bbf1f0e5 KB |
293 | } |
294 | ||
295 | wxDropTarget::~wxDropTarget() | |
296 | { | |
9e2896e5 | 297 | ReleaseInterface(m_pIDropTarget); |
bbf1f0e5 KB |
298 | } |
299 | ||
300 | // ---------------------------------------------------------------------------- | |
301 | // [un]register drop handler | |
302 | // ---------------------------------------------------------------------------- | |
303 | ||
304 | bool wxDropTarget::Register(WXHWND hwnd) | |
305 | { | |
9e2896e5 VZ |
306 | HRESULT hr = ::CoLockObjectExternal(m_pIDropTarget, TRUE, FALSE); |
307 | if ( FAILED(hr) ) { | |
308 | wxLogApiError("CoLockObjectExternal", hr); | |
309 | return FALSE; | |
310 | } | |
bbf1f0e5 | 311 | |
9e2896e5 VZ |
312 | hr = ::RegisterDragDrop((HWND) hwnd, m_pIDropTarget); |
313 | if ( FAILED(hr) ) { | |
314 | ::CoLockObjectExternal(m_pIDropTarget, FALSE, FALSE); | |
bbf1f0e5 | 315 | |
9e2896e5 VZ |
316 | wxLogApiError("RegisterDragDrop", hr); |
317 | return FALSE; | |
318 | } | |
bbf1f0e5 | 319 | |
8ee9d618 VZ |
320 | // we will need the window handle for coords transformation later |
321 | m_pIDropTarget->SetHwnd((HWND)hwnd); | |
322 | ||
9e2896e5 | 323 | return TRUE; |
bbf1f0e5 KB |
324 | } |
325 | ||
326 | void wxDropTarget::Revoke(WXHWND hwnd) | |
327 | { | |
9e2896e5 | 328 | HRESULT hr = ::RevokeDragDrop((HWND) hwnd); |
bbf1f0e5 | 329 | |
9e2896e5 VZ |
330 | if ( FAILED(hr) ) { |
331 | wxLogApiError("RevokeDragDrop", hr); | |
332 | } | |
bbf1f0e5 | 333 | |
9e2896e5 | 334 | ::CoLockObjectExternal(m_pIDropTarget, FALSE, TRUE); |
8ee9d618 VZ |
335 | |
336 | m_pIDropTarget->SetHwnd(0); | |
bbf1f0e5 KB |
337 | } |
338 | ||
339 | // ---------------------------------------------------------------------------- | |
9e2896e5 | 340 | // base class pure virtuals |
bbf1f0e5 | 341 | // ---------------------------------------------------------------------------- |
9e2896e5 VZ |
342 | |
343 | // OnDrop() is called only if we previously returned TRUE from | |
344 | // IsAcceptedData(), so no need to check anything here | |
345 | bool wxDropTarget::OnDrop(wxCoord WXUNUSED(x), wxCoord WXUNUSED(y)) | |
346 | { | |
347 | return TRUE; | |
348 | } | |
349 | ||
350 | // copy the data from the data source to the target data object | |
351 | bool wxDropTarget::GetData() | |
bbf1f0e5 | 352 | { |
9e2896e5 VZ |
353 | wxDataFormat format = GetSupportedFormat(m_pIDataSource); |
354 | if ( format == wxDF_INVALID ) { | |
355 | // this is strange because IsAcceptedData() succeeded previously! | |
356 | wxFAIL_MSG(wxT("strange - did supported formats list change?")); | |
357 | ||
358 | return FALSE; | |
359 | } | |
360 | ||
361 | STGMEDIUM stm; | |
362 | FORMATETC fmtMemory; | |
363 | fmtMemory.cfFormat = format; | |
364 | fmtMemory.ptd = NULL; | |
365 | fmtMemory.dwAspect = DVASPECT_CONTENT; | |
366 | fmtMemory.lindex = -1; | |
367 | fmtMemory.tymed = TYMED_HGLOBAL; // TODO to add other media | |
368 | ||
369 | bool rc = FALSE; | |
370 | ||
371 | HRESULT hr = m_pIDataSource->GetData(&fmtMemory, &stm); | |
372 | if ( SUCCEEDED(hr) ) { | |
373 | IDataObject *dataObject = m_dataObject->GetInterface(); | |
374 | ||
375 | hr = dataObject->SetData(&fmtMemory, &stm, TRUE); | |
376 | if ( SUCCEEDED(hr) ) { | |
377 | rc = TRUE; | |
378 | } | |
379 | else { | |
f6bcfd97 | 380 | wxLogLastError(wxT("IDataObject::SetData()")); |
9e2896e5 VZ |
381 | } |
382 | } | |
383 | else { | |
f6bcfd97 | 384 | wxLogLastError(wxT("IDataObject::GetData()")); |
bbf1f0e5 | 385 | } |
bbf1f0e5 | 386 | |
9e2896e5 | 387 | return rc; |
bbf1f0e5 KB |
388 | } |
389 | ||
9e2896e5 VZ |
390 | // ---------------------------------------------------------------------------- |
391 | // callbacks used by wxIDropTarget | |
392 | // ---------------------------------------------------------------------------- | |
bbf1f0e5 | 393 | |
9e2896e5 VZ |
394 | // we need a data source, so wxIDropTarget gives it to us using this function |
395 | void wxDropTarget::SetDataSource(IDataObject *pIDataSource) | |
bbf1f0e5 | 396 | { |
9e2896e5 | 397 | m_pIDataSource = pIDataSource; |
bbf1f0e5 KB |
398 | } |
399 | ||
9e2896e5 VZ |
400 | // determine if we accept data of this type |
401 | bool wxDropTarget::IsAcceptedData(IDataObject *pIDataSource) const | |
bbf1f0e5 | 402 | { |
9e2896e5 | 403 | return GetSupportedFormat(pIDataSource) != wxDF_INVALID; |
bbf1f0e5 KB |
404 | } |
405 | ||
9e2896e5 VZ |
406 | // ---------------------------------------------------------------------------- |
407 | // helper functions | |
408 | // ---------------------------------------------------------------------------- | |
409 | ||
410 | wxDataFormat wxDropTarget::GetSupportedFormat(IDataObject *pIDataSource) const | |
bbf1f0e5 | 411 | { |
9e2896e5 VZ |
412 | // this strucutre describes a data of any type (first field will be |
413 | // changing) being passed through global memory block. | |
414 | static FORMATETC s_fmtMemory = { | |
415 | 0, | |
416 | NULL, | |
417 | DVASPECT_CONTENT, | |
418 | -1, | |
419 | TYMED_HGLOBAL // TODO is it worth supporting other tymeds here? | |
420 | }; | |
421 | ||
422 | // get the list of supported formats | |
423 | size_t nFormats = m_dataObject->GetFormatCount(wxDataObject::Set); | |
33ac7e6f KB |
424 | wxDataFormat format; |
425 | wxDataFormat *formats; | |
9e2896e5 VZ |
426 | formats = nFormats == 1 ? &format : new wxDataFormat[nFormats]; |
427 | ||
428 | m_dataObject->GetAllFormats(formats, wxDataObject::Set); | |
429 | ||
430 | // cycle through all supported formats | |
431 | size_t n; | |
432 | for ( n = 0; n < nFormats; n++ ) { | |
433 | s_fmtMemory.cfFormat = formats[n]; | |
434 | ||
435 | // NB: don't use SUCCEEDED macro here: QueryGetData returns S_FALSE | |
436 | // for file drag and drop (format == CF_HDROP) | |
437 | if ( pIDataSource->QueryGetData(&s_fmtMemory) == S_OK ) { | |
438 | format = formats[n]; | |
439 | ||
440 | break; | |
441 | } | |
442 | } | |
443 | ||
444 | if ( formats != &format ) { | |
445 | // free memory if we allocated it | |
446 | delete [] formats; | |
447 | } | |
448 | ||
90bc25c7 | 449 | return n < nFormats ? format : wxFormatInvalid; |
bbf1f0e5 KB |
450 | } |
451 | ||
c9057ae1 VZ |
452 | // ---------------------------------------------------------------------------- |
453 | // private functions | |
454 | // ---------------------------------------------------------------------------- | |
455 | ||
456 | static wxDragResult ConvertDragEffectToResult(DWORD dwEffect) | |
457 | { | |
458 | switch ( dwEffect ) { | |
459 | case DROPEFFECT_COPY: | |
460 | return wxDragCopy; | |
461 | ||
462 | case DROPEFFECT_MOVE: | |
463 | return wxDragMove; | |
464 | ||
465 | default: | |
466 | wxFAIL_MSG(wxT("invalid value in ConvertDragEffectToResult")); | |
467 | // fall through | |
468 | ||
469 | case DROPEFFECT_NONE: | |
470 | return wxDragNone; | |
471 | } | |
472 | } | |
473 | ||
474 | static DWORD ConvertDragResultToEffect(wxDragResult result) | |
475 | { | |
476 | switch ( result ) { | |
477 | case wxDragCopy: | |
478 | return DROPEFFECT_COPY; | |
479 | ||
480 | case wxDragMove: | |
481 | return DROPEFFECT_MOVE; | |
482 | ||
483 | default: | |
484 | wxFAIL_MSG(wxT("invalid value in ConvertDragResultToEffect")); | |
485 | // fall through | |
486 | ||
487 | case wxDragNone: | |
488 | return DROPEFFECT_NONE; | |
489 | } | |
490 | } | |
491 | ||
bbf1f0e5 | 492 | #endif |
47d67540 | 493 | // wxUSE_DRAG_AND_DROP |