]> git.saurik.com Git - wxWidgets.git/blob - src/msw/ole/dataobj.cpp
14d98afe93c35f7f6d3271ff5bb79447afb06ef4
[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->hGlobal = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE,
302 sizeof(METAFILEPICT));
303 if ( !pmedium->hGlobal ) {
304 wxLogLastError("GlobalAlloc");
305 return E_OUTOFMEMORY;
306 }
307 pmedium->tymed = TYMED_MFPICT;
308 break;
309
310 default:
311 // alloc memory
312 size_t size = m_pDataObject->GetDataSize(format);
313 if ( !size ) {
314 // it probably means that the method is just not implemented
315 wxLogDebug(wxT("Invalid data size - can't be 0"));
316
317 return DV_E_FORMATETC;
318 }
319
320 if ( !format.IsStandard() ) {
321 // for custom formats, put the size with the data - alloc the
322 // space for it
323 size += sizeof(size_t);
324 }
325
326 HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, size);
327 if ( hGlobal == NULL ) {
328 wxLogLastError("GlobalAlloc");
329 return E_OUTOFMEMORY;
330 }
331
332 // copy data
333 pmedium->tymed = TYMED_HGLOBAL;
334 pmedium->hGlobal = hGlobal;
335 }
336
337 pmedium->pUnkForRelease = NULL;
338
339 // do copy the data
340 hr = GetDataHere(pformatetcIn, pmedium);
341 if ( FAILED(hr) ) {
342 // free resources we allocated
343 if ( pmedium->tymed & (TYMED_HGLOBAL | TYMED_MFPICT) ) {
344 GlobalFree(pmedium->hGlobal);
345 }
346
347 return hr;
348 }
349
350 return S_OK;
351 }
352
353 STDMETHODIMP wxIDataObject::GetDataHere(FORMATETC *pformatetc,
354 STGMEDIUM *pmedium)
355 {
356 wxLogTrace(wxTRACE_OleCalls, wxT("wxIDataObject::GetDataHere"));
357
358 // put data in caller provided medium
359 switch ( pmedium->tymed )
360 {
361 case TYMED_GDI:
362 if ( !m_pDataObject->GetDataHere(wxDF_BITMAP, &pmedium->hBitmap) )
363 return E_UNEXPECTED;
364 break;
365
366 case TYMED_MFPICT:
367 case TYMED_HGLOBAL:
368 {
369 // copy data
370 HGLOBAL hGlobal = pmedium->hGlobal;
371 void *pBuf = GlobalLock(hGlobal);
372 if ( pBuf == NULL ) {
373 wxLogLastError(wxT("GlobalLock"));
374 return E_OUTOFMEMORY;
375 }
376
377 if ( !wxDataFormat(pformatetc->cfFormat).IsStandard() ) {
378 // for custom formats, put the size with the data
379 size_t *p = (size_t *)pBuf;
380 *p++ = GlobalSize(hGlobal);
381 pBuf = p;
382 }
383
384 wxDataFormat format = pformatetc->cfFormat;
385 if ( !m_pDataObject->GetDataHere(format, pBuf) )
386 return E_UNEXPECTED;
387
388 GlobalUnlock(hGlobal);
389 }
390 break;
391
392 default:
393 return DV_E_TYMED;
394 }
395
396 return S_OK;
397 }
398
399 // set data functions
400 STDMETHODIMP wxIDataObject::SetData(FORMATETC *pformatetc,
401 STGMEDIUM *pmedium,
402 BOOL fRelease)
403 {
404 wxLogTrace(wxTRACE_OleCalls, wxT("wxIDataObject::SetData"));
405
406 switch ( pmedium->tymed )
407 {
408 case TYMED_GDI:
409 m_pDataObject->SetData(wxDF_BITMAP, 0, &pmedium->hBitmap);
410 break;
411
412 case TYMED_MFPICT:
413 case TYMED_HGLOBAL:
414 {
415 wxDataFormat format = pformatetc->cfFormat;
416
417 // this is quite weird, but for file drag and drop, explorer
418 // calls our SetData() with the formats we do *not* support!
419 //
420 // as we can't fix this bug in explorer (it's a bug because it
421 // should only use formats returned by EnumFormatEtc), do the
422 // check here
423 if ( !m_pDataObject->IsSupportedFormat(format) ) {
424 // go away!
425 return DV_E_FORMATETC;
426 }
427
428 // copy data
429 void *pBuf = GlobalLock(pmedium->hGlobal);
430 if ( pBuf == NULL ) {
431 wxLogLastError("GlobalLock");
432
433 return E_OUTOFMEMORY;
434 }
435
436 // we've got a problem with SetData() here because the base
437 // class version requires the size parameter which we don't
438 // have anywhere in OLE data transfer - so we need to
439 // synthetise it for known formats and we suppose that all data
440 // in custom formats starts with a DWORD containing the size
441 size_t size;
442 switch ( format )
443 {
444 case CF_TEXT:
445 case CF_OEMTEXT:
446 size = strlen((const char *)pBuf);
447 break;
448 #ifndef __WATCOMC__
449 case CF_UNICODETEXT:
450 size = wcslen((const wchar_t *)pBuf);
451 break;
452 #endif
453 case CF_BITMAP:
454 case CF_HDROP:
455 // these formats don't use size at all, anyhow (but
456 // pass data by handle, which is always a single DWORD)
457 size = 0;
458 break;
459
460 case CF_DIB:
461 // the handler will calculate size itself (it's too
462 // complicated to do it here)
463 size = 0;
464 break;
465
466 case CF_METAFILEPICT:
467 size = sizeof(METAFILEPICT);
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 any
495 // data we pass by handle because we have copied it elsewhere
496 switch ( pmedium->tymed )
497 {
498 case TYMED_GDI:
499 pmedium->hBitmap = 0;
500 break;
501
502 case TYMED_MFPICT:
503 pmedium->hMetaFilePict = 0;
504 break;
505 }
506
507 ReleaseStgMedium(pmedium);
508 }
509
510 return S_OK;
511 }
512
513 // information functions
514 STDMETHODIMP wxIDataObject::QueryGetData(FORMATETC *pformatetc)
515 {
516 // do we accept data in this format?
517 if ( pformatetc == NULL ) {
518 wxLogTrace(wxTRACE_OleCalls,
519 wxT("wxIDataObject::QueryGetData: invalid ptr."));
520
521 return E_INVALIDARG;
522 }
523
524 // the only one allowed by current COM implementation
525 if ( pformatetc->lindex != -1 ) {
526 wxLogTrace(wxTRACE_OleCalls,
527 wxT("wxIDataObject::QueryGetData: bad lindex %d"),
528 pformatetc->lindex);
529
530 return DV_E_LINDEX;
531 }
532
533 // we don't support anything other (THUMBNAIL, ICON, DOCPRINT...)
534 if ( pformatetc->dwAspect != DVASPECT_CONTENT ) {
535 wxLogTrace(wxTRACE_OleCalls,
536 wxT("wxIDataObject::QueryGetData: bad dwAspect %d"),
537 pformatetc->dwAspect);
538
539 return DV_E_DVASPECT;
540 }
541
542 // and now check the type of data requested
543 wxDataFormat format = pformatetc->cfFormat;
544 if ( m_pDataObject->IsSupportedFormat(format) ) {
545 wxLogTrace(wxTRACE_OleCalls, wxT("wxIDataObject::QueryGetData: %s ok"),
546 wxGetFormatName(format));
547 }
548 else {
549 wxLogTrace(wxTRACE_OleCalls,
550 wxT("wxIDataObject::QueryGetData: %s unsupported"),
551 wxGetFormatName(format));
552
553 return DV_E_FORMATETC;
554 }
555
556 // we only transfer data by global memory, except for some particular cases
557 DWORD tymed = pformatetc->tymed;
558 if ( (format == wxDF_BITMAP && !(tymed & TYMED_GDI)) &&
559 !(tymed & TYMED_HGLOBAL) ) {
560 // it's not what we're waiting for
561 wxLogTrace(wxTRACE_OleCalls,
562 wxT("wxIDataObject::QueryGetData: %s != %s"),
563 GetTymedName(tymed),
564 GetTymedName(format == wxDF_BITMAP ? TYMED_GDI
565 : TYMED_HGLOBAL));
566
567 return DV_E_TYMED;
568 }
569
570 return S_OK;
571 }
572
573 STDMETHODIMP wxIDataObject::GetCanonicalFormatEtc(FORMATETC *pFormatetcIn,
574 FORMATETC *pFormatetcOut)
575 {
576 wxLogTrace(wxTRACE_OleCalls, wxT("wxIDataObject::GetCanonicalFormatEtc"));
577
578 // TODO we might want something better than this trivial implementation here
579 if ( pFormatetcOut != NULL )
580 pFormatetcOut->ptd = NULL;
581
582 return DATA_S_SAMEFORMATETC;
583 }
584
585 STDMETHODIMP wxIDataObject::EnumFormatEtc(DWORD dwDir,
586 IEnumFORMATETC **ppenumFormatEtc)
587 {
588 wxLogTrace(wxTRACE_OleCalls, wxT("wxIDataObject::EnumFormatEtc"));
589
590 wxDataObject::Direction dir = dwDir == DATADIR_GET ? wxDataObject::Get
591 : wxDataObject::Set;
592
593 size_t nFormatCount = m_pDataObject->GetFormatCount(dir);
594 wxDataFormat format, *formats;
595 formats = nFormatCount == 1 ? &format : new wxDataFormat[nFormatCount];
596 m_pDataObject->GetAllFormats(formats, dir);
597
598 wxIEnumFORMATETC *pEnum = new wxIEnumFORMATETC(formats, nFormatCount);
599 pEnum->AddRef();
600 *ppenumFormatEtc = pEnum;
601
602 if ( formats != &format ) {
603 delete [] formats;
604 }
605
606 return S_OK;
607 }
608
609 // ----------------------------------------------------------------------------
610 // advise sink functions (not implemented)
611 // ----------------------------------------------------------------------------
612
613 STDMETHODIMP wxIDataObject::DAdvise(FORMATETC *pformatetc,
614 DWORD advf,
615 IAdviseSink *pAdvSink,
616 DWORD *pdwConnection)
617 {
618 return OLE_E_ADVISENOTSUPPORTED;
619 }
620
621 STDMETHODIMP wxIDataObject::DUnadvise(DWORD dwConnection)
622 {
623 return OLE_E_ADVISENOTSUPPORTED;
624 }
625
626 STDMETHODIMP wxIDataObject::EnumDAdvise(IEnumSTATDATA **ppenumAdvise)
627 {
628 return OLE_E_ADVISENOTSUPPORTED;
629 }
630
631 // ----------------------------------------------------------------------------
632 // wxDataObject
633 // ----------------------------------------------------------------------------
634
635 wxDataObject::wxDataObject()
636 {
637 m_pIDataObject = new wxIDataObject(this);
638 m_pIDataObject->AddRef();
639 }
640
641 wxDataObject::~wxDataObject()
642 {
643 ReleaseInterface(m_pIDataObject);
644 }
645
646 void wxDataObject::SetAutoDelete()
647 {
648 ((wxIDataObject *)m_pIDataObject)->SetDeleteFlag();
649 m_pIDataObject->Release();
650
651 // so that the dtor doesnt' crash
652 m_pIDataObject = NULL;
653 }
654
655 bool wxDataObject::IsSupportedFormat(const wxDataFormat& format) const
656 {
657 size_t nFormatCount = GetFormatCount();
658 if ( nFormatCount == 1 ) {
659 return format == GetPreferredFormat();
660 }
661 else {
662 wxDataFormat *formats = new wxDataFormat[nFormatCount];
663 GetAllFormats(formats);
664
665 size_t n;
666 for ( n = 0; n < nFormatCount; n++ ) {
667 if ( formats[n] == format )
668 break;
669 }
670
671 delete [] formats;
672
673 // found?
674 return n < nFormatCount;
675 }
676 }
677
678 #ifdef __WXDEBUG__
679
680 const char *wxDataObject::GetFormatName(wxDataFormat format)
681 {
682 // case 'xxx' is not a valid value for switch of enum 'wxDataFormat'
683 #ifdef __VISUALC__
684 #pragma warning(disable:4063)
685 #endif // VC++
686
687 static char s_szBuf[128];
688 switch ( format ) {
689 case CF_TEXT: return "CF_TEXT";
690 case CF_BITMAP: return "CF_BITMAP";
691 case CF_METAFILEPICT: return "CF_METAFILEPICT";
692 case CF_SYLK: return "CF_SYLK";
693 case CF_DIF: return "CF_DIF";
694 case CF_TIFF: return "CF_TIFF";
695 case CF_OEMTEXT: return "CF_OEMTEXT";
696 case CF_DIB: return "CF_DIB";
697 case CF_PALETTE: return "CF_PALETTE";
698 case CF_PENDATA: return "CF_PENDATA";
699 case CF_RIFF: return "CF_RIFF";
700 case CF_WAVE: return "CF_WAVE";
701 case CF_UNICODETEXT: return "CF_UNICODETEXT";
702 case CF_ENHMETAFILE: return "CF_ENHMETAFILE";
703 case CF_HDROP: return "CF_HDROP";
704 case CF_LOCALE: return "CF_LOCALE";
705 default:
706 sprintf(s_szBuf, "clipboard format 0x%x (unknown)", format);
707 return s_szBuf;
708 }
709
710 #ifdef __VISUALC__
711 #pragma warning(default:4063)
712 #endif // VC++
713 }
714
715 #endif // Debug
716
717 // ----------------------------------------------------------------------------
718 // wxBitmapDataObject supports CF_DIB format
719 // ----------------------------------------------------------------------------
720
721 size_t wxBitmapDataObject::GetDataSize() const
722 {
723 return wxConvertBitmapToDIB(NULL, GetBitmap());
724 }
725
726 bool wxBitmapDataObject::GetDataHere(void *buf) const
727 {
728 return wxConvertBitmapToDIB((LPBITMAPINFO)buf, GetBitmap()) != 0;
729 }
730
731 bool wxBitmapDataObject::SetData(size_t len, const void *buf)
732 {
733 wxBitmap bitmap(wxConvertDIBToBitmap((const LPBITMAPINFO)buf));
734
735 if ( !bitmap.Ok() ) {
736 wxFAIL_MSG(wxT("pasting/dropping invalid bitmap"));
737
738 return FALSE;
739 }
740
741 SetBitmap(bitmap);
742
743 return TRUE;
744 }
745
746 // ----------------------------------------------------------------------------
747 // wxBitmapDataObject2 supports CF_BITMAP format
748 // ----------------------------------------------------------------------------
749
750 // the bitmaps aren't passed by value as other types of data (i.e. by copying
751 // the data into a global memory chunk and passing it to the clipboard or
752 // another application or whatever), but by handle, so these generic functions
753 // don't make much sense to them.
754
755 size_t wxBitmapDataObject2::GetDataSize() const
756 {
757 return 0;
758 }
759
760 bool wxBitmapDataObject2::GetDataHere(void *pBuf) const
761 {
762 // we put a bitmap handle into pBuf
763 *(WXHBITMAP *)pBuf = GetBitmap().GetHBITMAP();
764
765 return TRUE;
766 }
767
768 bool wxBitmapDataObject2::SetData(size_t WXUNUSED(len), const void *pBuf)
769 {
770 HBITMAP hbmp = *(HBITMAP *)pBuf;
771
772 BITMAP bmp;
773 if ( !GetObject(hbmp, sizeof(BITMAP), &bmp) )
774 {
775 wxLogLastError("GetObject(HBITMAP)");
776 }
777
778 wxBitmap bitmap(bmp.bmWidth, bmp.bmHeight, bmp.bmPlanes);
779 bitmap.SetHBITMAP((WXHBITMAP)hbmp);
780
781 if ( !bitmap.Ok() ) {
782 wxFAIL_MSG(wxT("pasting/dropping invalid bitmap"));
783
784 return FALSE;
785 }
786
787 SetBitmap(bitmap);
788
789 return TRUE;
790 }
791
792 #if 0
793
794 size_t wxBitmapDataObject::GetDataSize(const wxDataFormat& format) const
795 {
796 if ( format.GetFormatId() == CF_DIB )
797 {
798 // create the DIB
799 ScreenHDC hdc;
800
801 // shouldn't be selected into a DC or GetDIBits() would fail
802 wxASSERT_MSG( !m_bitmap.GetSelectedInto(),
803 wxT("can't copy bitmap selected into wxMemoryDC") );
804
805 // first get the info
806 BITMAPINFO bi;
807 if ( !GetDIBits(hdc, (HBITMAP)m_bitmap.GetHBITMAP(), 0, 0,
808 NULL, &bi, DIB_RGB_COLORS) )
809 {
810 wxLogLastError("GetDIBits(NULL)");
811
812 return 0;
813 }
814
815 return sizeof(BITMAPINFO) + bi.bmiHeader.biSizeImage;
816 }
817 else // CF_BITMAP
818 {
819 // no data to copy - we don't pass HBITMAP via global memory
820 return 0;
821 }
822 }
823
824 bool wxBitmapDataObject::GetDataHere(const wxDataFormat& format,
825 void *pBuf) const
826 {
827 wxASSERT_MSG( m_bitmap.Ok(), wxT("copying invalid bitmap") );
828
829 HBITMAP hbmp = (HBITMAP)m_bitmap.GetHBITMAP();
830 if ( format.GetFormatId() == CF_DIB )
831 {
832 // create the DIB
833 ScreenHDC hdc;
834
835 // shouldn't be selected into a DC or GetDIBits() would fail
836 wxASSERT_MSG( !m_bitmap.GetSelectedInto(),
837 wxT("can't copy bitmap selected into wxMemoryDC") );
838
839 // first get the info
840 BITMAPINFO *pbi = (BITMAPINFO *)pBuf;
841 if ( !GetDIBits(hdc, hbmp, 0, 0, NULL, pbi, DIB_RGB_COLORS) )
842 {
843 wxLogLastError("GetDIBits(NULL)");
844
845 return 0;
846 }
847
848 // and now copy the bits
849 if ( !GetDIBits(hdc, hbmp, 0, pbi->bmiHeader.biHeight, pbi + 1,
850 pbi, DIB_RGB_COLORS) )
851 {
852 wxLogLastError("GetDIBits");
853
854 return FALSE;
855 }
856 }
857 else // CF_BITMAP
858 {
859 // we put a bitmap handle into pBuf
860 *(HBITMAP *)pBuf = hbmp;
861 }
862
863 return TRUE;
864 }
865
866 bool wxBitmapDataObject::SetData(const wxDataFormat& format,
867 size_t size, const void *pBuf)
868 {
869 HBITMAP hbmp;
870 if ( format.GetFormatId() == CF_DIB )
871 {
872 // here we get BITMAPINFO struct followed by the actual bitmap bits and
873 // BITMAPINFO starts with BITMAPINFOHEADER followed by colour info
874 ScreenHDC hdc;
875
876 BITMAPINFO *pbmi = (BITMAPINFO *)pBuf;
877 BITMAPINFOHEADER *pbmih = &pbmi->bmiHeader;
878 hbmp = CreateDIBitmap(hdc, pbmih, CBM_INIT,
879 pbmi + 1, pbmi, DIB_RGB_COLORS);
880 if ( !hbmp )
881 {
882 wxLogLastError("CreateDIBitmap");
883 }
884
885 m_bitmap.SetWidth(pbmih->biWidth);
886 m_bitmap.SetHeight(pbmih->biHeight);
887 }
888 else // CF_BITMAP
889 {
890 // it's easy with bitmaps: we pass them by handle
891 hbmp = *(HBITMAP *)pBuf;
892
893 BITMAP bmp;
894 if ( !GetObject(hbmp, sizeof(BITMAP), &bmp) )
895 {
896 wxLogLastError("GetObject(HBITMAP)");
897 }
898
899 m_bitmap.SetWidth(bmp.bmWidth);
900 m_bitmap.SetHeight(bmp.bmHeight);
901 m_bitmap.SetDepth(bmp.bmPlanes);
902 }
903
904 m_bitmap.SetHBITMAP((WXHBITMAP)hbmp);
905
906 wxASSERT_MSG( m_bitmap.Ok(), wxT("pasting invalid bitmap") );
907
908 return TRUE;
909 }
910
911 #endif // 0
912
913 // ----------------------------------------------------------------------------
914 // wxFileDataObject
915 // ----------------------------------------------------------------------------
916
917 bool wxFileDataObject::SetData(size_t WXUNUSED(size), const void *pData)
918 {
919 m_filenames.Empty();
920
921 // the documentation states that the first member of DROPFILES structure is
922 // a "DWORD offset of double NUL terminated file list". What they mean by
923 // this (I wonder if you see it immediately) is that the list starts at
924 // ((char *)&(pDropFiles.pFiles)) + pDropFiles.pFiles. We're also advised
925 // to use DragQueryFile to work with this structure, but not told where and
926 // how to get HDROP.
927 HDROP hdrop = (HDROP)pData; // NB: it works, but I'm not sure about it
928
929 // get number of files (magic value -1)
930 UINT nFiles = ::DragQueryFile(hdrop, (unsigned)-1, NULL, 0u);
931
932 wxCHECK_MSG ( nFiles != (UINT)-1, FALSE, wxT("wrong HDROP handle") );
933
934 // for each file get the length, allocate memory and then get the name
935 wxString str;
936 UINT len, n;
937 for ( n = 0; n < nFiles; n++ ) {
938 // +1 for terminating NUL
939 len = ::DragQueryFile(hdrop, n, NULL, 0) + 1;
940
941 UINT len2 = ::DragQueryFile(hdrop, n, str.GetWriteBuf(len), len);
942 str.UngetWriteBuf();
943 m_filenames.Add(str);
944
945 if ( len2 != len - 1 ) {
946 wxLogDebug(wxT("In wxFileDropTarget::OnDrop DragQueryFile returned"
947 " %d characters, %d expected."), len2, len - 1);
948 }
949 }
950
951 return TRUE;
952 }
953
954 void wxFileDataObject::AddFile(const wxString& file)
955 {
956 // just add file to filenames array
957 // all useful data (such as DROPFILES struct) will be
958 // created later as necessary
959 m_filenames.Add(file);
960 }
961
962 size_t wxFileDataObject::GetDataSize() const
963 {
964 // size returned will be the size of the DROPFILES structure,
965 // plus the list of filesnames (null byte separated), plus
966 // a double null at the end
967
968 // if no filenames in list, size is 0
969 if ( m_filenames.GetCount() == 0 )
970 return 0;
971
972 // inital size of DROPFILES struct + null byte
973 size_t sz = sizeof(DROPFILES) + 1;
974
975 size_t count = m_filenames.GetCount();
976 for ( size_t i = 0; i < count; i++ )
977 {
978 // add filename length plus null byte
979 sz += m_filenames[i].Len() + 1;
980 }
981
982 return sz;
983 }
984
985 bool wxFileDataObject::GetDataHere(void *pData) const
986 {
987 // pData points to an externally allocated memory block
988 // created using the size returned by GetDataSize()
989
990 // if pData is NULL, or there are no files, return
991 if ( !pData || m_filenames.GetCount() == 0 )
992 return FALSE;
993
994 // convert data pointer to a DROPFILES struct pointer
995 LPDROPFILES pDrop = (LPDROPFILES) pData;
996
997 // initialize DROPFILES struct
998 pDrop->pFiles = sizeof(DROPFILES);
999 pDrop->fNC = FALSE; // not non-client coords
1000 #if wxUSE_UNICODE
1001 pDrop->fWide = TRUE;
1002 #else // ANSI
1003 pDrop->fWide = FALSE;
1004 #endif // Unicode/Ansi
1005
1006 // set start of filenames list (null separated)
1007 wxChar *pbuf = (wxChar*) ((BYTE *)pDrop + sizeof(DROPFILES));
1008
1009 size_t count = m_filenames.GetCount();
1010 for (size_t i = 0; i < count; i++ )
1011 {
1012 // copy filename to pbuf and add null terminator
1013 size_t len = m_filenames[i].Len();
1014 memcpy(pbuf, m_filenames[i], len);
1015 pbuf += len;
1016 *pbuf++ = wxT('\0');
1017 }
1018
1019 *pbuf = wxT('\0'); // add final null terminator
1020
1021 return TRUE;
1022 }
1023
1024 // ----------------------------------------------------------------------------
1025 // private functions
1026 // ----------------------------------------------------------------------------
1027
1028 static size_t wxGetNumOfBitmapColors(size_t bitsPerPixel)
1029 {
1030 switch ( bitsPerPixel )
1031 {
1032 case 1:
1033 // monochrome bitmap, 2 entries
1034 return 2;
1035
1036 case 4:
1037 return 16;
1038
1039 case 8:
1040 return 256;
1041
1042 case 24:
1043 // may be used with 24bit bitmaps, but we don't use it here - fall
1044 // through
1045
1046 case 16:
1047 case 32:
1048 // bmiColors not used at all with these bitmaps
1049 return 0;
1050
1051 default:
1052 wxFAIL_MSG( wxT("unknown bitmap format") );
1053 return 0;
1054 }
1055 }
1056
1057 size_t wxConvertBitmapToDIB(LPBITMAPINFO pbi, const wxBitmap& bitmap)
1058 {
1059 wxASSERT_MSG( bitmap.Ok(), wxT("invalid bmp can't be converted to DIB") );
1060
1061 // shouldn't be selected into a DC or GetDIBits() would fail
1062 wxASSERT_MSG( !bitmap.GetSelectedInto(),
1063 wxT("can't copy bitmap selected into wxMemoryDC") );
1064
1065 // prepare all the info we need
1066 BITMAP bm;
1067 HBITMAP hbmp = (HBITMAP)bitmap.GetHBITMAP();
1068 if ( !GetObject(hbmp, sizeof(bm), &bm) )
1069 {
1070 wxLogLastError("GetObject(bitmap)");
1071
1072 return 0;
1073 }
1074
1075 // calculate the number of bits per pixel and the number of items in
1076 // bmiColors array (whose meaning depends on the bitmap format)
1077 WORD biBits = bm.bmPlanes * bm.bmBitsPixel;
1078 WORD biColors = wxGetNumOfBitmapColors(biBits);
1079
1080 BITMAPINFO bi2;
1081
1082 bool wantSizeOnly = pbi == NULL;
1083 if ( wantSizeOnly )
1084 pbi = &bi2;
1085
1086 // just for convenience
1087 BITMAPINFOHEADER& bi = pbi->bmiHeader;
1088
1089 bi.biSize = sizeof(BITMAPINFOHEADER);
1090 bi.biWidth = bm.bmWidth;
1091 bi.biHeight = bm.bmHeight;
1092 bi.biPlanes = 1;
1093 bi.biBitCount = biBits;
1094 bi.biCompression = BI_RGB;
1095 bi.biSizeImage = 0;
1096 bi.biXPelsPerMeter = 0;
1097 bi.biYPelsPerMeter = 0;
1098 bi.biClrUsed = 0;
1099 bi.biClrImportant = 0;
1100
1101 // memory we need for BITMAPINFO only
1102 DWORD dwLen = bi.biSize + biColors * sizeof(RGBQUAD);
1103
1104 // first get the image size
1105 ScreenHDC hdc;
1106 if ( !GetDIBits(hdc, hbmp, 0, bi.biHeight, NULL, pbi, DIB_RGB_COLORS) )
1107 {
1108 wxLogLastError("GetDIBits(NULL)");
1109
1110 return 0;
1111 }
1112
1113 if ( wantSizeOnly )
1114 {
1115 // size of the header + size of the image
1116 return dwLen + bi.biSizeImage;
1117 }
1118
1119 // and now copy the bits
1120 void *image = (char *)pbi + dwLen;
1121 if ( !GetDIBits(hdc, hbmp, 0, bi.biHeight, image, pbi, DIB_RGB_COLORS) )
1122 {
1123 wxLogLastError("GetDIBits");
1124
1125 return 0;
1126 }
1127
1128 return dwLen + bi.biSizeImage;
1129 }
1130
1131 wxBitmap wxConvertDIBToBitmap(const LPBITMAPINFO pbmi)
1132 {
1133 // here we get BITMAPINFO struct followed by the actual bitmap bits and
1134 // BITMAPINFO starts with BITMAPINFOHEADER followed by colour info
1135 const BITMAPINFOHEADER *pbmih = &pbmi->bmiHeader;
1136
1137 // offset of image from the beginning of the header
1138 DWORD ofs = wxGetNumOfBitmapColors(pbmih->biBitCount) * sizeof(RGBQUAD);
1139 void *image = (char *)pbmih + sizeof(BITMAPINFOHEADER) + ofs;
1140
1141 ScreenHDC hdc;
1142 HBITMAP hbmp = CreateDIBitmap(hdc, pbmih, CBM_INIT,
1143 image, pbmi, DIB_RGB_COLORS);
1144 if ( !hbmp )
1145 {
1146 wxLogLastError("CreateDIBitmap");
1147 }
1148
1149 wxBitmap bitmap(pbmih->biWidth, pbmih->biHeight, pbmih->biBitCount);
1150 bitmap.SetHBITMAP((WXHBITMAP)hbmp);
1151
1152 return bitmap;
1153 }
1154
1155 #ifdef __WXDEBUG__
1156
1157 static const wxChar *GetTymedName(DWORD tymed)
1158 {
1159 static wxChar s_szBuf[128];
1160 switch ( tymed ) {
1161 case TYMED_HGLOBAL: return wxT("TYMED_HGLOBAL");
1162 case TYMED_FILE: return wxT("TYMED_FILE");
1163 case TYMED_ISTREAM: return wxT("TYMED_ISTREAM");
1164 case TYMED_ISTORAGE: return wxT("TYMED_ISTORAGE");
1165 case TYMED_GDI: return wxT("TYMED_GDI");
1166 case TYMED_MFPICT: return wxT("TYMED_MFPICT");
1167 case TYMED_ENHMF: return wxT("TYMED_ENHMF");
1168 default:
1169 wxSprintf(s_szBuf, wxT("type of media format %d (unknown)"), tymed);
1170 return s_szBuf;
1171 }
1172 }
1173
1174 #endif // Debug
1175
1176 #endif // not using OLE at all
1177