From 13cb9d41d8f1ffc9cb11b9fe0c0a0ad59501ba45 Mon Sep 17 00:00:00 2001 From: Maarten Bent Date: Tue, 27 Aug 2019 23:14:35 +0200 Subject: [PATCH] Require wxWindow parameter for wxNativeFontInfo constructor in MSW Use the DPI of the window to determine the correct font pointSize. To not break user code, add a default argument when not building the library. --- include/wx/fontutil.h | 16 +++++++++++----- src/msw/font.cpp | 5 +++++ src/msw/fontdlg.cpp | 2 +- src/msw/fontutil.cpp | 2 +- src/msw/menuitem.cpp | 12 ++---------- src/msw/msgdlg.cpp | 11 ++--------- src/msw/settings.cpp | 16 ++++------------ src/msw/textctrl.cpp | 5 +++-- 8 files changed, 29 insertions(+), 40 deletions(-) diff --git a/include/wx/fontutil.h b/include/wx/fontutil.h index 424c100136..05a0a66666 100644 --- a/include/wx/fontutil.h +++ b/include/wx/fontutil.h @@ -34,6 +34,7 @@ #endif class WXDLLIMPEXP_FWD_BASE wxArrayString; +class WXDLLIMPEXP_FWD_CORE wxWindow; struct WXDLLIMPEXP_FWD_CORE wxNativeEncodingInfo; #if defined(_WX_X_FONTLIKE) @@ -118,11 +119,16 @@ public: // set the XFLD void SetXFontName(const wxString& xFontName); #elif defined(__WXMSW__) - wxNativeFontInfo(const LOGFONT& lf_) - : lf(lf_), - pointSize(GetPointSizeAtPPI(lf.lfHeight)) - { - } + // Preserve compatibility in the semi-public (i.e. private, but still + // unfortunately used by some existing code outside of the library) API + // by allowing to create wxNativeFontInfo from just LOGFONT, but ensure + // that we always specify the window, to use the correct DPI, when creating + // fonts inside the library itself. + wxNativeFontInfo(const LOGFONT& lf_, const wxWindow* win +#ifndef WXBUILDING + = NULL +#endif + ); // MSW-specific: get point size from LOGFONT height using specified DPI, // or screen DPI when 0. diff --git a/src/msw/font.cpp b/src/msw/font.cpp index e83b07488a..c54e2601ad 100644 --- a/src/msw/font.cpp +++ b/src/msw/font.cpp @@ -403,6 +403,11 @@ void wxFontRefData::Free() // wxNativeFontInfo // ---------------------------------------------------------------------------- +wxNativeFontInfo::wxNativeFontInfo(const LOGFONT& lf_, const wxWindow* win) + : lf(lf_), + pointSize(GetPointSizeAtPPI(lf.lfHeight, win ? win->GetDPI().y : 0)) +{ } + /* static */ float wxNativeFontInfo::GetPointSizeAtPPI(int lfHeight, int ppi) { diff --git a/src/msw/fontdlg.cpp b/src/msw/fontdlg.cpp index 8edd4a9f91..610b2c21b1 100644 --- a/src/msw/fontdlg.cpp +++ b/src/msw/fontdlg.cpp @@ -152,7 +152,7 @@ int wxFontDialog::ShowModal() if ( ChooseFont(&chooseFontStruct) != 0 ) { wxRGBToColour(m_fontData.m_fontColour, chooseFontStruct.rgbColors); - m_fontData.m_chosenFont = wxFont(wxNativeFontInfo(logFont)); + m_fontData.m_chosenFont = wxFont(wxNativeFontInfo(logFont, this)); m_fontData.EncodingInfo().facename = logFont.lfFaceName; m_fontData.EncodingInfo().charset = logFont.lfCharSet; diff --git a/src/msw/fontutil.cpp b/src/msw/fontutil.cpp index b55222a874..c22f668975 100644 --- a/src/msw/fontutil.cpp +++ b/src/msw/fontutil.cpp @@ -278,7 +278,7 @@ void wxFillLogFont(LOGFONT *logFont, const wxFont *font) wxFont wxCreateFontFromLogFont(const LOGFONT *logFont) { - return wxFont(wxNativeFontInfo(*logFont)); + return wxFont(wxNativeFontInfo(*logFont, NULL)); } #endif // WXWIN_COMPATIBILITY_3_0 diff --git a/src/msw/menuitem.cpp b/src/msw/menuitem.cpp index cbf114bb9a..3163cd5575 100644 --- a/src/msw/menuitem.cpp +++ b/src/msw/menuitem.cpp @@ -348,7 +348,7 @@ void MenuDrawData::Init() wxUxThemeFont themeFont; ::GetThemeSysFont(hTheme, TMT_MENUFONT, themeFont.GetPtr()); - Font = wxFont(themeFont.GetLOGFONT()); + Font = wxFont(wxNativeFontInfo(themeFont.GetLOGFONT(), window)); Theme = true; @@ -392,15 +392,7 @@ void MenuDrawData::Init() Offset = -12; - wxNativeFontInfo info(metrics.lfMenuFont); - // wxNativeFontInfo constructor calculates the pointSize using the - // main screen DPI. But lfHeight is based on the window DPI. - if ( window ) - { - info.pointSize = wxNativeFontInfo::GetPointSizeAtPPI( - info.lf.lfHeight, window->GetDPI().y); - } - Font = wxFont(info); + Font = wxFont(wxNativeFontInfo(metrics.lfMenuFont, window)); Theme = false; } diff --git a/src/msw/msgdlg.cpp b/src/msw/msgdlg.cpp index 21fbfc4e4b..54534f21f7 100644 --- a/src/msw/msgdlg.cpp +++ b/src/msw/msgdlg.cpp @@ -397,15 +397,8 @@ void wxMessageDialog::AdjustButtonLabels() wxFont wxMessageDialog::GetMessageFont() { const wxWindow* win = wxTheApp ? wxTheApp->GetTopWindow() : NULL; - wxNativeFontInfo info(wxMSWImpl::GetNonClientMetrics(win).lfMessageFont); - - // wxNativeFontInfo constructor calculates the pointSize using the - // main screen DPI. But lfHeight is based on the window DPI. - if ( win ) - { - info.pointSize = wxNativeFontInfo::GetPointSizeAtPPI( - info.lf.lfHeight, win->GetDPI().y); - } + const wxNativeFontInfo + info(wxMSWImpl::GetNonClientMetrics(win).lfMessageFont, win); return info; } diff --git a/src/msw/settings.cpp b/src/msw/settings.cpp index c2029cc67f..4be42f593d 100644 --- a/src/msw/settings.cpp +++ b/src/msw/settings.cpp @@ -150,7 +150,7 @@ wxFont wxCreateFontFromStockObject(int index) LOGFONT lf; if ( ::GetObject(hFont, sizeof(LOGFONT), &lf) != 0 ) { - wxNativeFontInfo info(lf); + wxNativeFontInfo info(lf, NULL); font.Create(info); } else @@ -183,16 +183,8 @@ wxFont wxSystemSettingsNative::GetFont(wxSystemFont index) // controls may prefer to use lfStatusFont or lfCaptionFont if it // is more appropriate for them const wxWindow* win = wxTheApp ? wxTheApp->GetTopWindow() : NULL; - wxNativeFontInfo - info(wxMSWImpl::GetNonClientMetrics(win).lfMessageFont); - - // wxNativeFontInfo constructor calculates the pointSize using the - // main screen DPI. But lfHeight is based on the window DPI. - if ( win ) - { - info.pointSize = wxNativeFontInfo::GetPointSizeAtPPI( - info.lf.lfHeight, win->GetDPI().y); - } + const wxNativeFontInfo + info(wxMSWImpl::GetNonClientMetrics(win).lfMessageFont, win); gs_fontDefault = new wxFont(info); } @@ -358,7 +350,7 @@ extern wxFont wxGetCCDefaultFont() win ) ) { - return wxFont(lf); + return wxFont(wxNativeFontInfo(lf, win)); } else { diff --git a/src/msw/textctrl.cpp b/src/msw/textctrl.cpp index a9139507ff..82c346a85b 100644 --- a/src/msw/textctrl.cpp +++ b/src/msw/textctrl.cpp @@ -3207,7 +3207,8 @@ bool wxTextCtrl::GetStyle(long position, wxTextAttr& style) // CHARFORMAT stores it to pixel-based units used by LOGFONT. // Note that RichEdit seems to always use standard DPI of 96, even when the // window is a monitor using a higher DPI. - lf.lfHeight = wxNativeFontInfo::GetLogFontHeightAtPPI(cf.yHeight/20.0f, 96); + lf.lfHeight = wxNativeFontInfo::GetLogFontHeightAtPPI(cf.yHeight/20.0f, + GetDPI().y); lf.lfWidth = 0; lf.lfCharSet = ANSI_CHARSET; // FIXME: how to get correct charset? lf.lfClipPrecision = 0; @@ -3240,7 +3241,7 @@ bool wxTextCtrl::GetStyle(long position, wxTextAttr& style) else lf.lfWeight = FW_NORMAL; - wxFont font(lf); + wxFont font(wxNativeFontInfo(lf, this)); if (font.IsOk()) { style.SetFont(font);