]>
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
52 #include "wx/msw/ole/oleutils.h"
54 // ----------------------------------------------------------------------------
55 // IDropTarget interface: forward all interesting things to wxDropTarget
56 // (the name is unfortunate, but wx_I_DropTarget is not at all the same thing
57 // as wxDropTarget which is 'public' class, while this one is private)
58 // ----------------------------------------------------------------------------
60 class wxIDropTarget
: public IDropTarget
63 wxIDropTarget(wxDropTarget
*p
);
66 // accessors for wxDropTarget
67 void SetHwnd(HWND hwnd
) { m_hwnd
= hwnd
; }
69 // IDropTarget methods
70 STDMETHODIMP
DragEnter(LPDATAOBJECT
, DWORD
, POINTL
, LPDWORD
);
71 STDMETHODIMP
DragOver(DWORD
, POINTL
, LPDWORD
);
72 STDMETHODIMP
DragLeave();
73 STDMETHODIMP
Drop(LPDATAOBJECT
, DWORD
, POINTL
, LPDWORD
);
75 DECLARE_IUNKNOWN_METHODS
;
78 IDataObject
*m_pIDataObject
; // !NULL between DragEnter and DragLeave/Drop
79 wxDropTarget
*m_pTarget
; // the real target (we're just a proxy)
81 HWND m_hwnd
; // window we're associated with
83 // get default drop effect for given keyboard flags
84 static inline DWORD
GetDropEffect(DWORD flags
);
87 // ----------------------------------------------------------------------------
89 // ----------------------------------------------------------------------------
91 static wxDragResult
ConvertDragEffectToResult(DWORD dwEffect
);
92 static DWORD
ConvertDragResultToEffect(wxDragResult result
);
94 // ============================================================================
95 // wxIDropTarget implementation
96 // ============================================================================
98 // Name : static wxIDropTarget::GetDropEffect
99 // Purpose : determine the drop operation from keyboard/mouse state.
100 // Returns : DWORD combined from DROPEFFECT_xxx constants
101 // Params : [in] DWORD flags kbd & mouse flags as passed to
102 // IDropTarget methods
103 // Notes : We do "move" normally and "copy" if <Ctrl> is pressed,
104 // which is the standard behaviour (currently there is no
105 // way to redefine it)
106 DWORD
wxIDropTarget::GetDropEffect(DWORD flags
)
108 return flags
& MK_CONTROL
? DROPEFFECT_COPY
: DROPEFFECT_MOVE
;
111 wxIDropTarget::wxIDropTarget(wxDropTarget
*pTarget
)
115 m_pIDataObject
= NULL
;
118 wxIDropTarget::~wxIDropTarget()
122 BEGIN_IID_TABLE(wxIDropTarget
)
127 IMPLEMENT_IUNKNOWN_METHODS(wxIDropTarget
)
129 // Name : wxIDropTarget::DragEnter
130 // Purpose : Called when the mouse enters the window (dragging something)
132 // Params : [in] IDataObject *pIDataSource : source data
133 // [in] DWORD grfKeyState : kbd & mouse state
134 // [in] POINTL pt : mouse coordinates
135 // [out]DWORD *pdwEffect : effect flag
137 STDMETHODIMP
wxIDropTarget::DragEnter(IDataObject
*pIDataSource
,
142 wxLogDebug(wxT("IDropTarget::DragEnter"));
144 wxASSERT( m_pIDataObject
== NULL
);
146 if ( !m_pTarget
->IsAcceptedData(pIDataSource
) ) {
147 // we don't accept this kind of data
148 *pdwEffect
= DROPEFFECT_NONE
;
153 // get hold of the data object
154 m_pIDataObject
= pIDataSource
;
155 m_pIDataObject
->AddRef();
157 // we need client coordinates to pass to wxWin functions
158 if ( !ScreenToClient(m_hwnd
, (POINT
*)&pt
) )
160 wxLogLastError("ScreenToClient");
163 // give some visual feedback
164 *pdwEffect
= ConvertDragResultToEffect(
165 m_pTarget
->OnEnter(pt
.x
, pt
.y
,
166 ConvertDragEffectToResult(GetDropEffect(grfKeyState
))
173 // Name : wxIDropTarget::DragOver
174 // Purpose : Indicates that the mouse was moved inside the window represented
175 // by this drop target.
177 // Params : [in] DWORD grfKeyState kbd & mouse state
178 // [in] POINTL pt mouse coordinates
179 // [out]LPDWORD pdwEffect effect flag
180 // Notes : We're called on every WM_MOUSEMOVE, so this function should be
182 STDMETHODIMP
wxIDropTarget::DragOver(DWORD grfKeyState
,
186 // there are too many of them... wxLogDebug("IDropTarget::DragOver");
189 if ( m_pIDataObject
) {
190 result
= ConvertDragEffectToResult(GetDropEffect(grfKeyState
));
193 // can't accept data anyhow normally
197 // we need client coordinates to pass to wxWin functions
198 if ( !ScreenToClient(m_hwnd
, (POINT
*)&pt
) )
200 wxLogLastError("ScreenToClient");
203 *pdwEffect
= ConvertDragResultToEffect(
204 m_pTarget
->OnDragOver(pt
.x
, pt
.y
, result
)
210 // Name : wxIDropTarget::DragLeave
211 // Purpose : Informs the drop target that the operation has left its window.
213 // Notes : good place to do any clean-up
214 STDMETHODIMP
wxIDropTarget::DragLeave()
216 wxLogDebug(wxT("IDropTarget::DragLeave"));
218 // remove the UI feedback
219 m_pTarget
->OnLeave();
221 // release the held object
222 RELEASE_AND_NULL(m_pIDataObject
);
227 // Name : wxIDropTarget::Drop
228 // Purpose : Instructs the drop target to paste data that was just now
231 // Params : [in] IDataObject *pIDataSource the data to paste
232 // [in] DWORD grfKeyState kbd & mouse state
233 // [in] POINTL pt where the drop occured?
234 // [ouy]DWORD *pdwEffect operation effect
236 STDMETHODIMP
wxIDropTarget::Drop(IDataObject
*pIDataSource
,
241 wxLogDebug(wxT("IDropTarget::Drop"));
243 // TODO I don't know why there is this parameter, but so far I assume
244 // that it's the same we've already got in DragEnter
245 wxASSERT( m_pIDataObject
== pIDataSource
);
247 // by default, nothing happens
248 *pdwEffect
= DROPEFFECT_NONE
;
250 // we need client coordinates to pass to wxWin functions
251 if ( !ScreenToClient(m_hwnd
, (POINT
*)&pt
) )
253 wxLogLastError("ScreenToClient");
256 // first ask the drop target if it wants data
257 if ( m_pTarget
->OnDrop(pt
.x
, pt
.y
) ) {
258 // it does, so give it the data source
259 m_pTarget
->SetDataSource(pIDataSource
);
261 // and now it has the data
262 wxDragResult rc
= ConvertDragEffectToResult(GetDropEffect(grfKeyState
));
263 rc
= m_pTarget
->OnData(pt
.x
, pt
.y
, rc
);
264 if ( wxIsDragResultOk(rc
) ) {
265 // operation succeeded
266 *pdwEffect
= ConvertDragResultToEffect(rc
);
268 //else: *pdwEffect is already DROPEFFECT_NONE
270 //else: OnDrop() returned FALSE, no need to copy data
272 // release the held object
273 RELEASE_AND_NULL(m_pIDataObject
);
278 // ============================================================================
279 // wxDropTarget implementation
280 // ============================================================================
282 // ----------------------------------------------------------------------------
284 // ----------------------------------------------------------------------------
286 wxDropTarget::wxDropTarget(wxDataObject
*dataObj
)
287 : wxDropTargetBase(dataObj
)
289 // create an IDropTarget implementation which will notify us about d&d
291 m_pIDropTarget
= new wxIDropTarget(this);
292 m_pIDropTarget
->AddRef();
295 wxDropTarget::~wxDropTarget()
297 ReleaseInterface(m_pIDropTarget
);
300 // ----------------------------------------------------------------------------
301 // [un]register drop handler
302 // ----------------------------------------------------------------------------
304 bool wxDropTarget::Register(WXHWND hwnd
)
306 HRESULT hr
= ::CoLockObjectExternal(m_pIDropTarget
, TRUE
, FALSE
);
308 wxLogApiError("CoLockObjectExternal", hr
);
312 hr
= ::RegisterDragDrop((HWND
) hwnd
, m_pIDropTarget
);
314 ::CoLockObjectExternal(m_pIDropTarget
, FALSE
, FALSE
);
316 wxLogApiError("RegisterDragDrop", hr
);
320 // we will need the window handle for coords transformation later
321 m_pIDropTarget
->SetHwnd((HWND
)hwnd
);
326 void wxDropTarget::Revoke(WXHWND hwnd
)
328 HRESULT hr
= ::RevokeDragDrop((HWND
) hwnd
);
331 wxLogApiError("RevokeDragDrop", hr
);
334 ::CoLockObjectExternal(m_pIDropTarget
, FALSE
, TRUE
);
336 m_pIDropTarget
->SetHwnd(0);
339 // ----------------------------------------------------------------------------
340 // base class pure virtuals
341 // ----------------------------------------------------------------------------
343 // OnDrop() is called only if we previously returned TRUE from
344 // IsAcceptedData(), so no need to check anything here
345 bool wxDropTarget::OnDrop(wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
))
350 // copy the data from the data source to the target data object
351 bool wxDropTarget::GetData()
353 wxDataFormat format
= GetSupportedFormat(m_pIDataSource
);
354 if ( format
== wxDF_INVALID
) {
355 // this is strange because IsAcceptedData() succeeded previously!
356 wxFAIL_MSG(wxT("strange - did supported formats list change?"));
363 fmtMemory
.cfFormat
= format
;
364 fmtMemory
.ptd
= NULL
;
365 fmtMemory
.dwAspect
= DVASPECT_CONTENT
;
366 fmtMemory
.lindex
= -1;
367 fmtMemory
.tymed
= TYMED_HGLOBAL
; // TODO to add other media
371 HRESULT hr
= m_pIDataSource
->GetData(&fmtMemory
, &stm
);
372 if ( SUCCEEDED(hr
) ) {
373 IDataObject
*dataObject
= m_dataObject
->GetInterface();
375 hr
= dataObject
->SetData(&fmtMemory
, &stm
, TRUE
);
376 if ( SUCCEEDED(hr
) ) {
380 wxLogLastError("IDataObject::SetData()");
384 wxLogLastError("IDataObject::GetData()");
390 // ----------------------------------------------------------------------------
391 // callbacks used by wxIDropTarget
392 // ----------------------------------------------------------------------------
394 // we need a data source, so wxIDropTarget gives it to us using this function
395 void wxDropTarget::SetDataSource(IDataObject
*pIDataSource
)
397 m_pIDataSource
= pIDataSource
;
400 // determine if we accept data of this type
401 bool wxDropTarget::IsAcceptedData(IDataObject
*pIDataSource
) const
403 return GetSupportedFormat(pIDataSource
) != wxDF_INVALID
;
406 // ----------------------------------------------------------------------------
408 // ----------------------------------------------------------------------------
410 wxDataFormat
wxDropTarget::GetSupportedFormat(IDataObject
*pIDataSource
) const
412 // this strucutre describes a data of any type (first field will be
413 // changing) being passed through global memory block.
414 static FORMATETC s_fmtMemory
= {
419 TYMED_HGLOBAL
// TODO is it worth supporting other tymeds here?
422 // get the list of supported formats
423 size_t nFormats
= m_dataObject
->GetFormatCount(wxDataObject::Set
);
424 wxDataFormat format
, *formats
;
425 formats
= nFormats
== 1 ? &format
: new wxDataFormat
[nFormats
];
427 m_dataObject
->GetAllFormats(formats
, wxDataObject::Set
);
429 // cycle through all supported formats
431 for ( n
= 0; n
< nFormats
; n
++ ) {
432 s_fmtMemory
.cfFormat
= formats
[n
];
434 // NB: don't use SUCCEEDED macro here: QueryGetData returns S_FALSE
435 // for file drag and drop (format == CF_HDROP)
436 if ( pIDataSource
->QueryGetData(&s_fmtMemory
) == S_OK
) {
443 if ( formats
!= &format
) {
444 // free memory if we allocated it
448 return n
< nFormats
? format
: wxFormatInvalid
;
451 // ----------------------------------------------------------------------------
453 // ----------------------------------------------------------------------------
455 static wxDragResult
ConvertDragEffectToResult(DWORD dwEffect
)
457 switch ( dwEffect
) {
458 case DROPEFFECT_COPY
:
461 case DROPEFFECT_MOVE
:
465 wxFAIL_MSG(wxT("invalid value in ConvertDragEffectToResult"));
468 case DROPEFFECT_NONE
:
473 static DWORD
ConvertDragResultToEffect(wxDragResult result
)
477 return DROPEFFECT_COPY
;
480 return DROPEFFECT_MOVE
;
483 wxFAIL_MSG(wxT("invalid value in ConvertDragResultToEffect"));
487 return DROPEFFECT_NONE
;
492 // wxUSE_DRAG_AND_DROP