]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/ole/droptgt.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: ole/droptgt.cpp
3 // Purpose: wxDropTarget implementation
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "droptgt.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
27 #if defined(__BORLANDC__)
33 #if wxUSE_DRAG_AND_DROP
39 #include <shlobj.h> // for DROPFILES structure
45 #include "wx/dataobj.h"
46 #include "wx/msw/ole/droptgt.h"
53 #include "wx/msw/ole/oleutils.h"
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 // ----------------------------------------------------------------------------
61 class wxIDropTarget
: public IDropTarget
64 wxIDropTarget(wxDropTarget
*p
);
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
);
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()
76 void SetSupportedFormat(wxDataFormat cfFormat
) { m_cfFormat
= cfFormat
; }
78 DECLARE_IUNKNOWN_METHODS
;
81 IDataObject
*m_pIDataObject
; // !NULL between DragEnter and DragLeave/Drop
82 wxDropTarget
*m_pTarget
; // the real target (we're just a proxy)
84 wxDataFormat m_cfFormat
; // the format in which to ask for data
87 static inline DWORD
GetDropEffect(DWORD flags
);
90 // ============================================================================
91 // wxIDropTarget implementation
92 // ============================================================================
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
)
104 return flags
& MK_CONTROL
? DROPEFFECT_COPY
: DROPEFFECT_MOVE
;
107 wxIDropTarget::wxIDropTarget(wxDropTarget
*pTarget
)
111 m_cfFormat
= wxDF_INVALID
;
112 m_pIDataObject
= NULL
;
115 wxIDropTarget::~wxIDropTarget()
119 BEGIN_IID_TABLE(wxIDropTarget
)
124 IMPLEMENT_IUNKNOWN_METHODS(wxIDropTarget
)
126 // Name : wxIDropTarget::DragEnter
127 // Purpose : Called when the mouse enters the window (dragging something)
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
134 STDMETHODIMP
wxIDropTarget::DragEnter(IDataObject
*pIDataSource
,
139 wxLogDebug(wxT("IDropTarget::DragEnter"));
141 wxASSERT( m_pIDataObject
== NULL
);
143 if ( !m_pTarget
->IsAcceptedData(pIDataSource
) ) {
144 // we don't accept this kind of data
145 *pdwEffect
= DROPEFFECT_NONE
;
150 // TODO should check the point also?
152 *pdwEffect
= GetDropEffect(grfKeyState
);
154 // get hold of the data object
155 m_pIDataObject
= pIDataSource
;
156 m_pIDataObject
->AddRef();
158 // give some visual feedback
159 m_pTarget
->OnEnter();
164 // Name : wxIDropTarget::DragOver
165 // Purpose : Indicates that the mouse was moved inside the window represented
166 // by this drop target.
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
173 STDMETHODIMP
wxIDropTarget::DragOver(DWORD grfKeyState
,
177 // there are too many of them... wxLogDebug("IDropTarget::DragOver");
179 *pdwEffect
= m_pIDataObject
== NULL
? DROPEFFECT_NONE
180 : GetDropEffect(grfKeyState
);
184 // Name : wxIDropTarget::DragLeave
185 // Purpose : Informs the drop target that the operation has left its window.
187 // Notes : good place to do any clean-up
188 STDMETHODIMP
wxIDropTarget::DragLeave()
190 wxLogDebug(wxT("IDropTarget::DragLeave"));
192 // remove the UI feedback
193 m_pTarget
->OnLeave();
195 // release the held object
196 RELEASE_AND_NULL(m_pIDataObject
);
201 // Name : wxIDropTarget::Drop
202 // Purpose : Instructs the drop target to paste data that was just now
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
210 STDMETHODIMP
wxIDropTarget::Drop(IDataObject
*pIDataSource
,
215 wxLogDebug(wxT("IDropTarget::Drop"));
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
);
222 *pdwEffect
= DROPEFFECT_NONE
;
224 // should be set by SetSupportedFormat() call
225 wxASSERT( m_cfFormat
!= wxDF_INVALID
);
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
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
241 GlobalUnlock(stm
.hGlobal
);
242 ReleaseStgMedium(&stm
);
247 // wxLogApiError("GetData", hr);
250 // release the held object
251 RELEASE_AND_NULL(m_pIDataObject
);
256 // ============================================================================
257 // wxDropTarget implementation
258 // ============================================================================
260 // ----------------------------------------------------------------------------
262 // ----------------------------------------------------------------------------
264 wxDropTarget::wxDropTarget()
266 // create an IDropTarget implementation which will notify us about
268 m_pIDropTarget
= new wxIDropTarget(this);
269 m_pIDropTarget
->AddRef();
272 wxDropTarget::~wxDropTarget()
274 ReleaseInterface(m_pIDropTarget
);
277 // ----------------------------------------------------------------------------
278 // [un]register drop handler
279 // ----------------------------------------------------------------------------
281 bool wxDropTarget::Register(WXHWND hwnd
)
283 HRESULT hr
= ::CoLockObjectExternal(m_pIDropTarget
, TRUE
, FALSE
);
285 wxLogApiError("CoLockObjectExternal", hr
);
289 hr
= ::RegisterDragDrop((HWND
) hwnd
, m_pIDropTarget
);
291 ::CoLockObjectExternal(m_pIDropTarget
, FALSE
, FALSE
);
293 wxLogApiError("RegisterDragDrop", hr
);
300 void wxDropTarget::Revoke(WXHWND hwnd
)
302 HRESULT hr
= ::RevokeDragDrop((HWND
) hwnd
);
305 wxLogApiError("RevokeDragDrop", hr
);
308 ::CoLockObjectExternal(m_pIDropTarget
, FALSE
, TRUE
);
311 // ----------------------------------------------------------------------------
312 // determine if we accept data of this type
313 // ----------------------------------------------------------------------------
314 bool wxDropTarget::IsAcceptedData(IDataObject
*pIDataSource
) const
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
= {
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
);
341 // ============================================================================
343 // ============================================================================
345 bool wxTextDropTarget::OnDrop(long x
, long y
, const void *pData
)
347 return OnDropText(x
, y
, (const wxChar
*)pData
);
350 size_t wxTextDropTarget::GetFormatCount() const
355 wxDataFormat
wxTextDropTarget::GetFormat(size_t WXUNUSED(n
)) const
360 // ============================================================================
362 // ============================================================================
364 bool wxFileDropTarget::OnDrop(long x
, long y
, const void *pData
)
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
372 HDROP hdrop
= (HDROP
)pData
; // NB: it works, but I'm not sure about it
374 // get number of files (magic value -1)
375 UINT nFiles
= ::DragQueryFile(hdrop
, (unsigned)-1, NULL
, 0u);
377 // for each file get the length, allocate memory and then get the name
378 wxChar
**aszFiles
= new wxChar
*[nFiles
];
380 for ( n
= 0; n
< nFiles
; n
++ ) {
381 // +1 for terminating NUL
382 len
= ::DragQueryFile(hdrop
, n
, NULL
, 0) + 1;
384 aszFiles
[n
] = new wxChar
[len
];
386 UINT len2
= ::DragQueryFile(hdrop
, n
, aszFiles
[n
], len
);
387 if ( len2
!= len
- 1 ) {
388 wxLogDebug(wxT("In wxFileDropTarget::OnDrop DragQueryFile returned %d "
389 "characters, %d expected."), len2
, len
- 1);
393 bool bResult
= OnDropFiles(x
, y
, nFiles
, (const wxChar
**) aszFiles
);
396 for ( n
= 0; n
< nFiles
; n
++ ) {
397 delete [] aszFiles
[n
];
404 size_t wxFileDropTarget::GetFormatCount() const
409 wxDataFormat
wxFileDropTarget::GetFormat(size_t WXUNUSED(n
)) const
412 return wxDF_FILENAME
;
414 // TODO: how to implement this in WIN16?
420 // wxUSE_DRAG_AND_DROP