]> git.saurik.com Git - wxWidgets.git/blame - src/common/dobjcmn.cpp
require libsm-dev, it's needed for KDE/GNOME detection
[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
6a59178a 235#if defined(__WXGTK20__) && wxUSE_UNICODE
c7d6d883 236
f4370741
VZ
237static inline wxMBConv& GetConv(const wxDataFormat& format)
238{
239 // use UTF8 for wxDF_UNICODETEXT and UCS4 for wxDF_TEXT
240 return format == wxDF_UNICODETEXT ? wxConvUTF8 : wxConvLibc;
241}
242
c7d6d883
RR
243size_t wxTextDataObject::GetDataSize(const wxDataFormat& format) const
244{
f4370741 245 wxCharBuffer buffer = GetConv(format).cWX2MB( GetText().c_str() );
8b445796 246
2c906a49 247 return buffer ? strlen( buffer ) : 0;
c7d6d883
RR
248}
249
250bool wxTextDataObject::GetDataHere(const wxDataFormat& format, void *buf) const
251{
f7570f57 252 if ( !buf )
8b445796
DS
253 return false;
254
f4370741 255 wxCharBuffer buffer = GetConv(format).cWX2MB( GetText().c_str() );
f7570f57
RR
256 if ( !buffer )
257 return false;
f4370741 258
f7570f57
RR
259 memcpy( (char*) buf, buffer, GetDataSize(format) );
260 // strcpy( (char*) buf, buffer );
68379eaf
WS
261
262 return true;
c7d6d883
RR
263}
264
265bool wxTextDataObject::SetData(const wxDataFormat& format,
266 size_t WXUNUSED(len), const void *buf)
267{
8b445796 268 if ( buf == NULL )
f4370741
VZ
269 return false;
270
8b445796 271 wxWCharBuffer buffer = GetConv(format).cMB2WX( (const char*)buf );
8b445796
DS
272
273 SetText( buffer );
68379eaf
WS
274
275 return true;
c7d6d883
RR
276}
277
25758297
SC
278#elif wxUSE_UNICODE && defined(__WXMAC__)
279
8b445796 280static wxMBConvUTF16 sUTF16Converter;
671478ee
SC
281
282static inline wxMBConv& GetConv(const wxDataFormat& format)
283{
8b445796
DS
284 return
285 format == wxDF_UNICODETEXT
286 ? (wxMBConv&) sUTF16Converter
287 : (wxMBConv&) wxConvLocal;
671478ee
SC
288}
289
25758297
SC
290size_t wxTextDataObject::GetDataSize(const wxDataFormat& format) const
291{
8b445796
DS
292 size_t len = GetConv(format).WC2MB( NULL, GetText().c_str(), 0 );
293 len += (format == wxDF_UNICODETEXT ? 2 : 1);
294
295 return len;
25758297
SC
296}
297
298bool wxTextDataObject::GetDataHere(const wxDataFormat& format, void *buf) const
299{
8b445796 300 if ( buf == NULL )
671478ee
SC
301 return false;
302
8b445796 303 wxCharBuffer buffer = GetConv(format).cWX2MB( GetText().c_str() );
671478ee 304
8b445796
DS
305 size_t len = GetConv(format).WC2MB( NULL, GetText().c_str(), 0 );
306 len += (format == wxDF_UNICODETEXT ? 2 : 1);
307
308 // trailing (uni)char 0
309 memcpy( (char*)buf, (const char*)buffer, len );
68379eaf
WS
310
311 return true;
25758297
SC
312}
313
314bool wxTextDataObject::SetData(const wxDataFormat& format,
315 size_t WXUNUSED(len), const void *buf)
316{
8b445796 317 if ( buf == NULL )
671478ee 318 return false;
8b445796
DS
319
320 wxWCharBuffer buffer = GetConv(format).cMB2WX( (const char*)buf );
321
322 SetText( buffer );
323
68379eaf 324 return true;
25758297
SC
325}
326
6a59178a 327#else
c7d6d883
RR
328
329size_t wxTextDataObject::GetDataSize() const
330{
e1b435af 331 return GetTextLength() * sizeof(wxChar);
3f364be8
VZ
332}
333
334bool wxTextDataObject::GetDataHere(void *buf) const
335{
8b445796 336 wxStrcpy( (wxChar*)buf, GetText().c_str() );
3f364be8 337
68379eaf 338 return true;
3f364be8
VZ
339}
340
341bool wxTextDataObject::SetData(size_t WXUNUSED(len), const void *buf)
342{
8b445796 343 SetText( wxString((const wxChar*)buf) );
3f364be8 344
68379eaf 345 return true;
3f364be8
VZ
346}
347
c7d6d883
RR
348#endif
349
3f364be8
VZ
350// ----------------------------------------------------------------------------
351// wxFileDataObjectBase
352// ----------------------------------------------------------------------------
353
9e2896e5
VZ
354// VZ: I don't need this in MSW finally, so if it is needed in wxGTK, it should
355// be moved to gtk/dataobj.cpp
356#if 0
357
3f364be8
VZ
358wxString wxFileDataObjectBase::GetFilenames() const
359{
360 wxString str;
361 size_t count = m_filenames.GetCount();
362 for ( size_t n = 0; n < count; n++ )
363 {
364 str << m_filenames[n] << wxT('\0');
365 }
366
367 return str;
368}
369
370void wxFileDataObjectBase::SetFilenames(const wxChar* filenames)
371{
372 m_filenames.Empty();
373
374 wxString current;
375 for ( const wxChar *pc = filenames; ; pc++ )
376 {
377 if ( *pc )
378 {
379 current += *pc;
380 }
381 else
382 {
383 if ( !current )
384 {
385 // 2 consecutive NULs - this is the end of the string
386 break;
387 }
388
389 m_filenames.Add(current);
390 current.Empty();
391 }
392 }
393}
394
8b445796 395#endif
9e2896e5 396
3f364be8
VZ
397// ----------------------------------------------------------------------------
398// wxCustomDataObject
399// ----------------------------------------------------------------------------
400
775e1c62
RD
401wxCustomDataObject::wxCustomDataObject(const wxDataFormat& format)
402 : wxDataObjectSimple(format)
403{
8b445796
DS
404 m_data = NULL;
405 m_size = 0;
775e1c62
RD
406}
407
3f364be8
VZ
408wxCustomDataObject::~wxCustomDataObject()
409{
410 Free();
411}
412
413void wxCustomDataObject::TakeData(size_t size, void *data)
414{
415 Free();
416
417 m_size = size;
418 m_data = data;
419}
420
421void *wxCustomDataObject::Alloc(size_t size)
422{
423 return (void *)new char[size];
424}
425
426void wxCustomDataObject::Free()
427{
8b445796 428 delete [] (char*)m_data;
3f364be8 429 m_size = 0;
8b445796 430 m_data = (void*)NULL;
3f364be8
VZ
431}
432
433size_t wxCustomDataObject::GetDataSize() const
434{
435 return GetSize();
436}
437
438bool wxCustomDataObject::GetDataHere(void *buf) const
439{
8b445796
DS
440 if ( buf == NULL )
441 return false;
442
3f364be8 443 void *data = GetData();
8b445796 444 if ( data == NULL )
68379eaf 445 return false;
3f364be8 446
8b445796 447 memcpy( buf, data, GetSize() );
3f364be8 448
68379eaf 449 return true;
3f364be8
VZ
450}
451
9e2896e5 452bool wxCustomDataObject::SetData(size_t size, const void *buf)
3f364be8
VZ
453{
454 Free();
455
456 m_data = Alloc(size);
8b445796 457 if ( m_data == NULL )
68379eaf 458 return false;
3f364be8 459
8b445796
DS
460 m_size = size;
461 memcpy( m_data, buf, m_size );
3f364be8 462
68379eaf 463 return true;
3f364be8
VZ
464}
465
8ee9d618
VZ
466// ============================================================================
467// some common dnd related code
468// ============================================================================
469
8fb3a512
JS
470#if wxUSE_DRAG_AND_DROP
471
8ee9d618
VZ
472#include "wx/dnd.h"
473
474// ----------------------------------------------------------------------------
475// wxTextDropTarget
476// ----------------------------------------------------------------------------
477
f3ac12aa
VZ
478// NB: we can't use "new" in ctor initializer lists because this provokes an
479// internal compiler error with VC++ 5.0 (hey, even gcc compiles this!),
480// so use SetDataObject() instead
481
8ee9d618 482wxTextDropTarget::wxTextDropTarget()
8ee9d618 483{
f3ac12aa 484 SetDataObject(new wxTextDataObject);
8ee9d618
VZ
485}
486
487wxDragResult wxTextDropTarget::OnData(wxCoord x, wxCoord y, wxDragResult def)
488{
489 if ( !GetData() )
490 return wxDragNone;
491
492 wxTextDataObject *dobj = (wxTextDataObject *)m_dataObject;
8b445796 493 return OnDropText( x, y, dobj->GetText() ) ? def : wxDragNone;
8ee9d618
VZ
494}
495
496// ----------------------------------------------------------------------------
497// wxFileDropTarget
498// ----------------------------------------------------------------------------
499
500wxFileDropTarget::wxFileDropTarget()
8ee9d618 501{
f3ac12aa 502 SetDataObject(new wxFileDataObject);
8ee9d618
VZ
503}
504
505wxDragResult wxFileDropTarget::OnData(wxCoord x, wxCoord y, wxDragResult def)
506{
507 if ( !GetData() )
508 return wxDragNone;
509
510 wxFileDataObject *dobj = (wxFileDataObject *)m_dataObject;
8b445796 511 return OnDropFiles( x, y, dobj->GetFilenames() ) ? def : wxDragNone;
8ee9d618
VZ
512}
513
1e6feb95 514#endif // wxUSE_DRAG_AND_DROP
8fb3a512 515
1e6feb95 516#endif // wxUSE_DATAOBJ