]> git.saurik.com Git - wxWidgets.git/blame - src/common/dobjcmn.cpp
special casing mac code
[wxWidgets.git] / src / common / dobjcmn.cpp
CommitLineData
3f364be8 1///////////////////////////////////////////////////////////////////////////////
8b445796 2// Name: src/common/dobjcmn.cpp
3f364be8
VZ
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$
77ffb593 8// Copyright: (c) wxWidgets Team
65571936 9// Licence: wxWindows licence
3f364be8
VZ
10///////////////////////////////////////////////////////////////////////////////
11
8b445796 12// For compilers that support precompilation, includes "wx.h".
3f364be8
VZ
13#include "wx/wxprec.h"
14
15#ifdef __BORLANDC__
16 #pragma hdrstop
17#endif
18
1e6feb95
VZ
19#if wxUSE_DATAOBJ
20
e7c80f9e
WS
21#include "wx/dataobj.h"
22
3f364be8
VZ
23#ifndef WX_PRECOMP
24 #include "wx/app.h"
8b445796 25#endif
3f364be8 26
3f364be8
VZ
27// ----------------------------------------------------------------------------
28// lists
29// ----------------------------------------------------------------------------
30
31#include "wx/listimpl.cpp"
32
259c43f6 33WX_DEFINE_LIST(wxSimpleDataObjectList)
3f364be8 34
0c2b453f
VZ
35// ----------------------------------------------------------------------------
36// globals
37// ----------------------------------------------------------------------------
38
39static wxDataFormat dataFormatInvalid;
31e78e0c 40WXDLLEXPORT const wxDataFormat& wxFormatInvalid = dataFormatInvalid;
0c2b453f 41
3f364be8
VZ
42// ============================================================================
43// implementation
44// ============================================================================
45
46// ----------------------------------------------------------------------------
47// wxDataObjectBase
48// ----------------------------------------------------------------------------
49
50wxDataObjectBase::~wxDataObjectBase()
51{
52}
53
d9317fd4
VZ
54bool wxDataObjectBase::IsSupported(const wxDataFormat& format,
55 Direction dir) const
56{
8b445796 57 size_t nFormatCount = GetFormatCount( dir );
d9317fd4
VZ
58 if ( nFormatCount == 1 )
59 {
8b445796 60 return format == GetPreferredFormat( dir );
d9317fd4
VZ
61 }
62 else
63 {
64 wxDataFormat *formats = new wxDataFormat[nFormatCount];
8b445796 65 GetAllFormats( formats, dir );
d9317fd4
VZ
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
3f364be8
VZ
81// ----------------------------------------------------------------------------
82// wxDataObjectComposite
83// ----------------------------------------------------------------------------
84
e6b01b78
VZ
85wxDataObjectComposite::wxDataObjectComposite()
86{
87 m_preferred = 0;
c072c757 88 m_receivedFormat = wxFormatInvalid;
222ed1d6 89}
e6b01b78 90
222ed1d6
MB
91wxDataObjectComposite::~wxDataObjectComposite()
92{
8b445796 93 WX_CLEAR_LIST( wxSimpleDataObjectList, m_dataObjects );
e6b01b78
VZ
94}
95
3f364be8 96wxDataObjectSimple *
dc892843 97wxDataObjectComposite::GetObject(const wxDataFormat& format, wxDataObjectBase::Direction dir) const
3f364be8 98{
222ed1d6 99 wxSimpleDataObjectList::compatibility_iterator node = m_dataObjects.GetFirst();
dc892843 100
3f364be8
VZ
101 while ( node )
102 {
103 wxDataObjectSimple *dataObj = node->GetData();
104
dc892843
RR
105 if (dataObj->IsSupported(format,dir))
106 return dataObj;
3f364be8
VZ
107 node = node->GetNext();
108 }
d3b9f782 109 return NULL;
3f364be8
VZ
110}
111
112void wxDataObjectComposite::Add(wxDataObjectSimple *dataObject, bool preferred)
113{
dc892843
RR
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;
9113ffaf 120
dc892843
RR
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);
9113ffaf 129
dc892843
RR
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:
3f364be8
VZ
137 if ( preferred )
138 m_preferred = m_dataObjects.GetCount();
3f364be8
VZ
139 m_dataObjects.Append( dataObject );
140}
141
c072c757
RD
142wxDataFormat wxDataObjectComposite::GetReceivedFormat() const
143{
144 return m_receivedFormat;
145}
146
3f364be8
VZ
147wxDataFormat
148wxDataObjectComposite::GetPreferredFormat(Direction WXUNUSED(dir)) const
149{
222ed1d6 150 wxSimpleDataObjectList::compatibility_iterator node = m_dataObjects.Item( m_preferred );
3f364be8 151
2ee3ee1b 152 wxCHECK_MSG( node, wxFormatInvalid, wxT("no preferred format") );
3f364be8
VZ
153
154 wxDataObjectSimple* dataObj = node->GetData();
155
156 return dataObj->GetFormat();
157}
158
e1b435af 159#if defined(__WXMSW__)
7d584866 160
e1b435af
MB
161size_t wxDataObjectComposite::GetBufferOffset( const wxDataFormat& format )
162{
163 wxDataObjectSimple *dataObj = GetObject(format);
164
f7f50f49 165 wxCHECK_MSG( dataObj, 0,
e1b435af
MB
166 wxT("unsupported format in wxDataObjectComposite"));
167
168 return dataObj->GetBufferOffset( format );
169}
170
7d584866 171
e1b435af
MB
172const void* wxDataObjectComposite::GetSizeFromBuffer( const void* buffer,
173 size_t* size,
174 const wxDataFormat& format )
175{
176 wxDataObjectSimple *dataObj = GetObject(format);
177
f7f50f49 178 wxCHECK_MSG( dataObj, NULL,
e1b435af
MB
179 wxT("unsupported format in wxDataObjectComposite"));
180
181 return dataObj->GetSizeFromBuffer( buffer, size, format );
182}
183
7d584866 184
e1b435af
MB
185void* wxDataObjectComposite::SetSizeInBuffer( void* buffer, size_t size,
186 const wxDataFormat& format )
187{
8b445796 188 wxDataObjectSimple *dataObj = GetObject( format );
e1b435af 189
f7f50f49 190 wxCHECK_MSG( dataObj, NULL,
e1b435af
MB
191 wxT("unsupported format in wxDataObjectComposite"));
192
193 return dataObj->SetSizeInBuffer( buffer, size, format );
194}
195
196#endif
197
9113ffaf 198size_t wxDataObjectComposite::GetFormatCount(Direction dir) const
3f364be8 199{
9113ffaf
FM
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;
3f364be8
VZ
210}
211
212void wxDataObjectComposite::GetAllFormats(wxDataFormat *formats,
0dd48109 213 Direction dir) const
3f364be8 214{
0dd48109 215 size_t index(0);
222ed1d6 216 wxSimpleDataObjectList::compatibility_iterator node;
0dd48109 217
3f364be8
VZ
218 for ( node = m_dataObjects.GetFirst(); node; node = node->GetNext() )
219 {
9113ffaf
FM
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);
3f364be8
VZ
225 }
226}
227
228size_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
238bool wxDataObjectComposite::GetDataHere(const wxDataFormat& format,
239 void *buf) const
240{
8b445796 241 wxDataObjectSimple *dataObj = GetObject( format );
3f364be8 242
68379eaf 243 wxCHECK_MSG( dataObj, false,
3f364be8
VZ
244 wxT("unsupported format in wxDataObjectComposite"));
245
8b445796 246 return dataObj->GetDataHere( buf );
3f364be8
VZ
247}
248
249bool wxDataObjectComposite::SetData(const wxDataFormat& format,
250 size_t len,
251 const void *buf)
252{
8b445796 253 wxDataObjectSimple *dataObj = GetObject( format );
3f364be8 254
68379eaf 255 wxCHECK_MSG( dataObj, false,
3f364be8
VZ
256 wxT("unsupported format in wxDataObjectComposite"));
257
c072c757 258 m_receivedFormat = format;
8b445796 259 return dataObj->SetData( len, buf );
3f364be8
VZ
260}
261
262// ----------------------------------------------------------------------------
263// wxTextDataObject
264// ----------------------------------------------------------------------------
265
98ecad06
VZ
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
c7d6d883 273
f4370741
VZ
274static 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
c7d6d883
RR
280size_t wxTextDataObject::GetDataSize(const wxDataFormat& format) const
281{
f4370741 282 wxCharBuffer buffer = GetConv(format).cWX2MB( GetText().c_str() );
8b445796 283
2c906a49 284 return buffer ? strlen( buffer ) : 0;
c7d6d883
RR
285}
286
287bool wxTextDataObject::GetDataHere(const wxDataFormat& format, void *buf) const
288{
f7570f57 289 if ( !buf )
8b445796
DS
290 return false;
291
f4370741 292 wxCharBuffer buffer = GetConv(format).cWX2MB( GetText().c_str() );
f7570f57
RR
293 if ( !buffer )
294 return false;
f4370741 295
f7570f57
RR
296 memcpy( (char*) buf, buffer, GetDataSize(format) );
297 // strcpy( (char*) buf, buffer );
68379eaf
WS
298
299 return true;
c7d6d883
RR
300}
301
302bool wxTextDataObject::SetData(const wxDataFormat& format,
303 size_t WXUNUSED(len), const void *buf)
304{
8b445796 305 if ( buf == NULL )
f4370741
VZ
306 return false;
307
8b445796 308 wxWCharBuffer buffer = GetConv(format).cMB2WX( (const char*)buf );
8b445796
DS
309
310 SetText( buffer );
68379eaf
WS
311
312 return true;
c7d6d883
RR
313}
314
98ecad06
VZ
315#else // wxUSE_UNICODE_UTF8
316
317size_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
330bool 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
351bool wxTextDataObject::SetData(const wxDataFormat& format,
352 size_t len, const void *buf_)
353{
5c33522f 354 const char * const buf = static_cast<const char *>(buf_);
98ecad06
VZ
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)
25758297 379
8b445796 380static wxMBConvUTF16 sUTF16Converter;
671478ee
SC
381
382static inline wxMBConv& GetConv(const wxDataFormat& format)
383{
8b445796
DS
384 return
385 format == wxDF_UNICODETEXT
386 ? (wxMBConv&) sUTF16Converter
387 : (wxMBConv&) wxConvLocal;
671478ee
SC
388}
389
25758297
SC
390size_t wxTextDataObject::GetDataSize(const wxDataFormat& format) const
391{
8b445796
DS
392 size_t len = GetConv(format).WC2MB( NULL, GetText().c_str(), 0 );
393 len += (format == wxDF_UNICODETEXT ? 2 : 1);
394
395 return len;
25758297
SC
396}
397
398bool wxTextDataObject::GetDataHere(const wxDataFormat& format, void *buf) const
399{
8b445796 400 if ( buf == NULL )
671478ee
SC
401 return false;
402
8b445796 403 wxCharBuffer buffer = GetConv(format).cWX2MB( GetText().c_str() );
671478ee 404
8b445796
DS
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 );
68379eaf
WS
410
411 return true;
25758297
SC
412}
413
414bool wxTextDataObject::SetData(const wxDataFormat& format,
415 size_t WXUNUSED(len), const void *buf)
416{
8b445796 417 if ( buf == NULL )
671478ee 418 return false;
8b445796
DS
419
420 wxWCharBuffer buffer = GetConv(format).cMB2WX( (const char*)buf );
421
422 SetText( buffer );
423
68379eaf 424 return true;
25758297
SC
425}
426
98ecad06 427#else // !wxNEEDS_UTF{8,16}_FOR_TEXT_DATAOBJ
c7d6d883
RR
428
429size_t wxTextDataObject::GetDataSize() const
430{
e1b435af 431 return GetTextLength() * sizeof(wxChar);
3f364be8
VZ
432}
433
434bool wxTextDataObject::GetDataHere(void *buf) const
435{
f453d7ea
FM
436 // NOTE: use wxTmemcpy() instead of wxStrncpy() to allow
437 // retrieval of strings with embedded NULLs
438 wxTmemcpy( (wxChar*)buf, GetText().c_str(), GetTextLength() );
3f364be8 439
68379eaf 440 return true;
3f364be8
VZ
441}
442
f453d7ea 443bool wxTextDataObject::SetData(size_t len, const void *buf)
3f364be8 444{
f453d7ea 445 SetText( wxString((const wxChar*)buf, len/sizeof(wxChar)) );
3f364be8 446
68379eaf 447 return true;
3f364be8
VZ
448}
449
98ecad06 450#endif // different wxTextDataObject implementations
9e2896e5 451
3f364be8
VZ
452// ----------------------------------------------------------------------------
453// wxCustomDataObject
454// ----------------------------------------------------------------------------
455
775e1c62
RD
456wxCustomDataObject::wxCustomDataObject(const wxDataFormat& format)
457 : wxDataObjectSimple(format)
458{
8b445796
DS
459 m_data = NULL;
460 m_size = 0;
775e1c62
RD
461}
462
3f364be8
VZ
463wxCustomDataObject::~wxCustomDataObject()
464{
465 Free();
466}
467
468void wxCustomDataObject::TakeData(size_t size, void *data)
469{
470 Free();
471
472 m_size = size;
473 m_data = data;
474}
475
476void *wxCustomDataObject::Alloc(size_t size)
477{
478 return (void *)new char[size];
479}
480
481void wxCustomDataObject::Free()
482{
8b445796 483 delete [] (char*)m_data;
3f364be8 484 m_size = 0;
d3b9f782 485 m_data = NULL;
3f364be8
VZ
486}
487
488size_t wxCustomDataObject::GetDataSize() const
489{
490 return GetSize();
491}
492
493bool wxCustomDataObject::GetDataHere(void *buf) const
494{
8b445796
DS
495 if ( buf == NULL )
496 return false;
497
3f364be8 498 void *data = GetData();
8b445796 499 if ( data == NULL )
68379eaf 500 return false;
3f364be8 501
8b445796 502 memcpy( buf, data, GetSize() );
3f364be8 503
68379eaf 504 return true;
3f364be8
VZ
505}
506
9e2896e5 507bool wxCustomDataObject::SetData(size_t size, const void *buf)
3f364be8
VZ
508{
509 Free();
510
511 m_data = Alloc(size);
8b445796 512 if ( m_data == NULL )
68379eaf 513 return false;
3f364be8 514
8b445796
DS
515 m_size = size;
516 memcpy( m_data, buf, m_size );
3f364be8 517
68379eaf 518 return true;
3f364be8
VZ
519}
520
8ee9d618
VZ
521// ============================================================================
522// some common dnd related code
523// ============================================================================
524
8fb3a512
JS
525#if wxUSE_DRAG_AND_DROP
526
8ee9d618
VZ
527#include "wx/dnd.h"
528
529// ----------------------------------------------------------------------------
530// wxTextDropTarget
531// ----------------------------------------------------------------------------
532
f3ac12aa
VZ
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
8ee9d618 537wxTextDropTarget::wxTextDropTarget()
8ee9d618 538{
f3ac12aa 539 SetDataObject(new wxTextDataObject);
8ee9d618
VZ
540}
541
542wxDragResult wxTextDropTarget::OnData(wxCoord x, wxCoord y, wxDragResult def)
543{
544 if ( !GetData() )
545 return wxDragNone;
546
547 wxTextDataObject *dobj = (wxTextDataObject *)m_dataObject;
8b445796 548 return OnDropText( x, y, dobj->GetText() ) ? def : wxDragNone;
8ee9d618
VZ
549}
550
551// ----------------------------------------------------------------------------
552// wxFileDropTarget
553// ----------------------------------------------------------------------------
554
555wxFileDropTarget::wxFileDropTarget()
8ee9d618 556{
f3ac12aa 557 SetDataObject(new wxFileDataObject);
8ee9d618
VZ
558}
559
560wxDragResult wxFileDropTarget::OnData(wxCoord x, wxCoord y, wxDragResult def)
561{
562 if ( !GetData() )
563 return wxDragNone;
564
565 wxFileDataObject *dobj = (wxFileDataObject *)m_dataObject;
8b445796 566 return OnDropFiles( x, y, dobj->GetFilenames() ) ? def : wxDragNone;
8ee9d618
VZ
567}
568
1e6feb95 569#endif // wxUSE_DRAG_AND_DROP
8fb3a512 570
1e6feb95 571#endif // wxUSE_DATAOBJ