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