X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/670f9935630beb2123a5ca62894ae92a3f0efa4f..7282b0678a879077a109495cb3cd20b28ea9cf45:/src/gtk1/bitmap.cpp diff --git a/src/gtk1/bitmap.cpp b/src/gtk1/bitmap.cpp index facb2e7204..109450dc55 100644 --- a/src/gtk1/bitmap.cpp +++ b/src/gtk1/bitmap.cpp @@ -14,13 +14,14 @@ #ifndef WX_PRECOMP #include "wx/app.h" + #include "wx/dcmemory.h" + #include "wx/palette.h" + #include "wx/icon.h" + #include "wx/math.h" + #include "wx/image.h" #endif // WX_PRECOMP -#include "wx/palette.h" -#include "wx/icon.h" #include "wx/filefn.h" -#include "wx/image.h" -#include "wx/dcmemory.h" #include #include @@ -28,8 +29,6 @@ #include -#include "wx/math.h" - extern void gdk_wx_draw_bitmap (GdkDrawable *drawable, GdkGC *gc, @@ -55,26 +54,26 @@ IMPLEMENT_DYNAMIC_CLASS(wxMask,wxObject) wxMask::wxMask() { - m_bitmap = (GdkBitmap *) NULL; + m_bitmap = NULL; } wxMask::wxMask( const wxBitmap& bitmap, const wxColour& colour ) { - m_bitmap = (GdkBitmap *) NULL; + m_bitmap = NULL; Create( bitmap, colour ); } #if wxUSE_PALETTE wxMask::wxMask( const wxBitmap& bitmap, int paletteIndex ) { - m_bitmap = (GdkBitmap *) NULL; + m_bitmap = NULL; Create( bitmap, paletteIndex ); } #endif // wxUSE_PALETTE wxMask::wxMask( const wxBitmap& bitmap ) { - m_bitmap = (GdkBitmap *) NULL; + m_bitmap = NULL; Create( bitmap ); } @@ -90,7 +89,7 @@ bool wxMask::Create( const wxBitmap& bitmap, if (m_bitmap) { gdk_bitmap_unref( m_bitmap ); - m_bitmap = (GdkBitmap*) NULL; + m_bitmap = NULL; } wxImage image = bitmap.ConvertToImage(); @@ -196,7 +195,7 @@ bool wxMask::Create( const wxBitmap& bitmap ) if (m_bitmap) { gdk_bitmap_unref( m_bitmap ); - m_bitmap = (GdkBitmap*) NULL; + m_bitmap = NULL; } if (!bitmap.Ok()) return false; @@ -221,15 +220,20 @@ GdkBitmap *wxMask::GetBitmap() const return m_bitmap; } + //----------------------------------------------------------------------------- -// wxBitmap +// wxBitmapRefData //----------------------------------------------------------------------------- -class wxBitmapRefData: public wxObjectRefData +class wxBitmapRefData : public wxGDIRefData { public: wxBitmapRefData(); - ~wxBitmapRefData(); + wxBitmapRefData(const wxBitmapRefData& data); + bool Create(int width, int height, int bpp); + virtual ~wxBitmapRefData(); + + virtual bool IsOk() const { return m_pixmap || m_bitmap; } GdkPixmap *m_pixmap; GdkBitmap *m_bitmap; @@ -237,18 +241,99 @@ public: int m_width; int m_height; int m_bpp; +#if wxUSE_PALETTE wxPalette *m_palette; +#endif // wxUSE_PALETTE }; wxBitmapRefData::wxBitmapRefData() { - m_pixmap = (GdkPixmap *) NULL; - m_bitmap = (GdkBitmap *) NULL; - m_mask = (wxMask *) NULL; + m_pixmap = NULL; + m_bitmap = NULL; + m_mask = NULL; m_width = 0; m_height = 0; m_bpp = 0; - m_palette = (wxPalette *) NULL; +#if wxUSE_PALETTE + m_palette = NULL; +#endif // wxUSE_PALETTE +} + +wxBitmapRefData::wxBitmapRefData(const wxBitmapRefData& data) +{ + Create(data.m_width, data.m_height, data.m_bpp); + + m_mask = data.m_mask ? new wxMask(*data.m_mask) : NULL; + +#if wxUSE_PALETTE + wxASSERT_MSG( !data.m_palette, + wxT("copying bitmaps palette not implemented") ); +#endif // wxUSE_PALETTE + + + // copy the bitmap data by simply drawing the source bitmap on this one + GdkPixmap **dst; + if ( data.m_pixmap ) + { + dst = &m_pixmap; + } + else if ( data.m_bitmap ) + { + dst = &m_bitmap; + } + else // invalid bitmap? + { + return; + } + + GdkGC *gc = gdk_gc_new(*dst); + if ( m_bpp == 1 ) + { + gdk_wx_draw_bitmap(m_bitmap, gc, data.m_bitmap, 0, 0, 0, 0, -1, -1); + } + else // colour pixmap + { + gdk_draw_pixmap(m_pixmap, gc, data.m_pixmap, 0, 0, 0, 0, -1, -1); + } + + gdk_gc_unref(gc); +} + +bool wxBitmapRefData::Create(int width, int height, int bpp) +{ + m_width = width; + m_height = height; + m_bpp = bpp; + + m_mask = NULL; +#if wxUSE_PALETTE + m_palette = NULL; +#endif + + // to understand how this compiles you should know that GdkPixmap and + // GdkBitmap are one and the same type in GTK+ 1 + GdkPixmap **ppix; + if ( m_bpp != 1 ) + { + const GdkVisual * const visual = wxTheApp->GetGdkVisual(); + + wxCHECK_MSG( (bpp == -1) || (bpp == visual->depth) || (bpp == 32), false, + wxT("invalid bitmap depth") ); + + m_bpp = visual->depth; + + ppix = &m_pixmap; + m_bitmap = NULL; + } + else // mono bitmap + { + ppix = &m_bitmap; + m_pixmap = NULL; + } + + *ppix = gdk_pixmap_new( wxGetRootWindow()->window, width, height, m_bpp ); + + return *ppix != NULL; } wxBitmapRefData::~wxBitmapRefData() @@ -263,19 +348,23 @@ wxBitmapRefData::~wxBitmapRefData() #endif // wxUSE_PALETTE } + +//----------------------------------------------------------------------------- +// wxBitmap //----------------------------------------------------------------------------- #define M_BMPDATA ((wxBitmapRefData *)m_refData) IMPLEMENT_DYNAMIC_CLASS(wxBitmap,wxGDIObject) -wxBitmap::wxBitmap() +wxGDIRefData *wxBitmap::CreateGDIRefData() const { + return new wxBitmapRefData; } -wxBitmap::wxBitmap( int width, int height, int depth ) +wxGDIRefData *wxBitmap::CloneGDIRefData(const wxGDIRefData *data) const { - Create( width, height, depth ); + return new wxBitmapRefData(*static_cast(data)); } bool wxBitmap::Create( int width, int height, int depth ) @@ -287,47 +376,23 @@ bool wxBitmap::Create( int width, int height, int depth ) return false; } - GdkVisual *visual = wxTheApp->GetGdkVisual(); - - if (depth == -1) - depth = visual->depth; - - wxCHECK_MSG( (depth == visual->depth) || (depth == 1) || (depth == 32), false, - wxT("invalid bitmap depth") ); - m_refData = new wxBitmapRefData(); - M_BMPDATA->m_mask = (wxMask *) NULL; - M_BMPDATA->m_width = width; - M_BMPDATA->m_height = height; - if (depth == 1) - { - M_BMPDATA->m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, width, height, 1 ); - M_BMPDATA->m_bpp = 1; - } - else - { - M_BMPDATA->m_pixmap = gdk_pixmap_new( wxGetRootWindow()->window, width, height, depth ); - M_BMPDATA->m_bpp = visual->depth; - } - - return Ok(); + return M_BMPDATA->Create(width, height, depth); } -bool wxBitmap::CreateFromXpm( const char **bits ) +wxBitmap::wxBitmap(const char* const* bits) { - UnRef(); - - wxCHECK_MSG( bits != NULL, false, wxT("invalid bitmap data") ); + wxCHECK2_MSG(bits != NULL, return, wxT("invalid bitmap data")); GdkVisual *visual = wxTheApp->GetGdkVisual(); m_refData = new wxBitmapRefData(); - GdkBitmap *mask = (GdkBitmap*) NULL; + GdkBitmap *mask = NULL; M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm_d( wxGetRootWindow()->window, &mask, NULL, (gchar **) bits ); - wxCHECK_MSG( M_BMPDATA->m_pixmap, false, wxT("couldn't create pixmap") ); + wxCHECK2_MSG(M_BMPDATA->m_pixmap, return, wxT("couldn't create pixmap")); if (mask) { @@ -338,8 +403,6 @@ bool wxBitmap::CreateFromXpm( const char **bits ) gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) ); M_BMPDATA->m_bpp = visual->depth; // Can we get a different depth from create_from_xpm_d() ? - - return true; } wxBitmap wxBitmap::Rescale( int clipx, int clipy, int clipwidth, int clipheight, int newx, int newy ) @@ -356,7 +419,7 @@ wxBitmap wxBitmap::Rescale( int clipx, int clipy, int clipwidth, int clipheight, wxBitmap bmp; - GdkImage *img = (GdkImage*) NULL; + GdkImage *img = NULL; if (GetPixmap()) img = gdk_image_get( GetPixmap(), 0, 0, GetWidth(), GetHeight() ); else if (GetBitmap()) @@ -575,7 +638,7 @@ bool wxBitmap::CreateFromImageAsBitmap(const wxImage& img) // Create mask image - GdkImage *mask_image = (GdkImage*) NULL; + GdkImage *mask_image = NULL; if (image.HasMask()) { @@ -711,7 +774,7 @@ bool wxBitmap::CreateFromImageAsPixmap(const wxImage& img) // Create mask image - GdkImage *mask_image = (GdkImage*) NULL; + GdkImage *mask_image = NULL; if (image.HasMask()) { @@ -909,7 +972,7 @@ wxImage wxBitmap::ConvertToImage() const static const int MASK_BLUE = 3; static const int MASK_BLUE_REPLACEMENT = 2; - GdkImage *gdk_image = (GdkImage*) NULL; + GdkImage *gdk_image = NULL; if (HasPixmap()) { @@ -930,7 +993,7 @@ wxImage wxBitmap::ConvertToImage() const wxCHECK_MSG( gdk_image, wxNullImage, wxT("couldn't create image") ); - GdkImage *gdk_image_mask = (GdkImage*) NULL; + GdkImage *gdk_image_mask = NULL; if (GetMask()) { gdk_image_mask = gdk_image_get( GetMask()->GetBitmap(), @@ -1051,7 +1114,7 @@ wxBitmap::wxBitmap( const char bits[], int width, int height, int WXUNUSED(depth { m_refData = new wxBitmapRefData(); - M_BMPDATA->m_mask = (wxMask *) NULL; + M_BMPDATA->m_mask = NULL; M_BMPDATA->m_bitmap = gdk_bitmap_create_from_data ( wxGetRootWindow()->window, @@ -1071,22 +1134,6 @@ wxBitmap::~wxBitmap() { } -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) && - (M_BMPDATA->m_bitmap || M_BMPDATA->m_pixmap); -} - int wxBitmap::GetHeight() const { wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") ); @@ -1110,7 +1157,7 @@ int wxBitmap::GetDepth() const wxMask *wxBitmap::GetMask() const { - wxCHECK_MSG( Ok(), (wxMask *) NULL, wxT("invalid bitmap") ); + wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") ); return M_BMPDATA->m_mask; } @@ -1119,6 +1166,7 @@ void wxBitmap::SetMask( wxMask *mask ) { wxCHECK_RET( Ok(), wxT("invalid bitmap") ); + AllocExclusive(); if (M_BMPDATA->m_mask) delete M_BMPDATA->m_mask; M_BMPDATA->m_mask = mask; @@ -1204,7 +1252,7 @@ bool wxBitmap::LoadFile( const wxString &name, wxBitmapType type ) { m_refData = new wxBitmapRefData(); - GdkBitmap *mask = (GdkBitmap*) NULL; + GdkBitmap *mask = NULL; M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm ( @@ -1240,7 +1288,7 @@ bool wxBitmap::LoadFile( const wxString &name, wxBitmapType type ) wxPalette *wxBitmap::GetPalette() const { if (!Ok()) - return (wxPalette *) NULL; + return NULL; return M_BMPDATA->m_palette; } @@ -1253,25 +1301,19 @@ void wxBitmap::SetPalette(const wxPalette& WXUNUSED(palette)) void wxBitmap::SetHeight( int height ) { - if (!m_refData) - m_refData = new wxBitmapRefData(); - + AllocExclusive(); M_BMPDATA->m_height = height; } void wxBitmap::SetWidth( int width ) { - if (!m_refData) - m_refData = new wxBitmapRefData(); - + AllocExclusive(); M_BMPDATA->m_width = width; } void wxBitmap::SetDepth( int depth ) { - if (!m_refData) - m_refData = new wxBitmapRefData(); - + AllocExclusive(); M_BMPDATA->m_bpp = depth; } @@ -1293,7 +1335,7 @@ void wxBitmap::SetBitmap( GdkPixmap *bitmap ) GdkPixmap *wxBitmap::GetPixmap() const { - wxCHECK_MSG( Ok(), (GdkPixmap *) NULL, wxT("invalid bitmap") ); + wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") ); return M_BMPDATA->m_pixmap; } @@ -1307,12 +1349,12 @@ bool wxBitmap::HasPixmap() const GdkBitmap *wxBitmap::GetBitmap() const { - wxCHECK_MSG( Ok(), (GdkBitmap *) NULL, wxT("invalid bitmap") ); + wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") ); return M_BMPDATA->m_bitmap; } -void *wxBitmap::GetRawData(wxPixelDataBase& data, int bpp) +void *wxBitmap::GetRawData(wxPixelDataBase& WXUNUSED(data), int WXUNUSED(bpp)) { return NULL; } @@ -1321,59 +1363,11 @@ void wxBitmap::UngetRawData(wxPixelDataBase& WXUNUSED(data)) { } - bool wxBitmap::HasAlpha() const { return false; } -void wxBitmap::UseAlpha() -{ -} - -//----------------------------------------------------------------------------- -// wxBitmapHandler -//----------------------------------------------------------------------------- - -IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler,wxBitmapHandlerBase) - -wxBitmapHandler::~wxBitmapHandler() -{ -} - -bool wxBitmapHandler::Create(wxBitmap * WXUNUSED(bitmap), - void * WXUNUSED(data), - long WXUNUSED(type), - int WXUNUSED(width), - int WXUNUSED(height), - int WXUNUSED(depth)) -{ - wxFAIL_MSG( _T("not implemented") ); - - return false; -} - -bool wxBitmapHandler::LoadFile(wxBitmap * WXUNUSED(bitmap), - const wxString& WXUNUSED(name), - long WXUNUSED(flags), - int WXUNUSED(desiredWidth), - int WXUNUSED(desiredHeight)) -{ - wxFAIL_MSG( _T("not implemented") ); - - return false; -} - -bool wxBitmapHandler::SaveFile(const wxBitmap * WXUNUSED(bitmap), - const wxString& WXUNUSED(name), - int WXUNUSED(type), - const wxPalette * WXUNUSED(palette)) -{ - wxFAIL_MSG( _T("not implemented") ); - - return false; -} - /* static */ void wxBitmap::InitStandardHandlers() { // TODO: Insert handler based on GdkPixbufs handler later