Handle transparency to the best of our ability in wxImageList

Don't take the value of "mask" parameter of wxImageList constructor too
prescriptively, it predates support for alpha in wxWidgets by many years
and was never meant to actually suppress using it.

Instead, do the best thing we can in all cases, i.e. use alpha if it's
specified and supported and use mask otherwise. But only create the mask
from light grey colour if we have nothing else if "mask" is true in
wxImageList constructor, as this is a potentially destructive action
that must not be performed if the user has explicitly decided to set
this parameter to false.

Incidentally fix handling of alpha with comctl32.dll v5 (which is used
in the absence of any manifest) by converting it to a mask in this case:
this is not ideal, but better than just using black background as it
happened before, and restores pre-3.1.5 behaviour.

Also simplify the generic version which just has to create the default
mask if necessary and doesn't have to do anything at all in all the
other cases.

Note that these changes required relaxing some of the existing unit
tests as wxMSW implementation now can add alpha channel to the bitmaps
that didn't have it -- but this is a more useful behaviour, and so it
makes more sense to adapt the tests to it rather than doing a less
useful thing just to conform to the tests.

This commit is best viewed with git --color-moved
--color-moved-ws=ignore-all-spac options.

Closes #22349.
This commit is contained in:
Vadim Zeitlin
2022-05-05 23:19:52 +01:00
parent 0e21b52d57
commit 6feeed9fe9
4 changed files with 152 additions and 143 deletions

View File

@@ -71,62 +71,14 @@ bool wxGenericImageList::Create( int width, int height, bool mask, int WXUNUSED(
wxBitmap wxGenericImageList::GetImageListBitmap(const wxBitmap& bitmap) const
{
wxBitmap bmp(bitmap);
if ( m_useMask )
// If we don't have neither mask nor alpha and were asked to use a mask,
// create a default one.
if ( m_useMask && !bmp.GetMask() && !bmp.HasAlpha() )
{
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 = bmp.ConvertToImage();
img.ClearAlpha();
bmp = wxBitmap(img, -1, bmp.GetScaleFactor());
#endif // wxUSE_IMAGE
}
}
else
{
if ( bmp.HasAlpha() )
{
// Convert alpha channel to mask.
#if wxUSE_IMAGE
wxImage img = bmp.ConvertToImage();
img.ConvertAlphaToMask();
bmp = wxBitmap(img, -1, bmp.GetScaleFactor());
#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() )
{
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 = wxBitmap(img, -1, bmp.GetScaleFactor());
#else
bmp.SetMask(NULL);
#endif // wxUSE_IMAGE
}
}
// 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));
}
// Ensure image size is the same as the size of the images on the image list.