wxGridCellRenderer/Editor made ref counted

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@6356 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2000-02-29 17:42:28 +00:00
parent af547d51b7
commit 39bcce60fe
2 changed files with 81 additions and 34 deletions

View File

@@ -81,6 +81,13 @@ class WXDLLEXPORT wxComboBox;
class WXDLLEXPORT wxTextCtrl; class WXDLLEXPORT wxTextCtrl;
class WXDLLEXPORT wxSpinCtrl; class WXDLLEXPORT wxSpinCtrl;
// ----------------------------------------------------------------------------
// macros
// ----------------------------------------------------------------------------
#define wxSafeIncRef(p) if ( p ) (p)->IncRef()
#define wxSafeDecRef(p) if ( p ) (p)->DecRef()
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// wxGridCellRenderer: this class is responsible for actually drawing the cell // wxGridCellRenderer: this class is responsible for actually drawing the cell
// in the grid. You may pass it to the wxGridCellAttr (below) to change the // in the grid. You may pass it to the wxGridCellAttr (below) to change the
@@ -92,6 +99,14 @@ class WXDLLEXPORT wxSpinCtrl;
class WXDLLEXPORT wxGridCellRenderer class WXDLLEXPORT wxGridCellRenderer
{ {
public: public:
wxGridCellRenderer() { m_nRef = 1; }
// this class is ref counted: it is created with ref count of 1, so
// calling DecRef() once will delete it. Calling IncRef() allows to lock
// it until the matching DecRef() is called
void IncRef() { m_nRef++; }
void DecRef() { if ( !--m_nRef ) delete this; }
// draw the given cell on the provided DC inside the given rectangle // draw the given cell on the provided DC inside the given rectangle
// using the style specified by the attribute and the default or selected // using the style specified by the attribute and the default or selected
// state corresponding to the isSelected value. // state corresponding to the isSelected value.
@@ -112,8 +127,17 @@ public:
wxDC& dc, wxDC& dc,
int row, int col) = 0; int row, int col) = 0;
// virtual dtor for any base class protected:
// virtual dtor for any base class - private because only DecRef() can
// delete us
virtual ~wxGridCellRenderer(); virtual ~wxGridCellRenderer();
private:
size_t m_nRef;
// suppress the stupid gcc warning about the class having private dtor and
// no friends
friend class wxGridCellRendererDummyFriend;
}; };
// the default renderer for the cells containing string data // the default renderer for the cells containing string data
@@ -236,7 +260,12 @@ class WXDLLEXPORT wxGridCellEditor
{ {
public: public:
wxGridCellEditor(); wxGridCellEditor();
virtual ~wxGridCellEditor();
// this class is ref counted: it is created with ref count of 1, so
// calling DecRef() once will delete it. Calling IncRef() allows to lock
// it until the matching DecRef() is called
void IncRef() { m_nRef++; }
void DecRef() { if ( !--m_nRef ) delete this; }
bool IsCreated() { return m_control != NULL; } bool IsCreated() { return m_control != NULL; }
@@ -284,6 +313,12 @@ public:
virtual void Destroy(); virtual void Destroy();
protected: protected:
// the dtor is private because only DecRef() can delete us
virtual ~wxGridCellEditor();
// the ref count - when it goes to 0, we die
size_t m_nRef;
// the control we show on screen // the control we show on screen
wxControl* m_control; wxControl* m_control;
@@ -293,6 +328,10 @@ protected:
wxColour m_colFgOld, wxColour m_colFgOld,
m_colBgOld; m_colBgOld;
wxFont m_fontOld; wxFont m_fontOld;
// suppress the stupid gcc warning about the class having private dtor and
// no friends
friend class wxGridCellEditorDummyFriend;
}; };
// the editor for string/text data // the editor for string/text data
@@ -465,18 +504,14 @@ public:
SetAlignment(hAlign, vAlign); SetAlignment(hAlign, vAlign);
} }
// creates a new copy of this object: warning, this is destructive copy // creates a new copy of this object
// (this is why it's non const), the renderer and editor are "given to" wxGridCellAttr *Clone() const;
// the new object
wxGridCellAttr *Clone();
// this class is ref counted: it is created with ref count of 1, so // this class is ref counted: it is created with ref count of 1, so
// calling DecRef() once will delete it. Calling IncRef() allows to lock // calling DecRef() once will delete it. Calling IncRef() allows to lock
// it until the matching DecRef() is called // it until the matching DecRef() is called
void IncRef() { m_nRef++; } void IncRef() { m_nRef++; }
void DecRef() { if ( !--m_nRef ) delete this; } void DecRef() { if ( !--m_nRef ) delete this; }
void SafeIncRef() { if ( this ) IncRef(); }
void SafeDecRef() { if ( this ) DecRef(); }
// setters // setters
void SetTextColour(const wxColour& colText) { m_colText = colText; } void SetTextColour(const wxColour& colText) { m_colText = colText; }
@@ -491,9 +526,9 @@ public:
// takes ownership of the pointer // takes ownership of the pointer
void SetRenderer(wxGridCellRenderer *renderer) void SetRenderer(wxGridCellRenderer *renderer)
{ delete m_renderer; m_renderer = renderer; } { wxSafeDecRef(m_renderer); m_renderer = renderer; }
void SetEditor(wxGridCellEditor* editor) void SetEditor(wxGridCellEditor* editor)
{ delete m_editor; m_editor = editor; } { wxSafeDecRef(m_editor); m_editor = editor; }
// accessors // accessors
bool HasTextColour() const { return m_colText.Ok(); } bool HasTextColour() const { return m_colText.Ok(); }
@@ -527,7 +562,11 @@ private:
} }
// the dtor is private because only DecRef() can delete us // the dtor is private because only DecRef() can delete us
~wxGridCellAttr() { delete m_renderer; delete m_editor; } ~wxGridCellAttr()
{
wxSafeDecRef(m_renderer);
wxSafeDecRef(m_editor);
}
// the ref count - when it goes to 0, we die // the ref count - when it goes to 0, we die
size_t m_nRef; size_t m_nRef;

