]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/ole/droptgt.cpp
7ff89237d5fb242c9104d42a22f3a9922735151f
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 #define IN_WX_MAIN_CPP
26 #include "wx/wxprec.h"
28 #if defined(__BORLANDC__)
40 #include <shlobj.h> // for DROPFILES structure
46 #include <wx/msw/ole/droptgt.h>
53 #include <wx/msw/ole/oleutils.h>
55 // ----------------------------------------------------------------------------
56 // IDropTarget interface: forward all interesting things to wxDropTarget
57 // (the name is unfortunate, but wx_I_DropTarget is not at all the same thing
58 // as wxDropTarget which is 'public' class, while this one is private)
59 // ----------------------------------------------------------------------------
61 class wxIDropTarget
: public IDropTarget
64 wxIDropTarget(wxDropTarget
*p
);
67 // IDropTarget methods
68 STDMETHODIMP
DragEnter(LPDATAOBJECT
, DWORD
, POINTL
, LPDWORD
);
69 STDMETHODIMP
DragOver(DWORD
, POINTL
, LPDWORD
);
70 STDMETHODIMP
DragLeave(void);
71 STDMETHODIMP
Drop(LPDATAOBJECT
, DWORD
, POINTL
, LPDWORD
);
73 // @@ we assume that if QueryGetData() returns S_OK, than we can really
74 // get data in this format, so we remember here the format for which
75 // QueryGetData() succeeded
76 void SetSupportedFormat(wxDataFormat cfFormat
) { m_cfFormat
= cfFormat
; }
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 wxDataFormat m_cfFormat
; // the format in which to ask for data
87 static inline DWORD
GetDropEffect(DWORD flags
);
90 // ============================================================================
91 // wxIDropTarget implementation
92 // ============================================================================
94 // Name : static wxDropTarget::GetDropEffect
95 // Purpose : determine the drop operation from keyboard/mouse state.
96 // Returns : DWORD combined from DROPEFFECT_xxx constants
97 // Params : [in] DWORD flags kbd & mouse flags as passed to
98 // IDropTarget methods
99 // Notes : We do "move" normally and "copy" if <Ctrl> is pressed,
100 // which is the standard behaviour (currently there is no
101 // way to redefine it)
102 DWORD
wxIDropTarget::GetDropEffect(DWORD flags
)
104 return flags
& MK_CONTROL
? DROPEFFECT_COPY
: DROPEFFECT_MOVE
;
107 wxIDropTarget::wxIDropTarget(wxDropTarget
*pTarget
)
112 m_pIDataObject
= NULL
;
115 wxIDropTarget::~wxIDropTarget()
119 BEGIN_IID_TABLE(wxIDropTarget
)
124 IMPLEMENT_IUNKNOWN_METHODS(wxIDropTarget
)
127 STDMETHODIMP
wxIDropTarget::QueryInterface(REFIID riid
, void **ppv
)
129 // wxLogQueryInterface(wxIDropTarget, riid);
131 if ( IsIidFromList(riid
, ms_aIids
, WXSIZEOF(ms_aIids
)) ) {
140 return (HRESULT
) E_NOINTERFACE
;
144 STDMETHODIMP_(ULONG
) wxIDropTarget::AddRef()
146 wxLogAddRef(wxIDropTarget
, m_cRef
);
151 STDMETHODIMP_(ULONG
) wxIDropTarget::Release()
153 wxLogRelease(wxIDropTarget
, m_cRef
);
155 if ( --m_cRef
== 0 ) {
164 // Name : wxIDropTarget::DragEnter
165 // Purpose : Called when the mouse enters the window (dragging something)
167 // Params : [in] IDataObject *pIDataSource : source data
168 // [in] DWORD grfKeyState : kbd & mouse state
169 // [in] POINTL pt : mouse coordinates
170 // [out]DWORD *pdwEffect : effect flag
172 STDMETHODIMP
wxIDropTarget::DragEnter(IDataObject
*pIDataSource
,
177 wxLogDebug("IDropTarget::DragEnter");
179 wxASSERT( m_pIDataObject
== NULL
);
181 if ( !m_pTarget
->IsAcceptedData(pIDataSource
) ) {
182 // we don't accept this kind of data
183 *pdwEffect
= DROPEFFECT_NONE
;
188 // @@ should check the point also?
190 *pdwEffect
= GetDropEffect(grfKeyState
);
192 // get hold of the data object
193 m_pIDataObject
= pIDataSource
;
194 m_pIDataObject
->AddRef();
196 // give some visual feedback
197 m_pTarget
->OnEnter();
202 // Name : wxIDropTarget::DragOver
203 // Purpose : Indicates that the mouse was moved inside the window represented
204 // by this drop target.
206 // Params : [in] DWORD grfKeyState kbd & mouse state
207 // [in] POINTL pt mouse coordinates
208 // [out]LPDWORD pdwEffect effect flag
209 // Notes : We're called on every WM_MOUSEMOVE, so this function should be
211 STDMETHODIMP
wxIDropTarget::DragOver(DWORD grfKeyState
,
215 // there are too many of them... wxLogDebug("IDropTarget::DragOver");
217 *pdwEffect
= m_pIDataObject
== NULL
? DROPEFFECT_NONE
218 : GetDropEffect(grfKeyState
);
222 // Name : wxIDropTarget::DragLeave
223 // Purpose : Informs the drop target that the operation has left its window.
225 // Notes : good place to do any clean-up
226 STDMETHODIMP
wxIDropTarget::DragLeave()
228 wxLogDebug("IDropTarget::DragLeave");
230 // remove the UI feedback
231 m_pTarget
->OnLeave();
233 // release the held object
234 RELEASE_AND_NULL(m_pIDataObject
);
239 // Name : wxIDropTarget::Drop
240 // Purpose : Instructs the drop target to paste data that was just now
243 // Params : [in] IDataObject *pIDataSource the data to paste
244 // [in] DWORD grfKeyState kbd & mouse state
245 // [in] POINTL pt where the drop occured?
246 // [ouy]DWORD *pdwEffect operation effect
248 STDMETHODIMP
wxIDropTarget::Drop(IDataObject
*pIDataSource
,
253 wxLogDebug("IDropTarget::Drop");
255 // @@ I don't know why there is this parameter, but so far I assume
256 // that it's the same we've already got in DragEnter
257 wxASSERT( m_pIDataObject
== pIDataSource
);
260 *pdwEffect
= DROPEFFECT_NONE
;
262 // should be set by SetSupportedFormat() call
263 wxASSERT( m_cfFormat
!= 0 );
266 fmtMemory
.cfFormat
= m_cfFormat
;
267 fmtMemory
.ptd
= NULL
;
268 fmtMemory
.dwAspect
= DVASPECT_CONTENT
;
269 fmtMemory
.lindex
= -1;
270 fmtMemory
.tymed
= TYMED_HGLOBAL
; // @@@@ to add other media
272 HRESULT hr
= pIDataSource
->GetData(&fmtMemory
, &stm
);
273 if ( SUCCEEDED(hr
) ) {
274 if ( stm
.hGlobal
!= NULL
) {
275 if ( m_pTarget
->OnDrop(pt
.x
, pt
.y
, GlobalLock(stm
.hGlobal
)) )
276 *pdwEffect
= GetDropEffect(grfKeyState
);
277 //else: DROPEFFECT_NONE
279 GlobalUnlock(stm
.hGlobal
);
280 ReleaseStgMedium(&stm
);
285 // wxLogApiError("GetData", hr);
288 // release the held object
289 RELEASE_AND_NULL(m_pIDataObject
);
294 // ============================================================================
295 // wxDropTarget implementation
296 // ============================================================================
298 // ----------------------------------------------------------------------------
300 // ----------------------------------------------------------------------------
302 wxDropTarget::wxDropTarget()
304 // create an IDropTarget implementation which will notify us about
306 m_pIDropTarget
= new wxIDropTarget(this);
307 m_pIDropTarget
->AddRef();
310 wxDropTarget::~wxDropTarget()
312 ReleaseInterface(m_pIDropTarget
);
315 // ----------------------------------------------------------------------------
316 // [un]register drop handler
317 // ----------------------------------------------------------------------------
319 bool wxDropTarget::Register(WXHWND hwnd
)
321 HRESULT hr
= ::CoLockObjectExternal(m_pIDropTarget
, TRUE
, FALSE
);
323 // wxLogApiError("CoLockObjectExternal", hr);
327 hr
= ::RegisterDragDrop((HWND
) hwnd
, m_pIDropTarget
);
329 ::CoLockObjectExternal(m_pIDropTarget
, FALSE
, FALSE
);
331 // wxLogApiError("RegisterDragDrop", hr);
338 void wxDropTarget::Revoke(WXHWND hwnd
)
340 HRESULT hr
= ::RevokeDragDrop((HWND
) hwnd
);
344 // wxLogApiError("RevokeDragDrop", hr);
347 ::CoLockObjectExternal(m_pIDropTarget
, FALSE
, TRUE
);
350 // ----------------------------------------------------------------------------
351 // determine if we accept data of this type
352 // ----------------------------------------------------------------------------
353 bool wxDropTarget::IsAcceptedData(IDataObject
*pIDataSource
) const
355 // this strucutre describes a data of any type (first field will be
356 // changing) being passed through global memory block.
357 static FORMATETC s_fmtMemory
= {
365 // cycle thorugh all supported formats
366 for ( size_t n
= 0; n
< GetFormatCount(); n
++ ) {
367 s_fmtMemory
.cfFormat
= GetFormat(n
);
368 // @ don't use SUCCEEDED macro here: QueryGetData returns 1 (whatever it
369 // means) for file drag and drop
370 if ( pIDataSource
->QueryGetData(&s_fmtMemory
) == S_OK
) {
371 // remember this format: we'll later ask for data in it
372 m_pIDropTarget
->SetSupportedFormat(s_fmtMemory
.cfFormat
);
380 // ============================================================================
382 // ============================================================================
384 bool wxTextDropTarget::OnDrop(long x
, long y
, const void *pData
)
386 return OnDropText(x
, y
, (const char *)pData
);
389 size_t wxTextDropTarget::GetFormatCount() const
394 wxDataFormat
wxTextDropTarget::GetFormat(size_t WXUNUSED(n
)) const
399 // ============================================================================
401 // ============================================================================
403 bool wxFileDropTarget::OnDrop(long x
, long y
, const void *pData
)
405 // the documentation states that the first member of DROPFILES structure
406 // is a "DWORD offset of double NUL terminated file list". What they mean by
407 // this (I wonder if you see it immediately) is that the list starts at
408 // ((char *)&(pDropFiles.pFiles)) + pDropFiles.pFiles. We're also advised to
409 // use DragQueryFile to work with this structure, but not told where and how
411 HDROP hdrop
= (HDROP
)pData
; // @@ it works, but I'm not sure about it
413 // get number of files (magic value -1)
414 UINT nFiles
= ::DragQueryFile(hdrop
, -1, NULL
, 0);
416 // for each file get the length, allocate memory and then get the name
417 char **aszFiles
= new char *[nFiles
];
419 for ( n
= 0; n
< nFiles
; n
++ ) {
420 // +1 for terminating NUL
421 len
= ::DragQueryFile(hdrop
, n
, NULL
, 0) + 1;
423 aszFiles
[n
] = new char[len
];
425 UINT len2
= ::DragQueryFile(hdrop
, n
, aszFiles
[n
], len
);
426 if ( len2
!= len
- 1 ) {
427 wxLogDebug("In wxFileDropTarget::OnDrop DragQueryFile returned %d "
428 "characters, %d expected.", len2
, len
- 1);
432 bool bResult
= OnDropFiles(x
, y
, nFiles
, (const char**) aszFiles
);
435 for ( n
= 0; n
< nFiles
; n
++ ) {
436 delete [] aszFiles
[n
];
443 size_t wxFileDropTarget::GetFormatCount() const
448 wxDataFormat
wxFileDropTarget::GetFormat(size_t WXUNUSED(n
)) const
453 // TODO: how to implement this in WIN16?