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