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