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:
@@ -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:
|
||||
|
@@ -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;
|
||||
};
|
||||
|
||||
|
||||
|
@@ -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);
|
||||
}
|
||||
}
|
||||
|
@@ -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;
|
||||
|
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -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));
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
@@ -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
|
||||
|
@@ -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
|
||||
|
@@ -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
|
||||
|
Reference in New Issue
Block a user