1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/dobjcmn.cpp
3 // Purpose: implementation of data object methods common to all platforms
4 // Author: Vadim Zeitlin, Robert Roebling
7 // Copyright: (c) wxWidgets Team
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
20 #include "wx/dataobj.h"
26 #include "wx/textbuf.h"
28 // ----------------------------------------------------------------------------
30 // ----------------------------------------------------------------------------
32 #include "wx/listimpl.cpp"
34 WX_DEFINE_LIST(wxSimpleDataObjectList
)
36 // ----------------------------------------------------------------------------
38 // ----------------------------------------------------------------------------
40 static wxDataFormat dataFormatInvalid
;
41 WXDLLEXPORT
const wxDataFormat
& wxFormatInvalid
= dataFormatInvalid
;
43 // ============================================================================
45 // ============================================================================
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
51 wxDataObjectBase::~wxDataObjectBase()
55 bool wxDataObjectBase::IsSupported(const wxDataFormat
& format
,
58 size_t nFormatCount
= GetFormatCount( dir
);
59 if ( nFormatCount
== 1 )
61 return format
== GetPreferredFormat( dir
);
65 wxDataFormat
*formats
= new wxDataFormat
[nFormatCount
];
66 GetAllFormats( formats
, dir
);
69 for ( n
= 0; n
< nFormatCount
; n
++ )
71 if ( formats
[n
] == format
)
78 return n
< nFormatCount
;
82 // ----------------------------------------------------------------------------
83 // wxDataObjectComposite
84 // ----------------------------------------------------------------------------
86 wxDataObjectComposite::wxDataObjectComposite()
89 m_receivedFormat
= wxFormatInvalid
;
92 wxDataObjectComposite::~wxDataObjectComposite()
94 WX_CLEAR_LIST( wxSimpleDataObjectList
, m_dataObjects
);
98 wxDataObjectComposite::GetObject(const wxDataFormat
& format
, wxDataObjectBase::Direction dir
) const
100 wxSimpleDataObjectList::compatibility_iterator node
= m_dataObjects
.GetFirst();
104 wxDataObjectSimple
*dataObj
= node
->GetData();
106 if (dataObj
->IsSupported(format
,dir
))
108 node
= node
->GetNext();
113 void wxDataObjectComposite::Add(wxDataObjectSimple
*dataObject
, bool preferred
)
116 m_preferred
= m_dataObjects
.GetCount();
118 m_dataObjects
.Append( dataObject
);
121 wxDataFormat
wxDataObjectComposite::GetReceivedFormat() const
123 return m_receivedFormat
;
127 wxDataObjectComposite::GetPreferredFormat(Direction
WXUNUSED(dir
)) const
129 wxSimpleDataObjectList::compatibility_iterator node
= m_dataObjects
.Item( m_preferred
);
131 wxCHECK_MSG( node
, wxFormatInvalid
, wxT("no preferred format") );
133 wxDataObjectSimple
* dataObj
= node
->GetData();
135 return dataObj
->GetFormat();
138 #if defined(__WXMSW__)
140 size_t wxDataObjectComposite::GetBufferOffset( const wxDataFormat
& format
)
142 wxDataObjectSimple
*dataObj
= GetObject(format
);
144 wxCHECK_MSG( dataObj
, 0,
145 wxT("unsupported format in wxDataObjectComposite"));
147 return dataObj
->GetBufferOffset( format
);
151 const void* wxDataObjectComposite::GetSizeFromBuffer( const void* buffer
,
153 const wxDataFormat
& format
)
155 wxDataObjectSimple
*dataObj
= GetObject(format
);
157 wxCHECK_MSG( dataObj
, NULL
,
158 wxT("unsupported format in wxDataObjectComposite"));
160 return dataObj
->GetSizeFromBuffer( buffer
, size
, format
);
164 void* wxDataObjectComposite::SetSizeInBuffer( void* buffer
, size_t size
,
165 const wxDataFormat
& format
)
167 wxDataObjectSimple
*dataObj
= GetObject( format
);
169 wxCHECK_MSG( dataObj
, NULL
,
170 wxT("unsupported format in wxDataObjectComposite"));
172 return dataObj
->SetSizeInBuffer( buffer
, size
, format
);
177 size_t wxDataObjectComposite::GetFormatCount(Direction dir
) const
181 // NOTE: some wxDataObjectSimple objects may return a number greater than 1
182 // from GetFormatCount(): this is the case of e.g. wxTextDataObject
183 // under wxMac and wxGTK
184 wxSimpleDataObjectList::compatibility_iterator node
;
185 for ( node
= m_dataObjects
.GetFirst(); node
; node
= node
->GetNext() )
186 n
+= node
->GetData()->GetFormatCount(dir
);
191 void wxDataObjectComposite::GetAllFormats(wxDataFormat
*formats
,
195 wxSimpleDataObjectList::compatibility_iterator node
;
197 for ( node
= m_dataObjects
.GetFirst(); node
; node
= node
->GetNext() )
199 // NOTE: some wxDataObjectSimple objects may return more than 1 format
200 // from GetAllFormats(): this is the case of e.g. wxTextDataObject
201 // under wxMac and wxGTK
202 node
->GetData()->GetAllFormats(formats
+index
, dir
);
203 index
+= node
->GetData()->GetFormatCount(dir
);
207 size_t wxDataObjectComposite::GetDataSize(const wxDataFormat
& format
) const
209 wxDataObjectSimple
*dataObj
= GetObject(format
);
211 wxCHECK_MSG( dataObj
, 0,
212 wxT("unsupported format in wxDataObjectComposite"));
214 return dataObj
->GetDataSize();
217 bool wxDataObjectComposite::GetDataHere(const wxDataFormat
& format
,
220 wxDataObjectSimple
*dataObj
= GetObject( format
);
222 wxCHECK_MSG( dataObj
, false,
223 wxT("unsupported format in wxDataObjectComposite"));
225 return dataObj
->GetDataHere( buf
);
228 bool wxDataObjectComposite::SetData(const wxDataFormat
& format
,
232 wxDataObjectSimple
*dataObj
= GetObject( format
);
234 wxCHECK_MSG( dataObj
, false,
235 wxT("unsupported format in wxDataObjectComposite"));
237 m_receivedFormat
= format
;
239 // Notice that we must pass "format" here as wxTextDataObject, that we can
240 // have as one of our "simple" sub-objects actually is not that simple and
241 // can support multiple formats (ASCII/UTF-8/UTF-16/...) and so needs to
242 // know which one it is given.
243 return dataObj
->SetData( format
, len
, buf
);
246 // ----------------------------------------------------------------------------
248 // ----------------------------------------------------------------------------
250 #ifdef wxNEEDS_UTF8_FOR_TEXT_DATAOBJ
252 // FIXME-UTF8: we should be able to merge wchar_t and UTF-8 versions once we
253 // have a way to get UTF-8 string (and its length) in both builds
254 // without loss of efficiency (i.e. extra buffer copy/strlen call)
256 #if wxUSE_UNICODE_WCHAR
258 static inline wxMBConv
& GetConv(const wxDataFormat
& format
)
260 // use UTF8 for wxDF_UNICODETEXT and UCS4 for wxDF_TEXT
261 return format
== wxDF_UNICODETEXT
? wxConvUTF8
: wxConvLibc
;
264 size_t wxTextDataObject::GetDataSize(const wxDataFormat
& format
) const
266 wxCharBuffer buffer
= GetConv(format
).cWX2MB( GetText().c_str() );
268 return buffer
? strlen( buffer
) : 0;
271 bool wxTextDataObject::GetDataHere(const wxDataFormat
& format
, void *buf
) const
276 wxCharBuffer buffer
= GetConv(format
).cWX2MB( GetText().c_str() );
280 memcpy( (char*) buf
, buffer
, GetDataSize(format
) );
281 // strcpy( (char*) buf, buffer );
286 bool wxTextDataObject::SetData(const wxDataFormat
& format
,
287 size_t WXUNUSED(len
), const void *buf
)
292 wxWCharBuffer buffer
= GetConv(format
).cMB2WX( (const char*)buf
);
299 #else // wxUSE_UNICODE_UTF8
301 size_t wxTextDataObject::GetDataSize(const wxDataFormat
& format
) const
303 const wxString
& text
= GetText();
304 if ( format
== wxDF_UNICODETEXT
|| wxLocaleIsUtf8
)
306 return text
.utf8_length();
310 const wxCharBuffer
buf(wxConvLocal
.cWC2MB(text
.wc_str()));
311 return buf
? strlen(buf
) : 0;
315 bool wxTextDataObject::GetDataHere(const wxDataFormat
& format
, void *buf
) const
320 const wxString
& text
= GetText();
321 if ( format
== wxDF_UNICODETEXT
|| wxLocaleIsUtf8
)
323 memcpy(buf
, text
.utf8_str(), text
.utf8_length());
327 const wxCharBuffer
bufLocal(wxConvLocal
.cWC2MB(text
.wc_str()));
331 memcpy(buf
, bufLocal
, strlen(bufLocal
));
337 bool wxTextDataObject::SetData(const wxDataFormat
& format
,
338 size_t len
, const void *buf_
)
340 const char * const buf
= static_cast<const char *>(buf_
);
345 if ( format
== wxDF_UNICODETEXT
|| wxLocaleIsUtf8
)
347 // normally the data is in UTF-8 so we could use FromUTF8Unchecked()
348 // but it's not absolutely clear what GTK+ does if the clipboard data
349 // is not in UTF-8 so do an extra check for tranquility, it shouldn't
350 // matter much if we lose a bit of performance when pasting from
352 SetText(wxString::FromUTF8(buf
, len
));
354 else // wxDF_TEXT, convert from current (non-UTF8) locale
356 SetText(wxConvLocal
.cMB2WC(buf
, len
, NULL
));
362 #endif // wxUSE_UNICODE_WCHAR/wxUSE_UNICODE_UTF8
364 #elif defined(wxNEEDS_UTF16_FOR_TEXT_DATAOBJ)
369 inline wxMBConv
& GetConv(const wxDataFormat
& format
)
371 static wxMBConvUTF16 s_UTF16Converter
;
373 return format
== wxDF_UNICODETEXT
? static_cast<wxMBConv
&>(s_UTF16Converter
)
374 : static_cast<wxMBConv
&>(wxConvLocal
);
377 } // anonymous namespace
379 size_t wxTextDataObject::GetDataSize(const wxDataFormat
& format
) const
381 return GetConv(format
).WC2MB(NULL
, GetText().wc_str(), 0);
384 bool wxTextDataObject::GetDataHere(const wxDataFormat
& format
, void *buf
) const
389 wxCharBuffer
buffer(GetConv(format
).cWX2MB(GetText().c_str()));
391 memcpy(buf
, buffer
.data(), buffer
.length());
396 bool wxTextDataObject::SetData(const wxDataFormat
& format
,
397 size_t WXUNUSED(len
),
403 SetText(GetConv(format
).cMB2WX(static_cast<const char*>(buf
)));
408 #else // !wxNEEDS_UTF{8,16}_FOR_TEXT_DATAOBJ
410 // NB: This branch, using native wxChar for the clipboard, is only used under
411 // Windows currently. It's just a coincidence, but Windows is also the only
412 // platform where we need to convert the text to the native EOL format, so
413 // wxTextBuffer::Translate() is only used here and not in the code above.
415 size_t wxTextDataObject::GetDataSize() const
417 return (wxTextBuffer::Translate(GetText()).length() + 1)*sizeof(wxChar
);
420 bool wxTextDataObject::GetDataHere(void *buf
) const
422 const wxString textNative
= wxTextBuffer::Translate(GetText());
424 // NOTE: use wxTmemcpy() instead of wxStrncpy() to allow
425 // retrieval of strings with embedded NULLs
426 wxTmemcpy(static_cast<wxChar
*>(buf
),
428 textNative
.length() + 1);
433 bool wxTextDataObject::SetData(size_t len
, const void *buf
)
436 text
= wxString(static_cast<const wxChar
*>(buf
), len
/sizeof(wxChar
));
437 SetText(wxTextBuffer::Translate(text
, wxTextFileType_Unix
));
442 #endif // different wxTextDataObject implementations
444 // ----------------------------------------------------------------------------
446 // ----------------------------------------------------------------------------
448 size_t wxHTMLDataObject::GetDataSize() const
450 // Ensure that the temporary string returned by GetHTML() is kept alive for
451 // as long as we need it here.
452 const wxString
& htmlStr
= GetHTML();
453 const wxScopedCharBuffer
buffer(htmlStr
.utf8_str());
455 size_t size
= buffer
.length();
458 // On Windows we need to add some stuff to the string to satisfy
459 // its clipboard format requirements.
466 bool wxHTMLDataObject::GetDataHere(void *buf
) const
471 // Windows and Mac always use UTF-8, and docs suggest GTK does as well.
472 const wxString
& htmlStr
= GetHTML();
473 const wxScopedCharBuffer
html(htmlStr
.utf8_str());
477 char* const buffer
= static_cast<char*>(buf
);
480 // add the extra info that the MSW clipboard format requires.
482 // Create a template string for the HTML header...
485 "StartHTML:00000000\r\n"
486 "EndHTML:00000000\r\n"
487 "StartFragment:00000000\r\n"
488 "EndFragment:00000000\r\n"
490 "<!--StartFragment -->\r\n");
492 // Append the HTML...
493 strcat(buffer
, html
);
494 strcat(buffer
, "\r\n");
495 // Finish up the HTML format...
497 "<!--EndFragment-->\r\n"
501 // Now go back, calculate all the lengths, and write out the
502 // necessary header information. Note, wsprintf() truncates the
503 // string when you overwrite it so you follow up with code to replace
504 // the 0 appended at the end with a '\r'...
505 char *ptr
= strstr(buffer
, "StartHTML");
506 sprintf(ptr
+10, "%08u", (unsigned)(strstr(buffer
, "<html>") - buffer
));
509 ptr
= strstr(buffer
, "EndHTML");
510 sprintf(ptr
+8, "%08u", (unsigned)strlen(buffer
));
513 ptr
= strstr(buffer
, "StartFragment");
514 sprintf(ptr
+14, "%08u", (unsigned)(strstr(buffer
, "<!--StartFrag") - buffer
));
517 ptr
= strstr(buffer
, "EndFragment");
518 sprintf(ptr
+12, "%08u", (unsigned)(strstr(buffer
, "<!--EndFrag") - buffer
));
521 strcpy(buffer
, html
);
527 bool wxHTMLDataObject::SetData(size_t WXUNUSED(len
), const void *buf
)
532 // Windows and Mac always use UTF-8, and docs suggest GTK does as well.
533 wxString html
= wxString::FromUTF8(static_cast<const char*>(buf
));
536 // To be consistent with other platforms, we only add the Fragment part
537 // of the Windows HTML clipboard format to the data object.
538 int fragmentStart
= html
.rfind("StartFragment");
539 int fragmentEnd
= html
.rfind("EndFragment");
541 if (fragmentStart
!= wxNOT_FOUND
&& fragmentEnd
!= wxNOT_FOUND
)
543 int startCommentEnd
= html
.find("-->", fragmentStart
) + 3;
544 int endCommentStart
= html
.rfind("<!--", fragmentEnd
);
546 if (startCommentEnd
!= wxNOT_FOUND
&& endCommentStart
!= wxNOT_FOUND
)
547 html
= html
.Mid(startCommentEnd
, endCommentStart
- startCommentEnd
);
557 // ----------------------------------------------------------------------------
558 // wxCustomDataObject
559 // ----------------------------------------------------------------------------
561 wxCustomDataObject::wxCustomDataObject(const wxDataFormat
& format
)
562 : wxDataObjectSimple(format
)
568 wxCustomDataObject::~wxCustomDataObject()
573 void wxCustomDataObject::TakeData(size_t size
, void *data
)
581 void *wxCustomDataObject::Alloc(size_t size
)
583 return (void *)new char[size
];
586 void wxCustomDataObject::Free()
588 delete [] (char*)m_data
;
593 size_t wxCustomDataObject::GetDataSize() const
598 bool wxCustomDataObject::GetDataHere(void *buf
) const
603 void *data
= GetData();
607 memcpy( buf
, data
, GetSize() );
612 bool wxCustomDataObject::SetData(size_t size
, const void *buf
)
616 m_data
= Alloc(size
);
617 if ( m_data
== NULL
)
621 memcpy( m_data
, buf
, m_size
);
626 // ============================================================================
627 // some common dnd related code
628 // ============================================================================
630 #if wxUSE_DRAG_AND_DROP
634 // ----------------------------------------------------------------------------
636 // ----------------------------------------------------------------------------
638 // NB: we can't use "new" in ctor initializer lists because this provokes an
639 // internal compiler error with VC++ 5.0 (hey, even gcc compiles this!),
640 // so use SetDataObject() instead
642 wxTextDropTarget::wxTextDropTarget()
644 SetDataObject(new wxTextDataObject
);
647 wxDragResult
wxTextDropTarget::OnData(wxCoord x
, wxCoord y
, wxDragResult def
)
652 wxTextDataObject
*dobj
= (wxTextDataObject
*)m_dataObject
;
653 return OnDropText( x
, y
, dobj
->GetText() ) ? def
: wxDragNone
;
656 // ----------------------------------------------------------------------------
658 // ----------------------------------------------------------------------------
660 wxFileDropTarget::wxFileDropTarget()
662 SetDataObject(new wxFileDataObject
);
665 wxDragResult
wxFileDropTarget::OnData(wxCoord x
, wxCoord y
, wxDragResult def
)
670 wxFileDataObject
*dobj
= (wxFileDataObject
*)m_dataObject
;
671 return OnDropFiles( x
, y
, dobj
->GetFilenames() ) ? def
: wxDragNone
;
674 #endif // wxUSE_DRAG_AND_DROP
676 #endif // wxUSE_DATAOBJ