From 838e45fd9b58392df0936cfb2bfd19bf95d1645d Mon Sep 17 00:00:00 2001 From: Artur Wieczorek Date: Tue, 12 Jan 2021 17:55:31 +0100 Subject: [PATCH] Fix adding wxBitmap to generic wxImageList supporting masks Wee need to assure that bitmap added to the internal collection always have a mask. If necessary this mask is created from alpha channel values. Also, for compatibility with wxMSW implementation we need to prevent the bitmap from having both a mask and alpha channel. --- src/generic/imaglist.cpp | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/src/generic/imaglist.cpp b/src/generic/imaglist.cpp index c6d285fa50..80b1c7b00a 100644 --- a/src/generic/imaglist.cpp +++ b/src/generic/imaglist.cpp @@ -20,6 +20,8 @@ #include "wx/image.h" #endif +#include "wx/settings.h" + //----------------------------------------------------------------------------- // wxImageList //----------------------------------------------------------------------------- @@ -84,7 +86,42 @@ int wxGenericImageList::Add( const wxBitmap &bitmap ) } wxBitmap bmp(bitmap); - if ( !m_useMask ) + if ( m_useMask ) + { + if ( bmp.GetMask() ) + { + if ( bmp.HasAlpha() ) + { + // We need to remove alpha channel for compatibility with + // native-based wxMSW wxImageList where stored images are not allowed + // to have both mask and alpha channel. +#if wxUSE_IMAGE + wxImage img = bitmap.ConvertToImage(); + img.ClearAlpha(); + bmp = img; +#endif // wxUSE_IMAGE + } + } + else + { + if ( bmp.HasAlpha() ) + { + // Convert alpha channel to mask. +#if wxUSE_IMAGE + wxImage img = bmp.ConvertToImage(); + img.ConvertAlphaToMask(); + bmp = img; +#endif // wxUSE_IMAGE + } + else + { + // Like for wxMSW, use the light grey from standard colour map as transparent colour. + wxColour col = wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE); + bmp.SetMask(new wxMask(bmp, col)); + } + } + } + else { if ( bmp.GetMask() ) {