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