]> git.saurik.com Git - wxWidgets.git/blame - src/msw/ole/droptgt.cpp
compilation fix
[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
47d67540 33#if wxUSE_DRAG_AND_DROP
bbf1f0e5 34
3f480da3 35#include "wx/log.h"
bbf1f0e5
KB
36
37#ifdef __WIN32__
3f480da3
VZ
38 #ifndef __GNUWIN32__
39 #include <shlobj.h> // for DROPFILES structure
40 #endif
bbf1f0e5 41#else
3f480da3 42 #include <shellapi.h>
bbf1f0e5
KB
43#endif
44
3f480da3
VZ
45#include "wx/dataobj.h"
46#include "wx/msw/ole/droptgt.h"
bbf1f0e5
KB
47
48#ifndef __WIN32__
3f480da3
VZ
49 #include <ole2.h>
50 #include <olestd.h>
bbf1f0e5
KB
51#endif
52
3f480da3 53#include "wx/msw/ole/oleutils.h"
bbf1f0e5
KB
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 ~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
3f480da3
VZ
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
bbf1f0e5
KB
76 void SetSupportedFormat(wxDataFormat cfFormat) { m_cfFormat = cfFormat; }
77
78 DECLARE_IUNKNOWN_METHODS;
79
80protected:
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
86private:
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.
3f480da3 96// Returns : DWORD combined from DROPEFFECT_xxx constants
bbf1f0e5
KB
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,
3f480da3 100// which is the standard behaviour (currently there is no
bbf1f0e5
KB
101// way to redefine it)
102DWORD wxIDropTarget::GetDropEffect(DWORD flags)
103{
104 return flags & MK_CONTROL ? DROPEFFECT_COPY : DROPEFFECT_MOVE;
105}
106
107wxIDropTarget::wxIDropTarget(wxDropTarget *pTarget)
3f480da3
VZ
108{
109 m_cRef = 0;
bbf1f0e5 110 m_pTarget = pTarget;
3f480da3
VZ
111 m_cfFormat = wxDF_INVALID;
112 m_pIDataObject = NULL;
bbf1f0e5
KB
113}
114
3f480da3
VZ
115wxIDropTarget::~wxIDropTarget()
116{
bbf1f0e5
KB
117}
118
119BEGIN_IID_TABLE(wxIDropTarget)
120 ADD_IID(Unknown)
121 ADD_IID(DropTarget)
122END_IID_TABLE;
123
124IMPLEMENT_IUNKNOWN_METHODS(wxIDropTarget)
125
bbf1f0e5
KB
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
3f480da3 133// Notes :
bbf1f0e5
KB
134STDMETHODIMP wxIDropTarget::DragEnter(IDataObject *pIDataSource,
135 DWORD grfKeyState,
136 POINTL pt,
137 DWORD *pdwEffect)
138{
5f8e1c16 139 wxLogDebug(_T("IDropTarget::DragEnter"));
bbf1f0e5
KB
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
3f480da3
VZ
150 // TODO should check the point also?
151
bbf1f0e5
KB
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
3f480da3 171// Notes : We're called on every WM_MOUSEMOVE, so this function should be
bbf1f0e5
KB
172// very efficient.
173STDMETHODIMP wxIDropTarget::DragOver(DWORD grfKeyState,
174 POINTL pt,
175 LPDWORD pdwEffect)
176{
177 // there are too many of them... wxLogDebug("IDropTarget::DragOver");
178
3f480da3 179 *pdwEffect = m_pIDataObject == NULL ? DROPEFFECT_NONE
bbf1f0e5
KB
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
188STDMETHODIMP wxIDropTarget::DragLeave()
189{
5f8e1c16 190 wxLogDebug(_T("IDropTarget::DragLeave"));
bbf1f0e5
KB
191
192 // remove the UI feedback
193 m_pTarget->OnLeave();
194
195 // release the held object
196 RELEASE_AND_NULL(m_pIDataObject);
3f480da3 197
bbf1f0e5
KB
198 return S_OK;
199}
200
201// Name : wxIDropTarget::Drop
3f480da3 202// Purpose : Instructs the drop target to paste data that was just now
bbf1f0e5
KB
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
3f480da3
VZ
209// Notes :
210STDMETHODIMP wxIDropTarget::Drop(IDataObject *pIDataSource,
211 DWORD grfKeyState,
212 POINTL pt,
bbf1f0e5
KB
213 DWORD *pdwEffect)
214{
5f8e1c16 215 wxLogDebug(_T("IDropTarget::Drop"));
bbf1f0e5 216
3f480da3
VZ
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
bbf1f0e5
KB
219 wxASSERT( m_pIDataObject == pIDataSource );
220
221 STGMEDIUM stm;
3f480da3
VZ
222 *pdwEffect = DROPEFFECT_NONE;
223
bbf1f0e5 224 // should be set by SetSupportedFormat() call
3f480da3 225 wxASSERT( m_cfFormat != wxDF_INVALID );
bbf1f0e5
KB
226
227 FORMATETC fmtMemory;
228 fmtMemory.cfFormat = m_cfFormat;
3f480da3 229 fmtMemory.ptd = NULL;
bbf1f0e5
KB
230 fmtMemory.dwAspect = DVASPECT_CONTENT;
231 fmtMemory.lindex = -1;
3f480da3 232 fmtMemory.tymed = TYMED_HGLOBAL; // TODO to add other media
bbf1f0e5
KB
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
264wxDropTarget::wxDropTarget()
265{
3f480da3 266 // create an IDropTarget implementation which will notify us about
bbf1f0e5
KB
267 // d&d operations.
268 m_pIDropTarget = new wxIDropTarget(this);
269 m_pIDropTarget->AddRef();
270}
271
272wxDropTarget::~wxDropTarget()
273{
274 ReleaseInterface(m_pIDropTarget);
275}
276
277// ----------------------------------------------------------------------------
278// [un]register drop handler
279// ----------------------------------------------------------------------------
280
281bool wxDropTarget::Register(WXHWND hwnd)
282{
283 HRESULT hr = ::CoLockObjectExternal(m_pIDropTarget, TRUE, FALSE);
284 if ( FAILED(hr) ) {
4075e9a4 285 wxLogApiError("CoLockObjectExternal", hr);
bbf1f0e5
KB
286 return FALSE;
287 }
288
289 hr = ::RegisterDragDrop((HWND) hwnd, m_pIDropTarget);
290 if ( FAILED(hr) ) {
291 ::CoLockObjectExternal(m_pIDropTarget, FALSE, FALSE);
292
4075e9a4 293 wxLogApiError("RegisterDragDrop", hr);
bbf1f0e5
KB
294 return FALSE;
295 }
296
297 return TRUE;
298}
299
300void wxDropTarget::Revoke(WXHWND hwnd)
301{
302 HRESULT hr = ::RevokeDragDrop((HWND) hwnd);
303
4075e9a4
VZ
304 if ( FAILED(hr) ) {
305 wxLogApiError("RevokeDragDrop", hr);
bbf1f0e5
KB
306 }
307
308 ::CoLockObjectExternal(m_pIDropTarget, FALSE, TRUE);
309}
310
311// ----------------------------------------------------------------------------
312// determine if we accept data of this type
313// ----------------------------------------------------------------------------
314bool 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.
3f480da3 318 static FORMATETC s_fmtMemory = {
bbf1f0e5 319 0,
3f480da3
VZ
320 NULL,
321 DVASPECT_CONTENT,
322 -1,
323 TYMED_HGLOBAL
bbf1f0e5
KB
324 };
325
326 // cycle thorugh all supported formats
327 for ( size_t n = 0; n < GetFormatCount(); n++ ) {
328 s_fmtMemory.cfFormat = GetFormat(n);
3f480da3
VZ
329 // NB: don't use SUCCEEDED macro here: QueryGetData returns 1 (whatever it
330 // means) for file drag and drop
bbf1f0e5
KB
331 if ( pIDataSource->QueryGetData(&s_fmtMemory) == S_OK ) {
332 // remember this format: we'll later ask for data in it
3f480da3 333 m_pIDropTarget->SetSupportedFormat((unsigned int)s_fmtMemory.cfFormat);
bbf1f0e5
KB
334 return TRUE;
335 }
336 }
337
338 return FALSE;
339}
340
341// ============================================================================
342// wxTextDropTarget
343// ============================================================================
344
345bool wxTextDropTarget::OnDrop(long x, long y, const void *pData)
346{
5f8e1c16 347 return OnDropText(x, y, (const wxChar *)pData);
bbf1f0e5
KB
348}
349
350size_t wxTextDropTarget::GetFormatCount() const
351{
352 return 1;
353}
354
355wxDataFormat wxTextDropTarget::GetFormat(size_t WXUNUSED(n)) const
356{
b3324be2 357 return wxDF_TEXT;
bbf1f0e5
KB
358}
359
360// ============================================================================
361// wxFileDropTarget
362// ============================================================================
363
364bool wxFileDropTarget::OnDrop(long x, long y, const void *pData)
365{
366 // the documentation states that the first member of DROPFILES structure
3f480da3 367 // is a "DWORD offset of double NUL terminated file list". What they mean by
bbf1f0e5
KB
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.
3f480da3 372 HDROP hdrop = (HDROP)pData; // NB: it works, but I'm not sure about it
bbf1f0e5
KB
373
374 // get number of files (magic value -1)
fd3f686c 375 UINT nFiles = ::DragQueryFile(hdrop, (unsigned)-1, NULL, 0u);
3f480da3 376
bbf1f0e5 377 // for each file get the length, allocate memory and then get the name
5f8e1c16 378 wxChar **aszFiles = new wxChar *[nFiles];
bbf1f0e5
KB
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
5f8e1c16 384 aszFiles[n] = new wxChar[len];
bbf1f0e5
KB
385
386 UINT len2 = ::DragQueryFile(hdrop, n, aszFiles[n], len);
387 if ( len2 != len - 1 ) {
5f8e1c16
OK
388 wxLogDebug(_T("In wxFileDropTarget::OnDrop DragQueryFile returned %d "
389 "characters, %d expected."), len2, len - 1);
bbf1f0e5
KB
390 }
391 }
392
5f8e1c16 393 bool bResult = OnDropFiles(x, y, nFiles, (const wxChar**) aszFiles);
bbf1f0e5
KB
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
404size_t wxFileDropTarget::GetFormatCount() const
405{
406 return 1;
407}
408
409wxDataFormat wxFileDropTarget::GetFormat(size_t WXUNUSED(n)) const
410{
411#ifdef __WIN32__
b3324be2 412 return wxDF_FILENAME;
bbf1f0e5
KB
413#else
414 // TODO: how to implement this in WIN16?
b3324be2 415 return wxDF_TEXT;
bbf1f0e5
KB
416#endif
417}
418
419#endif
47d67540 420 // wxUSE_DRAG_AND_DROP