View File

@@ -263,7 +263,11 @@ struct wxGridDataTypeInfo
: m_typeName(typeName), m_renderer(renderer), m_editor(editor) : m_typeName(typeName), m_renderer(renderer), m_editor(editor)
{ } { }
~wxGridDataTypeInfo() { delete m_renderer; delete m_editor; } ~wxGridDataTypeInfo()
{
wxSafeDecRef(m_renderer);
wxSafeDecRef(m_editor);
}
wxString m_typeName; wxString m_typeName;
wxGridCellRenderer* m_renderer; wxGridCellRenderer* m_renderer;
@@ -334,6 +338,8 @@ static const int GRID_HASH_SIZE = 100;
wxGridCellEditor::wxGridCellEditor() wxGridCellEditor::wxGridCellEditor()
{ {
m_control = NULL; m_control = NULL;
m_nRef = 1;
} }
@@ -1354,7 +1360,7 @@ void wxGridCellBoolRenderer::Draw(wxGrid& grid,
// wxGridCellAttr // wxGridCellAttr
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
wxGridCellAttr *wxGridCellAttr::Clone() wxGridCellAttr *wxGridCellAttr::Clone() const
{ {
wxGridCellAttr *attr = new wxGridCellAttr; wxGridCellAttr *attr = new wxGridCellAttr;
if ( HasTextColour() ) if ( HasTextColour() )
@@ -1369,12 +1375,12 @@ wxGridCellAttr *wxGridCellAttr::Clone()
if ( m_renderer ) if ( m_renderer )
{ {
attr->SetRenderer(m_renderer); attr->SetRenderer(m_renderer);
m_renderer = NULL; m_renderer->IncRef();
} }
if ( m_editor ) if ( m_editor )
{ {
attr->SetEditor(m_editor); attr->SetEditor(m_editor);
m_editor = NULL; m_editor->IncRef();
} }
if ( IsReadOnly() ) if ( IsReadOnly() )
@@ -1801,15 +1807,17 @@ void wxGridTypeRegistry::RegisterDataType(const wxString& typeName,
wxGridCellRenderer* renderer, wxGridCellRenderer* renderer,
wxGridCellEditor* editor) wxGridCellEditor* editor)
{ {
int loc;
wxGridDataTypeInfo* info = new wxGridDataTypeInfo(typeName, renderer, editor); wxGridDataTypeInfo* info = new wxGridDataTypeInfo(typeName, renderer, editor);
// is it already registered? // is it already registered?
if ((loc = FindDataType(typeName)) != -1) { int loc = FindDataType(typeName);
if ( loc != wxNOT_FOUND )
{
delete m_typeinfo[loc]; delete m_typeinfo[loc];
m_typeinfo[loc] = info; m_typeinfo[loc] = info;
} }
else { else
{
m_typeinfo.Add(info); m_typeinfo.Add(info);
} }
} }
@@ -1892,7 +1900,7 @@ void wxGridTableBase::SetAttr(wxGridCellAttr* attr, int row, int col)
{ {
// as we take ownership of the pointer and don't store it, we must // as we take ownership of the pointer and don't store it, we must
// free it now // free it now
attr->SafeDecRef(); wxSafeDecRef(attr);
} }
} }
@@ -1906,7 +1914,7 @@ void wxGridTableBase::SetRowAttr(wxGridCellAttr *attr, int row)
{ {
// as we take ownership of the pointer and don't store it, we must // as we take ownership of the pointer and don't store it, we must
// free it now // free it now
attr->SafeDecRef(); wxSafeDecRef(attr);
} }
} }
@@ -1920,7 +1928,7 @@ void wxGridTableBase::SetColAttr(wxGridCellAttr *attr, int col)
{ {
// as we take ownership of the pointer and don't store it, we must // as we take ownership of the pointer and don't store it, we must
// free it now // free it now
attr->SafeDecRef(); wxSafeDecRef(attr);
} }
} }
@@ -2760,7 +2768,7 @@ wxGrid::wxGrid( wxWindow *parent,
wxGrid::~wxGrid() wxGrid::~wxGrid()
{ {
ClearAttrCache(); ClearAttrCache();
m_defaultCellAttr->SafeDecRef(); wxSafeDecRef(m_defaultCellAttr);
#ifdef DEBUG_ATTR_CACHE #ifdef DEBUG_ATTR_CACHE
size_t total = gs_nAttrCacheHits + gs_nAttrCacheMisses; size_t total = gs_nAttrCacheHits + gs_nAttrCacheMisses;
@@ -6486,7 +6494,7 @@ wxColour wxGrid::GetCellBackgroundColour(int row, int col)
{ {
wxGridCellAttr *attr = GetCellAttr(row, col); wxGridCellAttr *attr = GetCellAttr(row, col);
wxColour colour = attr->GetBackgroundColour(); wxColour colour = attr->GetBackgroundColour();
attr->SafeDecRef(); attr->DecRef();
return colour; return colour;
} }
@@ -6494,7 +6502,7 @@ wxColour wxGrid::GetCellTextColour( int row, int col )
{ {
wxGridCellAttr *attr = GetCellAttr(row, col); wxGridCellAttr *attr = GetCellAttr(row, col);
wxColour colour = attr->GetTextColour(); wxColour colour = attr->GetTextColour();
attr->SafeDecRef(); attr->DecRef();
return colour; return colour;
} }
@@ -6502,7 +6510,7 @@ wxFont wxGrid::GetCellFont( int row, int col )
{ {
wxGridCellAttr *attr = GetCellAttr(row, col); wxGridCellAttr *attr = GetCellAttr(row, col);
wxFont font = attr->GetFont(); wxFont font = attr->GetFont();
attr->SafeDecRef(); attr->DecRef();
return font; return font;
} }
@@ -6510,7 +6518,7 @@ void wxGrid::GetCellAlignment( int row, int col, int *horiz, int *vert )
{ {
wxGridCellAttr *attr = GetCellAttr(row, col); wxGridCellAttr *attr = GetCellAttr(row, col);
attr->GetAlignment(horiz, vert); attr->GetAlignment(horiz, vert);
attr->SafeDecRef(); attr->DecRef();
} }
wxGridCellRenderer* wxGrid::GetCellRenderer(int row, int col) wxGridCellRenderer* wxGrid::GetCellRenderer(int row, int col)
@@ -6555,7 +6563,7 @@ void wxGrid::ClearAttrCache()
{ {
if ( m_attrCache.row != -1 ) if ( m_attrCache.row != -1 )
{ {
m_attrCache.attr->SafeDecRef(); wxSafeDecRef(m_attrCache.attr);
m_attrCache.row = -1; m_attrCache.row = -1;
} }
} }
@@ -6568,7 +6576,7 @@ void wxGrid::CacheAttr(int row, int col, wxGridCellAttr *attr) const
self->m_attrCache.row = row; self->m_attrCache.row = row;
self->m_attrCache.col = col; self->m_attrCache.col = col;
self->m_attrCache.attr = attr; self->m_attrCache.attr = attr;
attr->SafeIncRef(); wxSafeIncRef(attr);
} }
bool wxGrid::LookupAttr(int row, int col, wxGridCellAttr **attr) const bool wxGrid::LookupAttr(int row, int col, wxGridCellAttr **attr) const
@@ -6576,7 +6584,7 @@ bool wxGrid::LookupAttr(int row, int col, wxGridCellAttr **attr) const
if ( row == m_attrCache.row && col == m_attrCache.col ) if ( row == m_attrCache.row && col == m_attrCache.col )
{ {
*attr = m_attrCache.attr; *attr = m_attrCache.attr;
(*attr)->SafeIncRef(); wxSafeIncRef(m_attrCache.attr);
#ifdef DEBUG_ATTR_CACHE #ifdef DEBUG_ATTR_CACHE
gs_nAttrCacheHits++; gs_nAttrCacheHits++;
@@ -6652,7 +6660,7 @@ void wxGrid::SetRowAttr(int row, wxGridCellAttr *attr)
} }
else else
{ {
attr->SafeDecRef(); wxSafeDecRef(attr);
} }
} }
@@ -6664,7 +6672,7 @@ void wxGrid::SetColAttr(int col, wxGridCellAttr *attr)
} }
else else
{ {
attr->SafeDecRef(); wxSafeDecRef(attr);
} }
} }
@@ -6917,7 +6925,7 @@ void wxGrid::AutoSizeColOrRow( int colOrRow, bool setAsMin, bool column )
wxCoord extent, extentMax = 0; wxCoord extent, extentMax = 0;
int max = column ? m_numRows : m_numCols; int max = column ? m_numRows : m_numCols;
for ( int rowOrCol = 0; rowOrCol < m_numRows; rowOrCol++ ) for ( int rowOrCol = 0; rowOrCol < max; rowOrCol++ )
{ {
if ( column ) if ( column )
row = rowOrCol; row = rowOrCol;
@@ -6965,14 +6973,14 @@ void wxGrid::AutoSizeColOrRow( int colOrRow, bool setAsMin, bool column )
if ( column ) if ( column )
SetColSize(col, extentMax); SetColSize(col, extentMax);
else else
SetRowSize(col, extentMax); SetRowSize(row, extentMax);
if ( setAsMin ) if ( setAsMin )
{ {
if ( column ) if ( column )
SetColMinimalWidth(col, extentMax); SetColMinimalWidth(col, extentMax);
else else
SetRowMinimalHeight(col, extentMax); SetRowMinimalHeight(row, extentMax);
} }
} }