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