Fix grid autowrap row autosizing

Set the autowrap renderer DC font from the cell attribute before measuring
line heights, so small-font cells do not inherit another DC font when rows
are autosized.

Use the full spanned row or column extent when asking renderers for their
autosize measurement, and cover autowrap font and spanned-row sizing.

Fixes #14072.

Closes #27136.
This commit is contained in:
Richard
2026-10-01 16:22:59 +02:00
committed by Vadim Zeitlin
parent 4ff1b87a82
commit cb2cbef660
3 changed files with 76 additions and 5 deletions
+17 -5
View File
@@ -11033,11 +11033,23 @@ wxGrid::AutoSizeColOrRow(int colOrRow, bool setAsMin, wxGridDirection direction)
if ( renderer )
{
extent = column
? renderer->GetBestWidth(*this, *attr, dc, row, col,
GetRowHeight(row))
: renderer->GetBestHeight(*this, *attr, dc, row, col,
GetColWidth(col));
int availableExtent = 0;
if ( column )
{
for ( int n = 0; n < numRows; n++ )
availableExtent += GetRowHeight(row + n);
extent = renderer->GetBestWidth(*this, *attr, dc, row, col,
availableExtent);
}
else
{
for ( int n = 0; n < numCols; n++ )
availableExtent += GetColWidth(col + n);
extent = renderer->GetBestHeight(*this, *attr, dc, row, col,
availableExtent);
}
if ( span != CellSpan_None )
{
+6
View File
@@ -533,6 +533,8 @@ wxGridCellAutoWrapStringRenderer::GetBestSize(wxGrid& grid,
wxDC& dc,
int row, int col)
{
dc.SetFont(attr.GetFont());
// We have to make a choice here and fix either width or height because we
// don't have any naturally best size. This choice is mostly arbitrary, but
// we need to be consistent about it, otherwise wxGrid auto-sizing code
@@ -552,6 +554,8 @@ wxGridCellAutoWrapStringRenderer::GetBestHeight(wxGrid& grid,
int row, int col,
int width)
{
dc.SetFont(attr.GetFont());
const int lineHeight = dc.GetCharHeight();
// Use as many lines as we need for this width and add a small border to
@@ -567,6 +571,8 @@ wxGridCellAutoWrapStringRenderer::GetBestWidth(wxGrid& grid,
int row, int col,
int height)
{
dc.SetFont(attr.GetFont());
const int lineHeight = dc.GetCharHeight();
// Base the maximal number of lines either on how many fit or how many
+53
View File
@@ -1892,6 +1892,29 @@ void GridTestCase::CheckFirstColAutoSize(int expected)
CHECK(m_grid->GetColSize(0) == expected);
}
TEST_CASE_METHOD(GridTestCase, "Grid::AutoWrapStringRendererBestHeight",
"[grid]")
{
wxGridCellAutoWrapStringRenderer renderer;
wxGridCellAttrPtr attr(new wxGridCellAttr);
const int autoWrapMargin = 4;
const wxFont cellFont = m_grid->GetDefaultCellFont().Smaller();
attr->SetFont(cellFont);
m_grid->SetCellValue(0, 0, "one line");
wxClientDC dc(m_grid->GetGridWindow());
dc.SetFont(m_grid->GetDefaultCellFont().Larger());
const int height =
renderer.GetBestHeight(*m_grid, *attr, dc, 0, 0,
m_grid->GetColSize(0));
dc.SetFont(cellFont);
CHECK( height == dc.GetCharHeight() + autoWrapMargin );
}
TEST_CASE_METHOD(GridTestCase, "Grid::AutoSizeColumn", "[grid]")
{
#ifdef wxHAS_NATIVE_HEADER
@@ -2000,6 +2023,36 @@ TEST_CASE_METHOD(GridTestCase, "Grid::AutoSizeColumn", "[grid]")
}
}
TEST_CASE_METHOD(GridTestCase, "Grid::AutoSizeRow", "[grid]")
{
// Hardcoded extra margin for the rows used in grid.cpp.
const int margin = m_grid->FromDIP(6);
const int autoWrapMargin = 4;
m_grid->SetRowLabelValue(0, wxString());
const wxFont cellFont = m_grid->GetDefaultCellFont();
m_grid->SetCellFont(0, 0, cellFont);
wxClientDC dc(m_grid->GetGridWindow());
dc.SetFont(cellFont);
const wxString text = "Spanned text should fit";
const int textWidth = dc.GetTextExtent(text).x;
m_grid->SetCellValue(0, 0, text);
m_grid->SetCellRenderer(0, 0, new wxGridCellAutoWrapStringRenderer);
m_grid->SetCellSize(0, 0, 1, 2);
m_grid->SetColSize(0, textWidth / 2);
m_grid->SetColSize(1, textWidth - textWidth / 2);
m_grid->AutoSizeRow(0);
wxYield();
CHECK( m_grid->GetRowSize(0) ==
dc.GetCharHeight() + autoWrapMargin + margin );
}
TEST_CASE_METHOD(GridTestCase, "Grid::DrawInvalidCell", "[grid][multicell]")
{
// Set up a multicell with inside an overflowing cell.