X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/3103e8a97e834e9793f0eb149aa82a99fd64ef9a..e6d18909c9cb124feaab32966f2aa44c2f0b0617:/src/msw/ole/dataobj.cpp diff --git a/src/msw/ole/dataobj.cpp b/src/msw/ole/dataobj.cpp index e26a1ea1d5..f57954158a 100644 --- a/src/msw/ole/dataobj.cpp +++ b/src/msw/ole/dataobj.cpp @@ -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,6 +27,8 @@ #ifndef WX_PRECOMP #include "wx/intl.h" #include "wx/log.h" + #include "wx/utils.h" + #include "wx/wxcrtvararg.h" #endif #include "wx/dataobj.h" @@ -79,14 +77,14 @@ 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 @@ -110,8 +108,6 @@ public: // when this object is deleted - setting this flag enables such logic void SetDeleteFlag() { m_mustDelete = true; } - DECLARE_IUNKNOWN_METHODS; - // IDataObject STDMETHODIMP GetData(FORMATETC *pformatetcIn, STGMEDIUM *pmedium); STDMETHODIMP GetDataHere(FORMATETC *pformatetc, STGMEDIUM *pmedium); @@ -123,6 +119,8 @@ public: STDMETHODIMP DUnadvise(DWORD dwConnection); STDMETHODIMP EnumDAdvise(IEnumSTATDATA **ppenumAdvise); + DECLARE_IUNKNOWN_METHODS; + private: wxDataObject *m_pDataObject; // pointer to C++ class we belong to @@ -139,9 +137,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.wx_str()); if ( !m_format ) { wxLogError(_("Couldn't register clipboard format '%s'."), format); @@ -162,6 +160,7 @@ wxString wxDataFormat::GetId() const if ( !len ) { wxLogError(_("The clipboard format '%d' doesn't exist."), m_format); + return wxEmptyString; } return s; @@ -323,13 +322,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 +385,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; @@ -496,14 +494,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 corresponding 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); @@ -624,7 +617,7 @@ STDMETHODIMP wxIDataObject::EnumFormatEtc(DWORD dwDir, wxDataObject::Direction dir = dwDir == DATADIR_GET ? wxDataObject::Get : wxDataObject::Set; - size_t nFormatCount = m_pDataObject->GetFormatCount(dir); + ULONG nFormatCount = wx_truncate_cast(ULONG, m_pDataObject->GetFormatCount(dir)); wxDataFormat format; wxDataFormat *formats; formats = nFormatCount == 1 ? &format : new wxDataFormat[nFormatCount]; @@ -687,27 +680,43 @@ void wxDataObject::SetAutoDelete() 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, + wx_const_cast(void*, 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__ @@ -979,7 +988,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(); @@ -1015,7 +1025,6 @@ bool wxFileDataObject::SetData(size_t WXUNUSED(size), const void *pData) return true; #else - wxUnusedVar(pData); return false; #endif } @@ -1031,22 +1040,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; +#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 = sizeof(char); + } + else + { + sizeOfChar = sizeof(wxChar); + } +#else // !wxUSE_UNICODE_MSLU + static const size_t sizeOfChar = sizeof(wxChar); +#endif // wxUSE_UNICODE_MSLU/!wxUSE_UNICODE_MSLU + // inital size of DROPFILES struct + null byte - size_t sz = sizeof(DROPFILES) + (1 * sizeof(wxChar)); + size_t sz = sizeof(DROPFILES) + sizeOfChar; - size_t count = m_filenames.GetCount(); + 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) * sizeof(wxChar); + size_t len; +#if wxUSE_UNICODE_MSLU + if ( sizeOfChar == sizeof(char) ) + len = strlen(m_filenames[i].mb_str(*wxConvFileName)); + else +#endif // wxUSE_UNICODE_MSLU + len = m_filenames[i].length(); + + sz += (len + 1) * sizeOfChar; } return sz; @@ -1055,14 +1086,14 @@ 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 ) + if ( !pData || m_filenames.empty() ) return false; // convert data pointer to a DROPFILES struct pointer @@ -1071,31 +1102,47 @@ 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) : sizeof(char); // 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 == sizeof(char) ) + { + 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].wx_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; #else - wxUnusedVar(pData); return false; #endif } @@ -1104,11 +1151,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 _T("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; @@ -1151,16 +1204,19 @@ protected: -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, @@ -1189,13 +1245,15 @@ wxString wxURLDataObject::GetURL() const 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) + 1; // size with trailing NUL + SetData(wxDF_TEXT, len, urlMB); + SetData(wxDataFormat(CFSTR_SHELLURL), len, 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); + SetData(wxDF_UNICODETEXT, url.length() + 1, url.wc_str()); } // ----------------------------------------------------------------------------