Fixes #10437 (wxDataObjectComposite::Add may add objects having data formats that...
[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 WXUNUSED(dir)) const
199 {
200 // TODO what about the Get/Set only formats?
201 return m_dataObjects.GetCount();
202 }
203
204 void wxDataObjectComposite::GetAllFormats(wxDataFormat *formats,
205 Direction dir) const
206 {
207 size_t index(0);
208 wxSimpleDataObjectList::compatibility_iterator node;
209
210 for ( node = m_dataObjects.GetFirst(); node; node = node->GetNext() )
211 {
212 node->GetData()->GetAllFormats(formats+index,dir);
213 index += node->GetData()->GetFormatCount(dir);
214 }
215 }
216
217 size_t wxDataObjectComposite::GetDataSize(const wxDataFormat& format) const
218 {
219 wxDataObjectSimple *dataObj = GetObject(format);
220
221 wxCHECK_MSG( dataObj, 0,
222 wxT("unsupported format in wxDataObjectComposite"));
223
224 return dataObj->GetDataSize();
225 }
226
227 bool wxDataObjectComposite::GetDataHere(const wxDataFormat& format,
228 void *buf) const
229 {
230 wxDataObjectSimple *dataObj = GetObject( format );
231
232 wxCHECK_MSG( dataObj, false,
233 wxT("unsupported format in wxDataObjectComposite"));
234
235 return dataObj->GetDataHere( buf );
236 }
237
238 bool wxDataObjectComposite::SetData(const wxDataFormat& format,
239 size_t len,
240 const void *buf)
241 {
242 wxDataObjectSimple *dataObj = GetObject( format );
243
244 wxCHECK_MSG( dataObj, false,
245 wxT("unsupported format in wxDataObjectComposite"));
246
247 m_receivedFormat = format;
248 return dataObj->SetData( len, buf );
249 }
250
251 // ----------------------------------------------------------------------------
252 // wxTextDataObject
253 // ----------------------------------------------------------------------------
254
255 #ifdef wxNEEDS_UTF8_FOR_TEXT_DATAOBJ
256
257 // FIXME-UTF8: we should be able to merge wchar_t and UTF-8 versions once we
258 // have a way to get UTF-8 string (and its length) in both builds
259 // without loss of efficiency (i.e. extra buffer copy/strlen call)
260
261 #if wxUSE_UNICODE_WCHAR
262
263 static inline wxMBConv& GetConv(const wxDataFormat& format)
264 {
265 // use UTF8 for wxDF_UNICODETEXT and UCS4 for wxDF_TEXT
266 return format == wxDF_UNICODETEXT ? wxConvUTF8 : wxConvLibc;
267 }
268
269 size_t wxTextDataObject::GetDataSize(const wxDataFormat& format) const
270 {
271 wxCharBuffer buffer = GetConv(format).cWX2MB( GetText().c_str() );
272
273 return buffer ? strlen( buffer ) : 0;
274 }
275
276 bool wxTextDataObject::GetDataHere(const wxDataFormat& format, void *buf) const
277 {
278 if ( !buf )
279 return false;
280
281 wxCharBuffer buffer = GetConv(format).cWX2MB( GetText().c_str() );
282 if ( !buffer )
283 return false;
284
285 memcpy( (char*) buf, buffer, GetDataSize(format) );
286 // strcpy( (char*) buf, buffer );
287
288 return true;
289 }
290
291 bool wxTextDataObject::SetData(const wxDataFormat& format,
292 size_t WXUNUSED(len), const void *buf)
293 {
294 if ( buf == NULL )
295 return false;
296
297 wxWCharBuffer buffer = GetConv(format).cMB2WX( (const char*)buf );
298
299 SetText( buffer );
300
301 return true;
302 }
303
304 #else // wxUSE_UNICODE_UTF8
305
306 size_t wxTextDataObject::GetDataSize(const wxDataFormat& format) const
307 {
308 if ( format == wxDF_UNICODETEXT || wxLocaleIsUtf8 )
309 {
310 return m_text.utf8_length();
311 }
312 else // wxDF_TEXT
313 {
314 const wxCharBuffer buf(wxConvLocal.cWC2MB(m_text.wc_str()));
315 return buf ? strlen(buf) : 0;
316 }
317 }
318
319 bool wxTextDataObject::GetDataHere(const wxDataFormat& format, void *buf) const
320 {
321 if ( !buf )
322 return false;
323
324 if ( format == wxDF_UNICODETEXT || wxLocaleIsUtf8 )
325 {
326 memcpy(buf, m_text.utf8_str(), m_text.utf8_length());
327 }
328 else // wxDF_TEXT
329 {
330 const wxCharBuffer bufLocal(wxConvLocal.cWC2MB(m_text.wc_str()));
331 if ( !bufLocal )
332 return false;
333
334 memcpy(buf, bufLocal, strlen(bufLocal));
335 }
336
337 return true;
338 }
339
340 bool wxTextDataObject::SetData(const wxDataFormat& format,
341 size_t len, const void *buf_)
342 {
343 const char * const buf = static_cast<const char *>(buf_);
344
345 if ( buf == NULL )
346 return false;
347
348 if ( format == wxDF_UNICODETEXT || wxLocaleIsUtf8 )
349 {
350 // normally the data is in UTF-8 so we could use FromUTF8Unchecked()
351 // but it's not absolutely clear what GTK+ does if the clipboard data
352 // is not in UTF-8 so do an extra check for tranquility, it shouldn't
353 // matter much if we lose a bit of performance when pasting from
354 // clipboard
355 m_text = wxString::FromUTF8(buf, len);
356 }
357 else // wxDF_TEXT, convert from current (non-UTF8) locale
358 {
359 m_text = wxConvLocal.cMB2WC(buf, len, NULL);
360 }
361
362 return true;
363 }
364
365 #endif // wxUSE_UNICODE_WCHAR/wxUSE_UNICODE_UTF8
366
367 #elif defined(wxNEEDS_UTF16_FOR_TEXT_DATAOBJ)
368
369 static wxMBConvUTF16 sUTF16Converter;
370
371 static inline wxMBConv& GetConv(const wxDataFormat& format)
372 {
373 return
374 format == wxDF_UNICODETEXT
375 ? (wxMBConv&) sUTF16Converter
376 : (wxMBConv&) wxConvLocal;
377 }
378
379 size_t wxTextDataObject::GetDataSize(const wxDataFormat& format) const
380 {
381 size_t len = GetConv(format).WC2MB( NULL, GetText().c_str(), 0 );
382 len += (format == wxDF_UNICODETEXT ? 2 : 1);
383
384 return len;
385 }
386
387 bool wxTextDataObject::GetDataHere(const wxDataFormat& format, void *buf) const
388 {
389 if ( buf == NULL )
390 return false;
391
392 wxCharBuffer buffer = GetConv(format).cWX2MB( GetText().c_str() );
393
394 size_t len = GetConv(format).WC2MB( NULL, GetText().c_str(), 0 );
395 len += (format == wxDF_UNICODETEXT ? 2 : 1);
396
397 // trailing (uni)char 0
398 memcpy( (char*)buf, (const char*)buffer, len );
399
400 return true;
401 }
402
403 bool wxTextDataObject::SetData(const wxDataFormat& format,
404 size_t WXUNUSED(len), const void *buf)
405 {
406 if ( buf == NULL )
407 return false;
408
409 wxWCharBuffer buffer = GetConv(format).cMB2WX( (const char*)buf );
410
411 SetText( buffer );
412
413 return true;
414 }
415
416 #else // !wxNEEDS_UTF{8,16}_FOR_TEXT_DATAOBJ
417
418 size_t wxTextDataObject::GetDataSize() const
419 {
420 return GetTextLength() * sizeof(wxChar);
421 }
422
423 bool wxTextDataObject::GetDataHere(void *buf) const
424 {
425 wxStrcpy( (wxChar*)buf, GetText().c_str() );
426
427 return true;
428 }
429
430 bool wxTextDataObject::SetData(size_t WXUNUSED(len), const void *buf)
431 {
432 SetText( wxString((const wxChar*)buf) );
433
434 return true;
435 }
436
437 #endif // different wxTextDataObject implementations
438
439 // ----------------------------------------------------------------------------
440 // wxCustomDataObject
441 // ----------------------------------------------------------------------------
442
443 wxCustomDataObject::wxCustomDataObject(const wxDataFormat& format)
444 : wxDataObjectSimple(format)
445 {
446 m_data = NULL;
447 m_size = 0;
448 }
449
450 wxCustomDataObject::~wxCustomDataObject()
451 {
452 Free();
453 }
454
455 void wxCustomDataObject::TakeData(size_t size, void *data)
456 {
457 Free();
458
459 m_size = size;
460 m_data = data;
461 }
462
463 void *wxCustomDataObject::Alloc(size_t size)
464 {
465 return (void *)new char[size];
466 }
467
468 void wxCustomDataObject::Free()
469 {
470 delete [] (char*)m_data;
471 m_size = 0;
472 m_data = NULL;
473 }
474
475 size_t wxCustomDataObject::GetDataSize() const
476 {
477 return GetSize();
478 }
479
480 bool wxCustomDataObject::GetDataHere(void *buf) const
481 {
482 if ( buf == NULL )
483 return false;
484
485 void *data = GetData();
486 if ( data == NULL )
487 return false;
488
489 memcpy( buf, data, GetSize() );
490
491 return true;
492 }
493
494 bool wxCustomDataObject::SetData(size_t size, const void *buf)
495 {
496 Free();
497
498 m_data = Alloc(size);
499 if ( m_data == NULL )
500 return false;
501
502 m_size = size;
503 memcpy( m_data, buf, m_size );
504
505 return true;
506 }
507
508 // ============================================================================
509 // some common dnd related code
510 // ============================================================================
511
512 #if wxUSE_DRAG_AND_DROP
513
514 #include "wx/dnd.h"
515
516 // ----------------------------------------------------------------------------
517 // wxTextDropTarget
518 // ----------------------------------------------------------------------------
519
520 // NB: we can't use "new" in ctor initializer lists because this provokes an
521 // internal compiler error with VC++ 5.0 (hey, even gcc compiles this!),
522 // so use SetDataObject() instead
523
524 wxTextDropTarget::wxTextDropTarget()
525 {
526 SetDataObject(new wxTextDataObject);
527 }
528
529 wxDragResult wxTextDropTarget::OnData(wxCoord x, wxCoord y, wxDragResult def)
530 {
531 if ( !GetData() )
532 return wxDragNone;
533
534 wxTextDataObject *dobj = (wxTextDataObject *)m_dataObject;
535 return OnDropText( x, y, dobj->GetText() ) ? def : wxDragNone;
536 }
537
538 // ----------------------------------------------------------------------------
539 // wxFileDropTarget
540 // ----------------------------------------------------------------------------
541
542 wxFileDropTarget::wxFileDropTarget()
543 {
544 SetDataObject(new wxFileDataObject);
545 }
546
547 wxDragResult wxFileDropTarget::OnData(wxCoord x, wxCoord y, wxDragResult def)
548 {
549 if ( !GetData() )
550 return wxDragNone;
551
552 wxFileDataObject *dobj = (wxFileDataObject *)m_dataObject;
553 return OnDropFiles( x, y, dobj->GetFilenames() ) ? def : wxDragNone;
554 }
555
556 #endif // wxUSE_DRAG_AND_DROP
557
558 #endif // wxUSE_DATAOBJ