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"
17 #include "wx/mstream.h"
23 //-------------------------------------------------------------------------
25 //-------------------------------------------------------------------------
27 GdkAtom g_textAtom
= 0;
28 GdkAtom g_pngAtom
= 0;
30 //-------------------------------------------------------------------------
32 //-------------------------------------------------------------------------
34 IMPLEMENT_CLASS(wxDataFormat
, wxObject
)
36 wxDataFormat::wxDataFormat()
39 m_type
= wxDF_INVALID
;
44 wxDataFormat::wxDataFormat( wxDataFormatId type
)
50 wxDataFormat::wxDataFormat( const wxChar
*id
)
56 wxDataFormat::wxDataFormat( const wxString
&id
)
62 wxDataFormat::wxDataFormat( const wxDataFormat
&format
)
65 m_type
= format
.GetType();
66 m_id
= format
.GetId();
68 m_atom
= ((wxDataFormat
&)format
).GetAtom(); // const_cast
71 wxDataFormat::wxDataFormat( const GdkAtom atom
)
78 if (m_atom
== g_textAtom
)
82 if (m_atom
== GDK_TARGET_BITMAP
)
87 m_type
= wxDF_PRIVATE
;
88 m_id
= gdk_atom_name( m_atom
);
90 if (m_id
== T("file:ALL"))
92 m_type
= wxDF_FILENAME
;
97 void wxDataFormat::SetType( wxDataFormatId type
)
101 if (m_type
== wxDF_TEXT
)
106 if (m_type
== wxDF_BITMAP
)
108 m_id
= T("image/png");
111 if (m_type
== wxDF_FILENAME
)
113 m_id
= T("file:ALL");
117 wxFAIL_MSG( T("invalid dataformat") );
123 wxDataFormatId
wxDataFormat::GetType() const
128 wxString
wxDataFormat::GetId() const
133 void wxDataFormat::SetId( const wxChar
*id
)
135 m_type
= wxDF_PRIVATE
;
140 GdkAtom
wxDataFormat::GetAtom()
146 if (m_type
== wxDF_TEXT
)
151 if (m_type
== wxDF_BITMAP
)
153 m_atom
= GDK_TARGET_BITMAP
;
156 if (m_type
== wxDF_PRIVATE
)
158 m_atom
= gdk_atom_intern( wxMBSTRINGCAST m_id
.mbc_str(), FALSE
);
161 if (m_type
== wxDF_FILENAME
)
163 m_atom
= gdk_atom_intern( "file:ALL", FALSE
);
168 m_atom
= (GdkAtom
) 0;
175 void wxDataFormat::PrepareFormats()
177 if (!g_textAtom
) g_textAtom
= gdk_atom_intern( "STRING", FALSE
);
178 if (!g_pngAtom
) g_pngAtom
= gdk_atom_intern( "image/png", FALSE
);
181 //-------------------------------------------------------------------------
183 //-------------------------------------------------------------------------
185 IMPLEMENT_CLASS(wxDataBroker
,wxObject
)
187 wxDataBroker::wxDataBroker()
189 m_dataObjects
.DeleteContents(TRUE
);
193 void wxDataBroker::Add( wxDataObject
*dataObject
, bool preferred
)
195 if (preferred
) m_preferred
= m_dataObjects
.GetCount();
196 m_dataObjects
.Append( dataObject
);
199 size_t wxDataBroker::GetFormatCount() const
201 return m_dataObjects
.GetCount();
204 wxDataFormatId
wxDataBroker::GetPreferredFormat() const
206 wxNode
*node
= m_dataObjects
.Nth( m_preferred
);
210 wxDataObject
* data_obj
= (wxDataObject
*)node
->Data();
212 return data_obj
->GetFormat().GetType();
215 wxDataFormat
&wxDataBroker::GetNthFormat( size_t nth
) const
217 wxNode
*node
= m_dataObjects
.Nth( nth
);
221 wxDataObject
* data_obj
= (wxDataObject
*)node
->Data();
223 return data_obj
->GetFormat();
226 bool wxDataBroker::IsSupportedFormat( wxDataFormat
&format
) const
228 wxNode
*node
= m_dataObjects
.First();
231 wxDataObject
*dobj
= (wxDataObject
*)node
->Data();
233 if (dobj
->GetFormat().GetAtom() == format
.GetAtom())
244 size_t wxDataBroker::GetSize( wxDataFormat
& format
) const
246 wxNode
*node
= m_dataObjects
.First();
249 wxDataObject
*dobj
= (wxDataObject
*)node
->Data();
251 if (dobj
->GetFormat().GetAtom() == format
.GetAtom())
253 return dobj
->GetSize();
262 void wxDataBroker::WriteData( wxDataFormat
& format
, void *dest
) const
264 wxNode
*node
= m_dataObjects
.First();
267 wxDataObject
*dobj
= (wxDataObject
*)node
->Data();
269 if (dobj
->GetFormat().GetAtom() == format
.GetAtom())
271 dobj
->WriteData( dest
);
278 //-------------------------------------------------------------------------
280 //-------------------------------------------------------------------------
282 IMPLEMENT_ABSTRACT_CLASS( wxDataObject
, wxObject
)
284 wxDataObject::wxDataObject()
288 wxDataObject::~wxDataObject()
292 wxDataFormat
&wxDataObject::GetFormat()
297 wxDataFormatId
wxDataObject::GetFormatType() const
299 return m_format
.GetType();
302 wxString
wxDataObject::GetFormatId() const
304 return m_format
.GetId();
307 GdkAtom
wxDataObject::GetFormatAtom() const
309 GdkAtom ret
= ((wxDataObject
*) this)->m_format
.GetAtom();
313 // ----------------------------------------------------------------------------
315 // ----------------------------------------------------------------------------
317 IMPLEMENT_DYNAMIC_CLASS( wxTextDataObject
, wxDataObject
)
319 wxTextDataObject::wxTextDataObject()
321 m_format
.SetType( wxDF_TEXT
);
324 wxTextDataObject::wxTextDataObject( const wxString
& data
)
326 m_format
.SetType( wxDF_TEXT
);
331 void wxTextDataObject::SetText( const wxString
& data
)
336 wxString
wxTextDataObject::GetText() const
341 void wxTextDataObject::WriteData( void *dest
) const
343 WriteString( m_data
, dest
);
346 size_t wxTextDataObject::GetSize() const
348 return m_data
.Len() + 1;
351 void wxTextDataObject::WriteString( const wxString
&str
, void *dest
) const
353 memcpy( dest
, str
.mb_str(), str
.Len()+1 );
356 // ----------------------------------------------------------------------------
358 // ----------------------------------------------------------------------------
360 IMPLEMENT_DYNAMIC_CLASS( wxFileDataObject
, wxDataObject
)
362 wxFileDataObject::wxFileDataObject()
364 m_format
.SetType( wxDF_FILENAME
);
367 void wxFileDataObject::AddFile( const wxString
&file
)
370 m_files
+= (wxChar
)0;
373 wxString
wxFileDataObject::GetFiles() const
378 void wxFileDataObject::WriteData( void *dest
) const
380 memcpy( dest
, m_files
.mbc_str(), GetSize() );
383 size_t wxFileDataObject::GetSize() const
385 return m_files
.Len() + 1;
388 // ----------------------------------------------------------------------------
389 // wxBitmapDataObject
390 // ----------------------------------------------------------------------------
392 IMPLEMENT_DYNAMIC_CLASS( wxBitmapDataObject
, wxDataObject
)
394 wxBitmapDataObject::wxBitmapDataObject()
396 m_format
.SetType( wxDF_BITMAP
);
397 m_pngData
= (char*)NULL
;
401 wxBitmapDataObject::wxBitmapDataObject( const wxBitmap
& bitmap
)
403 m_format
.SetType( wxDF_BITMAP
);
404 m_pngData
= (char*)NULL
;
410 wxBitmapDataObject::~wxBitmapDataObject()
412 if (m_pngData
) delete[] m_pngData
;
415 void wxBitmapDataObject::SetBitmap( const wxBitmap
&bitmap
)
421 wxBitmap
wxBitmapDataObject::GetBitmap() const
426 void wxBitmapDataObject::WriteData( void *dest
) const
428 WriteBitmap( m_bitmap
, dest
);
431 size_t wxBitmapDataObject::GetSize() const
436 void wxBitmapDataObject::WriteBitmap( const wxBitmap
&WXUNUSED(bitmap
), void *dest
) const
438 // if (m_bitmap == bitmap)
439 memcpy( dest
, m_pngData
, m_pngSize
);
442 void wxBitmapDataObject::SetPngData( const char *pngData
, size_t pngSize
)
444 if (m_pngData
) delete[] m_pngData
;
445 m_pngData
= (char*) NULL
;
447 m_pngData
= new char[m_pngSize
];
448 memcpy( m_pngData
, pngData
, m_pngSize
);
450 wxMemoryInputStream
mstream( pngData
, pngSize
);
452 wxPNGHandler handler
;
453 handler
.LoadFile( &image
, mstream
);
454 m_bitmap
= image
.ConvertToBitmap();
457 void wxBitmapDataObject::DoConvertToPng()
459 if (m_pngData
) delete[] m_pngData
;
461 wxImage
image( m_bitmap
);
462 wxPNGHandler handler
;
464 wxCountingOutputStream count
;
465 handler
.SaveFile( &image
, count
);
466 m_pngSize
= count
.GetSize() + 100; // sometimes the size seems to vary ???
467 m_pngData
= new char[m_pngSize
];
469 wxMemoryOutputStream
mstream( m_pngData
, m_pngSize
);
470 handler
.SaveFile( &image
, mstream
);
473 // ----------------------------------------------------------------------------
474 // wxPrivateDataObject
475 // ----------------------------------------------------------------------------
477 IMPLEMENT_DYNAMIC_CLASS( wxPrivateDataObject
, wxDataObject
)
479 void wxPrivateDataObject::Free()
485 wxPrivateDataObject::wxPrivateDataObject()
487 wxString id
= T("application/");
488 id
+= wxTheApp
->GetAppName();
490 m_format
.SetId( id
);
493 m_data
= (void *)NULL
;
496 void wxPrivateDataObject::SetData( const void *data
, size_t size
)
501 m_data
= malloc(size
);
503 memcpy( m_data
, data
, size
);
506 void wxPrivateDataObject::WriteData( void *dest
) const
508 WriteData( m_data
, dest
);
511 size_t wxPrivateDataObject::GetSize() const
516 void wxPrivateDataObject::WriteData( const void *data
, void *dest
) const
518 memcpy( dest
, data
, GetSize() );