wxDataObject changes - ok for MSW, still not for GTK/Motif/...

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@4090 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
1999-10-21 01:58:57 +00:00
parent e1ee679c2e
commit 9e2896e5c8
17 changed files with 852 additions and 589 deletions

View File

@@ -33,6 +33,7 @@
#endif
#include "wx/thread.h"
#include "wx/clipbrd.h"
// ===========================================================================
// implementation

View File

@@ -175,6 +175,10 @@ bool wxTextDataObject::SetData(size_t WXUNUSED(len), const void *buf)
// wxFileDataObjectBase
// ----------------------------------------------------------------------------
// VZ: I don't need this in MSW finally, so if it is needed in wxGTK, it should
// be moved to gtk/dataobj.cpp
#if 0
wxString wxFileDataObjectBase::GetFilenames() const
{
wxString str;
@@ -212,6 +216,8 @@ void wxFileDataObjectBase::SetFilenames(const wxChar* filenames)
}
}
#endif // 0
// ----------------------------------------------------------------------------
// wxCustomDataObject
// ----------------------------------------------------------------------------
@@ -257,7 +263,7 @@ bool wxCustomDataObject::GetDataHere(void *buf) const
return TRUE;
}
bool wxCustomDataObject::SetData(size_t len, const void *buf)
bool wxCustomDataObject::SetData(size_t size, const void *buf)
{
Free();
@@ -265,7 +271,7 @@ bool wxCustomDataObject::SetData(size_t len, const void *buf)
if ( !m_data )
return FALSE;
memcpy(m_data, buf, m_size = len);
memcpy(m_data, buf, m_size = size);
return TRUE;
}

View File

@@ -584,7 +584,7 @@ bool wxClipboard::GetData( wxDataObject *data )
}
// build the list of supported formats
size_t nFormats = data->GetFormatCount(FALSE /* for SetData() */);
size_t nFormats = data->GetFormatCount(wxDataObject::Set);
wxDataFormat format, *formats;
if ( nFormats == 1 )
{
@@ -597,7 +597,7 @@ bool wxClipboard::GetData( wxDataObject *data )
formats = new wxDataFormat[nFormats];
}
data->GetAllFormats(formats, FALSE);
data->GetAllFormats(formats, wxDataObject::Set);
// get the format enumerator
bool result = FALSE;

View File

