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:
@@ -36,7 +36,6 @@
|
||||
|
||||
#include "wx/imaglist.h"
|
||||
#include "wx/dc.h"
|
||||
#include "wx/scopedptr.h"
|
||||
#include "wx/msw/dc.h"
|
||||
#include "wx/msw/dib.h"
|
||||
#include "wx/msw/private.h"
|
||||
@@ -53,9 +52,8 @@ wxIMPLEMENT_DYNAMIC_CLASS(wxImageList, wxObject);
|
||||
// private functions
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
// returns the mask if it's valid, otherwise the bitmap mask and, if it's not
|
||||
// valid neither, a "solid" mask (no transparent zones at all)
|
||||
static HBITMAP GetMaskForImage(const wxBitmap& bitmap, const wxBitmap& mask);
|
||||
// returns the default transparent colour to use for creating the mask
|
||||
static wxColour GetDefaultMaskColour();
|
||||
|
||||
// ============================================================================
|
||||
// implementation
|
||||
@@ -81,12 +79,16 @@ bool wxImageList::Create(int width, int height, bool mask, int initial)
|
||||
// (e.g. ILC_COLOR16) shows completely broken bitmaps
|
||||
flags |= ILC_COLOR32;
|
||||
|
||||
m_useMask = mask;
|
||||
|
||||
// For comctl32.dll < 6 always use masks as it doesn't support alpha.
|
||||
if ( mask || wxApp::GetComCtl32Version() < 600 )
|
||||
{
|
||||
m_useMask = true;
|
||||
//
|
||||
// We also have to use masks when we don't have wxImage and wxDIB that are
|
||||
// needed to handle alpha.
|
||||
#if wxUSE_WXDIB && wxUSE_IMAGE
|
||||
if ( wxApp::GetComCtl32Version() < 600 )
|
||||
#endif
|
||||
flags |= ILC_MASK;
|
||||
}
|
||||
|
||||
// Grow by 1, I guess this is reasonable behaviour most of the time
|
||||
m_hImageList = (WXHIMAGELIST) ImageList_Create(width, height, flags,
|
||||
@@ -142,6 +144,14 @@ class wxImageList::wxMSWBitmaps
|
||||
public:
|
||||
wxMSWBitmaps() : hbmp(NULL) { }
|
||||
|
||||
#if wxUSE_WXDIB && wxUSE_IMAGE
|
||||
void InitFromImageWithAlpha(const wxImage& img)
|
||||
{
|
||||
hbmp = wxDIB(img, wxDIB::PixelFormat_NotPreMultiplied).Detach();
|
||||
hbmpRelease.Init(hbmp);
|
||||
}
|
||||
#endif // wxUSE_WXDIB && wxUSE_IMAGE
|
||||
|
||||
// These fields are filled by GetImageListBitmaps().
|
||||
HBITMAP hbmp;
|
||||
AutoHBITMAP hbmpMask;
|
||||
@@ -151,10 +161,6 @@ private:
|
||||
// shouldn't be used otherwise, so it's private.
|
||||
AutoHBITMAP hbmpRelease;
|
||||
|
||||
friend void wxImageList::GetImageListBitmaps(wxMSWBitmaps&,
|
||||
const wxBitmap&,
|
||||
const wxBitmap&);
|
||||
|
||||
wxDECLARE_NO_COPY_CLASS(wxMSWBitmaps);
|
||||
};
|
||||
|
||||
@@ -162,25 +168,41 @@ void
|
||||
wxImageList::GetImageListBitmaps(wxMSWBitmaps& bitmaps,
|
||||
const wxBitmap& bitmap, const wxBitmap& mask)
|
||||
{
|
||||
// This can be overwritten below if we need to modify the bitmap, but it
|
||||
// doesn't cost anything to initialize the bitmap with this HBITMAP.
|
||||
bitmaps.hbmp = GetHbitmapOf(bitmap);
|
||||
|
||||
#if wxUSE_WXDIB && wxUSE_IMAGE
|
||||
// We can only use directly bitmaps without alpha and without mask unless
|
||||
// the image list uses masks and need to modify bitmap in all the other
|
||||
// cases, so check if this is necessary.
|
||||
if ( bitmap.HasAlpha() || (!m_useMask && (mask.IsOk() || bitmap.GetMask())) )
|
||||
if ( wxApp::GetComCtl32Version() >= 600 )
|
||||
{
|
||||
wxBitmap bmp(bitmap);
|
||||
|
||||
if ( mask.IsOk() || bmp.GetMask() )
|
||||
if ( mask.IsOk() )
|
||||
{
|
||||
// Explicitly specified mask overrides the mask associated with the
|
||||
// bitmap, if any.
|
||||
if ( mask.IsOk() )
|
||||
bmp.SetMask(new wxMask(mask));
|
||||
bmp.SetMask(new wxMask(mask));
|
||||
}
|
||||
|
||||
if ( bmp.GetMask() )
|
||||
{
|
||||
// Get rid of the mask by converting it to alpha.
|
||||
if ( bmp.HasAlpha() )
|
||||
bmp.MSWBlendMaskWithAlpha();
|
||||
}
|
||||
else if ( m_useMask )
|
||||
{
|
||||
// Create the mask from the default transparent colour if we have
|
||||
// nothing else.
|
||||
if ( !bmp.HasAlpha() )
|
||||
bmp.SetMask(new wxMask(bmp, GetDefaultMaskColour()));
|
||||
}
|
||||
else
|
||||
{
|
||||
// We actually don't have to do anything at all and can just use
|
||||
// the original bitmap as is.
|
||||
return;
|
||||
}
|
||||
|
||||
// wxBitmap normally stores alpha in pre-multiplied format but
|
||||
// ImageList_Draw() does pre-multiplication internally so we need to undo
|
||||
@@ -190,8 +212,8 @@ wxImageList::GetImageListBitmaps(wxMSWBitmaps& bitmaps,
|
||||
wxImage img = bmp.ConvertToImage();
|
||||
if ( !img.HasAlpha() )
|
||||
img.InitAlpha();
|
||||
bitmaps.hbmp = wxDIB(img, wxDIB::PixelFormat_NotPreMultiplied).Detach();
|
||||
bitmaps.hbmpRelease.Init(bitmaps.hbmp);
|
||||
|
||||
bitmaps.InitFromImageWithAlpha(img);
|
||||
|
||||
// In any case we'll never use mask at the native image list level as
|
||||
// it's incompatible with alpha and we need to use alpha.
|
||||
@@ -199,9 +221,46 @@ wxImageList::GetImageListBitmaps(wxMSWBitmaps& bitmaps,
|
||||
else
|
||||
#endif // wxUSE_WXDIB && wxUSE_IMAGE
|
||||
{
|
||||
bitmaps.hbmp = GetHbitmapOf(bitmap);
|
||||
if ( m_useMask )
|
||||
bitmaps.hbmpMask.Init(GetMaskForImage(bitmap, mask));
|
||||
wxMask maskToUse;
|
||||
|
||||
HBITMAP hbmpMask = NULL;
|
||||
|
||||
// Always use mask if it is specified.
|
||||
if ( mask.IsOk() )
|
||||
{
|
||||
hbmpMask = GetHbitmapOf(mask);
|
||||
}
|
||||
else if ( bitmap.GetMask() )
|
||||
{
|
||||
hbmpMask = bitmap.GetMask()->GetMaskBitmap();
|
||||
}
|
||||
#if wxUSE_WXDIB && wxUSE_IMAGE
|
||||
// We can also use alpha, but we have to convert it to a mask as it is
|
||||
// not supported by this comctl32.dll version.
|
||||
else if ( bitmap.HasAlpha() )
|
||||
{
|
||||
wxImage img = bitmap.ConvertToImage();
|
||||
img.ConvertAlphaToMask();
|
||||
bitmaps.InitFromImageWithAlpha(img);
|
||||
|
||||
maskToUse.MSWCreateFromImageMask(img);
|
||||
}
|
||||
#endif // wxUSE_WXDIB && wxUSE_IMAGE
|
||||
// We don't have neither mask nor alpha, only force creating the
|
||||
// mask from colour if requested to do it.
|
||||
else if ( m_useMask )
|
||||
{
|
||||
maskToUse.Create(bitmap, GetDefaultMaskColour());
|
||||
}
|
||||
|
||||
if ( !hbmpMask )
|
||||
hbmpMask = maskToUse.GetMaskBitmap();
|
||||
|
||||
if ( hbmpMask )
|
||||
{
|
||||
// windows mask convention is opposite to the wxWidgets one
|
||||
bitmaps.hbmpMask.Init(wxInvertMask(hbmpMask));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -470,35 +529,14 @@ wxIcon wxImageList::GetIcon(int index) const
|
||||
// helpers
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
static HBITMAP GetMaskForImage(const wxBitmap& bitmap, const wxBitmap& mask)
|
||||
static wxColour GetDefaultMaskColour()
|
||||
{
|
||||
HBITMAP hbmpMask;
|
||||
wxScopedPtr<wxMask> maskDeleter;
|
||||
// use the light grey count as transparent: the trouble here is
|
||||
// that the light grey might have been changed by Windows behind
|
||||
// our back, so use the standard colour map to get its real value
|
||||
wxCOLORMAP *cmap = wxGetStdColourMap();
|
||||
wxColour col;
|
||||
wxRGBToColour(col, cmap[wxSTD_COL_BTNFACE].from);
|
||||
|
||||
if ( mask.IsOk() )
|
||||
{
|
||||
hbmpMask = GetHbitmapOf(mask);
|
||||
}
|
||||
else
|
||||
{
|
||||
wxMask* pMask = bitmap.GetMask();
|
||||
if ( !pMask )
|
||||
{
|
||||
// use the light grey count as transparent: the trouble here is
|
||||
// that the light grey might have been changed by Windows behind
|
||||
// our back, so use the standard colour map to get its real value
|
||||
wxCOLORMAP *cmap = wxGetStdColourMap();
|
||||
wxColour col;
|
||||
wxRGBToColour(col, cmap[wxSTD_COL_BTNFACE].from);
|
||||
|
||||
pMask = new wxMask(bitmap, col);
|
||||
|
||||
maskDeleter.reset(pMask);
|
||||
}
|
||||
|
||||
hbmpMask = (HBITMAP)pMask->GetMaskBitmap();
|
||||
}
|
||||
|
||||
// windows mask convention is opposite to the wxWidgets one
|
||||
return wxInvertMask(hbmpMask);
|
||||
return col;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user