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