X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/77ffb5937e89927b621128789401db8921fe580f..63e1921d0be4956ab729735189ccf637773a7e27:/src/msw/ole/dataobj.cpp?ds=sidebyside diff --git a/src/msw/ole/dataobj.cpp b/src/msw/ole/dataobj.cpp index 3291e672b4..2c2c44603f 100644 --- a/src/msw/ole/dataobj.cpp +++ b/src/msw/ole/dataobj.cpp @@ -1,12 +1,12 @@ /////////////////////////////////////////////////////////////////////////////// -// Name: msw/ole/dataobj.cpp +// Name: src/msw/ole/dataobj.cpp // Purpose: implementation of wx[I]DataObject class // Author: Vadim Zeitlin // Modified by: // Created: 10.05.98 // RCS-ID: $Id$ // Copyright: (c) 1998 Vadim Zeitlin -// Licence: wxWidgets licence +// Licence: wxWindows licence /////////////////////////////////////////////////////////////////////////////// // ============================================================================ @@ -17,10 +17,6 @@ // headers // ---------------------------------------------------------------------------- -#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) - #pragma implementation "dataobj.h" -#endif - // For compilers that support precompilation, includes "wx.h". #include "wx/wxprec.h" @@ -31,12 +27,16 @@ #ifndef WX_PRECOMP #include "wx/intl.h" #include "wx/log.h" + #include "wx/utils.h" + #include "wx/wxcrtvararg.h" #endif #include "wx/dataobj.h" #if wxUSE_OLE && defined(__WIN32__) && !defined(__GNUWIN32_OLD__) +#include "wx/scopedarray.h" +#include "wx/vector.h" #include "wx/msw/private.h" // includes #ifdef __WXWINCE__ @@ -56,18 +56,108 @@ #include "wx/msw/dib.h" #ifndef CFSTR_SHELLURL -#define CFSTR_SHELLURL _T("UniformResourceLocator") +#define CFSTR_SHELLURL wxT("UniformResourceLocator") #endif // ---------------------------------------------------------------------------- // functions // ---------------------------------------------------------------------------- -#ifdef __WXDEBUG__ +#if wxDEBUG_LEVEL static const wxChar *GetTymedName(DWORD tymed); -#else // !Debug +#else // !wxDEBUG_LEVEL #define GetTymedName(tymed) wxEmptyString -#endif // Debug/!Debug +#endif // wxDEBUG_LEVEL/!wxDEBUG_LEVEL + +namespace +{ + +wxDataFormat HtmlFormatFixup(wxDataFormat format) +{ + // Since the HTML format is dynamically registered, the wxDF_HTML + // format does not match the native constant in the way other formats do, + // so for the format checks below to work, we must change the native + // id to the wxDF_HTML constant. + wxChar s_szBuf[256]; + if (::GetClipboardFormatName(format, s_szBuf, WXSIZEOF(s_szBuf))) + { + if (s_szBuf == wxString("HTML Format")) + format = wxDF_HTML; + } + return format; +} + +// helper function for wxCopyStgMedium() +HGLOBAL wxGlobalClone(HGLOBAL hglobIn) +{ + HGLOBAL hglobOut = NULL; + + LPVOID pvIn = GlobalLock(hglobIn); + if (pvIn) + { + SIZE_T cb = GlobalSize(hglobIn); + hglobOut = GlobalAlloc(GMEM_FIXED, cb); + if (hglobOut) + { + CopyMemory(hglobOut, pvIn, cb); + } + GlobalUnlock(hglobIn); + } + + return hglobOut; +} + +// Copies the given STGMEDIUM structure. +// +// This is an local implementation of the function with the same name in +// urlmon.lib but to use that function would require linking with urlmon.lib +// and we don't want to require it, so simple reimplement it here. +HRESULT wxCopyStgMedium(const STGMEDIUM *pmediumIn, STGMEDIUM *pmediumOut) +{ + HRESULT hres = S_OK; + STGMEDIUM stgmOut = *pmediumIn; + + if (pmediumIn->pUnkForRelease == NULL && + !(pmediumIn->tymed & (TYMED_ISTREAM | TYMED_ISTORAGE))) + { + // Object needs to be cloned. + if (pmediumIn->tymed == TYMED_HGLOBAL) + { + stgmOut.hGlobal = wxGlobalClone(pmediumIn->hGlobal); + if (!stgmOut.hGlobal) + { + hres = E_OUTOFMEMORY; + } + } + else + { + hres = DV_E_TYMED; // Don't know how to clone GDI objects. + } + } + + if ( SUCCEEDED(hres) ) + { + switch ( stgmOut.tymed ) + { + case TYMED_ISTREAM: + stgmOut.pstm->AddRef(); + break; + + case TYMED_ISTORAGE: + stgmOut.pstg->AddRef(); + break; + } + + if ( stgmOut.pUnkForRelease ) + stgmOut.pUnkForRelease->AddRef(); + + *pmediumOut = stgmOut; + } + + return hres; +} + +} // anonymous namespace // ---------------------------------------------------------------------------- // wxIEnumFORMATETC interface implementation @@ -79,20 +169,20 @@ public: wxIEnumFORMATETC(const wxDataFormat* formats, ULONG nCount); virtual ~wxIEnumFORMATETC() { delete [] m_formats; } - DECLARE_IUNKNOWN_METHODS; - // IEnumFORMATETC STDMETHODIMP Next(ULONG celt, FORMATETC *rgelt, ULONG *pceltFetched); STDMETHODIMP Skip(ULONG celt); STDMETHODIMP Reset(); STDMETHODIMP Clone(IEnumFORMATETC **ppenum); + DECLARE_IUNKNOWN_METHODS; + private: CLIPFORMAT *m_formats; // formats we can provide data in ULONG m_nCount, // number of formats we support m_nCurrent; // current enum position - DECLARE_NO_COPY_CLASS(wxIEnumFORMATETC) + wxDECLARE_NO_COPY_CLASS(wxIEnumFORMATETC); }; // ---------------------------------------------------------------------------- @@ -106,11 +196,9 @@ public: virtual ~wxIDataObject(); // normally, wxDataObject controls our lifetime (i.e. we're deleted when it - // is), but in some cases, the situation is inversed, that is we delete it + // is), but in some cases, the situation is reversed, that is we delete it // when this object is deleted - setting this flag enables such logic - void SetDeleteFlag() { m_mustDelete = TRUE; } - - DECLARE_IUNKNOWN_METHODS; + void SetDeleteFlag() { m_mustDelete = true; } // IDataObject STDMETHODIMP GetData(FORMATETC *pformatetcIn, STGMEDIUM *pmedium); @@ -123,14 +211,130 @@ public: STDMETHODIMP DUnadvise(DWORD dwConnection); STDMETHODIMP EnumDAdvise(IEnumSTATDATA **ppenumAdvise); + DECLARE_IUNKNOWN_METHODS; + private: wxDataObject *m_pDataObject; // pointer to C++ class we belong to bool m_mustDelete; - DECLARE_NO_COPY_CLASS(wxIDataObject) + wxDECLARE_NO_COPY_CLASS(wxIDataObject); + + // The following code is need to be able to store system data the operating + // system is using for it own purposes, e.g. drag images. + + class SystemDataEntry + { + public: + // Ctor takes ownership of the pointers. + SystemDataEntry(FORMATETC *pformatetc, STGMEDIUM *pmedium) + : pformatetc(pformatetc), pmedium(pmedium) + { + } + + ~SystemDataEntry() + { + delete pformatetc; + delete pmedium; + } + + FORMATETC *pformatetc; + STGMEDIUM *pmedium; + }; + typedef wxVector SystemData; + + // get system data specified by the given format + bool GetSystemData(wxDataFormat format, STGMEDIUM*) const; + + // determines if the data object contains system data specified by the given format. + bool HasSystemData(wxDataFormat format) const; + + // save system data + HRESULT SaveSystemData(FORMATETC*, STGMEDIUM*, BOOL fRelease); + + // container for system data + SystemData m_systemData; }; +bool +wxIDataObject::GetSystemData(wxDataFormat format, STGMEDIUM *pmedium) const +{ + for ( SystemData::const_iterator it = m_systemData.begin(); + it != m_systemData.end(); + ++it ) + { + FORMATETC* formatEtc = (*it)->pformatetc; + if ( formatEtc->cfFormat == format ) + { + wxCopyStgMedium((*it)->pmedium, pmedium); + return true; + } + } + + return false; +} + +bool +wxIDataObject::HasSystemData(wxDataFormat format) const +{ + for ( SystemData::const_iterator it = m_systemData.begin(); + it != m_systemData.end(); + ++it ) + { + FORMATETC* formatEtc = (*it)->pformatetc; + if ( formatEtc->cfFormat == format ) + return true; + } + + return false; +} + +// save system data +HRESULT +wxIDataObject::SaveSystemData(FORMATETC *pformatetc, + STGMEDIUM *pmedium, + BOOL fRelease) +{ + if ( pformatetc == NULL || pmedium == NULL ) + return E_INVALIDARG; + + // remove entry if already available + for ( SystemData::iterator it = m_systemData.begin(); + it != m_systemData.end(); + ++it ) + { + if ( pformatetc->tymed & (*it)->pformatetc->tymed && + pformatetc->dwAspect == (*it)->pformatetc->dwAspect && + pformatetc->cfFormat == (*it)->pformatetc->cfFormat ) + { + delete (*it); + m_systemData.erase(it); + break; + } + } + + // create new format/medium + FORMATETC* pnewformatEtc = new FORMATETC; + STGMEDIUM* pnewmedium = new STGMEDIUM; + + wxZeroMemory(*pnewformatEtc); + wxZeroMemory(*pnewmedium); + + // copy format + *pnewformatEtc = *pformatetc; + + // copy or take ownerschip of medium + if ( fRelease ) + *pnewmedium = *pmedium; + else + wxCopyStgMedium(pmedium, pnewmedium); + + // save entry + m_systemData.push_back(new SystemDataEntry(pnewformatEtc, pnewmedium)); + + return S_OK; +} + // ============================================================================ // implementation // ============================================================================ @@ -139,9 +343,9 @@ private: // wxDataFormat // ---------------------------------------------------------------------------- -void wxDataFormat::SetId(const wxChar *format) +void wxDataFormat::SetId(const wxString& format) { - m_format = (wxDataFormat::NativeFormat)::RegisterClipboardFormat(format); + m_format = (wxDataFormat::NativeFormat)::RegisterClipboardFormat(format.t_str()); if ( !m_format ) { wxLogError(_("Couldn't register clipboard format '%s'."), format); @@ -162,6 +366,7 @@ wxString wxDataFormat::GetId() const if ( !len ) { wxLogError(_("The clipboard format '%d' doesn't exist."), m_format); + return wxEmptyString; } return s; @@ -184,7 +389,10 @@ wxIEnumFORMATETC::wxIEnumFORMATETC(const wxDataFormat *formats, ULONG nCount) m_nCount = nCount; m_formats = new CLIPFORMAT[nCount]; for ( ULONG n = 0; n < nCount; n++ ) { - m_formats[n] = formats[n].GetFormatId(); + if (formats[n].GetFormatId() != wxDF_HTML) + m_formats[n] = formats[n].GetFormatId(); + else + m_formats[n] = ::RegisterClipboardFormat(wxT("HTML Format")); } } @@ -267,11 +475,19 @@ IMPLEMENT_IUNKNOWN_METHODS(wxIDataObject) wxIDataObject::wxIDataObject(wxDataObject *pDataObject) { m_pDataObject = pDataObject; - m_mustDelete = FALSE; + m_mustDelete = false; } wxIDataObject::~wxIDataObject() { + // delete system data + for ( SystemData::iterator it = m_systemData.begin(); + it != m_systemData.end(); + ++it ) + { + delete (*it); + } + if ( m_mustDelete ) { delete m_pDataObject; @@ -291,6 +507,14 @@ STDMETHODIMP wxIDataObject::GetData(FORMATETC *pformatetcIn, STGMEDIUM *pmedium) // for the bitmaps and metafiles we use the handles instead of global memory // to pass the data wxDataFormat format = (wxDataFormat::NativeFormat)pformatetcIn->cfFormat; + format = HtmlFormatFixup(format); + + // is this system data? + if ( GetSystemData(format, pmedium) ) + { + // pmedium is already filled with corresponding data, so we're ready. + return S_OK; + } switch ( format ) { @@ -323,13 +547,8 @@ STDMETHODIMP wxIDataObject::GetData(FORMATETC *pformatetcIn, STGMEDIUM *pmedium) return DV_E_FORMATETC; } - if ( !format.IsStandard() ) { - // for custom formats, put the size with the data - alloc the - // space for it - // MB: not completely sure this is correct, - // even if I can't figure out what's wrong - size += m_pDataObject->GetBufferOffset( format ); - } + // we may need extra space for the buffer size + size += m_pDataObject->GetBufferOffset( format ); HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, size); if ( hGlobal == NULL ) { @@ -391,10 +610,14 @@ STDMETHODIMP wxIDataObject::GetDataHere(FORMATETC *pformatetc, } wxDataFormat format = pformatetc->cfFormat; - if ( !format.IsStandard() ) { - // for custom formats, put the size with the data - pBuf = m_pDataObject->SetSizeInBuffer( pBuf, GlobalSize(hGlobal), format ); - } + + // possibly put the size in the beginning of the buffer + pBuf = m_pDataObject->SetSizeInBuffer + ( + pBuf, + ::GlobalSize(hGlobal), + format + ); if ( !m_pDataObject->GetDataHere(format, pBuf) ) return E_UNEXPECTED; @@ -428,21 +651,30 @@ STDMETHODIMP wxIDataObject::SetData(FORMATETC *pformatetc, m_pDataObject->SetData(wxDF_ENHMETAFILE, 0, &pmedium->hEnhMetaFile); break; + case TYMED_ISTREAM: + // check if this format is supported + if ( !m_pDataObject->IsSupported(pformatetc->cfFormat, + wxDataObject::Set) ) + { + // As this is not a supported format (content data), assume it + // is system data and save it. + return SaveSystemData(pformatetc, pmedium, fRelease); + } + break; + case TYMED_MFPICT: // fall through - we pass METAFILEPICT through HGLOBAL case TYMED_HGLOBAL: { wxDataFormat format = pformatetc->cfFormat; - // this is quite weird, but for file drag and drop, explorer - // calls our SetData() with the formats we do *not* support! - // - // as we can't fix this bug in explorer (it's a bug because it - // should only use formats returned by EnumFormatEtc), do the - // check here + format = HtmlFormatFixup(format); + + // check if this format is supported if ( !m_pDataObject->IsSupported(format, wxDataObject::Set) ) { - // go away! - return DV_E_FORMATETC; + // As above, assume that unsupported format must be system + // data and just save it. + return SaveSystemData(pformatetc, pmedium, fRelease); } // copy data @@ -461,14 +693,14 @@ STDMETHODIMP wxIDataObject::SetData(FORMATETC *pformatetc, size_t size; switch ( format ) { + case wxDF_HTML: case CF_TEXT: case CF_OEMTEXT: size = strlen((const char *)pBuf); break; -#if !defined(__WATCOMC__) && ! (defined(__BORLANDC__) && (__BORLANDC__ < 0x500)) +#if !(defined(__BORLANDC__) && (__BORLANDC__ < 0x500)) case CF_UNICODETEXT: -#if ( defined(__BORLANDC__) && (__BORLANDC__ > 0x530) ) \ - || ( defined(__MWERKS__) && defined(__WXMSW__) ) +#if ( defined(__BORLANDC__) && (__BORLANDC__ > 0x530) ) size = std::wcslen((const wchar_t *)pBuf) * sizeof(wchar_t); #else size = wxWcslen((const wchar_t *)pBuf) * sizeof(wchar_t); @@ -496,14 +728,9 @@ STDMETHODIMP wxIDataObject::SetData(FORMATETC *pformatetc, break; #endif default: - { - // we suppose that the size precedes the data - pBuf = m_pDataObject->GetSizeFromBuffer( pBuf, &size, format ); - if (! format.IsStandard() ) { - // see GetData for coresponding increment - size -= m_pDataObject->GetBufferOffset( format ); - } - } + pBuf = m_pDataObject-> + GetSizeFromBuffer(pBuf, &size, format); + size -= m_pDataObject->GetBufferOffset(format); } bool ok = m_pDataObject->SetData(format, size, pBuf); @@ -575,10 +802,19 @@ STDMETHODIMP wxIDataObject::QueryGetData(FORMATETC *pformatetc) // and now check the type of data requested wxDataFormat format = pformatetc->cfFormat; + format = HtmlFormatFixup(format); + if ( m_pDataObject->IsSupportedFormat(format) ) { wxLogTrace(wxTRACE_OleCalls, wxT("wxIDataObject::QueryGetData: %s ok"), wxGetFormatName(format)); } + else if ( HasSystemData(format) ) + { + wxLogTrace(wxTRACE_OleCalls, wxT("wxIDataObject::QueryGetData: %s ok (system data)"), + wxGetFormatName(format)); + // this is system data, so no further checks needed. + return S_OK; + } else { wxLogTrace(wxTRACE_OleCalls, wxT("wxIDataObject::QueryGetData: %s unsupported"), @@ -624,20 +860,31 @@ STDMETHODIMP wxIDataObject::EnumFormatEtc(DWORD dwDir, wxDataObject::Direction dir = dwDir == DATADIR_GET ? wxDataObject::Get : wxDataObject::Set; - size_t nFormatCount = m_pDataObject->GetFormatCount(dir); - wxDataFormat format; - wxDataFormat *formats; - formats = nFormatCount == 1 ? &format : new wxDataFormat[nFormatCount]; - m_pDataObject->GetAllFormats(formats, dir); + // format count is total of user specified and system formats. + const size_t ourFormatCount = m_pDataObject->GetFormatCount(dir); + const size_t sysFormatCount = m_systemData.size(); - wxIEnumFORMATETC *pEnum = new wxIEnumFORMATETC(formats, nFormatCount); - pEnum->AddRef(); - *ppenumFormatEtc = pEnum; + const ULONG + nFormatCount = wx_truncate_cast(ULONG, ourFormatCount + sysFormatCount); - if ( formats != &format ) { - delete [] formats; + // fill format array with formats ... + wxScopedArray formats(new wxDataFormat[nFormatCount]); + + // ... from content data (supported formats) + m_pDataObject->GetAllFormats(formats.get(), dir); + + // ... from system data + for ( size_t j = 0; j < sysFormatCount; j++ ) + { + SystemDataEntry* entry = m_systemData[j]; + wxDataFormat& format = formats[ourFormatCount + j]; + format = entry->pformatetc->cfFormat; } + wxIEnumFORMATETC *pEnum = new wxIEnumFORMATETC(formats.get(), nFormatCount); + pEnum->AddRef(); + *ppenumFormatEtc = pEnum; + return S_OK; } @@ -683,34 +930,50 @@ void wxDataObject::SetAutoDelete() ((wxIDataObject *)m_pIDataObject)->SetDeleteFlag(); m_pIDataObject->Release(); - // so that the dtor doesnt' crash + // so that the dtor doesn't crash m_pIDataObject = NULL; } -size_t wxDataObject::GetBufferOffset( const wxDataFormat& WXUNUSED(format) ) +size_t wxDataObject::GetBufferOffset(const wxDataFormat& format ) { - return sizeof(size_t); + // if we prepend the size of the data to the buffer itself, account for it + return NeedsVerbatimData(format) ? 0 : sizeof(size_t); } -const void* wxDataObject::GetSizeFromBuffer( const void* buffer, size_t* size, - const wxDataFormat& WXUNUSED(format) ) +const void *wxDataObject::GetSizeFromBuffer(const void *buffer, + size_t *size, + const wxDataFormat& WXUNUSED(format)) { - size_t* p = (size_t*)buffer; - *size = *p; + // hack: the third parameter is declared non-const in Wine's headers so + // cast away the const + const size_t realsz = ::HeapSize(::GetProcessHeap(), 0, + const_cast(buffer)); + if ( realsz == (size_t)-1 ) + { + // note that HeapSize() does not set last error + wxLogApiError(wxT("HeapSize"), 0); + return NULL; + } - return p + 1; + *size = realsz; + + return buffer; } void* wxDataObject::SetSizeInBuffer( void* buffer, size_t size, - const wxDataFormat& WXUNUSED(format) ) + const wxDataFormat& format ) { - size_t* p = (size_t*)buffer; - *p = size; + size_t* p = (size_t *)buffer; + if ( !NeedsVerbatimData(format) ) + { + // prepend the size to the data and skip it + *p++ = size; + } - return p + 1; + return p; } -#ifdef __WXDEBUG__ +#if wxDEBUG_LEVEL const wxChar *wxDataObject::GetFormatName(wxDataFormat format) { @@ -755,7 +1018,7 @@ const wxChar *wxDataObject::GetFormatName(wxDataFormat format) #endif // VC++ } -#endif // Debug +#endif // wxDEBUG_LEVEL // ---------------------------------------------------------------------------- // wxBitmapDataObject supports CF_DIB format @@ -779,7 +1042,8 @@ bool wxBitmapDataObject::GetDataHere(void *buf) const return wxDIB::ConvertFromBitmap(pbi, GetHbitmapOf(GetBitmap())) != 0; #else - return FALSE; + wxUnusedVar(buf); + return false; #endif } @@ -800,9 +1064,10 @@ bool wxBitmapDataObject::SetData(size_t WXUNUSED(len), const void *buf) SetBitmap(bitmap); - return TRUE; + return true; #else - return FALSE; + wxUnusedVar(buf); + return false; #endif } @@ -825,7 +1090,7 @@ bool wxBitmapDataObject2::GetDataHere(void *pBuf) const // we put a bitmap handle into pBuf *(WXHBITMAP *)pBuf = GetBitmap().GetHBITMAP(); - return TRUE; + return true; } bool wxBitmapDataObject2::SetData(size_t WXUNUSED(len), const void *pBuf) @@ -841,15 +1106,15 @@ bool wxBitmapDataObject2::SetData(size_t WXUNUSED(len), const void *pBuf) wxBitmap bitmap(bmp.bmWidth, bmp.bmHeight, bmp.bmPlanes); bitmap.SetHBITMAP((WXHBITMAP)hbmp); - if ( !bitmap.Ok() ) { + if ( !bitmap.IsOk() ) { wxFAIL_MSG(wxT("pasting/dropping invalid bitmap")); - return FALSE; + return false; } SetBitmap(bitmap); - return TRUE; + return true; } #if 0 @@ -887,7 +1152,7 @@ size_t wxBitmapDataObject::GetDataSize(const wxDataFormat& format) const bool wxBitmapDataObject::GetDataHere(const wxDataFormat& format, void *pBuf) const { - wxASSERT_MSG( m_bitmap.Ok(), wxT("copying invalid bitmap") ); + wxASSERT_MSG( m_bitmap.IsOk(), wxT("copying invalid bitmap") ); HBITMAP hbmp = (HBITMAP)m_bitmap.GetHBITMAP(); if ( format.GetFormatId() == CF_DIB ) @@ -914,7 +1179,7 @@ bool wxBitmapDataObject::GetDataHere(const wxDataFormat& format, { wxLogLastError(wxT("GetDIBits")); - return FALSE; + return false; } } else // CF_BITMAP @@ -923,7 +1188,7 @@ bool wxBitmapDataObject::GetDataHere(const wxDataFormat& format, *(HBITMAP *)pBuf = hbmp; } - return TRUE; + return true; } bool wxBitmapDataObject::SetData(const wxDataFormat& format, @@ -966,9 +1231,9 @@ bool wxBitmapDataObject::SetData(const wxDataFormat& format, m_bitmap.SetHBITMAP((WXHBITMAP)hbmp); - wxASSERT_MSG( m_bitmap.Ok(), wxT("pasting invalid bitmap") ); + wxASSERT_MSG( m_bitmap.IsOk(), wxT("pasting invalid bitmap") ); - return TRUE; + return true; } #endif // 0 @@ -977,7 +1242,8 @@ bool wxBitmapDataObject::SetData(const wxDataFormat& format, // wxFileDataObject // ---------------------------------------------------------------------------- -bool wxFileDataObject::SetData(size_t WXUNUSED(size), const void *pData) +bool wxFileDataObject::SetData(size_t WXUNUSED(size), + const void *WXUNUSED_IN_WINCE(pData)) { #ifndef __WXWINCE__ m_filenames.Empty(); @@ -1011,9 +1277,9 @@ bool wxFileDataObject::SetData(size_t WXUNUSED(size), const void *pData) } } - return TRUE; + return true; #else - return FALSE; + return false; #endif } @@ -1028,22 +1294,44 @@ void wxFileDataObject::AddFile(const wxString& file) size_t wxFileDataObject::GetDataSize() const { #ifndef __WXWINCE__ - // size returned will be the size of the DROPFILES structure, - // plus the list of filesnames (null byte separated), plus - // a double null at the end + // size returned will be the size of the DROPFILES structure, plus the list + // of filesnames (null byte separated), plus a double null at the end // if no filenames in list, size is 0 - if ( m_filenames.GetCount() == 0 ) + if ( m_filenames.empty() ) return 0; - // inital size of DROPFILES struct + null byte - size_t sz = sizeof(DROPFILES) + 1; +#if wxUSE_UNICODE_MSLU + size_t sizeOfChar; + if ( wxGetOsVersion() == wxOS_WINDOWS_9X ) + { + // Win9x always uses ANSI file names and MSLU doesn't help with this + sizeOfChar = 1; + } + else + { + sizeOfChar = sizeof(wxChar); + } +#else // !wxUSE_UNICODE_MSLU + static const size_t sizeOfChar = sizeof(wxChar); +#endif // wxUSE_UNICODE_MSLU/!wxUSE_UNICODE_MSLU - size_t count = m_filenames.GetCount(); + // initial size of DROPFILES struct + null byte + size_t sz = sizeof(DROPFILES) + sizeOfChar; + + const size_t count = m_filenames.size(); for ( size_t i = 0; i < count; i++ ) { // add filename length plus null byte - sz += m_filenames[i].Len() + 1; + size_t len; +#if wxUSE_UNICODE_MSLU + if ( sizeOfChar == 1 ) + len = strlen(m_filenames[i].mb_str(*wxConvFileName)); + else +#endif // wxUSE_UNICODE_MSLU + len = m_filenames[i].length(); + + sz += (len + 1) * sizeOfChar; } return sz; @@ -1052,15 +1340,15 @@ size_t wxFileDataObject::GetDataSize() const #endif } -bool wxFileDataObject::GetDataHere(void *pData) const +bool wxFileDataObject::GetDataHere(void *WXUNUSED_IN_WINCE(pData)) const { #ifndef __WXWINCE__ // pData points to an externally allocated memory block // created using the size returned by GetDataSize() // if pData is NULL, or there are no files, return - if ( !pData || m_filenames.GetCount() == 0 ) - return FALSE; + if ( !pData || m_filenames.empty() ) + return false; // convert data pointer to a DROPFILES struct pointer LPDROPFILES pDrop = (LPDROPFILES) pData; @@ -1068,31 +1356,48 @@ bool wxFileDataObject::GetDataHere(void *pData) const // initialize DROPFILES struct pDrop->pFiles = sizeof(DROPFILES); pDrop->fNC = FALSE; // not non-client coords -#if wxUSE_UNICODE - pDrop->fWide = TRUE; -#else // ANSI - pDrop->fWide = FALSE; -#endif // Unicode/Ansi +#if wxUSE_UNICODE_MSLU + pDrop->fWide = wxGetOsVersion() != wxOS_WINDOWS_9X ? TRUE : FALSE; +#else + pDrop->fWide = wxUSE_UNICODE; +#endif + + const size_t sizeOfChar = pDrop->fWide ? sizeof(wchar_t) : 1; // set start of filenames list (null separated) - wxChar *pbuf = (wxChar*) ((BYTE *)pDrop + sizeof(DROPFILES)); + BYTE *pbuf = (BYTE *)(pDrop + 1); - size_t count = m_filenames.GetCount(); - for (size_t i = 0; i < count; i++ ) + const size_t count = m_filenames.size(); + for ( size_t i = 0; i < count; i++ ) { // copy filename to pbuf and add null terminator - size_t len = m_filenames[i].Len(); - memcpy(pbuf, m_filenames[i], len*sizeof(wxChar)); - pbuf += len; - *pbuf++ = wxT('\0'); + size_t len; +#if wxUSE_UNICODE_MSLU + if ( sizeOfChar == 1 ) + { + wxCharBuffer buf(m_filenames[i].mb_str(*wxConvFileName)); + len = strlen(buf); + memcpy(pbuf, buf, len*sizeOfChar); + } + else +#endif // wxUSE_UNICODE_MSLU + { + len = m_filenames[i].length(); + memcpy(pbuf, m_filenames[i].t_str(), len*sizeOfChar); + } + + pbuf += len*sizeOfChar; + + memset(pbuf, 0, sizeOfChar); + pbuf += sizeOfChar; } // add final null terminator - *pbuf = wxT('\0'); + memset(pbuf, 0, sizeOfChar); - return TRUE; + return true; #else - return FALSE; + return false; #endif } @@ -1100,11 +1405,17 @@ bool wxFileDataObject::GetDataHere(void *pData) const // wxURLDataObject // ---------------------------------------------------------------------------- +// Work around bug in Wine headers +#if defined(__WINE__) && defined(CFSTR_SHELLURL) && wxUSE_UNICODE +#undef CFSTR_SHELLURL +#define CFSTR_SHELLURL wxT("CFSTR_SHELLURL") +#endif + class CFSTR_SHELLURLDataObject : public wxCustomDataObject { public: CFSTR_SHELLURLDataObject() : wxCustomDataObject(CFSTR_SHELLURL) {} -protected: + virtual size_t GetBufferOffset( const wxDataFormat& WXUNUSED(format) ) { return 0; @@ -1125,38 +1436,24 @@ protected: return buffer; } -#if wxUSE_UNICODE - virtual bool GetDataHere( void* buffer ) const - { - // CFSTR_SHELLURL is _always_ ANSI! - wxCharBuffer char_buffer( GetDataSize() ); - wxCustomDataObject::GetDataHere( (void*)char_buffer.data() ); - wxString unicode_buffer( char_buffer, wxConvLibc ); - memcpy( buffer, unicode_buffer.c_str(), - ( unicode_buffer.length() + 1 ) * sizeof(wxChar) ); - - return TRUE; - } - virtual bool GetDataHere(const wxDataFormat& WXUNUSED(format), - void *buf) const - { return GetDataHere(buf); } -#endif - - DECLARE_NO_COPY_CLASS(CFSTR_SHELLURLDataObject) + wxDECLARE_NO_COPY_CLASS(CFSTR_SHELLURLDataObject); }; -wxURLDataObject::wxURLDataObject() +wxURLDataObject::wxURLDataObject(const wxString& url) { - // we support CF_TEXT and CFSTR_SHELLURL formats which are basicly the same - // but it seems that some browsers only provide one of them so we have to - // support both + // we support CF_TEXT and CFSTR_SHELLURL formats which are basically the + // same but it seems that some browsers only provide one of them so we have + // to support both Add(new wxTextDataObject); Add(new CFSTR_SHELLURLDataObject()); // we don't have any data yet m_dataObjectLast = NULL; + + if ( !url.empty() ) + SetURL(url); } bool wxURLDataObject::SetData(const wxDataFormat& format, @@ -1174,31 +1471,63 @@ bool wxURLDataObject::SetData(const wxDataFormat& format, wxString wxURLDataObject::GetURL() const { wxString url; - wxCHECK_MSG( m_dataObjectLast, url, _T("no data in wxURLDataObject") ); + wxCHECK_MSG( m_dataObjectLast, url, wxT("no data in wxURLDataObject") ); - size_t len = m_dataObjectLast->GetDataSize(); + if ( m_dataObjectLast->GetPreferredFormat() == CFSTR_SHELLURL ) + { + const size_t len = m_dataObjectLast->GetDataSize(); + if ( !len ) + return wxString(); - m_dataObjectLast->GetDataHere(wxStringBuffer(url, len)); + // CFSTR_SHELLURL is always ANSI so we need to convert it from it in + // Unicode build +#if wxUSE_UNICODE + wxCharBuffer buf(len); + + if ( m_dataObjectLast->GetDataHere(buf.data()) ) + url = buf; +#else // !wxUSE_UNICODE + // in ANSI build no conversion is necessary + m_dataObjectLast->GetDataHere(wxStringBuffer(url, len)); +#endif // wxUSE_UNICODE/!wxUSE_UNICODE + } + else // must be wxTextDataObject + { + url = static_cast(m_dataObjectLast)->GetText(); + } return url; } void wxURLDataObject::SetURL(const wxString& url) { - SetData(wxDataFormat(wxUSE_UNICODE ? wxDF_UNICODETEXT : wxDF_TEXT), - url.Length()+1, url.c_str()); + wxCharBuffer urlMB(url.mb_str()); + if ( urlMB ) + { + const size_t len = strlen(urlMB); + +#if !wxUSE_UNICODE + // wxTextDataObject takes the number of characters in the string, not + // the size of the buffer (which would be len+1) + SetData(wxDF_TEXT, len, urlMB); +#endif // !wxUSE_UNICODE + + // however CFSTR_SHELLURLDataObject doesn't append NUL automatically + // but we probably still want to have it on the clipboard (just to be + // safe), so do append it + SetData(wxDataFormat(CFSTR_SHELLURL), len + 1, urlMB); + } - // CFSTR_SHELLURL is always supposed to be ANSI... - wxWX2MBbuf urlA = (wxWX2MBbuf)url.mbc_str(); - size_t len = strlen(urlA); - SetData(wxDataFormat(CFSTR_SHELLURL), len+1, (const char*)urlA); +#if wxUSE_UNICODE + SetData(wxDF_UNICODETEXT, url.length()*sizeof(wxChar), url.wc_str()); +#endif } // ---------------------------------------------------------------------------- // private functions // ---------------------------------------------------------------------------- -#ifdef __WXDEBUG__ +#if wxDEBUG_LEVEL static const wxChar *GetTymedName(DWORD tymed) { @@ -1220,6 +1549,7 @@ static const wxChar *GetTymedName(DWORD tymed) #endif // Debug #else // not using OLE at all + // ---------------------------------------------------------------------------- // wxDataObject // ---------------------------------------------------------------------------- @@ -1238,14 +1568,13 @@ void wxDataObject::SetAutoDelete() { } -#ifdef __WXDEBUG__ -const wxChar *wxDataObject::GetFormatName(wxDataFormat format) +const wxChar *wxDataObject::GetFormatName(wxDataFormat WXUNUSED(format)) { return NULL; } -#endif -#endif +#endif // wxUSE_DATAOBJ + +#endif // wxUSE_OLE/!wxUSE_OLE -#endif