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