]> git.saurik.com Git - wxWidgets.git/blob - src/msw/ole/droptgt.cpp
Fix to prevent Assert when there is an empty string in a combobox/choice.
[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/dataobj.h"
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 get data
74 // in this format, so we remember here the format for which QueryGetData()
75 // 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 = wxDF_INVALID;
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 // Name : wxIDropTarget::DragEnter
127 // Purpose : Called when the mouse enters the window (dragging something)
128 // Returns : S_OK
129 // Params : [in] IDataObject *pIDataSource : source data
130 // [in] DWORD grfKeyState : kbd & mouse state
131 // [in] POINTL pt : mouse coordinates
132 // [out]DWORD *pdwEffect : effect flag
133 // Notes :
134 STDMETHODIMP wxIDropTarget::DragEnter(IDataObject *pIDataSource,
135 DWORD grfKeyState,
136 POINTL pt,
137 DWORD *pdwEffect)
138 {
139 wxLogDebug(_T("IDropTarget::DragEnter"));
140
141 wxASSERT( m_pIDataObject == NULL );
142
143 if ( !m_pTarget->IsAcceptedData(pIDataSource) ) {
144 // we don't accept this kind of data
145 *pdwEffect = DROPEFFECT_NONE;
146
147 return S_OK;
148 }
149
150 // TODO should check the point also?
151
152 *pdwEffect = GetDropEffect(grfKeyState);
153
154 // get hold of the data object
155 m_pIDataObject = pIDataSource;
156 m_pIDataObject->AddRef();
157
158 // give some visual feedback
159 m_pTarget->OnEnter();
160
161 return S_OK;
162 }
163
164 // Name : wxIDropTarget::DragOver
165 // Purpose : Indicates that the mouse was moved inside the window represented
166 // by this drop target.
167 // Returns : S_OK
168 // Params : [in] DWORD grfKeyState kbd & mouse state
169 // [in] POINTL pt mouse coordinates
170 // [out]LPDWORD pdwEffect effect flag
171 // Notes : We're called on every WM_MOUSEMOVE, so this function should be
172 // very efficient.
173 STDMETHODIMP wxIDropTarget::DragOver(DWORD grfKeyState,
174 POINTL pt,
175 LPDWORD pdwEffect)
176 {
177 // there are too many of them... wxLogDebug("IDropTarget::DragOver");
178
179 *pdwEffect = m_pIDataObject == NULL ? DROPEFFECT_NONE
180 : GetDropEffect(grfKeyState);
181 return S_OK;
182 }
183
184 // Name : wxIDropTarget::DragLeave
185 // Purpose : Informs the drop target that the operation has left its window.
186 // Returns : S_OK
187 // Notes : good place to do any clean-up
188 STDMETHODIMP wxIDropTarget::DragLeave()
189 {
190 wxLogDebug(_T("IDropTarget::DragLeave"));
191
192 // remove the UI feedback
193 m_pTarget->OnLeave();
194
195 // release the held object
196 RELEASE_AND_NULL(m_pIDataObject);
197
198 return S_OK;
199 }
200
201 // Name : wxIDropTarget::Drop
202 // Purpose : Instructs the drop target to paste data that was just now
203 // dropped on it.
204 // Returns : S_OK
205 // Params : [in] IDataObject *pIDataSource the data to paste
206 // [in] DWORD grfKeyState kbd & mouse state
207 // [in] POINTL pt where the drop occured?
208 // [ouy]DWORD *pdwEffect operation effect
209 // Notes :
210 STDMETHODIMP wxIDropTarget::Drop(IDataObject *pIDataSource,
211 DWORD grfKeyState,
212 POINTL pt,
213 DWORD *pdwEffect)
214 {
215 wxLogDebug(_T("IDropTarget::Drop"));
216
217 // TODO I don't know why there is this parameter, but so far I assume
218 // that it's the same we've already got in DragEnter
219 wxASSERT( m_pIDataObject == pIDataSource );
220
221 STGMEDIUM stm;
222 *pdwEffect = DROPEFFECT_NONE;
223
224 // should be set by SetSupportedFormat() call
225 wxASSERT( m_cfFormat != wxDF_INVALID );
226
227 FORMATETC fmtMemory;
228 fmtMemory.cfFormat = m_cfFormat;
229 fmtMemory.ptd = NULL;
230 fmtMemory.dwAspect = DVASPECT_CONTENT;
231 fmtMemory.lindex = -1;
232 fmtMemory.tymed = TYMED_HGLOBAL; // TODO to add other media
233
234 HRESULT hr = pIDataSource->GetData(&fmtMemory, &stm);
235 if ( SUCCEEDED(hr) ) {
236 if ( stm.hGlobal != NULL ) {
237 if ( m_pTarget->OnDrop(pt.x, pt.y, GlobalLock(stm.hGlobal)) )
238 *pdwEffect = GetDropEffect(grfKeyState);
239 //else: DROPEFFECT_NONE
240
241 GlobalUnlock(stm.hGlobal);
242 ReleaseStgMedium(&stm);
243 }
244 }
245 else
246 {
247 // wxLogApiError("GetData", hr);
248 }
249
250 // release the held object
251 RELEASE_AND_NULL(m_pIDataObject);
252
253 return S_OK;
254 }
255
256 // ============================================================================
257 // wxDropTarget implementation
258 // ============================================================================
259
260 // ----------------------------------------------------------------------------
261 // ctor/dtor
262 // ----------------------------------------------------------------------------
263
264 wxDropTarget::wxDropTarget()
265 {
266 // create an IDropTarget implementation which will notify us about
267 // d&d operations.
268 m_pIDropTarget = new wxIDropTarget(this);
269 m_pIDropTarget->AddRef();
270 }
271
272 wxDropTarget::~wxDropTarget()
273 {
274 ReleaseInterface(m_pIDropTarget);
275 }
276
277 // ----------------------------------------------------------------------------
278 // [un]register drop handler
279 // ----------------------------------------------------------------------------
280
281 bool wxDropTarget::Register(WXHWND hwnd)
282 {
283 HRESULT hr = ::CoLockObjectExternal(m_pIDropTarget, TRUE, FALSE);
284 if ( FAILED(hr) ) {
285 wxLogApiError("CoLockObjectExternal", hr);
286 return FALSE;
287 }
288
289 hr = ::RegisterDragDrop((HWND) hwnd, m_pIDropTarget);
290 if ( FAILED(hr) ) {
291 ::CoLockObjectExternal(m_pIDropTarget, FALSE, FALSE);
292
293 wxLogApiError("RegisterDragDrop", hr);
294 return FALSE;
295 }
296
297 return TRUE;
298 }
299
300 void wxDropTarget::Revoke(WXHWND hwnd)
301 {
302 HRESULT hr = ::RevokeDragDrop((HWND) hwnd);
303
304 if ( FAILED(hr) ) {
305 wxLogApiError("RevokeDragDrop", hr);
306 }
307
308 ::CoLockObjectExternal(m_pIDropTarget, FALSE, TRUE);
309 }
310
311 // ----------------------------------------------------------------------------
312 // determine if we accept data of this type
313 // ----------------------------------------------------------------------------
314 bool wxDropTarget::IsAcceptedData(IDataObject *pIDataSource) const
315 {
316 // this strucutre describes a data of any type (first field will be
317 // changing) being passed through global memory block.
318 static FORMATETC s_fmtMemory = {
319 0,
320 NULL,
321 DVASPECT_CONTENT,
322 -1,
323 TYMED_HGLOBAL
324 };
325
326 // cycle thorugh all supported formats
327 for ( size_t n = 0; n < GetFormatCount(); n++ ) {
328 s_fmtMemory.cfFormat = GetFormat(n);
329 // NB: don't use SUCCEEDED macro here: QueryGetData returns 1 (whatever it
330 // means) for file drag and drop
331 if ( pIDataSource->QueryGetData(&s_fmtMemory) == S_OK ) {
332 // remember this format: we'll later ask for data in it
333 m_pIDropTarget->SetSupportedFormat((unsigned int)s_fmtMemory.cfFormat);
334 return TRUE;
335 }
336 }
337
338 return FALSE;
339 }
340
341 // ============================================================================
342 // wxTextDropTarget
343 // ============================================================================
344
345 bool wxTextDropTarget::OnDrop(long x, long y, const void *pData)
346 {
347 return OnDropText(x, y, (const wxChar *)pData);
348 }
349
350 size_t wxTextDropTarget::GetFormatCount() const
351 {
352 return 1;
353 }
354
355 wxDataFormat wxTextDropTarget::GetFormat(size_t WXUNUSED(n)) const
356 {
357 return wxDF_TEXT;
358 }
359
360 // ============================================================================
361 // wxFileDropTarget
362 // ============================================================================
363
364 bool wxFileDropTarget::OnDrop(long x, long y, const void *pData)
365 {
366 // the documentation states that the first member of DROPFILES structure
367 // is a "DWORD offset of double NUL terminated file list". What they mean by
368 // this (I wonder if you see it immediately) is that the list starts at
369 // ((char *)&(pDropFiles.pFiles)) + pDropFiles.pFiles. We're also advised to
370 // use DragQueryFile to work with this structure, but not told where and how
371 // to get HDROP.
372 HDROP hdrop = (HDROP)pData; // NB: it works, but I'm not sure about it
373
374 // get number of files (magic value -1)
375 UINT nFiles = ::DragQueryFile(hdrop, (unsigned)-1, NULL, 0u);
376
377 // for each file get the length, allocate memory and then get the name
378 wxChar **aszFiles = new wxChar *[nFiles];
379 UINT len, n;
380 for ( n = 0; n < nFiles; n++ ) {
381 // +1 for terminating NUL
382 len = ::DragQueryFile(hdrop, n, NULL, 0) + 1;
383
384 aszFiles[n] = new wxChar[len];
385
386 UINT len2 = ::DragQueryFile(hdrop, n, aszFiles[n], len);
387 if ( len2 != len - 1 ) {
388 wxLogDebug(_T("In wxFileDropTarget::OnDrop DragQueryFile returned %d "
389 "characters, %d expected."), len2, len - 1);
390 }
391 }
392
393 bool bResult = OnDropFiles(x, y, nFiles, (const wxChar**) aszFiles);
394
395 // free memory
396 for ( n = 0; n < nFiles; n++ ) {
397 delete [] aszFiles[n];
398 }
399 delete [] aszFiles;
400
401 return bResult;
402 }
403
404 size_t wxFileDropTarget::GetFormatCount() const
405 {
406 return 1;
407 }
408
409 wxDataFormat wxFileDropTarget::GetFormat(size_t WXUNUSED(n)) const
410 {
411 #ifdef __WIN32__
412 return wxDF_FILENAME;
413 #else
414 // TODO: how to implement this in WIN16?
415 return wxDF_TEXT;
416 #endif
417 }
418
419 #endif
420 // wxUSE_DRAG_AND_DROP