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