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