From 39bcce60fea0a8211b44c7f1fdbcea576ad39822 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Tue, 29 Feb 2000 17:42:28 +0000 Subject: [PATCH] wxGridCellRenderer/Editor made ref counted git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@6356 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/generic/grid.h | 61 ++++++++++++++++++++++++++++++++------- src/generic/grid.cpp | 54 +++++++++++++++++++--------------- 2 files changed, 81 insertions(+), 34 deletions(-) diff --git a/include/wx/generic/grid.h b/include/wx/generic/grid.h index 941a6a2537..89bae8bd1f 100644 --- a/include/wx/generic/grid.h +++ b/include/wx/generic/grid.h @@ -81,6 +81,13 @@ class WXDLLEXPORT wxComboBox; class WXDLLEXPORT wxTextCtrl; 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 // in the grid. You may pass it to the wxGridCellAttr (below) to change the @@ -92,6 +99,14 @@ class WXDLLEXPORT wxSpinCtrl; class WXDLLEXPORT wxGridCellRenderer { 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 // using the style specified by the attribute and the default or selected // state corresponding to the isSelected value. @@ -112,8 +127,17 @@ public: wxDC& dc, 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(); + +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 @@ -236,7 +260,12 @@ class WXDLLEXPORT wxGridCellEditor { public: 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; } @@ -284,6 +313,12 @@ public: virtual void Destroy(); 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 wxControl* m_control; @@ -293,6 +328,10 @@ protected: wxColour m_colFgOld, m_colBgOld; 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 @@ -465,18 +504,14 @@ public: SetAlignment(hAlign, vAlign); } - // creates a new copy of this object: warning, this is destructive copy - // (this is why it's non const), the renderer and editor are "given to" - // the new object - wxGridCellAttr *Clone(); + // creates a new copy of this object + wxGridCellAttr *Clone() const; // 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; } - void SafeIncRef() { if ( this ) IncRef(); } - void SafeDecRef() { if ( this ) DecRef(); } // setters void SetTextColour(const wxColour& colText) { m_colText = colText; } @@ -491,9 +526,9 @@ public: // takes ownership of the pointer void SetRenderer(wxGridCellRenderer *renderer) - { delete m_renderer; m_renderer = renderer; } + { wxSafeDecRef(m_renderer); m_renderer = renderer; } void SetEditor(wxGridCellEditor* editor) - { delete m_editor; m_editor = editor; } + { wxSafeDecRef(m_editor); m_editor = editor; } // accessors bool HasTextColour() const { return m_colText.Ok(); } @@ -527,7 +562,11 @@ private: } // 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 size_t m_nRef; diff --git a/src/generic/grid.cpp b/src/generic/grid.cpp index d3e2e86334..ea8228ec99 100644 --- a/src/generic/grid.cpp +++ b/src/generic/grid.cpp @@ -263,7 +263,11 @@ struct wxGridDataTypeInfo : 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; wxGridCellRenderer* m_renderer; @@ -334,6 +338,8 @@ static const int GRID_HASH_SIZE = 100; wxGridCellEditor::wxGridCellEditor() { m_control = NULL; + + m_nRef = 1; } @@ -1354,7 +1360,7 @@ void wxGridCellBoolRenderer::Draw(wxGrid& grid, // wxGridCellAttr // ---------------------------------------------------------------------------- -wxGridCellAttr *wxGridCellAttr::Clone() +wxGridCellAttr *wxGridCellAttr::Clone() const { wxGridCellAttr *attr = new wxGridCellAttr; if ( HasTextColour() ) @@ -1369,12 +1375,12 @@ wxGridCellAttr *wxGridCellAttr::Clone() if ( m_renderer ) { attr->SetRenderer(m_renderer); - m_renderer = NULL; + m_renderer->IncRef(); } if ( m_editor ) { attr->SetEditor(m_editor); - m_editor = NULL; + m_editor->IncRef(); } if ( IsReadOnly() ) @@ -1801,15 +1807,17 @@ void wxGridTypeRegistry::RegisterDataType(const wxString& typeName, wxGridCellRenderer* renderer, wxGridCellEditor* editor) { - int loc; wxGridDataTypeInfo* info = new wxGridDataTypeInfo(typeName, renderer, editor); // is it already registered? - if ((loc = FindDataType(typeName)) != -1) { + int loc = FindDataType(typeName); + if ( loc != wxNOT_FOUND ) + { delete m_typeinfo[loc]; m_typeinfo[loc] = info; } - else { + else + { 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 // 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 // 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 // free it now - attr->SafeDecRef(); + wxSafeDecRef(attr); } } @@ -2760,7 +2768,7 @@ wxGrid::wxGrid( wxWindow *parent, wxGrid::~wxGrid() { ClearAttrCache(); - m_defaultCellAttr->SafeDecRef(); + wxSafeDecRef(m_defaultCellAttr); #ifdef DEBUG_ATTR_CACHE size_t total = gs_nAttrCacheHits + gs_nAttrCacheMisses; @@ -6486,7 +6494,7 @@ wxColour wxGrid::GetCellBackgroundColour(int row, int col) { wxGridCellAttr *attr = GetCellAttr(row, col); wxColour colour = attr->GetBackgroundColour(); - attr->SafeDecRef(); + attr->DecRef(); return colour; } @@ -6494,7 +6502,7 @@ wxColour wxGrid::GetCellTextColour( int row, int col ) { wxGridCellAttr *attr = GetCellAttr(row, col); wxColour colour = attr->GetTextColour(); - attr->SafeDecRef(); + attr->DecRef(); return colour; } @@ -6502,7 +6510,7 @@ wxFont wxGrid::GetCellFont( int row, int col ) { wxGridCellAttr *attr = GetCellAttr(row, col); wxFont font = attr->GetFont(); - attr->SafeDecRef(); + attr->DecRef(); return font; } @@ -6510,7 +6518,7 @@ void wxGrid::GetCellAlignment( int row, int col, int *horiz, int *vert ) { wxGridCellAttr *attr = GetCellAttr(row, col); attr->GetAlignment(horiz, vert); - attr->SafeDecRef(); + attr->DecRef(); } wxGridCellRenderer* wxGrid::GetCellRenderer(int row, int col) @@ -6555,7 +6563,7 @@ void wxGrid::ClearAttrCache() { if ( m_attrCache.row != -1 ) { - m_attrCache.attr->SafeDecRef(); + wxSafeDecRef(m_attrCache.attr); 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.col = col; self->m_attrCache.attr = attr; - attr->SafeIncRef(); + wxSafeIncRef(attr); } 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 ) { *attr = m_attrCache.attr; - (*attr)->SafeIncRef(); + wxSafeIncRef(m_attrCache.attr); #ifdef DEBUG_ATTR_CACHE gs_nAttrCacheHits++; @@ -6652,7 +6660,7 @@ void wxGrid::SetRowAttr(int row, wxGridCellAttr *attr) } else { - attr->SafeDecRef(); + wxSafeDecRef(attr); } } @@ -6664,7 +6672,7 @@ void wxGrid::SetColAttr(int col, wxGridCellAttr *attr) } else { - attr->SafeDecRef(); + wxSafeDecRef(attr); } } @@ -6917,7 +6925,7 @@ void wxGrid::AutoSizeColOrRow( int colOrRow, bool setAsMin, bool column ) wxCoord extent, extentMax = 0; int max = column ? m_numRows : m_numCols; - for ( int rowOrCol = 0; rowOrCol < m_numRows; rowOrCol++ ) + for ( int rowOrCol = 0; rowOrCol < max; rowOrCol++ ) { if ( column ) row = rowOrCol; @@ -6965,14 +6973,14 @@ void wxGrid::AutoSizeColOrRow( int colOrRow, bool setAsMin, bool column ) if ( column ) SetColSize(col, extentMax); else - SetRowSize(col, extentMax); + SetRowSize(row, extentMax); if ( setAsMin ) { if ( column ) SetColMinimalWidth(col, extentMax); else - SetRowMinimalHeight(col, extentMax); + SetRowMinimalHeight(row, extentMax); } } -- 2.45.2