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