Fix adding wxBitmap with mask to generic wxImageList

If wxBitmap with mask is added to wxImageList that doesn't support masks
we need to convert a bitmap mask to alpha channel values prior to adding
bitmap to the list to preserve bitmap transparency.
This commit is contained in:
Artur Wieczorek
2021-01-12 17:52:44 +01:00
parent f3800cee1f
commit dfb09d2ae9
2 changed files with 27 additions and 2 deletions

View File

@@ -58,6 +58,7 @@ private:
const wxBitmap *DoGetPtr(int index) const; const wxBitmap *DoGetPtr(int index) const;
wxVector<wxBitmap> m_images; wxVector<wxBitmap> m_images;
bool m_useMask;
// Size of a single bitmap in the list. // Size of a single bitmap in the list.
wxSize m_size; wxSize m_size;

View File

@@ -42,9 +42,10 @@ int wxGenericImageList::GetImageCount() const
return static_cast<int>(m_images.size()); return static_cast<int>(m_images.size());
} }
bool wxGenericImageList::Create( int width, int height, bool WXUNUSED(mask), int WXUNUSED(initialCount) ) bool wxGenericImageList::Create( int width, int height, bool mask, int WXUNUSED(initialCount) )
{ {
m_size = wxSize(width, height); m_size = wxSize(width, height);
m_useMask = mask;
return true; return true;
} }
@@ -82,7 +83,30 @@ int wxGenericImageList::Add( const wxBitmap &bitmap )
"All bitmaps in wxImageList must have the same size" ); "All bitmaps in wxImageList must have the same size" );
} }
m_images.push_back(bitmap); wxBitmap bmp(bitmap);
if ( !m_useMask )
{
if ( bmp.GetMask() )
{
if ( bmp.HasAlpha() )
{
// TODO: It would be better to blend a mask with existing alpha values.
bmp.SetMask(NULL);
}
else
{
// Convert a mask to alpha values.
#if wxUSE_IMAGE
wxImage img = bmp.ConvertToImage();
img.InitAlpha();
bmp = img;
#else
bmp.SetMask(NULL);
#endif // wxUSE_IMAGE
}
}
}
m_images.push_back(bmp);
return GetImageCount() - 1; return GetImageCount() - 1;
} }