]>
Commit | Line | Data |
---|---|---|
269a5a34 VZ |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: msw/ole/dataobj.cpp | |
3 | // Purpose: implementation of wx[I]DataObject class | |
4 | // Author: Vadim Zeitlin | |
3f4a0c5b | 5 | // Modified by: |
269a5a34 VZ |
6 | // Created: 10.05.98 |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> | |
9 | // Licence: wxWindows license | |
10 | /////////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | #ifdef __GNUG__ | |
0d0512bd | 21 | #pragma implementation "dataobj.h" |
269a5a34 VZ |
22 | #endif |
23 | ||
24 | // For compilers that support precompilation, includes "wx.h". | |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #if defined(__BORLANDC__) | |
0d0512bd | 28 | #pragma hdrstop |
269a5a34 | 29 | #endif |
0d0512bd | 30 | |
7dee726c | 31 | #ifndef WX_PRECOMP |
0d0512bd VZ |
32 | #include "wx/intl.h" |
33 | #include "wx/log.h" | |
7dee726c | 34 | #endif |
5260b1c5 | 35 | |
3f480da3 | 36 | #include "wx/dataobj.h" |
17b74d79 | 37 | |
21709999 JS |
38 | #if wxUSE_OLE && defined(__WIN32__) && !defined(__GNUWIN32_OLD__) |
39 | ||
0d0512bd VZ |
40 | #include "wx/msw/private.h" // includes <windows.h> |
41 | ||
b64f0a5f | 42 | #if wxUSE_NORLANDER_HEADERS |
7dee726c RS |
43 | #include <ole2.h> |
44 | #endif | |
17b74d79 | 45 | #include <oleauto.h> |
269a5a34 | 46 | |
f6aa3903 | 47 | #ifndef __WIN32__ |
269a5a34 VZ |
48 | #include <ole2.h> |
49 | #include <olestd.h> | |
50 | #endif | |
51 | ||
8b85d24e VZ |
52 | #include <shlobj.h> |
53 | ||
0d0512bd VZ |
54 | #include "wx/msw/ole/oleutils.h" |
55 | ||
56 | #include "wx/msw/dib.h" | |
17b74d79 | 57 | |
dcbd7ce2 VS |
58 | #ifndef CFSTR_SHELLURL |
59 | #define CFSTR_SHELLURL _T("UniformResourceLocator") | |
60 | #endif | |
61 | ||
269a5a34 VZ |
62 | // ---------------------------------------------------------------------------- |
63 | // functions | |
64 | // ---------------------------------------------------------------------------- | |
65 | ||
8e193f38 | 66 | #ifdef __WXDEBUG__ |
d59ceba5 | 67 | static const wxChar *GetTymedName(DWORD tymed); |
1e8335b0 | 68 | #else // !Debug |
c5639a87 | 69 | #define GetTymedName(tymed) _T("") |
1e8335b0 | 70 | #endif // Debug/!Debug |
269a5a34 VZ |
71 | |
72 | // ---------------------------------------------------------------------------- | |
73 | // wxIEnumFORMATETC interface implementation | |
74 | // ---------------------------------------------------------------------------- | |
8e193f38 | 75 | |
269a5a34 VZ |
76 | class wxIEnumFORMATETC : public IEnumFORMATETC |
77 | { | |
78 | public: | |
8e193f38 | 79 | wxIEnumFORMATETC(const wxDataFormat* formats, ULONG nCount); |
33ac7e6f | 80 | virtual ~wxIEnumFORMATETC() { delete [] m_formats; } |
269a5a34 | 81 | |
8e193f38 | 82 | DECLARE_IUNKNOWN_METHODS; |
269a5a34 | 83 | |
8e193f38 VZ |
84 | // IEnumFORMATETC |
85 | STDMETHODIMP Next(ULONG celt, FORMATETC *rgelt, ULONG *pceltFetched); | |
86 | STDMETHODIMP Skip(ULONG celt); | |
87 | STDMETHODIMP Reset(); | |
88 | STDMETHODIMP Clone(IEnumFORMATETC **ppenum); | |
269a5a34 VZ |
89 | |
90 | private: | |
8e193f38 VZ |
91 | CLIPFORMAT *m_formats; // formats we can provide data in |
92 | ULONG m_nCount, // number of formats we support | |
93 | m_nCurrent; // current enum position | |
269a5a34 VZ |
94 | }; |
95 | ||
96 | // ---------------------------------------------------------------------------- | |
97 | // wxIDataObject implementation of IDataObject interface | |
98 | // ---------------------------------------------------------------------------- | |
8e193f38 | 99 | |
269a5a34 VZ |
100 | class wxIDataObject : public IDataObject |
101 | { | |
102 | public: | |
8e193f38 | 103 | wxIDataObject(wxDataObject *pDataObject); |
33ac7e6f | 104 | virtual ~wxIDataObject(); |
d59ceba5 VZ |
105 | |
106 | // normally, wxDataObject controls our lifetime (i.e. we're deleted when it | |
107 | // is), but in some cases, the situation is inversed, that is we delete it | |
108 | // when this object is deleted - setting this flag enables such logic | |
109 | void SetDeleteFlag() { m_mustDelete = TRUE; } | |
269a5a34 | 110 | |
8e193f38 | 111 | DECLARE_IUNKNOWN_METHODS; |
269a5a34 | 112 | |
8e193f38 VZ |
113 | // IDataObject |
114 | STDMETHODIMP GetData(FORMATETC *pformatetcIn, STGMEDIUM *pmedium); | |
115 | STDMETHODIMP GetDataHere(FORMATETC *pformatetc, STGMEDIUM *pmedium); | |
116 | STDMETHODIMP QueryGetData(FORMATETC *pformatetc); | |
117 | STDMETHODIMP GetCanonicalFormatEtc(FORMATETC *In, FORMATETC *pOut); | |
118 | STDMETHODIMP SetData(FORMATETC *pfetc, STGMEDIUM *pmedium, BOOL fRelease); | |
119 | STDMETHODIMP EnumFormatEtc(DWORD dwDirection, IEnumFORMATETC **ppenumFEtc); | |
120 | STDMETHODIMP DAdvise(FORMATETC *pfetc, DWORD ad, IAdviseSink *p, DWORD *pdw); | |
121 | STDMETHODIMP DUnadvise(DWORD dwConnection); | |
122 | STDMETHODIMP EnumDAdvise(IEnumSTATDATA **ppenumAdvise); | |
269a5a34 VZ |
123 | |
124 | private: | |
8e193f38 | 125 | wxDataObject *m_pDataObject; // pointer to C++ class we belong to |
d59ceba5 VZ |
126 | |
127 | bool m_mustDelete; | |
128 | }; | |
129 | ||
269a5a34 VZ |
130 | // ============================================================================ |
131 | // implementation | |
132 | // ============================================================================ | |
133 | ||
3f480da3 VZ |
134 | // ---------------------------------------------------------------------------- |
135 | // wxDataFormat | |
136 | // ---------------------------------------------------------------------------- | |
137 | ||
138 | void wxDataFormat::SetId(const wxChar *format) | |
139 | { | |
33ac7e6f | 140 | m_format = (wxDataFormat::NativeFormat)::RegisterClipboardFormat(format); |
3f480da3 VZ |
141 | if ( !m_format ) |
142 | { | |
143 | wxLogError(_("Couldn't register clipboard format '%s'."), format); | |
144 | } | |
145 | } | |
146 | ||
147 | wxString wxDataFormat::GetId() const | |
148 | { | |
149 | static const int max = 256; | |
150 | ||
151 | wxString s; | |
152 | ||
153 | wxCHECK_MSG( !IsStandard(), s, | |
223d09f6 | 154 | wxT("name of predefined format cannot be retrieved") ); |
3f480da3 VZ |
155 | |
156 | int len = ::GetClipboardFormatName(m_format, s.GetWriteBuf(max), max); | |
157 | s.UngetWriteBuf(); | |
158 | ||
159 | if ( !len ) | |
160 | { | |
161 | wxLogError(_("The clipboard format '%d' doesn't exist."), m_format); | |
162 | } | |
163 | ||
164 | return s; | |
165 | } | |
166 | ||
269a5a34 VZ |
167 | // ---------------------------------------------------------------------------- |
168 | // wxIEnumFORMATETC | |
169 | // ---------------------------------------------------------------------------- | |
170 | ||
171 | BEGIN_IID_TABLE(wxIEnumFORMATETC) | |
8e193f38 VZ |
172 | ADD_IID(Unknown) |
173 | ADD_IID(EnumFORMATETC) | |
269a5a34 VZ |
174 | END_IID_TABLE; |
175 | ||
176 | IMPLEMENT_IUNKNOWN_METHODS(wxIEnumFORMATETC) | |
177 | ||
8e193f38 | 178 | wxIEnumFORMATETC::wxIEnumFORMATETC(const wxDataFormat *formats, ULONG nCount) |
269a5a34 | 179 | { |
8e193f38 VZ |
180 | m_nCurrent = 0; |
181 | m_nCount = nCount; | |
182 | m_formats = new CLIPFORMAT[nCount]; | |
183 | for ( ULONG n = 0; n < nCount; n++ ) { | |
184 | m_formats[n] = formats[n].GetFormatId(); | |
185 | } | |
269a5a34 VZ |
186 | } |
187 | ||
188 | STDMETHODIMP wxIEnumFORMATETC::Next(ULONG celt, | |
189 | FORMATETC *rgelt, | |
33ac7e6f | 190 | ULONG *WXUNUSED(pceltFetched)) |
269a5a34 | 191 | { |
8e193f38 | 192 | wxLogTrace(wxTRACE_OleCalls, wxT("wxIEnumFORMATETC::Next")); |
269a5a34 | 193 | |
8e193f38 VZ |
194 | if ( celt > 1 ) { |
195 | // we only return 1 element at a time - mainly because I'm too lazy to | |
196 | // implement something which you're never asked for anyhow | |
197 | return S_FALSE; | |
198 | } | |
269a5a34 | 199 | |
8e193f38 VZ |
200 | if ( m_nCurrent < m_nCount ) { |
201 | FORMATETC format; | |
202 | format.cfFormat = m_formats[m_nCurrent++]; | |
203 | format.ptd = NULL; | |
204 | format.dwAspect = DVASPECT_CONTENT; | |
205 | format.lindex = -1; | |
206 | format.tymed = TYMED_HGLOBAL; | |
207 | *rgelt = format; | |
269a5a34 | 208 | |
8e193f38 VZ |
209 | return S_OK; |
210 | } | |
211 | else { | |
212 | // bad index | |
213 | return S_FALSE; | |
214 | } | |
269a5a34 VZ |
215 | } |
216 | ||
217 | STDMETHODIMP wxIEnumFORMATETC::Skip(ULONG celt) | |
218 | { | |
8e193f38 VZ |
219 | wxLogTrace(wxTRACE_OleCalls, wxT("wxIEnumFORMATETC::Skip")); |
220 | ||
221 | m_nCurrent += celt; | |
222 | if ( m_nCurrent < m_nCount ) | |
223 | return S_OK; | |
269a5a34 | 224 | |
8e193f38 VZ |
225 | // no, can't skip this many elements |
226 | m_nCurrent -= celt; | |
269a5a34 | 227 | |
8e193f38 | 228 | return S_FALSE; |
269a5a34 VZ |
229 | } |
230 | ||
231 | STDMETHODIMP wxIEnumFORMATETC::Reset() | |
232 | { | |
8e193f38 | 233 | wxLogTrace(wxTRACE_OleCalls, wxT("wxIEnumFORMATETC::Reset")); |
269a5a34 | 234 | |
8e193f38 | 235 | m_nCurrent = 0; |
269a5a34 | 236 | |
8e193f38 | 237 | return S_OK; |
269a5a34 VZ |
238 | } |
239 | ||
240 | STDMETHODIMP wxIEnumFORMATETC::Clone(IEnumFORMATETC **ppenum) | |
241 | { | |
8e193f38 VZ |
242 | wxLogTrace(wxTRACE_OleCalls, wxT("wxIEnumFORMATETC::Clone")); |
243 | ||
244 | // unfortunately, we can't reuse the code in ctor - types are different | |
245 | wxIEnumFORMATETC *pNew = new wxIEnumFORMATETC(NULL, 0); | |
246 | pNew->m_nCount = m_nCount; | |
247 | pNew->m_formats = new CLIPFORMAT[m_nCount]; | |
248 | for ( ULONG n = 0; n < m_nCount; n++ ) { | |
249 | pNew->m_formats[n] = m_formats[n]; | |
250 | } | |
251 | pNew->AddRef(); | |
252 | *ppenum = pNew; | |
269a5a34 | 253 | |
8e193f38 | 254 | return S_OK; |
269a5a34 VZ |
255 | } |
256 | ||
257 | // ---------------------------------------------------------------------------- | |
258 | // wxIDataObject | |
259 | // ---------------------------------------------------------------------------- | |
260 | ||
261 | BEGIN_IID_TABLE(wxIDataObject) | |
8e193f38 VZ |
262 | ADD_IID(Unknown) |
263 | ADD_IID(DataObject) | |
269a5a34 VZ |
264 | END_IID_TABLE; |
265 | ||
266 | IMPLEMENT_IUNKNOWN_METHODS(wxIDataObject) | |
267 | ||
268 | wxIDataObject::wxIDataObject(wxDataObject *pDataObject) | |
269 | { | |
8e193f38 | 270 | m_pDataObject = pDataObject; |
d59ceba5 VZ |
271 | m_mustDelete = FALSE; |
272 | } | |
273 | ||
274 | wxIDataObject::~wxIDataObject() | |
275 | { | |
276 | if ( m_mustDelete ) | |
277 | { | |
278 | delete m_pDataObject; | |
279 | } | |
269a5a34 VZ |
280 | } |
281 | ||
282 | // get data functions | |
283 | STDMETHODIMP wxIDataObject::GetData(FORMATETC *pformatetcIn, STGMEDIUM *pmedium) | |
284 | { | |
8e193f38 | 285 | wxLogTrace(wxTRACE_OleCalls, wxT("wxIDataObject::GetData")); |
269a5a34 | 286 | |
8e193f38 VZ |
287 | // is data is in our format? |
288 | HRESULT hr = QueryGetData(pformatetcIn); | |
289 | if ( FAILED(hr) ) | |
290 | return hr; | |
269a5a34 | 291 | |
8e193f38 VZ |
292 | // for the bitmaps and metafiles we use the handles instead of global memory |
293 | // to pass the data | |
f4d0cce0 | 294 | wxDataFormat format = (wxDataFormat::NativeFormat)pformatetcIn->cfFormat; |
269a5a34 | 295 | |
8e193f38 VZ |
296 | switch ( format ) |
297 | { | |
298 | case wxDF_BITMAP: | |
299 | pmedium->tymed = TYMED_GDI; | |
300 | break; | |
301 | ||
d9317fd4 VZ |
302 | case wxDF_ENHMETAFILE: |
303 | pmedium->tymed = TYMED_ENHMF; | |
304 | break; | |
305 | ||
8e193f38 | 306 | case wxDF_METAFILE: |
265b0c07 VZ |
307 | pmedium->hGlobal = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, |
308 | sizeof(METAFILEPICT)); | |
309 | if ( !pmedium->hGlobal ) { | |
f6bcfd97 | 310 | wxLogLastError(wxT("GlobalAlloc")); |
265b0c07 VZ |
311 | return E_OUTOFMEMORY; |
312 | } | |
8e193f38 VZ |
313 | pmedium->tymed = TYMED_MFPICT; |
314 | break; | |
315 | ||
316 | default: | |
317 | // alloc memory | |
318 | size_t size = m_pDataObject->GetDataSize(format); | |
319 | if ( !size ) { | |
320 | // it probably means that the method is just not implemented | |
321 | wxLogDebug(wxT("Invalid data size - can't be 0")); | |
322 | ||
323 | return DV_E_FORMATETC; | |
324 | } | |
325 | ||
9e2896e5 VZ |
326 | if ( !format.IsStandard() ) { |
327 | // for custom formats, put the size with the data - alloc the | |
328 | // space for it | |
e1b435af MB |
329 | // MB: not completely sure this is correct, |
330 | // even if I can't figure out what's wrong | |
331 | size += m_pDataObject->GetBufferOffset( format ); | |
9e2896e5 VZ |
332 | } |
333 | ||
8e193f38 VZ |
334 | HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, size); |
335 | if ( hGlobal == NULL ) { | |
f6bcfd97 | 336 | wxLogLastError(wxT("GlobalAlloc")); |
8e193f38 VZ |
337 | return E_OUTOFMEMORY; |
338 | } | |
339 | ||
340 | // copy data | |
341 | pmedium->tymed = TYMED_HGLOBAL; | |
342 | pmedium->hGlobal = hGlobal; | |
343 | } | |
269a5a34 | 344 | |
8e193f38 VZ |
345 | pmedium->pUnkForRelease = NULL; |
346 | ||
347 | // do copy the data | |
348 | hr = GetDataHere(pformatetcIn, pmedium); | |
349 | if ( FAILED(hr) ) { | |
350 | // free resources we allocated | |
265b0c07 | 351 | if ( pmedium->tymed & (TYMED_HGLOBAL | TYMED_MFPICT) ) { |
8e193f38 VZ |
352 | GlobalFree(pmedium->hGlobal); |
353 | } | |
354 | ||
355 | return hr; | |
356 | } | |
269a5a34 | 357 | |
8e193f38 | 358 | return S_OK; |
269a5a34 VZ |
359 | } |
360 | ||
361 | STDMETHODIMP wxIDataObject::GetDataHere(FORMATETC *pformatetc, | |
362 | STGMEDIUM *pmedium) | |
363 | { | |
8e193f38 | 364 | wxLogTrace(wxTRACE_OleCalls, wxT("wxIDataObject::GetDataHere")); |
269a5a34 | 365 | |
8e193f38 VZ |
366 | // put data in caller provided medium |
367 | switch ( pmedium->tymed ) | |
368 | { | |
369 | case TYMED_GDI: | |
d59ceba5 VZ |
370 | if ( !m_pDataObject->GetDataHere(wxDF_BITMAP, &pmedium->hBitmap) ) |
371 | return E_UNEXPECTED; | |
8e193f38 VZ |
372 | break; |
373 | ||
d9317fd4 VZ |
374 | case TYMED_ENHMF: |
375 | if ( !m_pDataObject->GetDataHere(wxDF_ENHMETAFILE, | |
376 | &pmedium->hEnhMetaFile) ) | |
377 | return E_UNEXPECTED; | |
378 | break; | |
379 | ||
8e193f38 | 380 | case TYMED_MFPICT: |
d9317fd4 VZ |
381 | // fall through - we pass METAFILEPICT through HGLOBAL |
382 | ||
8e193f38 VZ |
383 | case TYMED_HGLOBAL: |
384 | { | |
385 | // copy data | |
9e2896e5 VZ |
386 | HGLOBAL hGlobal = pmedium->hGlobal; |
387 | void *pBuf = GlobalLock(hGlobal); | |
8e193f38 VZ |
388 | if ( pBuf == NULL ) { |
389 | wxLogLastError(wxT("GlobalLock")); | |
390 | return E_OUTOFMEMORY; | |
391 | } | |
392 | ||
e1b435af MB |
393 | wxDataFormat format = pformatetc->cfFormat; |
394 | if ( !format.IsStandard() ) { | |
9e2896e5 | 395 | // for custom formats, put the size with the data |
e1b435af | 396 | pBuf = m_pDataObject->SetSizeInBuffer( pBuf, GlobalSize(hGlobal), format ); |
9e2896e5 VZ |
397 | } |
398 | ||
d59ceba5 VZ |
399 | if ( !m_pDataObject->GetDataHere(format, pBuf) ) |
400 | return E_UNEXPECTED; | |
8e193f38 | 401 | |
9e2896e5 | 402 | GlobalUnlock(hGlobal); |
8e193f38 VZ |
403 | } |
404 | break; | |
405 | ||
406 | default: | |
407 | return DV_E_TYMED; | |
408 | } | |
269a5a34 | 409 | |
8e193f38 | 410 | return S_OK; |
269a5a34 VZ |
411 | } |
412 | ||
9e2896e5 | 413 | // set data functions |
269a5a34 VZ |
414 | STDMETHODIMP wxIDataObject::SetData(FORMATETC *pformatetc, |
415 | STGMEDIUM *pmedium, | |
416 | BOOL fRelease) | |
417 | { | |
d59ceba5 | 418 | wxLogTrace(wxTRACE_OleCalls, wxT("wxIDataObject::SetData")); |
8e193f38 | 419 | |
d59ceba5 VZ |
420 | switch ( pmedium->tymed ) |
421 | { | |
422 | case TYMED_GDI: | |
9e2896e5 | 423 | m_pDataObject->SetData(wxDF_BITMAP, 0, &pmedium->hBitmap); |
d59ceba5 VZ |
424 | break; |
425 | ||
d9317fd4 VZ |
426 | case TYMED_ENHMF: |
427 | m_pDataObject->SetData(wxDF_ENHMETAFILE, 0, &pmedium->hEnhMetaFile); | |
428 | break; | |
429 | ||
d59ceba5 | 430 | case TYMED_MFPICT: |
d9317fd4 | 431 | // fall through - we pass METAFILEPICT through HGLOBAL |
d59ceba5 VZ |
432 | case TYMED_HGLOBAL: |
433 | { | |
51edda6a VZ |
434 | wxDataFormat format = pformatetc->cfFormat; |
435 | ||
436 | // this is quite weird, but for file drag and drop, explorer | |
437 | // calls our SetData() with the formats we do *not* support! | |
438 | // | |
439 | // as we can't fix this bug in explorer (it's a bug because it | |
440 | // should only use formats returned by EnumFormatEtc), do the | |
441 | // check here | |
442 | if ( !m_pDataObject->IsSupportedFormat(format) ) { | |
443 | // go away! | |
444 | return DV_E_FORMATETC; | |
445 | } | |
446 | ||
d59ceba5 | 447 | // copy data |
e1b435af | 448 | const void *pBuf = GlobalLock(pmedium->hGlobal); |
d59ceba5 | 449 | if ( pBuf == NULL ) { |
f6bcfd97 | 450 | wxLogLastError(wxT("GlobalLock")); |
d59ceba5 VZ |
451 | |
452 | return E_OUTOFMEMORY; | |
453 | } | |
454 | ||
9e2896e5 VZ |
455 | // we've got a problem with SetData() here because the base |
456 | // class version requires the size parameter which we don't | |
457 | // have anywhere in OLE data transfer - so we need to | |
458 | // synthetise it for known formats and we suppose that all data | |
459 | // in custom formats starts with a DWORD containing the size | |
460 | size_t size; | |
51edda6a | 461 | switch ( format ) |
9e2896e5 VZ |
462 | { |
463 | case CF_TEXT: | |
464 | case CF_OEMTEXT: | |
465 | size = strlen((const char *)pBuf); | |
466 | break; | |
790ad94f | 467 | #if !defined(__WATCOMC__) && ! (defined(__BORLANDC__) && (__BORLANDC__ < 0x500)) |
9e2896e5 | 468 | case CF_UNICODETEXT: |
de85a884 VZ |
469 | #if ( defined(__BORLANDC__) && (__BORLANDC__ > 0x530) ) \ |
470 | || ( defined(__MWERKS__) && defined(__WXMSW__) ) | |
e1b435af | 471 | size = std::wcslen((const wchar_t *)pBuf) * sizeof(wchar_t); |
d834f22c | 472 | #else |
e1b435af | 473 | size = ::wcslen((const wchar_t *)pBuf) * sizeof(wchar_t); |
d834f22c | 474 | #endif |
9e2896e5 | 475 | break; |
4d85bcd1 | 476 | #endif |
9e2896e5 VZ |
477 | case CF_BITMAP: |
478 | case CF_HDROP: | |
479 | // these formats don't use size at all, anyhow (but | |
480 | // pass data by handle, which is always a single DWORD) | |
481 | size = 0; | |
482 | break; | |
483 | ||
8ee9d618 VZ |
484 | case CF_DIB: |
485 | // the handler will calculate size itself (it's too | |
486 | // complicated to do it here) | |
487 | size = 0; | |
488 | break; | |
489 | ||
265b0c07 VZ |
490 | case CF_METAFILEPICT: |
491 | size = sizeof(METAFILEPICT); | |
492 | break; | |
493 | ||
9e2896e5 VZ |
494 | default: |
495 | { | |
496 | // we suppose that the size precedes the data | |
e1b435af | 497 | pBuf = m_pDataObject->GetSizeFromBuffer( pBuf, &size, format ); |
f0c5ebdc RD |
498 | if (! format.IsStandard() ) { |
499 | // see GetData for coresponding increment | |
e1b435af | 500 | size -= m_pDataObject->GetBufferOffset( format ); |
f0c5ebdc | 501 | } |
9e2896e5 VZ |
502 | } |
503 | } | |
504 | ||
9e2896e5 | 505 | bool ok = m_pDataObject->SetData(format, size, pBuf); |
d59ceba5 VZ |
506 | |
507 | GlobalUnlock(pmedium->hGlobal); | |
9e2896e5 VZ |
508 | |
509 | if ( !ok ) { | |
510 | return E_UNEXPECTED; | |
511 | } | |
d59ceba5 VZ |
512 | } |
513 | break; | |
514 | ||
515 | default: | |
516 | return DV_E_TYMED; | |
517 | } | |
518 | ||
519 | if ( fRelease ) { | |
265b0c07 VZ |
520 | // we own the medium, so we must release it - but do *not* free any |
521 | // data we pass by handle because we have copied it elsewhere | |
522 | switch ( pmedium->tymed ) | |
d59ceba5 | 523 | { |
265b0c07 VZ |
524 | case TYMED_GDI: |
525 | pmedium->hBitmap = 0; | |
526 | break; | |
527 | ||
528 | case TYMED_MFPICT: | |
529 | pmedium->hMetaFilePict = 0; | |
530 | break; | |
d9317fd4 VZ |
531 | |
532 | case TYMED_ENHMF: | |
533 | pmedium->hEnhMetaFile = 0; | |
534 | break; | |
d59ceba5 VZ |
535 | } |
536 | ||
537 | ReleaseStgMedium(pmedium); | |
538 | } | |
539 | ||
540 | return S_OK; | |
269a5a34 VZ |
541 | } |
542 | ||
543 | // information functions | |
544 | STDMETHODIMP wxIDataObject::QueryGetData(FORMATETC *pformatetc) | |
545 | { | |
d59ceba5 VZ |
546 | // do we accept data in this format? |
547 | if ( pformatetc == NULL ) { | |
548 | wxLogTrace(wxTRACE_OleCalls, | |
549 | wxT("wxIDataObject::QueryGetData: invalid ptr.")); | |
8e193f38 | 550 | |
d59ceba5 VZ |
551 | return E_INVALIDARG; |
552 | } | |
269a5a34 | 553 | |
d59ceba5 VZ |
554 | // the only one allowed by current COM implementation |
555 | if ( pformatetc->lindex != -1 ) { | |
556 | wxLogTrace(wxTRACE_OleCalls, | |
557 | wxT("wxIDataObject::QueryGetData: bad lindex %d"), | |
558 | pformatetc->lindex); | |
269a5a34 | 559 | |
d59ceba5 VZ |
560 | return DV_E_LINDEX; |
561 | } | |
269a5a34 | 562 | |
d59ceba5 VZ |
563 | // we don't support anything other (THUMBNAIL, ICON, DOCPRINT...) |
564 | if ( pformatetc->dwAspect != DVASPECT_CONTENT ) { | |
565 | wxLogTrace(wxTRACE_OleCalls, | |
566 | wxT("wxIDataObject::QueryGetData: bad dwAspect %d"), | |
567 | pformatetc->dwAspect); | |
568 | ||
569 | return DV_E_DVASPECT; | |
570 | } | |
571 | ||
572 | // and now check the type of data requested | |
9e2896e5 | 573 | wxDataFormat format = pformatetc->cfFormat; |
d59ceba5 | 574 | if ( m_pDataObject->IsSupportedFormat(format) ) { |
d59ceba5 | 575 | wxLogTrace(wxTRACE_OleCalls, wxT("wxIDataObject::QueryGetData: %s ok"), |
1e8335b0 | 576 | wxGetFormatName(format)); |
d59ceba5 VZ |
577 | } |
578 | else { | |
579 | wxLogTrace(wxTRACE_OleCalls, | |
580 | wxT("wxIDataObject::QueryGetData: %s unsupported"), | |
1e8335b0 | 581 | wxGetFormatName(format)); |
9e2896e5 | 582 | |
d59ceba5 VZ |
583 | return DV_E_FORMATETC; |
584 | } | |
585 | ||
586 | // we only transfer data by global memory, except for some particular cases | |
587 | DWORD tymed = pformatetc->tymed; | |
588 | if ( (format == wxDF_BITMAP && !(tymed & TYMED_GDI)) && | |
589 | !(tymed & TYMED_HGLOBAL) ) { | |
590 | // it's not what we're waiting for | |
d59ceba5 VZ |
591 | wxLogTrace(wxTRACE_OleCalls, |
592 | wxT("wxIDataObject::QueryGetData: %s != %s"), | |
593 | GetTymedName(tymed), | |
594 | GetTymedName(format == wxDF_BITMAP ? TYMED_GDI | |
595 | : TYMED_HGLOBAL)); | |
d59ceba5 VZ |
596 | |
597 | return DV_E_TYMED; | |
598 | } | |
599 | ||
269a5a34 | 600 | return S_OK; |
269a5a34 VZ |
601 | } |
602 | ||
33ac7e6f | 603 | STDMETHODIMP wxIDataObject::GetCanonicalFormatEtc(FORMATETC *WXUNUSED(pFormatetcIn), |
269a5a34 VZ |
604 | FORMATETC *pFormatetcOut) |
605 | { | |
8e193f38 VZ |
606 | wxLogTrace(wxTRACE_OleCalls, wxT("wxIDataObject::GetCanonicalFormatEtc")); |
607 | ||
608 | // TODO we might want something better than this trivial implementation here | |
609 | if ( pFormatetcOut != NULL ) | |
610 | pFormatetcOut->ptd = NULL; | |
269a5a34 | 611 | |
8e193f38 | 612 | return DATA_S_SAMEFORMATETC; |
269a5a34 VZ |
613 | } |
614 | ||
9e2896e5 | 615 | STDMETHODIMP wxIDataObject::EnumFormatEtc(DWORD dwDir, |
269a5a34 VZ |
616 | IEnumFORMATETC **ppenumFormatEtc) |
617 | { | |
8e193f38 | 618 | wxLogTrace(wxTRACE_OleCalls, wxT("wxIDataObject::EnumFormatEtc")); |
269a5a34 | 619 | |
9e2896e5 VZ |
620 | wxDataObject::Direction dir = dwDir == DATADIR_GET ? wxDataObject::Get |
621 | : wxDataObject::Set; | |
269a5a34 | 622 | |
9e2896e5 | 623 | size_t nFormatCount = m_pDataObject->GetFormatCount(dir); |
33ac7e6f | 624 | wxDataFormat format; |
c5639a87 | 625 | wxDataFormat *formats; |
9e2896e5 VZ |
626 | formats = nFormatCount == 1 ? &format : new wxDataFormat[nFormatCount]; |
627 | m_pDataObject->GetAllFormats(formats, dir); | |
8e193f38 VZ |
628 | |
629 | wxIEnumFORMATETC *pEnum = new wxIEnumFORMATETC(formats, nFormatCount); | |
630 | pEnum->AddRef(); | |
631 | *ppenumFormatEtc = pEnum; | |
632 | ||
633 | if ( formats != &format ) { | |
634 | delete [] formats; | |
635 | } | |
269a5a34 | 636 | |
8e193f38 | 637 | return S_OK; |
269a5a34 VZ |
638 | } |
639 | ||
9e2896e5 | 640 | // ---------------------------------------------------------------------------- |
269a5a34 | 641 | // advise sink functions (not implemented) |
9e2896e5 VZ |
642 | // ---------------------------------------------------------------------------- |
643 | ||
33ac7e6f KB |
644 | STDMETHODIMP wxIDataObject::DAdvise(FORMATETC *WXUNUSED(pformatetc), |
645 | DWORD WXUNUSED(advf), | |
646 | IAdviseSink *WXUNUSED(pAdvSink), | |
647 | DWORD *WXUNUSED(pdwConnection)) | |
269a5a34 VZ |
648 | { |
649 | return OLE_E_ADVISENOTSUPPORTED; | |
650 | } | |
651 | ||
33ac7e6f | 652 | STDMETHODIMP wxIDataObject::DUnadvise(DWORD WXUNUSED(dwConnection)) |
269a5a34 VZ |
653 | { |
654 | return OLE_E_ADVISENOTSUPPORTED; | |
655 | } | |
656 | ||
33ac7e6f | 657 | STDMETHODIMP wxIDataObject::EnumDAdvise(IEnumSTATDATA **WXUNUSED(ppenumAdvise)) |
269a5a34 VZ |
658 | { |
659 | return OLE_E_ADVISENOTSUPPORTED; | |
660 | } | |
661 | ||
662 | // ---------------------------------------------------------------------------- | |
663 | // wxDataObject | |
664 | // ---------------------------------------------------------------------------- | |
665 | ||
666 | wxDataObject::wxDataObject() | |
667 | { | |
d59ceba5 VZ |
668 | m_pIDataObject = new wxIDataObject(this); |
669 | m_pIDataObject->AddRef(); | |
269a5a34 VZ |
670 | } |
671 | ||
672 | wxDataObject::~wxDataObject() | |
673 | { | |
d59ceba5 VZ |
674 | ReleaseInterface(m_pIDataObject); |
675 | } | |
676 | ||
677 | void wxDataObject::SetAutoDelete() | |
678 | { | |
679 | ((wxIDataObject *)m_pIDataObject)->SetDeleteFlag(); | |
680 | m_pIDataObject->Release(); | |
681 | ||
682 | // so that the dtor doesnt' crash | |
683 | m_pIDataObject = NULL; | |
269a5a34 VZ |
684 | } |
685 | ||
574c939e | 686 | size_t wxDataObject::GetBufferOffset( const wxDataFormat& WXUNUSED(format) ) |
e1b435af MB |
687 | { |
688 | return sizeof(size_t); | |
689 | } | |
690 | ||
691 | const void* wxDataObject::GetSizeFromBuffer( const void* buffer, size_t* size, | |
574c939e | 692 | const wxDataFormat& WXUNUSED(format) ) |
e1b435af MB |
693 | { |
694 | size_t* p = (size_t*)buffer; | |
695 | *size = *p; | |
696 | ||
697 | return p + 1; | |
698 | } | |
699 | ||
700 | void* wxDataObject::SetSizeInBuffer( void* buffer, size_t size, | |
574c939e | 701 | const wxDataFormat& WXUNUSED(format) ) |
e1b435af MB |
702 | { |
703 | size_t* p = (size_t*)buffer; | |
704 | *p = size; | |
705 | ||
706 | return p + 1; | |
707 | } | |
708 | ||
d9317fd4 | 709 | #ifdef __WXDEBUG__ |
8e193f38 | 710 | |
f6bcfd97 | 711 | const wxChar *wxDataObject::GetFormatName(wxDataFormat format) |
d9317fd4 VZ |
712 | { |
713 | // case 'xxx' is not a valid value for switch of enum 'wxDataFormat' | |
714 | #ifdef __VISUALC__ | |
715 | #pragma warning(disable:4063) | |
716 | #endif // VC++ | |
717 | ||
f6bcfd97 | 718 | static wxChar s_szBuf[256]; |
d9317fd4 | 719 | switch ( format ) { |
f6bcfd97 BP |
720 | case CF_TEXT: return wxT("CF_TEXT"); |
721 | case CF_BITMAP: return wxT("CF_BITMAP"); | |
722 | case CF_METAFILEPICT: return wxT("CF_METAFILEPICT"); | |
723 | case CF_SYLK: return wxT("CF_SYLK"); | |
724 | case CF_DIF: return wxT("CF_DIF"); | |
725 | case CF_TIFF: return wxT("CF_TIFF"); | |
726 | case CF_OEMTEXT: return wxT("CF_OEMTEXT"); | |
727 | case CF_DIB: return wxT("CF_DIB"); | |
728 | case CF_PALETTE: return wxT("CF_PALETTE"); | |
729 | case CF_PENDATA: return wxT("CF_PENDATA"); | |
730 | case CF_RIFF: return wxT("CF_RIFF"); | |
731 | case CF_WAVE: return wxT("CF_WAVE"); | |
732 | case CF_UNICODETEXT: return wxT("CF_UNICODETEXT"); | |
733 | case CF_ENHMETAFILE: return wxT("CF_ENHMETAFILE"); | |
734 | case CF_HDROP: return wxT("CF_HDROP"); | |
735 | case CF_LOCALE: return wxT("CF_LOCALE"); | |
8e193f38 | 736 | |
d9317fd4 | 737 | default: |
c77ae1d9 | 738 | if ( !::GetClipboardFormatName(format, s_szBuf, WXSIZEOF(s_szBuf)) ) |
d9317fd4 VZ |
739 | { |
740 | // it must be a new predefined format we don't know the name of | |
f6bcfd97 | 741 | wxSprintf(s_szBuf, wxT("unknown CF (0x%04x)"), format.GetFormatId()); |
d9317fd4 | 742 | } |
8e193f38 | 743 | |
d9317fd4 | 744 | return s_szBuf; |
8e193f38 | 745 | } |
1e8335b0 | 746 | |
d9317fd4 VZ |
747 | #ifdef __VISUALC__ |
748 | #pragma warning(default:4063) | |
749 | #endif // VC++ | |
269a5a34 VZ |
750 | } |
751 | ||
1e8335b0 VZ |
752 | #endif // Debug |
753 | ||
3f480da3 | 754 | // ---------------------------------------------------------------------------- |
9e2896e5 | 755 | // wxBitmapDataObject supports CF_DIB format |
3f480da3 VZ |
756 | // ---------------------------------------------------------------------------- |
757 | ||
9e2896e5 | 758 | size_t wxBitmapDataObject::GetDataSize() const |
3f480da3 | 759 | { |
9e2896e5 | 760 | return wxConvertBitmapToDIB(NULL, GetBitmap()); |
3f480da3 VZ |
761 | } |
762 | ||
9e2896e5 | 763 | bool wxBitmapDataObject::GetDataHere(void *buf) const |
3f480da3 | 764 | { |
0d0512bd | 765 | return wxConvertBitmapToDIB((LPBITMAPINFO)buf, GetBitmap()) != 0; |
3f480da3 VZ |
766 | } |
767 | ||
33ac7e6f | 768 | bool wxBitmapDataObject::SetData(size_t WXUNUSED(len), const void *buf) |
3f480da3 | 769 | { |
0d0512bd | 770 | wxBitmap bitmap(wxConvertDIBToBitmap((const LPBITMAPINFO)buf)); |
9e2896e5 VZ |
771 | |
772 | if ( !bitmap.Ok() ) { | |
773 | wxFAIL_MSG(wxT("pasting/dropping invalid bitmap")); | |
3f480da3 | 774 | |
9e2896e5 VZ |
775 | return FALSE; |
776 | } | |
777 | ||
778 | SetBitmap(bitmap); | |
3f480da3 | 779 | |
9e2896e5 | 780 | return TRUE; |
3f480da3 VZ |
781 | } |
782 | ||
9e2896e5 VZ |
783 | // ---------------------------------------------------------------------------- |
784 | // wxBitmapDataObject2 supports CF_BITMAP format | |
785 | // ---------------------------------------------------------------------------- | |
786 | ||
787 | // the bitmaps aren't passed by value as other types of data (i.e. by copying | |
788 | // the data into a global memory chunk and passing it to the clipboard or | |
789 | // another application or whatever), but by handle, so these generic functions | |
790 | // don't make much sense to them. | |
791 | ||
792 | size_t wxBitmapDataObject2::GetDataSize() const | |
3f480da3 | 793 | { |
9e2896e5 | 794 | return 0; |
3f480da3 VZ |
795 | } |
796 | ||
9e2896e5 | 797 | bool wxBitmapDataObject2::GetDataHere(void *pBuf) const |
3f480da3 | 798 | { |
9e2896e5 VZ |
799 | // we put a bitmap handle into pBuf |
800 | *(WXHBITMAP *)pBuf = GetBitmap().GetHBITMAP(); | |
801 | ||
802 | return TRUE; | |
3f480da3 VZ |
803 | } |
804 | ||
265b0c07 | 805 | bool wxBitmapDataObject2::SetData(size_t WXUNUSED(len), const void *pBuf) |
3f480da3 | 806 | { |
9e2896e5 | 807 | HBITMAP hbmp = *(HBITMAP *)pBuf; |
3f480da3 | 808 | |
9e2896e5 VZ |
809 | BITMAP bmp; |
810 | if ( !GetObject(hbmp, sizeof(BITMAP), &bmp) ) | |
811 | { | |
f6bcfd97 | 812 | wxLogLastError(wxT("GetObject(HBITMAP)")); |
9e2896e5 | 813 | } |
8e193f38 | 814 | |
9e2896e5 VZ |
815 | wxBitmap bitmap(bmp.bmWidth, bmp.bmHeight, bmp.bmPlanes); |
816 | bitmap.SetHBITMAP((WXHBITMAP)hbmp); | |
d59ceba5 | 817 | |
9e2896e5 VZ |
818 | if ( !bitmap.Ok() ) { |
819 | wxFAIL_MSG(wxT("pasting/dropping invalid bitmap")); | |
820 | ||
821 | return FALSE; | |
822 | } | |
823 | ||
824 | SetBitmap(bitmap); | |
825 | ||
826 | return TRUE; | |
d59ceba5 VZ |
827 | } |
828 | ||
9e2896e5 | 829 | #if 0 |
8e193f38 VZ |
830 | |
831 | size_t wxBitmapDataObject::GetDataSize(const wxDataFormat& format) const | |
832 | { | |
d59ceba5 VZ |
833 | if ( format.GetFormatId() == CF_DIB ) |
834 | { | |
835 | // create the DIB | |
836 | ScreenHDC hdc; | |
837 | ||
838 | // shouldn't be selected into a DC or GetDIBits() would fail | |
839 | wxASSERT_MSG( !m_bitmap.GetSelectedInto(), | |
840 | wxT("can't copy bitmap selected into wxMemoryDC") ); | |
841 | ||
842 | // first get the info | |
843 | BITMAPINFO bi; | |
844 | if ( !GetDIBits(hdc, (HBITMAP)m_bitmap.GetHBITMAP(), 0, 0, | |
845 | NULL, &bi, DIB_RGB_COLORS) ) | |
846 | { | |
f6bcfd97 | 847 | wxLogLastError(wxT("GetDIBits(NULL)")); |
d59ceba5 VZ |
848 | |
849 | return 0; | |
850 | } | |
851 | ||
852 | return sizeof(BITMAPINFO) + bi.bmiHeader.biSizeImage; | |
853 | } | |
854 | else // CF_BITMAP | |
855 | { | |
856 | // no data to copy - we don't pass HBITMAP via global memory | |
857 | return 0; | |
858 | } | |
8e193f38 VZ |
859 | } |
860 | ||
d59ceba5 | 861 | bool wxBitmapDataObject::GetDataHere(const wxDataFormat& format, |
8e193f38 VZ |
862 | void *pBuf) const |
863 | { | |
d59ceba5 VZ |
864 | wxASSERT_MSG( m_bitmap.Ok(), wxT("copying invalid bitmap") ); |
865 | ||
866 | HBITMAP hbmp = (HBITMAP)m_bitmap.GetHBITMAP(); | |
867 | if ( format.GetFormatId() == CF_DIB ) | |
868 | { | |
869 | // create the DIB | |
870 | ScreenHDC hdc; | |
871 | ||
872 | // shouldn't be selected into a DC or GetDIBits() would fail | |
873 | wxASSERT_MSG( !m_bitmap.GetSelectedInto(), | |
874 | wxT("can't copy bitmap selected into wxMemoryDC") ); | |
875 | ||
876 | // first get the info | |
877 | BITMAPINFO *pbi = (BITMAPINFO *)pBuf; | |
878 | if ( !GetDIBits(hdc, hbmp, 0, 0, NULL, pbi, DIB_RGB_COLORS) ) | |
879 | { | |
f6bcfd97 | 880 | wxLogLastError(wxT("GetDIBits(NULL)")); |
d59ceba5 VZ |
881 | |
882 | return 0; | |
883 | } | |
884 | ||
885 | // and now copy the bits | |
886 | if ( !GetDIBits(hdc, hbmp, 0, pbi->bmiHeader.biHeight, pbi + 1, | |
887 | pbi, DIB_RGB_COLORS) ) | |
888 | { | |
f6bcfd97 | 889 | wxLogLastError(wxT("GetDIBits")); |
d59ceba5 VZ |
890 | |
891 | return FALSE; | |
892 | } | |
893 | } | |
894 | else // CF_BITMAP | |
895 | { | |
896 | // we put a bitmap handle into pBuf | |
897 | *(HBITMAP *)pBuf = hbmp; | |
898 | } | |
899 | ||
900 | return TRUE; | |
901 | } | |
902 | ||
9e2896e5 VZ |
903 | bool wxBitmapDataObject::SetData(const wxDataFormat& format, |
904 | size_t size, const void *pBuf) | |
d59ceba5 VZ |
905 | { |
906 | HBITMAP hbmp; | |
907 | if ( format.GetFormatId() == CF_DIB ) | |
908 | { | |
909 | // here we get BITMAPINFO struct followed by the actual bitmap bits and | |
910 | // BITMAPINFO starts with BITMAPINFOHEADER followed by colour info | |
911 | ScreenHDC hdc; | |
912 | ||
913 | BITMAPINFO *pbmi = (BITMAPINFO *)pBuf; | |
914 | BITMAPINFOHEADER *pbmih = &pbmi->bmiHeader; | |
915 | hbmp = CreateDIBitmap(hdc, pbmih, CBM_INIT, | |
916 | pbmi + 1, pbmi, DIB_RGB_COLORS); | |
917 | if ( !hbmp ) | |
918 | { | |
f6bcfd97 | 919 | wxLogLastError(wxT("CreateDIBitmap")); |
d59ceba5 VZ |
920 | } |
921 | ||
922 | m_bitmap.SetWidth(pbmih->biWidth); | |
923 | m_bitmap.SetHeight(pbmih->biHeight); | |
924 | } | |
925 | else // CF_BITMAP | |
926 | { | |
927 | // it's easy with bitmaps: we pass them by handle | |
928 | hbmp = *(HBITMAP *)pBuf; | |
929 | ||
930 | BITMAP bmp; | |
931 | if ( !GetObject(hbmp, sizeof(BITMAP), &bmp) ) | |
932 | { | |
f6bcfd97 | 933 | wxLogLastError(wxT("GetObject(HBITMAP)")); |
d59ceba5 VZ |
934 | } |
935 | ||
936 | m_bitmap.SetWidth(bmp.bmWidth); | |
937 | m_bitmap.SetHeight(bmp.bmHeight); | |
938 | m_bitmap.SetDepth(bmp.bmPlanes); | |
939 | } | |
940 | ||
941 | m_bitmap.SetHBITMAP((WXHBITMAP)hbmp); | |
942 | ||
943 | wxASSERT_MSG( m_bitmap.Ok(), wxT("pasting invalid bitmap") ); | |
944 | ||
945 | return TRUE; | |
8e193f38 VZ |
946 | } |
947 | ||
9e2896e5 VZ |
948 | #endif // 0 |
949 | ||
950 | // ---------------------------------------------------------------------------- | |
951 | // wxFileDataObject | |
952 | // ---------------------------------------------------------------------------- | |
953 | ||
954 | bool wxFileDataObject::SetData(size_t WXUNUSED(size), const void *pData) | |
955 | { | |
956 | m_filenames.Empty(); | |
957 | ||
958 | // the documentation states that the first member of DROPFILES structure is | |
959 | // a "DWORD offset of double NUL terminated file list". What they mean by | |
960 | // this (I wonder if you see it immediately) is that the list starts at | |
961 | // ((char *)&(pDropFiles.pFiles)) + pDropFiles.pFiles. We're also advised | |
962 | // to use DragQueryFile to work with this structure, but not told where and | |
963 | // how to get HDROP. | |
964 | HDROP hdrop = (HDROP)pData; // NB: it works, but I'm not sure about it | |
965 | ||
966 | // get number of files (magic value -1) | |
967 | UINT nFiles = ::DragQueryFile(hdrop, (unsigned)-1, NULL, 0u); | |
968 | ||
51edda6a VZ |
969 | wxCHECK_MSG ( nFiles != (UINT)-1, FALSE, wxT("wrong HDROP handle") ); |
970 | ||
9e2896e5 VZ |
971 | // for each file get the length, allocate memory and then get the name |
972 | wxString str; | |
973 | UINT len, n; | |
974 | for ( n = 0; n < nFiles; n++ ) { | |
975 | // +1 for terminating NUL | |
976 | len = ::DragQueryFile(hdrop, n, NULL, 0) + 1; | |
977 | ||
978 | UINT len2 = ::DragQueryFile(hdrop, n, str.GetWriteBuf(len), len); | |
979 | str.UngetWriteBuf(); | |
980 | m_filenames.Add(str); | |
981 | ||
982 | if ( len2 != len - 1 ) { | |
f6bcfd97 BP |
983 | wxLogDebug(wxT("In wxFileDropTarget::OnDrop DragQueryFile returned\ |
984 | %d characters, %d expected."), len2, len - 1); | |
9e2896e5 VZ |
985 | } |
986 | } | |
987 | ||
988 | return TRUE; | |
989 | } | |
990 | ||
8b85d24e VZ |
991 | void wxFileDataObject::AddFile(const wxString& file) |
992 | { | |
993 | // just add file to filenames array | |
994 | // all useful data (such as DROPFILES struct) will be | |
995 | // created later as necessary | |
996 | m_filenames.Add(file); | |
997 | } | |
998 | ||
999 | size_t wxFileDataObject::GetDataSize() const | |
1000 | { | |
1001 | // size returned will be the size of the DROPFILES structure, | |
1002 | // plus the list of filesnames (null byte separated), plus | |
1003 | // a double null at the end | |
1004 | ||
1005 | // if no filenames in list, size is 0 | |
dd10a646 VZ |
1006 | if ( m_filenames.GetCount() == 0 ) |
1007 | return 0; | |
8b85d24e VZ |
1008 | |
1009 | // inital size of DROPFILES struct + null byte | |
1010 | size_t sz = sizeof(DROPFILES) + 1; | |
1011 | ||
dd10a646 VZ |
1012 | size_t count = m_filenames.GetCount(); |
1013 | for ( size_t i = 0; i < count; i++ ) | |
8b85d24e VZ |
1014 | { |
1015 | // add filename length plus null byte | |
1016 | sz += m_filenames[i].Len() + 1; | |
1017 | } | |
dd10a646 | 1018 | |
8b85d24e VZ |
1019 | return sz; |
1020 | } | |
1021 | ||
1022 | bool wxFileDataObject::GetDataHere(void *pData) const | |
1023 | { | |
1024 | // pData points to an externally allocated memory block | |
1025 | // created using the size returned by GetDataSize() | |
1026 | ||
1027 | // if pData is NULL, or there are no files, return | |
dd10a646 VZ |
1028 | if ( !pData || m_filenames.GetCount() == 0 ) |
1029 | return FALSE; | |
8b85d24e VZ |
1030 | |
1031 | // convert data pointer to a DROPFILES struct pointer | |
1032 | LPDROPFILES pDrop = (LPDROPFILES) pData; | |
1033 | ||
1034 | // initialize DROPFILES struct | |
1035 | pDrop->pFiles = sizeof(DROPFILES); | |
dd10a646 VZ |
1036 | pDrop->fNC = FALSE; // not non-client coords |
1037 | #if wxUSE_UNICODE | |
1038 | pDrop->fWide = TRUE; | |
1039 | #else // ANSI | |
8b85d24e | 1040 | pDrop->fWide = FALSE; |
dd10a646 | 1041 | #endif // Unicode/Ansi |
8b85d24e VZ |
1042 | |
1043 | // set start of filenames list (null separated) | |
51babd09 | 1044 | wxChar *pbuf = (wxChar*) ((BYTE *)pDrop + sizeof(DROPFILES)); |
8b85d24e | 1045 | |
dd10a646 VZ |
1046 | size_t count = m_filenames.GetCount(); |
1047 | for (size_t i = 0; i < count; i++ ) | |
8b85d24e VZ |
1048 | { |
1049 | // copy filename to pbuf and add null terminator | |
1050 | size_t len = m_filenames[i].Len(); | |
1051 | memcpy(pbuf, m_filenames[i], len); | |
1052 | pbuf += len; | |
dd10a646 | 1053 | *pbuf++ = wxT('\0'); |
8b85d24e | 1054 | } |
dd10a646 | 1055 | |
c5639a87 VZ |
1056 | // add final null terminator |
1057 | *pbuf = wxT('\0'); | |
8b85d24e VZ |
1058 | |
1059 | return TRUE; | |
1060 | } | |
1061 | ||
444ad3a7 VZ |
1062 | // ---------------------------------------------------------------------------- |
1063 | // wxURLDataObject | |
1064 | // ---------------------------------------------------------------------------- | |
1065 | ||
4a09bc4e RD |
1066 | |
1067 | class CFSTR_SHELLURLDataObject : public wxCustomDataObject | |
e1b435af MB |
1068 | { |
1069 | public: | |
1070 | CFSTR_SHELLURLDataObject() : wxCustomDataObject(CFSTR_SHELLURL) {} | |
1071 | protected: | |
574c939e | 1072 | virtual size_t GetBufferOffset( const wxDataFormat& WXUNUSED(format) ) |
e1b435af MB |
1073 | { |
1074 | return 0; | |
1075 | } | |
1076 | ||
1077 | virtual const void* GetSizeFromBuffer( const void* buffer, size_t* size, | |
574c939e | 1078 | const wxDataFormat& WXUNUSED(format) ) |
e1b435af MB |
1079 | { |
1080 | // CFSTR_SHELLURL is _always_ ANSI text | |
1081 | *size = strlen( (const char*)buffer ); | |
1082 | ||
1083 | return buffer; | |
1084 | } | |
1085 | ||
574c939e KB |
1086 | virtual void* SetSizeInBuffer( void* buffer, size_t WXUNUSED(size), |
1087 | const wxDataFormat& WXUNUSED(format) ) | |
e1b435af MB |
1088 | { |
1089 | return buffer; | |
1090 | } | |
4a09bc4e | 1091 | |
e1b435af MB |
1092 | #if wxUSE_UNICODE |
1093 | virtual bool GetDataHere( void* buffer ) const | |
1094 | { | |
1095 | // CFSTR_SHELLURL is _always_ ANSI! | |
1096 | wxCharBuffer char_buffer( GetDataSize() ); | |
1097 | wxCustomDataObject::GetDataHere( (void*)char_buffer.data() ); | |
1098 | wxString unicode_buffer( char_buffer ); | |
1099 | memcpy( buffer, unicode_buffer.c_str(), | |
1100 | ( unicode_buffer.length() + 1 ) * sizeof(wxChar) ); | |
1101 | ||
1102 | return TRUE; | |
1103 | } | |
1104 | #endif | |
1105 | }; | |
1106 | ||
4a09bc4e RD |
1107 | |
1108 | ||
444ad3a7 VZ |
1109 | wxURLDataObject::wxURLDataObject() |
1110 | { | |
1111 | // we support CF_TEXT and CFSTR_SHELLURL formats which are basicly the same | |
e1b435af | 1112 | // but it seems that some browsers only provide one of them so we have to |
444ad3a7 | 1113 | // support both |
444ad3a7 | 1114 | Add(new wxTextDataObject); |
e6d318c2 | 1115 | Add(new CFSTR_SHELLURLDataObject()); |
444ad3a7 VZ |
1116 | |
1117 | // we don't have any data yet | |
1118 | m_dataObjectLast = NULL; | |
1119 | } | |
1120 | ||
1121 | bool wxURLDataObject::SetData(const wxDataFormat& format, | |
1122 | size_t len, | |
1123 | const void *buf) | |
1124 | { | |
1125 | m_dataObjectLast = GetObject(format); | |
1126 | ||
1127 | wxCHECK_MSG( m_dataObjectLast, FALSE, | |
1128 | wxT("unsupported format in wxURLDataObject")); | |
1129 | ||
1130 | return m_dataObjectLast->SetData(len, buf); | |
1131 | } | |
1132 | ||
1133 | wxString wxURLDataObject::GetURL() const | |
1134 | { | |
1135 | wxString url; | |
1136 | wxCHECK_MSG( m_dataObjectLast, url, _T("no data in wxURLDataObject") ); | |
1137 | ||
1138 | size_t len = m_dataObjectLast->GetDataSize(); | |
1139 | ||
e6d318c2 | 1140 | m_dataObjectLast->GetDataHere(url.GetWriteBuf(len)); |
444ad3a7 VZ |
1141 | url.UngetWriteBuf(); |
1142 | ||
1143 | return url; | |
1144 | } | |
1145 | ||
e6d318c2 RD |
1146 | void wxURLDataObject::SetURL(const wxString& url) |
1147 | { | |
4a09bc4e RD |
1148 | SetData(wxDataFormat(wxUSE_UNICODE ? wxDF_UNICODETEXT : wxDF_TEXT), |
1149 | url.Length()+1, url.c_str()); | |
1150 | ||
1151 | // CFSTR_SHELLURL is always supposed to be ANSI... | |
1152 | wxWX2MBbuf urlA = (wxWX2MBbuf)url.mbc_str(); | |
1153 | size_t len = strlen(urlA); | |
1154 | SetData(wxDataFormat(CFSTR_SHELLURL), len+1, (const char*)urlA); | |
e6d318c2 RD |
1155 | } |
1156 | ||
269a5a34 VZ |
1157 | // ---------------------------------------------------------------------------- |
1158 | // private functions | |
1159 | // ---------------------------------------------------------------------------- | |
8e193f38 | 1160 | |
8ee9d618 VZ |
1161 | static size_t wxGetNumOfBitmapColors(size_t bitsPerPixel) |
1162 | { | |
1163 | switch ( bitsPerPixel ) | |
1164 | { | |
1165 | case 1: | |
1166 | // monochrome bitmap, 2 entries | |
1167 | return 2; | |
1168 | ||
1169 | case 4: | |
1170 | return 16; | |
1171 | ||
1172 | case 8: | |
1173 | return 256; | |
1174 | ||
1175 | case 24: | |
1176 | // may be used with 24bit bitmaps, but we don't use it here - fall | |
1177 | // through | |
1178 | ||
1179 | case 16: | |
1180 | case 32: | |
1181 | // bmiColors not used at all with these bitmaps | |
1182 | return 0; | |
1183 | ||
1184 | default: | |
1185 | wxFAIL_MSG( wxT("unknown bitmap format") ); | |
1186 | return 0; | |
1187 | } | |
1188 | } | |
9e2896e5 | 1189 | |
0d0512bd | 1190 | size_t wxConvertBitmapToDIB(LPBITMAPINFO pbi, const wxBitmap& bitmap) |
9e2896e5 | 1191 | { |
8ee9d618 VZ |
1192 | wxASSERT_MSG( bitmap.Ok(), wxT("invalid bmp can't be converted to DIB") ); |
1193 | ||
9e2896e5 VZ |
1194 | // shouldn't be selected into a DC or GetDIBits() would fail |
1195 | wxASSERT_MSG( !bitmap.GetSelectedInto(), | |
1196 | wxT("can't copy bitmap selected into wxMemoryDC") ); | |
1197 | ||
8ee9d618 VZ |
1198 | // prepare all the info we need |
1199 | BITMAP bm; | |
9e2896e5 | 1200 | HBITMAP hbmp = (HBITMAP)bitmap.GetHBITMAP(); |
8ee9d618 VZ |
1201 | if ( !GetObject(hbmp, sizeof(bm), &bm) ) |
1202 | { | |
f6bcfd97 | 1203 | wxLogLastError(wxT("GetObject(bitmap)")); |
8ee9d618 VZ |
1204 | |
1205 | return 0; | |
1206 | } | |
1207 | ||
1208 | // calculate the number of bits per pixel and the number of items in | |
1209 | // bmiColors array (whose meaning depends on the bitmap format) | |
1210 | WORD biBits = bm.bmPlanes * bm.bmBitsPixel; | |
33ac7e6f | 1211 | WORD biColors = (WORD)wxGetNumOfBitmapColors(biBits); |
9e2896e5 | 1212 | |
8ee9d618 | 1213 | BITMAPINFO bi2; |
9e2896e5 | 1214 | |
8ee9d618 VZ |
1215 | bool wantSizeOnly = pbi == NULL; |
1216 | if ( wantSizeOnly ) | |
1217 | pbi = &bi2; | |
1218 | ||
1219 | // just for convenience | |
1220 | BITMAPINFOHEADER& bi = pbi->bmiHeader; | |
1221 | ||
1222 | bi.biSize = sizeof(BITMAPINFOHEADER); | |
1223 | bi.biWidth = bm.bmWidth; | |
1224 | bi.biHeight = bm.bmHeight; | |
1225 | bi.biPlanes = 1; | |
1226 | bi.biBitCount = biBits; | |
1227 | bi.biCompression = BI_RGB; | |
1228 | bi.biSizeImage = 0; | |
1229 | bi.biXPelsPerMeter = 0; | |
1230 | bi.biYPelsPerMeter = 0; | |
1231 | bi.biClrUsed = 0; | |
1232 | bi.biClrImportant = 0; | |
1233 | ||
1234 | // memory we need for BITMAPINFO only | |
1235 | DWORD dwLen = bi.biSize + biColors * sizeof(RGBQUAD); | |
1236 | ||
1237 | // first get the image size | |
9e2896e5 | 1238 | ScreenHDC hdc; |
8ee9d618 | 1239 | if ( !GetDIBits(hdc, hbmp, 0, bi.biHeight, NULL, pbi, DIB_RGB_COLORS) ) |
9e2896e5 | 1240 | { |
f6bcfd97 | 1241 | wxLogLastError(wxT("GetDIBits(NULL)")); |
9e2896e5 VZ |
1242 | |
1243 | return 0; | |
1244 | } | |
1245 | ||
8ee9d618 | 1246 | if ( wantSizeOnly ) |
9e2896e5 | 1247 | { |
8ee9d618 VZ |
1248 | // size of the header + size of the image |
1249 | return dwLen + bi.biSizeImage; | |
9e2896e5 VZ |
1250 | } |
1251 | ||
1252 | // and now copy the bits | |
8ee9d618 VZ |
1253 | void *image = (char *)pbi + dwLen; |
1254 | if ( !GetDIBits(hdc, hbmp, 0, bi.biHeight, image, pbi, DIB_RGB_COLORS) ) | |
9e2896e5 | 1255 | { |
f6bcfd97 | 1256 | wxLogLastError(wxT("GetDIBits")); |
9e2896e5 VZ |
1257 | |
1258 | return 0; | |
1259 | } | |
1260 | ||
8ee9d618 | 1261 | return dwLen + bi.biSizeImage; |
9e2896e5 VZ |
1262 | } |
1263 | ||
0d0512bd | 1264 | wxBitmap wxConvertDIBToBitmap(const LPBITMAPINFO pbmi) |
9e2896e5 VZ |
1265 | { |
1266 | // here we get BITMAPINFO struct followed by the actual bitmap bits and | |
1267 | // BITMAPINFO starts with BITMAPINFOHEADER followed by colour info | |
1268 | const BITMAPINFOHEADER *pbmih = &pbmi->bmiHeader; | |
1269 | ||
232b0051 JS |
1270 | // biClrUsed has the number of colors, unless it's 0 |
1271 | int numColors = pbmih->biClrUsed; | |
1272 | if (numColors==0) | |
1273 | { | |
1274 | numColors = wxGetNumOfBitmapColors(pbmih->biBitCount); | |
1275 | } | |
1276 | ||
8ee9d618 | 1277 | // offset of image from the beginning of the header |
232b0051 | 1278 | DWORD ofs = numColors * sizeof(RGBQUAD); |
8ee9d618 VZ |
1279 | void *image = (char *)pbmih + sizeof(BITMAPINFOHEADER) + ofs; |
1280 | ||
9e2896e5 VZ |
1281 | ScreenHDC hdc; |
1282 | HBITMAP hbmp = CreateDIBitmap(hdc, pbmih, CBM_INIT, | |
8ee9d618 | 1283 | image, pbmi, DIB_RGB_COLORS); |
9e2896e5 VZ |
1284 | if ( !hbmp ) |
1285 | { | |
f6bcfd97 | 1286 | wxLogLastError(wxT("CreateDIBitmap")); |
9e2896e5 VZ |
1287 | } |
1288 | ||
1289 | wxBitmap bitmap(pbmih->biWidth, pbmih->biHeight, pbmih->biBitCount); | |
1290 | bitmap.SetHBITMAP((WXHBITMAP)hbmp); | |
1291 | ||
1292 | return bitmap; | |
1293 | } | |
1294 | ||
8e193f38 VZ |
1295 | #ifdef __WXDEBUG__ |
1296 | ||
d59ceba5 VZ |
1297 | static const wxChar *GetTymedName(DWORD tymed) |
1298 | { | |
1299 | static wxChar s_szBuf[128]; | |
1300 | switch ( tymed ) { | |
1301 | case TYMED_HGLOBAL: return wxT("TYMED_HGLOBAL"); | |
1302 | case TYMED_FILE: return wxT("TYMED_FILE"); | |
1303 | case TYMED_ISTREAM: return wxT("TYMED_ISTREAM"); | |
1304 | case TYMED_ISTORAGE: return wxT("TYMED_ISTORAGE"); | |
1305 | case TYMED_GDI: return wxT("TYMED_GDI"); | |
1306 | case TYMED_MFPICT: return wxT("TYMED_MFPICT"); | |
1307 | case TYMED_ENHMF: return wxT("TYMED_ENHMF"); | |
1308 | default: | |
dfd2e675 | 1309 | wxSprintf(s_szBuf, wxT("type of media format %ld (unknown)"), tymed); |
d59ceba5 VZ |
1310 | return s_szBuf; |
1311 | } | |
269a5a34 | 1312 | } |
5260b1c5 | 1313 | |
8e193f38 | 1314 | #endif // Debug |
2845ddc2 | 1315 | |
21709999 JS |
1316 | #else // not using OLE at all |
1317 | // ---------------------------------------------------------------------------- | |
1318 | // wxDataObject | |
1319 | // ---------------------------------------------------------------------------- | |
1320 | ||
1321 | wxDataObject::wxDataObject() | |
1322 | { | |
1323 | } | |
1324 | ||
1325 | wxDataObject::~wxDataObject() | |
1326 | { | |
1327 | } | |
1328 | ||
1329 | void wxDataObject::SetAutoDelete() | |
1330 | { | |
1331 | } | |
1332 | ||
bd52bee1 | 1333 | #ifdef __WXDEBUG__ |
21709999 JS |
1334 | const wxChar *wxDataObject::GetFormatName(wxDataFormat format) |
1335 | { | |
1336 | return NULL; | |
1337 | } | |
bd52bee1 | 1338 | #endif |
21709999 JS |
1339 | |
1340 | #endif | |
5260b1c5 | 1341 |