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
;
238 // Notice that we must pass "format" here as wxTextDataObject, that we can
239 // have as one of our "simple" sub-objects actually is not that simple and
240 // can support multiple formats (ASCII/UTF-8/UTF-16/...) and so needs to
241 // know which one it is given.
242 return dataObj
->SetData( format
, len
, buf
);
245 // ----------------------------------------------------------------------------
247 // ----------------------------------------------------------------------------
249 #ifdef wxNEEDS_UTF8_FOR_TEXT_DATAOBJ
251 // FIXME-UTF8: we should be able to merge wchar_t and UTF-8 versions once we
252 // have a way to get UTF-8 string (and its length) in both builds
253 // without loss of efficiency (i.e. extra buffer copy/strlen call)
255 #if wxUSE_UNICODE_WCHAR
257 static inline wxMBConv
& GetConv(const wxDataFormat
& format
)
259 // use UTF8 for wxDF_UNICODETEXT and UCS4 for wxDF_TEXT
260 return format
== wxDF_UNICODETEXT
? wxConvUTF8
: wxConvLibc
;
263 size_t wxTextDataObject::GetDataSize(const wxDataFormat
& format
) const
265 wxCharBuffer buffer
= GetConv(format
).cWX2MB( GetText().c_str() );
267 return buffer
? strlen( buffer
) : 0;
270 bool wxTextDataObject::GetDataHere(const wxDataFormat
& format
, void *buf
) const
275 wxCharBuffer buffer
= GetConv(format
).cWX2MB( GetText().c_str() );
279 memcpy( (char*) buf
, buffer
, GetDataSize(format
) );
280 // strcpy( (char*) buf, buffer );
285 bool wxTextDataObject::SetData(const wxDataFormat
& format
,
286 size_t WXUNUSED(len
), const void *buf
)
291 wxWCharBuffer buffer
= GetConv(format
).cMB2WX( (const char*)buf
);
298 #else // wxUSE_UNICODE_UTF8
300 size_t wxTextDataObject::GetDataSize(const wxDataFormat
& format
) const
302 if ( format
== wxDF_UNICODETEXT
|| wxLocaleIsUtf8
)
304 return m_text
.utf8_length();
308 const wxCharBuffer
buf(wxConvLocal
.cWC2MB(m_text
.wc_str()));
309 return buf
? strlen(buf
) : 0;
313 bool wxTextDataObject::GetDataHere(const wxDataFormat
& format
, void *buf
) const
318 if ( format
== wxDF_UNICODETEXT
|| wxLocaleIsUtf8
)
320 memcpy(buf
, m_text
.utf8_str(), m_text
.utf8_length());
324 const wxCharBuffer
bufLocal(wxConvLocal
.cWC2MB(m_text
.wc_str()));
328 memcpy(buf
, bufLocal
, strlen(bufLocal
));
334 bool wxTextDataObject::SetData(const wxDataFormat
& format
,
335 size_t len
, const void *buf_
)
337 const char * const buf
= static_cast<const char *>(buf_
);
342 if ( format
== wxDF_UNICODETEXT
|| wxLocaleIsUtf8
)
344 // normally the data is in UTF-8 so we could use FromUTF8Unchecked()
345 // but it's not absolutely clear what GTK+ does if the clipboard data
346 // is not in UTF-8 so do an extra check for tranquility, it shouldn't
347 // matter much if we lose a bit of performance when pasting from
349 m_text
= wxString::FromUTF8(buf
, len
);
351 else // wxDF_TEXT, convert from current (non-UTF8) locale
353 m_text
= wxConvLocal
.cMB2WC(buf
, len
, NULL
);
359 #endif // wxUSE_UNICODE_WCHAR/wxUSE_UNICODE_UTF8
361 #elif defined(wxNEEDS_UTF16_FOR_TEXT_DATAOBJ)
366 inline wxMBConv
& GetConv(const wxDataFormat
& format
)
368 static wxMBConvUTF16 s_UTF16Converter
;
370 return format
== wxDF_UNICODETEXT
? static_cast<wxMBConv
&>(s_UTF16Converter
)
371 : static_cast<wxMBConv
&>(wxConvLocal
);
374 } // anonymous namespace
376 size_t wxTextDataObject::GetDataSize(const wxDataFormat
& format
) const
378 return GetConv(format
).WC2MB(NULL
, GetText().wc_str(), 0);
381 bool wxTextDataObject::GetDataHere(const wxDataFormat
& format
, void *buf
) const
386 wxCharBuffer
buffer(GetConv(format
).cWX2MB(GetText().c_str()));
388 memcpy(buf
, buffer
.data(), buffer
.length());
393 bool wxTextDataObject::SetData(const wxDataFormat
& format
,
394 size_t WXUNUSED(len
),
400 SetText(GetConv(format
).cMB2WX(static_cast<const char*>(buf
)));
405 #else // !wxNEEDS_UTF{8,16}_FOR_TEXT_DATAOBJ
407 size_t wxTextDataObject::GetDataSize() const
409 return GetTextLength() * sizeof(wxChar
);
412 bool wxTextDataObject::GetDataHere(void *buf
) const
414 // NOTE: use wxTmemcpy() instead of wxStrncpy() to allow
415 // retrieval of strings with embedded NULLs
416 wxTmemcpy( (wxChar
*)buf
, GetText().c_str(), GetTextLength() );
421 bool wxTextDataObject::SetData(size_t len
, const void *buf
)
423 SetText( wxString((const wxChar
*)buf
, len
/sizeof(wxChar
)) );
428 #endif // different wxTextDataObject implementations
430 size_t wxHTMLDataObject::GetDataSize() const
432 const wxScopedCharBuffer
buffer(GetHTML().utf8_str());
434 size_t size
= buffer
.length();
437 // On Windows we need to add some stuff to the string to satisfy
438 // its clipboard format requirements.
445 bool wxHTMLDataObject::GetDataHere(void *buf
) const
450 // Windows and Mac always use UTF-8, and docs suggest GTK does as well.
451 const wxScopedCharBuffer
html(GetHTML().utf8_str());
455 char* const buffer
= static_cast<char*>(buf
);
458 // add the extra info that the MSW clipboard format requires.
460 // Create a template string for the HTML header...
463 "StartHTML:00000000\r\n"
464 "EndHTML:00000000\r\n"
465 "StartFragment:00000000\r\n"
466 "EndFragment:00000000\r\n"
468 "<!--StartFragment -->\r\n");
470 // Append the HTML...
471 strcat(buffer
, html
);
472 strcat(buffer
, "\r\n");
473 // Finish up the HTML format...
475 "<!--EndFragment-->\r\n"
479 // Now go back, calculate all the lengths, and write out the
480 // necessary header information. Note, wsprintf() truncates the
481 // string when you overwrite it so you follow up with code to replace
482 // the 0 appended at the end with a '\r'...
483 char *ptr
= strstr(buffer
, "StartHTML");
484 sprintf(ptr
+10, "%08u", (unsigned)(strstr(buffer
, "<html>") - buffer
));
487 ptr
= strstr(buffer
, "EndHTML");
488 sprintf(ptr
+8, "%08u", (unsigned)strlen(buffer
));
491 ptr
= strstr(buffer
, "StartFragment");
492 sprintf(ptr
+14, "%08u", (unsigned)(strstr(buffer
, "<!--StartFrag") - buffer
));
495 ptr
= strstr(buffer
, "EndFragment");
496 sprintf(ptr
+12, "%08u", (unsigned)(strstr(buffer
, "<!--EndFrag") - buffer
));
499 strcpy(buffer
, html
);
505 bool wxHTMLDataObject::SetData(size_t WXUNUSED(len
), const void *buf
)
510 // Windows and Mac always use UTF-8, and docs suggest GTK does as well.
511 wxString html
= wxString::FromUTF8(static_cast<const char*>(buf
));
514 // To be consistent with other platforms, we only add the Fragment part
515 // of the Windows HTML clipboard format to the data object.
516 int fragmentStart
= html
.rfind("StartFragment");
517 int fragmentEnd
= html
.rfind("EndFragment");
519 if (fragmentStart
!= wxNOT_FOUND
&& fragmentEnd
!= wxNOT_FOUND
)
521 int startCommentEnd
= html
.find("-->", fragmentStart
) + 3;
522 int endCommentStart
= html
.rfind("<!--", fragmentEnd
);
524 if (startCommentEnd
!= wxNOT_FOUND
&& endCommentStart
!= wxNOT_FOUND
)
525 html
= html
.Mid(startCommentEnd
, endCommentStart
- startCommentEnd
);
535 // ----------------------------------------------------------------------------
536 // wxCustomDataObject
537 // ----------------------------------------------------------------------------
539 wxCustomDataObject::wxCustomDataObject(const wxDataFormat
& format
)
540 : wxDataObjectSimple(format
)
546 wxCustomDataObject::~wxCustomDataObject()
551 void wxCustomDataObject::TakeData(size_t size
, void *data
)
559 void *wxCustomDataObject::Alloc(size_t size
)
561 return (void *)new char[size
];
564 void wxCustomDataObject::Free()
566 delete [] (char*)m_data
;
571 size_t wxCustomDataObject::GetDataSize() const
576 bool wxCustomDataObject::GetDataHere(void *buf
) const
581 void *data
= GetData();
585 memcpy( buf
, data
, GetSize() );
590 bool wxCustomDataObject::SetData(size_t size
, const void *buf
)
594 m_data
= Alloc(size
);
595 if ( m_data
== NULL
)
599 memcpy( m_data
, buf
, m_size
);
604 // ============================================================================
605 // some common dnd related code
606 // ============================================================================
608 #if wxUSE_DRAG_AND_DROP
612 // ----------------------------------------------------------------------------
614 // ----------------------------------------------------------------------------
616 // NB: we can't use "new" in ctor initializer lists because this provokes an
617 // internal compiler error with VC++ 5.0 (hey, even gcc compiles this!),
618 // so use SetDataObject() instead
620 wxTextDropTarget::wxTextDropTarget()
622 SetDataObject(new wxTextDataObject
);
625 wxDragResult
wxTextDropTarget::OnData(wxCoord x
, wxCoord y
, wxDragResult def
)
630 wxTextDataObject
*dobj
= (wxTextDataObject
*)m_dataObject
;
631 return OnDropText( x
, y
, dobj
->GetText() ) ? def
: wxDragNone
;
634 // ----------------------------------------------------------------------------
636 // ----------------------------------------------------------------------------
638 wxFileDropTarget::wxFileDropTarget()
640 SetDataObject(new wxFileDataObject
);
643 wxDragResult
wxFileDropTarget::OnData(wxCoord x
, wxCoord y
, wxDragResult def
)
648 wxFileDataObject
*dobj
= (wxFileDataObject
*)m_dataObject
;
649 return OnDropFiles( x
, y
, dobj
->GetFilenames() ) ? def
: wxDragNone
;
652 #endif // wxUSE_DRAG_AND_DROP
654 #endif // wxUSE_DATAOBJ