+#ifdef wxNEEDS_UTF8_FOR_TEXT_DATAOBJ
+
+// FIXME-UTF8: we should be able to merge wchar_t and UTF-8 versions once we
+// have a way to get UTF-8 string (and its length) in both builds
+// without loss of efficiency (i.e. extra buffer copy/strlen call)
+
+#if wxUSE_UNICODE_WCHAR
+
+static inline wxMBConv& GetConv(const wxDataFormat& format)
+{
+ // use UTF8 for wxDF_UNICODETEXT and UCS4 for wxDF_TEXT
+ return format == wxDF_UNICODETEXT ? wxConvUTF8 : wxConvLibc;
+}
+
+size_t wxTextDataObject::GetDataSize(const wxDataFormat& format) const
+{
+ wxCharBuffer buffer = GetConv(format).cWX2MB( GetText().c_str() );
+
+ return buffer ? strlen( buffer ) : 0;
+}
+
+bool wxTextDataObject::GetDataHere(const wxDataFormat& format, void *buf) const
+{
+ if ( !buf )
+ return false;
+
+ wxCharBuffer buffer = GetConv(format).cWX2MB( GetText().c_str() );
+ if ( !buffer )
+ return false;
+
+ memcpy( (char*) buf, buffer, GetDataSize(format) );
+ // strcpy( (char*) buf, buffer );
+
+ return true;
+}
+
+bool wxTextDataObject::SetData(const wxDataFormat& format,
+ size_t WXUNUSED(len), const void *buf)
+{
+ if ( buf == NULL )
+ return false;
+
+ wxWCharBuffer buffer = GetConv(format).cMB2WX( (const char*)buf );
+
+ SetText( buffer );
+
+ return true;
+}
+
+#else // wxUSE_UNICODE_UTF8
+
+size_t wxTextDataObject::GetDataSize(const wxDataFormat& format) const
+{
+ const wxString& text = GetText();
+ if ( format == wxDF_UNICODETEXT || wxLocaleIsUtf8 )
+ {
+ return text.utf8_length();
+ }
+ else // wxDF_TEXT
+ {
+ const wxCharBuffer buf(wxConvLocal.cWC2MB(text.wc_str()));
+ return buf ? strlen(buf) : 0;
+ }
+}
+
+bool wxTextDataObject::GetDataHere(const wxDataFormat& format, void *buf) const
+{
+ if ( !buf )
+ return false;
+
+ const wxString& text = GetText();
+ if ( format == wxDF_UNICODETEXT || wxLocaleIsUtf8 )
+ {
+ memcpy(buf, text.utf8_str(), text.utf8_length());
+ }
+ else // wxDF_TEXT
+ {
+ const wxCharBuffer bufLocal(wxConvLocal.cWC2MB(text.wc_str()));
+ if ( !bufLocal )
+ return false;
+
+ memcpy(buf, bufLocal, strlen(bufLocal));
+ }
+
+ return true;
+}
+
+bool wxTextDataObject::SetData(const wxDataFormat& format,
+ size_t len, const void *buf_)
+{
+ const char * const buf = static_cast<const char *>(buf_);
+
+ if ( buf == NULL )
+ return false;
+
+ if ( format == wxDF_UNICODETEXT || wxLocaleIsUtf8 )
+ {
+ // normally the data is in UTF-8 so we could use FromUTF8Unchecked()
+ // but it's not absolutely clear what GTK+ does if the clipboard data
+ // is not in UTF-8 so do an extra check for tranquility, it shouldn't
+ // matter much if we lose a bit of performance when pasting from
+ // clipboard
+ SetText(wxString::FromUTF8(buf, len));
+ }
+ else // wxDF_TEXT, convert from current (non-UTF8) locale
+ {
+ SetText(wxConvLocal.cMB2WC(buf, len, NULL));
+ }
+
+ return true;
+}
+
+#endif // wxUSE_UNICODE_WCHAR/wxUSE_UNICODE_UTF8
+
+#elif defined(wxNEEDS_UTF16_FOR_TEXT_DATAOBJ)
+
+namespace
+{
+
+inline wxMBConv& GetConv(const wxDataFormat& format)
+{
+ static wxMBConvUTF16 s_UTF16Converter;
+
+ return format == wxDF_UNICODETEXT ? static_cast<wxMBConv&>(s_UTF16Converter)
+ : static_cast<wxMBConv&>(wxConvLocal);
+}
+
+} // anonymous namespace
+
+size_t wxTextDataObject::GetDataSize(const wxDataFormat& format) const
+{
+ return GetConv(format).WC2MB(NULL, GetText().wc_str(), 0);
+}
+
+bool wxTextDataObject::GetDataHere(const wxDataFormat& format, void *buf) const
+{
+ if ( buf == NULL )
+ return false;
+
+ wxCharBuffer buffer(GetConv(format).cWX2MB(GetText().c_str()));
+
+ memcpy(buf, buffer.data(), buffer.length());
+
+ return true;
+}
+
+bool wxTextDataObject::SetData(const wxDataFormat& format,
+ size_t WXUNUSED(len),
+ const void *buf)
+{
+ if ( buf == NULL )
+ return false;
+
+ SetText(GetConv(format).cMB2WX(static_cast<const char*>(buf)));
+
+ return true;
+}
+
+#else // !wxNEEDS_UTF{8,16}_FOR_TEXT_DATAOBJ
+
+// NB: This branch, using native wxChar for the clipboard, is only used under
+// Windows currently. It's just a coincidence, but Windows is also the only
+// platform where we need to convert the text to the native EOL format, so
+// wxTextBuffer::Translate() is only used here and not in the code above.
+