]>
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
38 #if !defined(__GNUWIN32__) || wxUSE_NORLANDER_HEADERS
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
);
64 // suppress gcc warning
70 // accessors for wxDropTarget
71 void SetHwnd(HWND hwnd
) { m_hwnd
= hwnd
; }
73 // IDropTarget methods
74 STDMETHODIMP
DragEnter(LPDATAOBJECT
, DWORD
, POINTL
, LPDWORD
);
75 STDMETHODIMP
DragOver(DWORD
, POINTL
, LPDWORD
);
76 STDMETHODIMP
DragLeave();
77 STDMETHODIMP
Drop(LPDATAOBJECT
, DWORD
, POINTL
, LPDWORD
);
79 DECLARE_IUNKNOWN_METHODS
;
82 IDataObject
*m_pIDataObject
; // !NULL between DragEnter and DragLeave/Drop
83 wxDropTarget
*m_pTarget
; // the real target (we're just a proxy)
85 HWND m_hwnd
; // window we're associated with
87 // get default drop effect for given keyboard flags
88 static inline DWORD
GetDropEffect(DWORD flags
);
91 // ----------------------------------------------------------------------------
93 // ----------------------------------------------------------------------------
95 static wxDragResult
ConvertDragEffectToResult(DWORD dwEffect
);
96 static DWORD
ConvertDragResultToEffect(wxDragResult result
);
98 // ============================================================================
99 // wxIDropTarget implementation
100 // ============================================================================
102 // Name : static wxIDropTarget::GetDropEffect
103 // Purpose : determine the drop operation from keyboard/mouse state.
104 // Returns : DWORD combined from DROPEFFECT_xxx constants
105 // Params : [in] DWORD flags kbd & mouse flags as passed to
106 // IDropTarget methods
107 // Notes : We do "move" normally and "copy" if <Ctrl> is pressed,
108 // which is the standard behaviour (currently there is no
109 // way to redefine it)
110 DWORD
wxIDropTarget::GetDropEffect(DWORD flags
)
112 return flags
& MK_CONTROL
? DROPEFFECT_COPY
: DROPEFFECT_MOVE
;
115 wxIDropTarget::wxIDropTarget(wxDropTarget
*pTarget
)
119 m_pIDataObject
= NULL
;
122 wxIDropTarget::~wxIDropTarget()
126 BEGIN_IID_TABLE(wxIDropTarget
)
131 IMPLEMENT_IUNKNOWN_METHODS(wxIDropTarget
)
133 // Name : wxIDropTarget::DragEnter
134 // Purpose : Called when the mouse enters the window (dragging something)
136 // Params : [in] IDataObject *pIDataSource : source data
137 // [in] DWORD grfKeyState : kbd & mouse state
138 // [in] POINTL pt : mouse coordinates
139 // [out]DWORD *pdwEffect : effect flag
141 STDMETHODIMP
wxIDropTarget::DragEnter(IDataObject
*pIDataSource
,
146 wxLogDebug(wxT("IDropTarget::DragEnter"));
148 wxASSERT( m_pIDataObject
== NULL
);
150 if ( !m_pTarget
->IsAcceptedData(pIDataSource
) ) {
151 // we don't accept this kind of data
152 *pdwEffect
= DROPEFFECT_NONE
;
157 // get hold of the data object
158 m_pIDataObject
= pIDataSource
;
159 m_pIDataObject
->AddRef();
161 // we need client coordinates to pass to wxWin functions
162 if ( !ScreenToClient(m_hwnd
, (POINT
*)&pt
) )
164 wxLogLastError(wxT("ScreenToClient"));
167 // give some visual feedback
168 *pdwEffect
= ConvertDragResultToEffect(
169 m_pTarget
->OnEnter(pt
.x
, pt
.y
,
170 ConvertDragEffectToResult(GetDropEffect(grfKeyState
))
177 // Name : wxIDropTarget::DragOver
178 // Purpose : Indicates that the mouse was moved inside the window represented
179 // by this drop target.
181 // Params : [in] DWORD grfKeyState kbd & mouse state
182 // [in] POINTL pt mouse coordinates
183 // [out]LPDWORD pdwEffect effect flag
184 // Notes : We're called on every WM_MOUSEMOVE, so this function should be
186 STDMETHODIMP
wxIDropTarget::DragOver(DWORD grfKeyState
,
190 // there are too many of them... wxLogDebug("IDropTarget::DragOver");
193 if ( m_pIDataObject
) {
194 result
= ConvertDragEffectToResult(GetDropEffect(grfKeyState
));
197 // can't accept data anyhow normally
201 // we need client coordinates to pass to wxWin functions
202 if ( !ScreenToClient(m_hwnd
, (POINT
*)&pt
) )
204 wxLogLastError(wxT("ScreenToClient"));
207 *pdwEffect
= ConvertDragResultToEffect(
208 m_pTarget
->OnDragOver(pt
.x
, pt
.y
, result
)
214 // Name : wxIDropTarget::DragLeave
215 // Purpose : Informs the drop target that the operation has left its window.
217 // Notes : good place to do any clean-up
218 STDMETHODIMP
wxIDropTarget::DragLeave()
220 wxLogDebug(wxT("IDropTarget::DragLeave"));
222 // remove the UI feedback
223 m_pTarget
->OnLeave();
225 // release the held object
226 RELEASE_AND_NULL(m_pIDataObject
);
231 // Name : wxIDropTarget::Drop
232 // Purpose : Instructs the drop target to paste data that was just now
235 // Params : [in] IDataObject *pIDataSource the data to paste
236 // [in] DWORD grfKeyState kbd & mouse state
237 // [in] POINTL pt where the drop occured?
238 // [ouy]DWORD *pdwEffect operation effect
240 STDMETHODIMP
wxIDropTarget::Drop(IDataObject
*pIDataSource
,
245 wxLogDebug(wxT("IDropTarget::Drop"));
247 // TODO I don't know why there is this parameter, but so far I assume
248 // that it's the same we've already got in DragEnter
249 wxASSERT( m_pIDataObject
== pIDataSource
);
251 // by default, nothing happens
252 *pdwEffect
= DROPEFFECT_NONE
;
254 // we need client coordinates to pass to wxWin functions
255 if ( !ScreenToClient(m_hwnd
, (POINT
*)&pt
) )
257 wxLogLastError(wxT("ScreenToClient"));
260 // first ask the drop target if it wants data
261 if ( m_pTarget
->OnDrop(pt
.x
, pt
.y
) ) {
262 // it does, so give it the data source
263 m_pTarget
->SetDataSource(pIDataSource
);
265 // and now it has the data
266 wxDragResult rc
= ConvertDragEffectToResult(GetDropEffect(grfKeyState
));
267 rc
= m_pTarget
->OnData(pt
.x
, pt
.y
, rc
);
268 if ( wxIsDragResultOk(rc
) ) {
269 // operation succeeded
270 *pdwEffect
= ConvertDragResultToEffect(rc
);
272 //else: *pdwEffect is already DROPEFFECT_NONE
274 //else: OnDrop() returned FALSE, no need to copy data
276 // release the held object
277 RELEASE_AND_NULL(m_pIDataObject
);
282 // ============================================================================
283 // wxDropTarget implementation
284 // ============================================================================
286 // ----------------------------------------------------------------------------
288 // ----------------------------------------------------------------------------
290 wxDropTarget::wxDropTarget(wxDataObject
*dataObj
)
291 : wxDropTargetBase(dataObj
)
293 // create an IDropTarget implementation which will notify us about d&d
295 m_pIDropTarget
= new wxIDropTarget(this);
296 m_pIDropTarget
->AddRef();
299 wxDropTarget::~wxDropTarget()
301 ReleaseInterface(m_pIDropTarget
);
304 // ----------------------------------------------------------------------------
305 // [un]register drop handler
306 // ----------------------------------------------------------------------------
308 bool wxDropTarget::Register(WXHWND hwnd
)
310 HRESULT hr
= ::CoLockObjectExternal(m_pIDropTarget
, TRUE
, FALSE
);
312 wxLogApiError("CoLockObjectExternal", hr
);
316 hr
= ::RegisterDragDrop((HWND
) hwnd
, m_pIDropTarget
);
318 ::CoLockObjectExternal(m_pIDropTarget
, FALSE
, FALSE
);
320 wxLogApiError("RegisterDragDrop", hr
);
324 // we will need the window handle for coords transformation later
325 m_pIDropTarget
->SetHwnd((HWND
)hwnd
);
330 void wxDropTarget::Revoke(WXHWND hwnd
)
332 HRESULT hr
= ::RevokeDragDrop((HWND
) hwnd
);
335 wxLogApiError("RevokeDragDrop", hr
);
338 ::CoLockObjectExternal(m_pIDropTarget
, FALSE
, TRUE
);
340 m_pIDropTarget
->SetHwnd(0);
343 // ----------------------------------------------------------------------------
344 // base class pure virtuals
345 // ----------------------------------------------------------------------------
347 // OnDrop() is called only if we previously returned TRUE from
348 // IsAcceptedData(), so no need to check anything here
349 bool wxDropTarget::OnDrop(wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
))
354 // copy the data from the data source to the target data object
355 bool wxDropTarget::GetData()
357 wxDataFormat format
= GetSupportedFormat(m_pIDataSource
);
358 if ( format
== wxDF_INVALID
) {
359 // this is strange because IsAcceptedData() succeeded previously!
360 wxFAIL_MSG(wxT("strange - did supported formats list change?"));
367 fmtMemory
.cfFormat
= format
;
368 fmtMemory
.ptd
= NULL
;
369 fmtMemory
.dwAspect
= DVASPECT_CONTENT
;
370 fmtMemory
.lindex
= -1;
371 fmtMemory
.tymed
= TYMED_HGLOBAL
; // TODO to add other media
375 HRESULT hr
= m_pIDataSource
->GetData(&fmtMemory
, &stm
);
376 if ( SUCCEEDED(hr
) ) {
377 IDataObject
*dataObject
= m_dataObject
->GetInterface();
379 hr
= dataObject
->SetData(&fmtMemory
, &stm
, TRUE
);
380 if ( SUCCEEDED(hr
) ) {
384 wxLogLastError(wxT("IDataObject::SetData()"));
388 wxLogLastError(wxT("IDataObject::GetData()"));
394 // ----------------------------------------------------------------------------
395 // callbacks used by wxIDropTarget
396 // ----------------------------------------------------------------------------
398 // we need a data source, so wxIDropTarget gives it to us using this function
399 void wxDropTarget::SetDataSource(IDataObject
*pIDataSource
)
401 m_pIDataSource
= pIDataSource
;
404 // determine if we accept data of this type
405 bool wxDropTarget::IsAcceptedData(IDataObject
*pIDataSource
) const
407 return GetSupportedFormat(pIDataSource
) != wxDF_INVALID
;
410 // ----------------------------------------------------------------------------
412 // ----------------------------------------------------------------------------
414 wxDataFormat
wxDropTarget::GetSupportedFormat(IDataObject
*pIDataSource
) const
416 // this strucutre describes a data of any type (first field will be
417 // changing) being passed through global memory block.
418 static FORMATETC s_fmtMemory
= {
423 TYMED_HGLOBAL
// TODO is it worth supporting other tymeds here?
426 // get the list of supported formats
427 size_t nFormats
= m_dataObject
->GetFormatCount(wxDataObject::Set
);
428 wxDataFormat format
, *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