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