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