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

@@ -62,6 +62,8 @@ private:
// Size of a single bitmap in the list. // Size of a single bitmap in the list.
wxSize m_size; wxSize m_size;
// Images in the list should have the same scale factor.
double m_scaleFactor;
wxDECLARE_DYNAMIC_CLASS_NO_COPY(wxGenericImageList); wxDECLARE_DYNAMIC_CLASS_NO_COPY(wxGenericImageList);
}; };

View File

@@ -48,13 +48,14 @@ bool wxGenericImageList::Create( int width, int height, bool mask, int WXUNUSED(
{ {
m_size = wxSize(width, height); m_size = wxSize(width, height);
m_useMask = mask; m_useMask = mask;
m_scaleFactor = 1.0;
return true; return true;
} }
namespace 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); wxBitmap bmp(bitmap);
if ( useMask ) if ( useMask )
@@ -69,7 +70,7 @@ wxBitmap GetImageListBitmap(const wxBitmap& bitmap, bool useMask, const wxSize&
#if wxUSE_IMAGE #if wxUSE_IMAGE
wxImage img = bmp.ConvertToImage(); wxImage img = bmp.ConvertToImage();
img.ClearAlpha(); img.ClearAlpha();
bmp = wxBitmap(img, -1, bmp.GetScaleFactor()); bmp = wxBitmap(img, -1, scaleFactor);
#endif // wxUSE_IMAGE #endif // wxUSE_IMAGE
} }
} }
@@ -81,7 +82,7 @@ wxBitmap GetImageListBitmap(const wxBitmap& bitmap, bool useMask, const wxSize&
#if wxUSE_IMAGE #if wxUSE_IMAGE
wxImage img = bmp.ConvertToImage(); wxImage img = bmp.ConvertToImage();
img.ConvertAlphaToMask(); img.ConvertAlphaToMask();
bmp = wxBitmap(img, -1, bmp.GetScaleFactor()); bmp = wxBitmap(img, -1, scaleFactor);
#endif // wxUSE_IMAGE #endif // wxUSE_IMAGE
} }
else else
@@ -107,7 +108,7 @@ wxBitmap GetImageListBitmap(const wxBitmap& bitmap, bool useMask, const wxSize&
#if wxUSE_IMAGE #if wxUSE_IMAGE
wxImage img = bmp.ConvertToImage(); wxImage img = bmp.ConvertToImage();
img.InitAlpha(); img.InitAlpha();
bmp = wxBitmap(img, -1, bmp.GetScaleFactor()); bmp = wxBitmap(img, -1, scaleFactor);
#else #else
bmp.SetMask(NULL); bmp.SetMask(NULL);
#endif // wxUSE_IMAGE #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 // 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. // size: adopt the size of our first bitmap as image size.
m_size = bitmapSize; 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, // 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. // ImageList_Add() does.
if ( bitmapSize.x == m_size.x ) 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 ) else if ( bitmapSize.x > m_size.x )
{ {
@@ -168,7 +176,7 @@ int wxGenericImageList::Add( const wxBitmap &bitmap )
for (int subIndex = 0; subIndex < numImages; subIndex++) for (int subIndex = 0; subIndex < numImages; subIndex++)
{ {
wxRect rect(m_size.x * subIndex, 0, m_size.x, m_size.y); 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 else
@@ -233,11 +241,17 @@ wxGenericImageList::Replace(int index,
if ( !DoGetPtr(index) ) if ( !DoGetPtr(index) )
return false; return false;
if ( bitmap.GetScaleFactor() != m_scaleFactor )
{
// All images in the list should have the same scale factor
return false;
}
wxBitmap bmp(bitmap); wxBitmap bmp(bitmap);
if ( mask.IsOk() ) if ( mask.IsOk() )
bmp.SetMask(new wxMask(mask)); 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; return true;
} }