+wxBitmap::wxBitmap( const wxBitmap& bmp )
+ : wxBitmapBase()
+{
+ Ref( bmp );
+}
+
+wxBitmap::wxBitmap( const wxString &filename, wxBitmapType type )
+{
+ LoadFile( filename, type );
+}
+
+wxBitmap::wxBitmap( const char bits[], int width, int height, int WXUNUSED(depth))
+{
+ if ( width > 0 && height > 0 )
+ {
+ m_refData = new wxBitmapRefData();
+
+ M_BMPDATA->m_mask = (wxMask *) NULL;
+ M_BMPDATA->m_bitmap = gdk_bitmap_create_from_data
+ (
+ wxGetRootWindow()->window,
+ (gchar *) bits,
+ width,
+ height
+ );
+ M_BMPDATA->m_width = width;
+ M_BMPDATA->m_height = height;
+ M_BMPDATA->m_bpp = 1;
+
+ wxASSERT_MSG( M_BMPDATA->m_bitmap, wxT("couldn't create bitmap") );
+ }
+}
+
+wxBitmap::~wxBitmap()
+{
+}
+
+wxBitmap& wxBitmap::operator = ( const wxBitmap& bmp )
+{
+ if ( m_refData != bmp.m_refData )
+ Ref( bmp );
+
+ return *this;
+}
+
+bool wxBitmap::operator == ( const wxBitmap& bmp ) const
+{
+ return m_refData == bmp.m_refData;
+}
+
+bool wxBitmap::operator != ( const wxBitmap& bmp ) const
+{
+ return m_refData != bmp.m_refData;
+}
+
+bool wxBitmap::Ok() const
+{
+ return (m_refData != NULL) &&
+ (
+#ifdef __WXGTK20__
+ M_BMPDATA->m_pixbuf ||
+#endif
+ M_BMPDATA->m_bitmap || M_BMPDATA->m_pixmap
+ );
+}
+
+int wxBitmap::GetHeight() const
+{
+ wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
+
+ return M_BMPDATA->m_height;
+}
+
+int wxBitmap::GetWidth() const
+{
+ wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
+
+ return M_BMPDATA->m_width;
+}
+
+int wxBitmap::GetDepth() const
+{
+ wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
+
+ return M_BMPDATA->m_bpp;
+}
+
+wxMask *wxBitmap::GetMask() const
+{
+ wxCHECK_MSG( Ok(), (wxMask *) NULL, wxT("invalid bitmap") );
+
+ return M_BMPDATA->m_mask;
+}
+
+void wxBitmap::SetMask( wxMask *mask )
+{
+ wxCHECK_RET( Ok(), wxT("invalid bitmap") );
+
+ if (M_BMPDATA->m_mask) delete M_BMPDATA->m_mask;
+
+ M_BMPDATA->m_mask = mask;
+}
+
+bool wxBitmap::CopyFromIcon(const wxIcon& icon)
+{
+ *this = icon;
+ return TRUE;
+}
+
+wxBitmap wxBitmap::GetSubBitmap( const wxRect& rect) const
+{
+ wxCHECK_MSG( Ok() &&
+ (rect.x >= 0) && (rect.y >= 0) &&
+ (rect.x+rect.width <= M_BMPDATA->m_width) && (rect.y+rect.height <= M_BMPDATA->m_height),
+ wxNullBitmap, wxT("invalid bitmap or bitmap region") );
+
+ wxBitmap ret( rect.width, rect.height, M_BMPDATA->m_bpp );
+ wxASSERT_MSG( ret.Ok(), wxT("GetSubBitmap error") );
+
+#ifdef __WXGTK20__
+ if (HasPixbuf())
+ {
+ GdkPixbuf *pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB,
+ gdk_pixbuf_get_has_alpha(GetPixbuf()),
+ 8, GetWidth(), GetHeight());
+ ret.SetPixbuf(pixbuf);
+ gdk_pixbuf_copy_area(GetPixbuf(),
+ rect.x, rect.y, rect.width, rect.height,
+ pixbuf, 0, 0);
+ }
+ else
+#endif // __WXGTK20__
+ {
+ if (ret.GetPixmap())
+ {
+ GdkGC *gc = gdk_gc_new( ret.GetPixmap() );
+ gdk_draw_pixmap( ret.GetPixmap(), gc, GetPixmap(), rect.x, rect.y, 0, 0, rect.width, rect.height );
+ gdk_gc_destroy( gc );
+ }
+ else
+ {
+ GdkGC *gc = gdk_gc_new( ret.GetBitmap() );
+ GdkColor col;
+ col.pixel = 0xFFFFFF;
+ gdk_gc_set_foreground( gc, &col );
+ col.pixel = 0;
+ gdk_gc_set_background( gc, &col );
+ gdk_wx_draw_bitmap( ret.GetBitmap(), gc, GetBitmap(), rect.x, rect.y, 0, 0, rect.width, rect.height );
+ gdk_gc_destroy( gc );
+ }
+ }
+
+ if (GetMask())
+ {
+ wxMask *mask = new wxMask;
+ mask->m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, rect.width, rect.height, 1 );
+
+ GdkGC *gc = gdk_gc_new( mask->m_bitmap );
+ GdkColor col;
+ col.pixel = 0xFFFFFF;
+ gdk_gc_set_foreground( gc, &col );
+ col.pixel = 0;
+ gdk_gc_set_background( gc, &col );
+ gdk_wx_draw_bitmap( mask->m_bitmap, gc, M_BMPDATA->m_mask->m_bitmap, rect.x, rect.y, 0, 0, rect.width, rect.height );
+ gdk_gc_destroy( gc );
+
+ ret.SetMask( mask );
+ }
+
+ return ret;
+}
+
+bool wxBitmap::SaveFile( const wxString &name, wxBitmapType type, const wxPalette *WXUNUSED(palette) ) const
+{
+ wxCHECK_MSG( Ok(), FALSE, wxT("invalid bitmap") );
+
+ // Try to save the bitmap via wxImage handlers:
+ {
+ wxImage image = ConvertToImage();
+ if (image.Ok()) return image.SaveFile( name, type );
+ }
+
+ return FALSE;
+}
+
+bool wxBitmap::LoadFile( const wxString &name, wxBitmapType type )
+{
+ UnRef();
+
+ if (!wxFileExists(name))
+ return FALSE;
+
+ GdkVisual *visual = wxTheApp->GetGdkVisual();
+
+ if (type == wxBITMAP_TYPE_XPM)
+ {
+ m_refData = new wxBitmapRefData();
+
+ GdkBitmap *mask = (GdkBitmap*) NULL;
+
+ M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm
+ (
+ wxGetRootWindow()->window,
+ &mask,
+ NULL,
+ name.fn_str()
+ );
+
+ if (mask)
+ {
+ M_BMPDATA->m_mask = new wxMask();
+ M_BMPDATA->m_mask->m_bitmap = mask;
+ }
+
+ gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) );
+
+ M_BMPDATA->m_bpp = visual->depth;
+ }
+ else // try if wxImage can load it
+ {
+ wxImage image;
+ if ( !image.LoadFile( name, type ) || !image.Ok() )
+ return FALSE;
+
+ *this = wxBitmap(image);
+ }
+
+ return TRUE;
+}
+
+wxPalette *wxBitmap::GetPalette() const
+{
+ if (!Ok())
+ return (wxPalette *) NULL;
+
+ return M_BMPDATA->m_palette;
+}
+
+void wxBitmap::SetPalette(const wxPalette& WXUNUSED(palette))
+{
+ // TODO
+}
+
+void wxBitmap::SetHeight( int height )
+{
+ if (!m_refData)
+ m_refData = new wxBitmapRefData();
+
+ M_BMPDATA->m_height = height;
+}
+
+void wxBitmap::SetWidth( int width )
+{
+ if (!m_refData)
+ m_refData = new wxBitmapRefData();
+
+ M_BMPDATA->m_width = width;
+}
+
+void wxBitmap::SetDepth( int depth )
+{
+ if (!m_refData)
+ m_refData = new wxBitmapRefData();
+
+ M_BMPDATA->m_bpp = depth;
+}
+
+void wxBitmap::SetPixmap( GdkPixmap *pixmap )
+{
+ if (!m_refData)
+ m_refData = new wxBitmapRefData();
+
+ M_BMPDATA->m_pixmap = pixmap;
+#ifdef __WXGTK20__
+ PurgeOtherRepresentations(Pixmap);
+#endif
+}
+
+void wxBitmap::SetBitmap( GdkPixmap *bitmap )
+{
+ if (!m_refData)
+ m_refData = new wxBitmapRefData();
+
+ M_BMPDATA->m_bitmap = bitmap;
+#ifdef __WXGTK20__
+ PurgeOtherRepresentations(Pixmap);
+#endif
+}
+
+GdkPixmap *wxBitmap::GetPixmap() const
+{
+ wxCHECK_MSG( Ok(), (GdkPixmap *) NULL, wxT("invalid bitmap") );
+
+#ifdef __WXGTK20__
+ // create the pixmap on the fly if we use Pixbuf representation:
+ if (HasPixbuf() && !HasPixmap())
+ {
+ delete M_BMPDATA->m_mask;
+ M_BMPDATA->m_mask = new wxMask();
+ gdk_pixbuf_render_pixmap_and_mask(M_BMPDATA->m_pixbuf,
+ &M_BMPDATA->m_pixmap,
+ &M_BMPDATA->m_mask->m_bitmap,
+ 128 /*threshold*/);
+ }
+#endif // __WXGTK20__
+
+ return M_BMPDATA->m_pixmap;
+}
+
+bool wxBitmap::HasPixmap() const
+{
+ wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
+
+ return M_BMPDATA->m_pixmap != NULL;
+}
+
+GdkBitmap *wxBitmap::GetBitmap() const
+{
+ wxCHECK_MSG( Ok(), (GdkBitmap *) NULL, wxT("invalid bitmap") );
+
+ return M_BMPDATA->m_bitmap;
+}
+
+#ifdef __WXGTK20__
+GdkPixbuf *wxBitmap::GetPixbuf() const
+{
+ wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") );
+
+ if (HasPixmap() && !HasPixbuf())
+ {
+ int width = GetWidth();
+ int height = GetHeight();
+
+ GdkPixbuf *pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB,
+ GetMask() != NULL,
+ 8, width, height);
+ M_BMPDATA->m_pixbuf =
+ gdk_pixbuf_get_from_drawable(pixbuf, M_BMPDATA->m_pixmap, NULL,
+ 0, 0, 0, 0, width, height);
+
+ // apply the mask to created pixbuf:
+ if (M_BMPDATA->m_pixbuf && M_BMPDATA->m_mask)
+ {
+ GdkPixbuf *pmask =
+ gdk_pixbuf_get_from_drawable(NULL,
+ M_BMPDATA->m_mask->GetBitmap(),
+ NULL,
+ 0, 0, 0, 0, width, height);
+ if (pmask)
+ {
+ guchar *bmp = gdk_pixbuf_get_pixels(pixbuf);
+ guchar *mask = gdk_pixbuf_get_pixels(pmask);
+ int bmprowinc = gdk_pixbuf_get_rowstride(pixbuf) - 4 * width;
+ int maskrowinc = gdk_pixbuf_get_rowstride(pmask) - 3 * width;
+
+ for (int y = 0; y < height;
+ y++, bmp += bmprowinc, mask += maskrowinc)
+ {
+ for (int x = 0; x < width; x++, bmp += 4, mask += 3)
+ {
+ if (mask[0] == 0 /*black pixel*/)
+ bmp[3] = 0;
+ }
+ }
+
+ gdk_pixbuf_unref(pmask);
+ }
+ }
+ }
+
+ return M_BMPDATA->m_pixbuf;
+}
+
+bool wxBitmap::HasPixbuf() const
+{
+ wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
+
+ return M_BMPDATA->m_pixbuf != NULL;
+}
+
+void wxBitmap::SetPixbuf( GdkPixbuf *pixbuf )
+{
+ if (!m_refData)
+ m_refData = new wxBitmapRefData();
+
+ M_BMPDATA->m_pixbuf = pixbuf;
+ PurgeOtherRepresentations(Pixbuf);
+}
+
+void wxBitmap::PurgeOtherRepresentations(wxBitmap::Representation keep)
+{
+ if (keep == Pixmap && HasPixbuf())
+ {
+ gdk_pixbuf_unref( M_BMPDATA->m_pixbuf );
+ M_BMPDATA->m_pixbuf = NULL;
+ }
+ if (keep == Pixbuf && HasPixmap())
+ {
+ gdk_pixmap_unref( M_BMPDATA->m_pixmap );
+ M_BMPDATA->m_pixmap = NULL;
+ }
+}
+
+#endif // __WXGTK20__
+
+void *wxBitmap::GetRawData(wxPixelDataBase& data, int bpp)
+{
+#ifdef __WXGTK20__
+ if (bpp != 32)
+ return NULL;
+
+ GdkPixbuf *pixbuf = GetPixbuf();
+ if (!pixbuf)
+ return NULL;
+
+#if 0
+ if (gdk_pixbuf_get_has_alpha( pixbuf ))
+ wxPrintf( wxT("Has alpha\n") );
+ else
+ wxPrintf( wxT("No alpha.\n") );
+#endif
+
+ data.m_height = gdk_pixbuf_get_height( pixbuf );
+ data.m_width = gdk_pixbuf_get_width( pixbuf );
+ data.m_stride = gdk_pixbuf_get_rowstride( pixbuf );
+
+ return gdk_pixbuf_get_pixels( pixbuf );
+#else
+ return NULL;
+#endif
+}
+
+void wxBitmap::UngetRawData(wxPixelDataBase& data)
+{
+}
+
+
+bool wxBitmap::HasAlpha() const
+{
+#ifdef __WXGTK20__
+ return HasPixbuf();
+#else
+ return false;
+#endif
+}
+
+void wxBitmap::UseAlpha()
+{
+#ifdef __WXGTK20__
+ GetPixbuf();
+#endif
+}
+
+//-----------------------------------------------------------------------------
+// wxBitmapHandler
+//-----------------------------------------------------------------------------
+
+IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler,wxBitmapHandlerBase)
+
+wxBitmapHandler::~wxBitmapHandler()
+{
+}
+
+bool wxBitmapHandler::Create(wxBitmap *bitmap, void *data, long type, int width, int height, int depth)
+{
+ return FALSE;
+}
+
+bool wxBitmapHandler::LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
+ int desiredWidth, int desiredHeight)
+{
+ return FALSE;
+}
+
+bool wxBitmapHandler::SaveFile(const wxBitmap *bitmap, const wxString& name, int type, const wxPalette *palette)
+{
+ return FALSE;
+}
+
+/* static */ void wxBitmap::InitStandardHandlers()
+{
+ // TODO: Insert handler based on GdkPixbufs handler later
+}