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
) const
99 wxSimpleDataObjectList::compatibility_iterator node
= m_dataObjects
.GetFirst();
102 wxDataObjectSimple
*dataObj
= node
->GetData();
104 if ( dataObj
->GetFormat() == format
)
109 node
= node
->GetNext();
112 return (wxDataObjectSimple
*)NULL
;
115 void wxDataObjectComposite::Add(wxDataObjectSimple
*dataObject
, bool preferred
)
118 m_preferred
= m_dataObjects
.GetCount();
120 m_dataObjects
.Append( dataObject
);
123 wxDataFormat
wxDataObjectComposite::GetReceivedFormat() const
125 return m_receivedFormat
;
129 wxDataObjectComposite::GetPreferredFormat(Direction
WXUNUSED(dir
)) const
131 wxSimpleDataObjectList::compatibility_iterator node
= m_dataObjects
.Item( m_preferred
);
133 wxCHECK_MSG( node
, wxFormatInvalid
, wxT("no preferred format") );
135 wxDataObjectSimple
* dataObj
= node
->GetData();
137 return dataObj
->GetFormat();
140 #if defined(__WXMSW__)
142 size_t wxDataObjectComposite::GetBufferOffset( const wxDataFormat
& format
)
144 wxDataObjectSimple
*dataObj
= GetObject(format
);
146 wxCHECK_MSG( dataObj
, 0,
147 wxT("unsupported format in wxDataObjectComposite"));
149 return dataObj
->GetBufferOffset( format
);
153 const void* wxDataObjectComposite::GetSizeFromBuffer( const void* buffer
,
155 const wxDataFormat
& format
)
157 wxDataObjectSimple
*dataObj
= GetObject(format
);
159 wxCHECK_MSG( dataObj
, NULL
,
160 wxT("unsupported format in wxDataObjectComposite"));
162 return dataObj
->GetSizeFromBuffer( buffer
, size
, format
);
166 void* wxDataObjectComposite::SetSizeInBuffer( void* buffer
, size_t size
,
167 const wxDataFormat
& format
)
169 wxDataObjectSimple
*dataObj
= GetObject( format
);
171 wxCHECK_MSG( dataObj
, NULL
,
172 wxT("unsupported format in wxDataObjectComposite"));
174 return dataObj
->SetSizeInBuffer( buffer
, size
, format
);
179 size_t wxDataObjectComposite::GetFormatCount(Direction
WXUNUSED(dir
)) const
181 // TODO what about the Get/Set only formats?
182 return m_dataObjects
.GetCount();
185 void wxDataObjectComposite::GetAllFormats(wxDataFormat
*formats
,
186 Direction
WXUNUSED(dir
)) const
189 wxSimpleDataObjectList::compatibility_iterator node
;
190 for ( node
= m_dataObjects
.GetFirst(); node
; node
= node
->GetNext() )
192 // TODO if ( !outputOnlyToo ) && this one counts ...
193 formats
[n
++] = node
->GetData()->GetFormat();
197 size_t wxDataObjectComposite::GetDataSize(const wxDataFormat
& format
) const
199 wxDataObjectSimple
*dataObj
= GetObject(format
);
201 wxCHECK_MSG( dataObj
, 0,
202 wxT("unsupported format in wxDataObjectComposite"));
204 return dataObj
->GetDataSize();
207 bool wxDataObjectComposite::GetDataHere(const wxDataFormat
& format
,
210 wxDataObjectSimple
*dataObj
= GetObject( format
);
212 wxCHECK_MSG( dataObj
, false,
213 wxT("unsupported format in wxDataObjectComposite"));
215 return dataObj
->GetDataHere( buf
);
218 bool wxDataObjectComposite::SetData(const wxDataFormat
& format
,
222 wxDataObjectSimple
*dataObj
= GetObject( format
);
224 wxCHECK_MSG( dataObj
, false,
225 wxT("unsupported format in wxDataObjectComposite"));
227 m_receivedFormat
= format
;
228 return dataObj
->SetData( len
, buf
);
231 // ----------------------------------------------------------------------------
233 // ----------------------------------------------------------------------------
235 #ifdef wxNEEDS_UTF8_FOR_TEXT_DATAOBJ
237 // FIXME-UTF8: we should be able to merge wchar_t and UTF-8 versions once we
238 // have a way to get UTF-8 string (and its length) in both builds
239 // without loss of efficiency (i.e. extra buffer copy/strlen call)
241 #if wxUSE_UNICODE_WCHAR
243 static inline wxMBConv
& GetConv(const wxDataFormat
& format
)
245 // use UTF8 for wxDF_UNICODETEXT and UCS4 for wxDF_TEXT
246 return format
== wxDF_UNICODETEXT
? wxConvUTF8
: wxConvLibc
;
249 size_t wxTextDataObject::GetDataSize(const wxDataFormat
& format
) const
251 wxCharBuffer buffer
= GetConv(format
).cWX2MB( GetText().c_str() );
253 return buffer
? strlen( buffer
) : 0;
256 bool wxTextDataObject::GetDataHere(const wxDataFormat
& format
, void *buf
) const
261 wxCharBuffer buffer
= GetConv(format
).cWX2MB( GetText().c_str() );
265 memcpy( (char*) buf
, buffer
, GetDataSize(format
) );
266 // strcpy( (char*) buf, buffer );
271 bool wxTextDataObject::SetData(const wxDataFormat
& format
,
272 size_t WXUNUSED(len
), const void *buf
)
277 wxWCharBuffer buffer
= GetConv(format
).cMB2WX( (const char*)buf
);
284 #else // wxUSE_UNICODE_UTF8
286 size_t wxTextDataObject::GetDataSize(const wxDataFormat
& format
) const
288 if ( format
== wxDF_UNICODETEXT
|| wxLocaleIsUtf8
)
290 return m_text
.utf8_length();
294 const wxCharBuffer
buf(wxConvLocal
.cWC2MB(m_text
.wc_str()));
295 return buf
? strlen(buf
) : 0;
299 bool wxTextDataObject::GetDataHere(const wxDataFormat
& format
, void *buf
) const
304 if ( format
== wxDF_UNICODETEXT
|| wxLocaleIsUtf8
)
306 memcpy(buf
, m_text
.utf8_str(), m_text
.utf8_length());
310 const wxCharBuffer
bufLocal(wxConvLocal
.cWC2MB(m_text
.wc_str()));
314 memcpy(buf
, bufLocal
, strlen(bufLocal
));
320 bool wxTextDataObject::SetData(const wxDataFormat
& format
,
321 size_t len
, const void *buf_
)
323 const char * const buf
= static_cast<const char *>(buf_
);
328 if ( format
== wxDF_UNICODETEXT
|| wxLocaleIsUtf8
)
330 // normally the data is in UTF-8 so we could use FromUTF8Unchecked()
331 // but it's not absolutely clear what GTK+ does if the clipboard data
332 // is not in UTF-8 so do an extra check for tranquility, it shouldn't
333 // matter much if we lose a bit of performance when pasting from
335 m_text
= wxString::FromUTF8(buf
, len
);
337 else // wxDF_TEXT, convert from current (non-UTF8) locale
339 m_text
= wxConvLocal
.cMB2WC(buf
, len
, NULL
);
345 #endif // wxUSE_UNICODE_WCHAR/wxUSE_UNICODE_UTF8
347 #elif defined(wxNEEDS_UTF16_FOR_TEXT_DATAOBJ)
349 static wxMBConvUTF16 sUTF16Converter
;
351 static inline wxMBConv
& GetConv(const wxDataFormat
& format
)
354 format
== wxDF_UNICODETEXT
355 ? (wxMBConv
&) sUTF16Converter
356 : (wxMBConv
&) wxConvLocal
;
359 size_t wxTextDataObject::GetDataSize(const wxDataFormat
& format
) const
361 size_t len
= GetConv(format
).WC2MB( NULL
, GetText().c_str(), 0 );
362 len
+= (format
== wxDF_UNICODETEXT
? 2 : 1);
367 bool wxTextDataObject::GetDataHere(const wxDataFormat
& format
, void *buf
) const
372 wxCharBuffer buffer
= GetConv(format
).cWX2MB( GetText().c_str() );
374 size_t len
= GetConv(format
).WC2MB( NULL
, GetText().c_str(), 0 );
375 len
+= (format
== wxDF_UNICODETEXT
? 2 : 1);
377 // trailing (uni)char 0
378 memcpy( (char*)buf
, (const char*)buffer
, len
);
383 bool wxTextDataObject::SetData(const wxDataFormat
& format
,
384 size_t WXUNUSED(len
), const void *buf
)
389 wxWCharBuffer buffer
= GetConv(format
).cMB2WX( (const char*)buf
);
396 #else // !wxNEEDS_UTF{8,16}_FOR_TEXT_DATAOBJ
398 size_t wxTextDataObject::GetDataSize() const
400 return GetTextLength() * sizeof(wxChar
);
403 bool wxTextDataObject::GetDataHere(void *buf
) const
405 wxStrcpy( (wxChar
*)buf
, GetText().c_str() );
410 bool wxTextDataObject::SetData(size_t WXUNUSED(len
), const void *buf
)
412 SetText( wxString((const wxChar
*)buf
) );
417 #endif // different wxTextDataObject implementations
419 // ----------------------------------------------------------------------------
420 // wxCustomDataObject
421 // ----------------------------------------------------------------------------
423 wxCustomDataObject::wxCustomDataObject(const wxDataFormat
& format
)
424 : wxDataObjectSimple(format
)
430 wxCustomDataObject::~wxCustomDataObject()
435 void wxCustomDataObject::TakeData(size_t size
, void *data
)
443 void *wxCustomDataObject::Alloc(size_t size
)
445 return (void *)new char[size
];
448 void wxCustomDataObject::Free()
450 delete [] (char*)m_data
;
452 m_data
= (void*)NULL
;
455 size_t wxCustomDataObject::GetDataSize() const
460 bool wxCustomDataObject::GetDataHere(void *buf
) const
465 void *data
= GetData();
469 memcpy( buf
, data
, GetSize() );
474 bool wxCustomDataObject::SetData(size_t size
, const void *buf
)
478 m_data
= Alloc(size
);
479 if ( m_data
== NULL
)
483 memcpy( m_data
, buf
, m_size
);
488 // ============================================================================
489 // some common dnd related code
490 // ============================================================================
492 #if wxUSE_DRAG_AND_DROP
496 // ----------------------------------------------------------------------------
498 // ----------------------------------------------------------------------------
500 // NB: we can't use "new" in ctor initializer lists because this provokes an
501 // internal compiler error with VC++ 5.0 (hey, even gcc compiles this!),
502 // so use SetDataObject() instead
504 wxTextDropTarget::wxTextDropTarget()
506 SetDataObject(new wxTextDataObject
);
509 wxDragResult
wxTextDropTarget::OnData(wxCoord x
, wxCoord y
, wxDragResult def
)
514 wxTextDataObject
*dobj
= (wxTextDataObject
*)m_dataObject
;
515 return OnDropText( x
, y
, dobj
->GetText() ) ? def
: wxDragNone
;
518 // ----------------------------------------------------------------------------
520 // ----------------------------------------------------------------------------
522 wxFileDropTarget::wxFileDropTarget()
524 SetDataObject(new wxFileDataObject
);
527 wxDragResult
wxFileDropTarget::OnData(wxCoord x
, wxCoord y
, wxDragResult def
)
532 wxFileDataObject
*dobj
= (wxFileDataObject
*)m_dataObject
;
533 return OnDropFiles( x
, y
, dobj
->GetFilenames() ) ? def
: wxDragNone
;
536 #endif // wxUSE_DRAG_AND_DROP
538 #endif // wxUSE_DATAOBJ