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