]> git.saurik.com Git - wxWidgets.git/blame - src/common/dobjcmn.cpp
build fixes for wxUSE_IPV6==1 under Windows
[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
VZ
96wxDataObjectSimple *
97wxDataObjectComposite::GetObject(const wxDataFormat& format) const
98{
222ed1d6 99 wxSimpleDataObjectList::compatibility_iterator node = m_dataObjects.GetFirst();
3f364be8
VZ
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 (wxDataObjectSimple *)NULL;
113}
114
115void wxDataObjectComposite::Add(wxDataObjectSimple *dataObject, bool preferred)
116{
117 if ( preferred )
118 m_preferred = m_dataObjects.GetCount();
119
120 m_dataObjects.Append( dataObject );
121}
122
c072c757
RD
123wxDataFormat wxDataObjectComposite::GetReceivedFormat() const
124{
125 return m_receivedFormat;
126}
127
3f364be8
VZ
128wxDataFormat
129wxDataObjectComposite::GetPreferredFormat(Direction WXUNUSED(dir)) const
130{
222ed1d6 131 wxSimpleDataObjectList::compatibility_iterator node = m_dataObjects.Item( m_preferred );
3f364be8 132
2ee3ee1b 133 wxCHECK_MSG( node, wxFormatInvalid, wxT("no preferred format") );
3f364be8
VZ
134
135 wxDataObjectSimple* dataObj = node->GetData();
136
137 return dataObj->GetFormat();
138}
139
e1b435af 140#if defined(__WXMSW__)
7d584866 141
e1b435af
MB
142size_t wxDataObjectComposite::GetBufferOffset( const wxDataFormat& format )
143{
144 wxDataObjectSimple *dataObj = GetObject(format);
145
f7f50f49 146 wxCHECK_MSG( dataObj, 0,
e1b435af
MB
147 wxT("unsupported format in wxDataObjectComposite"));
148
149 return dataObj->GetBufferOffset( format );
150}
151
7d584866 152
e1b435af
MB
153const void* wxDataObjectComposite::GetSizeFromBuffer( const void* buffer,
154 size_t* size,
155 const wxDataFormat& format )
156{
157 wxDataObjectSimple *dataObj = GetObject(format);
158
f7f50f49 159 wxCHECK_MSG( dataObj, NULL,
e1b435af
MB
160 wxT("unsupported format in wxDataObjectComposite"));
161
162 return dataObj->GetSizeFromBuffer( buffer, size, format );
163}
164
7d584866 165
e1b435af
MB
166void* wxDataObjectComposite::SetSizeInBuffer( void* buffer, size_t size,
167 const wxDataFormat& format )
168{
8b445796 169 wxDataObjectSimple *dataObj = GetObject( format );
e1b435af 170
f7f50f49 171 wxCHECK_MSG( dataObj, NULL,
e1b435af
MB
172 wxT("unsupported format in wxDataObjectComposite"));
173
174 return dataObj->SetSizeInBuffer( buffer, size, format );
175}
176
177#endif
178
3f364be8
VZ
179size_t wxDataObjectComposite::GetFormatCount(Direction WXUNUSED(dir)) const
180{
181 // TODO what about the Get/Set only formats?
182 return m_dataObjects.GetCount();
183}
184
185void wxDataObjectComposite::GetAllFormats(wxDataFormat *formats,
186 Direction WXUNUSED(dir)) const
187{
188 size_t n = 0;
222ed1d6 189 wxSimpleDataObjectList::compatibility_iterator node;
3f364be8
VZ
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
197size_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
207bool wxDataObjectComposite::GetDataHere(const wxDataFormat& format,
208 void *buf) const
209{
8b445796 210 wxDataObjectSimple *dataObj = GetObject( format );
3f364be8 211
68379eaf 212 wxCHECK_MSG( dataObj, false,
3f364be8
VZ
213 wxT("unsupported format in wxDataObjectComposite"));
214
8b445796 215 return dataObj->GetDataHere( buf );
3f364be8
VZ
216}
217
218bool wxDataObjectComposite::SetData(const wxDataFormat& format,
219 size_t len,
220 const void *buf)
221{
8b445796 222 wxDataObjectSimple *dataObj = GetObject( format );
3f364be8 223
68379eaf 224 wxCHECK_MSG( dataObj, false,
3f364be8
VZ
225 wxT("unsupported format in wxDataObjectComposite"));
226
c072c757 227 m_receivedFormat = format;
8b445796 228 return dataObj->SetData( len, buf );
3f364be8
VZ
229}
230
231// ----------------------------------------------------------------------------
232// wxTextDataObject
233// ----------------------------------------------------------------------------
234
98ecad06
VZ
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
c7d6d883 242
f4370741
VZ
243static 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
c7d6d883
RR
249size_t wxTextDataObject::GetDataSize(const wxDataFormat& format) const
250{
f4370741 251 wxCharBuffer buffer = GetConv(format).cWX2MB( GetText().c_str() );
8b445796 252
2c906a49 253 return buffer ? strlen( buffer ) : 0;
c7d6d883
RR
254}
255
256bool wxTextDataObject::GetDataHere(const wxDataFormat& format, void *buf) const
257{
f7570f57 258 if ( !buf )
8b445796
DS
259 return false;
260
f4370741 261 wxCharBuffer buffer = GetConv(format).cWX2MB( GetText().c_str() );
f7570f57
RR
262 if ( !buffer )
263 return false;
f4370741 264
f7570f57
RR
265 memcpy( (char*) buf, buffer, GetDataSize(format) );
266 // strcpy( (char*) buf, buffer );
68379eaf
WS
267
268 return true;
c7d6d883
RR
269}
270
271bool wxTextDataObject::SetData(const wxDataFormat& format,
272 size_t WXUNUSED(len), const void *buf)
273{
8b445796 274 if ( buf == NULL )
f4370741
VZ
275 return false;
276
8b445796 277 wxWCharBuffer buffer = GetConv(format).cMB2WX( (const char*)buf );
8b445796
DS
278
279 SetText( buffer );
68379eaf
WS
280
281 return true;
c7d6d883
RR
282}
283
98ecad06
VZ
284#else // wxUSE_UNICODE_UTF8
285
286size_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
299bool 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
320bool wxTextDataObject::SetData(const wxDataFormat& format,
321 size_t len, const void *buf_)
322{
5c33522f 323 const char * const buf = static_cast<const char *>(buf_);
98ecad06
VZ
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)
25758297 348
8b445796 349static wxMBConvUTF16 sUTF16Converter;
671478ee
SC
350
351static inline wxMBConv& GetConv(const wxDataFormat& format)
352{
8b445796
DS
353 return
354 format == wxDF_UNICODETEXT
355 ? (wxMBConv&) sUTF16Converter
356 : (wxMBConv&) wxConvLocal;
671478ee
SC
357}
358
25758297
SC
359size_t wxTextDataObject::GetDataSize(const wxDataFormat& format) const
360{
8b445796
DS
361 size_t len = GetConv(format).WC2MB( NULL, GetText().c_str(), 0 );
362 len += (format == wxDF_UNICODETEXT ? 2 : 1);
363
364 return len;
25758297
SC
365}
366
367bool wxTextDataObject::GetDataHere(const wxDataFormat& format, void *buf) const
368{
8b445796 369 if ( buf == NULL )
671478ee
SC
370 return false;
371
8b445796 372 wxCharBuffer buffer = GetConv(format).cWX2MB( GetText().c_str() );
671478ee 373
8b445796
DS
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 );
68379eaf
WS
379
380 return true;
25758297
SC
381}
382
383bool wxTextDataObject::SetData(const wxDataFormat& format,
384 size_t WXUNUSED(len), const void *buf)
385{
8b445796 386 if ( buf == NULL )
671478ee 387 return false;
8b445796
DS
388
389 wxWCharBuffer buffer = GetConv(format).cMB2WX( (const char*)buf );
390
391 SetText( buffer );
392
68379eaf 393 return true;
25758297
SC
394}
395
98ecad06 396#else // !wxNEEDS_UTF{8,16}_FOR_TEXT_DATAOBJ
c7d6d883
RR
397
398size_t wxTextDataObject::GetDataSize() const
399{
e1b435af 400 return GetTextLength() * sizeof(wxChar);
3f364be8
VZ
401}
402
403bool wxTextDataObject::GetDataHere(void *buf) const
404{
8b445796 405 wxStrcpy( (wxChar*)buf, GetText().c_str() );
3f364be8 406
68379eaf 407 return true;
3f364be8
VZ
408}
409
410bool wxTextDataObject::SetData(size_t WXUNUSED(len), const void *buf)
411{
8b445796 412 SetText( wxString((const wxChar*)buf) );
3f364be8 413
68379eaf 414 return true;
3f364be8
VZ
415}
416
98ecad06 417#endif // different wxTextDataObject implementations
9e2896e5 418
3f364be8
VZ
419// ----------------------------------------------------------------------------
420// wxCustomDataObject
421// ----------------------------------------------------------------------------
422
775e1c62
RD
423wxCustomDataObject::wxCustomDataObject(const wxDataFormat& format)
424 : wxDataObjectSimple(format)
425{
8b445796
DS
426 m_data = NULL;
427 m_size = 0;
775e1c62
RD
428}
429
3f364be8
VZ
430wxCustomDataObject::~wxCustomDataObject()
431{
432 Free();
433}
434
435void wxCustomDataObject::TakeData(size_t size, void *data)
436{
437 Free();
438
439 m_size = size;
440 m_data = data;
441}
442
443void *wxCustomDataObject::Alloc(size_t size)
444{
445 return (void *)new char[size];
446}
447
448void wxCustomDataObject::Free()
449{
8b445796 450 delete [] (char*)m_data;
3f364be8 451 m_size = 0;
8b445796 452 m_data = (void*)NULL;
3f364be8
VZ
453}
454
455size_t wxCustomDataObject::GetDataSize() const
456{
457 return GetSize();
458}
459
460bool wxCustomDataObject::GetDataHere(void *buf) const
461{
8b445796
DS
462 if ( buf == NULL )
463 return false;
464
3f364be8 465 void *data = GetData();
8b445796 466 if ( data == NULL )
68379eaf 467 return false;
3f364be8 468
8b445796 469 memcpy( buf, data, GetSize() );
3f364be8 470
68379eaf 471 return true;
3f364be8
VZ
472}
473
9e2896e5 474bool wxCustomDataObject::SetData(size_t size, const void *buf)
3f364be8
VZ
475{
476 Free();
477
478 m_data = Alloc(size);
8b445796 479 if ( m_data == NULL )
68379eaf 480 return false;
3f364be8 481
8b445796
DS
482 m_size = size;
483 memcpy( m_data, buf, m_size );
3f364be8 484
68379eaf 485 return true;
3f364be8
VZ
486}
487
8ee9d618
VZ
488// ============================================================================
489// some common dnd related code
490// ============================================================================
491
8fb3a512
JS
492#if wxUSE_DRAG_AND_DROP
493
8ee9d618
VZ
494#include "wx/dnd.h"
495
496// ----------------------------------------------------------------------------
497// wxTextDropTarget
498// ----------------------------------------------------------------------------
499
f3ac12aa
VZ
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
8ee9d618 504wxTextDropTarget::wxTextDropTarget()
8ee9d618 505{
f3ac12aa 506 SetDataObject(new wxTextDataObject);
8ee9d618
VZ
507}
508
509wxDragResult wxTextDropTarget::OnData(wxCoord x, wxCoord y, wxDragResult def)
510{
511 if ( !GetData() )
512 return wxDragNone;
513
514 wxTextDataObject *dobj = (wxTextDataObject *)m_dataObject;
8b445796 515 return OnDropText( x, y, dobj->GetText() ) ? def : wxDragNone;
8ee9d618
VZ
516}
517
518// ----------------------------------------------------------------------------
519// wxFileDropTarget
520// ----------------------------------------------------------------------------
521
522wxFileDropTarget::wxFileDropTarget()
8ee9d618 523{
f3ac12aa 524 SetDataObject(new wxFileDataObject);
8ee9d618
VZ
525}
526
527wxDragResult wxFileDropTarget::OnData(wxCoord x, wxCoord y, wxDragResult def)
528{
529 if ( !GetData() )
530 return wxDragNone;
531
532 wxFileDataObject *dobj = (wxFileDataObject *)m_dataObject;
8b445796 533 return OnDropFiles( x, y, dobj->GetFilenames() ) ? def : wxDragNone;
8ee9d618
VZ
534}
535
1e6feb95 536#endif // wxUSE_DRAG_AND_DROP
8fb3a512 537
1e6feb95 538#endif // wxUSE_DATAOBJ