From 9326aa3b6cae794e651e921f4976e9b57ee70ea1 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Thu, 21 Oct 2021 02:42:13 +0100 Subject: [PATCH] Add wxBitmapBundle::GetBitmapFor() helper This can be used to get the bitmap of the size appropriate for the DPI scaling used for the given window. --- include/wx/bmpbndl.h | 5 +++++ interface/wx/bmpbndl.h | 13 +++++++++++++ src/common/bmpbndl.cpp | 5 +++++ 3 files changed, 23 insertions(+) diff --git a/include/wx/bmpbndl.h b/include/wx/bmpbndl.h index 6657fb8056..c0813b1cc4 100644 --- a/include/wx/bmpbndl.h +++ b/include/wx/bmpbndl.h @@ -109,6 +109,11 @@ public: // If size == wxDefaultSize, GetDefaultSize() is used for it instead. wxBitmap GetBitmap(const wxSize& size) const; + // Helper combining GetBitmap() and GetPreferredSizeFor(): returns the + // bitmap of the size appropriate for the current DPI scaling of the given + // window. + wxBitmap GetBitmapFor(const wxWindow* window) const; + // Access implementation wxBitmapBundleImpl* GetImpl() const { return m_impl.get(); } diff --git a/interface/wx/bmpbndl.h b/interface/wx/bmpbndl.h index 5e8b111358..49cd69f233 100644 --- a/interface/wx/bmpbndl.h +++ b/interface/wx/bmpbndl.h @@ -288,6 +288,19 @@ public: consume resources until the application termination. */ wxBitmap GetBitmap(const wxSize& size) const; + + /** + Get bitmap of the size appropriate for the DPI scaling used by the + given window. + + This helper function simply combines GetBitmap() and + GetPreferredSizeFor(), i.e. it returns a (normally unscaled) bitmap + from the bundle of the closest size to the size that should be used at + the DPI scaling of the provided window. + + @param window Non-null and fully created window. + */ + wxBitmap GetBitmapFor(const wxWindow* window) const; }; /** diff --git a/src/common/bmpbndl.cpp b/src/common/bmpbndl.cpp index 202c1fc435..c310fce88f 100644 --- a/src/common/bmpbndl.cpp +++ b/src/common/bmpbndl.cpp @@ -407,6 +407,11 @@ wxBitmap wxBitmapBundle::GetBitmap(const wxSize& size) const return bmp; } +wxBitmap wxBitmapBundle::GetBitmapFor(const wxWindow* window) const +{ + return GetBitmap(GetPreferredSizeFor(window)); +} + // ============================================================================ // wxBitmapBundleImpl implementation // ============================================================================