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