X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/06497cba662c40d02a8f6c89670e0d15df4cd67b..726cc8697cd44a41b43257f05ca8cdd42b71a711:/src/gtk/bitmap.cpp diff --git a/src/gtk/bitmap.cpp b/src/gtk/bitmap.cpp index 3df05c6a59..a50e8a8e26 100644 --- a/src/gtk/bitmap.cpp +++ b/src/gtk/bitmap.cpp @@ -79,6 +79,23 @@ wxMask::wxMask() m_bitmap = NULL; } +wxMask::wxMask(const wxMask& mask) +{ + if ( !mask.m_bitmap ) + { + m_bitmap = NULL; + return; + } + + // create a copy of an existing mask + gint w, h; + gdk_drawable_get_size(mask.m_bitmap, &w, &h); + m_bitmap = gdk_pixmap_new(mask.m_bitmap, w, h, 1); + + wxGtkObject gc(gdk_gc_new(m_bitmap)); + gdk_draw_drawable(m_bitmap, gc, mask.m_bitmap, 0, 0, 0, 0, -1, -1); +} + wxMask::wxMask( const wxBitmap& bitmap, const wxColour& colour ) { m_bitmap = NULL; @@ -214,6 +231,13 @@ public: int m_height; int m_bpp; bool m_alphaRequested; + +private: + // We don't provide a copy ctor as copying m_pixmap and m_pixbuf properly + // is expensive and we don't want to do it implicitly (and possibly + // accidentally). wxBitmap::CloneGDIRefData() which does need to do it does + // it explicitly itself. + wxDECLARE_NO_COPY_CLASS(wxBitmapRefData); }; wxBitmapRefData::wxBitmapRefData(int width, int height, int depth) @@ -589,10 +613,8 @@ wxBitmap wxBitmap::GetSubBitmap( const wxRect& rect) const rect.y + h <= bmpData->m_height, ret, wxT("invalid bitmap region")); - wxBitmapRefData* newRef = new wxBitmapRefData(*bmpData); + wxBitmapRefData * const newRef = new wxBitmapRefData(w, h, bmpData->m_bpp); ret.m_refData = newRef; - newRef->m_width = w; - newRef->m_height = h; if (bmpData->m_pixbuf) { @@ -609,7 +631,6 @@ wxBitmap wxBitmap::GetSubBitmap( const wxRect& rect) const newRef->m_pixmap, gc, bmpData->m_pixmap, rect.x, rect.y, 0, 0, w, h); g_object_unref(gc); } - newRef->m_mask = NULL; if (bmpData->m_mask && bmpData->m_mask->m_bitmap) { GdkPixmap* sub_mask = gdk_pixmap_new(bmpData->m_mask->m_bitmap, w, h, 1); @@ -629,42 +650,39 @@ bool wxBitmap::SaveFile( const wxString &name, wxBitmapType type, const wxPalett wxCHECK_MSG( IsOk(), false, wxT("invalid bitmap") ); #if wxUSE_IMAGE - // Try to save the bitmap via wxImage handlers: wxImage image = ConvertToImage(); - return image.Ok() && image.SaveFile(name, type); -#else // !wxUSE_IMAGE - wxUnusedVar(name); - wxUnusedVar(type); - - return false; -#endif // wxUSE_IMAGE + if (image.IsOk() && image.SaveFile(name, type)) + return true; +#endif + const char* type_name = NULL; + switch (type) + { + case wxBITMAP_TYPE_BMP: type_name = "bmp"; break; + case wxBITMAP_TYPE_ICO: type_name = "ico"; break; + case wxBITMAP_TYPE_JPEG: type_name = "jpeg"; break; + case wxBITMAP_TYPE_PNG: type_name = "png"; break; + default: break; + } + return type_name && + gdk_pixbuf_save(GetPixbuf(), name.fn_str(), type_name, NULL, NULL); } bool wxBitmap::LoadFile( const wxString &name, wxBitmapType type ) { - UnRef(); - - if (type == wxBITMAP_TYPE_XPM) - { - GdkBitmap *mask = NULL; - SetPixmap(gdk_pixmap_create_from_xpm(wxGetRootWindow()->window, &mask, NULL, name.fn_str())); - if (!M_BMPDATA) - return false; // do not set the mask - - if (mask) - { - M_BMPDATA->m_mask = new wxMask; - M_BMPDATA->m_mask->m_bitmap = mask; - } - } #if wxUSE_IMAGE - else // try if wxImage can load it + wxImage image; + if (image.LoadFile(name, type) && image.IsOk()) + *this = wxBitmap(image); + else +#endif { - wxImage image; - if (image.LoadFile(name, type) && image.Ok()) - CreateFromImage(image, -1); + wxUnusedVar(type); // The type is detected automatically by GDK. + + UnRef(); + GdkPixbuf* pixbuf = gdk_pixbuf_new_from_file(name.fn_str(), NULL); + if (pixbuf) + SetPixbuf(pixbuf); } -#endif // wxUSE_IMAGE return IsOk(); } @@ -782,7 +800,7 @@ bool wxBitmap::HasPixbuf() const void wxBitmap::SetPixbuf(GdkPixbuf* pixbuf) { UnRef(); - + if (!pixbuf) return; @@ -847,7 +865,9 @@ wxGDIRefData* wxBitmap::CreateGDIRefData() const wxGDIRefData* wxBitmap::CloneGDIRefData(const wxGDIRefData* data) const { const wxBitmapRefData* oldRef = static_cast(data); - wxBitmapRefData* newRef = new wxBitmapRefData(*oldRef); + wxBitmapRefData * const newRef = new wxBitmapRefData(oldRef->m_width, + oldRef->m_height, + oldRef->m_bpp); if (oldRef->m_pixmap != NULL) { newRef->m_pixmap = gdk_pixmap_new( @@ -864,12 +884,7 @@ wxGDIRefData* wxBitmap::CloneGDIRefData(const wxGDIRefData* data) const } if (oldRef->m_mask != NULL) { - newRef->m_mask = new wxMask; - newRef->m_mask->m_bitmap = gdk_pixmap_new( - oldRef->m_mask->m_bitmap, oldRef->m_width, oldRef->m_height, 1); - wxGtkObject gc(gdk_gc_new(newRef->m_mask->m_bitmap)); - gdk_draw_drawable(newRef->m_mask->m_bitmap, - gc, oldRef->m_mask->m_bitmap, 0, 0, 0, 0, -1, -1); + newRef->m_mask = new wxMask(*oldRef->m_mask); } return newRef;