]>
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 // IDropTarget methods
67 STDMETHODIMP
DragEnter(LPDATAOBJECT
, DWORD
, POINTL
, LPDWORD
);
68 STDMETHODIMP
DragOver(DWORD
, POINTL
, LPDWORD
);
69 STDMETHODIMP
DragLeave(void);
70 STDMETHODIMP
Drop(LPDATAOBJECT
, DWORD
, POINTL
, LPDWORD
);
72 DECLARE_IUNKNOWN_METHODS
;
75 IDataObject
*m_pIDataObject
; // !NULL between DragEnter and DragLeave/Drop
76 wxDropTarget
*m_pTarget
; // the real target (we're just a proxy)
79 static inline DWORD
GetDropEffect(DWORD flags
);
82 // ----------------------------------------------------------------------------
84 // ----------------------------------------------------------------------------
86 static wxDragResult
ConvertDragEffectToResult(DWORD dwEffect
);
87 static DWORD
ConvertDragResultToEffect(wxDragResult result
);
89 // ============================================================================
90 // wxIDropTarget implementation
91 // ============================================================================
93 // Name : static wxDropTarget::GetDropEffect
94 // Purpose : determine the drop operation from keyboard/mouse state.
95 // Returns : DWORD combined from DROPEFFECT_xxx constants
96 // Params : [in] DWORD flags kbd & mouse flags as passed to
97 // IDropTarget methods
98 // Notes : We do "move" normally and "copy" if <Ctrl> is pressed,
99 // which is the standard behaviour (currently there is no
100 // way to redefine it)
101 DWORD
wxIDropTarget::GetDropEffect(DWORD flags
)
103 return flags
& MK_CONTROL
? DROPEFFECT_COPY
: DROPEFFECT_MOVE
;
106 wxIDropTarget::wxIDropTarget(wxDropTarget
*pTarget
)
110 m_pIDataObject
= NULL
;
113 wxIDropTarget::~wxIDropTarget()
117 BEGIN_IID_TABLE(wxIDropTarget
)
122 IMPLEMENT_IUNKNOWN_METHODS(wxIDropTarget
)
124 // Name : wxIDropTarget::DragEnter
125 // Purpose : Called when the mouse enters the window (dragging something)
127 // Params : [in] IDataObject *pIDataSource : source data
128 // [in] DWORD grfKeyState : kbd & mouse state
129 // [in] POINTL pt : mouse coordinates
130 // [out]DWORD *pdwEffect : effect flag
132 STDMETHODIMP
wxIDropTarget::DragEnter(IDataObject
*pIDataSource
,
137 wxLogDebug(wxT("IDropTarget::DragEnter"));
139 wxASSERT( m_pIDataObject
== NULL
);
141 if ( !m_pTarget
->IsAcceptedData(pIDataSource
) ) {
142 // we don't accept this kind of data
143 *pdwEffect
= DROPEFFECT_NONE
;
148 // get hold of the data object
149 m_pIDataObject
= pIDataSource
;
150 m_pIDataObject
->AddRef();
152 // give some visual feedback
153 *pdwEffect
= ConvertDragResultToEffect(
154 m_pTarget
->OnEnter(pt
.x
, pt
.y
,
155 ConvertDragEffectToResult(
156 GetDropEffect(grfKeyState
)
164 // Name : wxIDropTarget::DragOver
165 // Purpose : Indicates that the mouse was moved inside the window represented
166 // by this drop target.
168 // Params : [in] DWORD grfKeyState kbd & mouse state
169 // [in] POINTL pt mouse coordinates
170 // [out]LPDWORD pdwEffect effect flag
171 // Notes : We're called on every WM_MOUSEMOVE, so this function should be
173 STDMETHODIMP
wxIDropTarget::DragOver(DWORD grfKeyState
,
177 // there are too many of them... wxLogDebug("IDropTarget::DragOver");
180 if ( m_pIDataObject
) {
181 result
= ConvertDragEffectToResult(GetDropEffect(grfKeyState
));
184 // can't accept data anyhow normally
188 *pdwEffect
= ConvertDragResultToEffect(
189 m_pTarget
->OnDragOver(pt
.x
, pt
.y
, result
)
195 // Name : wxIDropTarget::DragLeave
196 // Purpose : Informs the drop target that the operation has left its window.
198 // Notes : good place to do any clean-up
199 STDMETHODIMP
wxIDropTarget::DragLeave()
201 wxLogDebug(wxT("IDropTarget::DragLeave"));
203 // remove the UI feedback
204 m_pTarget
->OnLeave();
206 // release the held object
207 RELEASE_AND_NULL(m_pIDataObject
);
212 // Name : wxIDropTarget::Drop
213 // Purpose : Instructs the drop target to paste data that was just now
216 // Params : [in] IDataObject *pIDataSource the data to paste
217 // [in] DWORD grfKeyState kbd & mouse state
218 // [in] POINTL pt where the drop occured?
219 // [ouy]DWORD *pdwEffect operation effect
221 STDMETHODIMP
wxIDropTarget::Drop(IDataObject
*pIDataSource
,
226 wxLogDebug(wxT("IDropTarget::Drop"));
228 // TODO I don't know why there is this parameter, but so far I assume
229 // that it's the same we've already got in DragEnter
230 wxASSERT( m_pIDataObject
== pIDataSource
);
232 // by default, nothing happens
233 *pdwEffect
= DROPEFFECT_NONE
;
235 // first ask the drop target if it wants data
236 if ( m_pTarget
->OnDrop(pt
.x
, pt
.y
) ) {
237 // it does, so give it the data source
238 m_pTarget
->SetDataSource(pIDataSource
);
240 // and now it has the data
241 if ( m_pTarget
->OnData(pt
.x
, pt
.y
) ) {
242 // operation succeeded
243 *pdwEffect
= GetDropEffect(grfKeyState
);
245 //else: *pdwEffect is already DROPEFFECT_NONE
247 //else: OnDrop() returned FALSE, no need to copy data
249 // release the held object
250 RELEASE_AND_NULL(m_pIDataObject
);
255 // ============================================================================
256 // wxDropTarget implementation
257 // ============================================================================
259 // ----------------------------------------------------------------------------
261 // ----------------------------------------------------------------------------
263 wxDropTarget::wxDropTarget(wxDataObject
*dataObj
)
264 : wxDropTargetBase(dataObj
)
266 // create an IDropTarget implementation which will notify us about d&d
268 m_pIDropTarget
= new wxIDropTarget(this);
269 m_pIDropTarget
->AddRef();
272 wxDropTarget::~wxDropTarget()
274 ReleaseInterface(m_pIDropTarget
);
277 // ----------------------------------------------------------------------------
278 // [un]register drop handler
279 // ----------------------------------------------------------------------------
281 bool wxDropTarget::Register(WXHWND hwnd
)
283 HRESULT hr
= ::CoLockObjectExternal(m_pIDropTarget
, TRUE
, FALSE
);
285 wxLogApiError("CoLockObjectExternal", hr
);
289 hr
= ::RegisterDragDrop((HWND
) hwnd
, m_pIDropTarget
);
291 ::CoLockObjectExternal(m_pIDropTarget
, FALSE
, FALSE
);
293 wxLogApiError("RegisterDragDrop", hr
);
300 void wxDropTarget::Revoke(WXHWND hwnd
)
302 HRESULT hr
= ::RevokeDragDrop((HWND
) hwnd
);
305 wxLogApiError("RevokeDragDrop", hr
);
308 ::CoLockObjectExternal(m_pIDropTarget
, FALSE
, TRUE
);
311 // ----------------------------------------------------------------------------
312 // base class pure virtuals
313 // ----------------------------------------------------------------------------
315 // OnDrop() is called only if we previously returned TRUE from
316 // IsAcceptedData(), so no need to check anything here
317 bool wxDropTarget::OnDrop(wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
))
322 // copy the data from the data source to the target data object
323 bool wxDropTarget::GetData()
325 wxDataFormat format
= GetSupportedFormat(m_pIDataSource
);
326 if ( format
== wxDF_INVALID
) {
327 // this is strange because IsAcceptedData() succeeded previously!
328 wxFAIL_MSG(wxT("strange - did supported formats list change?"));
335 fmtMemory
.cfFormat
= format
;
336 fmtMemory
.ptd
= NULL
;
337 fmtMemory
.dwAspect
= DVASPECT_CONTENT
;
338 fmtMemory
.lindex
= -1;
339 fmtMemory
.tymed
= TYMED_HGLOBAL
; // TODO to add other media
343 HRESULT hr
= m_pIDataSource
->GetData(&fmtMemory
, &stm
);
344 if ( SUCCEEDED(hr
) ) {
345 IDataObject
*dataObject
= m_dataObject
->GetInterface();
347 hr
= dataObject
->SetData(&fmtMemory
, &stm
, TRUE
);
348 if ( SUCCEEDED(hr
) ) {
352 wxLogLastError("IDataObject::SetData()");
356 wxLogLastError("IDataObject::GetData()");
362 // ----------------------------------------------------------------------------
363 // callbacks used by wxIDropTarget
364 // ----------------------------------------------------------------------------
366 // we need a data source, so wxIDropTarget gives it to us using this function
367 void wxDropTarget::SetDataSource(IDataObject
*pIDataSource
)
369 m_pIDataSource
= pIDataSource
;
372 // determine if we accept data of this type
373 bool wxDropTarget::IsAcceptedData(IDataObject
*pIDataSource
) const
375 return GetSupportedFormat(pIDataSource
) != wxDF_INVALID
;
378 // ----------------------------------------------------------------------------
380 // ----------------------------------------------------------------------------
382 wxDataFormat
wxDropTarget::GetSupportedFormat(IDataObject
*pIDataSource
) const
384 // this strucutre describes a data of any type (first field will be
385 // changing) being passed through global memory block.
386 static FORMATETC s_fmtMemory
= {
391 TYMED_HGLOBAL
// TODO is it worth supporting other tymeds here?
394 // get the list of supported formats
395 size_t nFormats
= m_dataObject
->GetFormatCount(wxDataObject::Set
);
396 wxDataFormat format
, *formats
;
397 formats
= nFormats
== 1 ? &format
: new wxDataFormat
[nFormats
];
399 m_dataObject
->GetAllFormats(formats
, wxDataObject::Set
);
401 // cycle through all supported formats
403 for ( n
= 0; n
< nFormats
; n
++ ) {
404 s_fmtMemory
.cfFormat
= formats
[n
];
406 // NB: don't use SUCCEEDED macro here: QueryGetData returns S_FALSE
407 // for file drag and drop (format == CF_HDROP)
408 if ( pIDataSource
->QueryGetData(&s_fmtMemory
) == S_OK
) {
415 if ( formats
!= &format
) {
416 // free memory if we allocated it
420 return n
< nFormats
? format
: wxFormatInvalid
;
423 // ----------------------------------------------------------------------------
425 // ----------------------------------------------------------------------------
427 wxTextDropTarget::wxTextDropTarget()
428 : wxDropTarget(new wxTextDataObject
)
432 bool wxTextDropTarget::OnData(wxCoord x
, wxCoord y
)
437 return OnDropText(x
, y
, ((wxTextDataObject
*)m_dataObject
)->GetText());
440 // ----------------------------------------------------------------------------
442 // ----------------------------------------------------------------------------
444 wxFileDropTarget::wxFileDropTarget()
445 : wxDropTarget(new wxFileDataObject
)
449 bool wxFileDropTarget::OnData(wxCoord x
, wxCoord y
)
454 return OnDropFiles(x
, y
,
455 ((wxFileDataObject
*)m_dataObject
)->GetFilenames());
458 // ----------------------------------------------------------------------------
460 // ----------------------------------------------------------------------------
462 static wxDragResult
ConvertDragEffectToResult(DWORD dwEffect
)
464 switch ( dwEffect
) {
465 case DROPEFFECT_COPY
:
468 case DROPEFFECT_MOVE
:
472 wxFAIL_MSG(wxT("invalid value in ConvertDragEffectToResult"));
475 case DROPEFFECT_NONE
:
480 static DWORD
ConvertDragResultToEffect(wxDragResult result
)
484 return DROPEFFECT_COPY
;
487 return DROPEFFECT_MOVE
;
490 wxFAIL_MSG(wxT("invalid value in ConvertDragResultToEffect"));
494 return DROPEFFECT_NONE
;
499 // wxUSE_DRAG_AND_DROP