]> git.saurik.com Git - wxWidgets.git/blob - src/common/dobjcmn.cpp
Correct UTF-32BE BOM detection in wxConvAuto.
[wxWidgets.git] / src / common / dobjcmn.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/dobjcmn.cpp
3 // Purpose: implementation of data object methods common to all platforms
4 // Author: Vadim Zeitlin, Robert Roebling
5 // Modified by:
6 // Created: 19.10.99
7 // RCS-ID: $Id$
8 // Copyright: (c) wxWidgets Team
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #if wxUSE_DATAOBJ
20
21 #include "wx/dataobj.h"
22
23 #ifndef WX_PRECOMP
24 #include "wx/app.h"
25 #endif
26
27 // ----------------------------------------------------------------------------
28 // lists
29 // ----------------------------------------------------------------------------
30
31 #include "wx/listimpl.cpp"
32
33 WX_DEFINE_LIST(wxSimpleDataObjectList)
34
35 // ----------------------------------------------------------------------------
36 // globals
37 // ----------------------------------------------------------------------------
38
39 static wxDataFormat dataFormatInvalid;
40 WXDLLEXPORT const wxDataFormat& wxFormatInvalid = dataFormatInvalid;
41
42 // ============================================================================
43 // implementation
44 // ============================================================================
45
46 // ----------------------------------------------------------------------------
47 // wxDataObjectBase
48 // ----------------------------------------------------------------------------
49
50 wxDataObjectBase::~wxDataObjectBase()
51 {
52 }
53
54 bool wxDataObjectBase::IsSupported(const wxDataFormat& format,
55 Direction dir) const
56 {
57 size_t nFormatCount = GetFormatCount( dir );
58 if ( nFormatCount == 1 )
59 {
60 return format == GetPreferredFormat( dir );
61 }
62 else
63 {
64 wxDataFormat *formats = new wxDataFormat[nFormatCount];
65 GetAllFormats( formats, dir );
66
67 size_t n;
68 for ( n = 0; n < nFormatCount; n++ )
69 {
70 if ( formats[n] == format )
71 break;
72 }
73
74 delete [] formats;
75
76 // found?
77 return n < nFormatCount;
78 }
79 }
80
81 // ----------------------------------------------------------------------------
82 // wxDataObjectComposite
83 // ----------------------------------------------------------------------------
84
85 wxDataObjectComposite::wxDataObjectComposite()
86 {
87 m_preferred = 0;
88 m_receivedFormat = wxFormatInvalid;
89 }
90
91 wxDataObjectComposite::~wxDataObjectComposite()
92 {
93 WX_CLEAR_LIST( wxSimpleDataObjectList, m_dataObjects );
94 }
95
96 wxDataObjectSimple *
97 wxDataObjectComposite::GetObject(const wxDataFormat& format, wxDataObjectBase::Direction dir) const
98 {
99 wxSimpleDataObjectList::compatibility_iterator node = m_dataObjects.GetFirst();
100
101 while ( node )
102 {
103 wxDataObjectSimple *dataObj = node->GetData();
104
105 if (dataObj->IsSupported(format,dir))
106 return dataObj;
107 node = node->GetNext();
108 }
109 return NULL;
110 }
111
112 void wxDataObjectComposite::Add(wxDataObjectSimple *dataObject, bool preferred)
113 {
114 // check if the data format of the passed object already exists in the composite data object, if this is the case
115 // do not add the data object and display a message in debug mode (otherwise this method fails silently):
116 // start checking if the data format exists for the 'GET' direction:
117 size_t indexFormats;
118 size_t noOfFormats;
119 wxDataFormat* formats;
120
121 noOfFormats = dataObject->GetFormatCount(wxDataObjectBase::Get);
122 formats = new wxDataFormat[noOfFormats];
123 for (indexFormats=0; indexFormats<noOfFormats; ++indexFormats)
124 wxCHECK_RET(this->GetObject(formats[indexFormats],wxDataObjectBase::Get) == NULL,
125 _("The data format for the GET-direction of the to be added data object already exists"));
126 delete[] formats;
127 // do the same with the 'SET' direction:
128 noOfFormats = dataObject->GetFormatCount(wxDataObjectBase::Set);
129
130 formats = new wxDataFormat[noOfFormats];
131 for (indexFormats=0; indexFormats<noOfFormats; ++indexFormats)
132 wxCHECK_RET(this->GetObject(formats[indexFormats],wxDataObjectBase::Set) == NULL,
133 _("The data format for the SET-direction of the to be added data object already exists"));
134 delete[] formats;
135
136 // if we reach this location the data object can simply be appended:
137 if ( preferred )
138 m_preferred = m_dataObjects.GetCount();
139 m_dataObjects.Append( dataObject );
140 }
141
142 wxDataFormat wxDataObjectComposite::GetReceivedFormat() const
143 {
144 return m_receivedFormat;
145 }
146
147 wxDataFormat
148 wxDataObjectComposite::GetPreferredFormat(Direction WXUNUSED(dir)) const
149 {
150 wxSimpleDataObjectList::compatibility_iterator node = m_dataObjects.Item( m_preferred );
151
152 wxCHECK_MSG( node, wxFormatInvalid, wxT("no preferred format") );
153
154 wxDataObjectSimple* dataObj = node->GetData();
155
156 return dataObj->GetFormat();
157 }
158
159 #if defined(__WXMSW__)
160
161 size_t wxDataObjectComposite::GetBufferOffset( const wxDataFormat& format )
162 {
163 wxDataObjectSimple *dataObj = GetObject(format);
164
165 wxCHECK_MSG( dataObj, 0,
166 wxT("unsupported format in wxDataObjectComposite"));
167
168 return dataObj->GetBufferOffset( format );
169 }
170
171
172 const void* wxDataObjectComposite::GetSizeFromBuffer( const void* buffer,
173 size_t* size,
174 const wxDataFormat& format )
175 {
176 wxDataObjectSimple *dataObj = GetObject(format);
177
178 wxCHECK_MSG( dataObj, NULL,
179 wxT("unsupported format in wxDataObjectComposite"));
180
181 return dataObj->GetSizeFromBuffer( buffer, size, format );
182 }
183
184
185 void* wxDataObjectComposite::SetSizeInBuffer( void* buffer, size_t size,
186 const wxDataFormat& format )
187 {
188 wxDataObjectSimple *dataObj = GetObject( format );
189
190 wxCHECK_MSG( dataObj, NULL,
191 wxT("unsupported format in wxDataObjectComposite"));
192
193 return dataObj->SetSizeInBuffer( buffer, size, format );
194 }
195
196 #endif
197
198 size_t wxDataObjectComposite::GetFormatCount(Direction dir) const
199 {
200 size_t n = 0;
201
202 // NOTE: some wxDataObjectSimple objects may return a number greater than 1
203 // from GetFormatCount(): this is the case of e.g. wxTextDataObject
204 // under wxMac and wxGTK
205 wxSimpleDataObjectList::compatibility_iterator node;
206 for ( node = m_dataObjects.GetFirst(); node; node = node->GetNext() )
207 n += node->GetData()->GetFormatCount(dir);
208
209 return n;
210 }
211
212 void wxDataObjectComposite::GetAllFormats(wxDataFormat *formats,
213 Direction dir) const
214 {
215 size_t index(0);
216 wxSimpleDataObjectList::compatibility_iterator node;
217
218 for ( node = m_dataObjects.GetFirst(); node; node = node->GetNext() )
219 {
220 // NOTE: some wxDataObjectSimple objects may return more than 1 format
221 // from GetAllFormats(): this is the case of e.g. wxTextDataObject
222 // under wxMac and wxGTK
223 node->GetData()->GetAllFormats(formats+index, dir);
224 index += node->GetData()->GetFormatCount(dir);
225 }
226 }
227
228 size_t wxDataObjectComposite::GetDataSize(const wxDataFormat& format) const
229 {
230 wxDataObjectSimple *dataObj = GetObject(format);
231
232 wxCHECK_MSG( dataObj, 0,
233 wxT("unsupported format in wxDataObjectComposite"));
234
235 return dataObj->GetDataSize();
236 }
237
238 bool wxDataObjectComposite::GetDataHere(const wxDataFormat& format,
239 void *buf) const
240 {
241 wxDataObjectSimple *dataObj = GetObject( format );
242
243 wxCHECK_MSG( dataObj, false,
244 wxT("unsupported format in wxDataObjectComposite"));
245
246 return dataObj->GetDataHere( buf );
247 }
248
249 bool wxDataObjectComposite::SetData(const wxDataFormat& format,
250 size_t len,
251 const void *buf)
252 {
253 wxDataObjectSimple *dataObj = GetObject( format );
254
255 wxCHECK_MSG( dataObj, false,
256 wxT("unsupported format in wxDataObjectComposite"));
257
258 m_receivedFormat = format;
259 return dataObj->SetData( len, buf );
260 }
261
262 // ----------------------------------------------------------------------------
263 // wxTextDataObject
264 // ----------------------------------------------------------------------------
265
266 #ifdef wxNEEDS_UTF8_FOR_TEXT_DATAOBJ
267
268 // FIXME-UTF8: we should be able to merge wchar_t and UTF-8 versions once we
269 // have a way to get UTF-8 string (and its length) in both builds
270 // without loss of efficiency (i.e. extra buffer copy/strlen call)
271
272 #if wxUSE_UNICODE_WCHAR
273
274 static inline wxMBConv& GetConv(const wxDataFormat& format)
275 {
276 // use UTF8 for wxDF_UNICODETEXT and UCS4 for wxDF_TEXT
277 return format == wxDF_UNICODETEXT ? wxConvUTF8 : wxConvLibc;
278 }
279
280 size_t wxTextDataObject::GetDataSize(const wxDataFormat& format) const
281 {
282 wxCharBuffer buffer = GetConv(format).cWX2MB( GetText().c_str() );
283
284 return buffer ? strlen( buffer ) : 0;
285 }
286
287 bool wxTextDataObject::GetDataHere(const wxDataFormat& format, void *buf) const
288 {
289 if ( !buf )
290 return false;
291
292 wxCharBuffer buffer = GetConv(format).cWX2MB( GetText().c_str() );
293 if ( !buffer )
294 return false;
295
296 memcpy( (char*) buf, buffer, GetDataSize(format) );
297 // strcpy( (char*) buf, buffer );
298
299 return true;
300 }
301
302 bool wxTextDataObject::SetData(const wxDataFormat& format,
303 size_t WXUNUSED(len), const void *buf)
304 {
305 if ( buf == NULL )
306 return false;
307
308 wxWCharBuffer buffer = GetConv(format).cMB2WX( (const char*)buf );
309
310 SetText( buffer );
311
312 return true;
313 }
314
315 #else // wxUSE_UNICODE_UTF8
316
317 size_t wxTextDataObject::GetDataSize(const wxDataFormat& format) const
318 {
319 if ( format == wxDF_UNICODETEXT || wxLocaleIsUtf8 )
320 {
321 return m_text.utf8_length();
322 }
323 else // wxDF_TEXT
324 {
325 const wxCharBuffer buf(wxConvLocal.cWC2MB(m_text.wc_str()));
326 return buf ? strlen(buf) : 0;
327 }
328 }
329
330 bool wxTextDataObject::GetDataHere(const wxDataFormat& format, void *buf) const
331 {
332 if ( !buf )
333 return false;
334
335 if ( format == wxDF_UNICODETEXT || wxLocaleIsUtf8 )
336 {
337 memcpy(buf, m_text.utf8_str(), m_text.utf8_length());
338 }
339 else // wxDF_TEXT
340 {
341 const wxCharBuffer bufLocal(wxConvLocal.cWC2MB(m_text.wc_str()));
342 if ( !bufLocal )
343 return false;
344
345 memcpy(buf, bufLocal, strlen(bufLocal));
346 }
347
348 return true;
349 }
350
351 bool wxTextDataObject::SetData(const wxDataFormat& format,
352 size_t len, const void *buf_)
353 {
354 const char * const buf = static_cast<const char *>(buf_);
355
356 if ( buf == NULL )
357 return false;
358
359 if ( format == wxDF_UNICODETEXT || wxLocaleIsUtf8 )
360 {
361 // normally the data is in UTF-8 so we could use FromUTF8Unchecked()
362 // but it's not absolutely clear what GTK+ does if the clipboard data
363 // is not in UTF-8 so do an extra check for tranquility, it shouldn't
364 // matter much if we lose a bit of performance when pasting from
365 // clipboard
366 m_text = wxString::FromUTF8(buf, len);
367 }
368 else // wxDF_TEXT, convert from current (non-UTF8) locale
369 {
370 m_text = wxConvLocal.cMB2WC(buf, len, NULL);
371 }
372
373 return true;
374 }
375
376 #endif // wxUSE_UNICODE_WCHAR/wxUSE_UNICODE_UTF8
377
378 #elif defined(wxNEEDS_UTF16_FOR_TEXT_DATAOBJ)
379
380 static wxMBConvUTF16 sUTF16Converter;
381
382 static inline wxMBConv& GetConv(const wxDataFormat& format)
383 {
384 return
385 format == wxDF_UNICODETEXT
386 ? (wxMBConv&) sUTF16Converter
387 : (wxMBConv&) wxConvLocal;
388 }
389
390 size_t wxTextDataObject::GetDataSize(const wxDataFormat& format) const
391 {
392 size_t len = GetConv(format).WC2MB( NULL, GetText().c_str(), 0 );
393 len += (format == wxDF_UNICODETEXT ? 2 : 1);
394
395 return len;
396 }
397
398 bool wxTextDataObject::GetDataHere(const wxDataFormat& format, void *buf) const
399 {
400 if ( buf == NULL )
401 return false;
402
403 wxCharBuffer buffer = GetConv(format).cWX2MB( GetText().c_str() );
404
405 size_t len = GetConv(format).WC2MB( NULL, GetText().c_str(), 0 );
406 len += (format == wxDF_UNICODETEXT ? 2 : 1);
407
408 // trailing (uni)char 0
409 memcpy( (char*)buf, (const char*)buffer, len );
410
411 return true;
412 }
413
414 bool wxTextDataObject::SetData(const wxDataFormat& format,
415 size_t WXUNUSED(len), const void *buf)
416 {
417 if ( buf == NULL )
418 return false;
419
420 wxWCharBuffer buffer = GetConv(format).cMB2WX( (const char*)buf );
421
422 SetText( buffer );
423
424 return true;
425 }
426
427 #else // !wxNEEDS_UTF{8,16}_FOR_TEXT_DATAOBJ
428
429 size_t wxTextDataObject::GetDataSize() const
430 {
431 return GetTextLength() * sizeof(wxChar);
432 }
433
434 bool wxTextDataObject::GetDataHere(void *buf) const
435 {
436 // NOTE: use wxTmemcpy() instead of wxStrncpy() to allow
437 // retrieval of strings with embedded NULLs
438 wxTmemcpy( (wxChar*)buf, GetText().c_str(), GetTextLength() );
439
440 return true;
441 }
442
443 bool wxTextDataObject::SetData(size_t len, const void *buf)
444 {
445 SetText( wxString((const wxChar*)buf, len/sizeof(wxChar)) );
446
447 return true;
448 }
449
450 #endif // different wxTextDataObject implementations
451
452 // ----------------------------------------------------------------------------
453 // wxCustomDataObject
454 // ----------------------------------------------------------------------------
455
456 wxCustomDataObject::wxCustomDataObject(const wxDataFormat& format)
457 : wxDataObjectSimple(format)
458 {
459 m_data = NULL;
460 m_size = 0;
461 }
462
463 wxCustomDataObject::~wxCustomDataObject()
464 {
465 Free();
466 }
467
468 void wxCustomDataObject::TakeData(size_t size, void *data)
469 {
470 Free();
471
472 m_size = size;
473 m_data = data;
474 }
475
476 void *wxCustomDataObject::Alloc(size_t size)
477 {
478 return (void *)new char[size];
479 }
480
481 void wxCustomDataObject::Free()
482 {
483 delete [] (char*)m_data;
484 m_size = 0;
485 m_data = NULL;
486 }
487
488 size_t wxCustomDataObject::GetDataSize() const
489 {
490 return GetSize();
491 }
492
493 bool wxCustomDataObject::GetDataHere(void *buf) const
494 {
495 if ( buf == NULL )
496 return false;
497
498 void *data = GetData();
499 if ( data == NULL )
500 return false;
501
502 memcpy( buf, data, GetSize() );
503
504 return true;
505 }
506
507 bool wxCustomDataObject::SetData(size_t size, const void *buf)
508 {
509 Free();
510
511 m_data = Alloc(size);
512 if ( m_data == NULL )
513 return false;
514
515 m_size = size;
516 memcpy( m_data, buf, m_size );
517
518 return true;
519 }
520
521 // ============================================================================
522 // some common dnd related code
523 // ============================================================================
524
525 #if wxUSE_DRAG_AND_DROP
526
527 #include "wx/dnd.h"
528
529 // ----------------------------------------------------------------------------
530 // wxTextDropTarget
531 // ----------------------------------------------------------------------------
532
533 // NB: we can't use "new" in ctor initializer lists because this provokes an
534 // internal compiler error with VC++ 5.0 (hey, even gcc compiles this!),
535 // so use SetDataObject() instead
536
537 wxTextDropTarget::wxTextDropTarget()
538 {
539 SetDataObject(new wxTextDataObject);
540 }
541
542 wxDragResult wxTextDropTarget::OnData(wxCoord x, wxCoord y, wxDragResult def)
543 {
544 if ( !GetData() )
545 return wxDragNone;
546
547 wxTextDataObject *dobj = (wxTextDataObject *)m_dataObject;
548 return OnDropText( x, y, dobj->GetText() ) ? def : wxDragNone;
549 }
550
551 // ----------------------------------------------------------------------------
552 // wxFileDropTarget
553 // ----------------------------------------------------------------------------
554
555 wxFileDropTarget::wxFileDropTarget()
556 {
557 SetDataObject(new wxFileDataObject);
558 }
559
560 wxDragResult wxFileDropTarget::OnData(wxCoord x, wxCoord y, wxDragResult def)
561 {
562 if ( !GetData() )
563 return wxDragNone;
564
565 wxFileDataObject *dobj = (wxFileDataObject *)m_dataObject;
566 return OnDropFiles( x, y, dobj->GetFilenames() ) ? def : wxDragNone;
567 }
568
569 #endif // wxUSE_DRAG_AND_DROP
570
571 #endif // wxUSE_DATAOBJ