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.
This commit is contained in:
Maarten Bent
2019-08-27 23:14:35 +02:00
parent 2704d32089
commit 13cb9d41d8
8 changed files with 29 additions and 40 deletions

View File

@@ -34,6 +34,7 @@
#endif #endif
class WXDLLIMPEXP_FWD_BASE wxArrayString; class WXDLLIMPEXP_FWD_BASE wxArrayString;
class WXDLLIMPEXP_FWD_CORE wxWindow;
struct WXDLLIMPEXP_FWD_CORE wxNativeEncodingInfo; struct WXDLLIMPEXP_FWD_CORE wxNativeEncodingInfo;
#if defined(_WX_X_FONTLIKE) #if defined(_WX_X_FONTLIKE)
@@ -118,11 +119,16 @@ public:
// set the XFLD // set the XFLD
void SetXFontName(const wxString& xFontName); void SetXFontName(const wxString& xFontName);
#elif defined(__WXMSW__) #elif defined(__WXMSW__)
wxNativeFontInfo(const LOGFONT& lf_) // Preserve compatibility in the semi-public (i.e. private, but still
: lf(lf_), // unfortunately used by some existing code outside of the library) API
pointSize(GetPointSizeAtPPI(lf.lfHeight)) // 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, // MSW-specific: get point size from LOGFONT height using specified DPI,
// or screen DPI when 0. // or screen DPI when 0.

View File

@@ -403,6 +403,11 @@ void wxFontRefData::Free()
// wxNativeFontInfo // wxNativeFontInfo
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
wxNativeFontInfo::wxNativeFontInfo(const LOGFONT& lf_, const wxWindow* win)
: lf(lf_),
pointSize(GetPointSizeAtPPI(lf.lfHeight, win ? win->GetDPI().y : 0))
{ }
/* static */ /* static */
float wxNativeFontInfo::GetPointSizeAtPPI(int lfHeight, int ppi) float wxNativeFontInfo::GetPointSizeAtPPI(int lfHeight, int ppi)
{ {

View File

@@ -152,7 +152,7 @@ int wxFontDialog::ShowModal()
if ( ChooseFont(&chooseFontStruct) != 0 ) if ( ChooseFont(&chooseFontStruct) != 0 )
{ {
wxRGBToColour(m_fontData.m_fontColour, chooseFontStruct.rgbColors); 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().facename = logFont.lfFaceName;
m_fontData.EncodingInfo().charset = logFont.lfCharSet; m_fontData.EncodingInfo().charset = logFont.lfCharSet;

View File

@@ -278,7 +278,7 @@ void wxFillLogFont(LOGFONT *logFont, const wxFont *font)
wxFont wxCreateFontFromLogFont(const LOGFONT *logFont) wxFont wxCreateFontFromLogFont(const LOGFONT *logFont)
{ {
return wxFont(wxNativeFontInfo(*logFont)); return wxFont(wxNativeFontInfo(*logFont, NULL));
} }
#endif // WXWIN_COMPATIBILITY_3_0 #endif // WXWIN_COMPATIBILITY_3_0

View File

@@ -348,7 +348,7 @@ void MenuDrawData::Init()
wxUxThemeFont themeFont; wxUxThemeFont themeFont;
::GetThemeSysFont(hTheme, TMT_MENUFONT, themeFont.GetPtr()); ::GetThemeSysFont(hTheme, TMT_MENUFONT, themeFont.GetPtr());
Font = wxFont(themeFont.GetLOGFONT()); Font = wxFont(wxNativeFontInfo(themeFont.GetLOGFONT(), window));
Theme = true; Theme = true;
@@ -392,15 +392,7 @@ void MenuDrawData::Init()
Offset = -12; Offset = -12;
wxNativeFontInfo info(metrics.lfMenuFont); Font = wxFont(wxNativeFontInfo(metrics.lfMenuFont, window));
// 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);
Theme = false; Theme = false;
} }

View File

@@ -397,15 +397,8 @@ void wxMessageDialog::AdjustButtonLabels()
wxFont wxMessageDialog::GetMessageFont() wxFont wxMessageDialog::GetMessageFont()
{ {
const wxWindow* win = wxTheApp ? wxTheApp->GetTopWindow() : NULL; const wxWindow* win = wxTheApp ? wxTheApp->GetTopWindow() : NULL;
wxNativeFontInfo info(wxMSWImpl::GetNonClientMetrics(win).lfMessageFont); const wxNativeFontInfo
info(wxMSWImpl::GetNonClientMetrics(win).lfMessageFont, win);
// 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);
}
return info; return info;
} }

View File

@@ -150,7 +150,7 @@ wxFont wxCreateFontFromStockObject(int index)
LOGFONT lf; LOGFONT lf;
if ( ::GetObject(hFont, sizeof(LOGFONT), &lf) != 0 ) if ( ::GetObject(hFont, sizeof(LOGFONT), &lf) != 0 )
{ {
wxNativeFontInfo info(lf); wxNativeFontInfo info(lf, NULL);
font.Create(info); font.Create(info);
} }
else else
@@ -183,16 +183,8 @@ wxFont wxSystemSettingsNative::GetFont(wxSystemFont index)
// controls may prefer to use lfStatusFont or lfCaptionFont if it // controls may prefer to use lfStatusFont or lfCaptionFont if it
// is more appropriate for them // is more appropriate for them
const wxWindow* win = wxTheApp ? wxTheApp->GetTopWindow() : NULL; const wxWindow* win = wxTheApp ? wxTheApp->GetTopWindow() : NULL;
wxNativeFontInfo const wxNativeFontInfo
info(wxMSWImpl::GetNonClientMetrics(win).lfMessageFont); info(wxMSWImpl::GetNonClientMetrics(win).lfMessageFont, win);
// 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);
}
gs_fontDefault = new wxFont(info); gs_fontDefault = new wxFont(info);
} }
@@ -358,7 +350,7 @@ extern wxFont wxGetCCDefaultFont()
win win
) ) ) )
{ {
return wxFont(lf); return wxFont(wxNativeFontInfo(lf, win));
} }
else else
{ {

View File

@@ -3207,7 +3207,8 @@ bool wxTextCtrl::GetStyle(long position, wxTextAttr& style)
// CHARFORMAT stores it to pixel-based units used by LOGFONT. // CHARFORMAT stores it to pixel-based units used by LOGFONT.
// Note that RichEdit seems to always use standard DPI of 96, even when the // Note that RichEdit seems to always use standard DPI of 96, even when the
// window is a monitor using a higher DPI. // 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.lfWidth = 0;
lf.lfCharSet = ANSI_CHARSET; // FIXME: how to get correct charset? lf.lfCharSet = ANSI_CHARSET; // FIXME: how to get correct charset?
lf.lfClipPrecision = 0; lf.lfClipPrecision = 0;
@@ -3240,7 +3241,7 @@ bool wxTextCtrl::GetStyle(long position, wxTextAttr& style)
else else
lf.lfWeight = FW_NORMAL; lf.lfWeight = FW_NORMAL;
wxFont font(lf); wxFont font(wxNativeFontInfo(lf, this));
if (font.IsOk()) if (font.IsOk())
{ {
style.SetFont(font); style.SetFont(font);