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 IMPLEMENT_ABSTRACT_CLASS( wxDataObject
, wxObject
)
133 wxDataObject::wxDataObject()
137 wxDataObject::~wxDataObject()
141 bool wxDataObject::IsSupportedFormat(const wxDataFormat
& format
) const
143 size_t nFormatCount
= GetFormatCount();
144 if ( nFormatCount
== 1 ) {
145 return format
== GetPreferredFormat();
148 wxDataFormat
*formats
= new wxDataFormat
[nFormatCount
];
149 GetAllFormats(formats
);
152 for ( n
= 0; n
< nFormatCount
; n
++ ) {
153 if ( formats
[n
] == format
)
160 return n
< nFormatCount
;
164 // ----------------------------------------------------------------------------
166 // ----------------------------------------------------------------------------
168 bool wxFileDataObject::GetDataHere(void *buf
) const
170 const wxString
& filenames
= GetFilenames();
171 memcpy( buf
, filenames
.mbc_str(), filenames
.Len() + 1 );
176 size_t wxFileDataObject::GetDataSize() const
178 return GetFilenames().Len() + 1;
181 bool wxFileDataObject::SetData(const void *buf
)
183 SetFilenames((const wxChar
*)buf
);
188 // ----------------------------------------------------------------------------
189 // wxBitmapDataObject
190 // ----------------------------------------------------------------------------
192 wxBitmapDataObject::wxBitmapDataObject()
197 wxBitmapDataObject::wxBitmapDataObject( const wxBitmap
& bitmap
)
198 : wxBitmapDataObjectBase(bitmap
)
205 wxBitmapDataObject::~wxBitmapDataObject()
210 void wxBitmapDataObject::SetBitmap( const wxBitmap
&bitmap
)
214 wxBitmapDataObjectBase::SetBitmap(bitmap
);
219 bool wxBitmapDataObject::GetDataHere(void *buf
) const
223 wxFAIL_MSG( wxT("attempt to copy empty bitmap failed") );
228 memcpy(buf
, m_pngData
, m_pngSize
);
233 bool wxBitmapDataObject::SetData(size_t size
, const void *buf
)
238 m_pngData
= malloc(m_pngSize
);
240 memcpy( m_pngData
, buf
, m_pngSize
);
242 wxMemoryInputStream
mstream( (char*) m_pngData
, m_pngSize
);
244 wxPNGHandler handler
;
245 if ( !handler
.LoadFile( &image
, mstream
) )
250 m_bitmap
= image
.ConvertToBitmap();
253 void wxBitmapDataObject::DoConvertToPng()
258 wxImage
image( m_bitmap
);
259 wxPNGHandler handler
;
261 wxCountingOutputStream count
;
262 handler
.SaveFile( &image
, count
);
264 m_pngSize
= count
.GetSize() + 100; // sometimes the size seems to vary ???
265 m_pngData
= malloc(m_pngSize
);
267 wxMemoryOutputStream
mstream( (char*) m_pngData
, m_pngSize
);
268 handler
.SaveFile( &image
, mstream
);
271 // ----------------------------------------------------------------------------
272 // wxPrivateDataObject
273 // ----------------------------------------------------------------------------
275 IMPLEMENT_CLASS( wxPrivateDataObject
, wxDataObject
)
277 void wxPrivateDataObject::Free()
283 wxPrivateDataObject::wxPrivateDataObject()
285 wxString id
= wxT("application/");
286 id
+= wxTheApp
->GetAppName();
288 m_format
.SetId( id
);
291 m_data
= (void *)NULL
;
294 void wxPrivateDataObject::SetData( const void *data
, size_t size
)
299 m_data
= malloc(size
);
301 memcpy( m_data
, data
, size
);
304 void wxPrivateDataObject::WriteData( void *dest
) const
306 WriteData( m_data
, dest
);
309 size_t wxPrivateDataObject::GetSize() const
314 void wxPrivateDataObject::WriteData( const void *data
, void *dest
) const
316 memcpy( dest
, data
, GetSize() );