]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/msw/ole/dataobj.cpp
added Show/HideNativeCaret() (patch 759924)
[wxWidgets.git] / src / msw / ole / dataobj.cpp
... / ...
CommitLineData
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
76class wxIEnumFORMATETC : public IEnumFORMATETC
77{
78public:
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
90private:
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
102class wxIDataObject : public IDataObject
103{
104public:
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
126private:
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
142void 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
151wxString 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
175BEGIN_IID_TABLE(wxIEnumFORMATETC)
176 ADD_IID(Unknown)
177 ADD_IID(EnumFORMATETC)
178END_IID_TABLE;
179
180IMPLEMENT_IUNKNOWN_METHODS(wxIEnumFORMATETC)
181
182wxIEnumFORMATETC::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
192STDMETHODIMP 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
217STDMETHODIMP 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
231STDMETHODIMP wxIEnumFORMATETC::Reset()
232{
233 wxLogTrace(wxTRACE_OleCalls, wxT("wxIEnumFORMATETC::Reset"));
234
235 m_nCurrent = 0;
236
237 return S_OK;
238}
239
240STDMETHODIMP 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
261BEGIN_IID_TABLE(wxIDataObject)
262 ADD_IID(Unknown)
263 ADD_IID(DataObject)
264END_IID_TABLE;
265
266IMPLEMENT_IUNKNOWN_METHODS(wxIDataObject)
267
268wxIDataObject::wxIDataObject(wxDataObject *pDataObject)
269{
270 m_pDataObject = pDataObject;
271 m_mustDelete = FALSE;
272}
273
274wxIDataObject::~wxIDataObject()
275{
276 if ( m_mustDelete )
277 {
278 delete m_pDataObject;
279 }
280}
281
282// get data functions
283STDMETHODIMP 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
362STDMETHODIMP 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
416STDMETHODIMP 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
549STDMETHODIMP 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
608STDMETHODIMP 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
620STDMETHODIMP 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
649STDMETHODIMP 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
657STDMETHODIMP wxIDataObject::DUnadvise(DWORD WXUNUSED(dwConnection))
658{
659 return OLE_E_ADVISENOTSUPPORTED;
660}
661
662STDMETHODIMP wxIDataObject::EnumDAdvise(IEnumSTATDATA **WXUNUSED(ppenumAdvise))
663{
664 return OLE_E_ADVISENOTSUPPORTED;
665}
666
667// ----------------------------------------------------------------------------
668// wxDataObject
669// ----------------------------------------------------------------------------
670
671wxDataObject::wxDataObject()
672{
673 m_pIDataObject = new wxIDataObject(this);
674 m_pIDataObject->AddRef();
675}
676
677wxDataObject::~wxDataObject()
678{
679 ReleaseInterface(m_pIDataObject);
680}
681
682void 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
691size_t wxDataObject::GetBufferOffset( const wxDataFormat& WXUNUSED(format) )
692{
693 return sizeof(size_t);
694}
695
696const 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
705void* 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
716const 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
763size_t wxBitmapDataObject::GetDataSize() const
764{
765#if wxUSE_WXDIB
766 return wxDIB::ConvertFromBitmap(NULL, GetHbitmapOf(GetBitmap()));
767#else
768 return 0;
769#endif
770}
771
772bool wxBitmapDataObject::GetDataHere(void *buf) const
773{
774#if wxUSE_WXDIB
775 BITMAPINFO * const pbi = (BITMAPINFO *)buf;
776
777 return wxDIB::ConvertFromBitmap(pbi, GetHbitmapOf(GetBitmap())) != 0;
778#else
779 return FALSE;
780#endif
781}
782
783bool wxBitmapDataObject::SetData(size_t WXUNUSED(len), const void *buf)
784{
785#if wxUSE_WXDIB
786 const BITMAPINFO * const pbmi = (const BITMAPINFO *)buf;
787
788 HBITMAP hbmp = wxDIB::ConvertToBitmap(pbmi);
789
790 wxCHECK_MSG( hbmp, FALSE, wxT("pasting/dropping invalid bitmap") );
791
792 const BITMAPINFOHEADER * const pbmih = &pbmi->bmiHeader;
793 wxBitmap bitmap(pbmih->biWidth, pbmih->biHeight, pbmih->biBitCount);
794 bitmap.SetHBITMAP((WXHBITMAP)hbmp);
795
796 // TODO: create wxPalette if the bitmap has any
797
798 SetBitmap(bitmap);
799
800 return TRUE;
801#else
802 return FALSE;
803#endif
804}
805
806// ----------------------------------------------------------------------------
807// wxBitmapDataObject2 supports CF_BITMAP format
808// ----------------------------------------------------------------------------
809
810// the bitmaps aren't passed by value as other types of data (i.e. by copying
811// the data into a global memory chunk and passing it to the clipboard or
812// another application or whatever), but by handle, so these generic functions
813// don't make much sense to them.
814
815size_t wxBitmapDataObject2::GetDataSize() const
816{
817 return 0;
818}
819
820bool wxBitmapDataObject2::GetDataHere(void *pBuf) const
821{
822 // we put a bitmap handle into pBuf
823 *(WXHBITMAP *)pBuf = GetBitmap().GetHBITMAP();
824
825 return TRUE;
826}
827
828bool wxBitmapDataObject2::SetData(size_t WXUNUSED(len), const void *pBuf)
829{
830 HBITMAP hbmp = *(HBITMAP *)pBuf;
831
832 BITMAP bmp;
833 if ( !GetObject(hbmp, sizeof(BITMAP), &bmp) )
834 {
835 wxLogLastError(wxT("GetObject(HBITMAP)"));
836 }
837
838 wxBitmap bitmap(bmp.bmWidth, bmp.bmHeight, bmp.bmPlanes);
839 bitmap.SetHBITMAP((WXHBITMAP)hbmp);
840
841 if ( !bitmap.Ok() ) {
842 wxFAIL_MSG(wxT("pasting/dropping invalid bitmap"));
843
844 return FALSE;
845 }
846
847 SetBitmap(bitmap);
848
849 return TRUE;
850}
851
852#if 0
853
854size_t wxBitmapDataObject::GetDataSize(const wxDataFormat& format) const
855{
856 if ( format.GetFormatId() == CF_DIB )
857 {
858 // create the DIB
859 ScreenHDC hdc;
860
861 // shouldn't be selected into a DC or GetDIBits() would fail
862 wxASSERT_MSG( !m_bitmap.GetSelectedInto(),
863 wxT("can't copy bitmap selected into wxMemoryDC") );
864
865 // first get the info
866 BITMAPINFO bi;
867 if ( !GetDIBits(hdc, (HBITMAP)m_bitmap.GetHBITMAP(), 0, 0,
868 NULL, &bi, DIB_RGB_COLORS) )
869 {
870 wxLogLastError(wxT("GetDIBits(NULL)"));
871
872 return 0;
873 }
874
875 return sizeof(BITMAPINFO) + bi.bmiHeader.biSizeImage;
876 }
877 else // CF_BITMAP
878 {
879 // no data to copy - we don't pass HBITMAP via global memory
880 return 0;
881 }
882}
883
884bool wxBitmapDataObject::GetDataHere(const wxDataFormat& format,
885 void *pBuf) const
886{
887 wxASSERT_MSG( m_bitmap.Ok(), wxT("copying invalid bitmap") );
888
889 HBITMAP hbmp = (HBITMAP)m_bitmap.GetHBITMAP();
890 if ( format.GetFormatId() == CF_DIB )
891 {
892 // create the DIB
893 ScreenHDC hdc;
894
895 // shouldn't be selected into a DC or GetDIBits() would fail
896 wxASSERT_MSG( !m_bitmap.GetSelectedInto(),
897 wxT("can't copy bitmap selected into wxMemoryDC") );
898
899 // first get the info
900 BITMAPINFO *pbi = (BITMAPINFO *)pBuf;
901 if ( !GetDIBits(hdc, hbmp, 0, 0, NULL, pbi, DIB_RGB_COLORS) )
902 {
903 wxLogLastError(wxT("GetDIBits(NULL)"));
904
905 return 0;
906 }
907
908 // and now copy the bits
909 if ( !GetDIBits(hdc, hbmp, 0, pbi->bmiHeader.biHeight, pbi + 1,
910 pbi, DIB_RGB_COLORS) )
911 {
912 wxLogLastError(wxT("GetDIBits"));
913
914 return FALSE;
915 }
916 }
917 else // CF_BITMAP
918 {
919 // we put a bitmap handle into pBuf
920 *(HBITMAP *)pBuf = hbmp;
921 }
922
923 return TRUE;
924}
925
926bool wxBitmapDataObject::SetData(const wxDataFormat& format,
927 size_t size, const void *pBuf)
928{
929 HBITMAP hbmp;
930 if ( format.GetFormatId() == CF_DIB )
931 {
932 // here we get BITMAPINFO struct followed by the actual bitmap bits and
933 // BITMAPINFO starts with BITMAPINFOHEADER followed by colour info
934 ScreenHDC hdc;
935
936 BITMAPINFO *pbmi = (BITMAPINFO *)pBuf;
937 BITMAPINFOHEADER *pbmih = &pbmi->bmiHeader;
938 hbmp = CreateDIBitmap(hdc, pbmih, CBM_INIT,
939 pbmi + 1, pbmi, DIB_RGB_COLORS);
940 if ( !hbmp )
941 {
942 wxLogLastError(wxT("CreateDIBitmap"));
943 }
944
945 m_bitmap.SetWidth(pbmih->biWidth);
946 m_bitmap.SetHeight(pbmih->biHeight);
947 }
948 else // CF_BITMAP
949 {
950 // it's easy with bitmaps: we pass them by handle
951 hbmp = *(HBITMAP *)pBuf;
952
953 BITMAP bmp;
954 if ( !GetObject(hbmp, sizeof(BITMAP), &bmp) )
955 {
956 wxLogLastError(wxT("GetObject(HBITMAP)"));
957 }
958
959 m_bitmap.SetWidth(bmp.bmWidth);
960 m_bitmap.SetHeight(bmp.bmHeight);
961 m_bitmap.SetDepth(bmp.bmPlanes);
962 }
963
964 m_bitmap.SetHBITMAP((WXHBITMAP)hbmp);
965
966 wxASSERT_MSG( m_bitmap.Ok(), wxT("pasting invalid bitmap") );
967
968 return TRUE;
969}
970
971#endif // 0
972
973// ----------------------------------------------------------------------------
974// wxFileDataObject
975// ----------------------------------------------------------------------------
976
977bool wxFileDataObject::SetData(size_t WXUNUSED(size), const void *pData)
978{
979#ifndef __WXWINCE__
980 m_filenames.Empty();
981
982 // the documentation states that the first member of DROPFILES structure is
983 // a "DWORD offset of double NUL terminated file list". What they mean by
984 // this (I wonder if you see it immediately) is that the list starts at
985 // ((char *)&(pDropFiles.pFiles)) + pDropFiles.pFiles. We're also advised
986 // to use DragQueryFile to work with this structure, but not told where and
987 // how to get HDROP.
988 HDROP hdrop = (HDROP)pData; // NB: it works, but I'm not sure about it
989
990 // get number of files (magic value -1)
991 UINT nFiles = ::DragQueryFile(hdrop, (unsigned)-1, NULL, 0u);
992
993 wxCHECK_MSG ( nFiles != (UINT)-1, FALSE, wxT("wrong HDROP handle") );
994
995 // for each file get the length, allocate memory and then get the name
996 wxString str;
997 UINT len, n;
998 for ( n = 0; n < nFiles; n++ ) {
999 // +1 for terminating NUL
1000 len = ::DragQueryFile(hdrop, n, NULL, 0) + 1;
1001
1002 UINT len2 = ::DragQueryFile(hdrop, n, str.GetWriteBuf(len), len);
1003 str.UngetWriteBuf();
1004 m_filenames.Add(str);
1005
1006 if ( len2 != len - 1 ) {
1007 wxLogDebug(wxT("In wxFileDropTarget::OnDrop DragQueryFile returned\
1008 %d characters, %d expected."), len2, len - 1);
1009 }
1010 }
1011
1012 return TRUE;
1013#else
1014 return FALSE;
1015#endif
1016}
1017
1018void wxFileDataObject::AddFile(const wxString& file)
1019{
1020 // just add file to filenames array
1021 // all useful data (such as DROPFILES struct) will be
1022 // created later as necessary
1023 m_filenames.Add(file);
1024}
1025
1026size_t wxFileDataObject::GetDataSize() const
1027{
1028#ifndef __WXWINCE__
1029 // size returned will be the size of the DROPFILES structure,
1030 // plus the list of filesnames (null byte separated), plus
1031 // a double null at the end
1032
1033 // if no filenames in list, size is 0
1034 if ( m_filenames.GetCount() == 0 )
1035 return 0;
1036
1037 // inital size of DROPFILES struct + null byte
1038 size_t sz = sizeof(DROPFILES) + 1;
1039
1040 size_t count = m_filenames.GetCount();
1041 for ( size_t i = 0; i < count; i++ )
1042 {
1043 // add filename length plus null byte
1044 sz += m_filenames[i].Len() + 1;
1045 }
1046
1047 return sz;
1048#else
1049 return 0;
1050#endif
1051}
1052
1053bool wxFileDataObject::GetDataHere(void *pData) const
1054{
1055#ifndef __WXWINCE__
1056 // pData points to an externally allocated memory block
1057 // created using the size returned by GetDataSize()
1058
1059 // if pData is NULL, or there are no files, return
1060 if ( !pData || m_filenames.GetCount() == 0 )
1061 return FALSE;
1062
1063 // convert data pointer to a DROPFILES struct pointer
1064 LPDROPFILES pDrop = (LPDROPFILES) pData;
1065
1066 // initialize DROPFILES struct
1067 pDrop->pFiles = sizeof(DROPFILES);
1068 pDrop->fNC = FALSE; // not non-client coords
1069#if wxUSE_UNICODE
1070 pDrop->fWide = TRUE;
1071#else // ANSI
1072 pDrop->fWide = FALSE;
1073#endif // Unicode/Ansi
1074
1075 // set start of filenames list (null separated)
1076 wxChar *pbuf = (wxChar*) ((BYTE *)pDrop + sizeof(DROPFILES));
1077
1078 size_t count = m_filenames.GetCount();
1079 for (size_t i = 0; i < count; i++ )
1080 {
1081 // copy filename to pbuf and add null terminator
1082 size_t len = m_filenames[i].Len();
1083 memcpy(pbuf, m_filenames[i], len);
1084 pbuf += len;
1085 *pbuf++ = wxT('\0');
1086 }
1087
1088 // add final null terminator
1089 *pbuf = wxT('\0');
1090
1091 return TRUE;
1092#else
1093 return FALSE;
1094#endif
1095}
1096
1097// ----------------------------------------------------------------------------
1098// wxURLDataObject
1099// ----------------------------------------------------------------------------
1100
1101class CFSTR_SHELLURLDataObject : public wxCustomDataObject
1102{
1103public:
1104 CFSTR_SHELLURLDataObject() : wxCustomDataObject(CFSTR_SHELLURL) {}
1105protected:
1106 virtual size_t GetBufferOffset( const wxDataFormat& WXUNUSED(format) )
1107 {
1108 return 0;
1109 }
1110
1111 virtual const void* GetSizeFromBuffer( const void* buffer, size_t* size,
1112 const wxDataFormat& WXUNUSED(format) )
1113 {
1114 // CFSTR_SHELLURL is _always_ ANSI text
1115 *size = strlen( (const char*)buffer );
1116
1117 return buffer;
1118 }
1119
1120 virtual void* SetSizeInBuffer( void* buffer, size_t WXUNUSED(size),
1121 const wxDataFormat& WXUNUSED(format) )
1122 {
1123 return buffer;
1124 }
1125
1126#if wxUSE_UNICODE
1127 virtual bool GetDataHere( void* buffer ) const
1128 {
1129 // CFSTR_SHELLURL is _always_ ANSI!
1130 wxCharBuffer char_buffer( GetDataSize() );
1131 wxCustomDataObject::GetDataHere( (void*)char_buffer.data() );
1132 wxString unicode_buffer( char_buffer, wxConvLibc );
1133 memcpy( buffer, unicode_buffer.c_str(),
1134 ( unicode_buffer.length() + 1 ) * sizeof(wxChar) );
1135
1136 return TRUE;
1137 }
1138#endif
1139};
1140
1141
1142
1143wxURLDataObject::wxURLDataObject()
1144{
1145 // we support CF_TEXT and CFSTR_SHELLURL formats which are basicly the same
1146 // but it seems that some browsers only provide one of them so we have to
1147 // support both
1148 Add(new wxTextDataObject);
1149 Add(new CFSTR_SHELLURLDataObject());
1150
1151 // we don't have any data yet
1152 m_dataObjectLast = NULL;
1153}
1154
1155bool wxURLDataObject::SetData(const wxDataFormat& format,
1156 size_t len,
1157 const void *buf)
1158{
1159 m_dataObjectLast = GetObject(format);
1160
1161 wxCHECK_MSG( m_dataObjectLast, FALSE,
1162 wxT("unsupported format in wxURLDataObject"));
1163
1164 return m_dataObjectLast->SetData(len, buf);
1165}
1166
1167wxString wxURLDataObject::GetURL() const
1168{
1169 wxString url;
1170 wxCHECK_MSG( m_dataObjectLast, url, _T("no data in wxURLDataObject") );
1171
1172 size_t len = m_dataObjectLast->GetDataSize();
1173
1174 m_dataObjectLast->GetDataHere(url.GetWriteBuf(len));
1175 url.UngetWriteBuf();
1176
1177 return url;
1178}
1179
1180void wxURLDataObject::SetURL(const wxString& url)
1181{
1182 SetData(wxDataFormat(wxUSE_UNICODE ? wxDF_UNICODETEXT : wxDF_TEXT),
1183 url.Length()+1, url.c_str());
1184
1185 // CFSTR_SHELLURL is always supposed to be ANSI...
1186 wxWX2MBbuf urlA = (wxWX2MBbuf)url.mbc_str();
1187 size_t len = strlen(urlA);
1188 SetData(wxDataFormat(CFSTR_SHELLURL), len+1, (const char*)urlA);
1189}
1190
1191// ----------------------------------------------------------------------------
1192// private functions
1193// ----------------------------------------------------------------------------
1194
1195#ifdef __WXDEBUG__
1196
1197static const wxChar *GetTymedName(DWORD tymed)
1198{
1199 static wxChar s_szBuf[128];
1200 switch ( tymed ) {
1201 case TYMED_HGLOBAL: return wxT("TYMED_HGLOBAL");
1202 case TYMED_FILE: return wxT("TYMED_FILE");
1203 case TYMED_ISTREAM: return wxT("TYMED_ISTREAM");
1204 case TYMED_ISTORAGE: return wxT("TYMED_ISTORAGE");
1205 case TYMED_GDI: return wxT("TYMED_GDI");
1206 case TYMED_MFPICT: return wxT("TYMED_MFPICT");
1207 case TYMED_ENHMF: return wxT("TYMED_ENHMF");
1208 default:
1209 wxSprintf(s_szBuf, wxT("type of media format %ld (unknown)"), tymed);
1210 return s_szBuf;
1211 }
1212}
1213
1214#endif // Debug
1215
1216#else // not using OLE at all
1217// ----------------------------------------------------------------------------
1218// wxDataObject
1219// ----------------------------------------------------------------------------
1220
1221wxDataObject::wxDataObject()
1222{
1223}
1224
1225wxDataObject::~wxDataObject()
1226{
1227}
1228
1229void wxDataObject::SetAutoDelete()
1230{
1231}
1232
1233#ifdef __WXDEBUG__
1234const wxChar *wxDataObject::GetFormatName(wxDataFormat format)
1235{
1236 return NULL;
1237}
1238#endif
1239
1240#endif
1241