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