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"
22 //-------------------------------------------------------------------------
24 //-------------------------------------------------------------------------
26 GdkAtom g_textAtom
= 0;
27 GdkAtom g_pngAtom
= 0;
28 GdkAtom g_fileAtom
= 0;
30 //-------------------------------------------------------------------------
32 //-------------------------------------------------------------------------
34 wxDataFormat::wxDataFormat()
36 // do *not* call PrepareFormats() from here for 2 reasons:
38 // 1. we will have time to do it later because some other Set function
39 // must be called before we really need them
41 // 2. doing so prevents us from declaring global wxDataFormats because
42 // calling PrepareFormats (and thus gdk_atom_intern) before GDK is
43 // initialised will result in a crash
44 m_type
= wxDF_INVALID
;
45 m_format
= (GdkAtom
) 0;
48 wxDataFormat::wxDataFormat( wxDataFormatId type
)
54 wxDataFormat::wxDataFormat( const wxChar
*id
)
60 wxDataFormat::wxDataFormat( const wxString
&id
)
66 wxDataFormat::wxDataFormat( NativeFormat format
)
72 void wxDataFormat::SetType( wxDataFormatId type
)
77 if (m_type
== wxDF_TEXT
)
78 m_format
= g_textAtom
;
80 if (m_type
== wxDF_BITMAP
)
83 if (m_type
== wxDF_FILENAME
)
84 m_format
= g_fileAtom
;
87 wxFAIL_MSG( wxT("invalid dataformat") );
91 wxDataFormatId
wxDataFormat::GetType() const
96 wxString
wxDataFormat::GetId() const
98 wxString
ret( gdk_atom_name( m_format
) ); // this will convert from ascii to Unicode
102 void wxDataFormat::SetId( NativeFormat format
)
107 if (m_format
== g_textAtom
)
110 if (m_format
== g_pngAtom
)
111 m_type
= wxDF_BITMAP
;
113 if (m_format
== g_fileAtom
)
114 m_type
= wxDF_FILENAME
;
116 m_type
= wxDF_PRIVATE
;
119 void wxDataFormat::SetId( const wxChar
*id
)
122 m_type
= wxDF_PRIVATE
;
124 m_format
= gdk_atom_intern( wxMBSTRINGCAST tmp
.mbc_str(), FALSE
); // what is the string cast for?
127 void wxDataFormat::PrepareFormats()
130 g_textAtom
= gdk_atom_intern( "STRING", FALSE
);
132 g_pngAtom
= gdk_atom_intern( "image/png", FALSE
);
134 g_fileAtom
= gdk_atom_intern( "file:ALL", FALSE
);
137 //-------------------------------------------------------------------------
139 //-------------------------------------------------------------------------
141 wxDataObject::wxDataObject()
145 bool wxDataObject::IsSupportedFormat(const wxDataFormat
& format
, Direction dir
) const
147 size_t nFormatCount
= GetFormatCount(dir
);
148 if ( nFormatCount
== 1 ) {
149 return format
== GetPreferredFormat();
152 wxDataFormat
*formats
= new wxDataFormat
[nFormatCount
];
153 GetAllFormats(formats
,dir
);
156 for ( n
= 0; n
< nFormatCount
; n
++ ) {
157 if ( formats
[n
] == format
)
164 return n
< nFormatCount
;
168 // ----------------------------------------------------------------------------
170 // ----------------------------------------------------------------------------
172 bool wxFileDataObject::GetDataHere(void *buf
) const
176 for (size_t i
= 0; i
< m_filenames
.GetCount(); i
++)
178 filenames
+= m_filenames
[i
];
179 filenames
+= (wxChar
) 0;
182 memcpy( buf
, filenames
.mbc_str(), filenames
.Len() + 1 );
187 size_t wxFileDataObject::GetDataSize() const
191 for (size_t i
= 0; i
< m_filenames
.GetCount(); i
++)
193 res
+= m_filenames
[i
].Len();
200 bool wxFileDataObject::SetData(size_t WXUNUSED(size
), const void *buf
)
204 wxString
file( (const char *)buf
); /* char, not wxChar */
211 void wxFileDataObject::AddFile( const wxString
&filename
)
213 m_filenames
.Add( filename
);
216 // ----------------------------------------------------------------------------
217 // wxBitmapDataObject
218 // ----------------------------------------------------------------------------
220 wxBitmapDataObject::wxBitmapDataObject()
225 wxBitmapDataObject::wxBitmapDataObject( const wxBitmap
& bitmap
)
226 : wxBitmapDataObjectBase(bitmap
)
233 wxBitmapDataObject::~wxBitmapDataObject()
238 void wxBitmapDataObject::SetBitmap( const wxBitmap
&bitmap
)
242 wxBitmapDataObjectBase::SetBitmap(bitmap
);
247 bool wxBitmapDataObject::GetDataHere(void *buf
) const
251 wxFAIL_MSG( wxT("attempt to copy empty bitmap failed") );
256 memcpy(buf
, m_pngData
, m_pngSize
);
261 bool wxBitmapDataObject::SetData(size_t size
, const void *buf
)
266 m_pngData
= malloc(m_pngSize
);
268 memcpy( m_pngData
, buf
, m_pngSize
);
270 wxMemoryInputStream
mstream( (char*) m_pngData
, m_pngSize
);
272 wxPNGHandler handler
;
273 if ( !handler
.LoadFile( &image
, mstream
) )
278 m_bitmap
= image
.ConvertToBitmap();
280 return m_bitmap
.Ok();
283 void wxBitmapDataObject::DoConvertToPng()
288 wxImage
image( m_bitmap
);
289 wxPNGHandler handler
;
291 wxCountingOutputStream count
;
292 handler
.SaveFile( &image
, count
);
294 m_pngSize
= count
.GetSize() + 100; // sometimes the size seems to vary ???
295 m_pngData
= malloc(m_pngSize
);
297 wxMemoryOutputStream
mstream( (char*) m_pngData
, m_pngSize
);
298 handler
.SaveFile( &image
, mstream
);