]> git.saurik.com Git - wxWidgets.git/blob - src/msw/ole/dataobj.cpp
Add class factories for filter streams. Also filters now follow the convention
[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 // hack: the third parameter is declared non-const in Wine's headers so
690 // cast away the const
691 size_t realsz = ::HeapSize(::GetProcessHeap(), 0,
692 wx_const_cast(void*, buffer));
693 if ( realsz == (size_t)-1 )
694 {
695 // note that HeapSize() does not set last error
696 wxLogApiError(wxT("HeapSize"), 0);
697 return NULL;
698 }
699
700 *size = realsz;
701
702 // check if this data has its size prepended (as it was by default for wx
703 // programs prior 2.6.3):
704 size_t *p = (size_t *)buffer;
705 if ( *p == realsz )
706 {
707 if ( NeedsVerbatimData(format) )
708 wxLogDebug(wxT("Apparent data format mismatch: size not needed"));
709
710 p++; // this data has its size prepended; skip first DWORD
711 }
712
713 return p;
714 }
715
716 void* wxDataObject::SetSizeInBuffer( void* buffer, size_t size,
717 const wxDataFormat& format )
718 {
719 size_t* p = (size_t *)buffer;
720 if ( !NeedsVerbatimData(format) )
721 {
722 // prepend the size to the data and skip it
723 *p++ = size;
724 }
725
726 return p;
727 }
728
729 #ifdef __WXDEBUG__
730
731 const wxChar *wxDataObject::GetFormatName(wxDataFormat format)
732 {
733 // case 'xxx' is not a valid value for switch of enum 'wxDataFormat'
734 #ifdef __VISUALC__
735 #pragma warning(disable:4063)
736 #endif // VC++
737
738 static wxChar s_szBuf[256];
739 switch ( format ) {
740 case CF_TEXT: return wxT("CF_TEXT");
741 case CF_BITMAP: return wxT("CF_BITMAP");
742 case CF_SYLK: return wxT("CF_SYLK");
743 case CF_DIF: return wxT("CF_DIF");
744 case CF_TIFF: return wxT("CF_TIFF");
745 case CF_OEMTEXT: return wxT("CF_OEMTEXT");
746 case CF_DIB: return wxT("CF_DIB");
747 case CF_PALETTE: return wxT("CF_PALETTE");
748 case CF_PENDATA: return wxT("CF_PENDATA");
749 case CF_RIFF: return wxT("CF_RIFF");
750 case CF_WAVE: return wxT("CF_WAVE");
751 case CF_UNICODETEXT: return wxT("CF_UNICODETEXT");
752 #ifndef __WXWINCE__
753 case CF_METAFILEPICT: return wxT("CF_METAFILEPICT");
754 case CF_ENHMETAFILE: return wxT("CF_ENHMETAFILE");
755 case CF_LOCALE: return wxT("CF_LOCALE");
756 case CF_HDROP: return wxT("CF_HDROP");
757 #endif
758
759 default:
760 if ( !::GetClipboardFormatName(format, s_szBuf, WXSIZEOF(s_szBuf)) )
761 {
762 // it must be a new predefined format we don't know the name of
763 wxSprintf(s_szBuf, wxT("unknown CF (0x%04x)"), format.GetFormatId());
764 }
765
766 return s_szBuf;
767 }
768
769 #ifdef __VISUALC__
770 #pragma warning(default:4063)
771 #endif // VC++
772 }
773
774 #endif // Debug
775
776 // ----------------------------------------------------------------------------
777 // wxBitmapDataObject supports CF_DIB format
778 // ----------------------------------------------------------------------------
779
780 // TODO: support CF_DIB under Windows CE as well
781
782 size_t wxBitmapDataObject::GetDataSize() const
783 {
784 #if wxUSE_WXDIB && !defined(__WXWINCE__)
785 return wxDIB::ConvertFromBitmap(NULL, GetHbitmapOf(GetBitmap()));
786 #else
787 return 0;
788 #endif
789 }
790
791 bool wxBitmapDataObject::GetDataHere(void *buf) const
792 {
793 #if wxUSE_WXDIB && !defined(__WXWINCE__)
794 BITMAPINFO * const pbi = (BITMAPINFO *)buf;
795
796 return wxDIB::ConvertFromBitmap(pbi, GetHbitmapOf(GetBitmap())) != 0;
797 #else
798 wxUnusedVar(buf);
799 return false;
800 #endif
801 }
802
803 bool wxBitmapDataObject::SetData(size_t WXUNUSED(len), const void *buf)
804 {
805 #if wxUSE_WXDIB && !defined(__WXWINCE__)
806 const BITMAPINFO * const pbmi = (const BITMAPINFO *)buf;
807
808 HBITMAP hbmp = wxDIB::ConvertToBitmap(pbmi);
809
810 wxCHECK_MSG( hbmp, FALSE, wxT("pasting/dropping invalid bitmap") );
811
812 const BITMAPINFOHEADER * const pbmih = &pbmi->bmiHeader;
813 wxBitmap bitmap(pbmih->biWidth, pbmih->biHeight, pbmih->biBitCount);
814 bitmap.SetHBITMAP((WXHBITMAP)hbmp);
815
816 // TODO: create wxPalette if the bitmap has any
817
818 SetBitmap(bitmap);
819
820 return true;
821 #else
822 wxUnusedVar(buf);
823 return false;
824 #endif
825 }
826
827 // ----------------------------------------------------------------------------
828 // wxBitmapDataObject2 supports CF_BITMAP format
829 // ----------------------------------------------------------------------------
830
831 // the bitmaps aren't passed by value as other types of data (i.e. by copying
832 // the data into a global memory chunk and passing it to the clipboard or
833 // another application or whatever), but by handle, so these generic functions
834 // don't make much sense to them.
835
836 size_t wxBitmapDataObject2::GetDataSize() const
837 {
838 return 0;
839 }
840
841 bool wxBitmapDataObject2::GetDataHere(void *pBuf) const
842 {
843 // we put a bitmap handle into pBuf
844 *(WXHBITMAP *)pBuf = GetBitmap().GetHBITMAP();
845
846 return true;
847 }
848
849 bool wxBitmapDataObject2::SetData(size_t WXUNUSED(len), const void *pBuf)
850 {
851 HBITMAP hbmp = *(HBITMAP *)pBuf;
852
853 BITMAP bmp;
854 if ( !GetObject(hbmp, sizeof(BITMAP), &bmp) )
855 {
856 wxLogLastError(wxT("GetObject(HBITMAP)"));
857 }
858
859 wxBitmap bitmap(bmp.bmWidth, bmp.bmHeight, bmp.bmPlanes);
860 bitmap.SetHBITMAP((WXHBITMAP)hbmp);
861
862 if ( !bitmap.Ok() ) {
863 wxFAIL_MSG(wxT("pasting/dropping invalid bitmap"));
864
865 return false;
866 }
867
868 SetBitmap(bitmap);
869
870 return true;
871 }
872
873 #if 0
874
875 size_t wxBitmapDataObject::GetDataSize(const wxDataFormat& format) const
876 {
877 if ( format.GetFormatId() == CF_DIB )
878 {
879 // create the DIB
880 ScreenHDC hdc;
881
882 // shouldn't be selected into a DC or GetDIBits() would fail
883 wxASSERT_MSG( !m_bitmap.GetSelectedInto(),
884 wxT("can't copy bitmap selected into wxMemoryDC") );
885
886 // first get the info
887 BITMAPINFO bi;
888 if ( !GetDIBits(hdc, (HBITMAP)m_bitmap.GetHBITMAP(), 0, 0,
889 NULL, &bi, DIB_RGB_COLORS) )
890 {
891 wxLogLastError(wxT("GetDIBits(NULL)"));
892
893 return 0;
894 }
895
896 return sizeof(BITMAPINFO) + bi.bmiHeader.biSizeImage;
897 }
898 else // CF_BITMAP
899 {
900 // no data to copy - we don't pass HBITMAP via global memory
901 return 0;
902 }
903 }
904
905 bool wxBitmapDataObject::GetDataHere(const wxDataFormat& format,
906 void *pBuf) const
907 {
908 wxASSERT_MSG( m_bitmap.Ok(), wxT("copying invalid bitmap") );
909
910 HBITMAP hbmp = (HBITMAP)m_bitmap.GetHBITMAP();
911 if ( format.GetFormatId() == CF_DIB )
912 {
913 // create the DIB
914 ScreenHDC hdc;
915
916 // shouldn't be selected into a DC or GetDIBits() would fail
917 wxASSERT_MSG( !m_bitmap.GetSelectedInto(),
918 wxT("can't copy bitmap selected into wxMemoryDC") );
919
920 // first get the info
921 BITMAPINFO *pbi = (BITMAPINFO *)pBuf;
922 if ( !GetDIBits(hdc, hbmp, 0, 0, NULL, pbi, DIB_RGB_COLORS) )
923 {
924 wxLogLastError(wxT("GetDIBits(NULL)"));
925
926 return 0;
927 }
928
929 // and now copy the bits
930 if ( !GetDIBits(hdc, hbmp, 0, pbi->bmiHeader.biHeight, pbi + 1,
931 pbi, DIB_RGB_COLORS) )
932 {
933 wxLogLastError(wxT("GetDIBits"));
934
935 return false;
936 }
937 }
938 else // CF_BITMAP
939 {
940 // we put a bitmap handle into pBuf
941 *(HBITMAP *)pBuf = hbmp;
942 }
943
944 return true;
945 }
946
947 bool wxBitmapDataObject::SetData(const wxDataFormat& format,
948 size_t size, const void *pBuf)
949 {
950 HBITMAP hbmp;
951 if ( format.GetFormatId() == CF_DIB )
952 {
953 // here we get BITMAPINFO struct followed by the actual bitmap bits and
954 // BITMAPINFO starts with BITMAPINFOHEADER followed by colour info
955 ScreenHDC hdc;
956
957 BITMAPINFO *pbmi = (BITMAPINFO *)pBuf;
958 BITMAPINFOHEADER *pbmih = &pbmi->bmiHeader;
959 hbmp = CreateDIBitmap(hdc, pbmih, CBM_INIT,
960 pbmi + 1, pbmi, DIB_RGB_COLORS);
961 if ( !hbmp )
962 {
963 wxLogLastError(wxT("CreateDIBitmap"));
964 }
965
966 m_bitmap.SetWidth(pbmih->biWidth);
967 m_bitmap.SetHeight(pbmih->biHeight);
968 }
969 else // CF_BITMAP
970 {
971 // it's easy with bitmaps: we pass them by handle
972 hbmp = *(HBITMAP *)pBuf;
973
974 BITMAP bmp;
975 if ( !GetObject(hbmp, sizeof(BITMAP), &bmp) )
976 {
977 wxLogLastError(wxT("GetObject(HBITMAP)"));
978 }
979
980 m_bitmap.SetWidth(bmp.bmWidth);
981 m_bitmap.SetHeight(bmp.bmHeight);
982 m_bitmap.SetDepth(bmp.bmPlanes);
983 }
984
985 m_bitmap.SetHBITMAP((WXHBITMAP)hbmp);
986
987 wxASSERT_MSG( m_bitmap.Ok(), wxT("pasting invalid bitmap") );
988
989 return true;
990 }
991
992 #endif // 0
993
994 // ----------------------------------------------------------------------------
995 // wxFileDataObject
996 // ----------------------------------------------------------------------------
997
998 bool wxFileDataObject::SetData(size_t WXUNUSED(size),
999 const void *WXUNUSED_IN_WINCE(pData))
1000 {
1001 #ifndef __WXWINCE__
1002 m_filenames.Empty();
1003
1004 // the documentation states that the first member of DROPFILES structure is
1005 // a "DWORD offset of double NUL terminated file list". What they mean by
1006 // this (I wonder if you see it immediately) is that the list starts at
1007 // ((char *)&(pDropFiles.pFiles)) + pDropFiles.pFiles. We're also advised
1008 // to use DragQueryFile to work with this structure, but not told where and
1009 // how to get HDROP.
1010 HDROP hdrop = (HDROP)pData; // NB: it works, but I'm not sure about it
1011
1012 // get number of files (magic value -1)
1013 UINT nFiles = ::DragQueryFile(hdrop, (unsigned)-1, NULL, 0u);
1014
1015 wxCHECK_MSG ( nFiles != (UINT)-1, FALSE, wxT("wrong HDROP handle") );
1016
1017 // for each file get the length, allocate memory and then get the name
1018 wxString str;
1019 UINT len, n;
1020 for ( n = 0; n < nFiles; n++ ) {
1021 // +1 for terminating NUL
1022 len = ::DragQueryFile(hdrop, n, NULL, 0) + 1;
1023
1024 UINT len2 = ::DragQueryFile(hdrop, n, wxStringBuffer(str, len), len);
1025 m_filenames.Add(str);
1026
1027 if ( len2 != len - 1 ) {
1028 wxLogDebug(wxT("In wxFileDropTarget::OnDrop DragQueryFile returned\
1029 %d characters, %d expected."), len2, len - 1);
1030 }
1031 }
1032
1033 return true;
1034 #else
1035 return false;
1036 #endif
1037 }
1038
1039 void wxFileDataObject::AddFile(const wxString& file)
1040 {
1041 // just add file to filenames array
1042 // all useful data (such as DROPFILES struct) will be
1043 // created later as necessary
1044 m_filenames.Add(file);
1045 }
1046
1047 size_t wxFileDataObject::GetDataSize() const
1048 {
1049 #ifndef __WXWINCE__
1050 // size returned will be the size of the DROPFILES structure,
1051 // plus the list of filesnames (null byte separated), plus
1052 // a double null at the end
1053
1054 // if no filenames in list, size is 0
1055 if ( m_filenames.GetCount() == 0 )
1056 return 0;
1057
1058 // inital size of DROPFILES struct + null byte
1059 size_t sz = sizeof(DROPFILES) + (1 * sizeof(wxChar));
1060
1061 size_t count = m_filenames.GetCount();
1062 for ( size_t i = 0; i < count; i++ )
1063 {
1064 // add filename length plus null byte
1065 sz += (m_filenames[i].Len() + 1) * sizeof(wxChar);
1066 }
1067
1068 return sz;
1069 #else
1070 return 0;
1071 #endif
1072 }
1073
1074 bool wxFileDataObject::GetDataHere(void *WXUNUSED_IN_WINCE(pData)) const
1075 {
1076 #ifndef __WXWINCE__
1077 // pData points to an externally allocated memory block
1078 // created using the size returned by GetDataSize()
1079
1080 // if pData is NULL, or there are no files, return
1081 if ( !pData || m_filenames.GetCount() == 0 )
1082 return false;
1083
1084 // convert data pointer to a DROPFILES struct pointer
1085 LPDROPFILES pDrop = (LPDROPFILES) pData;
1086
1087 // initialize DROPFILES struct
1088 pDrop->pFiles = sizeof(DROPFILES);
1089 pDrop->fNC = FALSE; // not non-client coords
1090 #if wxUSE_UNICODE
1091 pDrop->fWide = TRUE;
1092 #else // ANSI
1093 pDrop->fWide = FALSE;
1094 #endif // Unicode/Ansi
1095
1096 // set start of filenames list (null separated)
1097 wxChar *pbuf = (wxChar*) ((BYTE *)pDrop + sizeof(DROPFILES));
1098
1099 size_t count = m_filenames.GetCount();
1100 for (size_t i = 0; i < count; i++ )
1101 {
1102 // copy filename to pbuf and add null terminator
1103 size_t len = m_filenames[i].Len();
1104 memcpy(pbuf, m_filenames[i], len*sizeof(wxChar));
1105 pbuf += len;
1106 *pbuf++ = wxT('\0');
1107 }
1108
1109 // add final null terminator
1110 *pbuf = wxT('\0');
1111
1112 return true;
1113 #else
1114 return false;
1115 #endif
1116 }
1117
1118 // ----------------------------------------------------------------------------
1119 // wxURLDataObject
1120 // ----------------------------------------------------------------------------
1121
1122 class CFSTR_SHELLURLDataObject : public wxCustomDataObject
1123 {
1124 public:
1125 CFSTR_SHELLURLDataObject() : wxCustomDataObject(CFSTR_SHELLURL) {}
1126
1127 virtual size_t GetBufferOffset( const wxDataFormat& WXUNUSED(format) )
1128 {
1129 return 0;
1130 }
1131
1132 virtual const void* GetSizeFromBuffer( const void* buffer, size_t* size,
1133 const wxDataFormat& WXUNUSED(format) )
1134 {
1135 // CFSTR_SHELLURL is _always_ ANSI text
1136 *size = strlen( (const char*)buffer );
1137
1138 return buffer;
1139 }
1140
1141 virtual void* SetSizeInBuffer( void* buffer, size_t WXUNUSED(size),
1142 const wxDataFormat& WXUNUSED(format) )
1143 {
1144 return buffer;
1145 }
1146
1147 #if wxUSE_UNICODE
1148 virtual bool GetDataHere( void* buffer ) const
1149 {
1150 // CFSTR_SHELLURL is _always_ ANSI!
1151 wxCharBuffer char_buffer( GetDataSize() );
1152 wxCustomDataObject::GetDataHere( (void*)char_buffer.data() );
1153 wxString unicode_buffer( char_buffer, wxConvLibc );
1154 memcpy( buffer, unicode_buffer.c_str(),
1155 ( unicode_buffer.length() + 1 ) * sizeof(wxChar) );
1156
1157 return true;
1158 }
1159 virtual bool GetDataHere(const wxDataFormat& WXUNUSED(format),
1160 void *buf) const
1161 { return GetDataHere(buf); }
1162 #endif
1163
1164 DECLARE_NO_COPY_CLASS(CFSTR_SHELLURLDataObject)
1165 };
1166
1167
1168
1169 wxURLDataObject::wxURLDataObject(const wxString& url)
1170 {
1171 // we support CF_TEXT and CFSTR_SHELLURL formats which are basicly the same
1172 // but it seems that some browsers only provide one of them so we have to
1173 // support both
1174 Add(new wxTextDataObject);
1175 Add(new CFSTR_SHELLURLDataObject());
1176
1177 // we don't have any data yet
1178 m_dataObjectLast = NULL;
1179
1180 if ( !url.empty() )
1181 SetURL(url);
1182 }
1183
1184 bool wxURLDataObject::SetData(const wxDataFormat& format,
1185 size_t len,
1186 const void *buf)
1187 {
1188 m_dataObjectLast = GetObject(format);
1189
1190 wxCHECK_MSG( m_dataObjectLast, FALSE,
1191 wxT("unsupported format in wxURLDataObject"));
1192
1193 return m_dataObjectLast->SetData(len, buf);
1194 }
1195
1196 wxString wxURLDataObject::GetURL() const
1197 {
1198 wxString url;
1199 wxCHECK_MSG( m_dataObjectLast, url, _T("no data in wxURLDataObject") );
1200
1201 size_t len = m_dataObjectLast->GetDataSize();
1202
1203 m_dataObjectLast->GetDataHere(wxStringBuffer(url, len));
1204
1205 return url;
1206 }
1207
1208 void wxURLDataObject::SetURL(const wxString& url)
1209 {
1210 SetData(wxDataFormat(wxUSE_UNICODE ? wxDF_UNICODETEXT : wxDF_TEXT),
1211 url.Length()+1, url.c_str());
1212
1213 // CFSTR_SHELLURL is always supposed to be ANSI...
1214 wxWX2MBbuf urlA = (wxWX2MBbuf)url.mbc_str();
1215 size_t len = strlen(urlA);
1216 SetData(wxDataFormat(CFSTR_SHELLURL), len+1, (const char*)urlA);
1217 }
1218
1219 // ----------------------------------------------------------------------------
1220 // private functions
1221 // ----------------------------------------------------------------------------
1222
1223 #ifdef __WXDEBUG__
1224
1225 static const wxChar *GetTymedName(DWORD tymed)
1226 {
1227 static wxChar s_szBuf[128];
1228 switch ( tymed ) {
1229 case TYMED_HGLOBAL: return wxT("TYMED_HGLOBAL");
1230 case TYMED_FILE: return wxT("TYMED_FILE");
1231 case TYMED_ISTREAM: return wxT("TYMED_ISTREAM");
1232 case TYMED_ISTORAGE: return wxT("TYMED_ISTORAGE");
1233 case TYMED_GDI: return wxT("TYMED_GDI");
1234 case TYMED_MFPICT: return wxT("TYMED_MFPICT");
1235 case TYMED_ENHMF: return wxT("TYMED_ENHMF");
1236 default:
1237 wxSprintf(s_szBuf, wxT("type of media format %ld (unknown)"), tymed);
1238 return s_szBuf;
1239 }
1240 }
1241
1242 #endif // Debug
1243
1244 #else // not using OLE at all
1245
1246 // ----------------------------------------------------------------------------
1247 // wxDataObject
1248 // ----------------------------------------------------------------------------
1249
1250 #if wxUSE_DATAOBJ
1251
1252 wxDataObject::wxDataObject()
1253 {
1254 }
1255
1256 wxDataObject::~wxDataObject()
1257 {
1258 }
1259
1260 void wxDataObject::SetAutoDelete()
1261 {
1262 }
1263
1264 #ifdef __WXDEBUG__
1265 const wxChar *wxDataObject::GetFormatName(wxDataFormat WXUNUSED(format))
1266 {
1267 return NULL;
1268 }
1269 #endif // __WXDEBUG__
1270
1271 #endif // wxUSE_DATAOBJ
1272
1273 #endif // wxUSE_OLE/!wxUSE_OLE
1274
1275