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