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