]> git.saurik.com Git - wxWidgets.git/blob - src/msw/ole/dataobj.cpp
MSW compilation fixes (untested)
[wxWidgets.git] / src / msw / ole / dataobj.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: msw/ole/dataobj.cpp
3 // Purpose: implementation of wx[I]DataObject class
4 // Author: Vadim Zeitlin
5 // Modified by:
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
30 #ifndef WX_PRECOMP
31 #include "wx/intl.h"
32 #endif
33 #include "wx/defs.h"
34
35 #if defined(__WIN32__) && !defined(__GNUWIN32__) || defined(wxUSE_NORLANDER_HEADERS)
36
37 #include "wx/log.h"
38 #include "wx/dataobj.h"
39
40 #include <windows.h>
41 #ifdef wxUSE_NORLANDER_HEADERS
42 #include <ole2.h>
43 #endif
44 #include <oleauto.h>
45
46 #ifndef __WIN32__
47 #include <ole2.h>
48 #include <olestd.h>
49 #endif
50
51 #include "wx/msw/ole/oleutils.h"
52
53 // ----------------------------------------------------------------------------
54 // functions
55 // ----------------------------------------------------------------------------
56
57 #ifdef __WXDEBUG__
58 static const wxChar *GetTymedName(DWORD tymed);
59 #else // !Debug
60 #define GetTymedName(tymed) ""
61 #endif // Debug/!Debug
62
63 // to be moved into wx/msw/bitmap.h
64 extern size_t wxConvertBitmapToDIB(BITMAPINFO *pbi, const wxBitmap& bitmap);
65 extern wxBitmap wxConvertDIBToBitmap(const BITMAPINFO *bmi);
66
67 // ----------------------------------------------------------------------------
68 // wxIEnumFORMATETC interface implementation
69 // ----------------------------------------------------------------------------
70
71 class wxIEnumFORMATETC : public IEnumFORMATETC
72 {
73 public:
74 wxIEnumFORMATETC(const wxDataFormat* formats, ULONG nCount);
75 ~wxIEnumFORMATETC() { delete [] m_formats; }
76
77 DECLARE_IUNKNOWN_METHODS;
78
79 // IEnumFORMATETC
80 STDMETHODIMP Next(ULONG celt, FORMATETC *rgelt, ULONG *pceltFetched);
81 STDMETHODIMP Skip(ULONG celt);
82 STDMETHODIMP Reset();
83 STDMETHODIMP Clone(IEnumFORMATETC **ppenum);
84
85 private:
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
89 };
90
91 // ----------------------------------------------------------------------------
92 // wxIDataObject implementation of IDataObject interface
93 // ----------------------------------------------------------------------------
94
95 class wxIDataObject : public IDataObject
96 {
97 public:
98 wxIDataObject(wxDataObject *pDataObject);
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; }
105
106 DECLARE_IUNKNOWN_METHODS;
107
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);
118
119 private:
120 wxDataObject *m_pDataObject; // pointer to C++ class we belong to
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
130 class ScreenHDC
131 {
132 public:
133 ScreenHDC() { m_hdc = GetDC(NULL); }
134 ~ScreenHDC() { ReleaseDC(NULL, m_hdc); }
135 operator HDC() const { return m_hdc; }
136
137 private:
138 HDC m_hdc;
139 };
140
141 // ============================================================================
142 // implementation
143 // ============================================================================
144
145 // ----------------------------------------------------------------------------
146 // wxDataFormat
147 // ----------------------------------------------------------------------------
148
149 void 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
158 wxString wxDataFormat::GetId() const
159 {
160 static const int max = 256;
161
162 wxString s;
163
164 wxCHECK_MSG( !IsStandard(), s,
165 wxT("name of predefined format cannot be retrieved") );
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
178 // ----------------------------------------------------------------------------
179 // wxIEnumFORMATETC
180 // ----------------------------------------------------------------------------
181
182 BEGIN_IID_TABLE(wxIEnumFORMATETC)
183 ADD_IID(Unknown)
184 ADD_IID(EnumFORMATETC)
185 END_IID_TABLE;
186
187 IMPLEMENT_IUNKNOWN_METHODS(wxIEnumFORMATETC)
188
189 wxIEnumFORMATETC::wxIEnumFORMATETC(const wxDataFormat *formats, ULONG nCount)
190 {
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 }
198 }
199
200 STDMETHODIMP wxIEnumFORMATETC::Next(ULONG celt,
201 FORMATETC *rgelt,
202 ULONG *pceltFetched)
203 {
204 wxLogTrace(wxTRACE_OleCalls, wxT("wxIEnumFORMATETC::Next"));
205
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 }
211
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;
220
221 return S_OK;
222 }
223 else {
224 // bad index
225 return S_FALSE;
226 }
227 }
228
229 STDMETHODIMP wxIEnumFORMATETC::Skip(ULONG celt)
230 {
231 wxLogTrace(wxTRACE_OleCalls, wxT("wxIEnumFORMATETC::Skip"));
232
233 m_nCurrent += celt;
234 if ( m_nCurrent < m_nCount )
235 return S_OK;
236
237 // no, can't skip this many elements
238 m_nCurrent -= celt;
239
240 return S_FALSE;
241 }
242
243 STDMETHODIMP wxIEnumFORMATETC::Reset()
244 {
245 wxLogTrace(wxTRACE_OleCalls, wxT("wxIEnumFORMATETC::Reset"));
246
247 m_nCurrent = 0;
248
249 return S_OK;
250 }
251
252 STDMETHODIMP wxIEnumFORMATETC::Clone(IEnumFORMATETC **ppenum)
253 {
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;
265
266 return S_OK;
267 }
268
269 // ----------------------------------------------------------------------------
270 // wxIDataObject
271 // ----------------------------------------------------------------------------
272
273 BEGIN_IID_TABLE(wxIDataObject)
274 ADD_IID(Unknown)
275 ADD_IID(DataObject)
276 END_IID_TABLE;
277
278 IMPLEMENT_IUNKNOWN_METHODS(wxIDataObject)
279
280 wxIDataObject::wxIDataObject(wxDataObject *pDataObject)
281 {
282 m_cRef = 0;
283 m_pDataObject = pDataObject;
284 m_mustDelete = FALSE;
285 }
286
287 wxIDataObject::~wxIDataObject()
288 {
289 if ( m_mustDelete )
290 {
291 delete m_pDataObject;
292 }
293 }
294
295 // get data functions
296 STDMETHODIMP wxIDataObject::GetData(FORMATETC *pformatetcIn, STGMEDIUM *pmedium)
297 {
298 wxLogTrace(wxTRACE_OleCalls, wxT("wxIDataObject::GetData"));
299
300 // is data is in our format?
301 HRESULT hr = QueryGetData(pformatetcIn);
302 if ( FAILED(hr) )
303 return hr;
304
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;
308
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
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
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 }
345
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 }
358
359 return S_OK;
360 }
361
362 STDMETHODIMP wxIDataObject::GetDataHere(FORMATETC *pformatetc,
363 STGMEDIUM *pmedium)
364 {
365 wxLogTrace(wxTRACE_OleCalls, wxT("wxIDataObject::GetDataHere"));
366
367 // put data in caller provided medium
368 switch ( pmedium->tymed )
369 {
370 case TYMED_GDI:
371 if ( !m_pDataObject->GetDataHere(wxDF_BITMAP, &pmedium->hBitmap) )
372 return E_UNEXPECTED;
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
384 HGLOBAL hGlobal = pmedium->hGlobal;
385 void *pBuf = GlobalLock(hGlobal);
386 if ( pBuf == NULL ) {
387 wxLogLastError(wxT("GlobalLock"));
388 return E_OUTOFMEMORY;
389 }
390
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;
399 if ( !m_pDataObject->GetDataHere(format, pBuf) )
400 return E_UNEXPECTED;
401
402 GlobalUnlock(hGlobal);
403 }
404 break;
405
406 default:
407 return DV_E_TYMED;
408 }
409
410 return S_OK;
411 }
412
413 // set data functions
414 STDMETHODIMP wxIDataObject::SetData(FORMATETC *pformatetc,
415 STGMEDIUM *pmedium,
416 BOOL fRelease)
417 {
418 wxLogTrace(wxTRACE_OleCalls, wxT("wxIDataObject::SetData"));
419
420 switch ( pmedium->tymed )
421 {
422 case TYMED_GDI:
423 m_pDataObject->SetData(wxDF_BITMAP, 0, &pmedium->hBitmap);
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
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);
477
478 GlobalUnlock(pmedium->hGlobal);
479
480 if ( !ok ) {
481 return E_UNEXPECTED;
482 }
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;
502 }
503
504 // information functions
505 STDMETHODIMP wxIDataObject::QueryGetData(FORMATETC *pformatetc)
506 {
507 // do we accept data in this format?
508 if ( pformatetc == NULL ) {
509 wxLogTrace(wxTRACE_OleCalls,
510 wxT("wxIDataObject::QueryGetData: invalid ptr."));
511
512 return E_INVALIDARG;
513 }
514
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);
520
521 return DV_E_LINDEX;
522 }
523
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
534 wxDataFormat format = pformatetc->cfFormat;
535 if ( m_pDataObject->IsSupportedFormat(format) ) {
536 wxLogTrace(wxTRACE_OleCalls, wxT("wxIDataObject::QueryGetData: %s ok"),
537 wxGetFormatName(format));
538 }
539 else {
540 wxLogTrace(wxTRACE_OleCalls,
541 wxT("wxIDataObject::QueryGetData: %s unsupported"),
542 wxGetFormatName(format));
543
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
552 wxLogTrace(wxTRACE_OleCalls,
553 wxT("wxIDataObject::QueryGetData: %s != %s"),
554 GetTymedName(tymed),
555 GetTymedName(format == wxDF_BITMAP ? TYMED_GDI
556 : TYMED_HGLOBAL));
557
558 return DV_E_TYMED;
559 }
560
561 return S_OK;
562 }
563
564 STDMETHODIMP wxIDataObject::GetCanonicalFormatEtc(FORMATETC *pFormatetcIn,
565 FORMATETC *pFormatetcOut)
566 {
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;
572
573 return DATA_S_SAMEFORMATETC;
574 }
575
576 STDMETHODIMP wxIDataObject::EnumFormatEtc(DWORD dwDir,
577 IEnumFORMATETC **ppenumFormatEtc)
578 {
579 wxLogTrace(wxTRACE_OleCalls, wxT("wxIDataObject::EnumFormatEtc"));
580
581 wxDataObject::Direction dir = dwDir == DATADIR_GET ? wxDataObject::Get
582 : wxDataObject::Set;
583
584 size_t nFormatCount = m_pDataObject->GetFormatCount(dir);
585 wxDataFormat format, *formats;
586 formats = nFormatCount == 1 ? &format : new wxDataFormat[nFormatCount];
587 m_pDataObject->GetAllFormats(formats, dir);
588
589 wxIEnumFORMATETC *pEnum = new wxIEnumFORMATETC(formats, nFormatCount);
590 pEnum->AddRef();
591 *ppenumFormatEtc = pEnum;
592
593 if ( formats != &format ) {
594 delete [] formats;
595 }
596
597 return S_OK;
598 }
599
600 // ----------------------------------------------------------------------------
601 // advise sink functions (not implemented)
602 // ----------------------------------------------------------------------------
603
604 STDMETHODIMP wxIDataObject::DAdvise(FORMATETC *pformatetc,
605 DWORD advf,
606 IAdviseSink *pAdvSink,
607 DWORD *pdwConnection)
608 {
609 return OLE_E_ADVISENOTSUPPORTED;
610 }
611
612 STDMETHODIMP wxIDataObject::DUnadvise(DWORD dwConnection)
613 {
614 return OLE_E_ADVISENOTSUPPORTED;
615 }
616
617 STDMETHODIMP wxIDataObject::EnumDAdvise(IEnumSTATDATA **ppenumAdvise)
618 {
619 return OLE_E_ADVISENOTSUPPORTED;
620 }
621
622 // ----------------------------------------------------------------------------
623 // wxDataObject
624 // ----------------------------------------------------------------------------
625
626 wxDataObject::wxDataObject()
627 {
628 m_pIDataObject = new wxIDataObject(this);
629 m_pIDataObject->AddRef();
630 }
631
632 wxDataObject::~wxDataObject()
633 {
634 ReleaseInterface(m_pIDataObject);
635 }
636
637 void wxDataObject::SetAutoDelete()
638 {
639 ((wxIDataObject *)m_pIDataObject)->SetDeleteFlag();
640 m_pIDataObject->Release();
641
642 // so that the dtor doesnt' crash
643 m_pIDataObject = NULL;
644 }
645
646 bool wxDataObject::IsSupportedFormat(const wxDataFormat& format) const
647 {
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
669 #ifdef __WXDEBUG__
670
671 const char *wxDataObject::GetFormatName(wxDataFormat format)
672 {
673 // case 'xxx' is not a valid value for switch of enum 'wxDataFormat'
674 #ifdef __VISUALC__
675 #pragma warning(disable:4063)
676 #endif // VC++
677
678 static char s_szBuf[128];
679 switch ( format ) {
680 case CF_TEXT: return "CF_TEXT";
681 case CF_BITMAP: return "CF_BITMAP";
682 case CF_METAFILEPICT: return "CF_METAFILEPICT";
683 case CF_SYLK: return "CF_SYLK";
684 case CF_DIF: return "CF_DIF";
685 case CF_TIFF: return "CF_TIFF";
686 case CF_OEMTEXT: return "CF_OEMTEXT";
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";
692 case CF_UNICODETEXT: return "CF_UNICODETEXT";
693 case CF_ENHMETAFILE: return "CF_ENHMETAFILE";
694 case CF_HDROP: return "CF_HDROP";
695 case CF_LOCALE: return "CF_LOCALE";
696 default:
697 sprintf(s_szBuf, "clipboard format 0x%x (unknown)", format);
698 return s_szBuf;
699 }
700
701 #ifdef __VISUALC__
702 #pragma warning(default:4063)
703 #endif // VC++
704 }
705
706 #endif // Debug
707
708 // ----------------------------------------------------------------------------
709 // wxBitmapDataObject supports CF_DIB format
710 // ----------------------------------------------------------------------------
711
712 size_t wxBitmapDataObject::GetDataSize() const
713 {
714 return wxConvertBitmapToDIB(NULL, GetBitmap());
715 }
716
717 bool wxBitmapDataObject::GetDataHere(void *buf) const
718 {
719 return wxConvertBitmapToDIB((BITMAPINFO *)buf, GetBitmap()) != 0;
720 }
721
722 bool wxBitmapDataObject::SetData(size_t len, const void *buf)
723 {
724 wxBitmap bitmap(wxConvertDIBToBitmap((const BITMAPINFO *)buf));
725
726 if ( !bitmap.Ok() ) {
727 wxFAIL_MSG(wxT("pasting/dropping invalid bitmap"));
728
729 return FALSE;
730 }
731
732 SetBitmap(bitmap);
733
734 return TRUE;
735 }
736
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
746 size_t wxBitmapDataObject2::GetDataSize() const
747 {
748 return 0;
749 }
750
751 bool wxBitmapDataObject2::GetDataHere(void *pBuf) const
752 {
753 // we put a bitmap handle into pBuf
754 *(WXHBITMAP *)pBuf = GetBitmap().GetHBITMAP();
755
756 return TRUE;
757 }
758
759 bool wxBitmapDataObject2::SetData(size_t len, const void *pBuf)
760 {
761 HBITMAP hbmp = *(HBITMAP *)pBuf;
762
763 BITMAP bmp;
764 if ( !GetObject(hbmp, sizeof(BITMAP), &bmp) )
765 {
766 wxLogLastError("GetObject(HBITMAP)");
767 }
768
769 wxBitmap bitmap(bmp.bmWidth, bmp.bmHeight, bmp.bmPlanes);
770 bitmap.SetHBITMAP((WXHBITMAP)hbmp);
771
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;
781 }
782
783 #if 0
784
785 size_t wxBitmapDataObject::GetDataSize(const wxDataFormat& format) const
786 {
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 }
813 }
814
815 bool wxBitmapDataObject::GetDataHere(const wxDataFormat& format,
816 void *pBuf) const
817 {
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
857 bool wxBitmapDataObject::SetData(const wxDataFormat& format,
858 size_t size, const void *pBuf)
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;
900 }
901
902 #endif // 0
903
904 // ----------------------------------------------------------------------------
905 // wxFileDataObject
906 // ----------------------------------------------------------------------------
907
908 bool 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
943 // ----------------------------------------------------------------------------
944 // private functions
945 // ----------------------------------------------------------------------------
946
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
954 size_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
996 wxBitmap 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
1016 #ifdef __WXDEBUG__
1017
1018 static 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 }
1033 }
1034
1035 #endif // Debug
1036
1037 #endif // not using OLE at all
1038