From e8325a8e2dca67aeb4c0989c379de125286da8fb Mon Sep 17 00:00:00 2001 From: bubnikv Date: Wed, 30 Sep 2020 14:02:03 +0200 Subject: [PATCH] Fixes of DPI scaling on Windows. --- src/slic3r/GUI/AboutDialog.cpp | 4 ++-- src/slic3r/GUI/ConfigSnapshotDialog.cpp | 4 ++-- src/slic3r/GUI/GUI_Utils.cpp | 6 +++--- src/slic3r/GUI/GUI_Utils.hpp | 20 ++++++++------------ src/slic3r/GUI/SysInfoDialog.cpp | 4 ++-- 5 files changed, 17 insertions(+), 21 deletions(-) diff --git a/src/slic3r/GUI/AboutDialog.cpp b/src/slic3r/GUI/AboutDialog.cpp index 8d9ea97b9..2b1bea13c 100644 --- a/src/slic3r/GUI/AboutDialog.cpp +++ b/src/slic3r/GUI/AboutDialog.cpp @@ -59,7 +59,7 @@ CopyrightsDialog::CopyrightsDialog() m_html = new wxHtmlWindow(this, wxID_ANY, wxDefaultPosition, wxSize(40 * em_unit(), 20 * em_unit()), wxHW_SCROLLBAR_AUTO); - wxFont font = get_default_font_for_dpi(get_dpi_for_window(this));// GetFont(); + wxFont font = get_default_font(this); const int fs = font.GetPointSize(); const int fs2 = static_cast(1.2f*fs); int size[] = { fs, fs, fs, fs, fs2, fs2, fs2 }; @@ -269,7 +269,7 @@ AboutDialog::AboutDialog() m_html = new wxHtmlWindow(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxHW_SCROLLBAR_AUTO/*NEVER*/); { m_html->SetMinSize(wxSize(-1, 16 * wxGetApp().em_unit())); - wxFont font = get_default_font_for_dpi(get_dpi_for_window(this));// GetFont(); + wxFont font = get_default_font(this); const auto text_clr = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT); auto text_clr_str = wxString::Format(wxT("#%02X%02X%02X"), text_clr.Red(), text_clr.Green(), text_clr.Blue()); auto bgr_clr_str = wxString::Format(wxT("#%02X%02X%02X"), bgr_clr.Red(), bgr_clr.Green(), bgr_clr.Blue()); diff --git a/src/slic3r/GUI/ConfigSnapshotDialog.cpp b/src/slic3r/GUI/ConfigSnapshotDialog.cpp index 4855bea81..48b5a2b00 100644 --- a/src/slic3r/GUI/ConfigSnapshotDialog.cpp +++ b/src/slic3r/GUI/ConfigSnapshotDialog.cpp @@ -114,7 +114,7 @@ ConfigSnapshotDialog::ConfigSnapshotDialog(const Config::SnapshotDB &snapshot_db // text html = new wxHtmlWindow(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxHW_SCROLLBAR_AUTO); { - wxFont font = get_default_font_for_dpi(get_dpi_for_window(this));// wxGetApp().normal_font();//wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT); + wxFont font = get_default_font(this); #ifdef __WXMSW__ const int fs = font.GetPointSize(); const int fs1 = static_cast(0.8f*fs); @@ -140,7 +140,7 @@ ConfigSnapshotDialog::ConfigSnapshotDialog(const Config::SnapshotDB &snapshot_db void ConfigSnapshotDialog::on_dpi_changed(const wxRect &suggested_rect) { - wxFont font = get_default_font_for_dpi(get_dpi_for_window(this));// GetFont(); + wxFont font = get_default_font(this); const int fs = font.GetPointSize(); const int fs1 = static_cast(0.8f*fs); const int fs2 = static_cast(1.1f*fs); diff --git a/src/slic3r/GUI/GUI_Utils.cpp b/src/slic3r/GUI/GUI_Utils.cpp index 7db3d57ff..97e66812a 100644 --- a/src/slic3r/GUI/GUI_Utils.cpp +++ b/src/slic3r/GUI/GUI_Utils.cpp @@ -75,7 +75,7 @@ template typename F::FN winapi_get_function(const wchar_t *dll, const c #endif // If called with nullptr, a DPI for the primary monitor is returned. -int get_dpi_for_window(wxWindow *window) +int get_dpi_for_window(const wxWindow *window) { #ifdef _WIN32 enum MONITOR_DPI_TYPE_ { @@ -126,7 +126,7 @@ int get_dpi_for_window(wxWindow *window) #endif } -wxFont get_default_font_for_dpi(int dpi) +wxFont get_default_font_for_dpi(const wxWindow *window, int dpi) { #ifdef _WIN32 // First try to load the font with the Windows 10 specific way. @@ -137,7 +137,7 @@ wxFont get_default_font_for_dpi(int dpi) memset(&nm, 0, sizeof(NONCLIENTMETRICS)); nm.cbSize = sizeof(NONCLIENTMETRICS); if (SystemParametersInfoForDpi_fn(SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICS), &nm, 0, dpi)) - return wxFont(wxNativeFontInfo(nm.lfMessageFont)); + return wxFont(wxNativeFontInfo(nm.lfMessageFont, window)); } // Then try to guesstimate the font DPI scaling on Windows 8. // Let's hope that the font returned by the SystemParametersInfo(), which is used by wxWidgets internally, makes sense. diff --git a/src/slic3r/GUI/GUI_Utils.hpp b/src/slic3r/GUI/GUI_Utils.hpp index 1c88de570..a68a0a88a 100644 --- a/src/slic3r/GUI/GUI_Utils.hpp +++ b/src/slic3r/GUI/GUI_Utils.hpp @@ -53,8 +53,9 @@ void on_window_geometry(wxTopLevelWindow *tlw, std::function callback); enum { DPI_DEFAULT = 96 }; -int get_dpi_for_window(wxWindow *window); -wxFont get_default_font_for_dpi(int dpi); +int get_dpi_for_window(const wxWindow *window); +wxFont get_default_font_for_dpi(const wxWindow* window, int dpi); +inline wxFont get_default_font(const wxWindow* window) { return get_default_font_for_dpi(window, get_dpi_for_window(window)); } #if !wxVERSION_EQUAL_OR_GREATER_THAN(3,1,3) struct DpiChangedEvent : public wxEvent { @@ -84,7 +85,7 @@ public: int dpi = get_dpi_for_window(this); m_scale_factor = (float)dpi / (float)DPI_DEFAULT; m_prev_scale_factor = m_scale_factor; - m_normal_font = get_default_font_for_dpi(dpi); + m_normal_font = get_default_font_for_dpi(this, dpi); /* Because of default window font is a primary display font, * We should set correct font for window before getting em_unit value. @@ -106,15 +107,10 @@ public: #if wxVERSION_EQUAL_OR_GREATER_THAN(3,1,3) this->Bind(wxEVT_DPI_CHANGED, [this](wxDPIChangedEvent& evt) { - m_scale_factor = (float)evt.GetNewDPI().x / (float)DPI_DEFAULT; - - m_new_font_point_size = get_default_font_for_dpi(evt.GetNewDPI().x).GetPointSize(); - - if (!m_can_rescale) - return; - - if (m_force_rescale || is_new_scale_factor()) - rescale(wxRect()); + m_scale_factor = (float)evt.GetNewDPI().x / (float)DPI_DEFAULT; + m_new_font_point_size = get_default_font_for_dpi(this, evt.GetNewDPI().x).GetPointSize(); + if (m_can_rescale && (m_force_rescale || is_new_scale_factor())) + rescale(wxRect()); }); #else this->Bind(EVT_DPI_CHANGED_SLICER, [this](const DpiChangedEvent& evt) { diff --git a/src/slic3r/GUI/SysInfoDialog.cpp b/src/slic3r/GUI/SysInfoDialog.cpp index 34905fa6d..14d4fb0b3 100644 --- a/src/slic3r/GUI/SysInfoDialog.cpp +++ b/src/slic3r/GUI/SysInfoDialog.cpp @@ -129,7 +129,7 @@ SysInfoDialog::SysInfoDialog() } // main_info_text - wxFont font = get_default_font_for_dpi(get_dpi_for_window(this));// wxGetApp().normal_font(); + wxFont font = get_default_font(this); const auto text_clr = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT); auto text_clr_str = wxString::Format(wxT("#%02X%02X%02X"), text_clr.Red(), text_clr.Green(), text_clr.Blue()); auto bgr_clr_str = wxString::Format(wxT("#%02X%02X%02X"), bgr_clr.Red(), bgr_clr.Green(), bgr_clr.Blue()); @@ -195,7 +195,7 @@ void SysInfoDialog::on_dpi_changed(const wxRect &suggested_rect) m_logo_bmp.msw_rescale(); m_logo->SetBitmap(m_logo_bmp.bmp()); - wxFont font = get_default_font_for_dpi(get_dpi_for_window(this));// GetFont(); + wxFont font = get_default_font(this); const int fs = font.GetPointSize() - 1; int font_size[] = { static_cast(fs*1.5), static_cast(fs*1.4), static_cast(fs*1.3), fs, fs, fs, fs };