]> git.saurik.com Git - wxWidgets.git/blob - src/msw/ole/dataobj.cpp
fix bug with not accepting write-only formats in d&d
[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 #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 // for some compilers, the entire ole2.h must be included, not only oleauto.h
43 #if wxUSE_NORLANDER_HEADERS || defined(__WATCOMC__)
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) _T("")
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 inversed, 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, 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_nCurrent = 0;
181 m_nCount = nCount;
182 m_formats = new CLIPFORMAT[nCount];
183 for ( ULONG n = 0; n < nCount; n++ ) {
184 m_formats[n] = formats[n].GetFormatId();
185 }
186 }
187
188 STDMETHODIMP wxIEnumFORMATETC::Next(ULONG celt,
189 FORMATETC *rgelt,
190 ULONG *WXUNUSED(pceltFetched))
191 {
192 wxLogTrace(wxTRACE_OleCalls, wxT("wxIEnumFORMATETC::Next"));
193
194 if ( celt > 1 ) {
195 // we only return 1 element at a time - mainly because I'm too lazy to
196 // implement something which you're never asked for anyhow
197 return S_FALSE;
198 }
199
200 if ( m_nCurrent < m_nCount ) {
201 FORMATETC format;
202 format.cfFormat = m_formats[m_nCurrent++];
203 format.ptd = NULL;
204 format.dwAspect = DVASPECT_CONTENT;
205 format.lindex = -1;
206 format.tymed = TYMED_HGLOBAL;
207 *rgelt = format;
208
209 return S_OK;
210 }
211 else {
212 // bad index
213 return S_FALSE;
214 }
215 }
216
217 STDMETHODIMP wxIEnumFORMATETC::Skip(ULONG celt)
218 {
219 wxLogTrace(wxTRACE_OleCalls, wxT("wxIEnumFORMATETC::Skip"));
220
221 m_nCurrent += celt;
222 if ( m_nCurrent < m_nCount )
223 return S_OK;
224
225 // no, can't skip this many elements
226 m_nCurrent -= celt;
227
228 return S_FALSE;
229 }
230
231 STDMETHODIMP wxIEnumFORMATETC::Reset()
232 {
233 wxLogTrace(wxTRACE_OleCalls, wxT("wxIEnumFORMATETC::Reset"));
234
235 m_nCurrent = 0;
236
237 return S_OK;
238 }
239
240 STDMETHODIMP wxIEnumFORMATETC::Clone(IEnumFORMATETC **ppenum)
241 {
242 wxLogTrace(wxTRACE_OleCalls, wxT("wxIEnumFORMATETC::Clone"));
243
244 // unfortunately, we can't reuse the code in ctor - types are different
245 wxIEnumFORMATETC *pNew = new wxIEnumFORMATETC(NULL, 0);
246 pNew->m_nCount = m_nCount;
247 pNew->m_formats = new CLIPFORMAT[m_nCount];
248 for ( ULONG n = 0; n < m_nCount; n++ ) {
249 pNew->m_formats[n] = m_formats[n];
250 }
251 pNew->AddRef();
252 *ppenum = pNew;
253
254 return S_OK;
255 }
256
257 // ----------------------------------------------------------------------------
258 // wxIDataObject
259 // ----------------------------------------------------------------------------
260
261 BEGIN_IID_TABLE(wxIDataObject)
262 ADD_IID(Unknown)
263 ADD_IID(DataObject)
264 END_IID_TABLE;
265
266 IMPLEMENT_IUNKNOWN_METHODS(wxIDataObject)
267
268 wxIDataObject::wxIDataObject(wxDataObject *pDataObject)
269 {
270 m_pDataObject = pDataObject;
271 m_mustDelete = FALSE;
272 }
273
274 wxIDataObject::~wxIDataObject()
275 {
276 if ( m_mustDelete )
277 {
278 delete m_pDataObject;
279 }
280 }
281
282 // get data functions
283 STDMETHODIMP wxIDataObject::GetData(FORMATETC *pformatetcIn, STGMEDIUM *pmedium)
284 {
285 wxLogTrace(wxTRACE_OleCalls, wxT("wxIDataObject::GetData"));
286
287 // is data is in our format?
288 HRESULT hr = QueryGetData(pformatetcIn);
289 if ( FAILED(hr) )
290 return hr;
291
292 // for the bitmaps and metafiles we use the handles instead of global memory
293 // to pass the data
294 wxDataFormat format = (wxDataFormat::NativeFormat)pformatetcIn->cfFormat;
295
296 switch ( format )
297 {
298 case wxDF_BITMAP:
299 pmedium->tymed = TYMED_GDI;
300 break;
301
302 case wxDF_ENHMETAFILE:
303 pmedium->tymed = TYMED_ENHMF;
304 break;
305
306 case wxDF_METAFILE:
307 pmedium->hGlobal = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE,
308 sizeof(METAFILEPICT));
309 if ( !pmedium->hGlobal ) {
310 wxLogLastError(wxT("GlobalAlloc"));
311 return E_OUTOFMEMORY;
312 }
313 pmedium->tymed = TYMED_MFPICT;
314 break;
315
316 default:
317 // alloc memory
318 size_t size = m_pDataObject->GetDataSize(format);
319 if ( !size ) {
320 // it probably means that the method is just not implemented
321 wxLogDebug(wxT("Invalid data size - can't be 0"));
322
323 return DV_E_FORMATETC;
324 }
325
326 if ( !format.IsStandard() ) {
327 // for custom formats, put the size with the data - alloc the
328 // space for it
329 // MB: not completely sure this is correct,
330 // even if I can't figure out what's wrong
331 size += m_pDataObject->GetBufferOffset( format );
332 }
333
334 HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, size);
335 if ( hGlobal == NULL ) {
336 wxLogLastError(wxT("GlobalAlloc"));
337 return E_OUTOFMEMORY;
338 }
339
340 // copy data
341 pmedium->tymed = TYMED_HGLOBAL;
342 pmedium->hGlobal = hGlobal;
343 }
344
345 pmedium->pUnkForRelease = NULL;
346
347 // do copy the data
348 hr = GetDataHere(pformatetcIn, pmedium);
349 if ( FAILED(hr) ) {
350 // free resources we allocated
351 if ( pmedium->tymed & (TYMED_HGLOBAL | TYMED_MFPICT) ) {
352 GlobalFree(pmedium->hGlobal);
353 }
354
355 return hr;
356 }
357
358 return S_OK;
359 }
360
361 STDMETHODIMP wxIDataObject::GetDataHere(FORMATETC *pformatetc,
362 STGMEDIUM *pmedium)
363 {
364 wxLogTrace(wxTRACE_OleCalls, wxT("wxIDataObject::GetDataHere"));
365
366 // put data in caller provided medium
367 switch ( pmedium->tymed )
368 {
369 case TYMED_GDI:
370 if ( !m_pDataObject->GetDataHere(wxDF_BITMAP, &pmedium->hBitmap) )
371 return E_UNEXPECTED;
372 break;
373
374 case TYMED_ENHMF:
375 if ( !m_pDataObject->GetDataHere(wxDF_ENHMETAFILE,
376 &pmedium->hEnhMetaFile) )
377 return E_UNEXPECTED;
378 break;
379
380 case TYMED_MFPICT:
381 // fall through - we pass METAFILEPICT through HGLOBAL
382
383 case TYMED_HGLOBAL:
384 {
385 // copy data
386 HGLOBAL hGlobal = pmedium->hGlobal;
387 void *pBuf = GlobalLock(hGlobal);
388 if ( pBuf == NULL ) {
389 wxLogLastError(wxT("GlobalLock"));
390 return E_OUTOFMEMORY;
391 }
392
393 wxDataFormat format = pformatetc->cfFormat;
394 if ( !format.IsStandard() ) {
395 // for custom formats, put the size with the data
396 pBuf = m_pDataObject->SetSizeInBuffer( pBuf, GlobalSize(hGlobal), format );
397 }
398
399 if ( !m_pDataObject->GetDataHere(format, pBuf) )
400 return E_UNEXPECTED;
401
402 GlobalUnlock(hGlobal);
403 }
404 break;
405
406 default:
407 return DV_E_TYMED;
408 }
409
410 return S_OK;
411 }
412
413 // set data functions
414 STDMETHODIMP wxIDataObject::SetData(FORMATETC *pformatetc,
415 STGMEDIUM *pmedium,
416 BOOL fRelease)
417 {
418 wxLogTrace(wxTRACE_OleCalls, wxT("wxIDataObject::SetData"));
419
420 switch ( pmedium->tymed )
421 {
422 case TYMED_GDI:
423 m_pDataObject->SetData(wxDF_BITMAP, 0, &pmedium->hBitmap);
424 break;
425
426 case TYMED_ENHMF:
427 m_pDataObject->SetData(wxDF_ENHMETAFILE, 0, &pmedium->hEnhMetaFile);
428 break;
429
430 case TYMED_MFPICT:
431 // fall through - we pass METAFILEPICT through HGLOBAL
432 case TYMED_HGLOBAL:
433 {
434 wxDataFormat format = pformatetc->cfFormat;
435
436 // this is quite weird, but for file drag and drop, explorer
437 // calls our SetData() with the formats we do *not* support!
438 //
439 // as we can't fix this bug in explorer (it's a bug because it
440 // should only use formats returned by EnumFormatEtc), do the
441 // check here
442 if ( !m_pDataObject->IsSupported(format, wxDataObject::Set) ) {
443 // go away!
444 return DV_E_FORMATETC;
445 }
446
447 // copy data
448 const void *pBuf = GlobalLock(pmedium->hGlobal);
449 if ( pBuf == NULL ) {
450 wxLogLastError(wxT("GlobalLock"));
451
452 return E_OUTOFMEMORY;
453 }
454
455 // we've got a problem with SetData() here because the base
456 // class version requires the size parameter which we don't
457 // have anywhere in OLE data transfer - so we need to
458 // synthetise it for known formats and we suppose that all data
459 // in custom formats starts with a DWORD containing the size
460 size_t size;
461 switch ( format )
462 {
463 case CF_TEXT:
464 case CF_OEMTEXT:
465 size = strlen((const char *)pBuf);
466 break;
467 #if !defined(__WATCOMC__) && ! (defined(__BORLANDC__) && (__BORLANDC__ < 0x500))
468 case CF_UNICODETEXT:
469 #if ( defined(__BORLANDC__) && (__BORLANDC__ > 0x530) ) \
470 || ( defined(__MWERKS__) && defined(__WXMSW__) )
471 size = std::wcslen((const wchar_t *)pBuf) * sizeof(wchar_t);
472 #else
473 size = wxWcslen((const wchar_t *)pBuf) * sizeof(wchar_t);
474 #endif
475 break;
476 #endif
477 case CF_BITMAP:
478 case CF_HDROP:
479 // these formats don't use size at all, anyhow (but
480 // pass data by handle, which is always a single DWORD)
481 size = 0;
482 break;
483
484 case CF_DIB:
485 // the handler will calculate size itself (it's too
486 // complicated to do it here)
487 size = 0;
488 break;
489
490 case CF_METAFILEPICT:
491 size = sizeof(METAFILEPICT);
492 break;
493
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 coresponding 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 size_t nFormatCount = 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_METAFILEPICT: return wxT("CF_METAFILEPICT");
723 case CF_SYLK: return wxT("CF_SYLK");
724 case CF_DIF: return wxT("CF_DIF");
725 case CF_TIFF: return wxT("CF_TIFF");
726 case CF_OEMTEXT: return wxT("CF_OEMTEXT");
727 case CF_DIB: return wxT("CF_DIB");
728 case CF_PALETTE: return wxT("CF_PALETTE");
729 case CF_PENDATA: return wxT("CF_PENDATA");
730 case CF_RIFF: return wxT("CF_RIFF");
731 case CF_WAVE: return wxT("CF_WAVE");
732 case CF_UNICODETEXT: return wxT("CF_UNICODETEXT");
733 case CF_ENHMETAFILE: return wxT("CF_ENHMETAFILE");
734 case CF_HDROP: return wxT("CF_HDROP");
735 case CF_LOCALE: return wxT("CF_LOCALE");
736
737 default:
738 if ( !::GetClipboardFormatName(format, s_szBuf, WXSIZEOF(s_szBuf)) )
739 {
740 // it must be a new predefined format we don't know the name of
741 wxSprintf(s_szBuf, wxT("unknown CF (0x%04x)"), format.GetFormatId());
742 }
743
744 return s_szBuf;
745 }
746
747 #ifdef __VISUALC__
748 #pragma warning(default:4063)
749 #endif // VC++
750 }
751
752 #endif // Debug
753
754 // ----------------------------------------------------------------------------
755 // wxBitmapDataObject supports CF_DIB format
756 // ----------------------------------------------------------------------------
757
758 size_t wxBitmapDataObject::GetDataSize() const
759 {
760 return wxDIB::ConvertFromBitmap(NULL, GetHbitmapOf(GetBitmap()));
761 }
762
763 bool wxBitmapDataObject::GetDataHere(void *buf) const
764 {
765 BITMAPINFO * const pbi = (BITMAPINFO *)buf;
766
767 return wxDIB::ConvertFromBitmap(pbi, GetHbitmapOf(GetBitmap())) != 0;
768 }
769
770 bool wxBitmapDataObject::SetData(size_t WXUNUSED(len), const void *buf)
771 {
772 const BITMAPINFO * const pbmi = (const BITMAPINFO *)buf;
773
774 HBITMAP hbmp = wxDIB::ConvertToBitmap(pbmi);
775
776 wxCHECK_MSG( hbmp, FALSE, wxT("pasting/dropping invalid bitmap") );
777
778 const BITMAPINFOHEADER * const pbmih = &pbmi->bmiHeader;
779 wxBitmap bitmap(pbmih->biWidth, pbmih->biHeight, pbmih->biBitCount);
780 bitmap.SetHBITMAP((WXHBITMAP)hbmp);
781
782 // TODO: create wxPalette if the bitmap has any
783
784 SetBitmap(bitmap);
785
786 return TRUE;
787 }
788
789 // ----------------------------------------------------------------------------
790 // wxBitmapDataObject2 supports CF_BITMAP format
791 // ----------------------------------------------------------------------------
792
793 // the bitmaps aren't passed by value as other types of data (i.e. by copying
794 // the data into a global memory chunk and passing it to the clipboard or
795 // another application or whatever), but by handle, so these generic functions
796 // don't make much sense to them.
797
798 size_t wxBitmapDataObject2::GetDataSize() const
799 {
800 return 0;
801 }
802
803 bool wxBitmapDataObject2::GetDataHere(void *pBuf) const
804 {
805 // we put a bitmap handle into pBuf
806 *(WXHBITMAP *)pBuf = GetBitmap().GetHBITMAP();
807
808 return TRUE;
809 }
810
811 bool wxBitmapDataObject2::SetData(size_t WXUNUSED(len), const void *pBuf)
812 {
813 HBITMAP hbmp = *(HBITMAP *)pBuf;
814
815 BITMAP bmp;
816 if ( !GetObject(hbmp, sizeof(BITMAP), &bmp) )
817 {
818 wxLogLastError(wxT("GetObject(HBITMAP)"));
819 }
820
821 wxBitmap bitmap(bmp.bmWidth, bmp.bmHeight, bmp.bmPlanes);
822 bitmap.SetHBITMAP((WXHBITMAP)hbmp);
823
824 if ( !bitmap.Ok() ) {
825 wxFAIL_MSG(wxT("pasting/dropping invalid bitmap"));
826
827 return FALSE;
828 }
829
830 SetBitmap(bitmap);
831
832 return TRUE;
833 }
834
835 #if 0
836
837 size_t wxBitmapDataObject::GetDataSize(const wxDataFormat& format) const
838 {
839 if ( format.GetFormatId() == CF_DIB )
840 {
841 // create the DIB
842 ScreenHDC hdc;
843
844 // shouldn't be selected into a DC or GetDIBits() would fail
845 wxASSERT_MSG( !m_bitmap.GetSelectedInto(),
846 wxT("can't copy bitmap selected into wxMemoryDC") );
847
848 // first get the info
849 BITMAPINFO bi;
850 if ( !GetDIBits(hdc, (HBITMAP)m_bitmap.GetHBITMAP(), 0, 0,
851 NULL, &bi, DIB_RGB_COLORS) )
852 {
853 wxLogLastError(wxT("GetDIBits(NULL)"));
854
855 return 0;
856 }
857
858 return sizeof(BITMAPINFO) + bi.bmiHeader.biSizeImage;
859 }
860 else // CF_BITMAP
861 {
862 // no data to copy - we don't pass HBITMAP via global memory
863 return 0;
864 }
865 }
866
867 bool wxBitmapDataObject::GetDataHere(const wxDataFormat& format,
868 void *pBuf) const
869 {
870 wxASSERT_MSG( m_bitmap.Ok(), wxT("copying invalid bitmap") );
871
872 HBITMAP hbmp = (HBITMAP)m_bitmap.GetHBITMAP();
873 if ( format.GetFormatId() == CF_DIB )
874 {
875 // create the DIB
876 ScreenHDC hdc;
877
878 // shouldn't be selected into a DC or GetDIBits() would fail
879 wxASSERT_MSG( !m_bitmap.GetSelectedInto(),
880 wxT("can't copy bitmap selected into wxMemoryDC") );
881
882 // first get the info
883 BITMAPINFO *pbi = (BITMAPINFO *)pBuf;
884 if ( !GetDIBits(hdc, hbmp, 0, 0, NULL, pbi, DIB_RGB_COLORS) )
885 {
886 wxLogLastError(wxT("GetDIBits(NULL)"));
887
888 return 0;
889 }
890
891 // and now copy the bits
892 if ( !GetDIBits(hdc, hbmp, 0, pbi->bmiHeader.biHeight, pbi + 1,
893 pbi, DIB_RGB_COLORS) )
894 {
895 wxLogLastError(wxT("GetDIBits"));
896
897 return FALSE;
898 }
899 }
900 else // CF_BITMAP
901 {
902 // we put a bitmap handle into pBuf
903 *(HBITMAP *)pBuf = hbmp;
904 }
905
906 return TRUE;
907 }
908
909 bool wxBitmapDataObject::SetData(const wxDataFormat& format,
910 size_t size, const void *pBuf)
911 {
912 HBITMAP hbmp;
913 if ( format.GetFormatId() == CF_DIB )
914 {
915 // here we get BITMAPINFO struct followed by the actual bitmap bits and
916 // BITMAPINFO starts with BITMAPINFOHEADER followed by colour info
917 ScreenHDC hdc;
918
919 BITMAPINFO *pbmi = (BITMAPINFO *)pBuf;
920 BITMAPINFOHEADER *pbmih = &pbmi->bmiHeader;
921 hbmp = CreateDIBitmap(hdc, pbmih, CBM_INIT,
922 pbmi + 1, pbmi, DIB_RGB_COLORS);
923 if ( !hbmp )
924 {
925 wxLogLastError(wxT("CreateDIBitmap"));
926 }
927
928 m_bitmap.SetWidth(pbmih->biWidth);
929 m_bitmap.SetHeight(pbmih->biHeight);
930 }
931 else // CF_BITMAP
932 {
933 // it's easy with bitmaps: we pass them by handle
934 hbmp = *(HBITMAP *)pBuf;
935
936 BITMAP bmp;
937 if ( !GetObject(hbmp, sizeof(BITMAP), &bmp) )
938 {
939 wxLogLastError(wxT("GetObject(HBITMAP)"));
940 }
941
942 m_bitmap.SetWidth(bmp.bmWidth);
943 m_bitmap.SetHeight(bmp.bmHeight);
944 m_bitmap.SetDepth(bmp.bmPlanes);
945 }
946
947 m_bitmap.SetHBITMAP((WXHBITMAP)hbmp);
948
949 wxASSERT_MSG( m_bitmap.Ok(), wxT("pasting invalid bitmap") );
950
951 return TRUE;
952 }
953
954 #endif // 0
955
956 // ----------------------------------------------------------------------------
957 // wxFileDataObject
958 // ----------------------------------------------------------------------------
959
960 bool wxFileDataObject::SetData(size_t WXUNUSED(size), const void *pData)
961 {
962 m_filenames.Empty();
963
964 // the documentation states that the first member of DROPFILES structure is
965 // a "DWORD offset of double NUL terminated file list". What they mean by
966 // this (I wonder if you see it immediately) is that the list starts at
967 // ((char *)&(pDropFiles.pFiles)) + pDropFiles.pFiles. We're also advised
968 // to use DragQueryFile to work with this structure, but not told where and
969 // how to get HDROP.
970 HDROP hdrop = (HDROP)pData; // NB: it works, but I'm not sure about it
971
972 // get number of files (magic value -1)
973 UINT nFiles = ::DragQueryFile(hdrop, (unsigned)-1, NULL, 0u);
974
975 wxCHECK_MSG ( nFiles != (UINT)-1, FALSE, wxT("wrong HDROP handle") );
976
977 // for each file get the length, allocate memory and then get the name
978 wxString str;
979 UINT len, n;
980 for ( n = 0; n < nFiles; n++ ) {
981 // +1 for terminating NUL
982 len = ::DragQueryFile(hdrop, n, NULL, 0) + 1;
983
984 UINT len2 = ::DragQueryFile(hdrop, n, str.GetWriteBuf(len), len);
985 str.UngetWriteBuf();
986 m_filenames.Add(str);
987
988 if ( len2 != len - 1 ) {
989 wxLogDebug(wxT("In wxFileDropTarget::OnDrop DragQueryFile returned\
990 %d characters, %d expected."), len2, len - 1);
991 }
992 }
993
994 return TRUE;
995 }
996
997 void wxFileDataObject::AddFile(const wxString& file)
998 {
999 // just add file to filenames array
1000 // all useful data (such as DROPFILES struct) will be
1001 // created later as necessary
1002 m_filenames.Add(file);
1003 }
1004
1005 size_t wxFileDataObject::GetDataSize() const
1006 {
1007 // size returned will be the size of the DROPFILES structure,
1008 // plus the list of filesnames (null byte separated), plus
1009 // a double null at the end
1010
1011 // if no filenames in list, size is 0
1012 if ( m_filenames.GetCount() == 0 )
1013 return 0;
1014
1015 // inital size of DROPFILES struct + null byte
1016 size_t sz = sizeof(DROPFILES) + 1;
1017
1018 size_t count = m_filenames.GetCount();
1019 for ( size_t i = 0; i < count; i++ )
1020 {
1021 // add filename length plus null byte
1022 sz += m_filenames[i].Len() + 1;
1023 }
1024
1025 return sz;
1026 }
1027
1028 bool wxFileDataObject::GetDataHere(void *pData) const
1029 {
1030 // pData points to an externally allocated memory block
1031 // created using the size returned by GetDataSize()
1032
1033 // if pData is NULL, or there are no files, return
1034 if ( !pData || m_filenames.GetCount() == 0 )
1035 return FALSE;
1036
1037 // convert data pointer to a DROPFILES struct pointer
1038 LPDROPFILES pDrop = (LPDROPFILES) pData;
1039
1040 // initialize DROPFILES struct
1041 pDrop->pFiles = sizeof(DROPFILES);
1042 pDrop->fNC = FALSE; // not non-client coords
1043 #if wxUSE_UNICODE
1044 pDrop->fWide = TRUE;
1045 #else // ANSI
1046 pDrop->fWide = FALSE;
1047 #endif // Unicode/Ansi
1048
1049 // set start of filenames list (null separated)
1050 wxChar *pbuf = (wxChar*) ((BYTE *)pDrop + sizeof(DROPFILES));
1051
1052 size_t count = m_filenames.GetCount();
1053 for (size_t i = 0; i < count; i++ )
1054 {
1055 // copy filename to pbuf and add null terminator
1056 size_t len = m_filenames[i].Len();
1057 memcpy(pbuf, m_filenames[i], len);
1058 pbuf += len;
1059 *pbuf++ = wxT('\0');
1060 }
1061
1062 // add final null terminator
1063 *pbuf = wxT('\0');
1064
1065 return TRUE;
1066 }
1067
1068 // ----------------------------------------------------------------------------
1069 // wxURLDataObject
1070 // ----------------------------------------------------------------------------
1071
1072 class CFSTR_SHELLURLDataObject : public wxCustomDataObject
1073 {
1074 public:
1075 CFSTR_SHELLURLDataObject() : wxCustomDataObject(CFSTR_SHELLURL) {}
1076 protected:
1077 virtual size_t GetBufferOffset( const wxDataFormat& WXUNUSED(format) )
1078 {
1079 return 0;
1080 }
1081
1082 virtual const void* GetSizeFromBuffer( const void* buffer, size_t* size,
1083 const wxDataFormat& WXUNUSED(format) )
1084 {
1085 // CFSTR_SHELLURL is _always_ ANSI text
1086 *size = strlen( (const char*)buffer );
1087
1088 return buffer;
1089 }
1090
1091 virtual void* SetSizeInBuffer( void* buffer, size_t WXUNUSED(size),
1092 const wxDataFormat& WXUNUSED(format) )
1093 {
1094 return buffer;
1095 }
1096
1097 #if wxUSE_UNICODE
1098 virtual bool GetDataHere( void* buffer ) const
1099 {
1100 // CFSTR_SHELLURL is _always_ ANSI!
1101 wxCharBuffer char_buffer( GetDataSize() );
1102 wxCustomDataObject::GetDataHere( (void*)char_buffer.data() );
1103 wxString unicode_buffer( char_buffer, wxConvLibc );
1104 memcpy( buffer, unicode_buffer.c_str(),
1105 ( unicode_buffer.length() + 1 ) * sizeof(wxChar) );
1106
1107 return TRUE;
1108 }
1109 #endif
1110 };
1111
1112
1113
1114 wxURLDataObject::wxURLDataObject()
1115 {
1116 // we support CF_TEXT and CFSTR_SHELLURL formats which are basicly the same
1117 // but it seems that some browsers only provide one of them so we have to
1118 // support both
1119 Add(new wxTextDataObject);
1120 Add(new CFSTR_SHELLURLDataObject());
1121
1122 // we don't have any data yet
1123 m_dataObjectLast = NULL;
1124 }
1125
1126 bool wxURLDataObject::SetData(const wxDataFormat& format,
1127 size_t len,
1128 const void *buf)
1129 {
1130 m_dataObjectLast = GetObject(format);
1131
1132 wxCHECK_MSG( m_dataObjectLast, FALSE,
1133 wxT("unsupported format in wxURLDataObject"));
1134
1135 return m_dataObjectLast->SetData(len, buf);
1136 }
1137
1138 wxString wxURLDataObject::GetURL() const
1139 {
1140 wxString url;
1141 wxCHECK_MSG( m_dataObjectLast, url, _T("no data in wxURLDataObject") );
1142
1143 size_t len = m_dataObjectLast->GetDataSize();
1144
1145 m_dataObjectLast->GetDataHere(url.GetWriteBuf(len));
1146 url.UngetWriteBuf();
1147
1148 return url;
1149 }
1150
1151 void wxURLDataObject::SetURL(const wxString& url)
1152 {
1153 SetData(wxDataFormat(wxUSE_UNICODE ? wxDF_UNICODETEXT : wxDF_TEXT),
1154 url.Length()+1, url.c_str());
1155
1156 // CFSTR_SHELLURL is always supposed to be ANSI...
1157 wxWX2MBbuf urlA = (wxWX2MBbuf)url.mbc_str();
1158 size_t len = strlen(urlA);
1159 SetData(wxDataFormat(CFSTR_SHELLURL), len+1, (const char*)urlA);
1160 }
1161
1162 // ----------------------------------------------------------------------------
1163 // private functions
1164 // ----------------------------------------------------------------------------
1165
1166 #ifdef __WXDEBUG__
1167
1168 static const wxChar *GetTymedName(DWORD tymed)
1169 {
1170 static wxChar s_szBuf[128];
1171 switch ( tymed ) {
1172 case TYMED_HGLOBAL: return wxT("TYMED_HGLOBAL");
1173 case TYMED_FILE: return wxT("TYMED_FILE");
1174 case TYMED_ISTREAM: return wxT("TYMED_ISTREAM");
1175 case TYMED_ISTORAGE: return wxT("TYMED_ISTORAGE");
1176 case TYMED_GDI: return wxT("TYMED_GDI");
1177 case TYMED_MFPICT: return wxT("TYMED_MFPICT");
1178 case TYMED_ENHMF: return wxT("TYMED_ENHMF");
1179 default:
1180 wxSprintf(s_szBuf, wxT("type of media format %ld (unknown)"), tymed);
1181 return s_szBuf;
1182 }
1183 }
1184
1185 #endif // Debug
1186
1187 #else // not using OLE at all
1188 // ----------------------------------------------------------------------------
1189 // wxDataObject
1190 // ----------------------------------------------------------------------------
1191
1192 wxDataObject::wxDataObject()
1193 {
1194 }
1195
1196 wxDataObject::~wxDataObject()
1197 {
1198 }
1199
1200 void wxDataObject::SetAutoDelete()
1201 {
1202 }
1203
1204 #ifdef __WXDEBUG__
1205 const wxChar *wxDataObject::GetFormatName(wxDataFormat format)
1206 {
1207 return NULL;
1208 }
1209 #endif
1210
1211 #endif
1212