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