]>
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__)
39 #include <shlobj.h> // for DROPFILES structure
45 #include <wx/msw/ole/droptgt.h>
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 // @@ we assume that if QueryGetData() returns S_OK, than we can really
73 // get data in this format, so we remember here the format for which
74 // QueryGetData() succeeded
75 void SetSupportedFormat(wxDataFormat cfFormat
) { m_cfFormat
= cfFormat
; }
77 DECLARE_IUNKNOWN_METHODS
;
80 IDataObject
*m_pIDataObject
; // !NULL between DragEnter and DragLeave/Drop
81 wxDropTarget
*m_pTarget
; // the real target (we're just a proxy)
83 wxDataFormat m_cfFormat
; // the format in which to ask for data
86 static inline DWORD
GetDropEffect(DWORD flags
);
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_cfFormat
= (wxDataFormat
) 0;
111 m_pIDataObject
= NULL
;
114 wxIDropTarget::~wxIDropTarget()
118 BEGIN_IID_TABLE(wxIDropTarget
)
123 IMPLEMENT_IUNKNOWN_METHODS(wxIDropTarget
)
125 // Name : wxIDropTarget::DragEnter
126 // Purpose : Called when the mouse enters the window (dragging something)
128 // Params : [in] IDataObject *pIDataSource : source data
129 // [in] DWORD grfKeyState : kbd & mouse state
130 // [in] POINTL pt : mouse coordinates
131 // [out]DWORD *pdwEffect : effect flag
133 STDMETHODIMP
wxIDropTarget::DragEnter(IDataObject
*pIDataSource
,
138 wxLogDebug("IDropTarget::DragEnter");
140 wxASSERT( m_pIDataObject
== NULL
);
142 if ( !m_pTarget
->IsAcceptedData(pIDataSource
) ) {
143 // we don't accept this kind of data
144 *pdwEffect
= DROPEFFECT_NONE
;
149 // @@ should check the point also?
151 *pdwEffect
= GetDropEffect(grfKeyState
);
153 // get hold of the data object
154 m_pIDataObject
= pIDataSource
;
155 m_pIDataObject
->AddRef();
157 // give some visual feedback
158 m_pTarget
->OnEnter();
163 // Name : wxIDropTarget::DragOver
164 // Purpose : Indicates that the mouse was moved inside the window represented
165 // by this drop target.
167 // Params : [in] DWORD grfKeyState kbd & mouse state
168 // [in] POINTL pt mouse coordinates
169 // [out]LPDWORD pdwEffect effect flag
170 // Notes : We're called on every WM_MOUSEMOVE, so this function should be
172 STDMETHODIMP
wxIDropTarget::DragOver(DWORD grfKeyState
,
176 // there are too many of them... wxLogDebug("IDropTarget::DragOver");
178 *pdwEffect
= m_pIDataObject
== NULL
? DROPEFFECT_NONE
179 : GetDropEffect(grfKeyState
);
183 // Name : wxIDropTarget::DragLeave
184 // Purpose : Informs the drop target that the operation has left its window.
186 // Notes : good place to do any clean-up
187 STDMETHODIMP
wxIDropTarget::DragLeave()
189 wxLogDebug("IDropTarget::DragLeave");
191 // remove the UI feedback
192 m_pTarget
->OnLeave();
194 // release the held object
195 RELEASE_AND_NULL(m_pIDataObject
);
200 // Name : wxIDropTarget::Drop
201 // Purpose : Instructs the drop target to paste data that was just now
204 // Params : [in] IDataObject *pIDataSource the data to paste
205 // [in] DWORD grfKeyState kbd & mouse state
206 // [in] POINTL pt where the drop occured?
207 // [ouy]DWORD *pdwEffect operation effect
209 STDMETHODIMP
wxIDropTarget::Drop(IDataObject
*pIDataSource
,
214 wxLogDebug("IDropTarget::Drop");
216 // @@ I don't know why there is this parameter, but so far I assume
217 // that it's the same we've already got in DragEnter
218 wxASSERT( m_pIDataObject
== pIDataSource
);
221 *pdwEffect
= DROPEFFECT_NONE
;
223 // should be set by SetSupportedFormat() call
224 wxASSERT( m_cfFormat
!= 0 );
227 fmtMemory
.cfFormat
= m_cfFormat
;
228 fmtMemory
.ptd
= NULL
;
229 fmtMemory
.dwAspect
= DVASPECT_CONTENT
;
230 fmtMemory
.lindex
= -1;
231 fmtMemory
.tymed
= TYMED_HGLOBAL
; // @@@@ to add other media
233 HRESULT hr
= pIDataSource
->GetData(&fmtMemory
, &stm
);
234 if ( SUCCEEDED(hr
) ) {
235 if ( stm
.hGlobal
!= NULL
) {
236 if ( m_pTarget
->OnDrop(pt
.x
, pt
.y
, GlobalLock(stm
.hGlobal
)) )
237 *pdwEffect
= GetDropEffect(grfKeyState
);
238 //else: DROPEFFECT_NONE
240 GlobalUnlock(stm
.hGlobal
);
241 ReleaseStgMedium(&stm
);
246 // wxLogApiError("GetData", hr);
249 // release the held object
250 RELEASE_AND_NULL(m_pIDataObject
);
255 // ============================================================================
256 // wxDropTarget implementation
257 // ============================================================================
259 // ----------------------------------------------------------------------------
261 // ----------------------------------------------------------------------------
263 wxDropTarget::wxDropTarget()
265 // create an IDropTarget implementation which will notify us about
267 m_pIDropTarget
= new wxIDropTarget(this);
268 m_pIDropTarget
->AddRef();
271 wxDropTarget::~wxDropTarget()
273 ReleaseInterface(m_pIDropTarget
);
276 // ----------------------------------------------------------------------------
277 // [un]register drop handler
278 // ----------------------------------------------------------------------------
280 bool wxDropTarget::Register(WXHWND hwnd
)
282 HRESULT hr
= ::CoLockObjectExternal(m_pIDropTarget
, TRUE
, FALSE
);
284 wxLogApiError("CoLockObjectExternal", hr
);
288 hr
= ::RegisterDragDrop((HWND
) hwnd
, m_pIDropTarget
);
290 ::CoLockObjectExternal(m_pIDropTarget
, FALSE
, FALSE
);
292 wxLogApiError("RegisterDragDrop", hr
);
299 void wxDropTarget::Revoke(WXHWND hwnd
)
301 HRESULT hr
= ::RevokeDragDrop((HWND
) hwnd
);
304 wxLogApiError("RevokeDragDrop", hr
);
307 ::CoLockObjectExternal(m_pIDropTarget
, FALSE
, TRUE
);
310 // ----------------------------------------------------------------------------
311 // determine if we accept data of this type
312 // ----------------------------------------------------------------------------
313 bool wxDropTarget::IsAcceptedData(IDataObject
*pIDataSource
) const
315 // this strucutre describes a data of any type (first field will be
316 // changing) being passed through global memory block.
317 static FORMATETC s_fmtMemory
= {
325 // cycle thorugh all supported formats
326 for ( size_t n
= 0; n
< GetFormatCount(); n
++ ) {
327 s_fmtMemory
.cfFormat
= GetFormat(n
);
328 // @ don't use SUCCEEDED macro here: QueryGetData returns 1 (whatever it
329 // means) for file drag and drop
330 if ( pIDataSource
->QueryGetData(&s_fmtMemory
) == S_OK
) {
331 // remember this format: we'll later ask for data in it
332 m_pIDropTarget
->SetSupportedFormat((wxDataFormat
) s_fmtMemory
.cfFormat
);
340 // ============================================================================
342 // ============================================================================
344 bool wxTextDropTarget::OnDrop(long x
, long y
, const void *pData
)
346 return OnDropText(x
, y
, (const char *)pData
);
349 size_t wxTextDropTarget::GetFormatCount() const
354 wxDataFormat
wxTextDropTarget::GetFormat(size_t WXUNUSED(n
)) const
359 // ============================================================================
361 // ============================================================================
363 bool wxFileDropTarget::OnDrop(long x
, long y
, const void *pData
)
365 // the documentation states that the first member of DROPFILES structure
366 // is a "DWORD offset of double NUL terminated file list". What they mean by
367 // this (I wonder if you see it immediately) is that the list starts at
368 // ((char *)&(pDropFiles.pFiles)) + pDropFiles.pFiles. We're also advised to
369 // use DragQueryFile to work with this structure, but not told where and how
371 HDROP hdrop
= (HDROP
)pData
; // @@ it works, but I'm not sure about it
373 // get number of files (magic value -1)
374 UINT nFiles
= ::DragQueryFile(hdrop
, -1, NULL
, 0);
376 // for each file get the length, allocate memory and then get the name
377 char **aszFiles
= new char *[nFiles
];
379 for ( n
= 0; n
< nFiles
; n
++ ) {
380 // +1 for terminating NUL
381 len
= ::DragQueryFile(hdrop
, n
, NULL
, 0) + 1;
383 aszFiles
[n
] = new char[len
];
385 UINT len2
= ::DragQueryFile(hdrop
, n
, aszFiles
[n
], len
);
386 if ( len2
!= len
- 1 ) {
387 wxLogDebug("In wxFileDropTarget::OnDrop DragQueryFile returned %d "
388 "characters, %d expected.", len2
, len
- 1);
392 bool bResult
= OnDropFiles(x
, y
, nFiles
, (const char**) aszFiles
);
395 for ( n
= 0; n
< nFiles
; n
++ ) {
396 delete [] aszFiles
[n
];
403 size_t wxFileDropTarget::GetFormatCount() const
408 wxDataFormat
wxFileDropTarget::GetFormat(size_t WXUNUSED(n
)) const
411 return wxDF_FILENAME
;
413 // TODO: how to implement this in WIN16?