]> git.saurik.com Git - wxWidgets.git/blame - src/common/dobjcmn.cpp
fixed wxXmlResource::Load's detection of filenames to be done as early as possible
[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;
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:
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
3f364be8
VZ
198size_t wxDataObjectComposite::GetFormatCount(Direction WXUNUSED(dir)) const
199{
200 // TODO what about the Get/Set only formats?
201 return m_dataObjects.GetCount();
202}
203
204void wxDataObjectComposite::GetAllFormats(wxDataFormat *formats,
0dd48109 205 Direction dir) const
3f364be8 206{
0dd48109 207 size_t index(0);
222ed1d6 208 wxSimpleDataObjectList::compatibility_iterator node;
0dd48109 209
3f364be8
VZ
210 for ( node = m_dataObjects.GetFirst(); node; node = node->GetNext() )
211 {
0dd48109
RR
212 node->GetData()->GetAllFormats(formats+index,dir);
213 index += node->GetData()->GetFormatCount(dir);
3f364be8
VZ
214 }
215}
216
217size_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
227bool wxDataObjectComposite::GetDataHere(const wxDataFormat& format,
228 void *buf) const
229{
8b445796 230 wxDataObjectSimple *dataObj = GetObject( format );
3f364be8 231
68379eaf 232 wxCHECK_MSG( dataObj, false,
3f364be8
VZ
233 wxT("unsupported format in wxDataObjectComposite"));
234
8b445796 235 return dataObj->GetDataHere( buf );
3f364be8
VZ
236}
237
238bool wxDataObjectComposite::SetData(const wxDataFormat& format,
239 size_t len,
240 const void *buf)
241{
8b445796 242 wxDataObjectSimple *dataObj = GetObject( format );
3f364be8 243
68379eaf 244 wxCHECK_MSG( dataObj, false,
3f364be8
VZ
245 wxT("unsupported format in wxDataObjectComposite"));
246
c072c757 247 m_receivedFormat = format;
8b445796 248 return dataObj->SetData( len, buf );
3f364be8
VZ
249}
250
251// ----------------------------------------------------------------------------
252// wxTextDataObject
253// ----------------------------------------------------------------------------
254
98ecad06
VZ
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
c7d6d883 262
f4370741
VZ
263static 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
c7d6d883
RR
269size_t wxTextDataObject::GetDataSize(const wxDataFormat& format) const
270{
f4370741 271 wxCharBuffer buffer = GetConv(format).cWX2MB( GetText().c_str() );
8b445796 272
2c906a49 273 return buffer ? strlen( buffer ) : 0;
c7d6d883
RR
274}
275
276bool wxTextDataObject::GetDataHere(const wxDataFormat& format, void *buf) const
277{
f7570f57 278 if ( !buf )
8b445796
DS
279 return false;
280
f4370741 281 wxCharBuffer buffer = GetConv(format).cWX2MB( GetText().c_str() );
f7570f57
RR
282 if ( !buffer )
283 return false;
f4370741 284
f7570f57
RR
285 memcpy( (char*) buf, buffer, GetDataSize(format) );
286 // strcpy( (char*) buf, buffer );
68379eaf
WS
287
288 return true;
c7d6d883
RR
289}
290
291bool wxTextDataObject::SetData(const wxDataFormat& format,
292 size_t WXUNUSED(len), const void *buf)
293{
8b445796 294 if ( buf == NULL )
f4370741
VZ
295 return false;
296
8b445796 297 wxWCharBuffer buffer = GetConv(format).cMB2WX( (const char*)buf );
8b445796
DS
298
299 SetText( buffer );
68379eaf
WS
300
301 return true;
c7d6d883
RR
302}
303
98ecad06
VZ
304#else // wxUSE_UNICODE_UTF8
305
306size_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
319bool 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
340bool wxTextDataObject::SetData(const wxDataFormat& format,
341 size_t len, const void *buf_)
342{
5c33522f 343 const char * const buf = static_cast<const char *>(buf_);
98ecad06
VZ
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)
25758297 368
8b445796 369static wxMBConvUTF16 sUTF16Converter;
671478ee
SC
370
371static inline wxMBConv& GetConv(const wxDataFormat& format)
372{
8b445796
DS
373 return
374 format == wxDF_UNICODETEXT
375 ? (wxMBConv&) sUTF16Converter
376 : (wxMBConv&) wxConvLocal;
671478ee
SC
377}
378
25758297
SC
379size_t wxTextDataObject::GetDataSize(const wxDataFormat& format) const
380{
8b445796
DS
381 size_t len = GetConv(format).WC2MB( NULL, GetText().c_str(), 0 );
382 len += (format == wxDF_UNICODETEXT ? 2 : 1);
383
384 return len;
25758297
SC
385}
386
387bool wxTextDataObject::GetDataHere(const wxDataFormat& format, void *buf) const
388{
8b445796 389 if ( buf == NULL )
671478ee
SC
390 return false;
391
8b445796 392 wxCharBuffer buffer = GetConv(format).cWX2MB( GetText().c_str() );
671478ee 393
8b445796
DS
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 );
68379eaf
WS
399
400 return true;
25758297
SC
401}
402
403bool wxTextDataObject::SetData(const wxDataFormat& format,
404 size_t WXUNUSED(len), const void *buf)
405{
8b445796 406 if ( buf == NULL )
671478ee 407 return false;
8b445796
DS
408
409 wxWCharBuffer buffer = GetConv(format).cMB2WX( (const char*)buf );
410
411 SetText( buffer );
412
68379eaf 413 return true;
25758297
SC
414}
415
98ecad06 416#else // !wxNEEDS_UTF{8,16}_FOR_TEXT_DATAOBJ
c7d6d883
RR
417
418size_t wxTextDataObject::GetDataSize() const
419{
e1b435af 420 return GetTextLength() * sizeof(wxChar);
3f364be8
VZ
421}
422
423bool wxTextDataObject::GetDataHere(void *buf) const
424{
8b445796 425 wxStrcpy( (wxChar*)buf, GetText().c_str() );
3f364be8 426
68379eaf 427 return true;
3f364be8
VZ
428}
429
430bool wxTextDataObject::SetData(size_t WXUNUSED(len), const void *buf)
431{
8b445796 432 SetText( wxString((const wxChar*)buf) );
3f364be8 433
68379eaf 434 return true;
3f364be8
VZ
435}
436
98ecad06 437#endif // different wxTextDataObject implementations
9e2896e5 438
3f364be8
VZ
439// ----------------------------------------------------------------------------
440// wxCustomDataObject
441// ----------------------------------------------------------------------------
442
775e1c62
RD
443wxCustomDataObject::wxCustomDataObject(const wxDataFormat& format)
444 : wxDataObjectSimple(format)
445{
8b445796
DS
446 m_data = NULL;
447 m_size = 0;
775e1c62
RD
448}
449
3f364be8
VZ
450wxCustomDataObject::~wxCustomDataObject()
451{
452 Free();
453}
454
455void wxCustomDataObject::TakeData(size_t size, void *data)
456{
457 Free();
458
459 m_size = size;
460 m_data = data;
461}
462
463void *wxCustomDataObject::Alloc(size_t size)
464{
465 return (void *)new char[size];
466}
467
468void wxCustomDataObject::Free()
469{
8b445796 470 delete [] (char*)m_data;
3f364be8 471 m_size = 0;
d3b9f782 472 m_data = NULL;
3f364be8
VZ
473}
474
475size_t wxCustomDataObject::GetDataSize() const
476{
477 return GetSize();
478}
479
480bool wxCustomDataObject::GetDataHere(void *buf) const
481{
8b445796
DS
482 if ( buf == NULL )
483 return false;
484
3f364be8 485 void *data = GetData();
8b445796 486 if ( data == NULL )
68379eaf 487 return false;
3f364be8 488
8b445796 489 memcpy( buf, data, GetSize() );
3f364be8 490
68379eaf 491 return true;
3f364be8
VZ
492}
493
9e2896e5 494bool wxCustomDataObject::SetData(size_t size, const void *buf)
3f364be8
VZ
495{
496 Free();
497
498 m_data = Alloc(size);
8b445796 499 if ( m_data == NULL )
68379eaf 500 return false;
3f364be8 501
8b445796
DS
502 m_size = size;
503 memcpy( m_data, buf, m_size );
3f364be8 504
68379eaf 505 return true;
3f364be8
VZ
506}
507
8ee9d618
VZ
508// ============================================================================
509// some common dnd related code
510// ============================================================================
511
8fb3a512
JS
512#if wxUSE_DRAG_AND_DROP
513
8ee9d618
VZ
514#include "wx/dnd.h"
515
516// ----------------------------------------------------------------------------
517// wxTextDropTarget
518// ----------------------------------------------------------------------------
519
f3ac12aa
VZ
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
8ee9d618 524wxTextDropTarget::wxTextDropTarget()
8ee9d618 525{
f3ac12aa 526 SetDataObject(new wxTextDataObject);
8ee9d618
VZ
527}
528
529wxDragResult wxTextDropTarget::OnData(wxCoord x, wxCoord y, wxDragResult def)
530{
531 if ( !GetData() )
532 return wxDragNone;
533
534 wxTextDataObject *dobj = (wxTextDataObject *)m_dataObject;
8b445796 535 return OnDropText( x, y, dobj->GetText() ) ? def : wxDragNone;
8ee9d618
VZ
536}
537
538// ----------------------------------------------------------------------------
539// wxFileDropTarget
540// ----------------------------------------------------------------------------
541
542wxFileDropTarget::wxFileDropTarget()
8ee9d618 543{
f3ac12aa 544 SetDataObject(new wxFileDataObject);
8ee9d618
VZ
545}
546
547wxDragResult wxFileDropTarget::OnData(wxCoord x, wxCoord y, wxDragResult def)
548{
549 if ( !GetData() )
550 return wxDragNone;
551
552 wxFileDataObject *dobj = (wxFileDataObject *)m_dataObject;
8b445796 553 return OnDropFiles( x, y, dobj->GetFilenames() ) ? def : wxDragNone;
8ee9d618
VZ
554}
555
1e6feb95 556#endif // wxUSE_DRAG_AND_DROP
8fb3a512 557
1e6feb95 558#endif // wxUSE_DATAOBJ