]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/ole/droptgt.cpp
c9f5feb506068ab10aaf6cd1cbf0cc3d2d3d2488
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 // ============================================================================
83 // wxIDropTarget implementation
84 // ============================================================================
86 // Name : static wxDropTarget::GetDropEffect
87 // Purpose : determine the drop operation from keyboard/mouse state.
88 // Returns : DWORD combined from DROPEFFECT_xxx constants
89 // Params : [in] DWORD flags kbd & mouse flags as passed to
90 // IDropTarget methods
91 // Notes : We do "move" normally and "copy" if <Ctrl> is pressed,
92 // which is the standard behaviour (currently there is no
93 // way to redefine it)
94 DWORD
wxIDropTarget::GetDropEffect(DWORD flags
)
96 return flags
& MK_CONTROL
? DROPEFFECT_COPY
: DROPEFFECT_MOVE
;
99 wxIDropTarget::wxIDropTarget(wxDropTarget
*pTarget
)
103 m_pIDataObject
= NULL
;
106 wxIDropTarget::~wxIDropTarget()
110 BEGIN_IID_TABLE(wxIDropTarget
)
115 IMPLEMENT_IUNKNOWN_METHODS(wxIDropTarget
)
117 // Name : wxIDropTarget::DragEnter
118 // Purpose : Called when the mouse enters the window (dragging something)
120 // Params : [in] IDataObject *pIDataSource : source data
121 // [in] DWORD grfKeyState : kbd & mouse state
122 // [in] POINTL pt : mouse coordinates
123 // [out]DWORD *pdwEffect : effect flag
125 STDMETHODIMP
wxIDropTarget::DragEnter(IDataObject
*pIDataSource
,
130 wxLogDebug(wxT("IDropTarget::DragEnter"));
132 wxASSERT( m_pIDataObject
== NULL
);
134 if ( !m_pTarget
->IsAcceptedData(pIDataSource
) ) {
135 // we don't accept this kind of data
136 *pdwEffect
= DROPEFFECT_NONE
;
141 // TODO should check the point also?
143 *pdwEffect
= GetDropEffect(grfKeyState
);
145 // get hold of the data object
146 m_pIDataObject
= pIDataSource
;
147 m_pIDataObject
->AddRef();
149 // give some visual feedback
150 m_pTarget
->OnEnter();
155 // Name : wxIDropTarget::DragOver
156 // Purpose : Indicates that the mouse was moved inside the window represented
157 // by this drop target.
159 // Params : [in] DWORD grfKeyState kbd & mouse state
160 // [in] POINTL pt mouse coordinates
161 // [out]LPDWORD pdwEffect effect flag
162 // Notes : We're called on every WM_MOUSEMOVE, so this function should be
164 STDMETHODIMP
wxIDropTarget::DragOver(DWORD grfKeyState
,
168 // there are too many of them... wxLogDebug("IDropTarget::DragOver");
170 *pdwEffect
= m_pIDataObject
== NULL
? DROPEFFECT_NONE
171 : GetDropEffect(grfKeyState
);
175 // Name : wxIDropTarget::DragLeave
176 // Purpose : Informs the drop target that the operation has left its window.
178 // Notes : good place to do any clean-up
179 STDMETHODIMP
wxIDropTarget::DragLeave()
181 wxLogDebug(wxT("IDropTarget::DragLeave"));
183 // remove the UI feedback
184 m_pTarget
->OnLeave();
186 // release the held object
187 RELEASE_AND_NULL(m_pIDataObject
);
192 // Name : wxIDropTarget::Drop
193 // Purpose : Instructs the drop target to paste data that was just now
196 // Params : [in] IDataObject *pIDataSource the data to paste
197 // [in] DWORD grfKeyState kbd & mouse state
198 // [in] POINTL pt where the drop occured?
199 // [ouy]DWORD *pdwEffect operation effect
201 STDMETHODIMP
wxIDropTarget::Drop(IDataObject
*pIDataSource
,
206 wxLogDebug(wxT("IDropTarget::Drop"));
208 // TODO I don't know why there is this parameter, but so far I assume
209 // that it's the same we've already got in DragEnter
210 wxASSERT( m_pIDataObject
== pIDataSource
);
212 // by default, nothing happens
213 *pdwEffect
= DROPEFFECT_NONE
;
215 // first ask the drop target if it wants data
216 if ( m_pTarget
->OnDrop(pt
.x
, pt
.y
) ) {
217 // it does, so give it the data source
218 m_pTarget
->SetDataSource(pIDataSource
);
220 // and now it has the data
221 if ( m_pTarget
->OnData(pt
.x
, pt
.y
) ) {
222 // operation succeeded
223 *pdwEffect
= GetDropEffect(grfKeyState
);
225 //else: *pdwEffect is already DROPEFFECT_NONE
227 //else: OnDrop() returned FALSE, no need to copy data
229 // release the held object
230 RELEASE_AND_NULL(m_pIDataObject
);
235 // ============================================================================
236 // wxDropTarget implementation
237 // ============================================================================
239 // ----------------------------------------------------------------------------
241 // ----------------------------------------------------------------------------
243 wxDropTarget::wxDropTarget(wxDataObject
*dataObj
)
244 : wxDropTargetBase(dataObj
)
246 // create an IDropTarget implementation which will notify us about d&d
248 m_pIDropTarget
= new wxIDropTarget(this);
249 m_pIDropTarget
->AddRef();
252 wxDropTarget::~wxDropTarget()
254 ReleaseInterface(m_pIDropTarget
);
257 // ----------------------------------------------------------------------------
258 // [un]register drop handler
259 // ----------------------------------------------------------------------------
261 bool wxDropTarget::Register(WXHWND hwnd
)
263 HRESULT hr
= ::CoLockObjectExternal(m_pIDropTarget
, TRUE
, FALSE
);
265 wxLogApiError("CoLockObjectExternal", hr
);
269 hr
= ::RegisterDragDrop((HWND
) hwnd
, m_pIDropTarget
);
271 ::CoLockObjectExternal(m_pIDropTarget
, FALSE
, FALSE
);
273 wxLogApiError("RegisterDragDrop", hr
);
280 void wxDropTarget::Revoke(WXHWND hwnd
)
282 HRESULT hr
= ::RevokeDragDrop((HWND
) hwnd
);
285 wxLogApiError("RevokeDragDrop", hr
);
288 ::CoLockObjectExternal(m_pIDropTarget
, FALSE
, TRUE
);
291 // ----------------------------------------------------------------------------
292 // base class pure virtuals
293 // ----------------------------------------------------------------------------
295 // OnDrop() is called only if we previously returned TRUE from
296 // IsAcceptedData(), so no need to check anything here
297 bool wxDropTarget::OnDrop(wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
))
302 // copy the data from the data source to the target data object
303 bool wxDropTarget::GetData()
305 wxDataFormat format
= GetSupportedFormat(m_pIDataSource
);
306 if ( format
== wxDF_INVALID
) {
307 // this is strange because IsAcceptedData() succeeded previously!
308 wxFAIL_MSG(wxT("strange - did supported formats list change?"));
315 fmtMemory
.cfFormat
= format
;
316 fmtMemory
.ptd
= NULL
;
317 fmtMemory
.dwAspect
= DVASPECT_CONTENT
;
318 fmtMemory
.lindex
= -1;
319 fmtMemory
.tymed
= TYMED_HGLOBAL
; // TODO to add other media
323 HRESULT hr
= m_pIDataSource
->GetData(&fmtMemory
, &stm
);
324 if ( SUCCEEDED(hr
) ) {
325 IDataObject
*dataObject
= m_dataObject
->GetInterface();
327 hr
= dataObject
->SetData(&fmtMemory
, &stm
, TRUE
);
328 if ( SUCCEEDED(hr
) ) {
332 wxLogLastError("IDataObject::SetData()");
336 wxLogLastError("IDataObject::GetData()");
342 // ----------------------------------------------------------------------------
343 // callbacks used by wxIDropTarget
344 // ----------------------------------------------------------------------------
346 // we need a data source, so wxIDropTarget gives it to us using this function
347 void wxDropTarget::SetDataSource(IDataObject
*pIDataSource
)
349 m_pIDataSource
= pIDataSource
;
352 // determine if we accept data of this type
353 bool wxDropTarget::IsAcceptedData(IDataObject
*pIDataSource
) const
355 return GetSupportedFormat(pIDataSource
) != wxDF_INVALID
;
358 // ----------------------------------------------------------------------------
360 // ----------------------------------------------------------------------------
362 wxDataFormat
wxDropTarget::GetSupportedFormat(IDataObject
*pIDataSource
) const
364 // this strucutre describes a data of any type (first field will be
365 // changing) being passed through global memory block.
366 static FORMATETC s_fmtMemory
= {
371 TYMED_HGLOBAL
// TODO is it worth supporting other tymeds here?
374 // get the list of supported formats
375 size_t nFormats
= m_dataObject
->GetFormatCount(wxDataObject::Set
);
376 wxDataFormat format
, *formats
;
377 formats
= nFormats
== 1 ? &format
: new wxDataFormat
[nFormats
];
379 m_dataObject
->GetAllFormats(formats
, wxDataObject::Set
);
381 // cycle through all supported formats
383 for ( n
= 0; n
< nFormats
; n
++ ) {
384 s_fmtMemory
.cfFormat
= formats
[n
];
386 // NB: don't use SUCCEEDED macro here: QueryGetData returns S_FALSE
387 // for file drag and drop (format == CF_HDROP)
388 if ( pIDataSource
->QueryGetData(&s_fmtMemory
) == S_OK
) {
395 if ( formats
!= &format
) {
396 // free memory if we allocated it
400 return n
< nFormats
? format
: wxDF_INVALID
;
403 // ----------------------------------------------------------------------------
405 // ----------------------------------------------------------------------------
407 wxTextDropTarget::wxTextDropTarget()
408 : wxDropTarget(new wxTextDataObject
)
412 bool wxTextDropTarget::OnData(wxCoord x
, wxCoord y
)
417 return OnDropText(x
, y
, ((wxTextDataObject
*)m_dataObject
)->GetText());
420 // ----------------------------------------------------------------------------
422 // ----------------------------------------------------------------------------
424 wxFileDropTarget::wxFileDropTarget()
425 : wxDropTarget(new wxFileDataObject
)
429 bool wxFileDropTarget::OnData(wxCoord x
, wxCoord y
)
434 return OnDropFiles(x
, y
,
435 ((wxFileDataObject
*)m_dataObject
)->GetFilenames());
439 // wxUSE_DRAG_AND_DROP