#include "wx/dataobj.h"
#include "wx/app.h"
#include "wx/debug.h"
+#include "wx/mstream.h"
+#include "wx/image.h"
#include "gdk/gdk.h"
//-------------------------------------------------------------------------
GdkAtom g_textAtom = 0;
+GdkAtom g_pngAtom = 0;
//-------------------------------------------------------------------------
// wxDataFormat
wxDataFormat::wxDataFormat()
{
- if (!g_textAtom) g_textAtom = gdk_atom_intern( "STRING", FALSE );
+ PrepareFormats();
m_type = wxDF_INVALID;
m_hasAtom = FALSE;
m_atom = (GdkAtom) 0;
wxDataFormat::wxDataFormat( wxDataFormatId type )
{
- if (!g_textAtom) g_textAtom = gdk_atom_intern( "STRING", FALSE );
+ PrepareFormats();
SetType( type );
}
wxDataFormat::wxDataFormat( const wxChar *id )
{
- if (!g_textAtom) g_textAtom = gdk_atom_intern( "STRING", FALSE );
+ PrepareFormats();
SetId( id );
}
wxDataFormat::wxDataFormat( const wxString &id )
{
- if (!g_textAtom) g_textAtom = gdk_atom_intern( "STRING", FALSE );
+ PrepareFormats();
SetId( id );
}
wxDataFormat::wxDataFormat( const wxDataFormat &format )
{
- if (!g_textAtom) g_textAtom = gdk_atom_intern( "STRING", FALSE );
+ PrepareFormats();
m_type = format.GetType();
m_id = format.GetId();
m_hasAtom = TRUE;
wxDataFormat::wxDataFormat( const GdkAtom atom )
{
- if (!g_textAtom) g_textAtom = gdk_atom_intern( "STRING", FALSE );
+ PrepareFormats();
m_hasAtom = TRUE;
m_atom = atom;
m_type = wxDF_PRIVATE;
m_id = gdk_atom_name( m_atom );
- if (m_id == _T("file:ALL"))
+ if (m_id == wxT("file:ALL"))
{
m_type = wxDF_FILENAME;
}
if (m_type == wxDF_TEXT)
{
- m_id = _T("STRING");
+ m_id = wxT("STRING");
}
else
if (m_type == wxDF_BITMAP)
{
- m_id = _T("BITMAP");
+ m_id = wxT("image/png");
}
else
if (m_type == wxDF_FILENAME)
{
- m_id = _T("file:ALL");
+ m_id = wxT("file:ALL");
}
else
{
- wxFAIL_MSG( _T("invalid dataformat") );
+ wxFAIL_MSG( wxT("invalid dataformat") );
}
m_hasAtom = FALSE;
else
if (m_type == wxDF_PRIVATE)
{
- m_atom = gdk_atom_intern( MBSTRINGCAST m_id.mbc_str(), FALSE );
+ m_atom = gdk_atom_intern( wxMBSTRINGCAST m_id.mbc_str(), FALSE );
}
else
if (m_type == wxDF_FILENAME)
return m_atom;
}
+void wxDataFormat::PrepareFormats()
+{
+ if (!g_textAtom) g_textAtom = gdk_atom_intern( "STRING", FALSE );
+ if (!g_pngAtom) g_pngAtom = gdk_atom_intern( "image/png", FALSE );
+}
+
//-------------------------------------------------------------------------
// wxDataBroker
//-------------------------------------------------------------------------
wxBitmapDataObject::wxBitmapDataObject()
{
m_format.SetType( wxDF_BITMAP );
+ m_pngData = (char*)NULL;
+ m_pngSize = 0;
}
wxBitmapDataObject::wxBitmapDataObject( const wxBitmap& bitmap )
{
m_format.SetType( wxDF_BITMAP );
-
+ m_pngData = (char*)NULL;
+ m_pngSize = 0;
m_bitmap = bitmap;
+ DoConvertToPng();
+}
+
+wxBitmapDataObject::~wxBitmapDataObject()
+{
+ if (m_pngData) delete[] m_pngData;
}
void wxBitmapDataObject::SetBitmap( const wxBitmap &bitmap )
{
m_bitmap = bitmap;
+ DoConvertToPng();
}
wxBitmap wxBitmapDataObject::GetBitmap() const
size_t wxBitmapDataObject::GetSize() const
{
- return 0;
+ return m_pngSize;
+}
+
+void wxBitmapDataObject::WriteBitmap( const wxBitmap &WXUNUSED(bitmap), void *dest ) const
+{
+// if (m_bitmap == bitmap)
+ memcpy( dest, m_pngData, m_pngSize );
+}
+
+void wxBitmapDataObject::SetPngData( const char *pngData, size_t pngSize )
+{
+ if (m_pngData) delete[] m_pngData;
+ m_pngData = (char*) NULL;
+ m_pngSize = pngSize;
+ m_pngData = new char[m_pngSize];
+ memcpy( m_pngData, pngData, m_pngSize );
+
+ wxMemoryInputStream mstream( pngData, pngSize );
+ wxImage image;
+ wxPNGHandler handler;
+ handler.LoadFile( &image, mstream );
+ m_bitmap = image.ConvertToBitmap();
}
-void wxBitmapDataObject::WriteBitmap( const wxBitmap &bitmap, void *dest ) const
+void wxBitmapDataObject::DoConvertToPng()
{
- memcpy( dest, m_bitmap.GetPixmap(), GetSize() );
+ if (m_pngData) delete[] m_pngData;
+
+ wxImage image( m_bitmap );
+ wxPNGHandler handler;
+
+ wxCountingOutputStream count;
+ handler.SaveFile( &image, count );
+ m_pngSize = count.GetSize() + 100; // sometimes the size seems to vary ???
+ m_pngData = new char[m_pngSize];
+
+ wxMemoryOutputStream mstream( m_pngData, m_pngSize );
+ handler.SaveFile( &image, mstream );
}
// ----------------------------------------------------------------------------
wxPrivateDataObject::wxPrivateDataObject()
{
- wxString id = _T("application/");
+ wxString id = wxT("application/");
id += wxTheApp->GetAppName();
m_format.SetId( id );