]>
Commit | Line | Data |
---|---|---|
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 | ||
59 | class wxIDropTarget : public IDropTarget | |
60 | { | |
61 | public: | |
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 | ||
76 | protected: | |
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 inline DWORD GetDropEffect(DWORD flags, wxDragResult defaultAction); | |
84 | ||
85 | DECLARE_NO_COPY_CLASS(wxIDropTarget) | |
86 | }; | |
87 | ||
88 | // ---------------------------------------------------------------------------- | |
89 | // private functions | |
90 | // ---------------------------------------------------------------------------- | |
91 | ||
92 | static wxDragResult ConvertDragEffectToResult(DWORD dwEffect); | |
93 | static 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 | // Notes : We do "move" normally and "copy" if <Ctrl> is pressed, | |
105 | // which is the standard behaviour (currently there is no | |
106 | // way to redefine it) | |
107 | DWORD wxIDropTarget::GetDropEffect(DWORD flags, wxDragResult defaultAction) | |
108 | { | |
109 | if (defaultAction == wxDragCopy) | |
110 | return flags & MK_SHIFT ? DROPEFFECT_MOVE : DROPEFFECT_COPY; | |
111 | return flags & MK_CONTROL ? DROPEFFECT_COPY : DROPEFFECT_MOVE; | |
112 | } | |
113 | ||
114 | wxIDropTarget::wxIDropTarget(wxDropTarget *pTarget) | |
115 | { | |
116 | m_pTarget = pTarget; | |
117 | m_pIDataObject = NULL; | |
118 | } | |
119 | ||
120 | wxIDropTarget::~wxIDropTarget() | |
121 | { | |
122 | } | |
123 | ||
124 | BEGIN_IID_TABLE(wxIDropTarget) | |
125 | ADD_IID(Unknown) | |
126 | ADD_IID(DropTarget) | |
127 | END_IID_TABLE; | |
128 | ||
129 | IMPLEMENT_IUNKNOWN_METHODS(wxIDropTarget) | |
130 | ||
131 | // Name : wxIDropTarget::DragEnter | |
132 | // Purpose : Called when the mouse enters the window (dragging something) | |
133 | // Returns : S_OK | |
134 | // Params : [in] IDataObject *pIDataSource : source data | |
135 | // [in] DWORD grfKeyState : kbd & mouse state | |
136 | // [in] POINTL pt : mouse coordinates | |
137 | // [out]DWORD *pdwEffect : effect flag | |
138 | // Notes : | |
139 | STDMETHODIMP wxIDropTarget::DragEnter(IDataObject *pIDataSource, | |
140 | DWORD grfKeyState, | |
141 | POINTL pt, | |
142 | DWORD *pdwEffect) | |
143 | { | |
144 | wxLogTrace(wxTRACE_OleCalls, wxT("IDropTarget::DragEnter")); | |
145 | ||
146 | wxASSERT_MSG( m_pIDataObject == NULL, | |
147 | _T("drop target must have data object") ); | |
148 | ||
149 | // show the list of formats supported by the source data object for the | |
150 | // debugging purposes, this is quite useful sometimes - please don't remove | |
151 | #if 0 | |
152 | IEnumFORMATETC *penumFmt; | |
153 | if ( SUCCEEDED(pIDataSource->EnumFormatEtc(DATADIR_GET, &penumFmt)) ) | |
154 | { | |
155 | FORMATETC fmt; | |
156 | while ( penumFmt->Next(1, &fmt, NULL) == S_OK ) | |
157 | { | |
158 | wxLogDebug(_T("Drop source supports format %s"), | |
159 | wxDataObject::GetFormatName(fmt.cfFormat)); | |
160 | } | |
161 | ||
162 | penumFmt->Release(); | |
163 | } | |
164 | else | |
165 | { | |
166 | wxLogLastError(_T("IDataObject::EnumFormatEtc")); | |
167 | } | |
168 | #endif // 0 | |
169 | ||
170 | if ( !m_pTarget->IsAcceptedData(pIDataSource) ) { | |
171 | // we don't accept this kind of data | |
172 | *pdwEffect = DROPEFFECT_NONE; | |
173 | ||
174 | return S_OK; | |
175 | } | |
176 | ||
177 | // get hold of the data object | |
178 | m_pIDataObject = pIDataSource; | |
179 | m_pIDataObject->AddRef(); | |
180 | ||
181 | // we need client coordinates to pass to wxWin functions | |
182 | if ( !ScreenToClient(m_hwnd, (POINT *)&pt) ) | |
183 | { | |
184 | wxLogLastError(wxT("ScreenToClient")); | |
185 | } | |
186 | ||
187 | // give some visual feedback | |
188 | *pdwEffect = ConvertDragResultToEffect( | |
189 | m_pTarget->OnEnter(pt.x, pt.y, ConvertDragEffectToResult( | |
190 | GetDropEffect(grfKeyState, m_pTarget->GetDefaultAction())) | |
191 | ) | |
192 | ); | |
193 | ||
194 | return S_OK; | |
195 | } | |
196 | ||
197 | // Name : wxIDropTarget::DragOver | |
198 | // Purpose : Indicates that the mouse was moved inside the window represented | |
199 | // by this drop target. | |
200 | // Returns : S_OK | |
201 | // Params : [in] DWORD grfKeyState kbd & mouse state | |
202 | // [in] POINTL pt mouse coordinates | |
203 | // [out]LPDWORD pdwEffect effect flag | |
204 | // Notes : We're called on every WM_MOUSEMOVE, so this function should be | |
205 | // very efficient. | |
206 | STDMETHODIMP wxIDropTarget::DragOver(DWORD grfKeyState, | |
207 | POINTL pt, | |
208 | LPDWORD pdwEffect) | |
209 | { | |
210 | // there are too many of them... wxLogDebug("IDropTarget::DragOver"); | |
211 | ||
212 | wxDragResult result; | |
213 | if ( m_pIDataObject ) { | |
214 | result = ConvertDragEffectToResult( | |
215 | GetDropEffect(grfKeyState, m_pTarget->GetDefaultAction())); | |
216 | } | |
217 | else { | |
218 | // can't accept data anyhow normally | |
219 | result = wxDragNone; | |
220 | } | |
221 | ||
222 | // we need client coordinates to pass to wxWin functions | |
223 | if ( !ScreenToClient(m_hwnd, (POINT *)&pt) ) | |
224 | { | |
225 | wxLogLastError(wxT("ScreenToClient")); | |
226 | } | |
227 | ||
228 | *pdwEffect = ConvertDragResultToEffect( | |
229 | m_pTarget->OnDragOver(pt.x, pt.y, result) | |
230 | ); | |
231 | ||
232 | return S_OK; | |
233 | } | |
234 | ||
235 | // Name : wxIDropTarget::DragLeave | |
236 | // Purpose : Informs the drop target that the operation has left its window. | |
237 | // Returns : S_OK | |
238 | // Notes : good place to do any clean-up | |
239 | STDMETHODIMP wxIDropTarget::DragLeave() | |
240 | { | |
241 | wxLogTrace(wxTRACE_OleCalls, wxT("IDropTarget::DragLeave")); | |
242 | ||
243 | // remove the UI feedback | |
244 | m_pTarget->OnLeave(); | |
245 | ||
246 | // release the held object | |
247 | RELEASE_AND_NULL(m_pIDataObject); | |
248 | ||
249 | return S_OK; | |
250 | } | |
251 | ||
252 | // Name : wxIDropTarget::Drop | |
253 | // Purpose : Instructs the drop target to paste data that was just now | |
254 | // dropped on it. | |
255 | // Returns : S_OK | |
256 | // Params : [in] IDataObject *pIDataSource the data to paste | |
257 | // [in] DWORD grfKeyState kbd & mouse state | |
258 | // [in] POINTL pt where the drop occurred? | |
259 | // [ouy]DWORD *pdwEffect operation effect | |
260 | // Notes : | |
261 | STDMETHODIMP wxIDropTarget::Drop(IDataObject *pIDataSource, | |
262 | DWORD grfKeyState, | |
263 | POINTL pt, | |
264 | DWORD *pdwEffect) | |
265 | { | |
266 | wxLogTrace(wxTRACE_OleCalls, wxT("IDropTarget::Drop")); | |
267 | ||
268 | // TODO I don't know why there is this parameter, but so far I assume | |
269 | // that it's the same we've already got in DragEnter | |
270 | wxASSERT( m_pIDataObject == pIDataSource ); | |
271 | ||
272 | // by default, nothing happens | |
273 | *pdwEffect = DROPEFFECT_NONE; | |
274 | ||
275 | // we need client coordinates to pass to wxWin functions | |
276 | if ( !ScreenToClient(m_hwnd, (POINT *)&pt) ) | |
277 | { | |
278 | wxLogLastError(wxT("ScreenToClient")); | |
279 | } | |
280 | ||
281 | // first ask the drop target if it wants data | |
282 | if ( m_pTarget->OnDrop(pt.x, pt.y) ) { | |
283 | // it does, so give it the data source | |
284 | m_pTarget->SetDataSource(pIDataSource); | |
285 | ||
286 | // and now it has the data | |
287 | wxDragResult rc = ConvertDragEffectToResult( | |
288 | GetDropEffect(grfKeyState, m_pTarget->GetDefaultAction())); | |
289 | rc = m_pTarget->OnData(pt.x, pt.y, rc); | |
290 | if ( wxIsDragResultOk(rc) ) { | |
291 | // operation succeeded | |
292 | *pdwEffect = ConvertDragResultToEffect(rc); | |
293 | } | |
294 | //else: *pdwEffect is already DROPEFFECT_NONE | |
295 | } | |
296 | //else: OnDrop() returned false, no need to copy data | |
297 | ||
298 | // release the held object | |
299 | RELEASE_AND_NULL(m_pIDataObject); | |
300 | ||
301 | return S_OK; | |
302 | } | |
303 | ||
304 | // ============================================================================ | |
305 | // wxDropTarget implementation | |
306 | // ============================================================================ | |
307 | ||
308 | // ---------------------------------------------------------------------------- | |
309 | // ctor/dtor | |
310 | // ---------------------------------------------------------------------------- | |
311 | ||
312 | wxDropTarget::wxDropTarget(wxDataObject *dataObj) | |
313 | : wxDropTargetBase(dataObj) | |
314 | { | |
315 | // create an IDropTarget implementation which will notify us about d&d | |
316 | // operations. | |
317 | m_pIDropTarget = new wxIDropTarget(this); | |
318 | m_pIDropTarget->AddRef(); | |
319 | } | |
320 | ||
321 | wxDropTarget::~wxDropTarget() | |
322 | { | |
323 | ReleaseInterface(m_pIDropTarget); | |
324 | } | |
325 | ||
326 | // ---------------------------------------------------------------------------- | |
327 | // [un]register drop handler | |
328 | // ---------------------------------------------------------------------------- | |
329 | ||
330 | bool wxDropTarget::Register(WXHWND hwnd) | |
331 | { | |
332 | // FIXME | |
333 | // RegisterDragDrop not available on Windows CE >= 400? | |
334 | // Or maybe we can dynamically load them from ceshell.dll | |
335 | // or similar. | |
336 | #if defined(__WXWINCE__) && _WIN32_WCE >= 400 | |
337 | wxUnusedVar(hwnd); | |
338 | return false; | |
339 | #else | |
340 | HRESULT hr; | |
341 | ||
342 | // May exist in later WinCE versions | |
343 | #ifndef __WXWINCE__ | |
344 | hr = ::CoLockObjectExternal(m_pIDropTarget, TRUE, FALSE); | |
345 | if ( FAILED(hr) ) { | |
346 | wxLogApiError(wxT("CoLockObjectExternal"), hr); | |
347 | return false; | |
348 | } | |
349 | #endif | |
350 | ||
351 | hr = ::RegisterDragDrop((HWND) hwnd, m_pIDropTarget); | |
352 | if ( FAILED(hr) ) { | |
353 | // May exist in later WinCE versions | |
354 | #ifndef __WXWINCE__ | |
355 | ::CoLockObjectExternal(m_pIDropTarget, FALSE, FALSE); | |
356 | #endif | |
357 | wxLogApiError(wxT("RegisterDragDrop"), hr); | |
358 | return false; | |
359 | } | |
360 | ||
361 | // we will need the window handle for coords transformation later | |
362 | m_pIDropTarget->SetHwnd((HWND)hwnd); | |
363 | ||
364 | return true; | |
365 | #endif | |
366 | } | |
367 | ||
368 | void wxDropTarget::Revoke(WXHWND hwnd) | |
369 | { | |
370 | #if defined(__WXWINCE__) && _WIN32_WCE >= 400 | |
371 | // Not available, see note above | |
372 | wxUnusedVar(hwnd); | |
373 | #else | |
374 | HRESULT hr = ::RevokeDragDrop((HWND) hwnd); | |
375 | ||
376 | if ( FAILED(hr) ) { | |
377 | wxLogApiError(wxT("RevokeDragDrop"), hr); | |
378 | } | |
379 | ||
380 | // May exist in later WinCE versions | |
381 | #ifndef __WXWINCE__ | |
382 | ::CoLockObjectExternal(m_pIDropTarget, FALSE, TRUE); | |
383 | #endif | |
384 | ||
385 | m_pIDropTarget->SetHwnd(0); | |
386 | #endif | |
387 | } | |
388 | ||
389 | // ---------------------------------------------------------------------------- | |
390 | // base class pure virtuals | |
391 | // ---------------------------------------------------------------------------- | |
392 | ||
393 | // OnDrop() is called only if we previously returned true from | |
394 | // IsAcceptedData(), so no need to check anything here | |
395 | bool wxDropTarget::OnDrop(wxCoord WXUNUSED(x), wxCoord WXUNUSED(y)) | |
396 | { | |
397 | return true; | |
398 | } | |
399 | ||
400 | // copy the data from the data source to the target data object | |
401 | bool wxDropTarget::GetData() | |
402 | { | |
403 | wxDataFormat format = GetSupportedFormat(m_pIDataSource); | |
404 | if ( format == wxDF_INVALID ) { | |
405 | // this is strange because IsAcceptedData() succeeded previously! | |
406 | wxFAIL_MSG(wxT("strange - did supported formats list change?")); | |
407 | ||
408 | return false; | |
409 | } | |
410 | ||
411 | STGMEDIUM stm; | |
412 | FORMATETC fmtMemory; | |
413 | fmtMemory.cfFormat = format; | |
414 | fmtMemory.ptd = NULL; | |
415 | fmtMemory.dwAspect = DVASPECT_CONTENT; | |
416 | fmtMemory.lindex = -1; | |
417 | fmtMemory.tymed = TYMED_HGLOBAL; // TODO to add other media | |
418 | ||
419 | bool rc = false; | |
420 | ||
421 | HRESULT hr = m_pIDataSource->GetData(&fmtMemory, &stm); | |
422 | if ( SUCCEEDED(hr) ) { | |
423 | IDataObject *dataObject = m_dataObject->GetInterface(); | |
424 | ||
425 | hr = dataObject->SetData(&fmtMemory, &stm, TRUE); | |
426 | if ( SUCCEEDED(hr) ) { | |
427 | rc = true; | |
428 | } | |
429 | else { | |
430 | wxLogApiError(wxT("IDataObject::SetData()"), hr); | |
431 | } | |
432 | } | |
433 | else { | |
434 | wxLogApiError(wxT("IDataObject::GetData()"), hr); | |
435 | } | |
436 | ||
437 | return rc; | |
438 | } | |
439 | ||
440 | // ---------------------------------------------------------------------------- | |
441 | // callbacks used by wxIDropTarget | |
442 | // ---------------------------------------------------------------------------- | |
443 | ||
444 | // we need a data source, so wxIDropTarget gives it to us using this function | |
445 | void wxDropTarget::SetDataSource(IDataObject *pIDataSource) | |
446 | { | |
447 | m_pIDataSource = pIDataSource; | |
448 | } | |
449 | ||
450 | // determine if we accept data of this type | |
451 | bool wxDropTarget::IsAcceptedData(IDataObject *pIDataSource) const | |
452 | { | |
453 | return GetSupportedFormat(pIDataSource) != wxDF_INVALID; | |
454 | } | |
455 | ||
456 | // ---------------------------------------------------------------------------- | |
457 | // helper functions | |
458 | // ---------------------------------------------------------------------------- | |
459 | ||
460 | wxDataFormat wxDropTarget::GetSupportedFormat(IDataObject *pIDataSource) const | |
461 | { | |
462 | // this strucutre describes a data of any type (first field will be | |
463 | // changing) being passed through global memory block. | |
464 | static FORMATETC s_fmtMemory = { | |
465 | 0, | |
466 | NULL, | |
467 | DVASPECT_CONTENT, | |
468 | -1, | |
469 | TYMED_HGLOBAL // TODO is it worth supporting other tymeds here? | |
470 | }; | |
471 | ||
472 | // get the list of supported formats | |
473 | size_t nFormats = m_dataObject->GetFormatCount(wxDataObject::Set); | |
474 | wxDataFormat format; | |
475 | wxDataFormat *formats; | |
476 | formats = nFormats == 1 ? &format : new wxDataFormat[nFormats]; | |
477 | ||
478 | m_dataObject->GetAllFormats(formats, wxDataObject::Set); | |
479 | ||
480 | // cycle through all supported formats | |
481 | size_t n; | |
482 | for ( n = 0; n < nFormats; n++ ) { | |
483 | s_fmtMemory.cfFormat = formats[n]; | |
484 | ||
485 | // NB: don't use SUCCEEDED macro here: QueryGetData returns S_FALSE | |
486 | // for file drag and drop (format == CF_HDROP) | |
487 | if ( pIDataSource->QueryGetData(&s_fmtMemory) == S_OK ) { | |
488 | format = formats[n]; | |
489 | ||
490 | break; | |
491 | } | |
492 | } | |
493 | ||
494 | if ( formats != &format ) { | |
495 | // free memory if we allocated it | |
496 | delete [] formats; | |
497 | } | |
498 | ||
499 | return n < nFormats ? format : wxFormatInvalid; | |
500 | } | |
501 | ||
502 | // ---------------------------------------------------------------------------- | |
503 | // private functions | |
504 | // ---------------------------------------------------------------------------- | |
505 | ||
506 | static wxDragResult ConvertDragEffectToResult(DWORD dwEffect) | |
507 | { | |
508 | switch ( dwEffect ) { | |
509 | case DROPEFFECT_COPY: | |
510 | return wxDragCopy; | |
511 | ||
512 | case DROPEFFECT_LINK: | |
513 | return wxDragLink; | |
514 | ||
515 | case DROPEFFECT_MOVE: | |
516 | return wxDragMove; | |
517 | ||
518 | default: | |
519 | wxFAIL_MSG(wxT("invalid value in ConvertDragEffectToResult")); | |
520 | // fall through | |
521 | ||
522 | case DROPEFFECT_NONE: | |
523 | return wxDragNone; | |
524 | } | |
525 | } | |
526 | ||
527 | static DWORD ConvertDragResultToEffect(wxDragResult result) | |
528 | { | |
529 | switch ( result ) { | |
530 | case wxDragCopy: | |
531 | return DROPEFFECT_COPY; | |
532 | ||
533 | case wxDragLink: | |
534 | return DROPEFFECT_LINK; | |
535 | ||
536 | case wxDragMove: | |
537 | return DROPEFFECT_MOVE; | |
538 | ||
539 | default: | |
540 | wxFAIL_MSG(wxT("invalid value in ConvertDragResultToEffect")); | |
541 | // fall through | |
542 | ||
543 | case wxDragNone: | |
544 | return DROPEFFECT_NONE; | |
545 | } | |
546 | } | |
547 | ||
548 | #endif // wxUSE_OLE && wxUSE_DRAG_AND_DROP |