From 8f74ac7deff009d347ee50ff2761785e1bd35bbc Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 23 Oct 2021 21:19:49 +0200 Subject: [PATCH] Adjust scale of bitmaps returned by wxBitmapBundle::GetBitmap() Bitmaps returned from this function must have the appropriate scale factor, e.g. 2 in standard high DPI case, in order to be drawn at correct size in the ports where logical pixels are different from the physical ones, such as wxOSX and wxGTK3. Notably, this allows wxGenericStaticBitmap to work correctly in high DPI in these ports too. This also allows to undo some of the changes done in 399b0ff9ae (Use wxBitmapBundle instead of bitmap scale factor in wxGtkImage, 2021-10-16) to wxGtkImage code and keep using the same code that had been used before. --- src/common/bmpbndl.cpp | 11 ++++++++++- src/gtk/image_gtk.cpp | 15 ++------------- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/src/common/bmpbndl.cpp b/src/common/bmpbndl.cpp index becd229c9c..f89c3490f4 100644 --- a/src/common/bmpbndl.cpp +++ b/src/common/bmpbndl.cpp @@ -384,7 +384,16 @@ wxBitmap wxBitmapBundle::GetBitmap(const wxSize& size) const if ( !m_impl ) return wxBitmap(); - return m_impl->GetBitmap(size == wxDefaultSize ? GetDefaultSize() : size); + const wxSize sizeDef = GetDefaultSize(); + + wxBitmap bmp = m_impl->GetBitmap(size == wxDefaultSize ? sizeDef : size); + + // Ensure that the returned bitmap uses the scale factor such that it takes + // the same space, in logical pixels, as the bitmap in the default size. + if ( size != wxDefaultSize ) + bmp.SetScaleFactor(static_cast(size.y)/sizeDef.y); + + return bmp; } // ============================================================================ diff --git a/src/gtk/image_gtk.cpp b/src/gtk/image_gtk.cpp index 98ef9a8589..518b313f99 100644 --- a/src/gtk/image_gtk.cpp +++ b/src/gtk/image_gtk.cpp @@ -155,24 +155,13 @@ static gboolean wxGtkImageDraw(GtkWidget* widget, GdkEventExpose* event) #endif } - const double scaleFactor = image->m_provider->GetScale(); - GtkAllocation alloc; gtk_widget_get_allocation(widget, &alloc); - int x = (alloc.width - int(bitmap.GetWidth() /scaleFactor)) / 2; - int y = (alloc.height - int(bitmap.GetHeight()/scaleFactor)) / 2; + int x = (alloc.width - int(bitmap.GetScaledWidth() )) / 2; + int y = (alloc.height - int(bitmap.GetScaledHeight())) / 2; #ifdef __WXGTK3__ gtk_render_background(gtk_widget_get_style_context(widget), cr, 0, 0, alloc.width, alloc.height); - - if (!wxIsSameDouble(scaleFactor, 1)) - { - cairo_translate(cr, x, y); - const double scale = 1 / scaleFactor; - cairo_scale(cr, scale, scale); - x = 0; - y = 0; - } bitmap.Draw(cr, x, y); #else x += alloc.x;