From 3787f55a6b48880cfb0f59c7ef915ac3076d3b77 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 11 Jul 2021 19:31:51 +0100 Subject: [PATCH] Add wxDPIChangedEvent::Scale() and use it in this event handlers This is more concise and less error-prone than multiplying/dividing DPI values manually. No real changes except, maybe, in wxSlider code where the rounding was done differently before for some reason. --- include/wx/event.h | 7 +++++++ interface/wx/event.h | 32 ++++++++++++++++++++++++++++++++ src/generic/datavgen.cpp | 4 ++-- src/generic/grid.cpp | 4 ++-- src/msw/listctrl.cpp | 7 ++++--- src/msw/slider.cpp | 6 +----- src/msw/toolbar.cpp | 4 +--- src/stc/stc.cpp | 2 +- src/stc/stc.cpp.in | 2 +- 9 files changed, 51 insertions(+), 17 deletions(-) diff --git a/include/wx/event.h b/include/wx/event.h index ff7a38b48f..228df9e4f6 100644 --- a/include/wx/event.h +++ b/include/wx/event.h @@ -3140,6 +3140,13 @@ public: wxSize GetOldDPI() const { return m_oldDPI; } wxSize GetNewDPI() const { return m_newDPI; } + // Scale the value by the ratio between new and old DPIs carried by this + // event. + int ScaleX(int x) const { return wxRescaleCoord(x, m_newDPI.x, m_oldDPI.x); } + int ScaleY(int y) const { return wxRescaleCoord(y, m_newDPI.y, m_oldDPI.y); } + + wxSize Scale(wxSize sz) const { return wxSize(ScaleX(sz.x), ScaleY(sz.y)); } + virtual wxEvent *Clone() const wxOVERRIDE { return new wxDPIChangedEvent(*this); } private: diff --git a/interface/wx/event.h b/interface/wx/event.h index 8113b1b9a9..b6c9eb1829 100644 --- a/interface/wx/event.h +++ b/interface/wx/event.h @@ -3444,6 +3444,38 @@ public: Returns the new DPI. */ wxSize GetNewDPI() const; + + /** + Rescale a value in pixels to match the new DPI. + + This is a convenience function to use in wxEVT_DPI_CHANGED event + handlers, as they often need to update some sizes to the new DPI. + It simply calls wxRescaleCoord() with GetNewDPI() and GetOldDPI(). + + For example, the returned value will be twice bigger than the original + one when switching from normal (96) DPI to high (2x, 192) DPI. + + @since 3.1.6 + */ + wxSize Scale(wxSize sz) const; + + /** + Rescale horizontal component to match the new DPI. + + This is the same as Scale(), but for the horizontal component only. + + @since 3.1.6 + */ + int ScaleX(int x) const; + + /** + Rescale vertical component to match the new DPI. + + This is the same as Scale(), but for the vertical component only. + + @since 3.1.6 + */ + int ScaleY(int y) const; }; diff --git a/src/generic/datavgen.cpp b/src/generic/datavgen.cpp index a51b48190b..43c9facee3 100644 --- a/src/generic/datavgen.cpp +++ b/src/generic/datavgen.cpp @@ -5669,12 +5669,12 @@ void wxDataViewCtrl::OnDPIChanged(wxDPIChangedEvent& event) { int minWidth = m_cols[i]->GetMinWidth(); if ( minWidth > 0 ) - minWidth = minWidth * event.GetNewDPI().x / event.GetOldDPI().x; + minWidth = event.ScaleX(minWidth); m_cols[i]->SetMinWidth(minWidth); int width = m_cols[i]->WXGetSpecifiedWidth(); if ( width > 0 ) - width = width * event.GetNewDPI().x / event.GetOldDPI().x; + width = event.ScaleX(width); m_cols[i]->SetWidth(width); } } diff --git a/src/generic/grid.cpp b/src/generic/grid.cpp index b4b4fcdcc8..9e99ef2d74 100644 --- a/src/generic/grid.cpp +++ b/src/generic/grid.cpp @@ -5781,7 +5781,7 @@ void wxGrid::OnDPIChanged(wxDPIChangedEvent& event) if ( height <= 0 ) continue; - height = height * event.GetNewDPI().x / event.GetOldDPI().x; + height = event.ScaleY(height); total += height; m_rowHeights[i] = height; @@ -5804,7 +5804,7 @@ void wxGrid::OnDPIChanged(wxDPIChangedEvent& event) if ( width <= 0 ) continue; - width = width * event.GetNewDPI().x / event.GetOldDPI().x; + width = event.ScaleX(width); total += width; m_colWidths[i] = width; diff --git a/src/msw/listctrl.cpp b/src/msw/listctrl.cpp index 3e0b0ebcec..f48ad7e3e9 100644 --- a/src/msw/listctrl.cpp +++ b/src/msw/listctrl.cpp @@ -446,9 +446,10 @@ void wxListCtrl::OnDPIChanged(wxDPIChangedEvent &event) for ( int i = 0; i < numCols; ++i ) { int width = GetColumnWidth(i); - if ( width > 0 ) - width = width * event.GetNewDPI().x / event.GetOldDPI().x; - SetColumnWidth(i, width); + if ( width <= 0 ) + continue; + + SetColumnWidth(i, event.ScaleX(width)); } } diff --git a/src/msw/slider.cpp b/src/msw/slider.cpp index ccc1f5df95..6334a23fde 100644 --- a/src/msw/slider.cpp +++ b/src/msw/slider.cpp @@ -653,11 +653,7 @@ void wxSlider::OnDPIChanged(wxDPIChangedEvent& event) { int thumbLen = GetThumbLength(); - const double scaleFactor = (double)event.GetNewDPI().x / event.GetOldDPI().x; - const double thumbLenScaled = thumbLen * scaleFactor; - thumbLen = (int)(scaleFactor > 1.0 ? ceil(thumbLenScaled) : floor(thumbLenScaled)); - - SetThumbLength(thumbLen); + SetThumbLength(event.ScaleX(thumbLen)); } // ---------------------------------------------------------------------------- diff --git a/src/msw/toolbar.cpp b/src/msw/toolbar.cpp index af6dbc1ca1..07ccd29108 100644 --- a/src/msw/toolbar.cpp +++ b/src/msw/toolbar.cpp @@ -1949,8 +1949,6 @@ void wxToolBar::OnDPIChanged(wxDPIChangedEvent& event) { // Manually scale the size of the controls. Even though the font has been // updated, the internal size of the controls does not. - const float scaleFactor = (float)event.GetNewDPI().y / event.GetOldDPI().y; - wxToolBarToolsList::compatibility_iterator node; for ( node = m_tools.GetFirst(); node; node = node->GetNext() ) { @@ -1961,7 +1959,7 @@ void wxToolBar::OnDPIChanged(wxDPIChangedEvent& event) if ( wxControl* const control = tool->GetControl() ) { const wxSize oldSize = control->GetSize(); - wxSize newSize = oldSize * scaleFactor; + wxSize newSize = event.Scale(oldSize); // Use the best height for choice-based controls. // Scaling the current size does not work, because the control diff --git a/src/stc/stc.cpp b/src/stc/stc.cpp index 752a69c70a..a917e39432 100644 --- a/src/stc/stc.cpp +++ b/src/stc/stc.cpp @@ -5441,7 +5441,7 @@ void wxStyledTextCtrl::OnDPIChanged(wxDPIChangedEvent& evt) { // adjust the margins to the new DPI for ( int i = 0; i < SC_MAX_MARGIN; ++i ) { - SetMarginWidth(i, (int)wxMulDivInt32(GetMarginWidth(i), evt.GetNewDPI().y, evt.GetOldDPI().y)); + SetMarginWidth(i, evt.ScaleY(GetMarginWidth(i))); } // Hide auto-complete popup, there is no (easy) way to set it to the correct size diff --git a/src/stc/stc.cpp.in b/src/stc/stc.cpp.in index 81b5c7bf3f..7022ba7056 100644 --- a/src/stc/stc.cpp.in +++ b/src/stc/stc.cpp.in @@ -968,7 +968,7 @@ void wxStyledTextCtrl::OnDPIChanged(wxDPIChangedEvent& evt) { // adjust the margins to the new DPI for ( int i = 0; i < SC_MAX_MARGIN; ++i ) { - SetMarginWidth(i, (int)wxMulDivInt32(GetMarginWidth(i), evt.GetNewDPI().y, evt.GetOldDPI().y)); + SetMarginWidth(i, evt.ScaleY(GetMarginWidth(i))); } // Hide auto-complete popup, there is no (easy) way to set it to the correct size