]>
Commit | Line | Data |
---|---|---|
bbf1f0e5 KB |
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 | #define IN_WX_MAIN_CPP | |
26 | #include "wx/wxprec.h" | |
27 | ||
28 | #if defined(__BORLANDC__) | |
29 | #pragma hdrstop | |
30 | #endif | |
31 | ||
32 | #include <wx/setup.h> | |
33 | ||
34 | #if USE_DRAG_AND_DROP | |
35 | ||
36 | #include <wx/log.h> | |
37 | ||
38 | #ifdef __WIN32__ | |
39 | #ifndef __GNUWIN32__ | |
40 | #include <shlobj.h> // for DROPFILES structure | |
41 | #endif | |
42 | #else | |
43 | #include <shellapi.h> | |
44 | #endif | |
45 | ||
46 | #include <wx/msw/ole/droptgt.h> | |
47 | ||
48 | #ifndef __WIN32__ | |
49 | #include <ole2.h> | |
50 | #include <olestd.h> | |
51 | #endif | |
52 | ||
53 | #include <wx/msw/ole/oleutils.h> | |
54 | ||
55 | // ---------------------------------------------------------------------------- | |
56 | // IDropTarget interface: forward all interesting things to wxDropTarget | |
57 | // (the name is unfortunate, but wx_I_DropTarget is not at all the same thing | |
58 | // as wxDropTarget which is 'public' class, while this one is private) | |
59 | // ---------------------------------------------------------------------------- | |
60 | ||
61 | class wxIDropTarget : public IDropTarget | |
62 | { | |
63 | public: | |
64 | wxIDropTarget(wxDropTarget *p); | |
65 | ~wxIDropTarget(); | |
66 | ||
67 | // IDropTarget methods | |
68 | STDMETHODIMP DragEnter(LPDATAOBJECT, DWORD, POINTL, LPDWORD); | |
69 | STDMETHODIMP DragOver(DWORD, POINTL, LPDWORD); | |
70 | STDMETHODIMP DragLeave(void); | |
71 | STDMETHODIMP Drop(LPDATAOBJECT, DWORD, POINTL, LPDWORD); | |
72 | ||
73 | // @@ we assume that if QueryGetData() returns S_OK, than we can really | |
74 | // get data in this format, so we remember here the format for which | |
75 | // QueryGetData() succeeded | |
76 | void SetSupportedFormat(wxDataFormat cfFormat) { m_cfFormat = cfFormat; } | |
77 | ||
78 | DECLARE_IUNKNOWN_METHODS; | |
79 | ||
80 | protected: | |
81 | IDataObject *m_pIDataObject; // !NULL between DragEnter and DragLeave/Drop | |
82 | wxDropTarget *m_pTarget; // the real target (we're just a proxy) | |
83 | ||
84 | wxDataFormat m_cfFormat; // the format in which to ask for data | |
85 | ||
86 | private: | |
87 | static inline DWORD GetDropEffect(DWORD flags); | |
88 | }; | |
89 | ||
90 | // ============================================================================ | |
91 | // wxIDropTarget implementation | |
92 | // ============================================================================ | |
93 | ||
94 | // Name : static wxDropTarget::GetDropEffect | |
95 | // Purpose : determine the drop operation from keyboard/mouse state. | |
96 | // Returns : DWORD combined from DROPEFFECT_xxx constants | |
97 | // Params : [in] DWORD flags kbd & mouse flags as passed to | |
98 | // IDropTarget methods | |
99 | // Notes : We do "move" normally and "copy" if <Ctrl> is pressed, | |
100 | // which is the standard behaviour (currently there is no | |
101 | // way to redefine it) | |
102 | DWORD wxIDropTarget::GetDropEffect(DWORD flags) | |
103 | { | |
104 | return flags & MK_CONTROL ? DROPEFFECT_COPY : DROPEFFECT_MOVE; | |
105 | } | |
106 | ||
107 | wxIDropTarget::wxIDropTarget(wxDropTarget *pTarget) | |
108 | { | |
109 | m_cRef = 0; | |
110 | m_pTarget = pTarget; | |
111 | m_cfFormat = 0; | |
112 | m_pIDataObject = NULL; | |
113 | } | |
114 | ||
115 | wxIDropTarget::~wxIDropTarget() | |
116 | { | |
117 | } | |
118 | ||
119 | BEGIN_IID_TABLE(wxIDropTarget) | |
120 | ADD_IID(Unknown) | |
121 | ADD_IID(DropTarget) | |
122 | END_IID_TABLE; | |
123 | ||
124 | IMPLEMENT_IUNKNOWN_METHODS(wxIDropTarget) | |
125 | ||
126 | #if 0 | |
127 | STDMETHODIMP wxIDropTarget::QueryInterface(REFIID riid, void **ppv) | |
128 | { | |
129 | // wxLogQueryInterface(wxIDropTarget, riid); | |
130 | ||
131 | if ( IsIidFromList(riid, ms_aIids, WXSIZEOF(ms_aIids)) ) { | |
132 | *ppv = this; | |
133 | AddRef(); | |
134 | ||
135 | return S_OK; | |
136 | } | |
137 | else { | |
138 | *ppv = NULL; | |
139 | ||
140 | return (HRESULT) E_NOINTERFACE; | |
141 | } | |
142 | } | |
143 | ||
144 | STDMETHODIMP_(ULONG) wxIDropTarget::AddRef() | |
145 | { | |
146 | wxLogAddRef(wxIDropTarget, m_cRef); | |
147 | ||
148 | return ++m_cRef; | |
149 | } | |
150 | ||
151 | STDMETHODIMP_(ULONG) wxIDropTarget::Release() | |
152 | { | |
153 | wxLogRelease(wxIDropTarget, m_cRef); | |
154 | ||
155 | if ( --m_cRef == 0 ) { | |
156 | delete this; | |
157 | return 0; | |
158 | } | |
159 | else | |
160 | return m_cRef; | |
161 | } | |
162 | #endif | |
163 | ||
164 | // Name : wxIDropTarget::DragEnter | |
165 | // Purpose : Called when the mouse enters the window (dragging something) | |
166 | // Returns : S_OK | |
167 | // Params : [in] IDataObject *pIDataSource : source data | |
168 | // [in] DWORD grfKeyState : kbd & mouse state | |
169 | // [in] POINTL pt : mouse coordinates | |
170 | // [out]DWORD *pdwEffect : effect flag | |
171 | // Notes : | |
172 | STDMETHODIMP wxIDropTarget::DragEnter(IDataObject *pIDataSource, | |
173 | DWORD grfKeyState, | |
174 | POINTL pt, | |
175 | DWORD *pdwEffect) | |
176 | { | |
177 | wxLogDebug("IDropTarget::DragEnter"); | |
178 | ||
179 | wxASSERT( m_pIDataObject == NULL ); | |
180 | ||
181 | if ( !m_pTarget->IsAcceptedData(pIDataSource) ) { | |
182 | // we don't accept this kind of data | |
183 | *pdwEffect = DROPEFFECT_NONE; | |
184 | ||
185 | return S_OK; | |
186 | } | |
187 | ||
188 | // @@ should check the point also? | |
189 | ||
190 | *pdwEffect = GetDropEffect(grfKeyState); | |
191 | ||
192 | // get hold of the data object | |
193 | m_pIDataObject = pIDataSource; | |
194 | m_pIDataObject->AddRef(); | |
195 | ||
196 | // give some visual feedback | |
197 | m_pTarget->OnEnter(); | |
198 | ||
199 | return S_OK; | |
200 | } | |
201 | ||
202 | // Name : wxIDropTarget::DragOver | |
203 | // Purpose : Indicates that the mouse was moved inside the window represented | |
204 | // by this drop target. | |
205 | // Returns : S_OK | |
206 | // Params : [in] DWORD grfKeyState kbd & mouse state | |
207 | // [in] POINTL pt mouse coordinates | |
208 | // [out]LPDWORD pdwEffect effect flag | |
209 | // Notes : We're called on every WM_MOUSEMOVE, so this function should be | |
210 | // very efficient. | |
211 | STDMETHODIMP wxIDropTarget::DragOver(DWORD grfKeyState, | |
212 | POINTL pt, | |
213 | LPDWORD pdwEffect) | |
214 | { | |
215 | // there are too many of them... wxLogDebug("IDropTarget::DragOver"); | |
216 | ||
217 | *pdwEffect = m_pIDataObject == NULL ? DROPEFFECT_NONE | |
218 | : GetDropEffect(grfKeyState); | |
219 | return S_OK; | |
220 | } | |
221 | ||
222 | // Name : wxIDropTarget::DragLeave | |
223 | // Purpose : Informs the drop target that the operation has left its window. | |
224 | // Returns : S_OK | |
225 | // Notes : good place to do any clean-up | |
226 | STDMETHODIMP wxIDropTarget::DragLeave() | |
227 | { | |
228 | wxLogDebug("IDropTarget::DragLeave"); | |
229 | ||
230 | // remove the UI feedback | |
231 | m_pTarget->OnLeave(); | |
232 | ||
233 | // release the held object | |
234 | RELEASE_AND_NULL(m_pIDataObject); | |
235 | ||
236 | return S_OK; | |
237 | } | |
238 | ||
239 | // Name : wxIDropTarget::Drop | |
240 | // Purpose : Instructs the drop target to paste data that was just now | |
241 | // dropped on it. | |
242 | // Returns : S_OK | |
243 | // Params : [in] IDataObject *pIDataSource the data to paste | |
244 | // [in] DWORD grfKeyState kbd & mouse state | |
245 | // [in] POINTL pt where the drop occured? | |
246 | // [ouy]DWORD *pdwEffect operation effect | |
247 | // Notes : | |
248 | STDMETHODIMP wxIDropTarget::Drop(IDataObject *pIDataSource, | |
249 | DWORD grfKeyState, | |
250 | POINTL pt, | |
251 | DWORD *pdwEffect) | |
252 | { | |
253 | wxLogDebug("IDropTarget::Drop"); | |
254 | ||
255 | // @@ I don't know why there is this parameter, but so far I assume | |
256 | // that it's the same we've already got in DragEnter | |
257 | wxASSERT( m_pIDataObject == pIDataSource ); | |
258 | ||
259 | STGMEDIUM stm; | |
260 | *pdwEffect = DROPEFFECT_NONE; | |
261 | ||
262 | // should be set by SetSupportedFormat() call | |
263 | wxASSERT( m_cfFormat != 0 ); | |
264 | ||
265 | FORMATETC fmtMemory; | |
266 | fmtMemory.cfFormat = m_cfFormat; | |
267 | fmtMemory.ptd = NULL; | |
268 | fmtMemory.dwAspect = DVASPECT_CONTENT; | |
269 | fmtMemory.lindex = -1; | |
270 | fmtMemory.tymed = TYMED_HGLOBAL; // @@@@ to add other media | |
271 | ||
272 | HRESULT hr = pIDataSource->GetData(&fmtMemory, &stm); | |
273 | if ( SUCCEEDED(hr) ) { | |
274 | if ( stm.hGlobal != NULL ) { | |
275 | if ( m_pTarget->OnDrop(pt.x, pt.y, GlobalLock(stm.hGlobal)) ) | |
276 | *pdwEffect = GetDropEffect(grfKeyState); | |
277 | //else: DROPEFFECT_NONE | |
278 | ||
279 | GlobalUnlock(stm.hGlobal); | |
280 | ReleaseStgMedium(&stm); | |
281 | } | |
282 | } | |
283 | else | |
284 | { | |
285 | // wxLogApiError("GetData", hr); | |
286 | } | |
287 | ||
288 | // release the held object | |
289 | RELEASE_AND_NULL(m_pIDataObject); | |
290 | ||
291 | return S_OK; | |
292 | } | |
293 | ||
294 | // ============================================================================ | |
295 | // wxDropTarget implementation | |
296 | // ============================================================================ | |
297 | ||
298 | // ---------------------------------------------------------------------------- | |
299 | // ctor/dtor | |
300 | // ---------------------------------------------------------------------------- | |
301 | ||
302 | wxDropTarget::wxDropTarget() | |
303 | { | |
304 | // create an IDropTarget implementation which will notify us about | |
305 | // d&d operations. | |
306 | m_pIDropTarget = new wxIDropTarget(this); | |
307 | m_pIDropTarget->AddRef(); | |
308 | } | |
309 | ||
310 | wxDropTarget::~wxDropTarget() | |
311 | { | |
312 | ReleaseInterface(m_pIDropTarget); | |
313 | } | |
314 | ||
315 | // ---------------------------------------------------------------------------- | |
316 | // [un]register drop handler | |
317 | // ---------------------------------------------------------------------------- | |
318 | ||
319 | bool wxDropTarget::Register(WXHWND hwnd) | |
320 | { | |
321 | HRESULT hr = ::CoLockObjectExternal(m_pIDropTarget, TRUE, FALSE); | |
322 | if ( FAILED(hr) ) { | |
323 | // wxLogApiError("CoLockObjectExternal", hr); | |
324 | return FALSE; | |
325 | } | |
326 | ||
327 | hr = ::RegisterDragDrop((HWND) hwnd, m_pIDropTarget); | |
328 | if ( FAILED(hr) ) { | |
329 | ::CoLockObjectExternal(m_pIDropTarget, FALSE, FALSE); | |
330 | ||
331 | // wxLogApiError("RegisterDragDrop", hr); | |
332 | return FALSE; | |
333 | } | |
334 | ||
335 | return TRUE; | |
336 | } | |
337 | ||
338 | void wxDropTarget::Revoke(WXHWND hwnd) | |
339 | { | |
340 | HRESULT hr = ::RevokeDragDrop((HWND) hwnd); | |
341 | ||
342 | if ( FAILED(hr) ) | |
343 | { | |
344 | // wxLogApiError("RevokeDragDrop", hr); | |
345 | } | |
346 | ||
347 | ::CoLockObjectExternal(m_pIDropTarget, FALSE, TRUE); | |
348 | } | |
349 | ||
350 | // ---------------------------------------------------------------------------- | |
351 | // determine if we accept data of this type | |
352 | // ---------------------------------------------------------------------------- | |
353 | bool wxDropTarget::IsAcceptedData(IDataObject *pIDataSource) const | |
354 | { | |
355 | // this strucutre describes a data of any type (first field will be | |
356 | // changing) being passed through global memory block. | |
357 | static FORMATETC s_fmtMemory = { | |
358 | 0, | |
359 | NULL, | |
360 | DVASPECT_CONTENT, | |
361 | -1, | |
362 | TYMED_HGLOBAL | |
363 | }; | |
364 | ||
365 | // cycle thorugh all supported formats | |
366 | for ( size_t n = 0; n < GetFormatCount(); n++ ) { | |
367 | s_fmtMemory.cfFormat = GetFormat(n); | |
368 | // @ don't use SUCCEEDED macro here: QueryGetData returns 1 (whatever it | |
369 | // means) for file drag and drop | |
370 | if ( pIDataSource->QueryGetData(&s_fmtMemory) == S_OK ) { | |
371 | // remember this format: we'll later ask for data in it | |
372 | m_pIDropTarget->SetSupportedFormat(s_fmtMemory.cfFormat); | |
373 | return TRUE; | |
374 | } | |
375 | } | |
376 | ||
377 | return FALSE; | |
378 | } | |
379 | ||
380 | // ============================================================================ | |
381 | // wxTextDropTarget | |
382 | // ============================================================================ | |
383 | ||
384 | bool wxTextDropTarget::OnDrop(long x, long y, const void *pData) | |
385 | { | |
386 | return OnDropText(x, y, (const char *)pData); | |
387 | } | |
388 | ||
389 | size_t wxTextDropTarget::GetFormatCount() const | |
390 | { | |
391 | return 1; | |
392 | } | |
393 | ||
394 | wxDataFormat wxTextDropTarget::GetFormat(size_t WXUNUSED(n)) const | |
395 | { | |
396 | return CF_TEXT; | |
397 | } | |
398 | ||
399 | // ============================================================================ | |
400 | // wxFileDropTarget | |
401 | // ============================================================================ | |
402 | ||
403 | bool wxFileDropTarget::OnDrop(long x, long y, const void *pData) | |
404 | { | |
405 | // the documentation states that the first member of DROPFILES structure | |
406 | // is a "DWORD offset of double NUL terminated file list". What they mean by | |
407 | // this (I wonder if you see it immediately) is that the list starts at | |
408 | // ((char *)&(pDropFiles.pFiles)) + pDropFiles.pFiles. We're also advised to | |
409 | // use DragQueryFile to work with this structure, but not told where and how | |
410 | // to get HDROP. | |
411 | HDROP hdrop = (HDROP)pData; // @@ it works, but I'm not sure about it | |
412 | ||
413 | // get number of files (magic value -1) | |
414 | UINT nFiles = ::DragQueryFile(hdrop, -1, NULL, 0); | |
415 | ||
416 | // for each file get the length, allocate memory and then get the name | |
417 | char **aszFiles = new char *[nFiles]; | |
418 | UINT len, n; | |
419 | for ( n = 0; n < nFiles; n++ ) { | |
420 | // +1 for terminating NUL | |
421 | len = ::DragQueryFile(hdrop, n, NULL, 0) + 1; | |
422 | ||
423 | aszFiles[n] = new char[len]; | |
424 | ||
425 | UINT len2 = ::DragQueryFile(hdrop, n, aszFiles[n], len); | |
426 | if ( len2 != len - 1 ) { | |
427 | wxLogDebug("In wxFileDropTarget::OnDrop DragQueryFile returned %d " | |
428 | "characters, %d expected.", len2, len - 1); | |
429 | } | |
430 | } | |
431 | ||
432 | bool bResult = OnDropFiles(x, y, nFiles, (const char**) aszFiles); | |
433 | ||
434 | // free memory | |
435 | for ( n = 0; n < nFiles; n++ ) { | |
436 | delete [] aszFiles[n]; | |
437 | } | |
438 | delete [] aszFiles; | |
439 | ||
440 | return bResult; | |
441 | } | |
442 | ||
443 | size_t wxFileDropTarget::GetFormatCount() const | |
444 | { | |
445 | return 1; | |
446 | } | |
447 | ||
448 | wxDataFormat wxFileDropTarget::GetFormat(size_t WXUNUSED(n)) const | |
449 | { | |
450 | #ifdef __WIN32__ | |
451 | return CF_HDROP; | |
452 | #else | |
453 | // TODO: how to implement this in WIN16? | |
454 | return CF_TEXT; | |
455 | #endif | |
456 | } | |
457 | ||
458 | #endif | |
459 | // USE_DRAG_AND_DROP |