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