]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/dataobj.cpp
831a9122f1b973c2af9a22bd72a791597846d74c
[wxWidgets.git] / src / gtk1 / dataobj.cpp
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"
15 #include "wx/app.h"
16 #include "wx/debug.h"
17
18 #include "gdk/gdk.h"
19
20
21 //-------------------------------------------------------------------------
22 // global data
23 //-------------------------------------------------------------------------
24
25 GdkAtom g_textAtom = 0;
26
27 //-------------------------------------------------------------------------
28 // wxDataFormat
29 //-------------------------------------------------------------------------
30
31 IMPLEMENT_CLASS(wxDataFormat, wxObject)
32
33 wxDataFormat::wxDataFormat()
34 {
35 if (!g_textAtom) g_textAtom = gdk_atom_intern( "text/plain", FALSE );
36 m_type = wxDF_INVALID;
37 m_hasAtom = FALSE;
38 m_atom = (GdkAtom) 0;
39 }
40
41 wxDataFormat::wxDataFormat( wxDataType type )
42 {
43 if (!g_textAtom) g_textAtom = gdk_atom_intern( "text/plain", FALSE );
44 SetType( type );
45 }
46
47 wxDataFormat::wxDataFormat( const wxString &id )
48 {
49 if (!g_textAtom) g_textAtom = gdk_atom_intern( "text/plain", FALSE );
50 SetId( id );
51 }
52
53 wxDataFormat::wxDataFormat( wxDataFormat &format )
54 {
55 if (!g_textAtom) g_textAtom = gdk_atom_intern( "text/plain", FALSE );
56 m_type = format.GetType();
57 m_id = format.GetId();
58 m_hasAtom = TRUE;
59 m_atom = format.GetAtom();
60 }
61
62 wxDataFormat::wxDataFormat( const GdkAtom atom )
63 {
64 if (!g_textAtom) g_textAtom = gdk_atom_intern( "text/plain", FALSE );
65 m_hasAtom = TRUE;
66
67 m_atom = atom;
68
69 if (m_atom == g_textAtom)
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
81 if (m_id == "file:ALL")
82 {
83 m_type = wxDF_FILENAME;
84 }
85 }
86 }
87
88 void wxDataFormat::SetType( wxDataType type )
89 {
90 m_type = type;
91
92 if (m_type == wxDF_TEXT)
93 {
94 m_id = "text/plain";
95 }
96 else
97 if (m_type == wxDF_BITMAP)
98 {
99 m_id = "BITMAP";
100 }
101 else
102 if (m_type == wxDF_FILENAME)
103 {
104 m_id = "file:ALL";
105 }
106 else
107 {
108 wxFAIL_MSG( "invalid dataformat" );
109 }
110
111 m_hasAtom = FALSE;
112 }
113
114 wxDataType wxDataFormat::GetType() const
115 {
116 return m_type;
117 }
118
119 wxString wxDataFormat::GetId() const
120 {
121 return m_id;
122 }
123
124 void wxDataFormat::SetId( const wxString &id )
125 {
126 m_type = wxDF_PRIVATE;
127 m_id = id;
128 m_hasAtom = FALSE;
129 }
130
131 GdkAtom wxDataFormat::GetAtom()
132 {
133 if (!m_hasAtom)
134 {
135 m_hasAtom = TRUE;
136
137 if (m_type == wxDF_TEXT)
138 {
139 m_atom = g_textAtom;
140 }
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 {
149 m_atom = gdk_atom_intern( WXSTRINGCAST( m_id ), FALSE );
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
170 IMPLEMENT_CLASS(wxDataBroker,wxObject)
171
172 wxDataBroker::wxDataBroker()
173 {
174 m_dataObjects.DeleteContents(TRUE);
175 m_preferred = 0;
176 }
177
178 void wxDataBroker::Add( wxDataObject *dataObject, bool preferred )
179 {
180 if (preferred) m_preferred = m_dataObjects.GetCount();
181 m_dataObjects.Append( dataObject );
182 }
183
184 size_t wxDataBroker::GetFormatCount() const
185 {
186 return m_dataObjects.GetCount();
187 }
188
189 wxDataFormat &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
200 wxDataFormat &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
211 bool 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
229 size_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
247 void 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 {
256 dobj->WriteData( dest );
257 }
258
259 node = node->Next();
260 }
261 }
262
263 //-------------------------------------------------------------------------
264 // wxDataObject
265 //-------------------------------------------------------------------------
266
267 IMPLEMENT_ABSTRACT_CLASS( wxDataObject, wxObject )
268
269 wxDataObject::wxDataObject()
270 {
271 }
272
273 wxDataObject::~wxDataObject()
274 {
275 }
276
277 wxDataFormat &wxDataObject::GetFormat()
278 {
279 return m_format;
280 }
281
282 wxDataType wxDataObject::GetFormatType() const
283 {
284 return m_format.GetType();
285 }
286
287 wxString wxDataObject::GetFormatId() const
288 {
289 return m_format.GetId();
290 }
291
292 GdkAtom wxDataObject::GetFormatAtom() const
293 {
294 GdkAtom ret = ((wxDataObject*) this)->m_format.GetAtom();
295 return ret;
296 }
297
298 // ----------------------------------------------------------------------------
299 // wxTextDataObject
300 // ----------------------------------------------------------------------------
301
302 IMPLEMENT_DYNAMIC_CLASS( wxTextDataObject, wxDataObject )
303
304 wxTextDataObject::wxTextDataObject()
305 {
306 m_format.SetType( wxDF_TEXT );
307 }
308
309 wxTextDataObject::wxTextDataObject( const wxString& data )
310 {
311 m_format.SetType( wxDF_TEXT );
312
313 m_data = data;
314 }
315
316 void wxTextDataObject::SetText( const wxString& data )
317 {
318 m_data = data;
319 }
320
321 wxString wxTextDataObject::GetText() const
322 {
323 return m_data;
324 }
325
326 void wxTextDataObject::WriteData( void *dest ) const
327 {
328 WriteString( m_data, dest );
329 }
330
331 size_t wxTextDataObject::GetSize() const
332 {
333 return m_data.Len() + 1;
334 }
335
336 void wxTextDataObject::WriteString( const wxString &str, void *dest ) const
337 {
338 memcpy( dest, m_data.c_str(), GetSize() );
339 }
340
341 // ----------------------------------------------------------------------------
342 // wxFileDataObject
343 // ----------------------------------------------------------------------------
344
345 IMPLEMENT_DYNAMIC_CLASS( wxFileDataObject, wxDataObject )
346
347 wxFileDataObject::wxFileDataObject()
348 {
349 m_format.SetType( wxDF_FILENAME );
350 }
351
352 void wxFileDataObject::AddFile( const wxString &file )
353 {
354 m_files += file;
355 m_files += (char)0;
356 }
357
358 wxString wxFileDataObject::GetFiles() const
359 {
360 return m_files;
361 }
362
363 void wxFileDataObject::WriteData( void *dest ) const
364 {
365 memcpy( dest, m_files.c_str(), GetSize() );
366 }
367
368 size_t wxFileDataObject::GetSize() const
369 {
370 return m_files.Len() + 1;
371 }
372
373 // ----------------------------------------------------------------------------
374 // wxBitmapDataObject
375 // ----------------------------------------------------------------------------
376
377 IMPLEMENT_DYNAMIC_CLASS( wxBitmapDataObject, wxDataObject )
378
379 wxBitmapDataObject::wxBitmapDataObject()
380 {
381 m_format.SetType( wxDF_BITMAP );
382 }
383
384 wxBitmapDataObject::wxBitmapDataObject( const wxBitmap& bitmap )
385 {
386 m_format.SetType( wxDF_BITMAP );
387
388 m_bitmap = bitmap;
389 }
390
391 void wxBitmapDataObject::SetBitmap( const wxBitmap &bitmap )
392 {
393 m_bitmap = bitmap;
394 }
395
396 wxBitmap wxBitmapDataObject::GetBitmap() const
397 {
398 return m_bitmap;
399 }
400
401 void wxBitmapDataObject::WriteData( void *dest ) const
402 {
403 WriteBitmap( m_bitmap, dest );
404 }
405
406 size_t wxBitmapDataObject::GetSize() const
407 {
408 return 0;
409 }
410
411 void wxBitmapDataObject::WriteBitmap( const wxBitmap &bitmap, void *dest ) const
412 {
413 memcpy( dest, m_bitmap.GetPixmap(), GetSize() );
414 }
415
416 // ----------------------------------------------------------------------------
417 // wxPrivateDataObject
418 // ----------------------------------------------------------------------------
419
420 IMPLEMENT_DYNAMIC_CLASS( wxPrivateDataObject, wxDataObject )
421
422 wxPrivateDataObject::wxPrivateDataObject()
423 {
424 m_id = "application/";
425 m_id += wxTheApp->GetAppName();
426
427 m_format.SetId( m_id );
428
429 m_size = 0;
430 m_data = (char*) NULL;
431 }
432
433 wxPrivateDataObject::~wxPrivateDataObject()
434 {
435 if (m_data) delete[] m_data;
436 }
437
438 void wxPrivateDataObject::SetId( const wxString& id )
439 {
440 m_id = id;
441 m_format.SetId( m_id );
442 }
443
444 wxString wxPrivateDataObject::GetId() const
445 {
446 return m_id;
447 }
448
449 void 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
460 char* wxPrivateDataObject::GetData() const
461 {
462 return m_data;
463 }
464
465 void wxPrivateDataObject::WriteData( void *dest ) const
466 {
467 WriteData( m_data, dest );
468 }
469
470 size_t wxPrivateDataObject::GetSize() const
471 {
472 return m_size;
473 }
474
475 void wxPrivateDataObject::WriteData( const char *data, void *dest ) const
476 {
477 memcpy( dest, data, GetSize() );
478 }
479