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()
37 m_type
= wxDF_INVALID
;
38 m_format
= (GdkAtom
) 0;
41 wxDataFormat::wxDataFormat( wxDataFormatId type
)
47 wxDataFormat::wxDataFormat( const wxChar
*id
)
53 wxDataFormat::wxDataFormat( const wxString
&id
)
59 wxDataFormat::wxDataFormat( NativeFormat format
)
65 void wxDataFormat::SetType( wxDataFormatId type
)
69 if (m_type
== wxDF_TEXT
)
70 m_format
= g_textAtom
;
72 if (m_type
== wxDF_BITMAP
)
75 if (m_type
== wxDF_FILENAME
)
76 m_format
= g_fileAtom
;
79 wxFAIL_MSG( wxT("invalid dataformat") );
83 wxDataFormatId
wxDataFormat::GetType() const
88 wxString
wxDataFormat::GetId() const
90 wxString
ret( gdk_atom_name( m_format
) ); // this will convert from ascii to Unicode
94 void wxDataFormat::SetId( NativeFormat format
)
98 if (m_format
== g_textAtom
)
101 if (m_format
== g_pngAtom
)
102 m_type
= wxDF_BITMAP
;
104 if (m_format
== g_fileAtom
)
105 m_type
= wxDF_FILENAME
;
107 m_type
= wxDF_PRIVATE
;
110 void wxDataFormat::SetId( const wxChar
*id
)
112 m_type
= wxDF_PRIVATE
;
114 m_format
= gdk_atom_intern( wxMBSTRINGCAST tmp
.mbc_str(), FALSE
); // what is the string cast for?
117 void wxDataFormat::PrepareFormats()
120 g_textAtom
= gdk_atom_intern( "STRING", FALSE
);
122 g_pngAtom
= gdk_atom_intern( "image/png", FALSE
);
124 g_fileAtom
= gdk_atom_intern( "file:ALL", FALSE
);
127 //-------------------------------------------------------------------------
129 //-------------------------------------------------------------------------
131 wxDataObject::wxDataObject()
135 bool wxDataObject::IsSupportedFormat(const wxDataFormat
& format
, Direction dir
) const
137 size_t nFormatCount
= GetFormatCount(dir
);
138 if ( nFormatCount
== 1 ) {
139 return format
== GetPreferredFormat();
142 wxDataFormat
*formats
= new wxDataFormat
[nFormatCount
];
143 GetAllFormats(formats
,dir
);
146 for ( n
= 0; n
< nFormatCount
; n
++ ) {
147 if ( formats
[n
] == format
)
154 return n
< nFormatCount
;
158 // ----------------------------------------------------------------------------
160 // ----------------------------------------------------------------------------
162 bool wxFileDataObject::GetDataHere(void *buf
) const
166 for (size_t i
= 0; i
< m_filenames
.GetCount(); i
++)
168 filenames
+= m_filenames
[i
];
169 filenames
+= (wxChar
) 0;
172 memcpy( buf
, filenames
.mbc_str(), filenames
.Len() + 1 );
177 size_t wxFileDataObject::GetDataSize() const
181 for (size_t i
= 0; i
< m_filenames
.GetCount(); i
++)
183 res
+= m_filenames
[i
].Len();
190 bool wxFileDataObject::SetData(size_t WXUNUSED(size
), const void *buf
)
194 wxString
file( (const char *)buf
); /* char, not wxChar */
201 void wxFileDataObject::AddFile( const wxString
&filename
)
203 m_filenames
.Add( filename
);
206 // ----------------------------------------------------------------------------
207 // wxBitmapDataObject
208 // ----------------------------------------------------------------------------
210 wxBitmapDataObject::wxBitmapDataObject()
215 wxBitmapDataObject::wxBitmapDataObject( const wxBitmap
& bitmap
)
216 : wxBitmapDataObjectBase(bitmap
)
223 wxBitmapDataObject::~wxBitmapDataObject()
228 void wxBitmapDataObject::SetBitmap( const wxBitmap
&bitmap
)
232 wxBitmapDataObjectBase::SetBitmap(bitmap
);
237 bool wxBitmapDataObject::GetDataHere(void *buf
) const
241 wxFAIL_MSG( wxT("attempt to copy empty bitmap failed") );
246 memcpy(buf
, m_pngData
, m_pngSize
);
251 bool wxBitmapDataObject::SetData(size_t size
, const void *buf
)
256 m_pngData
= malloc(m_pngSize
);
258 memcpy( m_pngData
, buf
, m_pngSize
);
260 wxMemoryInputStream
mstream( (char*) m_pngData
, m_pngSize
);
262 wxPNGHandler handler
;
263 if ( !handler
.LoadFile( &image
, mstream
) )
268 m_bitmap
= image
.ConvertToBitmap();
270 return m_bitmap
.Ok();
273 void wxBitmapDataObject::DoConvertToPng()
278 wxImage
image( m_bitmap
);
279 wxPNGHandler handler
;
281 wxCountingOutputStream count
;
282 handler
.SaveFile( &image
, count
);
284 m_pngSize
= count
.GetSize() + 100; // sometimes the size seems to vary ???
285 m_pngData
= malloc(m_pngSize
);
287 wxMemoryOutputStream
mstream( (char*) m_pngData
, m_pngSize
);
288 handler
.SaveFile( &image
, mstream
);