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
)
115 m_preferred
= m_dataObjects
.GetCount();
117 m_dataObjects
.Append( dataObject
);
120 wxDataFormat
wxDataObjectComposite::GetReceivedFormat() const
122 return m_receivedFormat
;
126 wxDataObjectComposite::GetPreferredFormat(Direction
WXUNUSED(dir
)) const
128 wxSimpleDataObjectList::compatibility_iterator node
= m_dataObjects
.Item( m_preferred
);
130 wxCHECK_MSG( node
, wxFormatInvalid
, wxT("no preferred format") );
132 wxDataObjectSimple
* dataObj
= node
->GetData();
134 return dataObj
->GetFormat();
137 #if defined(__WXMSW__)
139 size_t wxDataObjectComposite::GetBufferOffset( const wxDataFormat
& format
)
141 wxDataObjectSimple
*dataObj
= GetObject(format
);
143 wxCHECK_MSG( dataObj
, 0,
144 wxT("unsupported format in wxDataObjectComposite"));
146 return dataObj
->GetBufferOffset( format
);
150 const void* wxDataObjectComposite::GetSizeFromBuffer( const void* buffer
,
152 const wxDataFormat
& format
)
154 wxDataObjectSimple
*dataObj
= GetObject(format
);
156 wxCHECK_MSG( dataObj
, NULL
,
157 wxT("unsupported format in wxDataObjectComposite"));
159 return dataObj
->GetSizeFromBuffer( buffer
, size
, format
);
163 void* wxDataObjectComposite::SetSizeInBuffer( void* buffer
, size_t size
,
164 const wxDataFormat
& format
)
166 wxDataObjectSimple
*dataObj
= GetObject( format
);
168 wxCHECK_MSG( dataObj
, NULL
,
169 wxT("unsupported format in wxDataObjectComposite"));
171 return dataObj
->SetSizeInBuffer( buffer
, size
, format
);
176 size_t wxDataObjectComposite::GetFormatCount(Direction dir
) const
180 // NOTE: some wxDataObjectSimple objects may return a number greater than 1
181 // from GetFormatCount(): this is the case of e.g. wxTextDataObject
182 // under wxMac and wxGTK
183 wxSimpleDataObjectList::compatibility_iterator node
;
184 for ( node
= m_dataObjects
.GetFirst(); node
; node
= node
->GetNext() )
185 n
+= node
->GetData()->GetFormatCount(dir
);
190 void wxDataObjectComposite::GetAllFormats(wxDataFormat
*formats
,
194 wxSimpleDataObjectList::compatibility_iterator node
;
196 for ( node
= m_dataObjects
.GetFirst(); node
; node
= node
->GetNext() )
198 // NOTE: some wxDataObjectSimple objects may return more than 1 format
199 // from GetAllFormats(): this is the case of e.g. wxTextDataObject
200 // under wxMac and wxGTK
201 node
->GetData()->GetAllFormats(formats
+index
, dir
);
202 index
+= node
->GetData()->GetFormatCount(dir
);
206 size_t wxDataObjectComposite::GetDataSize(const wxDataFormat
& format
) const
208 wxDataObjectSimple
*dataObj
= GetObject(format
);
210 wxCHECK_MSG( dataObj
, 0,
211 wxT("unsupported format in wxDataObjectComposite"));
213 return dataObj
->GetDataSize();
216 bool wxDataObjectComposite::GetDataHere(const wxDataFormat
& format
,
219 wxDataObjectSimple
*dataObj
= GetObject( format
);
221 wxCHECK_MSG( dataObj
, false,
222 wxT("unsupported format in wxDataObjectComposite"));
224 return dataObj
->GetDataHere( buf
);
227 bool wxDataObjectComposite::SetData(const wxDataFormat
& format
,
231 wxDataObjectSimple
*dataObj
= GetObject( format
);
233 wxCHECK_MSG( dataObj
, false,
234 wxT("unsupported format in wxDataObjectComposite"));
236 m_receivedFormat
= format
;
237 return dataObj
->SetData( len
, buf
);
240 // ----------------------------------------------------------------------------
242 // ----------------------------------------------------------------------------
244 #ifdef wxNEEDS_UTF8_FOR_TEXT_DATAOBJ
246 // FIXME-UTF8: we should be able to merge wchar_t and UTF-8 versions once we
247 // have a way to get UTF-8 string (and its length) in both builds
248 // without loss of efficiency (i.e. extra buffer copy/strlen call)
250 #if wxUSE_UNICODE_WCHAR
252 static inline wxMBConv
& GetConv(const wxDataFormat
& format
)
254 // use UTF8 for wxDF_UNICODETEXT and UCS4 for wxDF_TEXT
255 return format
== wxDF_UNICODETEXT
? wxConvUTF8
: wxConvLibc
;
258 size_t wxTextDataObject::GetDataSize(const wxDataFormat
& format
) const
260 wxCharBuffer buffer
= GetConv(format
).cWX2MB( GetText().c_str() );
262 return buffer
? strlen( buffer
) : 0;
265 bool wxTextDataObject::GetDataHere(const wxDataFormat
& format
, void *buf
) const
270 wxCharBuffer buffer
= GetConv(format
).cWX2MB( GetText().c_str() );
274 memcpy( (char*) buf
, buffer
, GetDataSize(format
) );
275 // strcpy( (char*) buf, buffer );
280 bool wxTextDataObject::SetData(const wxDataFormat
& format
,
281 size_t WXUNUSED(len
), const void *buf
)
286 wxWCharBuffer buffer
= GetConv(format
).cMB2WX( (const char*)buf
);
293 #else // wxUSE_UNICODE_UTF8
295 size_t wxTextDataObject::GetDataSize(const wxDataFormat
& format
) const
297 if ( format
== wxDF_UNICODETEXT
|| wxLocaleIsUtf8
)
299 return m_text
.utf8_length();
303 const wxCharBuffer
buf(wxConvLocal
.cWC2MB(m_text
.wc_str()));
304 return buf
? strlen(buf
) : 0;
308 bool wxTextDataObject::GetDataHere(const wxDataFormat
& format
, void *buf
) const
313 if ( format
== wxDF_UNICODETEXT
|| wxLocaleIsUtf8
)
315 memcpy(buf
, m_text
.utf8_str(), m_text
.utf8_length());
319 const wxCharBuffer
bufLocal(wxConvLocal
.cWC2MB(m_text
.wc_str()));
323 memcpy(buf
, bufLocal
, strlen(bufLocal
));
329 bool wxTextDataObject::SetData(const wxDataFormat
& format
,
330 size_t len
, const void *buf_
)
332 const char * const buf
= static_cast<const char *>(buf_
);
337 if ( format
== wxDF_UNICODETEXT
|| wxLocaleIsUtf8
)
339 // normally the data is in UTF-8 so we could use FromUTF8Unchecked()
340 // but it's not absolutely clear what GTK+ does if the clipboard data
341 // is not in UTF-8 so do an extra check for tranquility, it shouldn't
342 // matter much if we lose a bit of performance when pasting from
344 m_text
= wxString::FromUTF8(buf
, len
);
346 else // wxDF_TEXT, convert from current (non-UTF8) locale
348 m_text
= wxConvLocal
.cMB2WC(buf
, len
, NULL
);
354 #endif // wxUSE_UNICODE_WCHAR/wxUSE_UNICODE_UTF8
356 #elif defined(wxNEEDS_UTF16_FOR_TEXT_DATAOBJ)
361 inline wxMBConv
& GetConv(const wxDataFormat
& format
)
363 static wxMBConvUTF16 s_UTF16Converter
;
365 return format
== wxDF_UNICODETEXT
? static_cast<wxMBConv
&>(s_UTF16Converter
)
366 : static_cast<wxMBConv
&>(wxConvLocal
);
369 } // anonymous namespace
371 size_t wxTextDataObject::GetDataSize(const wxDataFormat
& format
) const
373 return GetConv(format
).WC2MB(NULL
, GetText().wc_str(), 0);
376 bool wxTextDataObject::GetDataHere(const wxDataFormat
& format
, void *buf
) const
381 wxCharBuffer
buffer(GetConv(format
).cWX2MB(GetText().c_str()));
383 memcpy(buf
, buffer
.data(), buffer
.length());
388 bool wxTextDataObject::SetData(const wxDataFormat
& format
,
389 size_t WXUNUSED(len
),
395 SetText(GetConv(format
).cMB2WX(static_cast<const char*>(buf
)));
400 #else // !wxNEEDS_UTF{8,16}_FOR_TEXT_DATAOBJ
402 size_t wxTextDataObject::GetDataSize() const
404 return GetTextLength() * sizeof(wxChar
);
407 bool wxTextDataObject::GetDataHere(void *buf
) const
409 // NOTE: use wxTmemcpy() instead of wxStrncpy() to allow
410 // retrieval of strings with embedded NULLs
411 wxTmemcpy( (wxChar
*)buf
, GetText().c_str(), GetTextLength() );
416 bool wxTextDataObject::SetData(size_t len
, const void *buf
)
418 SetText( wxString((const wxChar
*)buf
, len
/sizeof(wxChar
)) );
423 #endif // different wxTextDataObject implementations
425 // ----------------------------------------------------------------------------
426 // wxCustomDataObject
427 // ----------------------------------------------------------------------------
429 wxCustomDataObject::wxCustomDataObject(const wxDataFormat
& format
)
430 : wxDataObjectSimple(format
)
436 wxCustomDataObject::~wxCustomDataObject()
441 void wxCustomDataObject::TakeData(size_t size
, void *data
)
449 void *wxCustomDataObject::Alloc(size_t size
)
451 return (void *)new char[size
];
454 void wxCustomDataObject::Free()
456 delete [] (char*)m_data
;
461 size_t wxCustomDataObject::GetDataSize() const
466 bool wxCustomDataObject::GetDataHere(void *buf
) const
471 void *data
= GetData();
475 memcpy( buf
, data
, GetSize() );
480 bool wxCustomDataObject::SetData(size_t size
, const void *buf
)
484 m_data
= Alloc(size
);
485 if ( m_data
== NULL
)
489 memcpy( m_data
, buf
, m_size
);
494 // ============================================================================
495 // some common dnd related code
496 // ============================================================================
498 #if wxUSE_DRAG_AND_DROP
502 // ----------------------------------------------------------------------------
504 // ----------------------------------------------------------------------------
506 // NB: we can't use "new" in ctor initializer lists because this provokes an
507 // internal compiler error with VC++ 5.0 (hey, even gcc compiles this!),
508 // so use SetDataObject() instead
510 wxTextDropTarget::wxTextDropTarget()
512 SetDataObject(new wxTextDataObject
);
515 wxDragResult
wxTextDropTarget::OnData(wxCoord x
, wxCoord y
, wxDragResult def
)
520 wxTextDataObject
*dobj
= (wxTextDataObject
*)m_dataObject
;
521 return OnDropText( x
, y
, dobj
->GetText() ) ? def
: wxDragNone
;
524 // ----------------------------------------------------------------------------
526 // ----------------------------------------------------------------------------
528 wxFileDropTarget::wxFileDropTarget()
530 SetDataObject(new wxFileDataObject
);
533 wxDragResult
wxFileDropTarget::OnData(wxCoord x
, wxCoord y
, wxDragResult def
)
538 wxFileDataObject
*dobj
= (wxFileDataObject
*)m_dataObject
;
539 return OnDropFiles( x
, y
, dobj
->GetFilenames() ) ? def
: wxDragNone
;
542 #endif // wxUSE_DRAG_AND_DROP
544 #endif // wxUSE_DATAOBJ