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