@@ -58,6 +58,10 @@
static const wxChar *GetTymedName(DWORD tymed);
#endif // Debug
// to be moved into wx/msw/bitmap.h
extern size_t wxConvertBitmapToDIB(BITMAPINFO *pbi, const wxBitmap& bitmap);
extern wxBitmap wxConvertDIBToBitmap(const BITMAPINFO *bmi);
// ----------------------------------------------------------------------------
// wxIEnumFORMATETC interface implementation
// ----------------------------------------------------------------------------
@@ -320,6 +324,12 @@ 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
size += sizeof(size_t);
}
HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, size);
if ( hGlobal == NULL ) {
wxLogLastError("GlobalAlloc");
@@ -369,17 +379,25 @@ STDMETHODIMP wxIDataObject::GetDataHere(FORMATETC *pformatetc,
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;
@@ -390,7 +408,7 @@ STDMETHODIMP wxIDataObject::GetDataHere(FORMATETC *pformatetc,
return S_OK;
}
// set data functions (not implemented)
// set data functions
STDMETHODIMP wxIDataObject::SetData(FORMATETC *pformatetc,
STGMEDIUM *pmedium,
BOOL fRelease)
@@ -400,7 +418,7 @@ STDMETHODIMP wxIDataObject::SetData(FORMATETC *pformatetc,
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:
@@ -419,10 +437,47 @@ STDMETHODIMP wxIDataObject::SetData(FORMATETC *pformatetc,
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 ( pformatetc->cfFormat )
{
case CF_TEXT:
case CF_OEMTEXT:
size = strlen((const char *)pBuf);
break;
case CF_UNICODETEXT:
size = wcslen((const wchar_t *)pBuf);
break;
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;
default:
{
// we suppose that the size precedes the data
size_t *p = (size_t *)pBuf;
size = *p++;
pBuf = p;
}
}
wxDataFormat format = pformatetc->cfFormat;
bool ok = m_pDataObject->SetData(format, size, pBuf);
GlobalUnlock(pmedium->hGlobal);
if ( !ok ) {
return E_UNEXPECTED;
}
}
break;
@@ -474,7 +529,7 @@ STDMETHODIMP wxIDataObject::QueryGetData(FORMATETC *pformatetc)
}
// 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"),
@@ -482,11 +537,10 @@ STDMETHODIMP wxIDataObject::QueryGetData(FORMATETC *pformatetc)
#endif // Debug
}
else {
#ifdef __WXDEBUG__
wxLogTrace(wxTRACE_OleCalls,
wxT("wxIDataObject::QueryGetData: %s unsupported"),
wxDataObject::GetFormatName(format));
#endif
return DV_E_FORMATETC;
}
@@ -521,25 +575,18 @@ STDMETHODIMP wxIDataObject::GetCanonicalFormatEtc(FORMATETC *pFormatetcIn,
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();
@@ -552,7 +599,10 @@ STDMETHODIMP wxIDataObject::EnumFormatEtc(DWORD dwDirection,
return S_OK;
}
// ----------------------------------------------------------------------------
// advise sink functions (not implemented)
// ----------------------------------------------------------------------------
STDMETHODIMP wxIDataObject::DAdvise(FORMATETC *pformatetc,
DWORD advf,
IAdviseSink *pAdvSink,
@@ -656,67 +706,82 @@ const char *wxDataObject::GetFormatName(wxDataFormat format)
#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((BITMAPINFO *)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 BITMAPINFO *)buf));
m_size = size;
m_data = malloc(size);
if ( !bitmap.Ok() ) {
wxFAIL_MSG(wxT("pasting/dropping invalid bitmap"));
memcpy( m_data, data, size );
}
return FALSE;
}
void wxPrivateDataObject::WriteData( void *dest ) const
{
WriteData( m_data, dest );
}
SetBitmap(bitmap);
size_t wxPrivateDataObject::GetSize() const
{
return m_size;
}
void wxPrivateDataObject::WriteData( const void *data, void *dest ) const
{
memcpy( dest, data, GetSize() );
return TRUE;
}
// ----------------------------------------------------------------------------
// wxBitmapDataObject: it supports standard CF_BITMAP and CF_DIB formats
// wxBitmapDataObject2 supports CF_BITMAP format
// ----------------------------------------------------------------------------
size_t wxBitmapDataObject::GetFormatCount(bool outputOnlyToo) const
{
return 2;
}
void wxBitmapDataObject::GetAllFormats(wxDataFormat *formats,
bool outputOnlyToo) const
{
formats[0] = CF_BITMAP;
formats[1] = CF_DIB;
}
// the bitmaps aren't passed by value as other types of data (i.e. by copyign
// 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
{
return 0;
}
bool wxBitmapDataObject2::GetDataHere(void *pBuf) const
{
// we put a bitmap handle into pBuf
*(WXHBITMAP *)pBuf = GetBitmap().GetHBITMAP();
return TRUE;
}
bool wxBitmapDataObject2::SetData(size_t len, const void *pBuf)
{
HBITMAP hbmp = *(HBITMAP *)pBuf;
BITMAP bmp;
if ( !GetObject(hbmp, sizeof(BITMAP), &bmp) )
{
wxLogLastError("GetObject(HBITMAP)");
}
wxBitmap bitmap(bmp.bmWidth, bmp.bmHeight, bmp.bmPlanes);
bitmap.SetHBITMAP((WXHBITMAP)hbmp);
if ( !bitmap.Ok() ) {
wxFAIL_MSG(wxT("pasting/dropping invalid bitmap"));
return FALSE;
}
SetBitmap(bitmap);
return TRUE;
}
#if 0
size_t wxBitmapDataObject::GetDataSize(const wxDataFormat& format) const
{
if ( format.GetFormatId() == CF_DIB )
@@ -789,7 +854,8 @@ bool wxBitmapDataObject::GetDataHere(const wxDataFormat& format,
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 )
@@ -833,10 +899,120 @@ bool wxBitmapDataObject::SetData(const wxDataFormat& format, const void *pBuf)
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);
// 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;
}
// ----------------------------------------------------------------------------
// private functions
// ----------------------------------------------------------------------------
// otherwise VC++ would give here:
// "local variable 'bi' may be used without having been initialized"
// even though in fact it may not
#ifdef __VISUALC__
#pragma warning(disable:4701)
#endif // __VISUALC__
size_t wxConvertBitmapToDIB(BITMAPINFO *pbi, const wxBitmap& bitmap)
{
// shouldn't be selected into a DC or GetDIBits() would fail
wxASSERT_MSG( !bitmap.GetSelectedInto(),
wxT("can't copy bitmap selected into wxMemoryDC") );
HBITMAP hbmp = (HBITMAP)bitmap.GetHBITMAP();
BITMAPINFO bi;
// first get the info
ScreenHDC hdc;
if ( !GetDIBits(hdc, hbmp, 0, 0, NULL, pbi ? pbi : &bi, DIB_RGB_COLORS) )
{
wxLogLastError("GetDIBits(NULL)");
return 0;
}
if ( !pbi )
{
// we were only asked for size needed for the buffer, not to actually
// copy the data
return sizeof(BITMAPINFO) + bi.bmiHeader.biSizeImage;
}
// and now copy the bits
if ( !GetDIBits(hdc, hbmp, 0, pbi->bmiHeader.biHeight, pbi + 1,
pbi, DIB_RGB_COLORS) )
{
wxLogLastError("GetDIBits");
return 0;
}
return sizeof(BITMAPINFO) + pbi->bmiHeader.biSizeImage;
}
#ifdef __VISUALC__
#pragma warning(default:4701)
#endif // __VISUALC__
wxBitmap wxConvertDIBToBitmap(const BITMAPINFO *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;
ScreenHDC hdc;
HBITMAP hbmp = CreateDIBitmap(hdc, pbmih, CBM_INIT,
pbmi + 1, 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)

View File

@@ -36,17 +36,17 @@
#if wxUSE_DRAG_AND_DROP
#include "wx/log.h"
#include "wx/dataobj.h"
#include "wx/msw/ole/dropsrc.h"
#include "wx/dnd.h"
#include <windows.h>
#ifdef wxUSE_NORLANDER_HEADERS
#include <ole2.h>
#include <ole2.h>
#endif
#ifndef __WIN32__
#include <ole2.h>
#include <olestd.h>
#include <ole2.h>
#include <olestd.h>
#endif
#include <oleauto.h>
@@ -165,7 +165,6 @@ wxDropSource::wxDropSource(wxWindow* WXUNUSED(win),
const wxIcon & WXUNUSED(stop))
{
Init();
m_pData = NULL;
}
wxDropSource::wxDropSource(wxDataObject& data,
@@ -189,10 +188,10 @@ wxDropSource::~wxDropSource()
// Notes : you must call SetData() before if you had used def ctor
wxDragResult wxDropSource::DoDragDrop(bool bAllowMove)
{
wxCHECK_MSG( m_pData != NULL, wxDragNone, wxT("No data in wxDropSource!") );
wxCHECK_MSG( m_data != NULL, wxDragNone, wxT("No data in wxDropSource!") );
DWORD dwEffect;
HRESULT hr = ::DoDragDrop(m_pData->GetInterface(),
HRESULT hr = ::DoDragDrop(m_data->GetInterface(),
m_pIDropSource,
bAllowMove ? DROPEFFECT_COPY | DROPEFFECT_MOVE
: DROPEFFECT_COPY,

View File

@@ -42,8 +42,7 @@
#include <shellapi.h>
#endif
#include "wx/dataobj.h"
#include "wx/msw/ole/droptgt.h"
#include "wx/dnd.h"
#ifndef __WIN32__
#include <ole2.h>
@@ -70,19 +69,12 @@ public:
STDMETHODIMP DragLeave(void);
STDMETHODIMP Drop(LPDATAOBJECT, DWORD, POINTL, LPDWORD);
// we assume that if QueryGetData() returns S_OK, than we can really get data
// in this format, so we remember here the format for which QueryGetData()
// succeeded
void SetSupportedFormat(wxDataFormat cfFormat) { m_cfFormat = cfFormat; }
DECLARE_IUNKNOWN_METHODS;
protected:
IDataObject *m_pIDataObject; // !NULL between DragEnter and DragLeave/Drop
wxDropTarget *m_pTarget; // the real target (we're just a proxy)
wxDataFormat m_cfFormat; // the format in which to ask for data
private:
static inline DWORD GetDropEffect(DWORD flags);
};
@@ -108,7 +100,6 @@ wxIDropTarget::wxIDropTarget(wxDropTarget *pTarget)
{
m_cRef = 0;
m_pTarget = pTarget;
m_cfFormat = wxDF_INVALID;
m_pIDataObject = NULL;
}
@@ -212,45 +203,33 @@ STDMETHODIMP wxIDropTarget::Drop(IDataObject *pIDataSource,
POINTL pt,
DWORD *pdwEffect)
{
wxLogDebug(wxT("IDropTarget::Drop"));
wxLogDebug(wxT("IDropTarget::Drop"));
// TODO I don't know why there is this parameter, but so far I assume
// that it's the same we've already got in DragEnter
wxASSERT( m_pIDataObject == pIDataSource );
// TODO I don't know why there is this parameter, but so far I assume
// that it's the same we've already got in DragEnter
wxASSERT( m_pIDataObject == pIDataSource );
STGMEDIUM stm;
*pdwEffect = DROPEFFECT_NONE;
// by default, nothing happens
*pdwEffect = DROPEFFECT_NONE;
// should be set by SetSupportedFormat() call
wxASSERT( m_cfFormat != wxDF_INVALID );
// first ask the drop target if it wants data
if ( m_pTarget->OnDrop(pt.x, pt.y) ) {
// it does, so give it the data source
m_pTarget->SetDataSource(pIDataSource);
FORMATETC fmtMemory;
fmtMemory.cfFormat = m_cfFormat;
fmtMemory.ptd = NULL;
fmtMemory.dwAspect = DVASPECT_CONTENT;
fmtMemory.lindex = -1;
fmtMemory.tymed = TYMED_HGLOBAL; // TODO to add other media
HRESULT hr = pIDataSource->GetData(&fmtMemory, &stm);
if ( SUCCEEDED(hr) ) {
if ( stm.hGlobal != NULL ) {
if ( m_pTarget->OnDrop(pt.x, pt.y, GlobalLock(stm.hGlobal)) )
*pdwEffect = GetDropEffect(grfKeyState);
//else: DROPEFFECT_NONE
GlobalUnlock(stm.hGlobal);
ReleaseStgMedium(&stm);
// and now it has the data
if ( m_pTarget->OnData(pt.x, pt.y) ) {
// operation succeeded
*pdwEffect = GetDropEffect(grfKeyState);
}
//else: *pdwEffect is already DROPEFFECT_NONE
}
}
else
{
// wxLogApiError("GetData", hr);
}
//else: OnDrop() returned FALSE, no need to copy data
// release the held object
RELEASE_AND_NULL(m_pIDataObject);
// release the held object
RELEASE_AND_NULL(m_pIDataObject);
return S_OK;
return S_OK;
}
// ============================================================================
@@ -261,17 +240,18 @@ STDMETHODIMP wxIDropTarget::Drop(IDataObject *pIDataSource,
// ctor/dtor
// ----------------------------------------------------------------------------
wxDropTarget::wxDropTarget()
wxDropTarget::wxDropTarget(wxDataObject *dataObj)
: wxDropTargetBase(dataObj)
{
// create an IDropTarget implementation which will notify us about
// d&d operations.
m_pIDropTarget = new wxIDropTarget(this);
m_pIDropTarget->AddRef();
// create an IDropTarget implementation which will notify us about d&d
// operations.
m_pIDropTarget = new wxIDropTarget(this);
m_pIDropTarget->AddRef();
}
wxDropTarget::~wxDropTarget()
{
ReleaseInterface(m_pIDropTarget);
ReleaseInterface(m_pIDropTarget);
}
// ----------------------------------------------------------------------------
@@ -280,140 +260,179 @@ wxDropTarget::~wxDropTarget()
bool wxDropTarget::Register(WXHWND hwnd)
{
HRESULT hr = ::CoLockObjectExternal(m_pIDropTarget, TRUE, FALSE);
if ( FAILED(hr) ) {
wxLogApiError("CoLockObjectExternal", hr);
return FALSE;
}
HRESULT hr = ::CoLockObjectExternal(m_pIDropTarget, TRUE, FALSE);
if ( FAILED(hr) ) {
wxLogApiError("CoLockObjectExternal", hr);
return FALSE;
}
hr = ::RegisterDragDrop((HWND) hwnd, m_pIDropTarget);
if ( FAILED(hr) ) {
::CoLockObjectExternal(m_pIDropTarget, FALSE, FALSE);
hr = ::RegisterDragDrop((HWND) hwnd, m_pIDropTarget);
if ( FAILED(hr) ) {
::CoLockObjectExternal(m_pIDropTarget, FALSE, FALSE);
wxLogApiError("RegisterDragDrop", hr);
return FALSE;
}
wxLogApiError("RegisterDragDrop", hr);
return FALSE;
}
return TRUE;
return TRUE;
}
void wxDropTarget::Revoke(WXHWND hwnd)
{
HRESULT hr = ::RevokeDragDrop((HWND) hwnd);
HRESULT hr = ::RevokeDragDrop((HWND) hwnd);
if ( FAILED(hr) ) {
wxLogApiError("RevokeDragDrop", hr);
}
if ( FAILED(hr) ) {
wxLogApiError("RevokeDragDrop", hr);
}
::CoLockObjectExternal(m_pIDropTarget, FALSE, TRUE);
::CoLockObjectExternal(m_pIDropTarget, FALSE, TRUE);
}
// ----------------------------------------------------------------------------
// determine if we accept data of this type
// base class pure virtuals
// ----------------------------------------------------------------------------
// OnDrop() is called only if we previously returned TRUE from
// IsAcceptedData(), so no need to check anything here
bool wxDropTarget::OnDrop(wxCoord WXUNUSED(x), wxCoord WXUNUSED(y))
{
return TRUE;
}
// copy the data from the data source to the target data object
bool wxDropTarget::GetData()
{
wxDataFormat format = GetSupportedFormat(m_pIDataSource);
if ( format == wxDF_INVALID ) {
// this is strange because IsAcceptedData() succeeded previously!
wxFAIL_MSG(wxT("strange - did supported formats list change?"));
return FALSE;
}
STGMEDIUM stm;
FORMATETC fmtMemory;
fmtMemory.cfFormat = format;
fmtMemory.ptd = NULL;
fmtMemory.dwAspect = DVASPECT_CONTENT;
fmtMemory.lindex = -1;
fmtMemory.tymed = TYMED_HGLOBAL; // TODO to add other media
bool rc = FALSE;
HRESULT hr = m_pIDataSource->GetData(&fmtMemory, &stm);
if ( SUCCEEDED(hr) ) {
IDataObject *dataObject = m_dataObject->GetInterface();
hr = dataObject->SetData(&fmtMemory, &stm, TRUE);
if ( SUCCEEDED(hr) ) {
rc = TRUE;
}
else {
wxLogLastError("IDataObject::SetData()");
}
}
else {
wxLogLastError("IDataObject::GetData()");
}
return rc;
}
// ----------------------------------------------------------------------------
// callbacks used by wxIDropTarget
// ----------------------------------------------------------------------------
// we need a data source, so wxIDropTarget gives it to us using this function
void wxDropTarget::SetDataSource(IDataObject *pIDataSource)
{
m_pIDataSource = pIDataSource;
}
// determine if we accept data of this type
bool wxDropTarget::IsAcceptedData(IDataObject *pIDataSource) const
{
// this strucutre describes a data of any type (first field will be
// changing) being passed through global memory block.
static FORMATETC s_fmtMemory = {
0,
NULL,
DVASPECT_CONTENT,
-1,
TYMED_HGLOBAL
};
// cycle thorugh all supported formats
for ( size_t n = 0; n < GetFormatCount(); n++ ) {
s_fmtMemory.cfFormat = GetFormat(n);
// NB: don't use SUCCEEDED macro here: QueryGetData returns 1 (whatever it
// means) for file drag and drop
if ( pIDataSource->QueryGetData(&s_fmtMemory) == S_OK ) {
// remember this format: we'll later ask for data in it
m_pIDropTarget->SetSupportedFormat((unsigned int)s_fmtMemory.cfFormat);
return TRUE;
}
}
return FALSE;
return GetSupportedFormat(pIDataSource) != wxDF_INVALID;
}
// ============================================================================
// ----------------------------------------------------------------------------
// helper functions
// ----------------------------------------------------------------------------
wxDataFormat wxDropTarget::GetSupportedFormat(IDataObject *pIDataSource) const
{
// this strucutre describes a data of any type (first field will be
// changing) being passed through global memory block.
static FORMATETC s_fmtMemory = {
0,
NULL,
DVASPECT_CONTENT,
-1,
TYMED_HGLOBAL // TODO is it worth supporting other tymeds here?
};
// get the list of supported formats
size_t nFormats = m_dataObject->GetFormatCount(wxDataObject::Set);
wxDataFormat format, *formats;
formats = nFormats == 1 ? &format : new wxDataFormat[nFormats];
m_dataObject->GetAllFormats(formats, wxDataObject::Set);
// cycle through all supported formats
size_t n;
for ( n = 0; n < nFormats; n++ ) {
s_fmtMemory.cfFormat = formats[n];
// NB: don't use SUCCEEDED macro here: QueryGetData returns S_FALSE
// for file drag and drop (format == CF_HDROP)
if ( pIDataSource->QueryGetData(&s_fmtMemory) == S_OK ) {
format = formats[n];
break;
}
}
if ( formats != &format ) {
// free memory if we allocated it
delete [] formats;
}
return n < nFormats ? format : wxDF_INVALID;
}
// ----------------------------------------------------------------------------
// wxTextDropTarget
// ============================================================================
// ----------------------------------------------------------------------------
bool wxTextDropTarget::OnDrop(long x, long y, const void *pData)
wxTextDropTarget::wxTextDropTarget()
: wxDropTarget(new wxTextDataObject)
{
return OnDropText(x, y, (const wxChar *)pData);
}
size_t wxTextDropTarget::GetFormatCount() const
bool wxTextDropTarget::OnData(wxCoord x, wxCoord y)
{
return 1;
if ( !GetData() )
return FALSE;
return OnDropText(x, y, ((wxTextDataObject *)m_dataObject)->GetText());
}
wxDataFormat wxTextDropTarget::GetFormat(size_t WXUNUSED(n)) const
{
return wxDF_TEXT;
}
// ============================================================================
// ----------------------------------------------------------------------------
// wxFileDropTarget
// ============================================================================
// ----------------------------------------------------------------------------
bool wxFileDropTarget::OnDrop(long x, long y, const void *pData)
wxFileDropTarget::wxFileDropTarget()
: wxDropTarget(new wxFileDataObject)
{
// 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);
// for each file get the length, allocate memory and then get the name
wxChar **aszFiles = new wxChar *[nFiles];
UINT len, n;
for ( n = 0; n < nFiles; n++ ) {
// +1 for terminating NUL
len = ::DragQueryFile(hdrop, n, NULL, 0) + 1;
aszFiles[n] = new wxChar[len];
UINT len2 = ::DragQueryFile(hdrop, n, aszFiles[n], len);
if ( len2 != len - 1 ) {
wxLogDebug(wxT("In wxFileDropTarget::OnDrop DragQueryFile returned %d "
"characters, %d expected."), len2, len - 1);
}
}
bool bResult = OnDropFiles(x, y, nFiles, (const wxChar**) aszFiles);
// free memory
for ( n = 0; n < nFiles; n++ ) {
delete [] aszFiles[n];
}
delete [] aszFiles;
return bResult;
}
size_t wxFileDropTarget::GetFormatCount() const
bool wxFileDropTarget::OnData(wxCoord x, wxCoord y)
{
return 1;
}
if ( !GetData() )
return FALSE;
wxDataFormat wxFileDropTarget::GetFormat(size_t WXUNUSED(n)) const
{
#ifdef __WIN32__
return wxDF_FILENAME;
#else
// TODO: how to implement this in WIN16?
return wxDF_TEXT;
#endif
return OnDropFiles(x, y,
((wxFileDataObject *)m_dataObject)->GetFilenames());
}
#endif

View File

@@ -54,9 +54,8 @@
#include "wx/ownerdrw.h"
#endif
#if wxUSE_DRAG_AND_DROP
#include "wx/dataobj.h"
#include "wx/msw/ole/droptgt.h"
#if wxUSE_DRAG_AND_DROP
#include "wx/dnd.h"
#endif
#include "wx/menuitem.h"
@@ -1215,7 +1214,7 @@ void wxWindow::DoSetSize(int x, int y, int width, int height, int sizeFlags)
wxSize size(-1, -1);
if ( width == -1 )
{
if ( sizeFlags && wxSIZE_AUTO_WIDTH )
if ( sizeFlags & wxSIZE_AUTO_WIDTH )
{
size = DoGetBestSize();
width = size.x;
@@ -1229,7 +1228,7 @@ void wxWindow::DoSetSize(int x, int y, int width, int height, int sizeFlags)
if ( height == -1 )
{
if ( sizeFlags && wxSIZE_AUTO_HEIGHT )
if ( sizeFlags & wxSIZE_AUTO_HEIGHT )
{
if ( size.x == -1 )
{