Assure that all images in the generic wxImageList have the same scale factor

Mixing images with various scale factors could be misleading and error
prone.
This commit is contained in:
Artur Wieczorek
2021-04-03 18:39:50 +02:00
parent eb52e86553
commit b6d305e4f2
2 changed files with 23 additions and 7 deletions

View File

@@ -48,13 +48,14 @@ bool wxGenericImageList::Create( int width, int height, bool mask, int WXUNUSED(
{
m_size = wxSize(width, height);
m_useMask = mask;
m_scaleFactor = 1.0;
return true;
}
namespace
{
wxBitmap GetImageListBitmap(const wxBitmap& bitmap, bool useMask, const wxSize& imgSize)
wxBitmap GetImageListBitmap(const wxBitmap& bitmap, bool useMask, const wxSize& imgSize, double scaleFactor)
{
wxBitmap bmp(bitmap);
if ( useMask )
@@ -69,7 +70,7 @@ wxBitmap GetImageListBitmap(const wxBitmap& bitmap, bool useMask, const wxSize&
#if wxUSE_IMAGE
wxImage img = bmp.ConvertToImage();
img.ClearAlpha();
bmp = wxBitmap(img, -1, bmp.GetScaleFactor());
bmp = wxBitmap(img, -1, scaleFactor);
#endif // wxUSE_IMAGE
}
}
@@ -81,7 +82,7 @@ wxBitmap GetImageListBitmap(const wxBitmap& bitmap, bool useMask, const wxSize&
#if wxUSE_IMAGE
wxImage img = bmp.ConvertToImage();
img.ConvertAlphaToMask();
bmp = wxBitmap(img, -1, bmp.GetScaleFactor());
bmp = wxBitmap(img, -1, scaleFactor);
#endif // wxUSE_IMAGE
}
else
@@ -107,7 +108,7 @@ wxBitmap GetImageListBitmap(const wxBitmap& bitmap, bool useMask, const wxSize&
#if wxUSE_IMAGE
wxImage img = bmp.ConvertToImage();
img.InitAlpha();
bmp = wxBitmap(img, -1, bmp.GetScaleFactor());
bmp = wxBitmap(img, -1, scaleFactor);
#else
bmp.SetMask(NULL);
#endif // wxUSE_IMAGE
@@ -153,6 +154,13 @@ int wxGenericImageList::Add( const wxBitmap &bitmap )
// This is the first time Add() is called and we hadn't had any fixed
// size: adopt the size of our first bitmap as image size.
m_size = bitmapSize;
// Save scale factor to check if all images have the same scaling
m_scaleFactor = bitmap.GetScaleFactor();
}
else if ( bitmap.GetScaleFactor() != m_scaleFactor )
{
// All images in the list should have the same scale factor
return -1;
}
// There is a special case: a bitmap may contain more than one image,
@@ -160,7 +168,7 @@ int wxGenericImageList::Add( const wxBitmap &bitmap )
// ImageList_Add() does.
if ( bitmapSize.x == m_size.x )
{
m_images.push_back(GetImageListBitmap(bitmap, m_useMask, m_size));
m_images.push_back(GetImageListBitmap(bitmap, m_useMask, m_size, m_scaleFactor));
}
else if ( bitmapSize.x > m_size.x )
{
@@ -168,7 +176,7 @@ int wxGenericImageList::Add( const wxBitmap &bitmap )
for (int subIndex = 0; subIndex < numImages; subIndex++)
{
wxRect rect(m_size.x * subIndex, 0, m_size.x, m_size.y);
m_images.push_back(GetImageListBitmap(bitmap.GetSubBitmap(rect), m_useMask, m_size));
m_images.push_back(GetImageListBitmap(bitmap.GetSubBitmap(rect), m_useMask, m_size, m_scaleFactor));
}
}
else
@@ -233,11 +241,17 @@ wxGenericImageList::Replace(int index,
if ( !DoGetPtr(index) )
return false;
if ( bitmap.GetScaleFactor() != m_scaleFactor )
{
// All images in the list should have the same scale factor
return false;
}
wxBitmap bmp(bitmap);
if ( mask.IsOk() )
bmp.SetMask(new wxMask(mask));
m_images[index] = GetImageListBitmap(bmp, m_useMask, m_size);
m_images[index] = GetImageListBitmap(bmp, m_useMask, m_size, m_scaleFactor);
return true;
}