From 123e21c1810845b997b8e2aa350b6ce2d4289150 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 31 May 2020 17:46:32 +0200 Subject: [PATCH] Refactor wxGrid::SetOrCalcColumnSizes() and SetOrCalcRowSizes() Get rid of the unnecessarily complicated functions doing two quite different things depending on whether their first boolean parameter was true of false. Instead, split their body between AutoSize{Columns,Rows}() (which used to call them) and DoGetBestSize(), keeping just the part needed in each case. This is much simpler and even more efficient, as it avoids a completely unnecessary call to CalcDimensions() and Refresh() from DoGetBestSize(), which doesn't change the current size at all and so doesn't need to refresh anything, but previously did it and not only once, but twice, because both of SetOrCalc{Column,Row}Sizes() did it. --- include/wx/generic/grid.h | 11 ++------- src/generic/grid.cpp | 52 ++++++++++++--------------------------- 2 files changed, 18 insertions(+), 45 deletions(-) diff --git a/include/wx/generic/grid.h b/include/wx/generic/grid.h index 5bc3c9dfff..3411de34ee 100644 --- a/include/wx/generic/grid.h +++ b/include/wx/generic/grid.h @@ -1869,11 +1869,8 @@ public: { AutoSizeColOrRow(row, setAsMin, wxGRID_ROW); } // auto size all columns (very ineffective for big grids!) - void AutoSizeColumns( bool setAsMin = true ) - { (void)SetOrCalcColumnSizes(false, setAsMin); } - - void AutoSizeRows( bool setAsMin = true ) - { (void)SetOrCalcRowSizes(false, setAsMin); } + void AutoSizeColumns( bool setAsMin = true ); + void AutoSizeRows( bool setAsMin = true ); // auto size the grid, that is make the columns/rows of the "right" size // and also set the grid size to just fit its contents @@ -2414,10 +2411,6 @@ protected: wxColour m_gridFrozenBorderColour; int m_gridFrozenBorderPenWidth; - // common part of AutoSizeColumn/Row() and GetBestSize() - int SetOrCalcColumnSizes(bool calcOnly, bool setAsMin = true); - int SetOrCalcRowSizes(bool calcOnly, bool setAsMin = true); - // common part of AutoSizeColumn/Row() void AutoSizeColOrRow(int n, bool setAsMin, wxGridDirection direction); diff --git a/src/generic/grid.cpp b/src/generic/grid.cpp index 266107031a..181e9b57a6 100644 --- a/src/generic/grid.cpp +++ b/src/generic/grid.cpp @@ -10206,50 +10206,28 @@ wxCoord wxGrid::CalcColOrRowLabelAreaMinSize(wxGridDirection direction) return extentMax; } -int wxGrid::SetOrCalcColumnSizes(bool calcOnly, bool setAsMin) +void wxGrid::AutoSizeColumns(bool setAsMin) { - int width = m_rowLabelWidth; - - wxGridUpdateLocker locker; - if(!calcOnly) - locker.Create(this); + wxGridUpdateLocker locker(this); for ( int col = 0; col < m_numCols; col++ ) - { - if ( !calcOnly ) - AutoSizeColumn(col, setAsMin); - - width += GetColWidth(col); - } - - return width; + AutoSizeColumn(col, setAsMin); } -int wxGrid::SetOrCalcRowSizes(bool calcOnly, bool setAsMin) +void wxGrid::AutoSizeRows(bool setAsMin) { - int height = m_colLabelHeight; - - wxGridUpdateLocker locker; - if(!calcOnly) - locker.Create(this); + wxGridUpdateLocker locker(this); for ( int row = 0; row < m_numRows; row++ ) - { - if ( !calcOnly ) - AutoSizeRow(row, setAsMin); - - height += GetRowHeight(row); - } - - return height; + AutoSizeRow(row, setAsMin); } void wxGrid::AutoSize() { wxGridUpdateLocker locker(this); - wxSize size(SetOrCalcColumnSizes(false) + m_extraWidth, - SetOrCalcRowSizes(false) + m_extraHeight); + AutoSizeColumns(); + AutoSizeRows(); // we know that we're not going to have scrollbars so disable them now to // avoid trouble in SetClientSize() which can otherwise set the correct @@ -10257,7 +10235,7 @@ void wxGrid::AutoSize() SetScrollbars(m_xScrollPixelsPerLine, m_yScrollPixelsPerLine, 0, 0, 0, 0, true); - SetClientSize(size); + SetSize(DoGetBestSize()); } void wxGrid::AutoSizeRowLabelSize( int row ) @@ -10294,12 +10272,14 @@ void wxGrid::AutoSizeColLabelSize( int col ) wxSize wxGrid::DoGetBestSize() const { - wxGrid * const self = const_cast(this); + wxSize size(m_rowLabelWidth + m_extraWidth, + m_colLabelHeight + m_extraHeight); - // we do the same as in AutoSize() here with the exception that we don't - // change the column/row sizes, only calculate them - wxSize size(self->SetOrCalcColumnSizes(true) + m_extraWidth, - self->SetOrCalcRowSizes(true) + m_extraHeight); + for ( int col = 0; col < m_numCols; col++ ) + size.x += GetColWidth(col); + + for ( int row = 0; row < m_numRows; row++ ) + size.y += GetRowHeight(row); return size + GetWindowBorderSize(); }