From 8efc6fb0036aaa17f2646b558031cb09aedcc9d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Va=CC=81clav=20Slavi=CC=81k?= Date: Thu, 27 Aug 2020 15:49:48 +0200 Subject: [PATCH] Add wxWindow::WXAdjustFontToOwnPPI() Avoid calling GeTDPI() in font.WXAdjustToPPI(GetDPI()); invocations in common code on platforms that don't need any adjustment (i.e. anything other than MSW). This fixes wxOSX crashes when GetFont() is called too early during window creation, but is the right thing to do regardless. Closes https://github.com/wxWidgets/wxWidgets/pull/2036 Closes #18903. --- include/wx/font.h | 7 ------- include/wx/msw/font.h | 6 +++++- include/wx/msw/window.h | 2 ++ include/wx/window.h | 7 +++++++ src/common/wincmn.cpp | 4 ++-- src/msw/window.cpp | 5 +++++ 6 files changed, 21 insertions(+), 10 deletions(-) diff --git a/include/wx/font.h b/include/wx/font.h index 6b3ebd3c97..fa95bc192f 100644 --- a/include/wx/font.h +++ b/include/wx/font.h @@ -476,13 +476,6 @@ public: // account as well. static int GetNumericWeightOf(wxFontWeight weight); - // Some ports need to modify the font object when the DPI of the window it - // is used with changes, this function can be used to do it. - // - // Currently it is only used in wxMSW and is not considered to be part of - // wxWidgets public API. - virtual void WXAdjustToPPI(const wxSize& WXUNUSED(ppi)) { } - // this doesn't do anything and is kept for compatibility only #if WXWIN_COMPATIBILITY_2_8 wxDEPRECATED_INLINE(void SetNoAntiAliasing(bool no = true), wxUnusedVar(no);) diff --git a/include/wx/msw/font.h b/include/wx/msw/font.h index c5b2bb8286..0f9768b44e 100644 --- a/include/wx/msw/font.h +++ b/include/wx/msw/font.h @@ -120,7 +120,11 @@ public: virtual bool IsFixedWidth() const wxOVERRIDE; - virtual void WXAdjustToPPI(const wxSize& ppi) wxOVERRIDE; + // MSW needs to modify the font object when the DPI of the window it + // is used with changes, this function can be used to do it. + // + // This method is not considered to be part of wxWidgets public API. + void WXAdjustToPPI(const wxSize& ppi); wxDEPRECATED_MSG("use wxFONT{FAMILY,STYLE,WEIGHT}_XXX constants ie: wxFONTFAMILY_SWISS, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_BOLD") wxFont(int size, diff --git a/include/wx/msw/window.h b/include/wx/msw/window.h index b5d21eb7b8..9e09786d9b 100644 --- a/include/wx/msw/window.h +++ b/include/wx/msw/window.h @@ -599,6 +599,8 @@ public: void MSWUpdateOnDPIChange(const wxSize& oldDPI, const wxSize& newDPI); protected: + virtual void WXAdjustFontToOwnPPI(wxFont& font) const wxOVERRIDE; + // Called from MSWUpdateOnDPIChange() specifically to update the control // font, as this may need to be done differently for some specific native // controls. The default version updates m_font of this window. diff --git a/include/wx/window.h b/include/wx/window.h index 1c3cc3d759..8de7570428 100644 --- a/include/wx/window.h +++ b/include/wx/window.h @@ -964,6 +964,13 @@ public: // Get the DPI used by the given window or wxSize(0, 0) if unknown. virtual wxSize GetDPI() const; + // Some ports need to modify the font object when the DPI of the window it + // is used with changes, this function can be used to do it. + // + // Currently it is only used in wxMSW and is not considered to be part of + // wxWidgets public API. + virtual void WXAdjustFontToOwnPPI(wxFont& WXUNUSED(font)) const { } + // DPI-independent pixels, or DIPs, are pixel values for the standard // 96 DPI display, they are scaled to take the current resolution into // account (i.e. multiplied by the same factor as returned by diff --git a/src/common/wincmn.cpp b/src/common/wincmn.cpp index 2919c60416..e7d3aba767 100644 --- a/src/common/wincmn.cpp +++ b/src/common/wincmn.cpp @@ -1710,7 +1710,7 @@ wxFont wxWindowBase::GetFont() const if ( !font.IsOk() ) font = GetClassDefaultAttributes().font; - font.WXAdjustToPPI(GetDPI()); + WXAdjustFontToOwnPPI(font); return font; } @@ -1731,7 +1731,7 @@ bool wxWindowBase::SetFont(const wxFont& font) m_inheritFont = m_hasFont; if ( m_hasFont ) - m_font.WXAdjustToPPI(GetDPI()); + WXAdjustFontToOwnPPI(m_font); InvalidateBestSize(); diff --git a/src/msw/window.cpp b/src/msw/window.cpp index 123eb58bde..af90f27760 100644 --- a/src/msw/window.cpp +++ b/src/msw/window.cpp @@ -4864,6 +4864,11 @@ double wxWindowMSW::GetDPIScaleFactor() const return GetDPI().y / (double)wxDisplay::GetStdPPIValue(); } +void wxWindowMSW::WXAdjustFontToOwnPPI(wxFont& font) const +{ + font.WXAdjustToPPI(GetDPI()); +} + void wxWindowMSW::MSWUpdateFontOnDPIChange(const wxSize& newDPI) { if ( m_font.IsOk() )