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