]> git.saurik.com Git - wxWidgets.git/blob - src/msw/ole/dataobj.cpp
Applied patch [ 761138 ] Replaces references to wxT("") and _T("") with wxEmptyString
[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) wxEmptyString
66 #endif // Debug/!Debug
67
68 // ----------------------------------------------------------------------------
69 // wxIEnumFORMATETC interface implementation
70 // ----------------------------------------------------------------------------
71
72 class wxIEnumFORMATETC : public IEnumFORMATETC
73 {
74 public:
75 wxIEnumFORMATETC(const wxDataFormat* formats, ULONG nCount);
76 virtual ~wxIEnumFORMATETC() { delete [] m_formats; }
77
78 DECLARE_IUNKNOWN_METHODS;
79
80 // IEnumFORMATETC
81 STDMETHODIMP Next(ULONG celt, FORMATETC *rgelt, ULONG *pceltFetched);
82 STDMETHODIMP Skip(ULONG celt);
83 STDMETHODIMP Reset();
84 STDMETHODIMP Clone(IEnumFORMATETC **ppenum);
85
86 private:
87 CLIPFORMAT *m_formats; // formats we can provide data in
88 ULONG m_nCount, // number of formats we support
89 m_nCurrent; // current enum position
90
91 DECLARE_NO_COPY_CLASS(wxIEnumFORMATETC)
92 };
93
94 // ----------------------------------------------------------------------------
95 // wxIDataObject implementation of IDataObject interface
96 // ----------------------------------------------------------------------------
97
98 class wxIDataObject : public IDataObject
99 {
100 public:
101 wxIDataObject(wxDataObject *pDataObject);
102 virtual ~wxIDataObject();
103
104 // normally, wxDataObject controls our lifetime (i.e. we're deleted when it
105 // is), but in some cases, the situation is 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 *pceltFetched)
191 {
192 wxLogTrace(wxTRACE_OleCalls, wxT("wxIEnumFORMATETC::Next"));
193
194 ULONG numFetched = 0;
195 while (m_nCurrent < m_nCount && numFetched < celt) {
196 FORMATETC format;
197 format.cfFormat = m_formats[m_nCurrent++];
198 format.ptd = NULL;
199 format.dwAspect = DVASPECT_CONTENT;
200 format.lindex = -1;
201 format.tymed = TYMED_HGLOBAL;
202
203 *rgelt++ = format;
204 numFetched++;
205 }
206
207 if (pceltFetched)
208 *pceltFetched = numFetched;
209
210 return numFetched == celt ? S_OK : S_FALSE;
211 }
212
213 STDMETHODIMP wxIEnumFORMATETC::Skip(ULONG celt)
214 {
215 wxLogTrace(wxTRACE_OleCalls, wxT("wxIEnumFORMATETC::Skip"));
216
217 m_nCurrent += celt;
218 if ( m_nCurrent < m_nCount )
219 return S_OK;
220
221 // no, can't skip this many elements
222 m_nCurrent -= celt;
223
224 return S_FALSE;
225 }
226
227 STDMETHODIMP wxIEnumFORMATETC::Reset()
228 {
229 wxLogTrace(wxTRACE_OleCalls, wxT("wxIEnumFORMATETC::Reset"));
230
231 m_nCurrent = 0;
232
233 return S_OK;
234 }
235
236 STDMETHODIMP wxIEnumFORMATETC::Clone(IEnumFORMATETC **ppenum)
237 {
238 wxLogTrace(wxTRACE_OleCalls, wxT("wxIEnumFORMATETC::Clone"));
239
240 // unfortunately, we can't reuse the code in ctor - types are different
241 wxIEnumFORMATETC *pNew = new wxIEnumFORMATETC(NULL, 0);
242 pNew->m_nCount = m_nCount;
243 pNew->m_formats = new CLIPFORMAT[m_nCount];
244 for ( ULONG n = 0; n < m_nCount; n++ ) {
245 pNew->m_formats[n] = m_formats[n];
246 }
247 pNew->AddRef();
248 *ppenum = pNew;
249
250 return S_OK;
251 }
252
253 // ----------------------------------------------------------------------------
254 // wxIDataObject
255 // ----------------------------------------------------------------------------
256
257 BEGIN_IID_TABLE(wxIDataObject)
258 ADD_IID(Unknown)
259 ADD_IID(DataObject)
260 END_IID_TABLE;
261
262 IMPLEMENT_IUNKNOWN_METHODS(wxIDataObject)
263
264 wxIDataObject::wxIDataObject(wxDataObject *pDataObject)
265 {
266 m_pDataObject = pDataObject;
267 m_mustDelete = FALSE;
268 }
269
270 wxIDataObject::~wxIDataObject()
271 {
272 if ( m_mustDelete )
273 {
274 delete m_pDataObject;
275 }
276 }
277
278 // get data functions
279 STDMETHODIMP wxIDataObject::GetData(FORMATETC *pformatetcIn, STGMEDIUM *pmedium)
280 {
281 wxLogTrace(wxTRACE_OleCalls, wxT("wxIDataObject::GetData"));
282
283 // is data is in our format?
284 HRESULT hr = QueryGetData(pformatetcIn);
285 if ( FAILED(hr) )
286 return hr;
287
288 // for the bitmaps and metafiles we use the handles instead of global memory
289 // to pass the data
290 wxDataFormat format = (wxDataFormat::NativeFormat)pformatetcIn->cfFormat;
291
292 switch ( format )
293 {
294 case wxDF_BITMAP:
295 pmedium->tymed = TYMED_GDI;
296 break;
297
298 case wxDF_ENHMETAFILE:
299 pmedium->tymed = TYMED_ENHMF;
300 break;
301
302 case wxDF_METAFILE:
303 pmedium->hGlobal = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE,
304 sizeof(METAFILEPICT));
305 if ( !pmedium->hGlobal ) {
306 wxLogLastError(wxT("GlobalAlloc"));
307 return E_OUTOFMEMORY;
308 }
309 pmedium->tymed = TYMED_MFPICT;
310 break;
311
312 default:
313 // alloc memory
314 size_t size = m_pDataObject->GetDataSize(format);
315 if ( !size ) {
316 // it probably means that the method is just not implemented
317 wxLogDebug(wxT("Invalid data size - can't be 0"));
318
319 return DV_E_FORMATETC;
320 }
321
322 if ( !format.IsStandard() ) {
323 // for custom formats, put the size with the data - alloc the
324 // space for it
325 // MB: not completely sure this is correct,
326 // even if I can't figure out what's wrong
327 size += m_pDataObject->GetBufferOffset( format );
328 }
329
330 HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, size);
331 if ( hGlobal == NULL ) {
332 wxLogLastError(wxT("GlobalAlloc"));
333 return E_OUTOFMEMORY;
334 }
335
336 // copy data
337 pmedium->tymed = TYMED_HGLOBAL;
338 pmedium->hGlobal = hGlobal;
339 }
340
341 pmedium->pUnkForRelease = NULL;
342
343 // do copy the data
344 hr = GetDataHere(pformatetcIn, pmedium);
345 if ( FAILED(hr) ) {
346 // free resources we allocated
347 if ( pmedium->tymed & (TYMED_HGLOBAL | TYMED_MFPICT) ) {
348 GlobalFree(pmedium->hGlobal);
349 }
350
351 return hr;
352 }
353
354 return S_OK;
355 }
356
357 STDMETHODIMP wxIDataObject::GetDataHere(FORMATETC *pformatetc,
358 STGMEDIUM *pmedium)
359 {
360 wxLogTrace(wxTRACE_OleCalls, wxT("wxIDataObject::GetDataHere"));
361
362 // put data in caller provided medium
363 switch ( pmedium->tymed )
364 {
365 case TYMED_GDI:
366 if ( !m_pDataObject->GetDataHere(wxDF_BITMAP, &pmedium->hBitmap) )
367 return E_UNEXPECTED;
368 break;
369
370 case TYMED_ENHMF:
371 if ( !m_pDataObject->GetDataHere(wxDF_ENHMETAFILE,
372 &pmedium->hEnhMetaFile) )
373 return E_UNEXPECTED;
374 break;
375
376 case TYMED_MFPICT:
377 // fall through - we pass METAFILEPICT through HGLOBAL
378
379 case TYMED_HGLOBAL:
380 {
381 // copy data
382 HGLOBAL hGlobal = pmedium->hGlobal;
383 void *pBuf = GlobalLock(hGlobal);
384 if ( pBuf == NULL ) {
385 wxLogLastError(wxT("GlobalLock"));
386 return E_OUTOFMEMORY;
387 }
388
389 wxDataFormat format = pformatetc->cfFormat;
390 if ( !format.IsStandard() ) {
391 // for custom formats, put the size with the data
392 pBuf = m_pDataObject->SetSizeInBuffer( pBuf, GlobalSize(hGlobal), format );
393 }
394
395 if ( !m_pDataObject->GetDataHere(format, pBuf) )
396 return E_UNEXPECTED;
397
398 GlobalUnlock(hGlobal);
399 }
400 break;
401
402 default:
403 return DV_E_TYMED;
404 }
405
406 return S_OK;
407 }
408
409
410 // set data functions
411 STDMETHODIMP wxIDataObject::SetData(FORMATETC *pformatetc,
412 STGMEDIUM *pmedium,
413 BOOL fRelease)
414 {
415 wxLogTrace(wxTRACE_OleCalls, wxT("wxIDataObject::SetData"));
416
417 switch ( pmedium->tymed )
418 {
419 case TYMED_GDI:
420 m_pDataObject->SetData(wxDF_BITMAP, 0, &pmedium->hBitmap);
421 break;
422
423 case TYMED_ENHMF:
424 m_pDataObject->SetData(wxDF_ENHMETAFILE, 0, &pmedium->hEnhMetaFile);
425 break;
426
427 case TYMED_MFPICT:
428 // fall through - we pass METAFILEPICT through HGLOBAL
429 case TYMED_HGLOBAL:
430 {
431 wxDataFormat format = pformatetc->cfFormat;
432
433 // this is quite weird, but for file drag and drop, explorer
434 // calls our SetData() with the formats we do *not* support!
435 //
436 // as we can't fix this bug in explorer (it's a bug because it
437 // should only use formats returned by EnumFormatEtc), do the
438 // check here
439 if ( !m_pDataObject->IsSupported(format, wxDataObject::Set) ) {
440 // go away!
441 return DV_E_FORMATETC;
442 }
443
444 // copy data
445 const void *pBuf = GlobalLock(pmedium->hGlobal);
446 if ( pBuf == NULL ) {
447 wxLogLastError(wxT("GlobalLock"));
448
449 return E_OUTOFMEMORY;
450 }
451
452 // we've got a problem with SetData() here because the base
453 // class version requires the size parameter which we don't
454 // have anywhere in OLE data transfer - so we need to
455 // synthetise it for known formats and we suppose that all data
456 // in custom formats starts with a DWORD containing the size
457 size_t size;
458 switch ( format )
459 {
460 case CF_TEXT:
461 case CF_OEMTEXT:
462 size = strlen((const char *)pBuf);
463 break;
464 #if !defined(__WATCOMC__) && ! (defined(__BORLANDC__) && (__BORLANDC__ < 0x500))
465 case CF_UNICODETEXT:
466 #if ( defined(__BORLANDC__) && (__BORLANDC__ > 0x530) ) \
467 || ( defined(__MWERKS__) && defined(__WXMSW__) )
468 size = std::wcslen((const wchar_t *)pBuf) * sizeof(wchar_t);
469 #else
470 size = wxWcslen((const wchar_t *)pBuf) * sizeof(wchar_t);
471 #endif
472 break;
473 #endif
474 case CF_BITMAP:
475 case CF_HDROP:
476 // these formats don't use size at all, anyhow (but
477 // pass data by handle, which is always a single DWORD)
478 size = 0;
479 break;
480
481 case CF_DIB:
482 // the handler will calculate size itself (it's too
483 // complicated to do it here)
484 size = 0;
485 break;
486
487 case CF_METAFILEPICT:
488 size = sizeof(METAFILEPICT);
489 break;
490
491 default:
492 {
493 // we suppose that the size precedes the data
494 pBuf = m_pDataObject->GetSizeFromBuffer( pBuf, &size, format );
495 if (! format.IsStandard() ) {
496 // see GetData for coresponding increment
497 size -= m_pDataObject->GetBufferOffset( format );
498 }
499 }
500 }
501
502 bool ok = m_pDataObject->SetData(format, size, pBuf);
503
504 GlobalUnlock(pmedium->hGlobal);
505
506 if ( !ok ) {
507 return E_UNEXPECTED;
508 }
509 }
510 break;
511
512 default:
513 return DV_E_TYMED;
514 }
515
516 if ( fRelease ) {
517 // we own the medium, so we must release it - but do *not* free any
518 // data we pass by handle because we have copied it elsewhere
519 switch ( pmedium->tymed )
520 {
521 case TYMED_GDI:
522 pmedium->hBitmap = 0;
523 break;
524
525 case TYMED_MFPICT:
526 pmedium->hMetaFilePict = 0;
527 break;
528
529 case TYMED_ENHMF:
530 pmedium->hEnhMetaFile = 0;
531 break;
532 }
533
534 ReleaseStgMedium(pmedium);
535 }
536
537 return S_OK;
538 }
539
540 // information functions
541 STDMETHODIMP wxIDataObject::QueryGetData(FORMATETC *pformatetc)
542 {
543 // do we accept data in this format?
544 if ( pformatetc == NULL ) {
545 wxLogTrace(wxTRACE_OleCalls,
546 wxT("wxIDataObject::QueryGetData: invalid ptr."));
547
548 return E_INVALIDARG;
549 }
550
551 // the only one allowed by current COM implementation
552 if ( pformatetc->lindex != -1 ) {
553 wxLogTrace(wxTRACE_OleCalls,
554 wxT("wxIDataObject::QueryGetData: bad lindex %ld"),
555 pformatetc->lindex);
556
557 return DV_E_LINDEX;
558 }
559
560 // we don't support anything other (THUMBNAIL, ICON, DOCPRINT...)
561 if ( pformatetc->dwAspect != DVASPECT_CONTENT ) {
562 wxLogTrace(wxTRACE_OleCalls,
563 wxT("wxIDataObject::QueryGetData: bad dwAspect %ld"),
564 pformatetc->dwAspect);
565
566 return DV_E_DVASPECT;
567 }
568
569 // and now check the type of data requested
570 wxDataFormat format = pformatetc->cfFormat;
571 if ( m_pDataObject->IsSupportedFormat(format) ) {
572 wxLogTrace(wxTRACE_OleCalls, wxT("wxIDataObject::QueryGetData: %s ok"),
573 wxGetFormatName(format));
574 }
575 else {
576 wxLogTrace(wxTRACE_OleCalls,
577 wxT("wxIDataObject::QueryGetData: %s unsupported"),
578 wxGetFormatName(format));
579
580 return DV_E_FORMATETC;
581 }
582
583 // we only transfer data by global memory, except for some particular cases
584 DWORD tymed = pformatetc->tymed;
585 if ( (format == wxDF_BITMAP && !(tymed & TYMED_GDI)) &&
586 !(tymed & TYMED_HGLOBAL) ) {
587 // it's not what we're waiting for
588 wxLogTrace(wxTRACE_OleCalls,
589 wxT("wxIDataObject::QueryGetData: %s != %s"),
590 GetTymedName(tymed),
591 GetTymedName(format == wxDF_BITMAP ? TYMED_GDI
592 : TYMED_HGLOBAL));
593
594 return DV_E_TYMED;
595 }
596
597 return S_OK;
598 }
599
600 STDMETHODIMP wxIDataObject::GetCanonicalFormatEtc(FORMATETC *WXUNUSED(pFormatetcIn),
601 FORMATETC *pFormatetcOut)
602 {
603 wxLogTrace(wxTRACE_OleCalls, wxT("wxIDataObject::GetCanonicalFormatEtc"));
604
605 // TODO we might want something better than this trivial implementation here
606 if ( pFormatetcOut != NULL )
607 pFormatetcOut->ptd = NULL;
608
609 return DATA_S_SAMEFORMATETC;
610 }
611
612 STDMETHODIMP wxIDataObject::EnumFormatEtc(DWORD dwDir,
613 IEnumFORMATETC **ppenumFormatEtc)
614 {
615 wxLogTrace(wxTRACE_OleCalls, wxT("wxIDataObject::EnumFormatEtc"));
616
617 wxDataObject::Direction dir = dwDir == DATADIR_GET ? wxDataObject::Get
618 : wxDataObject::Set;
619
620 size_t nFormatCount = m_pDataObject->GetFormatCount(dir);
621 wxDataFormat format;
622 wxDataFormat *formats;
623 formats = nFormatCount == 1 ? &format : new wxDataFormat[nFormatCount];
624 m_pDataObject->GetAllFormats(formats, dir);
625
626 wxIEnumFORMATETC *pEnum = new wxIEnumFORMATETC(formats, nFormatCount);
627 pEnum->AddRef();
628 *ppenumFormatEtc = pEnum;
629
630 if ( formats != &format ) {
631 delete [] formats;
632 }
633
634 return S_OK;
635 }
636
637 // ----------------------------------------------------------------------------
638 // advise sink functions (not implemented)
639 // ----------------------------------------------------------------------------
640
641 STDMETHODIMP wxIDataObject::DAdvise(FORMATETC *WXUNUSED(pformatetc),
642 DWORD WXUNUSED(advf),
643 IAdviseSink *WXUNUSED(pAdvSink),
644 DWORD *WXUNUSED(pdwConnection))
645 {
646 return OLE_E_ADVISENOTSUPPORTED;
647 }
648
649 STDMETHODIMP wxIDataObject::DUnadvise(DWORD WXUNUSED(dwConnection))
650 {
651 return OLE_E_ADVISENOTSUPPORTED;
652 }
653
654 STDMETHODIMP wxIDataObject::EnumDAdvise(IEnumSTATDATA **WXUNUSED(ppenumAdvise))
655 {
656 return OLE_E_ADVISENOTSUPPORTED;
657 }
658
659 // ----------------------------------------------------------------------------
660 // wxDataObject
661 // ----------------------------------------------------------------------------
662
663 wxDataObject::wxDataObject()
664 {
665 m_pIDataObject = new wxIDataObject(this);
666 m_pIDataObject->AddRef();
667 }
668
669 wxDataObject::~wxDataObject()
670 {
671 ReleaseInterface(m_pIDataObject);
672 }
673
674 void wxDataObject::SetAutoDelete()
675 {
676 ((wxIDataObject *)m_pIDataObject)->SetDeleteFlag();
677 m_pIDataObject->Release();
678
679 // so that the dtor doesnt' crash
680 m_pIDataObject = NULL;
681 }
682
683 size_t wxDataObject::GetBufferOffset( const wxDataFormat& WXUNUSED(format) )
684 {
685 return sizeof(size_t);
686 }
687
688 const void* wxDataObject::GetSizeFromBuffer( const void* buffer, size_t* size,
689 const wxDataFormat& WXUNUSED(format) )
690 {
691 size_t* p = (size_t*)buffer;
692 *size = *p;
693
694 return p + 1;
695 }
696
697 void* wxDataObject::SetSizeInBuffer( void* buffer, size_t size,
698 const wxDataFormat& WXUNUSED(format) )
699 {
700 size_t* p = (size_t*)buffer;
701 *p = size;
702
703 return p + 1;
704 }
705
706 #ifdef __WXDEBUG__
707
708 const wxChar *wxDataObject::GetFormatName(wxDataFormat format)
709 {
710 // case 'xxx' is not a valid value for switch of enum 'wxDataFormat'
711 #ifdef __VISUALC__
712 #pragma warning(disable:4063)
713 #endif // VC++
714
715 static wxChar s_szBuf[256];
716 switch ( format ) {
717 case CF_TEXT: return wxT("CF_TEXT");
718 case CF_BITMAP: return wxT("CF_BITMAP");
719 case CF_METAFILEPICT: return wxT("CF_METAFILEPICT");
720 case CF_SYLK: return wxT("CF_SYLK");
721 case CF_DIF: return wxT("CF_DIF");
722 case CF_TIFF: return wxT("CF_TIFF");
723 case CF_OEMTEXT: return wxT("CF_OEMTEXT");
724 case CF_DIB: return wxT("CF_DIB");
725 case CF_PALETTE: return wxT("CF_PALETTE");
726 case CF_PENDATA: return wxT("CF_PENDATA");
727 case CF_RIFF: return wxT("CF_RIFF");
728 case CF_WAVE: return wxT("CF_WAVE");
729 case CF_UNICODETEXT: return wxT("CF_UNICODETEXT");
730 case CF_ENHMETAFILE: return wxT("CF_ENHMETAFILE");
731 case CF_HDROP: return wxT("CF_HDROP");
732 case CF_LOCALE: return wxT("CF_LOCALE");
733
734 default:
735 if ( !::GetClipboardFormatName(format, s_szBuf, WXSIZEOF(s_szBuf)) )
736 {
737 // it must be a new predefined format we don't know the name of
738 wxSprintf(s_szBuf, wxT("unknown CF (0x%04x)"), format.GetFormatId());
739 }
740
741 return s_szBuf;
742 }
743
744 #ifdef __VISUALC__
745 #pragma warning(default:4063)
746 #endif // VC++
747 }
748
749 #endif // Debug
750
751 // ----------------------------------------------------------------------------
752 // wxBitmapDataObject supports CF_DIB format
753 // ----------------------------------------------------------------------------
754
755 size_t wxBitmapDataObject::GetDataSize() const
756 {
757 return wxDIB::ConvertFromBitmap(NULL, GetHbitmapOf(GetBitmap()));
758 }
759
760 bool wxBitmapDataObject::GetDataHere(void *buf) const
761 {
762 BITMAPINFO * const pbi = (BITMAPINFO *)buf;
763
764 return wxDIB::ConvertFromBitmap(pbi, GetHbitmapOf(GetBitmap())) != 0;
765 }
766
767 bool wxBitmapDataObject::SetData(size_t WXUNUSED(len), const void *buf)
768 {
769 const BITMAPINFO * const pbmi = (const BITMAPINFO *)buf;
770
771 HBITMAP hbmp = wxDIB::ConvertToBitmap(pbmi);
772
773 wxCHECK_MSG( hbmp, FALSE, wxT("pasting/dropping invalid bitmap") );
774
775 const BITMAPINFOHEADER * const pbmih = &pbmi->bmiHeader;
776 wxBitmap bitmap(pbmih->biWidth, pbmih->biHeight, pbmih->biBitCount);
777 bitmap.SetHBITMAP((WXHBITMAP)hbmp);
778
779 // TODO: create wxPalette if the bitmap has any
780
781 SetBitmap(bitmap);
782
783 return TRUE;
784 }
785
786 // ----------------------------------------------------------------------------
787 // wxBitmapDataObject2 supports CF_BITMAP format
788 // ----------------------------------------------------------------------------
789
790 // the bitmaps aren't passed by value as other types of data (i.e. by copying
791 // the data into a global memory chunk and passing it to the clipboard or
792 // another application or whatever), but by handle, so these generic functions
793 // don't make much sense to them.
794
795 size_t wxBitmapDataObject2::GetDataSize() const
796 {
797 return 0;
798 }
799
800 bool wxBitmapDataObject2::GetDataHere(void *pBuf) const
801 {
802 // we put a bitmap handle into pBuf
803 *(WXHBITMAP *)pBuf = GetBitmap().GetHBITMAP();
804
805 return TRUE;
806 }
807
808 bool wxBitmapDataObject2::SetData(size_t WXUNUSED(len), const void *pBuf)
809 {
810 HBITMAP hbmp = *(HBITMAP *)pBuf;
811
812 BITMAP bmp;
813 if ( !GetObject(hbmp, sizeof(BITMAP), &bmp) )
814 {
815 wxLogLastError(wxT("GetObject(HBITMAP)"));
816 }
817
818 wxBitmap bitmap(bmp.bmWidth, bmp.bmHeight, bmp.bmPlanes);
819 bitmap.SetHBITMAP((WXHBITMAP)hbmp);
820
821 if ( !bitmap.Ok() ) {
822 wxFAIL_MSG(wxT("pasting/dropping invalid bitmap"));
823
824 return FALSE;
825 }
826
827 SetBitmap(bitmap);
828
829 return TRUE;
830 }
831
832 #if 0
833
834 size_t wxBitmapDataObject::GetDataSize(const wxDataFormat& format) const
835 {
836 if ( format.GetFormatId() == CF_DIB )
837 {
838 // create the DIB
839 ScreenHDC hdc;
840
841 // shouldn't be selected into a DC or GetDIBits() would fail
842 wxASSERT_MSG( !m_bitmap.GetSelectedInto(),
843 wxT("can't copy bitmap selected into wxMemoryDC") );
844
845 // first get the info
846 BITMAPINFO bi;
847 if ( !GetDIBits(hdc, (HBITMAP)m_bitmap.GetHBITMAP(), 0, 0,
848 NULL, &bi, DIB_RGB_COLORS) )
849 {
850 wxLogLastError(wxT("GetDIBits(NULL)"));
851
852 return 0;
853 }
854
855 return sizeof(BITMAPINFO) + bi.bmiHeader.biSizeImage;
856 }
857 else // CF_BITMAP
858 {
859 // no data to copy - we don't pass HBITMAP via global memory
860 return 0;
861 }
862 }
863
864 bool wxBitmapDataObject::GetDataHere(const wxDataFormat& format,
865 void *pBuf) const
866 {
867 wxASSERT_MSG( m_bitmap.Ok(), wxT("copying invalid bitmap") );
868
869 HBITMAP hbmp = (HBITMAP)m_bitmap.GetHBITMAP();
870 if ( format.GetFormatId() == CF_DIB )
871 {
872 // create the DIB
873 ScreenHDC hdc;
874
875 // shouldn't be selected into a DC or GetDIBits() would fail
876 wxASSERT_MSG( !m_bitmap.GetSelectedInto(),
877 wxT("can't copy bitmap selected into wxMemoryDC") );
878
879 // first get the info
880 BITMAPINFO *pbi = (BITMAPINFO *)pBuf;
881 if ( !GetDIBits(hdc, hbmp, 0, 0, NULL, pbi, DIB_RGB_COLORS) )
882 {
883 wxLogLastError(wxT("GetDIBits(NULL)"));
884
885 return 0;
886 }
887
888 // and now copy the bits
889 if ( !GetDIBits(hdc, hbmp, 0, pbi->bmiHeader.biHeight, pbi + 1,
890 pbi, DIB_RGB_COLORS) )
891 {
892 wxLogLastError(wxT("GetDIBits"));
893
894 return FALSE;
895 }
896 }
897 else // CF_BITMAP
898 {
899 // we put a bitmap handle into pBuf
900 *(HBITMAP *)pBuf = hbmp;
901 }
902
903 return TRUE;
904 }
905
906 bool wxBitmapDataObject::SetData(const wxDataFormat& format,
907 size_t size, const void *pBuf)
908 {
909 HBITMAP hbmp;
910 if ( format.GetFormatId() == CF_DIB )
911 {
912 // here we get BITMAPINFO struct followed by the actual bitmap bits and
913 // BITMAPINFO starts with BITMAPINFOHEADER followed by colour info
914 ScreenHDC hdc;
915
916 BITMAPINFO *pbmi = (BITMAPINFO *)pBuf;
917 BITMAPINFOHEADER *pbmih = &pbmi->bmiHeader;
918 hbmp = CreateDIBitmap(hdc, pbmih, CBM_INIT,
919 pbmi + 1, pbmi, DIB_RGB_COLORS);
920 if ( !hbmp )
921 {
922 wxLogLastError(wxT("CreateDIBitmap"));
923 }
924
925 m_bitmap.SetWidth(pbmih->biWidth);
926 m_bitmap.SetHeight(pbmih->biHeight);
927 }
928 else // CF_BITMAP
929 {
930 // it's easy with bitmaps: we pass them by handle
931 hbmp = *(HBITMAP *)pBuf;
932
933 BITMAP bmp;
934 if ( !GetObject(hbmp, sizeof(BITMAP), &bmp) )
935 {
936 wxLogLastError(wxT("GetObject(HBITMAP)"));
937 }
938
939 m_bitmap.SetWidth(bmp.bmWidth);
940 m_bitmap.SetHeight(bmp.bmHeight);
941 m_bitmap.SetDepth(bmp.bmPlanes);
942 }
943
944 m_bitmap.SetHBITMAP((WXHBITMAP)hbmp);
945
946 wxASSERT_MSG( m_bitmap.Ok(), wxT("pasting invalid bitmap") );
947
948 return TRUE;
949 }
950
951 #endif // 0
952
953 // ----------------------------------------------------------------------------
954 // wxFileDataObject
955 // ----------------------------------------------------------------------------
956
957 bool wxFileDataObject::SetData(size_t WXUNUSED(size), const void *pData)
958 {
959 m_filenames.Empty();
960
961 // the documentation states that the first member of DROPFILES structure is
962 // a "DWORD offset of double NUL terminated file list". What they mean by
963 // this (I wonder if you see it immediately) is that the list starts at
964 // ((char *)&(pDropFiles.pFiles)) + pDropFiles.pFiles. We're also advised
965 // to use DragQueryFile to work with this structure, but not told where and
966 // how to get HDROP.
967 HDROP hdrop = (HDROP)pData; // NB: it works, but I'm not sure about it
968
969 // get number of files (magic value -1)
970 UINT nFiles = ::DragQueryFile(hdrop, (unsigned)-1, NULL, 0u);
971
972 wxCHECK_MSG ( nFiles != (UINT)-1, FALSE, wxT("wrong HDROP handle") );
973
974 // for each file get the length, allocate memory and then get the name
975 wxString str;
976 UINT len, n;
977 for ( n = 0; n < nFiles; n++ ) {
978 // +1 for terminating NUL
979 len = ::DragQueryFile(hdrop, n, NULL, 0) + 1;
980
981 UINT len2 = ::DragQueryFile(hdrop, n, str.GetWriteBuf(len), len);
982 str.UngetWriteBuf();
983 m_filenames.Add(str);
984
985 if ( len2 != len - 1 ) {
986 wxLogDebug(wxT("In wxFileDropTarget::OnDrop DragQueryFile returned\
987 %d characters, %d expected."), len2, len - 1);
988 }
989 }
990
991 return TRUE;
992 }
993
994 void wxFileDataObject::AddFile(const wxString& file)
995 {
996 // just add file to filenames array
997 // all useful data (such as DROPFILES struct) will be
998 // created later as necessary
999 m_filenames.Add(file);
1000 }
1001
1002 size_t wxFileDataObject::GetDataSize() const
1003 {
1004 // size returned will be the size of the DROPFILES structure,
1005 // plus the list of filesnames (null byte separated), plus
1006 // a double null at the end
1007
1008 // if no filenames in list, size is 0
1009 if ( m_filenames.GetCount() == 0 )
1010 return 0;
1011
1012 // inital size of DROPFILES struct + null byte
1013 size_t sz = sizeof(DROPFILES) + 1;
1014
1015 size_t count = m_filenames.GetCount();
1016 for ( size_t i = 0; i < count; i++ )
1017 {
1018 // add filename length plus null byte
1019 sz += m_filenames[i].Len() + 1;
1020 }
1021
1022 return sz;
1023 }
1024
1025 bool wxFileDataObject::GetDataHere(void *pData) const
1026 {
1027 // pData points to an externally allocated memory block
1028 // created using the size returned by GetDataSize()
1029
1030 // if pData is NULL, or there are no files, return
1031 if ( !pData || m_filenames.GetCount() == 0 )
1032 return FALSE;
1033
1034 // convert data pointer to a DROPFILES struct pointer
1035 LPDROPFILES pDrop = (LPDROPFILES) pData;
1036
1037 // initialize DROPFILES struct
1038 pDrop->pFiles = sizeof(DROPFILES);
1039 pDrop->fNC = FALSE; // not non-client coords
1040 #if wxUSE_UNICODE
1041 pDrop->fWide = TRUE;
1042 #else // ANSI
1043 pDrop->fWide = FALSE;
1044 #endif // Unicode/Ansi
1045
1046 // set start of filenames list (null separated)
1047 wxChar *pbuf = (wxChar*) ((BYTE *)pDrop + sizeof(DROPFILES));
1048
1049 size_t count = m_filenames.GetCount();
1050 for (size_t i = 0; i < count; i++ )
1051 {
1052 // copy filename to pbuf and add null terminator
1053 size_t len = m_filenames[i].Len();
1054 memcpy(pbuf, m_filenames[i], len);
1055 pbuf += len;
1056 *pbuf++ = wxT('\0');
1057 }
1058
1059 // add final null terminator
1060 *pbuf = wxT('\0');
1061
1062 return TRUE;
1063 }
1064
1065 // ----------------------------------------------------------------------------
1066 // wxURLDataObject
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, wxConvLibc );
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 #ifdef __WXDEBUG__
1164
1165 static const wxChar *GetTymedName(DWORD tymed)
1166 {
1167 static wxChar s_szBuf[128];
1168 switch ( tymed ) {
1169 case TYMED_HGLOBAL: return wxT("TYMED_HGLOBAL");
1170 case TYMED_FILE: return wxT("TYMED_FILE");
1171 case TYMED_ISTREAM: return wxT("TYMED_ISTREAM");
1172 case TYMED_ISTORAGE: return wxT("TYMED_ISTORAGE");
1173 case TYMED_GDI: return wxT("TYMED_GDI");
1174 case TYMED_MFPICT: return wxT("TYMED_MFPICT");
1175 case TYMED_ENHMF: return wxT("TYMED_ENHMF");
1176 default:
1177 wxSprintf(s_szBuf, wxT("type of media format %ld (unknown)"), tymed);
1178 return s_szBuf;
1179 }
1180 }
1181
1182 #endif // Debug
1183
1184 #else // not using OLE at all
1185 // ----------------------------------------------------------------------------
1186 // wxDataObject
1187 // ----------------------------------------------------------------------------
1188
1189 wxDataObject::wxDataObject()
1190 {
1191 }
1192
1193 wxDataObject::~wxDataObject()
1194 {
1195 }
1196
1197 void wxDataObject::SetAutoDelete()
1198 {
1199 }
1200
1201 #ifdef __WXDEBUG__
1202 const wxChar *wxDataObject::GetFormatName(wxDataFormat format)
1203 {
1204 return NULL;
1205 }
1206 #endif
1207
1208 #endif
1209