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