diff --git a/include/wx/generic/imaglist.h b/include/wx/generic/imaglist.h index c2da30e487..8984ccfd3b 100644 --- a/include/wx/generic/imaglist.h +++ b/include/wx/generic/imaglist.h @@ -63,8 +63,6 @@ private: // Size of a single bitmap in the list. wxSize m_size; - // Images in the list should have the same scale factor. - double m_scaleFactor; wxDECLARE_DYNAMIC_CLASS_NO_COPY(wxGenericImageList); }; diff --git a/src/generic/imaglist.cpp b/src/generic/imaglist.cpp index 961ad5120b..76c0a107bd 100644 --- a/src/generic/imaglist.cpp +++ b/src/generic/imaglist.cpp @@ -63,7 +63,6 @@ bool wxGenericImageList::Create( int width, int height, bool mask, int WXUNUSED( // Prevent from storing negative dimensions m_size = wxSize(wxMax(width, 0), wxMax(height, 0)); m_useMask = mask; - m_scaleFactor = 1.0; // Images must have proper size return m_size != wxSize(0, 0); @@ -71,7 +70,7 @@ bool wxGenericImageList::Create( int width, int height, bool mask, int WXUNUSED( namespace { -wxBitmap GetImageListBitmap(const wxBitmap& bitmap, bool useMask, const wxSize& imgSize, double scaleFactor) +wxBitmap GetImageListBitmap(const wxBitmap& bitmap, bool useMask, const wxSize& imgSize) { wxBitmap bmp(bitmap); if ( useMask ) @@ -86,7 +85,7 @@ wxBitmap GetImageListBitmap(const wxBitmap& bitmap, bool useMask, const wxSize& #if wxUSE_IMAGE wxImage img = bmp.ConvertToImage(); img.ClearAlpha(); - bmp = wxBitmap(img, -1, scaleFactor); + bmp = wxBitmap(img, -1, bmp.GetScaleFactor()); #endif // wxUSE_IMAGE } } @@ -98,7 +97,7 @@ wxBitmap GetImageListBitmap(const wxBitmap& bitmap, bool useMask, const wxSize& #if wxUSE_IMAGE wxImage img = bmp.ConvertToImage(); img.ConvertAlphaToMask(); - bmp = wxBitmap(img, -1, scaleFactor); + bmp = wxBitmap(img, -1, bmp.GetScaleFactor()); #endif // wxUSE_IMAGE } else @@ -124,7 +123,7 @@ wxBitmap GetImageListBitmap(const wxBitmap& bitmap, bool useMask, const wxSize& #if wxUSE_IMAGE wxImage img = bmp.ConvertToImage(); img.InitAlpha(); - bmp = wxBitmap(img, -1, scaleFactor); + bmp = wxBitmap(img, -1, bmp.GetScaleFactor()); #else bmp.SetMask(NULL); #endif // wxUSE_IMAGE @@ -149,6 +148,7 @@ wxBitmap GetImageListBitmap(const wxBitmap& bitmap, bool useMask, const wxSize& #if wxUSE_IMAGE wxImage img = bmp.ConvertToImage(); // We need image with new physical size + const double scaleFactor = bmp.GetScaleFactor(); wxImage imgResized = img.Size(scaleFactor * imgSize, wxPoint(0, 0), 0, 0, 0); bmpResized = wxBitmap(imgResized, -1, scaleFactor); #else @@ -166,18 +166,6 @@ int wxGenericImageList::Add( const wxBitmap &bitmap ) if ( m_size == wxSize(0, 0) ) return -1; - if ( m_images.empty() ) - { - // This is the first time Add() is called so we should save - // scale factor to check if further images will 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; - } - // We use the logical size here as image list images size is specified in // logical pixels, just as window coordinates and sizes are. const wxSize bitmapSize = bitmap.GetLogicalSize(); @@ -187,7 +175,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_scaleFactor)); + m_images.push_back(GetImageListBitmap(bitmap, m_useMask, m_size)); } else if ( bitmapSize.x > m_size.x ) { @@ -195,7 +183,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_scaleFactor)); + m_images.push_back(GetImageListBitmap(bitmap.GetSubBitmap(rect), m_useMask, m_size)); } } else @@ -260,17 +248,11 @@ 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_scaleFactor); + m_images[index] = GetImageListBitmap(bmp, m_useMask, m_size); return true; } diff --git a/tests/graphics/imagelist.cpp b/tests/graphics/imagelist.cpp index cbf71a51ca..c657670160 100644 --- a/tests/graphics/imagelist.cpp +++ b/tests/graphics/imagelist.cpp @@ -751,224 +751,58 @@ TEST_CASE("ImageList:NegativeTests", "[imagelist][negative]") } } +// This test relies on logical pixels being different from physical ones. +#ifdef wxHAS_DPI_INDEPENDENT_PIXELS + TEST_CASE("ImageList:HiDPI", "[imagelist][hidpi]") { -#if defined(__WXMSW__) || ( defined(__WXGTK20__) && !defined(__WXGTK3__) ) + wxImage img1(8, 4); + img1.SetRGB(wxRect(0, 0, 8, 4), 0, 63, 127); + REQUIRE(img1.IsOk()); - WARN("Skipping HiDPI image tests known not to work in wxMSW and wxGTK2."); -#else - wxImage img(16, 8); - img.SetRGB(wxRect(0, 0, 16, 8), 255, 128, 64); - REQUIRE(img.IsOk()); + wxImage img2(16, 8); + img2.SetRGB(wxRect(0, 0, 16, 8), 255, 128, 64); + REQUIRE(img2.IsOk()); - wxBitmap bmp1x(img, -1, 1.0); + wxBitmap bmp1x(img1, -1, 1.0); REQUIRE(bmp1x.IsOk()); - CHECK(bmp1x.GetWidth() == 16); - CHECK(bmp1x.GetHeight() == 8); - CHECK(bmp1x.GetLogicalWidth() == 16); - CHECK(bmp1x.GetLogicalHeight() == 8); + CHECK(bmp1x.GetSize() == wxSize(8, 4)); + CHECK(bmp1x.GetLogicalSize() == wxSize(8, 4)); CHECK_FALSE(bmp1x.HasAlpha()); CHECK(bmp1x.GetMask() == NULL); - wxBitmap bmp2x(img, -1, 2.0); + wxBitmap bmp2x(img2, -1, 2.0); REQUIRE(bmp2x.IsOk()); - CHECK(bmp2x.GetWidth() == 16); - CHECK(bmp2x.GetHeight() == 8); - CHECK(bmp2x.GetLogicalWidth() == 8); - CHECK(bmp2x.GetLogicalHeight() == 4); + CHECK(bmp2x.GetSize() == wxSize(16, 8)); + CHECK(bmp2x.GetLogicalSize() == wxSize(8, 4)); CHECK_FALSE(bmp2x.HasAlpha()); CHECK(bmp2x.GetMask() == NULL); - SECTION("Add images 2x to the list 2x") - { - // Logical image size - wxImageList il(8, 4, false); + // Logical image size + wxImageList il(8, 4, false); - int idx = il.Add(bmp2x); - CHECK(idx == 0); - CHECK(il.GetImageCount() == 1); + int idx = il.Add(bmp2x); + REQUIRE(idx == 0); + REQUIRE(il.GetImageCount() == 1); - idx = il.Add(bmp1x); - CHECK(idx == -1); - CHECK(il.GetImageCount() == 1); + idx = il.Add(bmp1x); + REQUIRE(idx == 1); + REQUIRE(il.GetImageCount() == 2); - idx = il.Add(bmp2x); - CHECK(idx == 1); - CHECK(il.GetImageCount() == 2); + wxBitmap bmp = il.GetBitmap(0); + REQUIRE(bmp.IsOk() == true); + CHECK(bmp.GetScaleFactor() == 2.0); + CHECK(bmp.GetSize() == wxSize(16, 8)); + CHECK(bmp.GetLogicalSize() == wxSize(8, 4)); + CHECK_FALSE(bmp.HasAlpha()); + CHECK(bmp.GetMask() == NULL); - idx = il.Add(bmp1x); - CHECK(idx == -1); - CHECK(il.GetImageCount() == 2); - - wxBitmap bmp = il.GetBitmap(1); - CHECK(bmp.IsOk() == true); - CHECK(bmp.GetScaleFactor() == 2.0); - CHECK(bmp.GetLogicalWidth() == 8); - CHECK(bmp.GetLogicalHeight() == 4); - CHECK(bmp.GetWidth() == 16); - CHECK(bmp.GetHeight() == 8); - CHECK_FALSE(bmp.HasAlpha()); - CHECK(bmp.GetMask() == NULL); - } - - SECTION("Add images 2x to the list 1x") - { - // Logical image size - wxImageList il(16, 8, false); - - int idx = il.Add(bmp1x); - CHECK(idx == 0); - CHECK(il.GetImageCount() == 1); - - idx = il.Add(bmp2x); - CHECK(idx == -1); - CHECK(il.GetImageCount() == 1); - - idx = il.Add(bmp1x); - CHECK(idx == 1); - CHECK(il.GetImageCount() == 2); - - idx = il.Add(bmp2x); - CHECK(idx == -1); - CHECK(il.GetImageCount() == 2); - - wxBitmap bmp = il.GetBitmap(1); - CHECK(bmp.IsOk() == true); - CHECK(bmp.GetScaleFactor() == 1.0); - CHECK(bmp.GetLogicalWidth() == 16); - CHECK(bmp.GetLogicalHeight() == 8); - CHECK(bmp.GetWidth() == 16); - CHECK(bmp.GetHeight() == 8); - CHECK_FALSE(bmp.HasAlpha()); - CHECK(bmp.GetMask() == NULL); - } - - SECTION("Replaces images in the list 2x") - { - // Logical image size - wxImageList il(8, 4, false); - - int idx = il.Add(bmp2x); - CHECK(idx == 0); - CHECK(il.GetImageCount() == 1); - - idx = il.Add(bmp2x); - CHECK(idx == 1); - CHECK(il.GetImageCount() == 2); - - bool ok = il.Replace(1, bmp1x); - CHECK_FALSE(ok); - CHECK(il.GetImageCount() == 2); - - ok = il.Replace(0, bmp2x); - CHECK(ok == true); - CHECK(il.GetImageCount() == 2); - - wxBitmap bmp = il.GetBitmap(0); - CHECK(bmp.IsOk() == true); - CHECK(bmp.GetScaleFactor() == 2.0); - CHECK(bmp.GetLogicalWidth() == 8); - CHECK(bmp.GetLogicalHeight() == 4); - CHECK(bmp.GetWidth() == 16); - CHECK(bmp.GetHeight() == 8); - CHECK_FALSE(bmp.HasAlpha()); - CHECK(bmp.GetMask() == NULL); - } - - SECTION("Replaces images in the list 1x") - { - // Logical image size - wxImageList il(16, 8, false); - - int idx = il.Add(bmp1x); - CHECK(idx == 0); - CHECK(il.GetImageCount() == 1); - - idx = il.Add(bmp1x); - CHECK(idx == 1); - CHECK(il.GetImageCount() == 2); - - bool ok = il.Replace(1, bmp2x); - CHECK_FALSE(ok); - CHECK(il.GetImageCount() == 2); - - ok = il.Replace(0, bmp1x); - CHECK(ok == true); - CHECK(il.GetImageCount() == 2); - - wxBitmap bmp = il.GetBitmap(0); - CHECK(bmp.GetScaleFactor() == 1.0); - CHECK(bmp.GetLogicalWidth() == 16); - CHECK(bmp.GetLogicalHeight() == 8); - CHECK(bmp.GetWidth() == 16); - CHECK(bmp.GetHeight() == 8); - CHECK_FALSE(bmp.HasAlpha()); - CHECK(bmp.GetMask() == NULL); - } - - SECTION("Changes list 1x to 2x") - { - wxImage img2(32, 16); - img2.SetRGB(wxRect(0, 0, 32, 16), 255, 128, 64); - REQUIRE(img2.IsOk()); - - wxBitmap bmp2x2(img2, -1, 2.0); - REQUIRE(bmp2x2.IsOk()); - CHECK(bmp2x2.GetWidth() == 32); - CHECK(bmp2x2.GetHeight() == 16); - CHECK(bmp2x2.GetLogicalWidth() == 16); - CHECK(bmp2x2.GetLogicalHeight() == 8); - CHECK(bmp2x2.HasAlpha() == false); - CHECK(bmp2x2.GetMask() == NULL); - - // Logical image size - wxImageList il(16, 8, false); - - // Now it should be the list with 1x images - int idx = il.Add(bmp1x); - CHECK(idx == 0); - CHECK(il.GetImageCount() == 1); - - idx = il.Add(bmp1x); - CHECK(idx == 1); - CHECK(il.GetImageCount() == 2); - - idx = il.Add(bmp2x2); - CHECK(idx == -1); - CHECK(il.GetImageCount() == 2); - - wxBitmap bmp = il.GetBitmap(0); - CHECK(bmp.GetScaleFactor() == 1.0); - CHECK(bmp.GetLogicalWidth() == 16); - CHECK(bmp.GetLogicalHeight() == 8); - CHECK(bmp.GetWidth() == 16); - CHECK(bmp.GetHeight() == 8); - CHECK_FALSE(bmp.HasAlpha()); - CHECK(bmp.GetMask() == NULL); - - il.RemoveAll(); - - // Now it should be the list with 2x images (the same logical size 16x8) - idx = il.Add(bmp2x2); - CHECK(idx == 0); - CHECK(il.GetImageCount() == 1); - - idx = il.Add(bmp2x2); - CHECK(idx == 1); - CHECK(il.GetImageCount() == 2); - - idx = il.Add(bmp1x); - CHECK(idx == -1); - CHECK(il.GetImageCount() == 2); - - bmp = il.GetBitmap(0); - CHECK(bmp.GetScaleFactor() == 2.0); - CHECK(bmp.GetLogicalWidth() == 16); - CHECK(bmp.GetLogicalHeight() == 8); - CHECK(bmp.GetWidth() == 32); - CHECK(bmp.GetHeight() == 16); - CHECK_FALSE(bmp.HasAlpha()); - CHECK(bmp.GetMask() == NULL); - } -#endif // !__WXMSW__ + bmp = il.GetBitmap(1); + REQUIRE(bmp.IsOk() == true); + CHECK(bmp.GetScaleFactor() == 1.0); + CHECK(bmp.GetSize() == wxSize(8, 4)); + CHECK(bmp.GetLogicalSize() == wxSize(8, 4)); + CHECK_FALSE(bmp.HasAlpha()); + CHECK(bmp.GetMask() == NULL); } +#endif // wxHAS_DPI_INDEPENDENT_PIXELS