Add wxGridCellChoiceRenderer to optimize column size calculation

Previously columns using a set of predetermined values used plain
wxGridCellStringRenderer, which didn't allow to determine their best
size efficiently, as wxGrid had to iterate over all the rows of the
table, even if they only took a couple of possible values.

Add wxGridCellChoiceRenderer (refactoring wxGridCellEnumRenderer to
extract the common code from it in the process) which implements
GetMaxBestSize() by just finding the best size of all of these values,
which is much faster for large grids.

This does result in a change in behaviour, as the column now adapts to
its "theoretical" best size and not just the size of the values actually
shown in it, but this seems to be a worthwhile trade-off and could even
be seen as an advantage, as editing a cell won't make its value overflow
the auto-sized column width any more, as it is wide enough to show any
of the column values.
This commit is contained in:
Vadim Zeitlin
2020-06-13 16:10:14 +02:00
parent 71d42a8290
commit 96de24d1bb
3 changed files with 63 additions and 44 deletions

View File

@@ -221,8 +221,37 @@ protected:
#endif // wxUSE_DATETIME
// Renderer for fields taking one of a limited set of values: this is the same
// as the renderer for strings, except that it can implement GetMaxBestSize().
class WXDLLIMPEXP_ADV wxGridCellChoiceRenderer : public wxGridCellStringRenderer
{
public:
wxGridCellChoiceRenderer() { }
virtual wxSize GetMaxBestSize(wxGrid& grid,
wxGridCellAttr& attr,
wxDC& dc) wxOVERRIDE;
// Parameters string is a comma-separated list of values.
virtual void SetParameters(const wxString& params) wxOVERRIDE;
virtual wxGridCellRenderer *Clone() const wxOVERRIDE
{
return new wxGridCellChoiceRenderer(*this);
}
protected:
wxGridCellChoiceRenderer(const wxGridCellChoiceRenderer& other)
: m_choices(other.m_choices)
{
}
wxArrayString m_choices;
};
// renders a number using the corresponding text string
class WXDLLIMPEXP_ADV wxGridCellEnumRenderer : public wxGridCellStringRenderer
class WXDLLIMPEXP_ADV wxGridCellEnumRenderer : public wxGridCellChoiceRenderer
{
public:
wxGridCellEnumRenderer( const wxString& choices = wxEmptyString );
@@ -240,20 +269,10 @@ public:
wxDC& dc,
int row, int col) wxOVERRIDE;
virtual wxSize GetMaxBestSize(wxGrid& grid,
wxGridCellAttr& attr,
wxDC& dc) wxOVERRIDE;
virtual wxGridCellRenderer *Clone() const wxOVERRIDE;
// parameters string format is "item1[,item2[...,itemN]]" where itemN will
// be used if the cell value is N-1
virtual void SetParameters(const wxString& params) wxOVERRIDE;
protected:
wxString GetString(const wxGrid& grid, int row, int col);
wxArrayString m_choices;
};