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