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