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;
29 GdkAtom g_fileAtom
= 0;
31 //-------------------------------------------------------------------------
33 //-------------------------------------------------------------------------
35 wxDataFormat::wxDataFormat()
38 m_type
= wxDF_INVALID
;
39 m_format
= (GdkAtom
) 0;
42 wxDataFormat::wxDataFormat( wxDataFormatId type
)
48 wxDataFormat::wxDataFormat( const wxChar
*id
)
54 wxDataFormat::wxDataFormat( const wxString
&id
)
60 wxDataFormat::wxDataFormat( NativeFormat format
)
66 void wxDataFormat::SetType( wxDataFormatId type
)
70 if (m_type
== wxDF_TEXT
)
71 m_format
= g_textAtom
;
73 if (m_type
== wxDF_BITMAP
)
76 if (m_type
== wxDF_FILENAME
)
77 m_format
= g_fileAtom
;
80 wxFAIL_MSG( wxT("invalid dataformat") );
84 wxDataFormatId
wxDataFormat::GetType() const
89 wxString
wxDataFormat::GetId() const
91 wxString
ret( gdk_atom_name( m_format
) ); // this will convert from ascii to Unicode
95 void wxDataFormat::SetId( NativeFormat format
)
99 if (m_format
== g_textAtom
)
102 if (m_format
== g_pngAtom
)
103 m_type
= wxDF_BITMAP
;
105 if (m_format
== g_fileAtom
)
106 m_type
= wxDF_FILENAME
;
108 m_type
= wxDF_PRIVATE
;
111 void wxDataFormat::SetId( const wxChar
*id
)
113 m_type
= wxDF_PRIVATE
;
115 m_format
= gdk_atom_intern( wxMBSTRINGCAST tmp
.mbc_str(), FALSE
); // what is the string cast for?
118 void wxDataFormat::PrepareFormats()
121 g_textAtom
= gdk_atom_intern( "STRING", FALSE
);
123 g_pngAtom
= gdk_atom_intern( "image/png", FALSE
);
125 g_fileAtom
= gdk_atom_intern( "file:ALL", FALSE
);
128 //-------------------------------------------------------------------------
130 //-------------------------------------------------------------------------
132 IMPLEMENT_ABSTRACT_CLASS( wxDataObject
, wxObject
)
134 wxDataObject::wxDataObject()
138 wxDataObject::~wxDataObject()
142 bool wxDataObject::IsSupportedFormat(const wxDataFormat
& format
) const
144 size_t nFormatCount
= GetFormatCount();
145 if ( nFormatCount
== 1 ) {
146 return format
== GetPreferredFormat();
149 wxDataFormat
*formats
= new wxDataFormat
[nFormatCount
];
150 GetAllFormats(formats
);
153 for ( n
= 0; n
< nFormatCount
; n
++ ) {
154 if ( formats
[n
] == format
)
161 return n
< nFormatCount
;
166 // ----------------------------------------------------------------------------
168 // ----------------------------------------------------------------------------
170 IMPLEMENT_DYNAMIC_CLASS( wxTextDataObject
, wxDataObject
)
172 wxTextDataObject::wxTextDataObject()
176 wxTextDataObject::wxTextDataObject(const wxString
& strText
)
181 size_t wxTextDataObject::GetDataSize(const wxDataFormat
& format
) const
183 return m_strText
.Len() + 1; // +1 for trailing '\0'of course
186 bool wxTextDataObject::GetDataHere(const wxDataFormat
& format
, void *buf
) const
188 memcpy(buf
, m_strText
.c_str(), GetDataSize(format
));
192 bool wxTextDataObject::SetData(const wxDataFormat
& format
, const void *buf
)
194 m_strText
= (const wxChar
*)buf
;
198 // ----------------------------------------------------------------------------
200 // ----------------------------------------------------------------------------
202 IMPLEMENT_DYNAMIC_CLASS( wxFileDataObject
, wxDataObject
)
204 wxFileDataObject::wxFileDataObject()
208 void wxFileDataObject::AddFile( const wxString
&file
)
211 m_files
+= (wxChar
)0;
214 wxString
wxFileDataObject::GetFiles() const
219 bool wxFileDataObject::GetDataHere(const wxDataFormat
& format
, void *buf
) const
221 if (format
== wxDF_FILENAME
)
223 memcpy( buf
, m_files
.mbc_str(), m_files
.Len() + 1 );
230 size_t wxFileDataObject::GetDataSize(const wxDataFormat
& format
) const
232 if (format
!= wxDF_FILENAME
) return 0;
234 return m_files
.Len() + 1;
237 bool wxFileDataObject::SetData(const wxDataFormat
& format
, const void *buf
)
239 if (format
!= wxDF_FILENAME
)
242 m_files
= (char*)(buf
); // this is so ugly, I cannot look at it
247 // ----------------------------------------------------------------------------
248 // wxBitmapDataObject
249 // ----------------------------------------------------------------------------
251 IMPLEMENT_DYNAMIC_CLASS( wxBitmapDataObject
, wxDataObject
)
253 wxBitmapDataObject::wxBitmapDataObject()
255 m_pngData
= (void*)NULL
;
259 wxBitmapDataObject::wxBitmapDataObject( const wxBitmap
& bitmap
)
261 m_pngData
= (void*)NULL
;
267 wxBitmapDataObject::~wxBitmapDataObject()
273 void wxBitmapDataObject::SetBitmap( const wxBitmap
&bitmap
)
277 m_pngData
= (void*)NULL
;
284 size_t wxBitmapDataObject::GetDataSize(const wxDataFormat
& format
) const
286 if (format
!= wxDF_BITMAP
) return 0;
291 bool wxBitmapDataObject::GetDataHere(const wxDataFormat
& format
, void *buf
) const
293 if (format
!= wxDF_BITMAP
) return FALSE
;
297 memcpy(buf
, m_pngData
, m_pngSize
);
304 bool wxBitmapDataObject::SetData(const wxDataFormat
& format
, const void *buf
)
306 if (m_pngData
) delete[] m_pngData
;
307 m_pngData
= (void*) NULL
;
309 m_bitmap
= wxNullBitmap
;
314 void wxBitmapDataObject::SetPngData(const void *buf
, size_t size
)
316 if (m_pngData
) delete[] m_pngData
;
318 m_pngData
= (void*) new char[m_pngSize
];
320 memcpy( m_pngData
, buf
, m_pngSize
);
322 wxMemoryInputStream
mstream( (char*) m_pngData
, m_pngSize
);
324 wxPNGHandler handler
;
325 handler
.LoadFile( &image
, mstream
);
326 m_bitmap
= image
.ConvertToBitmap();
329 void wxBitmapDataObject::DoConvertToPng()
331 if (!m_bitmap
.Ok()) return;
333 wxImage
image( m_bitmap
);
334 wxPNGHandler handler
;
336 wxCountingOutputStream count
;
337 handler
.SaveFile( &image
, count
);
338 m_pngSize
= count
.GetSize() + 100; // sometimes the size seems to vary ???
339 m_pngData
= (void*) new char[m_pngSize
];
341 wxMemoryOutputStream
mstream( (char*) m_pngData
, m_pngSize
);
342 handler
.SaveFile( &image
, mstream
);
345 // ----------------------------------------------------------------------------
346 // wxPrivateDataObject
347 // ----------------------------------------------------------------------------
349 IMPLEMENT_CLASS( wxPrivateDataObject
, wxDataObject
)
351 void wxPrivateDataObject::Free()
357 wxPrivateDataObject::wxPrivateDataObject()
359 wxString id
= wxT("application/");
360 id
+= wxTheApp
->GetAppName();
362 m_format
.SetId( id
);
365 m_data
= (void *)NULL
;
368 void wxPrivateDataObject::SetData( const void *data
, size_t size
)
373 m_data
= malloc(size
);
375 memcpy( m_data
, data
, size
);
378 void wxPrivateDataObject::WriteData( void *dest
) const
380 WriteData( m_data
, dest
);
383 size_t wxPrivateDataObject::GetSize() const
388 void wxPrivateDataObject::WriteData( const void *data
, void *dest
) const
390 memcpy( dest
, data
, GetSize() );