]>
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 licence
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
);
89 DECLARE_NO_COPY_CLASS(wxIDropTarget
)
92 // ----------------------------------------------------------------------------
94 // ----------------------------------------------------------------------------
96 static wxDragResult
ConvertDragEffectToResult(DWORD dwEffect
);
97 static DWORD
ConvertDragResultToEffect(wxDragResult result
);
99 // ============================================================================
100 // wxIDropTarget implementation
101 // ============================================================================
103 // Name : static wxIDropTarget::GetDropEffect
104 // Purpose : determine the drop operation from keyboard/mouse state.
105 // Returns : DWORD combined from DROPEFFECT_xxx constants
106 // Params : [in] DWORD flags kbd & mouse flags as passed to
107 // IDropTarget methods
108 // Notes : We do "move" normally and "copy" if <Ctrl> is pressed,
109 // which is the standard behaviour (currently there is no
110 // way to redefine it)
111 DWORD
wxIDropTarget::GetDropEffect(DWORD flags
)
113 return flags
& MK_CONTROL
? DROPEFFECT_COPY
: DROPEFFECT_MOVE
;
116 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 wxLogTrace(wxTRACE_OleCalls
, wxT("IDropTarget::DragEnter"));
148 wxASSERT_MSG( m_pIDataObject
== NULL
,
149 _T("drop target must have data object") );
151 // show the list of formats supported by the source data object for the
152 // debugging purposes, this is quite useful sometimes - please don't remove
154 IEnumFORMATETC
*penumFmt
;
155 if ( SUCCEEDED(pIDataSource
->EnumFormatEtc(DATADIR_GET
, &penumFmt
)) )
158 while ( penumFmt
->Next(1, &fmt
, NULL
) == S_OK
)
160 wxLogDebug(_T("Drop source supports format %s"),
161 wxDataObject::GetFormatName(fmt
.cfFormat
));
168 wxLogLastError(_T("IDataObject::EnumFormatEtc"));
172 if ( !m_pTarget
->IsAcceptedData(pIDataSource
) ) {
173 // we don't accept this kind of data
174 *pdwEffect
= DROPEFFECT_NONE
;
179 // get hold of the data object
180 m_pIDataObject
= pIDataSource
;
181 m_pIDataObject
->AddRef();
183 // we need client coordinates to pass to wxWin functions
184 if ( !ScreenToClient(m_hwnd
, (POINT
*)&pt
) )
186 wxLogLastError(wxT("ScreenToClient"));
189 // give some visual feedback
190 *pdwEffect
= ConvertDragResultToEffect(
191 m_pTarget
->OnEnter(pt
.x
, pt
.y
,
192 ConvertDragEffectToResult(GetDropEffect(grfKeyState
))
199 // Name : wxIDropTarget::DragOver
200 // Purpose : Indicates that the mouse was moved inside the window represented
201 // by this drop target.
203 // Params : [in] DWORD grfKeyState kbd & mouse state
204 // [in] POINTL pt mouse coordinates
205 // [out]LPDWORD pdwEffect effect flag
206 // Notes : We're called on every WM_MOUSEMOVE, so this function should be
208 STDMETHODIMP
wxIDropTarget::DragOver(DWORD grfKeyState
,
212 // there are too many of them... wxLogDebug("IDropTarget::DragOver");
215 if ( m_pIDataObject
) {
216 result
= ConvertDragEffectToResult(GetDropEffect(grfKeyState
));
219 // can't accept data anyhow normally
223 // we need client coordinates to pass to wxWin functions
224 if ( !ScreenToClient(m_hwnd
, (POINT
*)&pt
) )
226 wxLogLastError(wxT("ScreenToClient"));
229 *pdwEffect
= ConvertDragResultToEffect(
230 m_pTarget
->OnDragOver(pt
.x
, pt
.y
, result
)
236 // Name : wxIDropTarget::DragLeave
237 // Purpose : Informs the drop target that the operation has left its window.
239 // Notes : good place to do any clean-up
240 STDMETHODIMP
wxIDropTarget::DragLeave()
242 wxLogTrace(wxTRACE_OleCalls
, wxT("IDropTarget::DragLeave"));
244 // remove the UI feedback
245 m_pTarget
->OnLeave();
247 // release the held object
248 RELEASE_AND_NULL(m_pIDataObject
);
253 // Name : wxIDropTarget::Drop
254 // Purpose : Instructs the drop target to paste data that was just now
257 // Params : [in] IDataObject *pIDataSource the data to paste
258 // [in] DWORD grfKeyState kbd & mouse state
259 // [in] POINTL pt where the drop occured?
260 // [ouy]DWORD *pdwEffect operation effect
262 STDMETHODIMP
wxIDropTarget::Drop(IDataObject
*pIDataSource
,
267 wxLogTrace(wxTRACE_OleCalls
, wxT("IDropTarget::Drop"));
269 // TODO I don't know why there is this parameter, but so far I assume
270 // that it's the same we've already got in DragEnter
271 wxASSERT( m_pIDataObject
== pIDataSource
);
273 // by default, nothing happens
274 *pdwEffect
= DROPEFFECT_NONE
;
276 // we need client coordinates to pass to wxWin functions
277 if ( !ScreenToClient(m_hwnd
, (POINT
*)&pt
) )
279 wxLogLastError(wxT("ScreenToClient"));
282 // first ask the drop target if it wants data
283 if ( m_pTarget
->OnDrop(pt
.x
, pt
.y
) ) {
284 // it does, so give it the data source
285 m_pTarget
->SetDataSource(pIDataSource
);
287 // and now it has the data
288 wxDragResult rc
= ConvertDragEffectToResult(GetDropEffect(grfKeyState
));
289 rc
= m_pTarget
->OnData(pt
.x
, pt
.y
, rc
);
290 if ( wxIsDragResultOk(rc
) ) {
291 // operation succeeded
292 *pdwEffect
= ConvertDragResultToEffect(rc
);
294 //else: *pdwEffect is already DROPEFFECT_NONE
296 //else: OnDrop() returned FALSE, no need to copy data
298 // release the held object
299 RELEASE_AND_NULL(m_pIDataObject
);
304 // ============================================================================
305 // wxDropTarget implementation
306 // ============================================================================
308 // ----------------------------------------------------------------------------
310 // ----------------------------------------------------------------------------
312 wxDropTarget::wxDropTarget(wxDataObject
*dataObj
)
313 : wxDropTargetBase(dataObj
)
315 // create an IDropTarget implementation which will notify us about d&d
317 m_pIDropTarget
= new wxIDropTarget(this);
318 m_pIDropTarget
->AddRef();
321 wxDropTarget::~wxDropTarget()
323 ReleaseInterface(m_pIDropTarget
);
326 // ----------------------------------------------------------------------------
327 // [un]register drop handler
328 // ----------------------------------------------------------------------------
330 bool wxDropTarget::Register(WXHWND hwnd
)
332 HRESULT hr
= ::CoLockObjectExternal(m_pIDropTarget
, TRUE
, FALSE
);
334 wxLogApiError(wxT("CoLockObjectExternal"), hr
);
338 hr
= ::RegisterDragDrop((HWND
) hwnd
, m_pIDropTarget
);
340 ::CoLockObjectExternal(m_pIDropTarget
, FALSE
, FALSE
);
342 wxLogApiError(wxT("RegisterDragDrop"), hr
);
346 // we will need the window handle for coords transformation later
347 m_pIDropTarget
->SetHwnd((HWND
)hwnd
);
352 void wxDropTarget::Revoke(WXHWND hwnd
)
354 HRESULT hr
= ::RevokeDragDrop((HWND
) hwnd
);
357 wxLogApiError(wxT("RevokeDragDrop"), hr
);
360 ::CoLockObjectExternal(m_pIDropTarget
, FALSE
, TRUE
);
362 m_pIDropTarget
->SetHwnd(0);
365 // ----------------------------------------------------------------------------
366 // base class pure virtuals
367 // ----------------------------------------------------------------------------
369 // OnDrop() is called only if we previously returned TRUE from
370 // IsAcceptedData(), so no need to check anything here
371 bool wxDropTarget::OnDrop(wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
))
376 // copy the data from the data source to the target data object
377 bool wxDropTarget::GetData()
379 wxDataFormat format
= GetSupportedFormat(m_pIDataSource
);
380 if ( format
== wxDF_INVALID
) {
381 // this is strange because IsAcceptedData() succeeded previously!
382 wxFAIL_MSG(wxT("strange - did supported formats list change?"));
389 fmtMemory
.cfFormat
= format
;
390 fmtMemory
.ptd
= NULL
;
391 fmtMemory
.dwAspect
= DVASPECT_CONTENT
;
392 fmtMemory
.lindex
= -1;
393 fmtMemory
.tymed
= TYMED_HGLOBAL
; // TODO to add other media
397 HRESULT hr
= m_pIDataSource
->GetData(&fmtMemory
, &stm
);
398 if ( SUCCEEDED(hr
) ) {
399 IDataObject
*dataObject
= m_dataObject
->GetInterface();
401 hr
= dataObject
->SetData(&fmtMemory
, &stm
, TRUE
);
402 if ( SUCCEEDED(hr
) ) {
406 wxLogApiError(wxT("IDataObject::SetData()"), hr
);
410 wxLogApiError(wxT("IDataObject::GetData()"), hr
);
416 // ----------------------------------------------------------------------------
417 // callbacks used by wxIDropTarget
418 // ----------------------------------------------------------------------------
420 // we need a data source, so wxIDropTarget gives it to us using this function
421 void wxDropTarget::SetDataSource(IDataObject
*pIDataSource
)
423 m_pIDataSource
= pIDataSource
;
426 // determine if we accept data of this type
427 bool wxDropTarget::IsAcceptedData(IDataObject
*pIDataSource
) const
429 return GetSupportedFormat(pIDataSource
) != wxDF_INVALID
;
432 // ----------------------------------------------------------------------------
434 // ----------------------------------------------------------------------------
436 wxDataFormat
wxDropTarget::GetSupportedFormat(IDataObject
*pIDataSource
) const
438 // this strucutre describes a data of any type (first field will be
439 // changing) being passed through global memory block.
440 static FORMATETC s_fmtMemory
= {
445 TYMED_HGLOBAL
// TODO is it worth supporting other tymeds here?
448 // get the list of supported formats
449 size_t nFormats
= m_dataObject
->GetFormatCount(wxDataObject::Set
);
451 wxDataFormat
*formats
;
452 formats
= nFormats
== 1 ? &format
: new wxDataFormat
[nFormats
];
454 m_dataObject
->GetAllFormats(formats
, wxDataObject::Set
);
456 // cycle through all supported formats
458 for ( n
= 0; n
< nFormats
; n
++ ) {
459 s_fmtMemory
.cfFormat
= formats
[n
];
461 // NB: don't use SUCCEEDED macro here: QueryGetData returns S_FALSE
462 // for file drag and drop (format == CF_HDROP)
463 if ( pIDataSource
->QueryGetData(&s_fmtMemory
) == S_OK
) {
470 if ( formats
!= &format
) {
471 // free memory if we allocated it
475 return n
< nFormats
? format
: wxFormatInvalid
;
478 // ----------------------------------------------------------------------------
480 // ----------------------------------------------------------------------------
482 static wxDragResult
ConvertDragEffectToResult(DWORD dwEffect
)
484 switch ( dwEffect
) {
485 case DROPEFFECT_COPY
:
488 case DROPEFFECT_LINK
:
491 case DROPEFFECT_MOVE
:
495 wxFAIL_MSG(wxT("invalid value in ConvertDragEffectToResult"));
498 case DROPEFFECT_NONE
:
503 static DWORD
ConvertDragResultToEffect(wxDragResult result
)
507 return DROPEFFECT_COPY
;
510 return DROPEFFECT_LINK
;
513 return DROPEFFECT_MOVE
;
516 wxFAIL_MSG(wxT("invalid value in ConvertDragResultToEffect"));
520 return DROPEFFECT_NONE
;
525 // wxUSE_DRAG_AND_DROP