1. more corrections for ref counted editors/renderers (doesn't crash any more,

might leak memory though)
2. attempt to add parameters to renderers - failed.


git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@6357 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2000-02-29 18:36:04 +00:00
parent 39bcce60fe
commit 0b190b0f6a
3 changed files with 237 additions and 44 deletions

View File

@@ -127,6 +127,10 @@ public:
wxDC& dc, wxDC& dc,
int row, int col) = 0; int row, int col) = 0;
// interpret renderer parameters: arbitrary string whose interpretatin is
// left to the derived classes
virtual void SetParameters(const wxString& params);
protected: protected:
// virtual dtor for any base class - private because only DecRef() can // virtual dtor for any base class - private because only DecRef() can
// delete us // delete us
@@ -195,13 +199,13 @@ protected:
class WXDLLEXPORT wxGridCellFloatRenderer : public wxGridCellStringRenderer class WXDLLEXPORT wxGridCellFloatRenderer : public wxGridCellStringRenderer
{ {
public: public:
wxGridCellFloatRenderer(int width, int precision); wxGridCellFloatRenderer(int width = -1, int precision = -1);
// get/change formatting parameters // get/change formatting parameters
int GetWidth() const { return m_width; } int GetWidth() const { return m_width; }
void SetWidth(int width) { m_width = width; } void SetWidth(int width) { m_width = width; m_format.clear(); }
int GetPrecision() const { return m_precision; } int GetPrecision() const { return m_precision; }
void SetPrecision(int precision) { m_precision = precision; } void SetPrecision(int precision) { m_precision = precision; m_format.clear(); }
// draw the string right aligned with given width/precision // draw the string right aligned with given width/precision
virtual void Draw(wxGrid& grid, virtual void Draw(wxGrid& grid,
@@ -215,6 +219,10 @@ public:
wxGridCellAttr& attr, wxGridCellAttr& attr,
wxDC& dc, wxDC& dc,
int row, int col); int row, int col);
// parameters string format is "width[,precision]"
virtual void SetParameters(const wxString& params);
protected: protected:
wxString GetString(wxGrid& grid, int row, int col); wxString GetString(wxGrid& grid, int row, int col);
@@ -1089,11 +1097,18 @@ public:
void DisableDragGridSize() { EnableDragGridSize(FALSE); } void DisableDragGridSize() { EnableDragGridSize(FALSE); }
bool CanDragGridSize() { return m_canDragGridSize; } bool CanDragGridSize() { return m_canDragGridSize; }
// this sets the specified attribute for all cells in this row/col // this sets the specified attribute for all cells in this row/col
void SetRowAttr(int row, wxGridCellAttr *attr); void SetRowAttr(int row, wxGridCellAttr *attr);
void SetColAttr(int col, wxGridCellAttr *attr); void SetColAttr(int col, wxGridCellAttr *attr);
// shortcuts for setting the column parameters
// set the format for the data in the column: default is string
void SetColFormatBool(int col);
void SetColFormatNumber(int col);
void SetColFormatFloat(int col, int width = -1, int precision = -1);
void SetColFormatCustom(int col, const wxString& typeName);
void EnableGridLines( bool enable = TRUE ); void EnableGridLines( bool enable = TRUE );
bool GridLinesEnabled() { return m_gridLinesEnabled; } bool GridLinesEnabled() { return m_gridLinesEnabled; }

View File

@@ -231,6 +231,16 @@ GridFrame::GridFrame()
grid->SetColSize(4, 120); grid->SetColSize(4, 120);
grid->SetColMinimalWidth(4, 120); grid->SetColMinimalWidth(4, 120);
grid->SetColFormatFloat(5);
grid->SetCellValue(0, 5, "3.1415");
grid->SetCellValue(1, 5, "1415");
grid->SetCellValue(2, 5, "12345.67890");
grid->SetColFormatFloat(6, 6, 2);
grid->SetCellValue(0, 6, "3.1415");
grid->SetCellValue(1, 6, "1415");
grid->SetCellValue(2, 6, "12345.67890");
wxBoxSizer *topSizer = new wxBoxSizer( wxVERTICAL ); wxBoxSizer *topSizer = new wxBoxSizer( wxVERTICAL );
topSizer->Add( grid, topSizer->Add( grid,
1, 1,
@@ -1096,5 +1106,8 @@ BugsGridFrame::BugsGridFrame()
grid->SetMargins(0, 0); grid->SetMargins(0, 0);
grid->Fit(); grid->Fit();
Fit(); wxSize size = grid->GetSize();
size.x += 10;
size.y += 10;
SetClientSize(size);
} }

View File

@@ -1079,6 +1079,11 @@ void wxGridCellRenderer::Draw(wxGrid& grid,
dc.DrawRectangle(rect); dc.DrawRectangle(rect);
} }
void wxGridCellRenderer::SetParameters(const wxString& WXUNUSED(params))
{
// nothing to do
}
wxGridCellRenderer::~wxGridCellRenderer() wxGridCellRenderer::~wxGridCellRenderer()
{ {
} }
@@ -1214,21 +1219,45 @@ wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width, int precision)
wxString wxGridCellFloatRenderer::GetString(wxGrid& grid, int row, int col) wxString wxGridCellFloatRenderer::GetString(wxGrid& grid, int row, int col)
{ {
wxGridTableBase *table = grid.GetTable(); wxGridTableBase *table = grid.GetTable();
bool hasDouble;
double val;
wxString text; wxString text;
if ( table->CanGetValueAs(row, col, wxGRID_VALUE_FLOAT) ) if ( table->CanGetValueAs(row, col, wxGRID_VALUE_FLOAT) )
{ {
if ( !m_format ) val = table->GetValueAsDouble(row, col);
{ hasDouble = TRUE;
m_format.Printf(_T("%%%d.%d%%f"), m_width, m_precision);
}
text.Printf(m_format, table->GetValueAsDouble(row, col));
} }
else else
{ {
text = table->GetValue(row, col); text = table->GetValue(row, col);
hasDouble = text.ToDouble(&val);
} }
if ( hasDouble )
{
if ( !m_format )
{
if ( m_width == -1 )
{
// default width/precision
m_format = _T("%f");
}
else if ( m_precision == -1 )
{
// default precision
m_format.Printf(_T("%%%d.f"), m_width);
}
else
{
m_format.Printf(_T("%%%d.%df"), m_width, m_precision);
}
}
text.Printf(m_format, val);
}
//else: text already contains the string
return text; return text;
} }
@@ -1262,6 +1291,54 @@ wxSize wxGridCellFloatRenderer::GetBestSize(wxGrid& grid,
return DoGetBestSize(attr, dc, GetString(grid, row, col)); return DoGetBestSize(attr, dc, GetString(grid, row, col));
} }
void wxGridCellFloatRenderer::SetParameters(const wxString& params)
{
bool ok = TRUE;
if ( !params )
{
// reset to defaults
SetWidth(-1);
SetPrecision(-1);
}
else
{
wxString tmp = params.BeforeFirst(_T(','));
if ( !!tmp )
{
long width;
if ( !tmp.ToLong(&width) )
{
ok = FALSE;
}
else
{
SetWidth((int)width);
tmp = params.AfterFirst(_T(','));
if ( !!tmp )
{
long precision;
if ( !tmp.ToLong(&precision) )
{
ok = FALSE;
}
else
{
SetPrecision((int)precision);
}
}
}
}
if ( !ok )
{
wxLogDebug(_T("Invalid wxGridCellFloatRenderer parameter string "
"'%s ignored"), params.c_str());
}
}
}
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// wxGridCellBoolRenderer // wxGridCellBoolRenderer
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@@ -1462,29 +1539,47 @@ void wxGridCellAttr::GetAlignment(int *hAlign, int *vAlign) const
wxGridCellRenderer* wxGridCellAttr::GetRenderer(wxGrid* grid, int row, int col) const wxGridCellRenderer* wxGridCellAttr::GetRenderer(wxGrid* grid, int row, int col) const
{ {
if ((m_defGridAttr != this || grid == NULL) && HasRenderer())
return m_renderer; // use local attribute
wxGridCellRenderer* renderer = NULL; wxGridCellRenderer* renderer = NULL;
if (grid) // get renderer for the data type
if ( m_defGridAttr != this || grid == NULL )
{
renderer = m_renderer; // use local attribute
if ( renderer )
renderer->IncRef();
}
if ( !renderer && grid ) // get renderer for the data type
{
// GetDefaultRendererForCell() will do IncRef() for us
renderer = grid->GetDefaultRendererForCell(row, col); renderer = grid->GetDefaultRendererForCell(row, col);
}
if ( !renderer ) if ( !renderer )
{
// if we still don't have one then use the grid default // if we still don't have one then use the grid default
// (no need for IncRef() here neither)
renderer = m_defGridAttr->GetRenderer(NULL,0,0); renderer = m_defGridAttr->GetRenderer(NULL,0,0);
}
if ( !renderer) if ( !renderer)
{
wxFAIL_MSG(wxT("Missing default cell attribute")); wxFAIL_MSG(wxT("Missing default cell attribute"));
}
return renderer; return renderer;
} }
wxGridCellEditor* wxGridCellAttr::GetEditor(wxGrid* grid, int row, int col) const wxGridCellEditor* wxGridCellAttr::GetEditor(wxGrid* grid, int row, int col) const
{ {
if ((m_defGridAttr != this || grid == NULL) && HasEditor())
return m_editor; // use local attribute
wxGridCellEditor* editor = NULL; wxGridCellEditor* editor = NULL;
if ( m_defGridAttr != this || grid == NULL )
{
editor = m_editor; // use local attribute
if ( editor )
editor->IncRef();
}
if ( grid ) // get renderer for the data type if ( grid ) // get renderer for the data type
editor = grid->GetDefaultEditorForCell(row, col); editor = grid->GetDefaultEditorForCell(row, col);
@@ -1493,7 +1588,9 @@ wxGridCellEditor* wxGridCellAttr::GetEditor(wxGrid* grid, int row, int col) cons
editor = m_defGridAttr->GetEditor(NULL,0,0); editor = m_defGridAttr->GetEditor(NULL,0,0);
if ( !editor ) if ( !editor )
{
wxFAIL_MSG(wxT("Missing default cell attribute")); wxFAIL_MSG(wxT("Missing default cell attribute"));
}
return editor; return editor;
} }
@@ -1839,12 +1936,14 @@ int wxGridTypeRegistry::FindDataType(const wxString& typeName)
wxGridCellRenderer* wxGridTypeRegistry::GetRenderer(int index) wxGridCellRenderer* wxGridTypeRegistry::GetRenderer(int index)
{ {
wxGridCellRenderer* renderer = m_typeinfo[index]->m_renderer; wxGridCellRenderer* renderer = m_typeinfo[index]->m_renderer;
renderer->IncRef();
return renderer; return renderer;
} }
wxGridCellEditor* wxGridTypeRegistry::GetEditor(int index) wxGridCellEditor* wxGridTypeRegistry::GetEditor(int index)
{ {
wxGridCellEditor* editor = m_typeinfo[index]->m_editor; wxGridCellEditor* editor = m_typeinfo[index]->m_editor;
editor->IncRef();
return editor; return editor;
} }
@@ -2091,7 +2190,6 @@ void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row), int WXUNUSED(col),
{ {
} }
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
// //
// Message class for the grid table to send requests and notifications // Message class for the grid table to send requests and notifications
@@ -2832,6 +2930,9 @@ void wxGrid::Create()
RegisterDataType(wxGRID_VALUE_NUMBER, RegisterDataType(wxGRID_VALUE_NUMBER,
new wxGridCellNumberRenderer, new wxGridCellNumberRenderer,
new wxGridCellNumberEditor); new wxGridCellNumberEditor);
RegisterDataType(wxGRID_VALUE_FLOAT,
new wxGridCellFloatRenderer,
new wxGridCellFloatEditor);
// subwindow components that make up the wxGrid // subwindow components that make up the wxGrid
m_cornerLabelWin = new wxGridCornerLabelWindow( this, m_cornerLabelWin = new wxGridCornerLabelWindow( this,
@@ -4041,7 +4142,11 @@ void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent& event )
EnableCellEditControl(); EnableCellEditControl();
wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords); wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords);
attr->GetEditor(this, coords.GetRow(), coords.GetCol())->StartingClick(); wxGridCellEditor *editor = attr->GetEditor(this,
coords.GetRow(),
coords.GetCol());
editor->StartingClick();
editor->DecRef();
attr->DecRef(); attr->DecRef();
m_waitForSlowClick = FALSE; m_waitForSlowClick = FALSE;
@@ -4810,7 +4915,9 @@ void wxGrid::OnKeyDown( wxKeyEvent& event )
int row = m_currentCellCoords.GetRow(); int row = m_currentCellCoords.GetRow();
int col = m_currentCellCoords.GetCol(); int col = m_currentCellCoords.GetCol();
wxGridCellAttr* attr = GetCellAttr(row, col); wxGridCellAttr* attr = GetCellAttr(row, col);
attr->GetEditor(this, row, col)->StartingKey(event); wxGridCellEditor *editor = attr->GetEditor(this, row, col);
editor->StartingKey(event);
editor->DecRef();
attr->DecRef(); attr->DecRef();
} }
else else
@@ -4991,15 +5098,17 @@ void wxGrid::DrawCell( wxDC& dc, const wxGridCellCoords& coords )
// if the editor is shown, we should use it and not the renderer // if the editor is shown, we should use it and not the renderer
if ( isCurrent && IsCellEditControlEnabled() ) if ( isCurrent && IsCellEditControlEnabled() )
{ {
attr->GetEditor(this, row, col)->PaintBackground(rect, attr); wxGridCellEditor *editor = attr->GetEditor(this, row, col);
editor->PaintBackground(rect, attr);
editor->DecRef();
} }
else else
{ {
// but all the rest is drawn by the cell renderer and hence may be // but all the rest is drawn by the cell renderer and hence may be
// customized // customized
attr->GetRenderer(this, row, col)-> wxGridCellRenderer *renderer = attr->GetRenderer(this, row, col);
Draw(*this, *attr, dc, rect, row, col, IsInSelection(coords)); renderer->Draw(*this, *attr, dc, rect, row, col, IsInSelection(coords));
renderer->DecRef();
} }
attr->DecRef(); attr->DecRef();
@@ -5516,6 +5625,8 @@ void wxGrid::ShowCellEditControl()
editor->SetSize( rect ); editor->SetSize( rect );
editor->BeginEdit(row, col, this); editor->BeginEdit(row, col, this);
editor->DecRef();
attr->DecRef(); attr->DecRef();
} }
} }
@@ -5530,7 +5641,9 @@ void wxGrid::HideCellEditControl()
int col = m_currentCellCoords.GetCol(); int col = m_currentCellCoords.GetCol();
wxGridCellAttr* attr = GetCellAttr(row, col); wxGridCellAttr* attr = GetCellAttr(row, col);
attr->GetEditor(this, row, col)->Show( FALSE ); wxGridCellEditor *editor = attr->GetEditor(this, row, col);
editor->Show( FALSE );
editor->DecRef();
attr->DecRef(); attr->DecRef();
m_gridWin->SetFocus(); m_gridWin->SetFocus();
} }
@@ -5548,6 +5661,7 @@ void wxGrid::SaveEditControlValue()
wxGridCellEditor* editor = attr->GetEditor(this, row, col); wxGridCellEditor* editor = attr->GetEditor(this, row, col);
bool changed = editor->EndEdit(row, col, this); bool changed = editor->EndEdit(row, col, this);
editor->DecRef();
attr->DecRef(); attr->DecRef();
if (changed) if (changed)
@@ -6526,6 +6640,7 @@ wxGridCellRenderer* wxGrid::GetCellRenderer(int row, int col)
wxGridCellAttr* attr = GetCellAttr(row, col); wxGridCellAttr* attr = GetCellAttr(row, col);
wxGridCellRenderer* renderer = attr->GetRenderer(this, row, col); wxGridCellRenderer* renderer = attr->GetRenderer(this, row, col);
attr->DecRef(); attr->DecRef();
return renderer; return renderer;
} }
@@ -6534,6 +6649,7 @@ wxGridCellEditor* wxGrid::GetCellEditor(int row, int col)
wxGridCellAttr* attr = GetCellAttr(row, col); wxGridCellAttr* attr = GetCellAttr(row, col);
wxGridCellEditor* editor = attr->GetEditor(this, row, col); wxGridCellEditor* editor = attr->GetEditor(this, row, col);
attr->DecRef(); attr->DecRef();
return editor; return editor;
} }
@@ -6648,6 +6764,40 @@ wxGridCellAttr *wxGrid::GetOrCreateCellAttr(int row, int col) const
return attr; return attr;
} }
// ----------------------------------------------------------------------------
// setting column attributes (wrappers around SetColAttr)
// ----------------------------------------------------------------------------
void wxGrid::SetColFormatBool(int col)
{
SetColFormatCustom(col, wxGRID_VALUE_BOOL);
}
void wxGrid::SetColFormatNumber(int col)
{
SetColFormatCustom(col, wxGRID_VALUE_NUMBER);
}
void wxGrid::SetColFormatFloat(int col, int width, int precision)
{
wxString typeName = wxGRID_VALUE_FLOAT;
if ( (width != -1) || (precision != -1) )
{
typeName << _T(':') << width << _T(',') << precision;
}
SetColFormatCustom(col, typeName);
}
void wxGrid::SetColFormatCustom(int col, const wxString& typeName)
{
wxGridCellAttr *attr = new wxGridCellAttr;
wxGridCellRenderer *renderer = GetDefaultRendererForType(typeName);
attr->SetRenderer(renderer);
SetColAttr(col, attr);
}
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// setting cell attributes: this is forwarded to the table // setting cell attributes: this is forwarded to the table
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@@ -6774,24 +6924,37 @@ wxGridCellEditor*
wxGrid::GetDefaultEditorForType(const wxString& typeName) const wxGrid::GetDefaultEditorForType(const wxString& typeName) const
{ {
int index = m_typeRegistry->FindDataType(typeName); int index = m_typeRegistry->FindDataType(typeName);
if (index == -1) { if ( index == wxNOT_FOUND )
// Should we force the failure here or let it fallback to string handling??? {
// wxFAIL_MSG(wxT("Unknown data type name")); wxFAIL_MSG(wxT("Unknown data type name"));
return NULL; return NULL;
} }
return m_typeRegistry->GetEditor(index); return m_typeRegistry->GetEditor(index);
} }
wxGridCellRenderer* wxGridCellRenderer*
wxGrid::GetDefaultRendererForType(const wxString& typeName) const wxGrid::GetDefaultRendererForType(const wxString& typeName) const
{ {
int index = m_typeRegistry->FindDataType(typeName); // the first part of the typename is the "real" type, anything after ':'
if (index == -1) { // are the parameters for the renderer
// Should we force the failure here or let it fallback to string handling??? wxString type = typeName.BeforeFirst(_T(':'));
// wxFAIL_MSG(wxT("Unknown data type name"));
int index = m_typeRegistry->FindDataType(type);
if ( index == wxNOT_FOUND )
{
wxFAIL_MSG(wxT("Unknown data type name"));
return NULL; return NULL;
} }
return m_typeRegistry->GetRenderer(index);
wxGridCellRenderer *renderer = m_typeRegistry->GetRenderer(index);
// do it even if there are no parameters to reset them to defaults
renderer->SetParameters(typeName.AfterFirst(_T(':')));
return renderer;
} }
@@ -6942,6 +7105,8 @@ void wxGrid::AutoSizeColOrRow( int colOrRow, bool setAsMin, bool column )
{ {
extentMax = extent; extentMax = extent;
} }
renderer->DecRef();
} }
attr->DecRef(); attr->DecRef();