Update grid row/column sizes when switching DPI

Because the size of the text (usually) shown in the grid changes, the
size of its columns/rows should change as well when DPI changes. Scale
them by the DPI ratio if necessary, i.e. if any of them were set to
non-default values to achieve this.

Note that this is not ideal because it can result in rounding errors and
hence in slight change of row/column size when moving the grid window to
another display with a different DPI and then back, but to prevent this
from happening we'd need to store the column/rows sizes in DIPs inside
wxGrid and convert them to actual physical pixels everywhere, which
would require much more changes. Still, if the round trip problems turn
out to be a real problem in practice, we probably will need to do this.

Also note that this doesn't take care of the grid with a native header,
but this will be addressed by upcoming commits.
This commit is contained in:
Vadim Zeitlin
2020-04-01 00:58:59 +02:00
parent 5e761ad99f
commit 5525e0ed3f

View File

@@ -5354,9 +5354,50 @@ void wxGrid::OnDPIChanged(wxDPIChangedEvent& event)
{
InitPixelFields();
// If we have any non-default row sizes, we need to scale them (default
// ones will be scaled due to the reinitialization of m_defaultRowHeight
// inside InitPixelFields() above).
if ( !m_rowHeights.empty() )
{
int total = 0;
for ( unsigned i = 0; i < m_rowHeights.size(); ++i )
{
int height = m_rowHeights[i];
// Skip hidden rows.
if ( height <= 0 )
continue;
height = height * event.GetNewDPI().x / event.GetOldDPI().x;
total += height;
m_rowHeights[i] = height;
m_rowBottoms[i] = total;
}
}
// Similarly for columns.
if ( !m_colWidths.empty() )
{
int total = 0;
for ( unsigned i = 0; i < m_colWidths.size(); ++i )
{
int width = m_colWidths[i];
if ( width <= 0 )
continue;
width = width * event.GetNewDPI().x / event.GetOldDPI().x;
total += width;
m_colWidths[i] = width;
m_colRights[i] = total;
}
}
InvalidateBestSize();
CalcWindowSizes();
CalcDimensions();
Refresh();