]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/dataobj.cpp
wxMenu code clean up
[wxWidgets.git] / src / gtk / 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 // wxDataFormat
22 //-------------------------------------------------------------------------
23
24 IMPLEMENT_CLASS(wxDataFormat, wxObject)
25
26 wxDataFormat::wxDataFormat()
27 {
28 m_type = wxDF_INVALID;
29 m_hasAtom = FALSE;
30 m_atom = (GdkAtom) 0;
31 }
32
33 wxDataFormat::wxDataFormat( wxDataType type )
34 {
35 SetType( type );
36 }
37
38 wxDataFormat::wxDataFormat( const wxString &id )
39 {
40 SetId( id );
41 }
42
43 wxDataFormat::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
51 wxDataFormat::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
76 void 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
102 wxDataType wxDataFormat::GetType() const
103 {
104 return m_type;
105 }
106
107 wxString wxDataFormat::GetId() const
108 {
109 return m_id;
110 }
111
112 void wxDataFormat::SetId( const wxString &id )
113 {
114 m_type = wxDF_PRIVATE;
115 m_id = id;
116 m_hasAtom = FALSE;
117 }
118
119 GdkAtom 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
158 IMPLEMENT_CLASS(wxDataBroker,wxObject)
159
160 wxDataBroker::wxDataBroker()
161 {
162 m_dataObjects.DeleteContents(TRUE);
163 m_preferred = 0;
164 }
165
166 void wxDataBroker::Add( wxDataObject *dataObject, bool preferred )
167 {
168 if (preferred) m_preferred = m_dataObjects.GetCount();
169 m_dataObjects.Append( dataObject );
170 }
171
172 size_t wxDataBroker::GetFormatCount() const
173 {
174 return m_dataObjects.GetCount();
175 }
176
177 wxDataFormat &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
188 wxDataFormat &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
199 bool 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
217 size_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
235 void 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 {
244 dobj->WriteData( dest );
245 }
246
247 node = node->Next();
248 }
249 }
250
251 //-------------------------------------------------------------------------
252 // wxDataObject
253 //-------------------------------------------------------------------------
254
255 IMPLEMENT_ABSTRACT_CLASS( wxDataObject, wxObject )
256
257 wxDataObject::wxDataObject()
258 {
259 }
260
261 wxDataObject::~wxDataObject()
262 {
263 }
264
265 wxDataFormat &wxDataObject::GetFormat()
266 {
267 return m_format;
268 }
269
270 wxDataType wxDataObject::GetFormatType() const
271 {
272 return m_format.GetType();
273 }
274
275 wxString wxDataObject::GetFormatId() const
276 {
277 return m_format.GetId();
278 }
279
280 GdkAtom wxDataObject::GetFormatAtom() const
281 {
282 GdkAtom ret = ((wxDataObject*) this)->m_format.GetAtom();
283 return ret;
284 }
285
286 // ----------------------------------------------------------------------------
287 // wxTextDataObject
288 // ----------------------------------------------------------------------------
289
290 IMPLEMENT_DYNAMIC_CLASS( wxTextDataObject, wxDataObject )
291
292 wxTextDataObject::wxTextDataObject()
293 {
294 m_format.SetType( wxDF_TEXT );
295 }
296
297 wxTextDataObject::wxTextDataObject( const wxString& data )
298 {
299 m_format.SetType( wxDF_TEXT );
300
301 m_data = data;
302 }
303
304 void wxTextDataObject::SetText( const wxString& data )
305 {
306 m_data = data;
307 }
308
309 wxString wxTextDataObject::GetText() const
310 {
311 return m_data;
312 }
313
314 void wxTextDataObject::WriteData( void *dest ) const
315 {
316 WriteString( m_data, dest );
317 }
318
319 size_t wxTextDataObject::GetSize() const
320 {
321 return m_data.Len() + 1;
322 }
323
324 void wxTextDataObject::WriteString( const wxString &str, void *dest ) const
325 {
326 memcpy( dest, m_data.c_str(), GetSize() );
327 }
328
329 // ----------------------------------------------------------------------------
330 // wxFileDataObject
331 // ----------------------------------------------------------------------------
332
333 IMPLEMENT_DYNAMIC_CLASS( wxFileDataObject, wxDataObject )
334
335 wxFileDataObject::wxFileDataObject()
336 {
337 m_format.SetType( wxDF_FILENAME );
338 }
339
340 void wxFileDataObject::AddFile( const wxString &file )
341 {
342 m_files += file;
343 m_files += (char)0;
344 }
345
346 wxString wxFileDataObject::GetFiles() const
347 {
348 return m_files;
349 }
350
351 void wxFileDataObject::WriteData( void *dest ) const
352 {
353 memcpy( dest, m_files.c_str(), GetSize() );
354 }
355
356 size_t wxFileDataObject::GetSize() const
357 {
358 return m_files.Len() + 1;
359 }
360
361 // ----------------------------------------------------------------------------
362 // wxBitmapDataObject
363 // ----------------------------------------------------------------------------
364
365 IMPLEMENT_DYNAMIC_CLASS( wxBitmapDataObject, wxDataObject )
366
367 wxBitmapDataObject::wxBitmapDataObject()
368 {
369 m_format.SetType( wxDF_BITMAP );
370 }
371
372 wxBitmapDataObject::wxBitmapDataObject( const wxBitmap& bitmap )
373 {
374 m_format.SetType( wxDF_BITMAP );
375
376 m_bitmap = bitmap;
377 }
378
379 void wxBitmapDataObject::SetBitmap( const wxBitmap &bitmap )
380 {
381 m_bitmap = bitmap;
382 }
383
384 wxBitmap wxBitmapDataObject::GetBitmap() const
385 {
386 return m_bitmap;
387 }
388
389 void wxBitmapDataObject::WriteData( void *dest ) const
390 {
391 WriteBitmap( m_bitmap, dest );
392 }
393
394 size_t wxBitmapDataObject::GetSize() const
395 {
396 return 0;
397 }
398
399 void wxBitmapDataObject::WriteBitmap( const wxBitmap &bitmap, void *dest ) const
400 {
401 memcpy( dest, m_bitmap.GetPixmap(), GetSize() );
402 }
403
404 // ----------------------------------------------------------------------------
405 // wxPrivateDataObject
406 // ----------------------------------------------------------------------------
407
408 IMPLEMENT_DYNAMIC_CLASS( wxPrivateDataObject, wxDataObject )
409
410 wxPrivateDataObject::wxPrivateDataObject()
411 {
412 m_id = "application/";
413 m_id += wxTheApp->GetAppName();
414
415 m_format.SetId( m_id );
416
417 m_size = 0;
418 m_data = (char*) NULL;
419 }
420
421 wxPrivateDataObject::~wxPrivateDataObject()
422 {
423 if (m_data) delete[] m_data;
424 }
425
426 void wxPrivateDataObject::SetId( const wxString& id )
427 {
428 m_id = id;
429 m_format.SetId( m_id );
430 }
431
432 wxString wxPrivateDataObject::GetId() const
433 {
434 return m_id;
435 }
436
437 void 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
448 char* wxPrivateDataObject::GetData() const
449 {
450 return m_data;
451 }
452
453 void wxPrivateDataObject::WriteData( void *dest ) const
454 {
455 WriteData( m_data, dest );
456 }
457
458 size_t wxPrivateDataObject::GetSize() const
459 {
460 return m_size;
461 }
462
463 void wxPrivateDataObject::WriteData( const char *data, void *dest ) const
464 {
465 memcpy( dest, data, GetSize() );
466 }
467