]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/ole/dataobj.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/ole/dataobj.cpp
3 // Purpose: implementation of wx[I]DataObject class
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
23 #if defined(__BORLANDC__)
31 #include "wx/wxcrtvararg.h"
34 #include "wx/dataobj.h"
36 #if wxUSE_OLE && defined(__WIN32__) && !defined(__GNUWIN32_OLD__)
38 #include "wx/msw/private.h" // includes <windows.h>
44 // for some compilers, the entire ole2.h must be included, not only oleauto.h
45 #if wxUSE_NORLANDER_HEADERS || defined(__WATCOMC__) || defined(__WXWINCE__)
52 #include "wx/msw/ole/oleutils.h"
54 #include "wx/msw/dib.h"
56 #ifndef CFSTR_SHELLURL
57 #define CFSTR_SHELLURL wxT("UniformResourceLocator")
60 // ----------------------------------------------------------------------------
62 // ----------------------------------------------------------------------------
65 static const wxChar
*GetTymedName(DWORD tymed
);
66 #else // !wxDEBUG_LEVEL
67 #define GetTymedName(tymed) wxEmptyString
68 #endif // wxDEBUG_LEVEL/!wxDEBUG_LEVEL
70 wxDataFormat
HtmlFormatFixup(wxDataFormat format
)
72 // Since the HTML format is dynamically registered, the wxDF_HTML
73 // format does not match the native constant in the way other formats do,
74 // so for the format checks below to work, we must change the native
75 // id to the wxDF_HTML constant.
77 if (::GetClipboardFormatName(format
, s_szBuf
, WXSIZEOF(s_szBuf
)))
79 if (s_szBuf
== wxString("HTML Format"))
85 // ----------------------------------------------------------------------------
86 // wxIEnumFORMATETC interface implementation
87 // ----------------------------------------------------------------------------
89 class wxIEnumFORMATETC
: public IEnumFORMATETC
92 wxIEnumFORMATETC(const wxDataFormat
* formats
, ULONG nCount
);
93 virtual ~wxIEnumFORMATETC() { delete [] m_formats
; }
96 STDMETHODIMP
Next(ULONG celt
, FORMATETC
*rgelt
, ULONG
*pceltFetched
);
97 STDMETHODIMP
Skip(ULONG celt
);
99 STDMETHODIMP
Clone(IEnumFORMATETC
**ppenum
);
101 DECLARE_IUNKNOWN_METHODS
;
104 CLIPFORMAT
*m_formats
; // formats we can provide data in
105 ULONG m_nCount
, // number of formats we support
106 m_nCurrent
; // current enum position
108 wxDECLARE_NO_COPY_CLASS(wxIEnumFORMATETC
);
111 // ----------------------------------------------------------------------------
112 // wxIDataObject implementation of IDataObject interface
113 // ----------------------------------------------------------------------------
115 class wxIDataObject
: public IDataObject
118 wxIDataObject(wxDataObject
*pDataObject
);
119 virtual ~wxIDataObject();
121 // normally, wxDataObject controls our lifetime (i.e. we're deleted when it
122 // is), but in some cases, the situation is reversed, that is we delete it
123 // when this object is deleted - setting this flag enables such logic
124 void SetDeleteFlag() { m_mustDelete
= true; }
127 STDMETHODIMP
GetData(FORMATETC
*pformatetcIn
, STGMEDIUM
*pmedium
);
128 STDMETHODIMP
GetDataHere(FORMATETC
*pformatetc
, STGMEDIUM
*pmedium
);
129 STDMETHODIMP
QueryGetData(FORMATETC
*pformatetc
);
130 STDMETHODIMP
GetCanonicalFormatEtc(FORMATETC
*In
, FORMATETC
*pOut
);
131 STDMETHODIMP
SetData(FORMATETC
*pfetc
, STGMEDIUM
*pmedium
, BOOL fRelease
);
132 STDMETHODIMP
EnumFormatEtc(DWORD dwDirection
, IEnumFORMATETC
**ppenumFEtc
);
133 STDMETHODIMP
DAdvise(FORMATETC
*pfetc
, DWORD ad
, IAdviseSink
*p
, DWORD
*pdw
);
134 STDMETHODIMP
DUnadvise(DWORD dwConnection
);
135 STDMETHODIMP
EnumDAdvise(IEnumSTATDATA
**ppenumAdvise
);
137 DECLARE_IUNKNOWN_METHODS
;
140 wxDataObject
*m_pDataObject
; // pointer to C++ class we belong to
144 wxDECLARE_NO_COPY_CLASS(wxIDataObject
);
147 // ============================================================================
149 // ============================================================================
151 // ----------------------------------------------------------------------------
153 // ----------------------------------------------------------------------------
155 void wxDataFormat::SetId(const wxString
& format
)
157 m_format
= (wxDataFormat::NativeFormat
)::RegisterClipboardFormat(format
.t_str());
160 wxLogError(_("Couldn't register clipboard format '%s'."), format
);
164 wxString
wxDataFormat::GetId() const
166 static const int max
= 256;
170 wxCHECK_MSG( !IsStandard(), s
,
171 wxT("name of predefined format cannot be retrieved") );
173 int len
= ::GetClipboardFormatName(m_format
, wxStringBuffer(s
, max
), max
);
177 wxLogError(_("The clipboard format '%d' doesn't exist."), m_format
);
178 return wxEmptyString
;
184 // ----------------------------------------------------------------------------
186 // ----------------------------------------------------------------------------
188 BEGIN_IID_TABLE(wxIEnumFORMATETC
)
190 ADD_IID(EnumFORMATETC
)
193 IMPLEMENT_IUNKNOWN_METHODS(wxIEnumFORMATETC
)
195 wxIEnumFORMATETC::wxIEnumFORMATETC(const wxDataFormat
*formats
, ULONG nCount
)
199 m_formats
= new CLIPFORMAT
[nCount
];
200 for ( ULONG n
= 0; n
< nCount
; n
++ ) {
201 if (formats
[n
].GetFormatId() != wxDF_HTML
)
202 m_formats
[n
] = formats
[n
].GetFormatId();
204 m_formats
[n
] = ::RegisterClipboardFormat(wxT("HTML Format"));
208 STDMETHODIMP
wxIEnumFORMATETC::Next(ULONG celt
,
212 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIEnumFORMATETC::Next"));
214 ULONG numFetched
= 0;
215 while (m_nCurrent
< m_nCount
&& numFetched
< celt
) {
217 format
.cfFormat
= m_formats
[m_nCurrent
++];
219 format
.dwAspect
= DVASPECT_CONTENT
;
221 format
.tymed
= TYMED_HGLOBAL
;
228 *pceltFetched
= numFetched
;
230 return numFetched
== celt
? S_OK
: S_FALSE
;
233 STDMETHODIMP
wxIEnumFORMATETC::Skip(ULONG celt
)
235 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIEnumFORMATETC::Skip"));
238 if ( m_nCurrent
< m_nCount
)
241 // no, can't skip this many elements
247 STDMETHODIMP
wxIEnumFORMATETC::Reset()
249 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIEnumFORMATETC::Reset"));
256 STDMETHODIMP
wxIEnumFORMATETC::Clone(IEnumFORMATETC
**ppenum
)
258 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIEnumFORMATETC::Clone"));
260 // unfortunately, we can't reuse the code in ctor - types are different
261 wxIEnumFORMATETC
*pNew
= new wxIEnumFORMATETC(NULL
, 0);
262 pNew
->m_nCount
= m_nCount
;
263 pNew
->m_formats
= new CLIPFORMAT
[m_nCount
];
264 for ( ULONG n
= 0; n
< m_nCount
; n
++ ) {
265 pNew
->m_formats
[n
] = m_formats
[n
];
273 // ----------------------------------------------------------------------------
275 // ----------------------------------------------------------------------------
277 BEGIN_IID_TABLE(wxIDataObject
)
282 IMPLEMENT_IUNKNOWN_METHODS(wxIDataObject
)
284 wxIDataObject::wxIDataObject(wxDataObject
*pDataObject
)
286 m_pDataObject
= pDataObject
;
287 m_mustDelete
= false;
290 wxIDataObject::~wxIDataObject()
294 delete m_pDataObject
;
298 // get data functions
299 STDMETHODIMP
wxIDataObject::GetData(FORMATETC
*pformatetcIn
, STGMEDIUM
*pmedium
)
301 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::GetData"));
303 // is data is in our format?
304 HRESULT hr
= QueryGetData(pformatetcIn
);
308 // for the bitmaps and metafiles we use the handles instead of global memory
310 wxDataFormat format
= (wxDataFormat::NativeFormat
)pformatetcIn
->cfFormat
;
311 format
= HtmlFormatFixup(format
);
316 pmedium
->tymed
= TYMED_GDI
;
319 case wxDF_ENHMETAFILE
:
320 pmedium
->tymed
= TYMED_ENHMF
;
325 pmedium
->hGlobal
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_SHARE
,
326 sizeof(METAFILEPICT
));
327 if ( !pmedium
->hGlobal
) {
328 wxLogLastError(wxT("GlobalAlloc"));
329 return E_OUTOFMEMORY
;
331 pmedium
->tymed
= TYMED_MFPICT
;
336 size_t size
= m_pDataObject
->GetDataSize(format
);
338 // it probably means that the method is just not implemented
339 wxLogDebug(wxT("Invalid data size - can't be 0"));
341 return DV_E_FORMATETC
;
344 // we may need extra space for the buffer size
345 size
+= m_pDataObject
->GetBufferOffset( format
);
347 HGLOBAL hGlobal
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_SHARE
, size
);
348 if ( hGlobal
== NULL
) {
349 wxLogLastError(wxT("GlobalAlloc"));
350 return E_OUTOFMEMORY
;
354 pmedium
->tymed
= TYMED_HGLOBAL
;
355 pmedium
->hGlobal
= hGlobal
;
358 pmedium
->pUnkForRelease
= NULL
;
361 hr
= GetDataHere(pformatetcIn
, pmedium
);
363 // free resources we allocated
364 if ( pmedium
->tymed
& (TYMED_HGLOBAL
| TYMED_MFPICT
) ) {
365 GlobalFree(pmedium
->hGlobal
);
374 STDMETHODIMP
wxIDataObject::GetDataHere(FORMATETC
*pformatetc
,
377 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::GetDataHere"));
379 // put data in caller provided medium
380 switch ( pmedium
->tymed
)
383 if ( !m_pDataObject
->GetDataHere(wxDF_BITMAP
, &pmedium
->hBitmap
) )
388 if ( !m_pDataObject
->GetDataHere(wxDF_ENHMETAFILE
,
389 &pmedium
->hEnhMetaFile
) )
394 // fall through - we pass METAFILEPICT through HGLOBAL
399 HGLOBAL hGlobal
= pmedium
->hGlobal
;
400 void *pBuf
= GlobalLock(hGlobal
);
401 if ( pBuf
== NULL
) {
402 wxLogLastError(wxT("GlobalLock"));
403 return E_OUTOFMEMORY
;
406 wxDataFormat format
= pformatetc
->cfFormat
;
408 // possibly put the size in the beginning of the buffer
409 pBuf
= m_pDataObject
->SetSizeInBuffer
412 ::GlobalSize(hGlobal
),
416 if ( !m_pDataObject
->GetDataHere(format
, pBuf
) )
419 GlobalUnlock(hGlobal
);
431 // set data functions
432 STDMETHODIMP
wxIDataObject::SetData(FORMATETC
*pformatetc
,
436 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::SetData"));
438 switch ( pmedium
->tymed
)
441 m_pDataObject
->SetData(wxDF_BITMAP
, 0, &pmedium
->hBitmap
);
445 m_pDataObject
->SetData(wxDF_ENHMETAFILE
, 0, &pmedium
->hEnhMetaFile
);
449 // fall through - we pass METAFILEPICT through HGLOBAL
452 wxDataFormat format
= pformatetc
->cfFormat
;
454 format
= HtmlFormatFixup(format
);
456 // this is quite weird, but for file drag and drop, explorer
457 // calls our SetData() with the formats we do *not* support!
459 // as we can't fix this bug in explorer (it's a bug because it
460 // should only use formats returned by EnumFormatEtc), do the
462 if ( !m_pDataObject
->IsSupported(format
, wxDataObject::Set
) ) {
464 return DV_E_FORMATETC
;
468 const void *pBuf
= GlobalLock(pmedium
->hGlobal
);
469 if ( pBuf
== NULL
) {
470 wxLogLastError(wxT("GlobalLock"));
472 return E_OUTOFMEMORY
;
475 // we've got a problem with SetData() here because the base
476 // class version requires the size parameter which we don't
477 // have anywhere in OLE data transfer - so we need to
478 // synthetise it for known formats and we suppose that all data
479 // in custom formats starts with a DWORD containing the size
486 size
= strlen((const char *)pBuf
);
488 #if !(defined(__BORLANDC__) && (__BORLANDC__ < 0x500))
490 #if ( defined(__BORLANDC__) && (__BORLANDC__ > 0x530) )
491 size
= std::wcslen((const wchar_t *)pBuf
) * sizeof(wchar_t);
493 size
= wxWcslen((const wchar_t *)pBuf
) * sizeof(wchar_t);
500 // these formats don't use size at all, anyhow (but
501 // pass data by handle, which is always a single DWORD)
507 // the handler will calculate size itself (it's too
508 // complicated to do it here)
513 case CF_METAFILEPICT
:
514 size
= sizeof(METAFILEPICT
);
518 pBuf
= m_pDataObject
->
519 GetSizeFromBuffer(pBuf
, &size
, format
);
520 size
-= m_pDataObject
->GetBufferOffset(format
);
523 bool ok
= m_pDataObject
->SetData(format
, size
, pBuf
);
525 GlobalUnlock(pmedium
->hGlobal
);
538 // we own the medium, so we must release it - but do *not* free any
539 // data we pass by handle because we have copied it elsewhere
540 switch ( pmedium
->tymed
)
543 pmedium
->hBitmap
= 0;
547 pmedium
->hMetaFilePict
= 0;
551 pmedium
->hEnhMetaFile
= 0;
555 ReleaseStgMedium(pmedium
);
561 // information functions
562 STDMETHODIMP
wxIDataObject::QueryGetData(FORMATETC
*pformatetc
)
564 // do we accept data in this format?
565 if ( pformatetc
== NULL
) {
566 wxLogTrace(wxTRACE_OleCalls
,
567 wxT("wxIDataObject::QueryGetData: invalid ptr."));
572 // the only one allowed by current COM implementation
573 if ( pformatetc
->lindex
!= -1 ) {
574 wxLogTrace(wxTRACE_OleCalls
,
575 wxT("wxIDataObject::QueryGetData: bad lindex %ld"),
581 // we don't support anything other (THUMBNAIL, ICON, DOCPRINT...)
582 if ( pformatetc
->dwAspect
!= DVASPECT_CONTENT
) {
583 wxLogTrace(wxTRACE_OleCalls
,
584 wxT("wxIDataObject::QueryGetData: bad dwAspect %ld"),
585 pformatetc
->dwAspect
);
587 return DV_E_DVASPECT
;
590 // and now check the type of data requested
591 wxDataFormat format
= pformatetc
->cfFormat
;
592 format
= HtmlFormatFixup(format
);
594 if ( m_pDataObject
->IsSupportedFormat(format
) ) {
595 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::QueryGetData: %s ok"),
596 wxGetFormatName(format
));
599 wxLogTrace(wxTRACE_OleCalls
,
600 wxT("wxIDataObject::QueryGetData: %s unsupported"),
601 wxGetFormatName(format
));
603 return DV_E_FORMATETC
;
606 // we only transfer data by global memory, except for some particular cases
607 DWORD tymed
= pformatetc
->tymed
;
608 if ( (format
== wxDF_BITMAP
&& !(tymed
& TYMED_GDI
)) &&
609 !(tymed
& TYMED_HGLOBAL
) ) {
610 // it's not what we're waiting for
611 wxLogTrace(wxTRACE_OleCalls
,
612 wxT("wxIDataObject::QueryGetData: %s != %s"),
614 GetTymedName(format
== wxDF_BITMAP
? TYMED_GDI
623 STDMETHODIMP
wxIDataObject::GetCanonicalFormatEtc(FORMATETC
*WXUNUSED(pFormatetcIn
),
624 FORMATETC
*pFormatetcOut
)
626 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::GetCanonicalFormatEtc"));
628 // TODO we might want something better than this trivial implementation here
629 if ( pFormatetcOut
!= NULL
)
630 pFormatetcOut
->ptd
= NULL
;
632 return DATA_S_SAMEFORMATETC
;
635 STDMETHODIMP
wxIDataObject::EnumFormatEtc(DWORD dwDir
,
636 IEnumFORMATETC
**ppenumFormatEtc
)
638 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::EnumFormatEtc"));
640 wxDataObject::Direction dir
= dwDir
== DATADIR_GET
? wxDataObject::Get
643 ULONG nFormatCount
= wx_truncate_cast(ULONG
, m_pDataObject
->GetFormatCount(dir
));
645 wxDataFormat
*formats
;
646 formats
= nFormatCount
== 1 ? &format
: new wxDataFormat
[nFormatCount
];
647 m_pDataObject
->GetAllFormats(formats
, dir
);
649 wxIEnumFORMATETC
*pEnum
= new wxIEnumFORMATETC(formats
, nFormatCount
);
651 *ppenumFormatEtc
= pEnum
;
653 if ( formats
!= &format
) {
660 // ----------------------------------------------------------------------------
661 // advise sink functions (not implemented)
662 // ----------------------------------------------------------------------------
664 STDMETHODIMP
wxIDataObject::DAdvise(FORMATETC
*WXUNUSED(pformatetc
),
665 DWORD
WXUNUSED(advf
),
666 IAdviseSink
*WXUNUSED(pAdvSink
),
667 DWORD
*WXUNUSED(pdwConnection
))
669 return OLE_E_ADVISENOTSUPPORTED
;
672 STDMETHODIMP
wxIDataObject::DUnadvise(DWORD
WXUNUSED(dwConnection
))
674 return OLE_E_ADVISENOTSUPPORTED
;
677 STDMETHODIMP
wxIDataObject::EnumDAdvise(IEnumSTATDATA
**WXUNUSED(ppenumAdvise
))
679 return OLE_E_ADVISENOTSUPPORTED
;
682 // ----------------------------------------------------------------------------
684 // ----------------------------------------------------------------------------
686 wxDataObject::wxDataObject()
688 m_pIDataObject
= new wxIDataObject(this);
689 m_pIDataObject
->AddRef();
692 wxDataObject::~wxDataObject()
694 ReleaseInterface(m_pIDataObject
);
697 void wxDataObject::SetAutoDelete()
699 ((wxIDataObject
*)m_pIDataObject
)->SetDeleteFlag();
700 m_pIDataObject
->Release();
702 // so that the dtor doesnt' crash
703 m_pIDataObject
= NULL
;
706 size_t wxDataObject::GetBufferOffset(const wxDataFormat
& format
)
708 // if we prepend the size of the data to the buffer itself, account for it
709 return NeedsVerbatimData(format
) ? 0 : sizeof(size_t);
712 const void *wxDataObject::GetSizeFromBuffer(const void *buffer
,
714 const wxDataFormat
& WXUNUSED(format
))
716 // hack: the third parameter is declared non-const in Wine's headers so
717 // cast away the const
718 const size_t realsz
= ::HeapSize(::GetProcessHeap(), 0,
719 const_cast<void*>(buffer
));
720 if ( realsz
== (size_t)-1 )
722 // note that HeapSize() does not set last error
723 wxLogApiError(wxT("HeapSize"), 0);
732 void* wxDataObject::SetSizeInBuffer( void* buffer
, size_t size
,
733 const wxDataFormat
& format
)
735 size_t* p
= (size_t *)buffer
;
736 if ( !NeedsVerbatimData(format
) )
738 // prepend the size to the data and skip it
747 const wxChar
*wxDataObject::GetFormatName(wxDataFormat format
)
749 // case 'xxx' is not a valid value for switch of enum 'wxDataFormat'
751 #pragma warning(disable:4063)
754 static wxChar s_szBuf
[256];
756 case CF_TEXT
: return wxT("CF_TEXT");
757 case CF_BITMAP
: return wxT("CF_BITMAP");
758 case CF_SYLK
: return wxT("CF_SYLK");
759 case CF_DIF
: return wxT("CF_DIF");
760 case CF_TIFF
: return wxT("CF_TIFF");
761 case CF_OEMTEXT
: return wxT("CF_OEMTEXT");
762 case CF_DIB
: return wxT("CF_DIB");
763 case CF_PALETTE
: return wxT("CF_PALETTE");
764 case CF_PENDATA
: return wxT("CF_PENDATA");
765 case CF_RIFF
: return wxT("CF_RIFF");
766 case CF_WAVE
: return wxT("CF_WAVE");
767 case CF_UNICODETEXT
: return wxT("CF_UNICODETEXT");
769 case CF_METAFILEPICT
: return wxT("CF_METAFILEPICT");
770 case CF_ENHMETAFILE
: return wxT("CF_ENHMETAFILE");
771 case CF_LOCALE
: return wxT("CF_LOCALE");
772 case CF_HDROP
: return wxT("CF_HDROP");
776 if ( !::GetClipboardFormatName(format
, s_szBuf
, WXSIZEOF(s_szBuf
)) )
778 // it must be a new predefined format we don't know the name of
779 wxSprintf(s_szBuf
, wxT("unknown CF (0x%04x)"), format
.GetFormatId());
786 #pragma warning(default:4063)
790 #endif // wxDEBUG_LEVEL
792 // ----------------------------------------------------------------------------
793 // wxBitmapDataObject supports CF_DIB format
794 // ----------------------------------------------------------------------------
796 // TODO: support CF_DIB under Windows CE as well
798 size_t wxBitmapDataObject::GetDataSize() const
800 #if wxUSE_WXDIB && !defined(__WXWINCE__)
801 return wxDIB::ConvertFromBitmap(NULL
, GetHbitmapOf(GetBitmap()));
807 bool wxBitmapDataObject::GetDataHere(void *buf
) const
809 #if wxUSE_WXDIB && !defined(__WXWINCE__)
810 BITMAPINFO
* const pbi
= (BITMAPINFO
*)buf
;
812 return wxDIB::ConvertFromBitmap(pbi
, GetHbitmapOf(GetBitmap())) != 0;
819 bool wxBitmapDataObject::SetData(size_t WXUNUSED(len
), const void *buf
)
821 #if wxUSE_WXDIB && !defined(__WXWINCE__)
822 const BITMAPINFO
* const pbmi
= (const BITMAPINFO
*)buf
;
824 HBITMAP hbmp
= wxDIB::ConvertToBitmap(pbmi
);
826 wxCHECK_MSG( hbmp
, FALSE
, wxT("pasting/dropping invalid bitmap") );
828 const BITMAPINFOHEADER
* const pbmih
= &pbmi
->bmiHeader
;
829 wxBitmap
bitmap(pbmih
->biWidth
, pbmih
->biHeight
, pbmih
->biBitCount
);
830 bitmap
.SetHBITMAP((WXHBITMAP
)hbmp
);
832 // TODO: create wxPalette if the bitmap has any
843 // ----------------------------------------------------------------------------
844 // wxBitmapDataObject2 supports CF_BITMAP format
845 // ----------------------------------------------------------------------------
847 // the bitmaps aren't passed by value as other types of data (i.e. by copying
848 // the data into a global memory chunk and passing it to the clipboard or
849 // another application or whatever), but by handle, so these generic functions
850 // don't make much sense to them.
852 size_t wxBitmapDataObject2::GetDataSize() const
857 bool wxBitmapDataObject2::GetDataHere(void *pBuf
) const
859 // we put a bitmap handle into pBuf
860 *(WXHBITMAP
*)pBuf
= GetBitmap().GetHBITMAP();
865 bool wxBitmapDataObject2::SetData(size_t WXUNUSED(len
), const void *pBuf
)
867 HBITMAP hbmp
= *(HBITMAP
*)pBuf
;
870 if ( !GetObject(hbmp
, sizeof(BITMAP
), &bmp
) )
872 wxLogLastError(wxT("GetObject(HBITMAP)"));
875 wxBitmap
bitmap(bmp
.bmWidth
, bmp
.bmHeight
, bmp
.bmPlanes
);
876 bitmap
.SetHBITMAP((WXHBITMAP
)hbmp
);
878 if ( !bitmap
.IsOk() ) {
879 wxFAIL_MSG(wxT("pasting/dropping invalid bitmap"));
891 size_t wxBitmapDataObject::GetDataSize(const wxDataFormat
& format
) const
893 if ( format
.GetFormatId() == CF_DIB
)
898 // shouldn't be selected into a DC or GetDIBits() would fail
899 wxASSERT_MSG( !m_bitmap
.GetSelectedInto(),
900 wxT("can't copy bitmap selected into wxMemoryDC") );
902 // first get the info
904 if ( !GetDIBits(hdc
, (HBITMAP
)m_bitmap
.GetHBITMAP(), 0, 0,
905 NULL
, &bi
, DIB_RGB_COLORS
) )
907 wxLogLastError(wxT("GetDIBits(NULL)"));
912 return sizeof(BITMAPINFO
) + bi
.bmiHeader
.biSizeImage
;
916 // no data to copy - we don't pass HBITMAP via global memory
921 bool wxBitmapDataObject::GetDataHere(const wxDataFormat
& format
,
924 wxASSERT_MSG( m_bitmap
.IsOk(), wxT("copying invalid bitmap") );
926 HBITMAP hbmp
= (HBITMAP
)m_bitmap
.GetHBITMAP();
927 if ( format
.GetFormatId() == CF_DIB
)
932 // shouldn't be selected into a DC or GetDIBits() would fail
933 wxASSERT_MSG( !m_bitmap
.GetSelectedInto(),
934 wxT("can't copy bitmap selected into wxMemoryDC") );
936 // first get the info
937 BITMAPINFO
*pbi
= (BITMAPINFO
*)pBuf
;
938 if ( !GetDIBits(hdc
, hbmp
, 0, 0, NULL
, pbi
, DIB_RGB_COLORS
) )
940 wxLogLastError(wxT("GetDIBits(NULL)"));
945 // and now copy the bits
946 if ( !GetDIBits(hdc
, hbmp
, 0, pbi
->bmiHeader
.biHeight
, pbi
+ 1,
947 pbi
, DIB_RGB_COLORS
) )
949 wxLogLastError(wxT("GetDIBits"));
956 // we put a bitmap handle into pBuf
957 *(HBITMAP
*)pBuf
= hbmp
;
963 bool wxBitmapDataObject::SetData(const wxDataFormat
& format
,
964 size_t size
, const void *pBuf
)
967 if ( format
.GetFormatId() == CF_DIB
)
969 // here we get BITMAPINFO struct followed by the actual bitmap bits and
970 // BITMAPINFO starts with BITMAPINFOHEADER followed by colour info
973 BITMAPINFO
*pbmi
= (BITMAPINFO
*)pBuf
;
974 BITMAPINFOHEADER
*pbmih
= &pbmi
->bmiHeader
;
975 hbmp
= CreateDIBitmap(hdc
, pbmih
, CBM_INIT
,
976 pbmi
+ 1, pbmi
, DIB_RGB_COLORS
);
979 wxLogLastError(wxT("CreateDIBitmap"));
982 m_bitmap
.SetWidth(pbmih
->biWidth
);
983 m_bitmap
.SetHeight(pbmih
->biHeight
);
987 // it's easy with bitmaps: we pass them by handle
988 hbmp
= *(HBITMAP
*)pBuf
;
991 if ( !GetObject(hbmp
, sizeof(BITMAP
), &bmp
) )
993 wxLogLastError(wxT("GetObject(HBITMAP)"));
996 m_bitmap
.SetWidth(bmp
.bmWidth
);
997 m_bitmap
.SetHeight(bmp
.bmHeight
);
998 m_bitmap
.SetDepth(bmp
.bmPlanes
);
1001 m_bitmap
.SetHBITMAP((WXHBITMAP
)hbmp
);
1003 wxASSERT_MSG( m_bitmap
.IsOk(), wxT("pasting invalid bitmap") );
1010 // ----------------------------------------------------------------------------
1012 // ----------------------------------------------------------------------------
1014 bool wxFileDataObject::SetData(size_t WXUNUSED(size
),
1015 const void *WXUNUSED_IN_WINCE(pData
))
1018 m_filenames
.Empty();
1020 // the documentation states that the first member of DROPFILES structure is
1021 // a "DWORD offset of double NUL terminated file list". What they mean by
1022 // this (I wonder if you see it immediately) is that the list starts at
1023 // ((char *)&(pDropFiles.pFiles)) + pDropFiles.pFiles. We're also advised
1024 // to use DragQueryFile to work with this structure, but not told where and
1025 // how to get HDROP.
1026 HDROP hdrop
= (HDROP
)pData
; // NB: it works, but I'm not sure about it
1028 // get number of files (magic value -1)
1029 UINT nFiles
= ::DragQueryFile(hdrop
, (unsigned)-1, NULL
, 0u);
1031 wxCHECK_MSG ( nFiles
!= (UINT
)-1, FALSE
, wxT("wrong HDROP handle") );
1033 // for each file get the length, allocate memory and then get the name
1036 for ( n
= 0; n
< nFiles
; n
++ ) {
1037 // +1 for terminating NUL
1038 len
= ::DragQueryFile(hdrop
, n
, NULL
, 0) + 1;
1040 UINT len2
= ::DragQueryFile(hdrop
, n
, wxStringBuffer(str
, len
), len
);
1041 m_filenames
.Add(str
);
1043 if ( len2
!= len
- 1 ) {
1044 wxLogDebug(wxT("In wxFileDropTarget::OnDrop DragQueryFile returned\
1045 %d characters, %d expected."), len2
, len
- 1);
1055 void wxFileDataObject::AddFile(const wxString
& file
)
1057 // just add file to filenames array
1058 // all useful data (such as DROPFILES struct) will be
1059 // created later as necessary
1060 m_filenames
.Add(file
);
1063 size_t wxFileDataObject::GetDataSize() const
1066 // size returned will be the size of the DROPFILES structure, plus the list
1067 // of filesnames (null byte separated), plus a double null at the end
1069 // if no filenames in list, size is 0
1070 if ( m_filenames
.empty() )
1073 #if wxUSE_UNICODE_MSLU
1075 if ( wxGetOsVersion() == wxOS_WINDOWS_9X
)
1077 // Win9x always uses ANSI file names and MSLU doesn't help with this
1078 sizeOfChar
= sizeof(char);
1082 sizeOfChar
= sizeof(wxChar
);
1084 #else // !wxUSE_UNICODE_MSLU
1085 static const size_t sizeOfChar
= sizeof(wxChar
);
1086 #endif // wxUSE_UNICODE_MSLU/!wxUSE_UNICODE_MSLU
1088 // inital size of DROPFILES struct + null byte
1089 size_t sz
= sizeof(DROPFILES
) + sizeOfChar
;
1091 const size_t count
= m_filenames
.size();
1092 for ( size_t i
= 0; i
< count
; i
++ )
1094 // add filename length plus null byte
1096 #if wxUSE_UNICODE_MSLU
1097 if ( sizeOfChar
== sizeof(char) )
1098 len
= strlen(m_filenames
[i
].mb_str(*wxConvFileName
));
1100 #endif // wxUSE_UNICODE_MSLU
1101 len
= m_filenames
[i
].length();
1103 sz
+= (len
+ 1) * sizeOfChar
;
1112 bool wxFileDataObject::GetDataHere(void *WXUNUSED_IN_WINCE(pData
)) const
1115 // pData points to an externally allocated memory block
1116 // created using the size returned by GetDataSize()
1118 // if pData is NULL, or there are no files, return
1119 if ( !pData
|| m_filenames
.empty() )
1122 // convert data pointer to a DROPFILES struct pointer
1123 LPDROPFILES pDrop
= (LPDROPFILES
) pData
;
1125 // initialize DROPFILES struct
1126 pDrop
->pFiles
= sizeof(DROPFILES
);
1127 pDrop
->fNC
= FALSE
; // not non-client coords
1128 #if wxUSE_UNICODE_MSLU
1129 pDrop
->fWide
= wxGetOsVersion() != wxOS_WINDOWS_9X
? TRUE
: FALSE
;
1131 pDrop
->fWide
= wxUSE_UNICODE
;
1134 const size_t sizeOfChar
= pDrop
->fWide
? sizeof(wchar_t) : sizeof(char);
1136 // set start of filenames list (null separated)
1137 BYTE
*pbuf
= (BYTE
*)(pDrop
+ 1);
1139 const size_t count
= m_filenames
.size();
1140 for ( size_t i
= 0; i
< count
; i
++ )
1142 // copy filename to pbuf and add null terminator
1144 #if wxUSE_UNICODE_MSLU
1145 if ( sizeOfChar
== sizeof(char) )
1147 wxCharBuffer
buf(m_filenames
[i
].mb_str(*wxConvFileName
));
1149 memcpy(pbuf
, buf
, len
*sizeOfChar
);
1152 #endif // wxUSE_UNICODE_MSLU
1154 len
= m_filenames
[i
].length();
1155 memcpy(pbuf
, m_filenames
[i
].t_str(), len
*sizeOfChar
);
1158 pbuf
+= len
*sizeOfChar
;
1160 memset(pbuf
, 0, sizeOfChar
);
1164 // add final null terminator
1165 memset(pbuf
, 0, sizeOfChar
);
1173 // ----------------------------------------------------------------------------
1175 // ----------------------------------------------------------------------------
1177 // Work around bug in Wine headers
1178 #if defined(__WINE__) && defined(CFSTR_SHELLURL) && wxUSE_UNICODE
1179 #undef CFSTR_SHELLURL
1180 #define CFSTR_SHELLURL wxT("CFSTR_SHELLURL")
1183 class CFSTR_SHELLURLDataObject
: public wxCustomDataObject
1186 CFSTR_SHELLURLDataObject() : wxCustomDataObject(CFSTR_SHELLURL
) {}
1188 virtual size_t GetBufferOffset( const wxDataFormat
& WXUNUSED(format
) )
1193 virtual const void* GetSizeFromBuffer( const void* buffer
, size_t* size
,
1194 const wxDataFormat
& WXUNUSED(format
) )
1196 // CFSTR_SHELLURL is _always_ ANSI text
1197 *size
= strlen( (const char*)buffer
);
1202 virtual void* SetSizeInBuffer( void* buffer
, size_t WXUNUSED(size
),
1203 const wxDataFormat
& WXUNUSED(format
) )
1208 wxDECLARE_NO_COPY_CLASS(CFSTR_SHELLURLDataObject
);
1213 wxURLDataObject::wxURLDataObject(const wxString
& url
)
1215 // we support CF_TEXT and CFSTR_SHELLURL formats which are basically the
1216 // same but it seems that some browsers only provide one of them so we have
1218 Add(new wxTextDataObject
);
1219 Add(new CFSTR_SHELLURLDataObject());
1221 // we don't have any data yet
1222 m_dataObjectLast
= NULL
;
1228 bool wxURLDataObject::SetData(const wxDataFormat
& format
,
1232 m_dataObjectLast
= GetObject(format
);
1234 wxCHECK_MSG( m_dataObjectLast
, FALSE
,
1235 wxT("unsupported format in wxURLDataObject"));
1237 return m_dataObjectLast
->SetData(len
, buf
);
1240 wxString
wxURLDataObject::GetURL() const
1243 wxCHECK_MSG( m_dataObjectLast
, url
, wxT("no data in wxURLDataObject") );
1245 if ( m_dataObjectLast
->GetPreferredFormat() == CFSTR_SHELLURL
)
1247 const size_t len
= m_dataObjectLast
->GetDataSize();
1251 // CFSTR_SHELLURL is always ANSI so we need to convert it from it in
1254 wxCharBuffer
buf(len
);
1256 if ( m_dataObjectLast
->GetDataHere(buf
.data()) )
1258 #else // !wxUSE_UNICODE
1259 // in ANSI build no conversion is necessary
1260 m_dataObjectLast
->GetDataHere(wxStringBuffer(url
, len
));
1261 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
1263 else // must be wxTextDataObject
1265 url
= static_cast<wxTextDataObject
*>(m_dataObjectLast
)->GetText();
1271 void wxURLDataObject::SetURL(const wxString
& url
)
1273 wxCharBuffer
urlMB(url
.mb_str());
1276 const size_t len
= strlen(urlMB
);
1279 // wxTextDataObject takes the number of characters in the string, not
1280 // the size of the buffer (which would be len+1)
1281 SetData(wxDF_TEXT
, len
, urlMB
);
1282 #endif // !wxUSE_UNICODE
1284 // however CFSTR_SHELLURLDataObject doesn't append NUL automatically
1285 // but we probably still want to have it on the clipboard (just to be
1286 // safe), so do append it
1287 SetData(wxDataFormat(CFSTR_SHELLURL
), len
+ 1, urlMB
);
1291 SetData(wxDF_UNICODETEXT
, url
.length()*sizeof(wxChar
), url
.wc_str());
1295 // ----------------------------------------------------------------------------
1296 // private functions
1297 // ----------------------------------------------------------------------------
1301 static const wxChar
*GetTymedName(DWORD tymed
)
1303 static wxChar s_szBuf
[128];
1305 case TYMED_HGLOBAL
: return wxT("TYMED_HGLOBAL");
1306 case TYMED_FILE
: return wxT("TYMED_FILE");
1307 case TYMED_ISTREAM
: return wxT("TYMED_ISTREAM");
1308 case TYMED_ISTORAGE
: return wxT("TYMED_ISTORAGE");
1309 case TYMED_GDI
: return wxT("TYMED_GDI");
1310 case TYMED_MFPICT
: return wxT("TYMED_MFPICT");
1311 case TYMED_ENHMF
: return wxT("TYMED_ENHMF");
1313 wxSprintf(s_szBuf
, wxT("type of media format %ld (unknown)"), tymed
);
1320 #else // not using OLE at all
1322 // ----------------------------------------------------------------------------
1324 // ----------------------------------------------------------------------------
1328 wxDataObject::wxDataObject()
1332 wxDataObject::~wxDataObject()
1336 void wxDataObject::SetAutoDelete()
1340 const wxChar
*wxDataObject::GetFormatName(wxDataFormat
WXUNUSED(format
))
1345 #endif // wxUSE_DATAOBJ
1347 #endif // wxUSE_OLE/!wxUSE_OLE