X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/de564874d60233c977c93496da1aa406245c37c2..d44bc7333a3d4cbb4a37ba6cdb02cb6b637e44ed:/src/msw/ole/dataobj.cpp diff --git a/src/msw/ole/dataobj.cpp b/src/msw/ole/dataobj.cpp index ac37833d0d..540d2330bf 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" @@ -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" @@ -56,18 +54,18 @@ #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 // ---------------------------------------------------------------------------- // wxIEnumFORMATETC interface implementation @@ -79,20 +77,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 +104,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,12 +119,14 @@ 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); }; // ============================================================================ @@ -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; @@ -267,7 +266,7 @@ IMPLEMENT_IUNKNOWN_METHODS(wxIDataObject) wxIDataObject::wxIDataObject(wxDataObject *pDataObject) { m_pDataObject = pDataObject; - m_mustDelete = FALSE; + m_mustDelete = false; } wxIDataObject::~wxIDataObject() @@ -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; @@ -465,7 +463,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__) ) @@ -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 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); @@ -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,30 +680,46 @@ 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, + 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) { @@ -723,7 +732,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"); @@ -734,9 +742,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)) ) @@ -753,15 +764,17 @@ const wxChar *wxDataObject::GetFormatName(wxDataFormat format) #endif // VC++ } -#endif // Debug +#endif // wxDEBUG_LEVEL // ---------------------------------------------------------------------------- // wxBitmapDataObject supports CF_DIB format // ---------------------------------------------------------------------------- +// TODO: support CF_DIB under Windows CE as well + size_t wxBitmapDataObject::GetDataSize() const { -#if wxUSE_WXDIB +#if wxUSE_WXDIB && !defined(__WXWINCE__) return wxDIB::ConvertFromBitmap(NULL, GetHbitmapOf(GetBitmap())); #else return 0; @@ -770,18 +783,19 @@ size_t wxBitmapDataObject::GetDataSize() const bool wxBitmapDataObject::GetDataHere(void *buf) const { -#if wxUSE_WXDIB +#if wxUSE_WXDIB && !defined(__WXWINCE__) BITMAPINFO * const pbi = (BITMAPINFO *)buf; return wxDIB::ConvertFromBitmap(pbi, GetHbitmapOf(GetBitmap())) != 0; #else - return FALSE; + wxUnusedVar(buf); + return false; #endif } bool wxBitmapDataObject::SetData(size_t WXUNUSED(len), const void *buf) { -#if wxUSE_WXDIB +#if wxUSE_WXDIB && !defined(__WXWINCE__) const BITMAPINFO * const pbmi = (const BITMAPINFO *)buf; HBITMAP hbmp = wxDIB::ConvertToBitmap(pbmi); @@ -796,9 +810,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 } @@ -821,7 +836,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) @@ -840,12 +855,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 @@ -910,7 +925,7 @@ bool wxBitmapDataObject::GetDataHere(const wxDataFormat& format, { wxLogLastError(wxT("GetDIBits")); - return FALSE; + return false; } } else // CF_BITMAP @@ -919,7 +934,7 @@ bool wxBitmapDataObject::GetDataHere(const wxDataFormat& format, *(HBITMAP *)pBuf = hbmp; } - return TRUE; + return true; } bool wxBitmapDataObject::SetData(const wxDataFormat& format, @@ -964,7 +979,7 @@ bool wxBitmapDataObject::SetData(const wxDataFormat& format, wxASSERT_MSG( m_bitmap.Ok(), wxT("pasting invalid bitmap") ); - return TRUE; + return true; } #endif // 0 @@ -973,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(); @@ -1007,9 +1023,9 @@ bool wxFileDataObject::SetData(size_t WXUNUSED(size), const void *pData) } } - return TRUE; + return true; #else - return FALSE; + return false; #endif } @@ -1024,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; + 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; + 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; @@ -1048,15 +1086,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; @@ -1064,31 +1102,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) : 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); - 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; + return true; #else - return FALSE; + return false; #endif } @@ -1096,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 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; @@ -1121,33 +1182,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; - } -#endif + 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, @@ -1165,31 +1217,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) { @@ -1211,10 +1295,13 @@ static const wxChar *GetTymedName(DWORD tymed) #endif // Debug #else // not using OLE at all + // ---------------------------------------------------------------------------- // wxDataObject // ---------------------------------------------------------------------------- +#if wxUSE_DATAOBJ + wxDataObject::wxDataObject() { } @@ -1227,12 +1314,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 +