// ----------------------------------------------------------------------------
#ifdef __GNUG__
- #pragma implementation "dataobj.h"
+ #pragma implementation "dataobj.h"
#endif
// For compilers that support precompilation, includes "wx.h".
#include "wx/wxprec.h"
#if defined(__BORLANDC__)
- #pragma hdrstop
+ #pragma hdrstop
#endif
+
+#if defined(__WIN32__) && !defined(__GNUWIN32_OLD__)
+
#ifndef WX_PRECOMP
-#include "wx/intl.h"
+ #include "wx/intl.h"
+ #include "wx/log.h"
#endif
-#include "wx/defs.h"
-
-#if defined(__WIN32__) && !defined(__GNUWIN32__) || defined(wxUSE_NORLANDER_HEADERS)
-#include "wx/log.h"
#include "wx/dataobj.h"
-#include <windows.h>
+#include "wx/msw/private.h" // includes <windows.h>
+
#ifdef wxUSE_NORLANDER_HEADERS
#include <ole2.h>
#endif
#include <oleauto.h>
-#ifndef __WIN32__
+#ifndef __WIN32__
#include <ole2.h>
#include <olestd.h>
#endif
-#include "wx/msw/ole/oleutils.h"
+#include <shlobj.h>
+
+#include "wx/msw/ole/oleutils.h"
+
+#include "wx/msw/dib.h"
// ----------------------------------------------------------------------------
// functions
#ifdef __WXDEBUG__
static const wxChar *GetTymedName(DWORD tymed);
-#endif // Debug
+#else // !Debug
+ #define GetTymedName(tymed) ""
+#endif // Debug/!Debug
// ----------------------------------------------------------------------------
// wxIEnumFORMATETC interface implementation
bool m_mustDelete;
};
-// ----------------------------------------------------------------------------
-// small helper class for getting screen DC (we're working with bitmaps and
-// DIBs here)
-// ----------------------------------------------------------------------------
-
-class ScreenHDC
-{
-public:
- ScreenHDC() { m_hdc = GetDC(NULL); }
- ~ScreenHDC() { ReleaseDC(NULL, m_hdc); }
- operator HDC() const { return m_hdc; }
-
-private:
- HDC m_hdc;
-};
-
// ============================================================================
// implementation
// ============================================================================
return DV_E_FORMATETC;
}
+ if ( !format.IsStandard() ) {
+ // for custom formats, put the size with the data - alloc the
+ // space for it
+ size += sizeof(size_t);
+ }
+
HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, size);
if ( hGlobal == NULL ) {
wxLogLastError("GlobalAlloc");
case TYMED_HGLOBAL:
{
// copy data
- void *pBuf = GlobalLock(pmedium->hGlobal);
+ HGLOBAL hGlobal = pmedium->hGlobal;
+ void *pBuf = GlobalLock(hGlobal);
if ( pBuf == NULL ) {
wxLogLastError(wxT("GlobalLock"));
return E_OUTOFMEMORY;
}
- wxDataFormat format = (wxDataFormatId)pformatetc->cfFormat;
+ if ( !wxDataFormat(pformatetc->cfFormat).IsStandard() ) {
+ // for custom formats, put the size with the data
+ size_t *p = (size_t *)pBuf;
+ *p++ = GlobalSize(hGlobal);
+ pBuf = p;
+ }
+
+ wxDataFormat format = pformatetc->cfFormat;
if ( !m_pDataObject->GetDataHere(format, pBuf) )
return E_UNEXPECTED;
- GlobalUnlock(pmedium->hGlobal);
+ GlobalUnlock(hGlobal);
}
break;
return S_OK;
}
-// set data functions (not implemented)
+// set data functions
STDMETHODIMP wxIDataObject::SetData(FORMATETC *pformatetc,
STGMEDIUM *pmedium,
BOOL fRelease)
switch ( pmedium->tymed )
{
case TYMED_GDI:
- m_pDataObject->SetData(wxDF_BITMAP, &pmedium->hBitmap);
+ m_pDataObject->SetData(wxDF_BITMAP, 0, &pmedium->hBitmap);
break;
case TYMED_MFPICT:
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
+ if ( !m_pDataObject->IsSupportedFormat(format) ) {
+ // go away!
+ return DV_E_FORMATETC;
+ }
+
// copy data
void *pBuf = GlobalLock(pmedium->hGlobal);
if ( pBuf == NULL ) {
return E_OUTOFMEMORY;
}
- wxDataFormat format = (wxDataFormatId)pformatetc->cfFormat;
- m_pDataObject->SetData(format, pBuf);
+ // we've got a problem with SetData() here because the base
+ // class version requires the size parameter which we don't
+ // have anywhere in OLE data transfer - so we need to
+ // synthetise it for known formats and we suppose that all data
+ // in custom formats starts with a DWORD containing the size
+ size_t size;
+ switch ( format )
+ {
+ case CF_TEXT:
+ case CF_OEMTEXT:
+ size = strlen((const char *)pBuf);
+ break;
+#ifndef __WATCOMC__
+ case CF_UNICODETEXT:
+ size = wcslen((const wchar_t *)pBuf);
+ break;
+#endif
+ case CF_BITMAP:
+ 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;
+
+ case CF_DIB:
+ // the handler will calculate size itself (it's too
+ // complicated to do it here)
+ size = 0;
+ break;
+
+ default:
+ {
+ // we suppose that the size precedes the data
+ size_t *p = (size_t *)pBuf;
+ size = *p++;
+ pBuf = p;
+ }
+ }
+
+ bool ok = m_pDataObject->SetData(format, size, pBuf);
GlobalUnlock(pmedium->hGlobal);
+
+ if ( !ok ) {
+ return E_UNEXPECTED;
+ }
}
break;
}
// and now check the type of data requested
- wxDataFormat format = (wxDataFormatId)pformatetc->cfFormat;
+ wxDataFormat format = pformatetc->cfFormat;
if ( m_pDataObject->IsSupportedFormat(format) ) {
-#ifdef __WXDEBUG__
wxLogTrace(wxTRACE_OleCalls, wxT("wxIDataObject::QueryGetData: %s ok"),
- wxDataObject::GetFormatName(format));
-#endif // Debug
+ wxGetFormatName(format));
}
else {
wxLogTrace(wxTRACE_OleCalls,
wxT("wxIDataObject::QueryGetData: %s unsupported"),
- wxDataObject::GetFormatName(format));
+ wxGetFormatName(format));
return DV_E_FORMATETC;
}
if ( (format == wxDF_BITMAP && !(tymed & TYMED_GDI)) &&
!(tymed & TYMED_HGLOBAL) ) {
// it's not what we're waiting for
-#ifdef __WXDEBUG__
wxLogTrace(wxTRACE_OleCalls,
wxT("wxIDataObject::QueryGetData: %s != %s"),
GetTymedName(tymed),
GetTymedName(format == wxDF_BITMAP ? TYMED_GDI
: TYMED_HGLOBAL));
-#endif // Debug
return DV_E_TYMED;
}
return DATA_S_SAMEFORMATETC;
}
-STDMETHODIMP wxIDataObject::EnumFormatEtc(DWORD dwDirection,
+STDMETHODIMP wxIDataObject::EnumFormatEtc(DWORD dwDir,
IEnumFORMATETC **ppenumFormatEtc)
{
wxLogTrace(wxTRACE_OleCalls, wxT("wxIDataObject::EnumFormatEtc"));
- bool allowOutputOnly = dwDirection == DATADIR_GET;
+ wxDataObject::Direction dir = dwDir == DATADIR_GET ? wxDataObject::Get
+ : wxDataObject::Set;
- size_t nFormatCount = m_pDataObject->GetFormatCount(allowOutputOnly);
+ size_t nFormatCount = m_pDataObject->GetFormatCount(dir);
wxDataFormat format, *formats;
- if ( nFormatCount == 1 ) {
- // this is the most common case, this is why we consider it separately
- formats = &format;
- format = m_pDataObject->GetPreferredFormat();
- }
- else {
- // bad luck, build the array with all formats
- formats = new wxDataFormat[nFormatCount];
- m_pDataObject->GetAllFormats(formats, allowOutputOnly);
- }
+ formats = nFormatCount == 1 ? &format : new wxDataFormat[nFormatCount];
+ m_pDataObject->GetAllFormats(formats, dir);
wxIEnumFORMATETC *pEnum = new wxIEnumFORMATETC(formats, nFormatCount);
pEnum->AddRef();
return S_OK;
}
+// ----------------------------------------------------------------------------
// advise sink functions (not implemented)
+// ----------------------------------------------------------------------------
+
STDMETHODIMP wxIDataObject::DAdvise(FORMATETC *pformatetc,
DWORD advf,
IAdviseSink *pAdvSink,
}
#ifdef __WXDEBUG__
+
const char *wxDataObject::GetFormatName(wxDataFormat format)
{
// case 'xxx' is not a valid value for switch of enum 'wxDataFormat'
#pragma warning(default:4063)
#endif // VC++
}
+
#endif // Debug
// ----------------------------------------------------------------------------
-// wxPrivateDataObject
+// wxBitmapDataObject supports CF_DIB format
// ----------------------------------------------------------------------------
-wxPrivateDataObject::wxPrivateDataObject()
+size_t wxBitmapDataObject::GetDataSize() const
{
- m_size = 0;
- m_data = NULL;
+ return wxConvertBitmapToDIB(NULL, GetBitmap());
}
-void wxPrivateDataObject::Free()
+bool wxBitmapDataObject::GetDataHere(void *buf) const
{
- if ( m_data )
- free(m_data);
+ return wxConvertBitmapToDIB((LPBITMAPINFO)buf, GetBitmap()) != 0;
}
-void wxPrivateDataObject::SetData( const void *data, size_t size )
+bool wxBitmapDataObject::SetData(size_t len, const void *buf)
{
- Free();
+ wxBitmap bitmap(wxConvertDIBToBitmap((const LPBITMAPINFO)buf));
+
+ if ( !bitmap.Ok() ) {
+ wxFAIL_MSG(wxT("pasting/dropping invalid bitmap"));
- m_size = size;
- m_data = malloc(size);
+ return FALSE;
+ }
- memcpy( m_data, data, size );
+ SetBitmap(bitmap);
+
+ return TRUE;
}
-void wxPrivateDataObject::WriteData( void *dest ) const
+// ----------------------------------------------------------------------------
+// wxBitmapDataObject2 supports CF_BITMAP format
+// ----------------------------------------------------------------------------
+
+// the bitmaps aren't passed by value as other types of data (i.e. by copying
+// the data into a global memory chunk and passing it to the clipboard or
+// another application or whatever), but by handle, so these generic functions
+// don't make much sense to them.
+
+size_t wxBitmapDataObject2::GetDataSize() const
{
- WriteData( m_data, dest );
+ return 0;
}
-size_t wxPrivateDataObject::GetSize() const
+bool wxBitmapDataObject2::GetDataHere(void *pBuf) const
{
- return m_size;
+ // we put a bitmap handle into pBuf
+ *(WXHBITMAP *)pBuf = GetBitmap().GetHBITMAP();
+
+ return TRUE;
}
-void wxPrivateDataObject::WriteData( const void *data, void *dest ) const
+bool wxBitmapDataObject2::SetData(size_t len, const void *pBuf)
{
- memcpy( dest, data, GetSize() );
-}
+ HBITMAP hbmp = *(HBITMAP *)pBuf;
-// ----------------------------------------------------------------------------
-// wxBitmapDataObject: it supports standard CF_BITMAP and CF_DIB formats
-// ----------------------------------------------------------------------------
+ BITMAP bmp;
+ if ( !GetObject(hbmp, sizeof(BITMAP), &bmp) )
+ {
+ wxLogLastError("GetObject(HBITMAP)");
+ }
-size_t wxBitmapDataObject::GetFormatCount(bool outputOnlyToo) const
-{
- return 2;
-}
+ wxBitmap bitmap(bmp.bmWidth, bmp.bmHeight, bmp.bmPlanes);
+ bitmap.SetHBITMAP((WXHBITMAP)hbmp);
-void wxBitmapDataObject::GetAllFormats(wxDataFormat *formats,
- bool outputOnlyToo) const
-{
- formats[0] = CF_BITMAP;
- formats[1] = CF_DIB;
+ if ( !bitmap.Ok() ) {
+ wxFAIL_MSG(wxT("pasting/dropping invalid bitmap"));
+
+ return FALSE;
+ }
+
+ SetBitmap(bitmap);
+
+ return TRUE;
}
-// the bitmaps aren't passed by value as other types of data (i.e. by copyign
-// the data into a global memory chunk and passing it to the clipboard or
-// another application or whatever), but by handle, so these generic functions
-// don't make much sense to them.
+#if 0
size_t wxBitmapDataObject::GetDataSize(const wxDataFormat& format) const
{
return TRUE;
}
-bool wxBitmapDataObject::SetData(const wxDataFormat& format, const void *pBuf)
+bool wxBitmapDataObject::SetData(const wxDataFormat& format,
+ size_t size, const void *pBuf)
{
HBITMAP hbmp;
if ( format.GetFormatId() == CF_DIB )
return TRUE;
}
+#endif // 0
+
+// ----------------------------------------------------------------------------
+// wxFileDataObject
+// ----------------------------------------------------------------------------
+
+bool wxFileDataObject::SetData(size_t WXUNUSED(size), const void *pData)
+{
+ m_filenames.Empty();
+
+ // the documentation states that the first member of DROPFILES structure is
+ // a "DWORD offset of double NUL terminated file list". What they mean by
+ // this (I wonder if you see it immediately) is that the list starts at
+ // ((char *)&(pDropFiles.pFiles)) + pDropFiles.pFiles. We're also advised
+ // to use DragQueryFile to work with this structure, but not told where and
+ // how to get HDROP.
+ HDROP hdrop = (HDROP)pData; // NB: it works, but I'm not sure about it
+
+ // get number of files (magic value -1)
+ UINT nFiles = ::DragQueryFile(hdrop, (unsigned)-1, NULL, 0u);
+
+ wxCHECK_MSG ( nFiles != (UINT)-1, FALSE, wxT("wrong HDROP handle") );
+
+ // for each file get the length, allocate memory and then get the name
+ wxString str;
+ UINT len, n;
+ for ( n = 0; n < nFiles; n++ ) {
+ // +1 for terminating NUL
+ len = ::DragQueryFile(hdrop, n, NULL, 0) + 1;
+
+ UINT len2 = ::DragQueryFile(hdrop, n, str.GetWriteBuf(len), len);
+ str.UngetWriteBuf();
+ m_filenames.Add(str);
+
+ if ( len2 != len - 1 ) {
+ wxLogDebug(wxT("In wxFileDropTarget::OnDrop DragQueryFile returned"
+ " %d characters, %d expected."), len2, len - 1);
+ }
+ }
+
+ return TRUE;
+}
+
+void wxFileDataObject::AddFile(const wxString& file)
+{
+ // just add file to filenames array
+ // all useful data (such as DROPFILES struct) will be
+ // created later as necessary
+ m_filenames.Add(file);
+}
+
+size_t wxFileDataObject::GetDataSize() const
+{
+ // 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 )
+ return 0;
+
+ // inital size of DROPFILES struct + null byte
+ size_t sz = sizeof(DROPFILES) + 1;
+
+ 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;
+ }
+
+ return sz;
+}
+
+bool wxFileDataObject::GetDataHere(void *pData) const
+{
+ // 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;
+
+ // convert data pointer to a DROPFILES struct pointer
+ LPDROPFILES pDrop = (LPDROPFILES) pData;
+
+ // 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
+
+ // set start of filenames list (null separated)
+ wxChar *pbuf = (wxChar*) ((BYTE *)pDrop + sizeof(DROPFILES));
+
+ size_t count = m_filenames.GetCount();
+ 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');
+ }
+
+ *pbuf = wxT('\0'); // add final null terminator
+
+ return TRUE;
+}
+
// ----------------------------------------------------------------------------
// private functions
// ----------------------------------------------------------------------------
+static size_t wxGetNumOfBitmapColors(size_t bitsPerPixel)
+{
+ switch ( bitsPerPixel )
+ {
+ case 1:
+ // monochrome bitmap, 2 entries
+ return 2;
+
+ case 4:
+ return 16;
+
+ case 8:
+ return 256;
+
+ case 24:
+ // may be used with 24bit bitmaps, but we don't use it here - fall
+ // through
+
+ case 16:
+ case 32:
+ // bmiColors not used at all with these bitmaps
+ return 0;
+
+ default:
+ wxFAIL_MSG( wxT("unknown bitmap format") );
+ return 0;
+ }
+}
+
+size_t wxConvertBitmapToDIB(LPBITMAPINFO pbi, const wxBitmap& bitmap)
+{
+ wxASSERT_MSG( bitmap.Ok(), wxT("invalid bmp can't be converted to DIB") );
+
+ // shouldn't be selected into a DC or GetDIBits() would fail
+ wxASSERT_MSG( !bitmap.GetSelectedInto(),
+ wxT("can't copy bitmap selected into wxMemoryDC") );
+
+ // prepare all the info we need
+ BITMAP bm;
+ HBITMAP hbmp = (HBITMAP)bitmap.GetHBITMAP();
+ if ( !GetObject(hbmp, sizeof(bm), &bm) )
+ {
+ wxLogLastError("GetObject(bitmap)");
+
+ return 0;
+ }
+
+ // calculate the number of bits per pixel and the number of items in
+ // bmiColors array (whose meaning depends on the bitmap format)
+ WORD biBits = bm.bmPlanes * bm.bmBitsPixel;
+ WORD biColors = wxGetNumOfBitmapColors(biBits);
+
+ BITMAPINFO bi2;
+
+ bool wantSizeOnly = pbi == NULL;
+ if ( wantSizeOnly )
+ pbi = &bi2;
+
+ // just for convenience
+ BITMAPINFOHEADER& bi = pbi->bmiHeader;
+
+ bi.biSize = sizeof(BITMAPINFOHEADER);
+ bi.biWidth = bm.bmWidth;
+ bi.biHeight = bm.bmHeight;
+ bi.biPlanes = 1;
+ bi.biBitCount = biBits;
+ bi.biCompression = BI_RGB;
+ bi.biSizeImage = 0;
+ bi.biXPelsPerMeter = 0;
+ bi.biYPelsPerMeter = 0;
+ bi.biClrUsed = 0;
+ bi.biClrImportant = 0;
+
+ // memory we need for BITMAPINFO only
+ DWORD dwLen = bi.biSize + biColors * sizeof(RGBQUAD);
+
+ // first get the image size
+ ScreenHDC hdc;
+ if ( !GetDIBits(hdc, hbmp, 0, bi.biHeight, NULL, pbi, DIB_RGB_COLORS) )
+ {
+ wxLogLastError("GetDIBits(NULL)");
+
+ return 0;
+ }
+
+ if ( wantSizeOnly )
+ {
+ // size of the header + size of the image
+ return dwLen + bi.biSizeImage;
+ }
+
+ // and now copy the bits
+ void *image = (char *)pbi + dwLen;
+ if ( !GetDIBits(hdc, hbmp, 0, bi.biHeight, image, pbi, DIB_RGB_COLORS) )
+ {
+ wxLogLastError("GetDIBits");
+
+ return 0;
+ }
+
+ return dwLen + bi.biSizeImage;
+}
+
+wxBitmap wxConvertDIBToBitmap(const LPBITMAPINFO pbmi)
+{
+ // here we get BITMAPINFO struct followed by the actual bitmap bits and
+ // BITMAPINFO starts with BITMAPINFOHEADER followed by colour info
+ const BITMAPINFOHEADER *pbmih = &pbmi->bmiHeader;
+
+ // offset of image from the beginning of the header
+ DWORD ofs = wxGetNumOfBitmapColors(pbmih->biBitCount) * sizeof(RGBQUAD);
+ void *image = (char *)pbmih + sizeof(BITMAPINFOHEADER) + ofs;
+
+ ScreenHDC hdc;
+ HBITMAP hbmp = CreateDIBitmap(hdc, pbmih, CBM_INIT,
+ image, pbmi, DIB_RGB_COLORS);
+ if ( !hbmp )
+ {
+ wxLogLastError("CreateDIBitmap");
+ }
+
+ wxBitmap bitmap(pbmih->biWidth, pbmih->biHeight, pbmih->biBitCount);
+ bitmap.SetHBITMAP((WXHBITMAP)hbmp);
+
+ return bitmap;
+}
+
#ifdef __WXDEBUG__
static const wxChar *GetTymedName(DWORD tymed)