1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/dobjcmn.cpp
3 // Purpose: implementation of data object methods common to all platforms
4 // Author: Vadim Zeitlin, Robert Roebling
8 // Copyright: (c) wxWidgets Team
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
21 #include "wx/dataobj.h"
27 // ----------------------------------------------------------------------------
29 // ----------------------------------------------------------------------------
31 #include "wx/listimpl.cpp"
33 WX_DEFINE_LIST(wxSimpleDataObjectList
)
35 // ----------------------------------------------------------------------------
37 // ----------------------------------------------------------------------------
39 static wxDataFormat dataFormatInvalid
;
40 WXDLLEXPORT
const wxDataFormat
& wxFormatInvalid
= dataFormatInvalid
;
42 // ============================================================================
44 // ============================================================================
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
50 wxDataObjectBase::~wxDataObjectBase()
54 bool wxDataObjectBase::IsSupported(const wxDataFormat
& format
,
57 size_t nFormatCount
= GetFormatCount( dir
);
58 if ( nFormatCount
== 1 )
60 return format
== GetPreferredFormat( dir
);
64 wxDataFormat
*formats
= new wxDataFormat
[nFormatCount
];
65 GetAllFormats( formats
, dir
);
68 for ( n
= 0; n
< nFormatCount
; n
++ )
70 if ( formats
[n
] == format
)
77 return n
< nFormatCount
;
81 // ----------------------------------------------------------------------------
82 // wxDataObjectComposite
83 // ----------------------------------------------------------------------------
85 wxDataObjectComposite::wxDataObjectComposite()
88 m_receivedFormat
= wxFormatInvalid
;
91 wxDataObjectComposite::~wxDataObjectComposite()
93 WX_CLEAR_LIST( wxSimpleDataObjectList
, m_dataObjects
);
97 wxDataObjectComposite::GetObject(const wxDataFormat
& format
, wxDataObjectBase::Direction dir
) const
99 wxSimpleDataObjectList::compatibility_iterator node
= m_dataObjects
.GetFirst();
103 wxDataObjectSimple
*dataObj
= node
->GetData();
105 if (dataObj
->IsSupported(format
,dir
))
107 node
= node
->GetNext();
112 void wxDataObjectComposite::Add(wxDataObjectSimple
*dataObject
, bool preferred
)
114 // check if the data format of the passed object already exists in the composite data object, if this is the case
115 // do not add the data object and display a message in debug mode (otherwise this method fails silently):
116 // start checking if the data format exists for the 'GET' direction:
119 wxDataFormat
* formats
;
121 noOfFormats
= dataObject
->GetFormatCount(wxDataObjectBase::Get
);
122 formats
= new wxDataFormat
[noOfFormats
];
123 for (indexFormats
=0; indexFormats
<noOfFormats
; ++indexFormats
)
124 wxCHECK_RET(this->GetObject(formats
[indexFormats
],wxDataObjectBase::Get
) == NULL
,
125 _("The data format for the GET-direction of the to be added data object already exists"));
127 // do the same with the 'SET' direction:
128 noOfFormats
= dataObject
->GetFormatCount(wxDataObjectBase::Set
);
130 formats
= new wxDataFormat
[noOfFormats
];
131 for (indexFormats
=0; indexFormats
<noOfFormats
; ++indexFormats
)
132 wxCHECK_RET(this->GetObject(formats
[indexFormats
],wxDataObjectBase::Set
) == NULL
,
133 _("The data format for the SET-direction of the to be added data object already exists"));
136 // if we reach this location the data object can simply be appended:
138 m_preferred
= m_dataObjects
.GetCount();
139 m_dataObjects
.Append( dataObject
);
142 wxDataFormat
wxDataObjectComposite::GetReceivedFormat() const
144 return m_receivedFormat
;
148 wxDataObjectComposite::GetPreferredFormat(Direction
WXUNUSED(dir
)) const
150 wxSimpleDataObjectList::compatibility_iterator node
= m_dataObjects
.Item( m_preferred
);
152 wxCHECK_MSG( node
, wxFormatInvalid
, wxT("no preferred format") );
154 wxDataObjectSimple
* dataObj
= node
->GetData();
156 return dataObj
->GetFormat();
159 #if defined(__WXMSW__)
161 size_t wxDataObjectComposite::GetBufferOffset( const wxDataFormat
& format
)
163 wxDataObjectSimple
*dataObj
= GetObject(format
);
165 wxCHECK_MSG( dataObj
, 0,
166 wxT("unsupported format in wxDataObjectComposite"));
168 return dataObj
->GetBufferOffset( format
);
172 const void* wxDataObjectComposite::GetSizeFromBuffer( const void* buffer
,
174 const wxDataFormat
& format
)
176 wxDataObjectSimple
*dataObj
= GetObject(format
);
178 wxCHECK_MSG( dataObj
, NULL
,
179 wxT("unsupported format in wxDataObjectComposite"));
181 return dataObj
->GetSizeFromBuffer( buffer
, size
, format
);
185 void* wxDataObjectComposite::SetSizeInBuffer( void* buffer
, size_t size
,
186 const wxDataFormat
& format
)
188 wxDataObjectSimple
*dataObj
= GetObject( format
);
190 wxCHECK_MSG( dataObj
, NULL
,
191 wxT("unsupported format in wxDataObjectComposite"));
193 return dataObj
->SetSizeInBuffer( buffer
, size
, format
);
198 size_t wxDataObjectComposite::GetFormatCount(Direction
WXUNUSED(dir
)) const
200 // TODO what about the Get/Set only formats?
201 return m_dataObjects
.GetCount();
204 void wxDataObjectComposite::GetAllFormats(wxDataFormat
*formats
,
208 wxSimpleDataObjectList::compatibility_iterator node
;
210 for ( node
= m_dataObjects
.GetFirst(); node
; node
= node
->GetNext() )
212 node
->GetData()->GetAllFormats(formats
+index
,dir
);
213 index
+= node
->GetData()->GetFormatCount(dir
);
217 size_t wxDataObjectComposite::GetDataSize(const wxDataFormat
& format
) const
219 wxDataObjectSimple
*dataObj
= GetObject(format
);
221 wxCHECK_MSG( dataObj
, 0,
222 wxT("unsupported format in wxDataObjectComposite"));
224 return dataObj
->GetDataSize();
227 bool wxDataObjectComposite::GetDataHere(const wxDataFormat
& format
,
230 wxDataObjectSimple
*dataObj
= GetObject( format
);
232 wxCHECK_MSG( dataObj
, false,
233 wxT("unsupported format in wxDataObjectComposite"));
235 return dataObj
->GetDataHere( buf
);
238 bool wxDataObjectComposite::SetData(const wxDataFormat
& format
,
242 wxDataObjectSimple
*dataObj
= GetObject( format
);
244 wxCHECK_MSG( dataObj
, false,
245 wxT("unsupported format in wxDataObjectComposite"));
247 m_receivedFormat
= format
;
248 return dataObj
->SetData( len
, buf
);
251 // ----------------------------------------------------------------------------
253 // ----------------------------------------------------------------------------
255 #ifdef wxNEEDS_UTF8_FOR_TEXT_DATAOBJ
257 // FIXME-UTF8: we should be able to merge wchar_t and UTF-8 versions once we
258 // have a way to get UTF-8 string (and its length) in both builds
259 // without loss of efficiency (i.e. extra buffer copy/strlen call)
261 #if wxUSE_UNICODE_WCHAR
263 static inline wxMBConv
& GetConv(const wxDataFormat
& format
)
265 // use UTF8 for wxDF_UNICODETEXT and UCS4 for wxDF_TEXT
266 return format
== wxDF_UNICODETEXT
? wxConvUTF8
: wxConvLibc
;
269 size_t wxTextDataObject::GetDataSize(const wxDataFormat
& format
) const
271 wxCharBuffer buffer
= GetConv(format
).cWX2MB( GetText().c_str() );
273 return buffer
? strlen( buffer
) : 0;
276 bool wxTextDataObject::GetDataHere(const wxDataFormat
& format
, void *buf
) const
281 wxCharBuffer buffer
= GetConv(format
).cWX2MB( GetText().c_str() );
285 memcpy( (char*) buf
, buffer
, GetDataSize(format
) );
286 // strcpy( (char*) buf, buffer );
291 bool wxTextDataObject::SetData(const wxDataFormat
& format
,
292 size_t WXUNUSED(len
), const void *buf
)
297 wxWCharBuffer buffer
= GetConv(format
).cMB2WX( (const char*)buf
);
304 #else // wxUSE_UNICODE_UTF8
306 size_t wxTextDataObject::GetDataSize(const wxDataFormat
& format
) const
308 if ( format
== wxDF_UNICODETEXT
|| wxLocaleIsUtf8
)
310 return m_text
.utf8_length();
314 const wxCharBuffer
buf(wxConvLocal
.cWC2MB(m_text
.wc_str()));
315 return buf
? strlen(buf
) : 0;
319 bool wxTextDataObject::GetDataHere(const wxDataFormat
& format
, void *buf
) const
324 if ( format
== wxDF_UNICODETEXT
|| wxLocaleIsUtf8
)
326 memcpy(buf
, m_text
.utf8_str(), m_text
.utf8_length());
330 const wxCharBuffer
bufLocal(wxConvLocal
.cWC2MB(m_text
.wc_str()));
334 memcpy(buf
, bufLocal
, strlen(bufLocal
));
340 bool wxTextDataObject::SetData(const wxDataFormat
& format
,
341 size_t len
, const void *buf_
)
343 const char * const buf
= static_cast<const char *>(buf_
);
348 if ( format
== wxDF_UNICODETEXT
|| wxLocaleIsUtf8
)
350 // normally the data is in UTF-8 so we could use FromUTF8Unchecked()
351 // but it's not absolutely clear what GTK+ does if the clipboard data
352 // is not in UTF-8 so do an extra check for tranquility, it shouldn't
353 // matter much if we lose a bit of performance when pasting from
355 m_text
= wxString::FromUTF8(buf
, len
);
357 else // wxDF_TEXT, convert from current (non-UTF8) locale
359 m_text
= wxConvLocal
.cMB2WC(buf
, len
, NULL
);
365 #endif // wxUSE_UNICODE_WCHAR/wxUSE_UNICODE_UTF8
367 #elif defined(wxNEEDS_UTF16_FOR_TEXT_DATAOBJ)
369 static wxMBConvUTF16 sUTF16Converter
;
371 static inline wxMBConv
& GetConv(const wxDataFormat
& format
)
374 format
== wxDF_UNICODETEXT
375 ? (wxMBConv
&) sUTF16Converter
376 : (wxMBConv
&) wxConvLocal
;
379 size_t wxTextDataObject::GetDataSize(const wxDataFormat
& format
) const
381 size_t len
= GetConv(format
).WC2MB( NULL
, GetText().c_str(), 0 );
382 len
+= (format
== wxDF_UNICODETEXT
? 2 : 1);
387 bool wxTextDataObject::GetDataHere(const wxDataFormat
& format
, void *buf
) const
392 wxCharBuffer buffer
= GetConv(format
).cWX2MB( GetText().c_str() );
394 size_t len
= GetConv(format
).WC2MB( NULL
, GetText().c_str(), 0 );
395 len
+= (format
== wxDF_UNICODETEXT
? 2 : 1);
397 // trailing (uni)char 0
398 memcpy( (char*)buf
, (const char*)buffer
, len
);
403 bool wxTextDataObject::SetData(const wxDataFormat
& format
,
404 size_t WXUNUSED(len
), const void *buf
)
409 wxWCharBuffer buffer
= GetConv(format
).cMB2WX( (const char*)buf
);
416 #else // !wxNEEDS_UTF{8,16}_FOR_TEXT_DATAOBJ
418 size_t wxTextDataObject::GetDataSize() const
420 return GetTextLength() * sizeof(wxChar
);
423 bool wxTextDataObject::GetDataHere(void *buf
) const
425 wxStrcpy( (wxChar
*)buf
, GetText().c_str() );
430 bool wxTextDataObject::SetData(size_t WXUNUSED(len
), const void *buf
)
432 SetText( wxString((const wxChar
*)buf
) );
437 #endif // different wxTextDataObject implementations
439 // ----------------------------------------------------------------------------
440 // wxCustomDataObject
441 // ----------------------------------------------------------------------------
443 wxCustomDataObject::wxCustomDataObject(const wxDataFormat
& format
)
444 : wxDataObjectSimple(format
)
450 wxCustomDataObject::~wxCustomDataObject()
455 void wxCustomDataObject::TakeData(size_t size
, void *data
)
463 void *wxCustomDataObject::Alloc(size_t size
)
465 return (void *)new char[size
];
468 void wxCustomDataObject::Free()
470 delete [] (char*)m_data
;
475 size_t wxCustomDataObject::GetDataSize() const
480 bool wxCustomDataObject::GetDataHere(void *buf
) const
485 void *data
= GetData();
489 memcpy( buf
, data
, GetSize() );
494 bool wxCustomDataObject::SetData(size_t size
, const void *buf
)
498 m_data
= Alloc(size
);
499 if ( m_data
== NULL
)
503 memcpy( m_data
, buf
, m_size
);
508 // ============================================================================
509 // some common dnd related code
510 // ============================================================================
512 #if wxUSE_DRAG_AND_DROP
516 // ----------------------------------------------------------------------------
518 // ----------------------------------------------------------------------------
520 // NB: we can't use "new" in ctor initializer lists because this provokes an
521 // internal compiler error with VC++ 5.0 (hey, even gcc compiles this!),
522 // so use SetDataObject() instead
524 wxTextDropTarget::wxTextDropTarget()
526 SetDataObject(new wxTextDataObject
);
529 wxDragResult
wxTextDropTarget::OnData(wxCoord x
, wxCoord y
, wxDragResult def
)
534 wxTextDataObject
*dobj
= (wxTextDataObject
*)m_dataObject
;
535 return OnDropText( x
, y
, dobj
->GetText() ) ? def
: wxDragNone
;
538 // ----------------------------------------------------------------------------
540 // ----------------------------------------------------------------------------
542 wxFileDropTarget::wxFileDropTarget()
544 SetDataObject(new wxFileDataObject
);
547 wxDragResult
wxFileDropTarget::OnData(wxCoord x
, wxCoord y
, wxDragResult def
)
552 wxFileDataObject
*dobj
= (wxFileDataObject
*)m_dataObject
;
553 return OnDropFiles( x
, y
, dobj
->GetFilenames() ) ? def
: wxDragNone
;
556 #endif // wxUSE_DRAG_AND_DROP
558 #endif // wxUSE_DATAOBJ