X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/b9a5faed6a26a148eea3319b8bde8cfb0b64478b..63e1921d0be4956ab729735189ccf637773a7e27:/src/msw/ole/dataobj.cpp?ds=sidebyside diff --git a/src/msw/ole/dataobj.cpp b/src/msw/ole/dataobj.cpp index 3003abdd38..2c2c44603f 100644 --- a/src/msw/ole/dataobj.cpp +++ b/src/msw/ole/dataobj.cpp @@ -1,5 +1,5 @@ /////////////////////////////////////////////////////////////////////////////// -// Name: msw/ole/dataobj.cpp +// Name: src/msw/ole/dataobj.cpp // Purpose: implementation of wx[I]DataObject class // Author: Vadim Zeitlin // Modified by: @@ -35,6 +35,8 @@ #if wxUSE_OLE && defined(__WIN32__) && !defined(__GNUWIN32_OLD__) +#include "wx/scopedarray.h" +#include "wx/vector.h" #include "wx/msw/private.h" // includes #ifdef __WXWINCE__ @@ -54,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 @@ -90,7 +182,7 @@ private: ULONG m_nCount, // number of formats we support m_nCurrent; // current enum position - DECLARE_NO_COPY_CLASS(wxIEnumFORMATETC) + wxDECLARE_NO_COPY_CLASS(wxIEnumFORMATETC); }; // ---------------------------------------------------------------------------- @@ -126,9 +218,123 @@ private: 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,7 +345,7 @@ private: void wxDataFormat::SetId(const wxString& format) { - m_format = (wxDataFormat::NativeFormat)::RegisterClipboardFormat(format.wx_str()); + m_format = (wxDataFormat::NativeFormat)::RegisterClipboardFormat(format.t_str()); if ( !m_format ) { wxLogError(_("Couldn't register clipboard format '%s'."), format); @@ -183,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")); } } @@ -271,6 +480,14 @@ wxIDataObject::wxIDataObject(wxDataObject *pDataObject) 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; @@ -290,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 ) { @@ -426,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 @@ -459,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(__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); @@ -568,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"), @@ -617,20 +860,31 @@ STDMETHODIMP wxIDataObject::EnumFormatEtc(DWORD dwDir, wxDataObject::Direction dir = dwDir == DATADIR_GET ? wxDataObject::Get : wxDataObject::Set; - ULONG nFormatCount = wx_truncate_cast(ULONG, 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; } @@ -676,7 +930,7 @@ 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; } @@ -693,7 +947,7 @@ const void *wxDataObject::GetSizeFromBuffer(const void *buffer, // 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)); + const_cast(buffer)); if ( realsz == (size_t)-1 ) { // note that HeapSize() does not set last error @@ -719,7 +973,7 @@ void* wxDataObject::SetSizeInBuffer( void* buffer, size_t size, return p; } -#ifdef __WXDEBUG__ +#if wxDEBUG_LEVEL const wxChar *wxDataObject::GetFormatName(wxDataFormat format) { @@ -764,7 +1018,7 @@ const wxChar *wxDataObject::GetFormatName(wxDataFormat format) #endif // VC++ } -#endif // Debug +#endif // wxDEBUG_LEVEL // ---------------------------------------------------------------------------- // wxBitmapDataObject supports CF_DIB format @@ -852,7 +1106,7 @@ 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; @@ -898,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 ) @@ -977,7 +1231,7 @@ 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; } @@ -1052,7 +1306,7 @@ size_t wxFileDataObject::GetDataSize() const if ( wxGetOsVersion() == wxOS_WINDOWS_9X ) { // Win9x always uses ANSI file names and MSLU doesn't help with this - sizeOfChar = sizeof(char); + sizeOfChar = 1; } else { @@ -1062,7 +1316,7 @@ size_t wxFileDataObject::GetDataSize() const static const size_t sizeOfChar = sizeof(wxChar); #endif // wxUSE_UNICODE_MSLU/!wxUSE_UNICODE_MSLU - // inital size of DROPFILES struct + null byte + // initial size of DROPFILES struct + null byte size_t sz = sizeof(DROPFILES) + sizeOfChar; const size_t count = m_filenames.size(); @@ -1071,7 +1325,7 @@ size_t wxFileDataObject::GetDataSize() const // add filename length plus null byte size_t len; #if wxUSE_UNICODE_MSLU - if ( sizeOfChar == sizeof(char) ) + if ( sizeOfChar == 1 ) len = strlen(m_filenames[i].mb_str(*wxConvFileName)); else #endif // wxUSE_UNICODE_MSLU @@ -1108,7 +1362,7 @@ bool wxFileDataObject::GetDataHere(void *WXUNUSED_IN_WINCE(pData)) const pDrop->fWide = wxUSE_UNICODE; #endif - const size_t sizeOfChar = pDrop->fWide ? sizeof(wchar_t) : sizeof(char); + const size_t sizeOfChar = pDrop->fWide ? sizeof(wchar_t) : 1; // set start of filenames list (null separated) BYTE *pbuf = (BYTE *)(pDrop + 1); @@ -1119,7 +1373,7 @@ bool wxFileDataObject::GetDataHere(void *WXUNUSED_IN_WINCE(pData)) const // copy filename to pbuf and add null terminator size_t len; #if wxUSE_UNICODE_MSLU - if ( sizeOfChar == sizeof(char) ) + if ( sizeOfChar == 1 ) { wxCharBuffer buf(m_filenames[i].mb_str(*wxConvFileName)); len = strlen(buf); @@ -1129,7 +1383,7 @@ bool wxFileDataObject::GetDataHere(void *WXUNUSED_IN_WINCE(pData)) const #endif // wxUSE_UNICODE_MSLU { len = m_filenames[i].length(); - memcpy(pbuf, m_filenames[i].wx_str(), len*sizeOfChar); + memcpy(pbuf, m_filenames[i].t_str(), len*sizeOfChar); } pbuf += len*sizeOfChar; @@ -1154,7 +1408,7 @@ bool wxFileDataObject::GetDataHere(void *WXUNUSED_IN_WINCE(pData)) const // Work around bug in Wine headers #if defined(__WINE__) && defined(CFSTR_SHELLURL) && wxUSE_UNICODE #undef CFSTR_SHELLURL -#define CFSTR_SHELLURL _T("CFSTR_SHELLURL") +#define CFSTR_SHELLURL wxT("CFSTR_SHELLURL") #endif class CFSTR_SHELLURLDataObject : public wxCustomDataObject @@ -1182,24 +1436,7 @@ public: 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); }; @@ -1234,11 +1471,30 @@ 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; } @@ -1248,15 +1504,22 @@ void wxURLDataObject::SetURL(const wxString& url) wxCharBuffer urlMB(url.mb_str()); if ( urlMB ) { - const size_t len = strlen(urlMB) + 1; // size with trailing NUL + 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 - SetData(wxDataFormat(CFSTR_SHELLURL), 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); } #if wxUSE_UNICODE - SetData(wxDF_UNICODETEXT, url.length() + 1, url.wc_str()); + SetData(wxDF_UNICODETEXT, url.length()*sizeof(wxChar), url.wc_str()); #endif } @@ -1264,7 +1527,7 @@ void wxURLDataObject::SetURL(const wxString& url) // private functions // ---------------------------------------------------------------------------- -#ifdef __WXDEBUG__ +#if wxDEBUG_LEVEL static const wxChar *GetTymedName(DWORD tymed) { @@ -1305,12 +1568,10 @@ void wxDataObject::SetAutoDelete() { } -#ifdef __WXDEBUG__ const wxChar *wxDataObject::GetFormatName(wxDataFormat WXUNUSED(format)) { return NULL; } -#endif // __WXDEBUG__ #endif // wxUSE_DATAOBJ