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