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