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