X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/0c2b453f0a694284e2582a76923753abf0f316d5..04cc1e93edd19f0430f9681ef1fdaf81ccdabf8f:/src/common/dobjcmn.cpp diff --git a/src/common/dobjcmn.cpp b/src/common/dobjcmn.cpp index 7ead643ae9..b16c8e219f 100644 --- a/src/common/dobjcmn.cpp +++ b/src/common/dobjcmn.cpp @@ -5,8 +5,8 @@ // Modified by: // Created: 19.10.99 // RCS-ID: $Id$ -// Copyright: (c) wxWindows Team -// Licence: wxWindows license +// Copyright: (c) wxWidgets Team +// Licence: wxWindows licence /////////////////////////////////////////////////////////////////////////////// // ============================================================================ @@ -17,7 +17,7 @@ // headers // ---------------------------------------------------------------------------- -#ifdef __GNUG__ +#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) #pragma implementation "dataobjbase.h" #endif @@ -27,6 +27,8 @@ #pragma hdrstop #endif +#if wxUSE_DATAOBJ + #ifndef WX_PRECOMP #include "wx/app.h" #include "wx/debug.h" @@ -47,7 +49,7 @@ WX_DEFINE_LIST(wxSimpleDataObjectList); // ---------------------------------------------------------------------------- static wxDataFormat dataFormatInvalid; -const wxDataFormat& wxFormatInvalid = dataFormatInvalid; +WXDLLEXPORT const wxDataFormat& wxFormatInvalid = dataFormatInvalid; // ============================================================================ // implementation @@ -61,14 +63,51 @@ wxDataObjectBase::~wxDataObjectBase() { } +bool wxDataObjectBase::IsSupported(const wxDataFormat& format, + Direction dir) const +{ + size_t nFormatCount = GetFormatCount(dir); + if ( nFormatCount == 1 ) + { + return format == GetPreferredFormat(dir); + } + else + { + wxDataFormat *formats = new wxDataFormat[nFormatCount]; + GetAllFormats(formats, dir); + + size_t n; + for ( n = 0; n < nFormatCount; n++ ) + { + if ( formats[n] == format ) + break; + } + + delete [] formats; + + // found? + return n < nFormatCount; + } +} + // ---------------------------------------------------------------------------- // wxDataObjectComposite // ---------------------------------------------------------------------------- +wxDataObjectComposite::wxDataObjectComposite() +{ + m_preferred = 0; +} + +wxDataObjectComposite::~wxDataObjectComposite() +{ + WX_CLEAR_LIST(wxSimpleDataObjectList, m_dataObjects); +} + wxDataObjectSimple * wxDataObjectComposite::GetObject(const wxDataFormat& format) const { - wxSimpleDataObjectList::Node *node = m_dataObjects.GetFirst(); + wxSimpleDataObjectList::compatibility_iterator node = m_dataObjects.GetFirst(); while ( node ) { wxDataObjectSimple *dataObj = node->GetData(); @@ -95,15 +134,54 @@ void wxDataObjectComposite::Add(wxDataObjectSimple *dataObject, bool preferred) wxDataFormat wxDataObjectComposite::GetPreferredFormat(Direction WXUNUSED(dir)) const { - wxSimpleDataObjectList::Node *node = m_dataObjects.Item( m_preferred ); + wxSimpleDataObjectList::compatibility_iterator node = m_dataObjects.Item( m_preferred ); - wxCHECK_MSG( node, wxDF_INVALID, wxT("no preferred format") ); + wxCHECK_MSG( node, wxFormatInvalid, wxT("no preferred format") ); wxDataObjectSimple* dataObj = node->GetData(); return dataObj->GetFormat(); } +#if defined(__WXMSW__) + +size_t wxDataObjectComposite::GetBufferOffset( const wxDataFormat& format ) +{ + wxDataObjectSimple *dataObj = GetObject(format); + + wxCHECK_MSG( dataObj, 0, + wxT("unsupported format in wxDataObjectComposite")); + + return dataObj->GetBufferOffset( format ); +} + + +const void* wxDataObjectComposite::GetSizeFromBuffer( const void* buffer, + size_t* size, + const wxDataFormat& format ) +{ + wxDataObjectSimple *dataObj = GetObject(format); + + wxCHECK_MSG( dataObj, NULL, + wxT("unsupported format in wxDataObjectComposite")); + + return dataObj->GetSizeFromBuffer( buffer, size, format ); +} + + +void* wxDataObjectComposite::SetSizeInBuffer( void* buffer, size_t size, + const wxDataFormat& format ) +{ + wxDataObjectSimple *dataObj = GetObject(format); + + wxCHECK_MSG( dataObj, NULL, + wxT("unsupported format in wxDataObjectComposite")); + + return dataObj->SetSizeInBuffer( buffer, size, format ); +} + +#endif + size_t wxDataObjectComposite::GetFormatCount(Direction WXUNUSED(dir)) const { // TODO what about the Get/Set only formats? @@ -114,7 +192,7 @@ void wxDataObjectComposite::GetAllFormats(wxDataFormat *formats, Direction WXUNUSED(dir)) const { size_t n = 0; - wxSimpleDataObjectList::Node *node; + wxSimpleDataObjectList::compatibility_iterator node; for ( node = m_dataObjects.GetFirst(); node; node = node->GetNext() ) { // TODO if ( !outputOnlyToo ) && this one counts ... @@ -159,25 +237,125 @@ bool wxDataObjectComposite::SetData(const wxDataFormat& format, // wxTextDataObject // ---------------------------------------------------------------------------- +#if defined(__WXGTK20__) && wxUSE_UNICODE + +size_t wxTextDataObject::GetDataSize(const wxDataFormat& format) const +{ + if (format == wxDF_UNICODETEXT) + { + // Use UTF8 not UCS4 + wxCharBuffer buffer = wxConvUTF8.cWX2MB( GetText().c_str() ); + return strlen( (const char*) buffer ) + 1; + } + else // == wxDF_TEXT + { + wxCharBuffer buffer = wxConvLibc.cWX2MB( GetText().c_str() ); + return strlen( (const char*) buffer ) + 1; + } +} + +bool wxTextDataObject::GetDataHere(const wxDataFormat& format, void *buf) const +{ + if (format == wxDF_UNICODETEXT) + { + // Use UTF8 not UCS4 + wxCharBuffer buffer = wxConvUTF8.cWX2MB( GetText().c_str() ); + strcpy( (char*) buf, (const char*) buffer ); + } + else + { + wxCharBuffer buffer = wxConvLibc.cWX2MB( GetText().c_str() ); + strcpy( (char*) buf, (const char*) buffer ); + } + + return TRUE; +} + +bool wxTextDataObject::SetData(const wxDataFormat& format, + size_t WXUNUSED(len), const void *buf) +{ + if (format == wxDF_UNICODETEXT) + SetText( wxConvUTF8.cMB2WX( (const char*) buf ) ); + else + SetText( wxConvLibc.cMB2WX( (const char*) buf ) ); + + return TRUE; +} + +#elif wxUSE_UNICODE && defined(__WXMAC__) + +size_t wxTextDataObject::GetDataSize(const wxDataFormat& format) const +{ + if (format == wxDF_UNICODETEXT) + { + // host native is UTF16 + wxMBConvUTF16BE converter ; + wxCharBuffer buffer = converter.cWX2MB( GetText().c_str() ); + return strlen( (const char*) buffer ) + 1; + } + else // == wxDF_TEXT + { + wxCharBuffer buffer = wxConvLibc.cWX2MB( GetText().c_str() ); + return strlen( (const char*) buffer ) + 1; + } +} + +bool wxTextDataObject::GetDataHere(const wxDataFormat& format, void *buf) const +{ + if (format == wxDF_UNICODETEXT) + { + // host native is UTF16 + wxMBConvUTF16BE converter ; + wxCharBuffer buffer = converter.cWX2MB( GetText().c_str() ); + strcpy( (char*) buf, (const char*) buffer ); + } + else + { + wxCharBuffer buffer = wxConvLibc.cWX2MB( GetText().c_str() ); + strcpy( (char*) buf, (const char*) buffer ); + } + + return TRUE; +} + +bool wxTextDataObject::SetData(const wxDataFormat& format, + size_t WXUNUSED(len), const void *buf) +{ + if (format == wxDF_UNICODETEXT) + { + // host native is UTF16 + wxMBConvUTF16BE converter ; + SetText( converter.cMB2WX( (const char*) buf ) ); + } + else + SetText( wxConvLibc.cMB2WX( (const char*) buf ) ); + + return TRUE; +} + +#else + size_t wxTextDataObject::GetDataSize() const { - return GetTextLength(); + return GetTextLength() * sizeof(wxChar); } bool wxTextDataObject::GetDataHere(void *buf) const { - strcpy((char *)buf, GetText().mb_str()); + wxStrcpy((wxChar *)buf, GetText().c_str()); return TRUE; } bool wxTextDataObject::SetData(size_t WXUNUSED(len), const void *buf) { - SetText(wxString((const char *)buf)); + SetText(wxString((const wxChar *)buf)); return TRUE; } +#endif + // ---------------------------------------------------------------------------- // wxFileDataObjectBase // ---------------------------------------------------------------------------- @@ -229,6 +407,12 @@ void wxFileDataObjectBase::SetFilenames(const wxChar* filenames) // wxCustomDataObject // ---------------------------------------------------------------------------- +wxCustomDataObject::wxCustomDataObject(const wxDataFormat& format) + : wxDataObjectSimple(format) +{ + m_data = (void *)NULL; +} + wxCustomDataObject::~wxCustomDataObject() { Free(); @@ -249,7 +433,7 @@ void *wxCustomDataObject::Alloc(size_t size) void wxCustomDataObject::Free() { - delete [] m_data; + delete [] (char *)m_data; m_size = 0; m_data = (void *)NULL; } @@ -283,3 +467,54 @@ bool wxCustomDataObject::SetData(size_t size, const void *buf) return TRUE; } +// ============================================================================ +// some common dnd related code +// ============================================================================ + +#if wxUSE_DRAG_AND_DROP + +#include "wx/dnd.h" + +// ---------------------------------------------------------------------------- +// wxTextDropTarget +// ---------------------------------------------------------------------------- + +// NB: we can't use "new" in ctor initializer lists because this provokes an +// internal compiler error with VC++ 5.0 (hey, even gcc compiles this!), +// so use SetDataObject() instead + +wxTextDropTarget::wxTextDropTarget() +{ + SetDataObject(new wxTextDataObject); +} + +wxDragResult wxTextDropTarget::OnData(wxCoord x, wxCoord y, wxDragResult def) +{ + if ( !GetData() ) + return wxDragNone; + + wxTextDataObject *dobj = (wxTextDataObject *)m_dataObject; + return OnDropText(x, y, dobj->GetText()) ? def : wxDragNone; +} + +// ---------------------------------------------------------------------------- +// wxFileDropTarget +// ---------------------------------------------------------------------------- + +wxFileDropTarget::wxFileDropTarget() +{ + SetDataObject(new wxFileDataObject); +} + +wxDragResult wxFileDropTarget::OnData(wxCoord x, wxCoord y, wxDragResult def) +{ + if ( !GetData() ) + return wxDragNone; + + wxFileDataObject *dobj = (wxFileDataObject *)m_dataObject; + return OnDropFiles(x, y, dobj->GetFilenames()) ? def : wxDragNone; +} + +#endif // wxUSE_DRAG_AND_DROP + +#endif // wxUSE_DATAOBJ