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.
This commit is contained in:
Vadim Zeitlin
2021-07-11 19:31:51 +01:00
parent 453468f2f9
commit 3787f55a6b
9 changed files with 51 additions and 17 deletions

View File

@@ -3140,6 +3140,13 @@ public:
wxSize GetOldDPI() const { return m_oldDPI; } wxSize GetOldDPI() const { return m_oldDPI; }
wxSize GetNewDPI() const { return m_newDPI; } 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); } virtual wxEvent *Clone() const wxOVERRIDE { return new wxDPIChangedEvent(*this); }
private: private:

View File

@@ -3444,6 +3444,38 @@ public:
Returns the new DPI. Returns the new DPI.
*/ */
wxSize GetNewDPI() const; 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;
}; };

View File

@@ -5669,12 +5669,12 @@ void wxDataViewCtrl::OnDPIChanged(wxDPIChangedEvent& event)
{ {
int minWidth = m_cols[i]->GetMinWidth(); int minWidth = m_cols[i]->GetMinWidth();
if ( minWidth > 0 ) if ( minWidth > 0 )
minWidth = minWidth * event.GetNewDPI().x / event.GetOldDPI().x; minWidth = event.ScaleX(minWidth);
m_cols[i]->SetMinWidth(minWidth); m_cols[i]->SetMinWidth(minWidth);
int width = m_cols[i]->WXGetSpecifiedWidth(); int width = m_cols[i]->WXGetSpecifiedWidth();
if ( width > 0 ) if ( width > 0 )
width = width * event.GetNewDPI().x / event.GetOldDPI().x; width = event.ScaleX(width);
m_cols[i]->SetWidth(width); m_cols[i]->SetWidth(width);
} }
} }

View File

@@ -5781,7 +5781,7 @@ void wxGrid::OnDPIChanged(wxDPIChangedEvent& event)
if ( height <= 0 ) if ( height <= 0 )
continue; continue;
height = height * event.GetNewDPI().x / event.GetOldDPI().x; height = event.ScaleY(height);
total += height; total += height;
m_rowHeights[i] = height; m_rowHeights[i] = height;
@@ -5804,7 +5804,7 @@ void wxGrid::OnDPIChanged(wxDPIChangedEvent& event)
if ( width <= 0 ) if ( width <= 0 )
continue; continue;
width = width * event.GetNewDPI().x / event.GetOldDPI().x; width = event.ScaleX(width);
total += width; total += width;
m_colWidths[i] = width; m_colWidths[i] = width;

View File

@@ -446,9 +446,10 @@ void wxListCtrl::OnDPIChanged(wxDPIChangedEvent &event)
for ( int i = 0; i < numCols; ++i ) for ( int i = 0; i < numCols; ++i )
{ {
int width = GetColumnWidth(i); int width = GetColumnWidth(i);
if ( width > 0 ) if ( width <= 0 )
width = width * event.GetNewDPI().x / event.GetOldDPI().x; continue;
SetColumnWidth(i, width);
SetColumnWidth(i, event.ScaleX(width));
} }
} }

View File

@@ -653,11 +653,7 @@ void wxSlider::OnDPIChanged(wxDPIChangedEvent& event)
{ {
int thumbLen = GetThumbLength(); int thumbLen = GetThumbLength();
const double scaleFactor = (double)event.GetNewDPI().x / event.GetOldDPI().x; SetThumbLength(event.ScaleX(thumbLen));
const double thumbLenScaled = thumbLen * scaleFactor;
thumbLen = (int)(scaleFactor > 1.0 ? ceil(thumbLenScaled) : floor(thumbLenScaled));
SetThumbLength(thumbLen);
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------

View File

@@ -1949,8 +1949,6 @@ void wxToolBar::OnDPIChanged(wxDPIChangedEvent& event)
{ {
// Manually scale the size of the controls. Even though the font has been // Manually scale the size of the controls. Even though the font has been
// updated, the internal size of the controls does not. // updated, the internal size of the controls does not.
const float scaleFactor = (float)event.GetNewDPI().y / event.GetOldDPI().y;
wxToolBarToolsList::compatibility_iterator node; wxToolBarToolsList::compatibility_iterator node;
for ( node = m_tools.GetFirst(); node; node = node->GetNext() ) for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
{ {
@@ -1961,7 +1959,7 @@ void wxToolBar::OnDPIChanged(wxDPIChangedEvent& event)
if ( wxControl* const control = tool->GetControl() ) if ( wxControl* const control = tool->GetControl() )
{ {
const wxSize oldSize = control->GetSize(); const wxSize oldSize = control->GetSize();
wxSize newSize = oldSize * scaleFactor; wxSize newSize = event.Scale(oldSize);
// Use the best height for choice-based controls. // Use the best height for choice-based controls.
// Scaling the current size does not work, because the control // Scaling the current size does not work, because the control

View File

@@ -5441,7 +5441,7 @@ void wxStyledTextCtrl::OnDPIChanged(wxDPIChangedEvent& evt) {
// adjust the margins to the new DPI // adjust the margins to the new DPI
for ( int i = 0; i < SC_MAX_MARGIN; ++i ) 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 // Hide auto-complete popup, there is no (easy) way to set it to the correct size

View File

@@ -968,7 +968,7 @@ void wxStyledTextCtrl::OnDPIChanged(wxDPIChangedEvent& evt) {
// adjust the margins to the new DPI // adjust the margins to the new DPI
for ( int i = 0; i < SC_MAX_MARGIN; ++i ) 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 // Hide auto-complete popup, there is no (easy) way to set it to the correct size