Implement wxGridCellNumberRenderer::GetMaxBestSize()

This allows to make computing the best width of numeric columns an O(1)
operation instead of O(number-of-rows), which can make a huge difference
for big grids.
This commit is contained in:
Vadim Zeitlin
2020-06-13 16:31:33 +02:00
parent 96de24d1bb
commit d2a403408f
2 changed files with 46 additions and 1 deletions

View File

@@ -57,6 +57,13 @@ protected:
class WXDLLIMPEXP_ADV wxGridCellNumberRenderer : public wxGridCellStringRenderer class WXDLLIMPEXP_ADV wxGridCellNumberRenderer : public wxGridCellStringRenderer
{ {
public: public:
explicit wxGridCellNumberRenderer(long minValue = LONG_MIN,
long maxValue = LONG_MAX)
: m_minValue(minValue),
m_maxValue(maxValue)
{
}
// draw the string right aligned // draw the string right aligned
virtual void Draw(wxGrid& grid, virtual void Draw(wxGrid& grid,
wxGridCellAttr& attr, wxGridCellAttr& attr,
@@ -70,11 +77,21 @@ public:
wxDC& dc, wxDC& dc,
int row, int col) wxOVERRIDE; int row, int col) wxOVERRIDE;
virtual wxSize GetMaxBestSize(wxGrid& grid,
wxGridCellAttr& attr,
wxDC& dc) wxOVERRIDE;
// Optional parameters for this renderer are "<min>,<max>".
virtual void SetParameters(const wxString& params) wxOVERRIDE;
virtual wxGridCellRenderer *Clone() const wxOVERRIDE virtual wxGridCellRenderer *Clone() const wxOVERRIDE
{ return new wxGridCellNumberRenderer; } { return new wxGridCellNumberRenderer(m_minValue, m_maxValue); }
protected: protected:
wxString GetString(const wxGrid& grid, int row, int col); wxString GetString(const wxGrid& grid, int row, int col);
long m_minValue,
m_maxValue;
}; };
class WXDLLIMPEXP_ADV wxGridCellFloatRenderer : public wxGridCellStringRenderer class WXDLLIMPEXP_ADV wxGridCellFloatRenderer : public wxGridCellStringRenderer

View File

@@ -748,6 +748,34 @@ wxSize wxGridCellNumberRenderer::GetBestSize(wxGrid& grid,
return DoGetBestSize(attr, dc, GetString(grid, row, col)); return DoGetBestSize(attr, dc, GetString(grid, row, col));
} }
wxSize wxGridCellNumberRenderer::GetMaxBestSize(wxGrid& WXUNUSED(grid),
wxGridCellAttr& attr,
wxDC& dc)
{
// In theory, it's possible that there is a value in min..max range which
// is longer than both min and max, e.g. we could conceivably have "88" be
// wider than both "87" and "91" with some fonts, but it seems something
// too exotic to worry about in practice.
wxSize size = DoGetBestSize(attr, dc, wxString::Format("%ld", m_minValue));
size.IncTo(DoGetBestSize(attr, dc, wxString::Format("%ld", m_maxValue)));
return size;
}
void wxGridCellNumberRenderer::SetParameters(const wxString& params)
{
if ( params.empty() )
return;
wxString maxStr;
const wxString minStr = params.BeforeFirst(',', &maxStr);
if ( !minStr.ToLong(&m_minValue) || !maxStr.ToLong(&m_maxValue) )
{
wxLogDebug("Invalid wxGridCellNumberRenderer parameters \"%s\"", params);
}
}
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// wxGridCellFloatRenderer // wxGridCellFloatRenderer
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------