]> git.saurik.com Git - wxWidgets.git/blame - src/msw/ole/dataobj.cpp
attempt to fix wxMenuBar::EnableTop
[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;
4d85bcd1 454#ifndef __WATCOMC__
9e2896e5
VZ
455 case CF_UNICODETEXT:
456 size = wcslen((const wchar_t *)pBuf);
457 break;
4d85bcd1 458#endif
9e2896e5
VZ
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
8ee9d618
VZ
466 case CF_DIB:
467 // the handler will calculate size itself (it's too
468 // complicated to do it here)
469 size = 0;
470 break;
471
9e2896e5
VZ
472 default:
473 {
474 // we suppose that the size precedes the data
475 size_t *p = (size_t *)pBuf;
476 size = *p++;
477 pBuf = p;
478 }
479 }
480
481 wxDataFormat format = pformatetc->cfFormat;
482 bool ok = m_pDataObject->SetData(format, size, pBuf);
d59ceba5
VZ
483
484 GlobalUnlock(pmedium->hGlobal);
9e2896e5
VZ
485
486 if ( !ok ) {
487 return E_UNEXPECTED;
488 }
d59ceba5
VZ
489 }
490 break;
491
492 default:
493 return DV_E_TYMED;
494 }
495
496 if ( fRelease ) {
497 // we own the medium, so we must release it - but do *not* free the
498 // bitmap handle fi we have it because we have copied it elsewhere
499 if ( pmedium->tymed == TYMED_GDI )
500 {
501 pmedium->hBitmap = 0;
502 }
503
504 ReleaseStgMedium(pmedium);
505 }
506
507 return S_OK;
269a5a34
VZ
508}
509
510// information functions
511STDMETHODIMP wxIDataObject::QueryGetData(FORMATETC *pformatetc)
512{
d59ceba5
VZ
513 // do we accept data in this format?
514 if ( pformatetc == NULL ) {
515 wxLogTrace(wxTRACE_OleCalls,
516 wxT("wxIDataObject::QueryGetData: invalid ptr."));
8e193f38 517
d59ceba5
VZ
518 return E_INVALIDARG;
519 }
269a5a34 520
d59ceba5
VZ
521 // the only one allowed by current COM implementation
522 if ( pformatetc->lindex != -1 ) {
523 wxLogTrace(wxTRACE_OleCalls,
524 wxT("wxIDataObject::QueryGetData: bad lindex %d"),
525 pformatetc->lindex);
269a5a34 526
d59ceba5
VZ
527 return DV_E_LINDEX;
528 }
269a5a34 529
d59ceba5
VZ
530 // we don't support anything other (THUMBNAIL, ICON, DOCPRINT...)
531 if ( pformatetc->dwAspect != DVASPECT_CONTENT ) {
532 wxLogTrace(wxTRACE_OleCalls,
533 wxT("wxIDataObject::QueryGetData: bad dwAspect %d"),
534 pformatetc->dwAspect);
535
536 return DV_E_DVASPECT;
537 }
538
539 // and now check the type of data requested
9e2896e5 540 wxDataFormat format = pformatetc->cfFormat;
d59ceba5 541 if ( m_pDataObject->IsSupportedFormat(format) ) {
d59ceba5 542 wxLogTrace(wxTRACE_OleCalls, wxT("wxIDataObject::QueryGetData: %s ok"),
1e8335b0 543 wxGetFormatName(format));
d59ceba5
VZ
544 }
545 else {
546 wxLogTrace(wxTRACE_OleCalls,
547 wxT("wxIDataObject::QueryGetData: %s unsupported"),
1e8335b0 548 wxGetFormatName(format));
9e2896e5 549
d59ceba5
VZ
550 return DV_E_FORMATETC;
551 }
552
553 // we only transfer data by global memory, except for some particular cases
554 DWORD tymed = pformatetc->tymed;
555 if ( (format == wxDF_BITMAP && !(tymed & TYMED_GDI)) &&
556 !(tymed & TYMED_HGLOBAL) ) {
557 // it's not what we're waiting for
d59ceba5
VZ
558 wxLogTrace(wxTRACE_OleCalls,
559 wxT("wxIDataObject::QueryGetData: %s != %s"),
560 GetTymedName(tymed),
561 GetTymedName(format == wxDF_BITMAP ? TYMED_GDI
562 : TYMED_HGLOBAL));
d59ceba5
VZ
563
564 return DV_E_TYMED;
565 }
566
269a5a34 567 return S_OK;
269a5a34
VZ
568}
569
570STDMETHODIMP wxIDataObject::GetCanonicalFormatEtc(FORMATETC *pFormatetcIn,
571 FORMATETC *pFormatetcOut)
572{
8e193f38
VZ
573 wxLogTrace(wxTRACE_OleCalls, wxT("wxIDataObject::GetCanonicalFormatEtc"));
574
575 // TODO we might want something better than this trivial implementation here
576 if ( pFormatetcOut != NULL )
577 pFormatetcOut->ptd = NULL;
269a5a34 578
8e193f38 579 return DATA_S_SAMEFORMATETC;
269a5a34
VZ
580}
581
9e2896e5 582STDMETHODIMP wxIDataObject::EnumFormatEtc(DWORD dwDir,
269a5a34
VZ
583 IEnumFORMATETC **ppenumFormatEtc)
584{
8e193f38 585 wxLogTrace(wxTRACE_OleCalls, wxT("wxIDataObject::EnumFormatEtc"));
269a5a34 586
9e2896e5
VZ
587 wxDataObject::Direction dir = dwDir == DATADIR_GET ? wxDataObject::Get
588 : wxDataObject::Set;
269a5a34 589
9e2896e5 590 size_t nFormatCount = m_pDataObject->GetFormatCount(dir);
8e193f38 591 wxDataFormat format, *formats;
9e2896e5
VZ
592 formats = nFormatCount == 1 ? &format : new wxDataFormat[nFormatCount];
593 m_pDataObject->GetAllFormats(formats, dir);
8e193f38
VZ
594
595 wxIEnumFORMATETC *pEnum = new wxIEnumFORMATETC(formats, nFormatCount);
596 pEnum->AddRef();
597 *ppenumFormatEtc = pEnum;
598
599 if ( formats != &format ) {
600 delete [] formats;
601 }
269a5a34 602
8e193f38 603 return S_OK;
269a5a34
VZ
604}
605
9e2896e5 606// ----------------------------------------------------------------------------
269a5a34 607// advise sink functions (not implemented)
9e2896e5
VZ
608// ----------------------------------------------------------------------------
609
269a5a34
VZ
610STDMETHODIMP wxIDataObject::DAdvise(FORMATETC *pformatetc,
611 DWORD advf,
612 IAdviseSink *pAdvSink,
613 DWORD *pdwConnection)
614{
615 return OLE_E_ADVISENOTSUPPORTED;
616}
617
618STDMETHODIMP wxIDataObject::DUnadvise(DWORD dwConnection)
619{
620 return OLE_E_ADVISENOTSUPPORTED;
621}
622
623STDMETHODIMP wxIDataObject::EnumDAdvise(IEnumSTATDATA **ppenumAdvise)
624{
625 return OLE_E_ADVISENOTSUPPORTED;
626}
627
628// ----------------------------------------------------------------------------
629// wxDataObject
630// ----------------------------------------------------------------------------
631
632wxDataObject::wxDataObject()
633{
d59ceba5
VZ
634 m_pIDataObject = new wxIDataObject(this);
635 m_pIDataObject->AddRef();
269a5a34
VZ
636}
637
638wxDataObject::~wxDataObject()
639{
d59ceba5
VZ
640 ReleaseInterface(m_pIDataObject);
641}
642
643void wxDataObject::SetAutoDelete()
644{
645 ((wxIDataObject *)m_pIDataObject)->SetDeleteFlag();
646 m_pIDataObject->Release();
647
648 // so that the dtor doesnt' crash
649 m_pIDataObject = NULL;
269a5a34
VZ
650}
651
8e193f38 652bool wxDataObject::IsSupportedFormat(const wxDataFormat& format) const
269a5a34 653{
8e193f38
VZ
654 size_t nFormatCount = GetFormatCount();
655 if ( nFormatCount == 1 ) {
656 return format == GetPreferredFormat();
657 }
658 else {
659 wxDataFormat *formats = new wxDataFormat[nFormatCount];
660 GetAllFormats(formats);
661
662 size_t n;
663 for ( n = 0; n < nFormatCount; n++ ) {
664 if ( formats[n] == format )
665 break;
666 }
667
668 delete [] formats;
669
670 // found?
671 return n < nFormatCount;
672 }
673}
674
1e8335b0
VZ
675#ifdef __WXDEBUG__
676
8e193f38
VZ
677const char *wxDataObject::GetFormatName(wxDataFormat format)
678{
fd3f686c 679 // case 'xxx' is not a valid value for switch of enum 'wxDataFormat'
3f4a0c5b 680 #ifdef __VISUALC__
fd3f686c
VZ
681 #pragma warning(disable:4063)
682 #endif // VC++
683
269a5a34
VZ
684 static char s_szBuf[128];
685 switch ( format ) {
9b64e798 686 case CF_TEXT: return "CF_TEXT";
269a5a34 687 case CF_BITMAP: return "CF_BITMAP";
9b64e798 688 case CF_METAFILEPICT: return "CF_METAFILEPICT";
269a5a34
VZ
689 case CF_SYLK: return "CF_SYLK";
690 case CF_DIF: return "CF_DIF";
691 case CF_TIFF: return "CF_TIFF";
9b64e798 692 case CF_OEMTEXT: return "CF_OEMTEXT";
269a5a34
VZ
693 case CF_DIB: return "CF_DIB";
694 case CF_PALETTE: return "CF_PALETTE";
695 case CF_PENDATA: return "CF_PENDATA";
696 case CF_RIFF: return "CF_RIFF";
697 case CF_WAVE: return "CF_WAVE";
9b64e798 698 case CF_UNICODETEXT: return "CF_UNICODETEXT";
269a5a34
VZ
699 case CF_ENHMETAFILE: return "CF_ENHMETAFILE";
700 case CF_HDROP: return "CF_HDROP";
701 case CF_LOCALE: return "CF_LOCALE";
702 default:
d59ceba5 703 sprintf(s_szBuf, "clipboard format 0x%x (unknown)", format);
269a5a34
VZ
704 return s_szBuf;
705 }
fd3f686c 706
3f4a0c5b 707 #ifdef __VISUALC__
fd3f686c
VZ
708 #pragma warning(default:4063)
709 #endif // VC++
269a5a34
VZ
710}
711
1e8335b0
VZ
712#endif // Debug
713
3f480da3 714// ----------------------------------------------------------------------------
9e2896e5 715// wxBitmapDataObject supports CF_DIB format
3f480da3
VZ
716// ----------------------------------------------------------------------------
717
9e2896e5 718size_t wxBitmapDataObject::GetDataSize() const
3f480da3 719{
9e2896e5 720 return wxConvertBitmapToDIB(NULL, GetBitmap());
3f480da3
VZ
721}
722
9e2896e5 723bool wxBitmapDataObject::GetDataHere(void *buf) const
3f480da3 724{
9e2896e5 725 return wxConvertBitmapToDIB((BITMAPINFO *)buf, GetBitmap()) != 0;
3f480da3
VZ
726}
727
9e2896e5 728bool wxBitmapDataObject::SetData(size_t len, const void *buf)
3f480da3 729{
9e2896e5
VZ
730 wxBitmap bitmap(wxConvertDIBToBitmap((const BITMAPINFO *)buf));
731
732 if ( !bitmap.Ok() ) {
733 wxFAIL_MSG(wxT("pasting/dropping invalid bitmap"));
3f480da3 734
9e2896e5
VZ
735 return FALSE;
736 }
737
738 SetBitmap(bitmap);
3f480da3 739
9e2896e5 740 return TRUE;
3f480da3
VZ
741}
742
9e2896e5
VZ
743// ----------------------------------------------------------------------------
744// wxBitmapDataObject2 supports CF_BITMAP format
745// ----------------------------------------------------------------------------
746
747// the bitmaps aren't passed by value as other types of data (i.e. by copying
748// the data into a global memory chunk and passing it to the clipboard or
749// another application or whatever), but by handle, so these generic functions
750// don't make much sense to them.
751
752size_t wxBitmapDataObject2::GetDataSize() const
3f480da3 753{
9e2896e5 754 return 0;
3f480da3
VZ
755}
756
9e2896e5 757bool wxBitmapDataObject2::GetDataHere(void *pBuf) const
3f480da3 758{
9e2896e5
VZ
759 // we put a bitmap handle into pBuf
760 *(WXHBITMAP *)pBuf = GetBitmap().GetHBITMAP();
761
762 return TRUE;
3f480da3
VZ
763}
764
9e2896e5 765bool wxBitmapDataObject2::SetData(size_t len, const void *pBuf)
3f480da3 766{
9e2896e5 767 HBITMAP hbmp = *(HBITMAP *)pBuf;
3f480da3 768
9e2896e5
VZ
769 BITMAP bmp;
770 if ( !GetObject(hbmp, sizeof(BITMAP), &bmp) )
771 {
772 wxLogLastError("GetObject(HBITMAP)");
773 }
8e193f38 774
9e2896e5
VZ
775 wxBitmap bitmap(bmp.bmWidth, bmp.bmHeight, bmp.bmPlanes);
776 bitmap.SetHBITMAP((WXHBITMAP)hbmp);
d59ceba5 777
9e2896e5
VZ
778 if ( !bitmap.Ok() ) {
779 wxFAIL_MSG(wxT("pasting/dropping invalid bitmap"));
780
781 return FALSE;
782 }
783
784 SetBitmap(bitmap);
785
786 return TRUE;
d59ceba5
VZ
787}
788
9e2896e5 789#if 0
8e193f38
VZ
790
791size_t wxBitmapDataObject::GetDataSize(const wxDataFormat& format) const
792{
d59ceba5
VZ
793 if ( format.GetFormatId() == CF_DIB )
794 {
795 // create the DIB
796 ScreenHDC hdc;
797
798 // shouldn't be selected into a DC or GetDIBits() would fail
799 wxASSERT_MSG( !m_bitmap.GetSelectedInto(),
800 wxT("can't copy bitmap selected into wxMemoryDC") );
801
802 // first get the info
803 BITMAPINFO bi;
804 if ( !GetDIBits(hdc, (HBITMAP)m_bitmap.GetHBITMAP(), 0, 0,
805 NULL, &bi, DIB_RGB_COLORS) )
806 {
807 wxLogLastError("GetDIBits(NULL)");
808
809 return 0;
810 }
811
812 return sizeof(BITMAPINFO) + bi.bmiHeader.biSizeImage;
813 }
814 else // CF_BITMAP
815 {
816 // no data to copy - we don't pass HBITMAP via global memory
817 return 0;
818 }
8e193f38
VZ
819}
820
d59ceba5 821bool wxBitmapDataObject::GetDataHere(const wxDataFormat& format,
8e193f38
VZ
822 void *pBuf) const
823{
d59ceba5
VZ
824 wxASSERT_MSG( m_bitmap.Ok(), wxT("copying invalid bitmap") );
825
826 HBITMAP hbmp = (HBITMAP)m_bitmap.GetHBITMAP();
827 if ( format.GetFormatId() == CF_DIB )
828 {
829 // create the DIB
830 ScreenHDC hdc;
831
832 // shouldn't be selected into a DC or GetDIBits() would fail
833 wxASSERT_MSG( !m_bitmap.GetSelectedInto(),
834 wxT("can't copy bitmap selected into wxMemoryDC") );
835
836 // first get the info
837 BITMAPINFO *pbi = (BITMAPINFO *)pBuf;
838 if ( !GetDIBits(hdc, hbmp, 0, 0, NULL, pbi, DIB_RGB_COLORS) )
839 {
840 wxLogLastError("GetDIBits(NULL)");
841
842 return 0;
843 }
844
845 // and now copy the bits
846 if ( !GetDIBits(hdc, hbmp, 0, pbi->bmiHeader.biHeight, pbi + 1,
847 pbi, DIB_RGB_COLORS) )
848 {
849 wxLogLastError("GetDIBits");
850
851 return FALSE;
852 }
853 }
854 else // CF_BITMAP
855 {
856 // we put a bitmap handle into pBuf
857 *(HBITMAP *)pBuf = hbmp;
858 }
859
860 return TRUE;
861}
862
9e2896e5
VZ
863bool wxBitmapDataObject::SetData(const wxDataFormat& format,
864 size_t size, const void *pBuf)
d59ceba5
VZ
865{
866 HBITMAP hbmp;
867 if ( format.GetFormatId() == CF_DIB )
868 {
869 // here we get BITMAPINFO struct followed by the actual bitmap bits and
870 // BITMAPINFO starts with BITMAPINFOHEADER followed by colour info
871 ScreenHDC hdc;
872
873 BITMAPINFO *pbmi = (BITMAPINFO *)pBuf;
874 BITMAPINFOHEADER *pbmih = &pbmi->bmiHeader;
875 hbmp = CreateDIBitmap(hdc, pbmih, CBM_INIT,
876 pbmi + 1, pbmi, DIB_RGB_COLORS);
877 if ( !hbmp )
878 {
879 wxLogLastError("CreateDIBitmap");
880 }
881
882 m_bitmap.SetWidth(pbmih->biWidth);
883 m_bitmap.SetHeight(pbmih->biHeight);
884 }
885 else // CF_BITMAP
886 {
887 // it's easy with bitmaps: we pass them by handle
888 hbmp = *(HBITMAP *)pBuf;
889
890 BITMAP bmp;
891 if ( !GetObject(hbmp, sizeof(BITMAP), &bmp) )
892 {
893 wxLogLastError("GetObject(HBITMAP)");
894 }
895
896 m_bitmap.SetWidth(bmp.bmWidth);
897 m_bitmap.SetHeight(bmp.bmHeight);
898 m_bitmap.SetDepth(bmp.bmPlanes);
899 }
900
901 m_bitmap.SetHBITMAP((WXHBITMAP)hbmp);
902
903 wxASSERT_MSG( m_bitmap.Ok(), wxT("pasting invalid bitmap") );
904
905 return TRUE;
8e193f38
VZ
906}
907
9e2896e5
VZ
908#endif // 0
909
910// ----------------------------------------------------------------------------
911// wxFileDataObject
912// ----------------------------------------------------------------------------
913
914bool wxFileDataObject::SetData(size_t WXUNUSED(size), const void *pData)
915{
916 m_filenames.Empty();
917
918 // the documentation states that the first member of DROPFILES structure is
919 // a "DWORD offset of double NUL terminated file list". What they mean by
920 // this (I wonder if you see it immediately) is that the list starts at
921 // ((char *)&(pDropFiles.pFiles)) + pDropFiles.pFiles. We're also advised
922 // to use DragQueryFile to work with this structure, but not told where and
923 // how to get HDROP.
924 HDROP hdrop = (HDROP)pData; // NB: it works, but I'm not sure about it
925
926 // get number of files (magic value -1)
927 UINT nFiles = ::DragQueryFile(hdrop, (unsigned)-1, NULL, 0u);
928
929 // for each file get the length, allocate memory and then get the name
930 wxString str;
931 UINT len, n;
932 for ( n = 0; n < nFiles; n++ ) {
933 // +1 for terminating NUL
934 len = ::DragQueryFile(hdrop, n, NULL, 0) + 1;
935
936 UINT len2 = ::DragQueryFile(hdrop, n, str.GetWriteBuf(len), len);
937 str.UngetWriteBuf();
938 m_filenames.Add(str);
939
940 if ( len2 != len - 1 ) {
941 wxLogDebug(wxT("In wxFileDropTarget::OnDrop DragQueryFile returned"
942 " %d characters, %d expected."), len2, len - 1);
943 }
944 }
945
946 return TRUE;
947}
948
269a5a34
VZ
949// ----------------------------------------------------------------------------
950// private functions
951// ----------------------------------------------------------------------------
8e193f38 952
8ee9d618
VZ
953static size_t wxGetNumOfBitmapColors(size_t bitsPerPixel)
954{
955 switch ( bitsPerPixel )
956 {
957 case 1:
958 // monochrome bitmap, 2 entries
959 return 2;
960
961 case 4:
962 return 16;
963
964 case 8:
965 return 256;
966
967 case 24:
968 // may be used with 24bit bitmaps, but we don't use it here - fall
969 // through
970
971 case 16:
972 case 32:
973 // bmiColors not used at all with these bitmaps
974 return 0;
975
976 default:
977 wxFAIL_MSG( wxT("unknown bitmap format") );
978 return 0;
979 }
980}
9e2896e5
VZ
981
982size_t wxConvertBitmapToDIB(BITMAPINFO *pbi, const wxBitmap& bitmap)
983{
8ee9d618
VZ
984 wxASSERT_MSG( bitmap.Ok(), wxT("invalid bmp can't be converted to DIB") );
985
9e2896e5
VZ
986 // shouldn't be selected into a DC or GetDIBits() would fail
987 wxASSERT_MSG( !bitmap.GetSelectedInto(),
988 wxT("can't copy bitmap selected into wxMemoryDC") );
989
8ee9d618
VZ
990 // prepare all the info we need
991 BITMAP bm;
9e2896e5 992 HBITMAP hbmp = (HBITMAP)bitmap.GetHBITMAP();
8ee9d618
VZ
993 if ( !GetObject(hbmp, sizeof(bm), &bm) )
994 {
995 wxLogLastError("GetObject(bitmap)");
996
997 return 0;
998 }
999
1000 // calculate the number of bits per pixel and the number of items in
1001 // bmiColors array (whose meaning depends on the bitmap format)
1002 WORD biBits = bm.bmPlanes * bm.bmBitsPixel;
1003 WORD biColors = wxGetNumOfBitmapColors(biBits);
9e2896e5 1004
8ee9d618 1005 BITMAPINFO bi2;
9e2896e5 1006
8ee9d618
VZ
1007 bool wantSizeOnly = pbi == NULL;
1008 if ( wantSizeOnly )
1009 pbi = &bi2;
1010
1011 // just for convenience
1012 BITMAPINFOHEADER& bi = pbi->bmiHeader;
1013
1014 bi.biSize = sizeof(BITMAPINFOHEADER);
1015 bi.biWidth = bm.bmWidth;
1016 bi.biHeight = bm.bmHeight;
1017 bi.biPlanes = 1;
1018 bi.biBitCount = biBits;
1019 bi.biCompression = BI_RGB;
1020 bi.biSizeImage = 0;
1021 bi.biXPelsPerMeter = 0;
1022 bi.biYPelsPerMeter = 0;
1023 bi.biClrUsed = 0;
1024 bi.biClrImportant = 0;
1025
1026 // memory we need for BITMAPINFO only
1027 DWORD dwLen = bi.biSize + biColors * sizeof(RGBQUAD);
1028
1029 // first get the image size
9e2896e5 1030 ScreenHDC hdc;
8ee9d618 1031 if ( !GetDIBits(hdc, hbmp, 0, bi.biHeight, NULL, pbi, DIB_RGB_COLORS) )
9e2896e5
VZ
1032 {
1033 wxLogLastError("GetDIBits(NULL)");
1034
1035 return 0;
1036 }
1037
8ee9d618 1038 if ( wantSizeOnly )
9e2896e5 1039 {
8ee9d618
VZ
1040 // size of the header + size of the image
1041 return dwLen + bi.biSizeImage;
9e2896e5
VZ
1042 }
1043
1044 // and now copy the bits
8ee9d618
VZ
1045 void *image = (char *)pbi + dwLen;
1046 if ( !GetDIBits(hdc, hbmp, 0, bi.biHeight, image, pbi, DIB_RGB_COLORS) )
9e2896e5
VZ
1047 {
1048 wxLogLastError("GetDIBits");
1049
1050 return 0;
1051 }
1052
8ee9d618 1053 return dwLen + bi.biSizeImage;
9e2896e5
VZ
1054}
1055
9e2896e5
VZ
1056wxBitmap wxConvertDIBToBitmap(const BITMAPINFO *pbmi)
1057{
1058 // here we get BITMAPINFO struct followed by the actual bitmap bits and
1059 // BITMAPINFO starts with BITMAPINFOHEADER followed by colour info
1060 const BITMAPINFOHEADER *pbmih = &pbmi->bmiHeader;
1061
8ee9d618
VZ
1062 // offset of image from the beginning of the header
1063 DWORD ofs = wxGetNumOfBitmapColors(pbmih->biBitCount) * sizeof(RGBQUAD);
1064 void *image = (char *)pbmih + sizeof(BITMAPINFOHEADER) + ofs;
1065
9e2896e5
VZ
1066 ScreenHDC hdc;
1067 HBITMAP hbmp = CreateDIBitmap(hdc, pbmih, CBM_INIT,
8ee9d618 1068 image, pbmi, DIB_RGB_COLORS);
9e2896e5
VZ
1069 if ( !hbmp )
1070 {
1071 wxLogLastError("CreateDIBitmap");
1072 }
1073
1074 wxBitmap bitmap(pbmih->biWidth, pbmih->biHeight, pbmih->biBitCount);
1075 bitmap.SetHBITMAP((WXHBITMAP)hbmp);
1076
1077 return bitmap;
1078}
1079
8e193f38
VZ
1080#ifdef __WXDEBUG__
1081
d59ceba5
VZ
1082static const wxChar *GetTymedName(DWORD tymed)
1083{
1084 static wxChar s_szBuf[128];
1085 switch ( tymed ) {
1086 case TYMED_HGLOBAL: return wxT("TYMED_HGLOBAL");
1087 case TYMED_FILE: return wxT("TYMED_FILE");
1088 case TYMED_ISTREAM: return wxT("TYMED_ISTREAM");
1089 case TYMED_ISTORAGE: return wxT("TYMED_ISTORAGE");
1090 case TYMED_GDI: return wxT("TYMED_GDI");
1091 case TYMED_MFPICT: return wxT("TYMED_MFPICT");
1092 case TYMED_ENHMF: return wxT("TYMED_ENHMF");
1093 default:
1094 wxSprintf(s_szBuf, wxT("type of media format %d (unknown)"), tymed);
1095 return s_szBuf;
1096 }
269a5a34 1097}
5260b1c5 1098
8e193f38 1099#endif // Debug
2845ddc2 1100
d59ceba5 1101#endif // not using OLE at all
5260b1c5 1102