]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk1/dataobj.cpp
1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxDataObject class
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 ///////////////////////////////////////////////////////////////////////////////
11 #pragma implementation "dataobj.h"
14 #include "wx/dataobj.h"
21 //-------------------------------------------------------------------------
23 //-------------------------------------------------------------------------
25 GdkAtom g_textAtom
= 0;
27 //-------------------------------------------------------------------------
29 //-------------------------------------------------------------------------
31 IMPLEMENT_CLASS(wxDataFormat
, wxObject
)
33 wxDataFormat::wxDataFormat()
35 if (!g_textAtom
) g_textAtom
= gdk_atom_intern( "STRING", FALSE
);
36 m_type
= wxDF_INVALID
;
41 wxDataFormat::wxDataFormat( wxDataFormatId type
)
43 if (!g_textAtom
) g_textAtom
= gdk_atom_intern( "STRING", FALSE
);
47 wxDataFormat::wxDataFormat( const wxChar
*id
)
49 if (!g_textAtom
) g_textAtom
= gdk_atom_intern( "STRING", FALSE
);
53 wxDataFormat::wxDataFormat( const wxString
&id
)
55 if (!g_textAtom
) g_textAtom
= gdk_atom_intern( "STRING", FALSE
);
59 wxDataFormat::wxDataFormat( const wxDataFormat
&format
)
61 if (!g_textAtom
) g_textAtom
= gdk_atom_intern( "STRING", FALSE
);
62 m_type
= format
.GetType();
63 m_id
= format
.GetId();
65 m_atom
= ((wxDataFormat
&)format
).GetAtom(); // const_cast
68 wxDataFormat::wxDataFormat( const GdkAtom atom
)
70 if (!g_textAtom
) g_textAtom
= gdk_atom_intern( "STRING", FALSE
);
75 if (m_atom
== g_textAtom
)
79 if (m_atom
== GDK_TARGET_BITMAP
)
84 m_type
= wxDF_PRIVATE
;
85 m_id
= gdk_atom_name( m_atom
);
87 if (m_id
== _T("file:ALL"))
89 m_type
= wxDF_FILENAME
;
94 void wxDataFormat::SetType( wxDataFormatId type
)
98 if (m_type
== wxDF_TEXT
)
103 if (m_type
== wxDF_BITMAP
)
108 if (m_type
== wxDF_FILENAME
)
110 m_id
= _T("file:ALL");
114 wxFAIL_MSG( _T("invalid dataformat") );
120 wxDataFormatId
wxDataFormat::GetType() const
125 wxString
wxDataFormat::GetId() const
130 void wxDataFormat::SetId( const wxChar
*id
)
132 m_type
= wxDF_PRIVATE
;
137 GdkAtom
wxDataFormat::GetAtom()
143 if (m_type
== wxDF_TEXT
)
148 if (m_type
== wxDF_BITMAP
)
150 m_atom
= GDK_TARGET_BITMAP
;
153 if (m_type
== wxDF_PRIVATE
)
155 m_atom
= gdk_atom_intern( MBSTRINGCAST m_id
.mbc_str(), FALSE
);
158 if (m_type
== wxDF_FILENAME
)
160 m_atom
= gdk_atom_intern( "file:ALL", FALSE
);
165 m_atom
= (GdkAtom
) 0;
172 //-------------------------------------------------------------------------
174 //-------------------------------------------------------------------------
176 IMPLEMENT_CLASS(wxDataBroker
,wxObject
)
178 wxDataBroker::wxDataBroker()
180 m_dataObjects
.DeleteContents(TRUE
);
184 void wxDataBroker::Add( wxDataObject
*dataObject
, bool preferred
)
186 if (preferred
) m_preferred
= m_dataObjects
.GetCount();
187 m_dataObjects
.Append( dataObject
);
190 size_t wxDataBroker::GetFormatCount() const
192 return m_dataObjects
.GetCount();
195 wxDataFormatId
wxDataBroker::GetPreferredFormat() const
197 wxNode
*node
= m_dataObjects
.Nth( m_preferred
);
201 wxDataObject
* data_obj
= (wxDataObject
*)node
->Data();
203 return data_obj
->GetFormat().GetType();
206 wxDataFormat
&wxDataBroker::GetNthFormat( size_t nth
) const
208 wxNode
*node
= m_dataObjects
.Nth( nth
);
212 wxDataObject
* data_obj
= (wxDataObject
*)node
->Data();
214 return data_obj
->GetFormat();
217 bool wxDataBroker::IsSupportedFormat( wxDataFormat
&format
) const
219 wxNode
*node
= m_dataObjects
.First();
222 wxDataObject
*dobj
= (wxDataObject
*)node
->Data();
224 if (dobj
->GetFormat().GetAtom() == format
.GetAtom())
235 size_t wxDataBroker::GetSize( wxDataFormat
& format
) const
237 wxNode
*node
= m_dataObjects
.First();
240 wxDataObject
*dobj
= (wxDataObject
*)node
->Data();
242 if (dobj
->GetFormat().GetAtom() == format
.GetAtom())
244 return dobj
->GetSize();
253 void wxDataBroker::WriteData( wxDataFormat
& format
, void *dest
) const
255 wxNode
*node
= m_dataObjects
.First();
258 wxDataObject
*dobj
= (wxDataObject
*)node
->Data();
260 if (dobj
->GetFormat().GetAtom() == format
.GetAtom())
262 dobj
->WriteData( dest
);
269 //-------------------------------------------------------------------------
271 //-------------------------------------------------------------------------
273 IMPLEMENT_ABSTRACT_CLASS( wxDataObject
, wxObject
)
275 wxDataObject::wxDataObject()
279 wxDataObject::~wxDataObject()
283 wxDataFormat
&wxDataObject::GetFormat()
288 wxDataFormatId
wxDataObject::GetFormatType() const
290 return m_format
.GetType();
293 wxString
wxDataObject::GetFormatId() const
295 return m_format
.GetId();
298 GdkAtom
wxDataObject::GetFormatAtom() const
300 GdkAtom ret
= ((wxDataObject
*) this)->m_format
.GetAtom();
304 // ----------------------------------------------------------------------------
306 // ----------------------------------------------------------------------------
308 IMPLEMENT_DYNAMIC_CLASS( wxTextDataObject
, wxDataObject
)
310 wxTextDataObject::wxTextDataObject()
312 m_format
.SetType( wxDF_TEXT
);
315 wxTextDataObject::wxTextDataObject( const wxString
& data
)
317 m_format
.SetType( wxDF_TEXT
);
322 void wxTextDataObject::SetText( const wxString
& data
)
327 wxString
wxTextDataObject::GetText() const
332 void wxTextDataObject::WriteData( void *dest
) const
334 WriteString( m_data
, dest
);
337 size_t wxTextDataObject::GetSize() const
339 return m_data
.Len() + 1;
342 void wxTextDataObject::WriteString( const wxString
&str
, void *dest
) const
344 memcpy( dest
, str
.mb_str(), str
.Len()+1 );
347 // ----------------------------------------------------------------------------
349 // ----------------------------------------------------------------------------
351 IMPLEMENT_DYNAMIC_CLASS( wxFileDataObject
, wxDataObject
)
353 wxFileDataObject::wxFileDataObject()
355 m_format
.SetType( wxDF_FILENAME
);
358 void wxFileDataObject::AddFile( const wxString
&file
)
361 m_files
+= (wxChar
)0;
364 wxString
wxFileDataObject::GetFiles() const
369 void wxFileDataObject::WriteData( void *dest
) const
371 memcpy( dest
, m_files
.mbc_str(), GetSize() );
374 size_t wxFileDataObject::GetSize() const
376 return m_files
.Len() + 1;
379 // ----------------------------------------------------------------------------
380 // wxBitmapDataObject
381 // ----------------------------------------------------------------------------
383 IMPLEMENT_DYNAMIC_CLASS( wxBitmapDataObject
, wxDataObject
)
385 wxBitmapDataObject::wxBitmapDataObject()
387 m_format
.SetType( wxDF_BITMAP
);
390 wxBitmapDataObject::wxBitmapDataObject( const wxBitmap
& bitmap
)
392 m_format
.SetType( wxDF_BITMAP
);
397 void wxBitmapDataObject::SetBitmap( const wxBitmap
&bitmap
)
402 wxBitmap
wxBitmapDataObject::GetBitmap() const
407 void wxBitmapDataObject::WriteData( void *dest
) const
409 WriteBitmap( m_bitmap
, dest
);
412 size_t wxBitmapDataObject::GetSize() const
417 void wxBitmapDataObject::WriteBitmap( const wxBitmap
&bitmap
, void *dest
) const
419 memcpy( dest
, m_bitmap
.GetPixmap(), GetSize() );
422 // ----------------------------------------------------------------------------
423 // wxPrivateDataObject
424 // ----------------------------------------------------------------------------
426 IMPLEMENT_DYNAMIC_CLASS( wxPrivateDataObject
, wxDataObject
)
428 void wxPrivateDataObject::Free()
434 wxPrivateDataObject::wxPrivateDataObject()
436 wxString id
= _T("application/");
437 id
+= wxTheApp
->GetAppName();
439 m_format
.SetId( id
);
442 m_data
= (void *)NULL
;
445 void wxPrivateDataObject::SetData( const void *data
, size_t size
)
450 m_data
= malloc(size
);
452 memcpy( m_data
, data
, size
);
455 void wxPrivateDataObject::WriteData( void *dest
) const
457 WriteData( m_data
, dest
);
460 size_t wxPrivateDataObject::GetSize() const
465 void wxPrivateDataObject::WriteData( const void *data
, void *dest
) const
467 memcpy( dest
, data
, GetSize() );