X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/1f1c42109dce7d0760966eb57127a0bb668d50f5..6505ca85df415198d1686d48253545dcb93a22c6:/src/msw/ole/dataobj.cpp diff --git a/src/msw/ole/dataobj.cpp b/src/msw/ole/dataobj.cpp index 502137ce8f..ded312e08d 100644 --- a/src/msw/ole/dataobj.cpp +++ b/src/msw/ole/dataobj.cpp @@ -17,10 +17,6 @@ // headers // ---------------------------------------------------------------------------- -#ifdef __GNUG__ - #pragma implementation "dataobj.h" -#endif - // For compilers that support precompilation, includes "wx.h". #include "wx/wxprec.h" @@ -39,8 +35,12 @@ #include "wx/msw/private.h" // includes +#ifdef __WXWINCE__ +#include +#endif + // for some compilers, the entire ole2.h must be included, not only oleauto.h -#if wxUSE_NORLANDER_HEADERS || defined(__WATCOMC__) +#if wxUSE_NORLANDER_HEADERS || defined(__WATCOMC__) || defined(__WXWINCE__) #include #endif @@ -62,7 +62,7 @@ #ifdef __WXDEBUG__ static const wxChar *GetTymedName(DWORD tymed); #else // !Debug - #define GetTymedName(tymed) _T("") + #define GetTymedName(tymed) wxEmptyString #endif // Debug/!Debug // ---------------------------------------------------------------------------- @@ -75,14 +75,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 @@ -102,11 +102,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); @@ -119,6 +117,8 @@ public: STDMETHODIMP DUnadvise(DWORD dwConnection); STDMETHODIMP EnumDAdvise(IEnumSTATDATA **ppenumAdvise); + DECLARE_IUNKNOWN_METHODS; + private: wxDataObject *m_pDataObject; // pointer to C++ class we belong to @@ -153,8 +153,7 @@ wxString wxDataFormat::GetId() const wxCHECK_MSG( !IsStandard(), s, wxT("name of predefined format cannot be retrieved") ); - int len = ::GetClipboardFormatName(m_format, s.GetWriteBuf(max), max); - s.UngetWriteBuf(); + int len = ::GetClipboardFormatName(m_format, wxStringBuffer(s, max), max); if ( !len ) { @@ -264,7 +263,7 @@ IMPLEMENT_IUNKNOWN_METHODS(wxIDataObject) wxIDataObject::wxIDataObject(wxDataObject *pDataObject) { m_pDataObject = pDataObject; - m_mustDelete = FALSE; + m_mustDelete = false; } wxIDataObject::~wxIDataObject() @@ -299,6 +298,7 @@ STDMETHODIMP wxIDataObject::GetData(FORMATETC *pformatetcIn, STGMEDIUM *pmedium) pmedium->tymed = TYMED_ENHMF; break; +#ifndef __WXWINCE__ case wxDF_METAFILE: pmedium->hGlobal = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, sizeof(METAFILEPICT)); @@ -308,7 +308,7 @@ STDMETHODIMP wxIDataObject::GetData(FORMATETC *pformatetcIn, STGMEDIUM *pmedium) } pmedium->tymed = TYMED_MFPICT; break; - +#endif default: // alloc memory size_t size = m_pDataObject->GetDataSize(format); @@ -319,13 +319,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 ) { @@ -387,10 +382,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; @@ -461,7 +460,7 @@ STDMETHODIMP wxIDataObject::SetData(FORMATETC *pformatetc, 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__) ) @@ -472,11 +471,13 @@ STDMETHODIMP wxIDataObject::SetData(FORMATETC *pformatetc, break; #endif case CF_BITMAP: +#ifndef __WXWINCE__ case CF_HDROP: // these formats don't use size at all, anyhow (but // pass data by handle, which is always a single DWORD) size = 0; break; +#endif case CF_DIB: // the handler will calculate size itself (it's too @@ -484,19 +485,15 @@ STDMETHODIMP wxIDataObject::SetData(FORMATETC *pformatetc, size = 0; break; +#ifndef __WXWINCE__ case CF_METAFILEPICT: size = sizeof(METAFILEPICT); 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); @@ -617,7 +614,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]; @@ -680,27 +677,50 @@ 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 wxDataFormat& format ) { - size_t* p = (size_t*)buffer; - *size = *p; + SIZE_T realsz = ::HeapSize(::GetProcessHeap(), 0, 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; + + // check if this data has its size prepended (as it was by default for wx + // programs prior 2.6.3): + size_t *p = (size_t *)buffer; + if ( *p == realsz ) + { + if ( NeedsVerbatimData(format) ) + wxLogDebug(wxT("Apparent data format mismatch: size not needed")); + + p++; // this data has its size prepended; skip first DWORD + } + + return p; } 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__ @@ -716,7 +736,6 @@ const wxChar *wxDataObject::GetFormatName(wxDataFormat format) switch ( format ) { case CF_TEXT: return wxT("CF_TEXT"); case CF_BITMAP: return wxT("CF_BITMAP"); - case CF_METAFILEPICT: return wxT("CF_METAFILEPICT"); case CF_SYLK: return wxT("CF_SYLK"); case CF_DIF: return wxT("CF_DIF"); case CF_TIFF: return wxT("CF_TIFF"); @@ -727,9 +746,12 @@ const wxChar *wxDataObject::GetFormatName(wxDataFormat format) case CF_RIFF: return wxT("CF_RIFF"); case CF_WAVE: return wxT("CF_WAVE"); case CF_UNICODETEXT: return wxT("CF_UNICODETEXT"); +#ifndef __WXWINCE__ + case CF_METAFILEPICT: return wxT("CF_METAFILEPICT"); case CF_ENHMETAFILE: return wxT("CF_ENHMETAFILE"); - case CF_HDROP: return wxT("CF_HDROP"); case CF_LOCALE: return wxT("CF_LOCALE"); + case CF_HDROP: return wxT("CF_HDROP"); +#endif default: if ( !::GetClipboardFormatName(format, s_szBuf, WXSIZEOF(s_szBuf)) ) @@ -752,20 +774,32 @@ const wxChar *wxDataObject::GetFormatName(wxDataFormat format) // wxBitmapDataObject supports CF_DIB format // ---------------------------------------------------------------------------- +// TODO: support CF_DIB under Windows CE as well + size_t wxBitmapDataObject::GetDataSize() const { +#if wxUSE_WXDIB && !defined(__WXWINCE__) return wxDIB::ConvertFromBitmap(NULL, GetHbitmapOf(GetBitmap())); +#else + return 0; +#endif } bool wxBitmapDataObject::GetDataHere(void *buf) const { +#if wxUSE_WXDIB && !defined(__WXWINCE__) BITMAPINFO * const pbi = (BITMAPINFO *)buf; return wxDIB::ConvertFromBitmap(pbi, GetHbitmapOf(GetBitmap())) != 0; +#else + wxUnusedVar(buf); + return false; +#endif } bool wxBitmapDataObject::SetData(size_t WXUNUSED(len), const void *buf) { +#if wxUSE_WXDIB && !defined(__WXWINCE__) const BITMAPINFO * const pbmi = (const BITMAPINFO *)buf; HBITMAP hbmp = wxDIB::ConvertToBitmap(pbmi); @@ -780,7 +814,11 @@ bool wxBitmapDataObject::SetData(size_t WXUNUSED(len), const void *buf) SetBitmap(bitmap); - return TRUE; + return true; +#else + wxUnusedVar(buf); + return false; +#endif } // ---------------------------------------------------------------------------- @@ -802,7 +840,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) @@ -821,12 +859,12 @@ bool wxBitmapDataObject2::SetData(size_t WXUNUSED(len), const void *pBuf) if ( !bitmap.Ok() ) { wxFAIL_MSG(wxT("pasting/dropping invalid bitmap")); - return FALSE; + return false; } SetBitmap(bitmap); - return TRUE; + return true; } #if 0 @@ -891,7 +929,7 @@ bool wxBitmapDataObject::GetDataHere(const wxDataFormat& format, { wxLogLastError(wxT("GetDIBits")); - return FALSE; + return false; } } else // CF_BITMAP @@ -900,7 +938,7 @@ bool wxBitmapDataObject::GetDataHere(const wxDataFormat& format, *(HBITMAP *)pBuf = hbmp; } - return TRUE; + return true; } bool wxBitmapDataObject::SetData(const wxDataFormat& format, @@ -945,7 +983,7 @@ bool wxBitmapDataObject::SetData(const wxDataFormat& format, wxASSERT_MSG( m_bitmap.Ok(), wxT("pasting invalid bitmap") ); - return TRUE; + return true; } #endif // 0 @@ -954,8 +992,10 @@ 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(); // the documentation states that the first member of DROPFILES structure is @@ -978,8 +1018,7 @@ bool wxFileDataObject::SetData(size_t WXUNUSED(size), const void *pData) // +1 for terminating NUL len = ::DragQueryFile(hdrop, n, NULL, 0) + 1; - UINT len2 = ::DragQueryFile(hdrop, n, str.GetWriteBuf(len), len); - str.UngetWriteBuf(); + UINT len2 = ::DragQueryFile(hdrop, n, wxStringBuffer(str, len), len); m_filenames.Add(str); if ( len2 != len - 1 ) { @@ -988,7 +1027,10 @@ bool wxFileDataObject::SetData(size_t WXUNUSED(size), const void *pData) } } - return TRUE; + return true; +#else + return false; +#endif } void wxFileDataObject::AddFile(const wxString& file) @@ -1001,6 +1043,7 @@ 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 @@ -1010,26 +1053,30 @@ size_t wxFileDataObject::GetDataSize() const return 0; // inital size of DROPFILES struct + null byte - size_t sz = sizeof(DROPFILES) + 1; + size_t sz = sizeof(DROPFILES) + (1 * sizeof(wxChar)); size_t count = m_filenames.GetCount(); for ( size_t i = 0; i < count; i++ ) { // add filename length plus null byte - sz += m_filenames[i].Len() + 1; + sz += (m_filenames[i].Len() + 1) * sizeof(wxChar); } return sz; +#else + return 0; +#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; + return false; // convert data pointer to a DROPFILES struct pointer LPDROPFILES pDrop = (LPDROPFILES) pData; @@ -1051,7 +1098,7 @@ bool wxFileDataObject::GetDataHere(void *pData) const { // copy filename to pbuf and add null terminator size_t len = m_filenames[i].Len(); - memcpy(pbuf, m_filenames[i], len); + memcpy(pbuf, m_filenames[i], len*sizeof(wxChar)); pbuf += len; *pbuf++ = wxT('\0'); } @@ -1059,7 +1106,10 @@ bool wxFileDataObject::GetDataHere(void *pData) const // add final null terminator *pbuf = wxT('\0'); - return TRUE; + return true; +#else + return false; +#endif } // ---------------------------------------------------------------------------- @@ -1070,7 +1120,7 @@ class CFSTR_SHELLURLDataObject : public wxCustomDataObject { public: CFSTR_SHELLURLDataObject() : wxCustomDataObject(CFSTR_SHELLURL) {} -protected: + virtual size_t GetBufferOffset( const wxDataFormat& WXUNUSED(format) ) { return 0; @@ -1101,9 +1151,14 @@ protected: memcpy( buffer, unicode_buffer.c_str(), ( unicode_buffer.length() + 1 ) * sizeof(wxChar) ); - return TRUE; + return true; } + virtual bool GetDataHere(const wxDataFormat& WXUNUSED(format), + void *buf) const + { return GetDataHere(buf); } #endif + + DECLARE_NO_COPY_CLASS(CFSTR_SHELLURLDataObject) }; @@ -1139,8 +1194,7 @@ wxString wxURLDataObject::GetURL() const size_t len = m_dataObjectLast->GetDataSize(); - m_dataObjectLast->GetDataHere(url.GetWriteBuf(len)); - url.UngetWriteBuf(); + m_dataObjectLast->GetDataHere(wxStringBuffer(url, len)); return url; } @@ -1182,10 +1236,13 @@ static const wxChar *GetTymedName(DWORD tymed) #endif // Debug #else // not using OLE at all + // ---------------------------------------------------------------------------- // wxDataObject // ---------------------------------------------------------------------------- +#if wxUSE_DATAOBJ + wxDataObject::wxDataObject() { } @@ -1199,11 +1256,14 @@ void wxDataObject::SetAutoDelete() } #ifdef __WXDEBUG__ -const wxChar *wxDataObject::GetFormatName(wxDataFormat format) +const wxChar *wxDataObject::GetFormatName(wxDataFormat WXUNUSED(format)) { return NULL; } -#endif +#endif // __WXDEBUG__ + +#endif // wxUSE_DATAOBJ + +#endif // wxUSE_OLE/!wxUSE_OLE -#endif