]>
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
35 #include "wx/msw/private.h"
44 #if !defined(__GNUWIN32__) || wxUSE_NORLANDER_HEADERS
45 #if wxCHECK_W32API_VERSION( 1, 0 )
46 #include "wx/msw/wrapwin.h"
48 #include <shlobj.h> // for DROPFILES structure
56 #include "wx/msw/ole/oleutils.h"
58 // ----------------------------------------------------------------------------
59 // IDropTarget interface: forward all interesting things to wxDropTarget
60 // (the name is unfortunate, but wx_I_DropTarget is not at all the same thing
61 // as wxDropTarget which is 'public' class, while this one is private)
62 // ----------------------------------------------------------------------------
64 class wxIDropTarget
: public IDropTarget
67 wxIDropTarget(wxDropTarget
*p
);
68 virtual ~wxIDropTarget();
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
);
90 DECLARE_NO_COPY_CLASS(wxIDropTarget
)
93 // ----------------------------------------------------------------------------
95 // ----------------------------------------------------------------------------
97 static wxDragResult
ConvertDragEffectToResult(DWORD dwEffect
);
98 static DWORD
ConvertDragResultToEffect(wxDragResult result
);
100 // ============================================================================
101 // wxIDropTarget implementation
102 // ============================================================================
104 // Name : static wxIDropTarget::GetDropEffect
105 // Purpose : determine the drop operation from keyboard/mouse state.
106 // Returns : DWORD combined from DROPEFFECT_xxx constants
107 // Params : [in] DWORD flags kbd & mouse flags as passed to
108 // IDropTarget methods
109 // Notes : We do "move" normally and "copy" if <Ctrl> is pressed,
110 // which is the standard behaviour (currently there is no
111 // way to redefine it)
112 DWORD
wxIDropTarget::GetDropEffect(DWORD flags
)
114 return flags
& MK_CONTROL
? DROPEFFECT_COPY
: DROPEFFECT_MOVE
;
117 wxIDropTarget::wxIDropTarget(wxDropTarget
*pTarget
)
120 m_pIDataObject
= NULL
;
123 wxIDropTarget::~wxIDropTarget()
127 BEGIN_IID_TABLE(wxIDropTarget
)
132 IMPLEMENT_IUNKNOWN_METHODS(wxIDropTarget
)
134 // Name : wxIDropTarget::DragEnter
135 // Purpose : Called when the mouse enters the window (dragging something)
137 // Params : [in] IDataObject *pIDataSource : source data
138 // [in] DWORD grfKeyState : kbd & mouse state
139 // [in] POINTL pt : mouse coordinates
140 // [out]DWORD *pdwEffect : effect flag
142 STDMETHODIMP
wxIDropTarget::DragEnter(IDataObject
*pIDataSource
,
147 wxLogTrace(wxTRACE_OleCalls
, wxT("IDropTarget::DragEnter"));
149 wxASSERT_MSG( m_pIDataObject
== NULL
,
150 _T("drop target must have data object") );
152 // show the list of formats supported by the source data object for the
153 // debugging purposes, this is quite useful sometimes - please don't remove
155 IEnumFORMATETC
*penumFmt
;
156 if ( SUCCEEDED(pIDataSource
->EnumFormatEtc(DATADIR_GET
, &penumFmt
)) )
159 while ( penumFmt
->Next(1, &fmt
, NULL
) == S_OK
)
161 wxLogDebug(_T("Drop source supports format %s"),
162 wxDataObject::GetFormatName(fmt
.cfFormat
));
169 wxLogLastError(_T("IDataObject::EnumFormatEtc"));
173 if ( !m_pTarget
->IsAcceptedData(pIDataSource
) ) {
174 // we don't accept this kind of data
175 *pdwEffect
= DROPEFFECT_NONE
;
180 // get hold of the data object
181 m_pIDataObject
= pIDataSource
;
182 m_pIDataObject
->AddRef();
184 // we need client coordinates to pass to wxWin functions
185 if ( !ScreenToClient(m_hwnd
, (POINT
*)&pt
) )
187 wxLogLastError(wxT("ScreenToClient"));
190 // give some visual feedback
191 *pdwEffect
= ConvertDragResultToEffect(
192 m_pTarget
->OnEnter(pt
.x
, pt
.y
,
193 ConvertDragEffectToResult(GetDropEffect(grfKeyState
))
200 // Name : wxIDropTarget::DragOver
201 // Purpose : Indicates that the mouse was moved inside the window represented
202 // by this drop target.
204 // Params : [in] DWORD grfKeyState kbd & mouse state
205 // [in] POINTL pt mouse coordinates
206 // [out]LPDWORD pdwEffect effect flag
207 // Notes : We're called on every WM_MOUSEMOVE, so this function should be
209 STDMETHODIMP
wxIDropTarget::DragOver(DWORD grfKeyState
,
213 // there are too many of them... wxLogDebug("IDropTarget::DragOver");
216 if ( m_pIDataObject
) {
217 result
= ConvertDragEffectToResult(GetDropEffect(grfKeyState
));
220 // can't accept data anyhow normally
224 // we need client coordinates to pass to wxWin functions
225 if ( !ScreenToClient(m_hwnd
, (POINT
*)&pt
) )
227 wxLogLastError(wxT("ScreenToClient"));
230 *pdwEffect
= ConvertDragResultToEffect(
231 m_pTarget
->OnDragOver(pt
.x
, pt
.y
, result
)
237 // Name : wxIDropTarget::DragLeave
238 // Purpose : Informs the drop target that the operation has left its window.
240 // Notes : good place to do any clean-up
241 STDMETHODIMP
wxIDropTarget::DragLeave()
243 wxLogTrace(wxTRACE_OleCalls
, wxT("IDropTarget::DragLeave"));
245 // remove the UI feedback
246 m_pTarget
->OnLeave();
248 // release the held object
249 RELEASE_AND_NULL(m_pIDataObject
);
254 // Name : wxIDropTarget::Drop
255 // Purpose : Instructs the drop target to paste data that was just now
258 // Params : [in] IDataObject *pIDataSource the data to paste
259 // [in] DWORD grfKeyState kbd & mouse state
260 // [in] POINTL pt where the drop occured?
261 // [ouy]DWORD *pdwEffect operation effect
263 STDMETHODIMP
wxIDropTarget::Drop(IDataObject
*pIDataSource
,
268 wxLogTrace(wxTRACE_OleCalls
, wxT("IDropTarget::Drop"));
270 // TODO I don't know why there is this parameter, but so far I assume
271 // that it's the same we've already got in DragEnter
272 wxASSERT( m_pIDataObject
== pIDataSource
);
274 // by default, nothing happens
275 *pdwEffect
= DROPEFFECT_NONE
;
277 // we need client coordinates to pass to wxWin functions
278 if ( !ScreenToClient(m_hwnd
, (POINT
*)&pt
) )
280 wxLogLastError(wxT("ScreenToClient"));
283 // first ask the drop target if it wants data
284 if ( m_pTarget
->OnDrop(pt
.x
, pt
.y
) ) {
285 // it does, so give it the data source
286 m_pTarget
->SetDataSource(pIDataSource
);
288 // and now it has the data
289 wxDragResult rc
= ConvertDragEffectToResult(GetDropEffect(grfKeyState
));
290 rc
= m_pTarget
->OnData(pt
.x
, pt
.y
, rc
);
291 if ( wxIsDragResultOk(rc
) ) {
292 // operation succeeded
293 *pdwEffect
= ConvertDragResultToEffect(rc
);
295 //else: *pdwEffect is already DROPEFFECT_NONE
297 //else: OnDrop() returned FALSE, no need to copy data
299 // release the held object
300 RELEASE_AND_NULL(m_pIDataObject
);
305 // ============================================================================
306 // wxDropTarget implementation
307 // ============================================================================
309 // ----------------------------------------------------------------------------
311 // ----------------------------------------------------------------------------
313 wxDropTarget::wxDropTarget(wxDataObject
*dataObj
)
314 : wxDropTargetBase(dataObj
)
316 // create an IDropTarget implementation which will notify us about d&d
318 m_pIDropTarget
= new wxIDropTarget(this);
319 m_pIDropTarget
->AddRef();
322 wxDropTarget::~wxDropTarget()
324 ReleaseInterface(m_pIDropTarget
);
327 // ----------------------------------------------------------------------------
328 // [un]register drop handler
329 // ----------------------------------------------------------------------------
331 bool wxDropTarget::Register(WXHWND hwnd
)
335 // May exist in later WinCE versions
337 hr
= ::CoLockObjectExternal(m_pIDropTarget
, TRUE
, FALSE
);
339 wxLogApiError(wxT("CoLockObjectExternal"), hr
);
344 hr
= ::RegisterDragDrop((HWND
) hwnd
, m_pIDropTarget
);
346 // May exist in later WinCE versions
348 ::CoLockObjectExternal(m_pIDropTarget
, FALSE
, FALSE
);
350 wxLogApiError(wxT("RegisterDragDrop"), hr
);
354 // we will need the window handle for coords transformation later
355 m_pIDropTarget
->SetHwnd((HWND
)hwnd
);
360 void wxDropTarget::Revoke(WXHWND hwnd
)
362 HRESULT hr
= ::RevokeDragDrop((HWND
) hwnd
);
365 wxLogApiError(wxT("RevokeDragDrop"), hr
);
368 // May exist in later WinCE versions
370 ::CoLockObjectExternal(m_pIDropTarget
, FALSE
, TRUE
);
373 m_pIDropTarget
->SetHwnd(0);
376 // ----------------------------------------------------------------------------
377 // base class pure virtuals
378 // ----------------------------------------------------------------------------
380 // OnDrop() is called only if we previously returned TRUE from
381 // IsAcceptedData(), so no need to check anything here
382 bool wxDropTarget::OnDrop(wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
))
387 // copy the data from the data source to the target data object
388 bool wxDropTarget::GetData()
390 wxDataFormat format
= GetSupportedFormat(m_pIDataSource
);
391 if ( format
== wxDF_INVALID
) {
392 // this is strange because IsAcceptedData() succeeded previously!
393 wxFAIL_MSG(wxT("strange - did supported formats list change?"));
400 fmtMemory
.cfFormat
= format
;
401 fmtMemory
.ptd
= NULL
;
402 fmtMemory
.dwAspect
= DVASPECT_CONTENT
;
403 fmtMemory
.lindex
= -1;
404 fmtMemory
.tymed
= TYMED_HGLOBAL
; // TODO to add other media
408 HRESULT hr
= m_pIDataSource
->GetData(&fmtMemory
, &stm
);
409 if ( SUCCEEDED(hr
) ) {
410 IDataObject
*dataObject
= m_dataObject
->GetInterface();
412 hr
= dataObject
->SetData(&fmtMemory
, &stm
, TRUE
);
413 if ( SUCCEEDED(hr
) ) {
417 wxLogApiError(wxT("IDataObject::SetData()"), hr
);
421 wxLogApiError(wxT("IDataObject::GetData()"), hr
);
427 // ----------------------------------------------------------------------------
428 // callbacks used by wxIDropTarget
429 // ----------------------------------------------------------------------------
431 // we need a data source, so wxIDropTarget gives it to us using this function
432 void wxDropTarget::SetDataSource(IDataObject
*pIDataSource
)
434 m_pIDataSource
= pIDataSource
;
437 // determine if we accept data of this type
438 bool wxDropTarget::IsAcceptedData(IDataObject
*pIDataSource
) const
440 return GetSupportedFormat(pIDataSource
) != wxDF_INVALID
;
443 // ----------------------------------------------------------------------------
445 // ----------------------------------------------------------------------------
447 wxDataFormat
wxDropTarget::GetSupportedFormat(IDataObject
*pIDataSource
) const
449 // this strucutre describes a data of any type (first field will be
450 // changing) being passed through global memory block.
451 static FORMATETC s_fmtMemory
= {
456 TYMED_HGLOBAL
// TODO is it worth supporting other tymeds here?
459 // get the list of supported formats
460 size_t nFormats
= m_dataObject
->GetFormatCount(wxDataObject::Set
);
462 wxDataFormat
*formats
;
463 formats
= nFormats
== 1 ? &format
: new wxDataFormat
[nFormats
];
465 m_dataObject
->GetAllFormats(formats
, wxDataObject::Set
);
467 // cycle through all supported formats
469 for ( n
= 0; n
< nFormats
; n
++ ) {
470 s_fmtMemory
.cfFormat
= formats
[n
];
472 // NB: don't use SUCCEEDED macro here: QueryGetData returns S_FALSE
473 // for file drag and drop (format == CF_HDROP)
474 if ( pIDataSource
->QueryGetData(&s_fmtMemory
) == S_OK
) {
481 if ( formats
!= &format
) {
482 // free memory if we allocated it
486 return n
< nFormats
? format
: wxFormatInvalid
;
489 // ----------------------------------------------------------------------------
491 // ----------------------------------------------------------------------------
493 static wxDragResult
ConvertDragEffectToResult(DWORD dwEffect
)
495 switch ( dwEffect
) {
496 case DROPEFFECT_COPY
:
499 case DROPEFFECT_LINK
:
502 case DROPEFFECT_MOVE
:
506 wxFAIL_MSG(wxT("invalid value in ConvertDragEffectToResult"));
509 case DROPEFFECT_NONE
:
514 static DWORD
ConvertDragResultToEffect(wxDragResult result
)
518 return DROPEFFECT_COPY
;
521 return DROPEFFECT_LINK
;
524 return DROPEFFECT_MOVE
;
527 wxFAIL_MSG(wxT("invalid value in ConvertDragResultToEffect"));
531 return DROPEFFECT_NONE
;
536 // wxUSE_DRAG_AND_DROP