]>
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_OLE && wxUSE_DRAG_AND_DROP
38 #if !defined(__GNUWIN32__) || wxUSE_NORLANDER_HEADERS
39 #if wxCHECK_W32API_VERSION( 1, 0 )
42 #include <shlobj.h> // for DROPFILES structure
55 #include "wx/msw/ole/oleutils.h"
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 // ----------------------------------------------------------------------------
63 class wxIDropTarget
: public IDropTarget
66 wxIDropTarget(wxDropTarget
*p
);
67 virtual ~wxIDropTarget();
69 // accessors for wxDropTarget
70 void SetHwnd(HWND hwnd
) { m_hwnd
= hwnd
; }
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
);
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 HWND m_hwnd
; // window we're associated with
86 // get default drop effect for given keyboard flags
87 static inline DWORD
GetDropEffect(DWORD flags
);
90 // ----------------------------------------------------------------------------
92 // ----------------------------------------------------------------------------
94 static wxDragResult
ConvertDragEffectToResult(DWORD dwEffect
);
95 static DWORD
ConvertDragResultToEffect(wxDragResult result
);
97 // ============================================================================
98 // wxIDropTarget implementation
99 // ============================================================================
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)
109 DWORD
wxIDropTarget::GetDropEffect(DWORD flags
)
111 return flags
& MK_CONTROL
? DROPEFFECT_COPY
: DROPEFFECT_MOVE
;
114 wxIDropTarget::wxIDropTarget(wxDropTarget
*pTarget
)
118 m_pIDataObject
= NULL
;
121 wxIDropTarget::~wxIDropTarget()
125 BEGIN_IID_TABLE(wxIDropTarget
)
130 IMPLEMENT_IUNKNOWN_METHODS(wxIDropTarget
)
132 // Name : wxIDropTarget::DragEnter
133 // Purpose : Called when the mouse enters the window (dragging something)
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
140 STDMETHODIMP
wxIDropTarget::DragEnter(IDataObject
*pIDataSource
,
145 wxLogDebug(wxT("IDropTarget::DragEnter"));
147 wxASSERT( m_pIDataObject
== NULL
);
149 if ( !m_pTarget
->IsAcceptedData(pIDataSource
) ) {
150 // we don't accept this kind of data
151 *pdwEffect
= DROPEFFECT_NONE
;
156 // get hold of the data object
157 m_pIDataObject
= pIDataSource
;
158 m_pIDataObject
->AddRef();
160 // we need client coordinates to pass to wxWin functions
161 if ( !ScreenToClient(m_hwnd
, (POINT
*)&pt
) )
163 wxLogLastError(wxT("ScreenToClient"));
166 // give some visual feedback
167 *pdwEffect
= ConvertDragResultToEffect(
168 m_pTarget
->OnEnter(pt
.x
, pt
.y
,
169 ConvertDragEffectToResult(GetDropEffect(grfKeyState
))
176 // Name : wxIDropTarget::DragOver
177 // Purpose : Indicates that the mouse was moved inside the window represented
178 // by this drop target.
180 // Params : [in] DWORD grfKeyState kbd & mouse state
181 // [in] POINTL pt mouse coordinates
182 // [out]LPDWORD pdwEffect effect flag
183 // Notes : We're called on every WM_MOUSEMOVE, so this function should be
185 STDMETHODIMP
wxIDropTarget::DragOver(DWORD grfKeyState
,
189 // there are too many of them... wxLogDebug("IDropTarget::DragOver");
192 if ( m_pIDataObject
) {
193 result
= ConvertDragEffectToResult(GetDropEffect(grfKeyState
));
196 // can't accept data anyhow normally
200 // we need client coordinates to pass to wxWin functions
201 if ( !ScreenToClient(m_hwnd
, (POINT
*)&pt
) )
203 wxLogLastError(wxT("ScreenToClient"));
206 *pdwEffect
= ConvertDragResultToEffect(
207 m_pTarget
->OnDragOver(pt
.x
, pt
.y
, result
)
213 // Name : wxIDropTarget::DragLeave
214 // Purpose : Informs the drop target that the operation has left its window.
216 // Notes : good place to do any clean-up
217 STDMETHODIMP
wxIDropTarget::DragLeave()
219 wxLogDebug(wxT("IDropTarget::DragLeave"));
221 // remove the UI feedback
222 m_pTarget
->OnLeave();
224 // release the held object
225 RELEASE_AND_NULL(m_pIDataObject
);
230 // Name : wxIDropTarget::Drop
231 // Purpose : Instructs the drop target to paste data that was just now
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
239 STDMETHODIMP
wxIDropTarget::Drop(IDataObject
*pIDataSource
,
244 wxLogDebug(wxT("IDropTarget::Drop"));
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
);
250 // by default, nothing happens
251 *pdwEffect
= DROPEFFECT_NONE
;
253 // we need client coordinates to pass to wxWin functions
254 if ( !ScreenToClient(m_hwnd
, (POINT
*)&pt
) )
256 wxLogLastError(wxT("ScreenToClient"));
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
);
264 // and now it has the data
265 wxDragResult rc
= ConvertDragEffectToResult(GetDropEffect(grfKeyState
));
266 rc
= m_pTarget
->OnData(pt
.x
, pt
.y
, rc
);
267 if ( wxIsDragResultOk(rc
) ) {
268 // operation succeeded
269 *pdwEffect
= ConvertDragResultToEffect(rc
);
271 //else: *pdwEffect is already DROPEFFECT_NONE
273 //else: OnDrop() returned FALSE, no need to copy data
275 // release the held object
276 RELEASE_AND_NULL(m_pIDataObject
);
281 // ============================================================================
282 // wxDropTarget implementation
283 // ============================================================================
285 // ----------------------------------------------------------------------------
287 // ----------------------------------------------------------------------------
289 wxDropTarget::wxDropTarget(wxDataObject
*dataObj
)
290 : wxDropTargetBase(dataObj
)
292 // create an IDropTarget implementation which will notify us about d&d
294 m_pIDropTarget
= new wxIDropTarget(this);
295 m_pIDropTarget
->AddRef();
298 wxDropTarget::~wxDropTarget()
300 ReleaseInterface(m_pIDropTarget
);
303 // ----------------------------------------------------------------------------
304 // [un]register drop handler
305 // ----------------------------------------------------------------------------
307 bool wxDropTarget::Register(WXHWND hwnd
)
309 HRESULT hr
= ::CoLockObjectExternal(m_pIDropTarget
, TRUE
, FALSE
);
311 wxLogApiError(wxT("CoLockObjectExternal"), hr
);
315 hr
= ::RegisterDragDrop((HWND
) hwnd
, m_pIDropTarget
);
317 ::CoLockObjectExternal(m_pIDropTarget
, FALSE
, FALSE
);
319 wxLogApiError(wxT("RegisterDragDrop"), hr
);
323 // we will need the window handle for coords transformation later
324 m_pIDropTarget
->SetHwnd((HWND
)hwnd
);
329 void wxDropTarget::Revoke(WXHWND hwnd
)
331 HRESULT hr
= ::RevokeDragDrop((HWND
) hwnd
);
334 wxLogApiError(wxT("RevokeDragDrop"), hr
);
337 ::CoLockObjectExternal(m_pIDropTarget
, FALSE
, TRUE
);
339 m_pIDropTarget
->SetHwnd(0);
342 // ----------------------------------------------------------------------------
343 // base class pure virtuals
344 // ----------------------------------------------------------------------------
346 // OnDrop() is called only if we previously returned TRUE from
347 // IsAcceptedData(), so no need to check anything here
348 bool wxDropTarget::OnDrop(wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
))
353 // copy the data from the data source to the target data object
354 bool wxDropTarget::GetData()
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?"));
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
374 HRESULT hr
= m_pIDataSource
->GetData(&fmtMemory
, &stm
);
375 if ( SUCCEEDED(hr
) ) {
376 IDataObject
*dataObject
= m_dataObject
->GetInterface();
378 hr
= dataObject
->SetData(&fmtMemory
, &stm
, TRUE
);
379 if ( SUCCEEDED(hr
) ) {
383 wxLogLastError(wxT("IDataObject::SetData()"));
387 wxLogLastError(wxT("IDataObject::GetData()"));
393 // ----------------------------------------------------------------------------
394 // callbacks used by wxIDropTarget
395 // ----------------------------------------------------------------------------
397 // we need a data source, so wxIDropTarget gives it to us using this function
398 void wxDropTarget::SetDataSource(IDataObject
*pIDataSource
)
400 m_pIDataSource
= pIDataSource
;
403 // determine if we accept data of this type
404 bool wxDropTarget::IsAcceptedData(IDataObject
*pIDataSource
) const
406 return GetSupportedFormat(pIDataSource
) != wxDF_INVALID
;
409 // ----------------------------------------------------------------------------
411 // ----------------------------------------------------------------------------
413 wxDataFormat
wxDropTarget::GetSupportedFormat(IDataObject
*pIDataSource
) const
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
= {
422 TYMED_HGLOBAL
// TODO is it worth supporting other tymeds here?
425 // get the list of supported formats
426 size_t nFormats
= m_dataObject
->GetFormatCount(wxDataObject::Set
);
428 wxDataFormat
*formats
;
429 formats
= nFormats
== 1 ? &format
: new wxDataFormat
[nFormats
];
431 m_dataObject
->GetAllFormats(formats
, wxDataObject::Set
);
433 // cycle through all supported formats
435 for ( n
= 0; n
< nFormats
; n
++ ) {
436 s_fmtMemory
.cfFormat
= formats
[n
];
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
) {
447 if ( formats
!= &format
) {
448 // free memory if we allocated it
452 return n
< nFormats
? format
: wxFormatInvalid
;
455 // ----------------------------------------------------------------------------
457 // ----------------------------------------------------------------------------
459 static wxDragResult
ConvertDragEffectToResult(DWORD dwEffect
)
461 switch ( dwEffect
) {
462 case DROPEFFECT_COPY
:
465 case DROPEFFECT_MOVE
:
469 wxFAIL_MSG(wxT("invalid value in ConvertDragEffectToResult"));
472 case DROPEFFECT_NONE
:
477 static DWORD
ConvertDragResultToEffect(wxDragResult result
)
481 return DROPEFFECT_COPY
;
484 return DROPEFFECT_MOVE
;
487 wxFAIL_MSG(wxT("invalid value in ConvertDragResultToEffect"));
491 return DROPEFFECT_NONE
;
496 // wxUSE_DRAG_AND_DROP