///////////////////////////////////////////////////////////////////////////////
-// Name: msw/ole/dataobj.cpp
+// Name: src/msw/ole/dataobj.cpp
// Purpose: implementation of wx[I]DataObject class
// Author: Vadim Zeitlin
// Modified by:
#if wxUSE_OLE && defined(__WIN32__) && !defined(__GNUWIN32_OLD__)
+#include "wx/scopedarray.h"
+#include "wx/vector.h"
#include "wx/msw/private.h" // includes <windows.h>
#ifdef __WXWINCE__
#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
ULONG m_nCount, // number of formats we support
m_nCurrent; // current enum position
- DECLARE_NO_COPY_CLASS(wxIEnumFORMATETC)
+ wxDECLARE_NO_COPY_CLASS(wxIEnumFORMATETC);
};
// ----------------------------------------------------------------------------
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<SystemDataEntry*> 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
// ============================================================================
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);
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"));
}
}
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;
// 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 )
{
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
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);
// 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"),
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);
+
+ // fill format array with formats ...
+ wxScopedArray<wxDataFormat> formats(new wxDataFormat[nFormatCount]);
+
+ // ... from content data (supported formats)
+ m_pDataObject->GetAllFormats(formats.get(), dir);
- if ( formats != &format ) {
- delete [] formats;
+ // ... 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;
}
((wxIDataObject *)m_pIDataObject)->SetDeleteFlag();
m_pIDataObject->Release();
- // so that the dtor doesnt' crash
+ // so that the dtor doesn't crash
m_pIDataObject = NULL;
}
return NeedsVerbatimData(format) ? 0 : sizeof(size_t);
}
-const void* wxDataObject::GetSizeFromBuffer( const void* buffer, size_t* size,
- const wxDataFormat& format )
+const void *wxDataObject::GetSizeFromBuffer(const void *buffer,
+ size_t *size,
+ const wxDataFormat& WXUNUSED(format))
{
// hack: the third parameter is declared non-const in Wine's headers so
// cast away the const
- size_t realsz = ::HeapSize(::GetProcessHeap(), 0,
- wx_const_cast(void*, buffer));
+ const size_t realsz = ::HeapSize(::GetProcessHeap(), 0,
+ const_cast<void*>(buffer));
if ( realsz == (size_t)-1 )
{
// note that HeapSize() does not set last error
*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;
+ return buffer;
}
void* wxDataObject::SetSizeInBuffer( void* buffer, size_t size,
return p;
}
-#ifdef __WXDEBUG__
+#if wxDEBUG_LEVEL
const wxChar *wxDataObject::GetFormatName(wxDataFormat format)
{
#endif // VC++
}
-#endif // Debug
+#endif // wxDEBUG_LEVEL
// ----------------------------------------------------------------------------
// wxBitmapDataObject supports CF_DIB format
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;
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 )
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;
}
if ( wxGetOsVersion() == wxOS_WINDOWS_9X )
{
// Win9x always uses ANSI file names and MSLU doesn't help with this
- sizeOfChar = sizeof(char);
+ sizeOfChar = 1;
}
else
{
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();
// 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
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);
// 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);
#endif // wxUSE_UNICODE_MSLU
{
len = m_filenames[i].length();
- memcpy(pbuf, m_filenames[i].c_str(), len*sizeOfChar);
+ memcpy(pbuf, m_filenames[i].t_str(), len*sizeOfChar);
}
pbuf += len*sizeOfChar;
// 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
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);
};
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<wxTextDataObject *>(m_dataObjectLast)->GetText();
+ }
return 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);
- 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);
}
- SetData(wxDF_UNICODETEXT, url.length() + 1, url.wc_str());
+#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)
{
{
}
-#ifdef __WXDEBUG__
const wxChar *wxDataObject::GetFormatName(wxDataFormat WXUNUSED(format))
{
return NULL;
}
-#endif // __WXDEBUG__
#endif // wxUSE_DATAOBJ