]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/dataobj.cpp
Added wxOKlibc(), which checks for glibc2.0, which incorrectly does UTF-8
[wxWidgets.git] / src / gtk1 / dataobj.cpp
CommitLineData
8b53e5a2
RR
1///////////////////////////////////////////////////////////////////////////////
2// Name: dataobj.cpp
3// Purpose: wxDataObject class
4// Author: Robert Roebling
5// Id: $Id$
6// Copyright: (c) 1998 Robert Roebling
7// Licence: wxWindows licence
8///////////////////////////////////////////////////////////////////////////////
9
10#ifdef __GNUG__
11#pragma implementation "dataobj.h"
12#endif
13
14#include "wx/dataobj.h"
ab8884ac 15#include "wx/app.h"
0d2a2b60
RR
16#include "wx/debug.h"
17
18#include "gdk/gdk.h"
19
d6086ea6
RR
20
21//-------------------------------------------------------------------------
22// global data
23//-------------------------------------------------------------------------
24
25GdkAtom g_textAtom = 0;
26
0d2a2b60
RR
27//-------------------------------------------------------------------------
28// wxDataFormat
29//-------------------------------------------------------------------------
30
31IMPLEMENT_CLASS(wxDataFormat, wxObject)
32
cd5bf2a6 33wxDataFormat::wxDataFormat()
0d2a2b60 34{
034be888 35 if (!g_textAtom) g_textAtom = gdk_atom_intern( "STRING", FALSE );
cd5bf2a6 36 m_type = wxDF_INVALID;
0d2a2b60 37 m_hasAtom = FALSE;
cd5bf2a6
RR
38 m_atom = (GdkAtom) 0;
39}
40
41wxDataFormat::wxDataFormat( wxDataType type )
42{
034be888 43 if (!g_textAtom) g_textAtom = gdk_atom_intern( "STRING", FALSE );
cd5bf2a6 44 SetType( type );
0d2a2b60
RR
45}
46
b5be07d4 47wxDataFormat::wxDataFormat( const wxChar *id )
8a126fcc
RR
48{
49 if (!g_textAtom) g_textAtom = gdk_atom_intern( "STRING", FALSE );
50 SetId( id );
51}
52
0d2a2b60
RR
53wxDataFormat::wxDataFormat( const wxString &id )
54{
034be888 55 if (!g_textAtom) g_textAtom = gdk_atom_intern( "STRING", FALSE );
cd5bf2a6 56 SetId( id );
0d2a2b60
RR
57}
58
59wxDataFormat::wxDataFormat( wxDataFormat &format )
60{
034be888 61 if (!g_textAtom) g_textAtom = gdk_atom_intern( "STRING", FALSE );
0d2a2b60
RR
62 m_type = format.GetType();
63 m_id = format.GetId();
64 m_hasAtom = TRUE;
65 m_atom = format.GetAtom();
66}
67
68wxDataFormat::wxDataFormat( const GdkAtom atom )
69{
034be888 70 if (!g_textAtom) g_textAtom = gdk_atom_intern( "STRING", FALSE );
0d2a2b60
RR
71 m_hasAtom = TRUE;
72
73 m_atom = atom;
74
d6086ea6 75 if (m_atom == g_textAtom)
0d2a2b60
RR
76 {
77 m_type = wxDF_TEXT;
78 } else
79 if (m_atom == GDK_TARGET_BITMAP)
80 {
81 m_type = wxDF_BITMAP;
82 } else
83 {
84 m_type = wxDF_PRIVATE;
85 m_id = gdk_atom_name( m_atom );
86
93c5dd39 87 if (m_id == _T("file:ALL"))
0d2a2b60
RR
88 {
89 m_type = wxDF_FILENAME;
90 }
91 }
92}
93
cd5bf2a6
RR
94void wxDataFormat::SetType( wxDataType type )
95{
96 m_type = type;
97
98 if (m_type == wxDF_TEXT)
99 {
93c5dd39 100 m_id = _T("STRING");
cd5bf2a6
RR
101 }
102 else
103 if (m_type == wxDF_BITMAP)
104 {
93c5dd39 105 m_id = _T("BITMAP");
cd5bf2a6
RR
106 }
107 else
108 if (m_type == wxDF_FILENAME)
109 {
93c5dd39 110 m_id = _T("file:ALL");
cd5bf2a6
RR
111 }
112 else
113 {
93c5dd39 114 wxFAIL_MSG( _T("invalid dataformat") );
cd5bf2a6
RR
115 }
116
117 m_hasAtom = FALSE;
118}
119
120wxDataType wxDataFormat::GetType() const
0d2a2b60
RR
121{
122 return m_type;
123}
124
125wxString wxDataFormat::GetId() const
126{
127 return m_id;
128}
129
b5be07d4 130void wxDataFormat::SetId( const wxChar *id )
0d2a2b60
RR
131{
132 m_type = wxDF_PRIVATE;
133 m_id = id;
134 m_hasAtom = FALSE;
135}
136
137GdkAtom wxDataFormat::GetAtom()
138{
139 if (!m_hasAtom)
140 {
141 m_hasAtom = TRUE;
142
143 if (m_type == wxDF_TEXT)
144 {
d6086ea6
RR
145 m_atom = g_textAtom;
146 }
0d2a2b60
RR
147 else
148 if (m_type == wxDF_BITMAP)
149 {
150 m_atom = GDK_TARGET_BITMAP;
151 }
152 else
153 if (m_type == wxDF_PRIVATE)
154 {
93c5dd39 155 m_atom = gdk_atom_intern( MBSTRINGCAST m_id.mbc_str(), FALSE );
0d2a2b60
RR
156 }
157 else
158 if (m_type == wxDF_FILENAME)
159 {
160 m_atom = gdk_atom_intern( "file:ALL", FALSE );
161 }
162 else
163 {
164 m_hasAtom = FALSE;
165 m_atom = (GdkAtom) 0;
166 }
167 }
168
169 return m_atom;
170}
171
172//-------------------------------------------------------------------------
173// wxDataBroker
174//-------------------------------------------------------------------------
175
176IMPLEMENT_CLASS(wxDataBroker,wxObject)
177
178wxDataBroker::wxDataBroker()
179{
180 m_dataObjects.DeleteContents(TRUE);
181 m_preferred = 0;
182}
183
184void wxDataBroker::Add( wxDataObject *dataObject, bool preferred )
185{
186 if (preferred) m_preferred = m_dataObjects.GetCount();
187 m_dataObjects.Append( dataObject );
188}
189
190size_t wxDataBroker::GetFormatCount() const
191{
192 return m_dataObjects.GetCount();
193}
194
195wxDataFormat &wxDataBroker::GetPreferredFormat() const
196{
197 wxNode *node = m_dataObjects.Nth( m_preferred );
198
199 wxASSERT( node );
200
201 wxDataObject* data_obj = (wxDataObject*)node->Data();
202
203 return data_obj->GetFormat();
204}
205
206wxDataFormat &wxDataBroker::GetNthFormat( size_t nth ) const
207{
208 wxNode *node = m_dataObjects.Nth( nth );
209
210 wxASSERT( node );
211
212 wxDataObject* data_obj = (wxDataObject*)node->Data();
213
214 return data_obj->GetFormat();
215}
216
217bool wxDataBroker::IsSupportedFormat( wxDataFormat &format ) const
218{
219 wxNode *node = m_dataObjects.First();
220 while (node)
221 {
222 wxDataObject *dobj = (wxDataObject*)node->Data();
223
224 if (dobj->GetFormat().GetAtom() == format.GetAtom())
225 {
226 return TRUE;
227 }
228
229 node = node->Next();
230 }
231
232 return FALSE;
233}
234
235size_t wxDataBroker::GetSize( wxDataFormat& format ) const
236{
237 wxNode *node = m_dataObjects.First();
238 while (node)
239 {
240 wxDataObject *dobj = (wxDataObject*)node->Data();
241
242 if (dobj->GetFormat().GetAtom() == format.GetAtom())
243 {
244 return dobj->GetSize();
245 }
246
247 node = node->Next();
248 }
249
250 return 0;
251}
252
253void wxDataBroker::WriteData( wxDataFormat& format, void *dest ) const
254{
255 wxNode *node = m_dataObjects.First();
256 while (node)
257 {
258 wxDataObject *dobj = (wxDataObject*)node->Data();
259
260 if (dobj->GetFormat().GetAtom() == format.GetAtom())
261 {
06ad8636 262 dobj->WriteData( dest );
0d2a2b60
RR
263 }
264
265 node = node->Next();
266 }
267}
8b53e5a2
RR
268
269//-------------------------------------------------------------------------
270// wxDataObject
271//-------------------------------------------------------------------------
272
273IMPLEMENT_ABSTRACT_CLASS( wxDataObject, wxObject )
274
0d2a2b60
RR
275wxDataObject::wxDataObject()
276{
0d2a2b60
RR
277}
278
279wxDataObject::~wxDataObject()
280{
0d2a2b60
RR
281}
282
cd5bf2a6 283wxDataFormat &wxDataObject::GetFormat()
0d2a2b60 284{
cd5bf2a6
RR
285 return m_format;
286}
0d2a2b60 287
cd5bf2a6
RR
288wxDataType wxDataObject::GetFormatType() const
289{
290 return m_format.GetType();
291}
292
293wxString wxDataObject::GetFormatId() const
294{
295 return m_format.GetId();
0d2a2b60
RR
296}
297
cd5bf2a6
RR
298GdkAtom wxDataObject::GetFormatAtom() const
299{
06ad8636 300 GdkAtom ret = ((wxDataObject*) this)->m_format.GetAtom();
cd5bf2a6
RR
301 return ret;
302}
303
8b53e5a2
RR
304// ----------------------------------------------------------------------------
305// wxTextDataObject
306// ----------------------------------------------------------------------------
307
308IMPLEMENT_DYNAMIC_CLASS( wxTextDataObject, wxDataObject )
309
0d2a2b60
RR
310wxTextDataObject::wxTextDataObject()
311{
cd5bf2a6 312 m_format.SetType( wxDF_TEXT );
0d2a2b60
RR
313}
314
315wxTextDataObject::wxTextDataObject( const wxString& data )
316{
cd5bf2a6 317 m_format.SetType( wxDF_TEXT );
0d2a2b60
RR
318
319 m_data = data;
320}
321
322void wxTextDataObject::SetText( const wxString& data )
323{
324 m_data = data;
325}
326
327wxString wxTextDataObject::GetText() const
328{
329 return m_data;
330}
331
332void wxTextDataObject::WriteData( void *dest ) const
333{
334 WriteString( m_data, dest );
335}
336
337size_t wxTextDataObject::GetSize() const
338{
339 return m_data.Len() + 1;
340}
341
342void wxTextDataObject::WriteString( const wxString &str, void *dest ) const
343{
344 memcpy( dest, m_data.c_str(), GetSize() );
345}
346
8b53e5a2
RR
347// ----------------------------------------------------------------------------
348// wxFileDataObject
349// ----------------------------------------------------------------------------
350
351IMPLEMENT_DYNAMIC_CLASS( wxFileDataObject, wxDataObject )
352
cd5bf2a6 353wxFileDataObject::wxFileDataObject()
0d2a2b60 354{
cd5bf2a6 355 m_format.SetType( wxDF_FILENAME );
0d2a2b60
RR
356}
357
358void wxFileDataObject::AddFile( const wxString &file )
359{
360 m_files += file;
93c5dd39 361 m_files += (wxChar)0;
0d2a2b60
RR
362}
363
364wxString wxFileDataObject::GetFiles() const
365{
366 return m_files;
367}
368
369void wxFileDataObject::WriteData( void *dest ) const
370{
371 memcpy( dest, m_files.c_str(), GetSize() );
372}
373
374size_t wxFileDataObject::GetSize() const
375{
376 return m_files.Len() + 1;
377}
378
8b53e5a2
RR
379// ----------------------------------------------------------------------------
380// wxBitmapDataObject
381// ----------------------------------------------------------------------------
382
383IMPLEMENT_DYNAMIC_CLASS( wxBitmapDataObject, wxDataObject )
384
0d2a2b60
RR
385wxBitmapDataObject::wxBitmapDataObject()
386{
cd5bf2a6 387 m_format.SetType( wxDF_BITMAP );
0d2a2b60
RR
388}
389
390wxBitmapDataObject::wxBitmapDataObject( const wxBitmap& bitmap )
391{
cd5bf2a6 392 m_format.SetType( wxDF_BITMAP );
0d2a2b60
RR
393
394 m_bitmap = bitmap;
395}
396
397void wxBitmapDataObject::SetBitmap( const wxBitmap &bitmap )
398{
399 m_bitmap = bitmap;
400}
401
402wxBitmap wxBitmapDataObject::GetBitmap() const
403{
404 return m_bitmap;
405}
406
407void wxBitmapDataObject::WriteData( void *dest ) const
408{
409 WriteBitmap( m_bitmap, dest );
410}
411
412size_t wxBitmapDataObject::GetSize() const
413{
414 return 0;
415}
416
417void wxBitmapDataObject::WriteBitmap( const wxBitmap &bitmap, void *dest ) const
418{
419 memcpy( dest, m_bitmap.GetPixmap(), GetSize() );
420}
421
8b53e5a2
RR
422// ----------------------------------------------------------------------------
423// wxPrivateDataObject
424// ----------------------------------------------------------------------------
425
426IMPLEMENT_DYNAMIC_CLASS( wxPrivateDataObject, wxDataObject )
427
ab8884ac
RR
428wxPrivateDataObject::wxPrivateDataObject()
429{
b5be07d4 430 m_id = _T("application/");
0d2a2b60
RR
431 m_id += wxTheApp->GetAppName();
432
cd5bf2a6 433 m_format.SetId( m_id );
0d2a2b60 434
ab8884ac
RR
435 m_size = 0;
436 m_data = (char*) NULL;
ab8884ac
RR
437}
438
439wxPrivateDataObject::~wxPrivateDataObject()
440{
441 if (m_data) delete[] m_data;
442}
443
0d2a2b60
RR
444void wxPrivateDataObject::SetId( const wxString& id )
445{
446 m_id = id;
cd5bf2a6 447 m_format.SetId( m_id );
0d2a2b60
RR
448}
449
450wxString wxPrivateDataObject::GetId() const
451{
452 return m_id;
453}
454
8b53e5a2
RR
455void wxPrivateDataObject::SetData( const char *data, size_t size )
456{
457 m_size = size;
458
459 if (m_data) delete[] m_data;
460
461 m_data = new char[size];
462
463 memcpy( m_data, data, size );
464}
465
0d2a2b60
RR
466char* wxPrivateDataObject::GetData() const
467{
468 return m_data;
469}
470
471void wxPrivateDataObject::WriteData( void *dest ) const
472{
473 WriteData( m_data, dest );
474}
475
476size_t wxPrivateDataObject::GetSize() const
477{
478 return m_size;
479}
480
481void wxPrivateDataObject::WriteData( const char *data, void *dest ) const
482{
483 memcpy( dest, data, GetSize() );
484}
485