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