From 2e64ba6d6e61b58a9a7d33e632a6126cc76691ef Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 8 Feb 2020 15:14:24 +0100 Subject: [PATCH] Also add wxGridCellEditorPtr and wxGridCellRendererPtr This is similar to the previous commit and replaces manual calls to DecRef() on the renderers/editors with the use of smart pointers for them too. --- include/wx/generic/grid.h | 16 ++++++++++++ interface/wx/grid.h | 54 +++++++++++++++++++++++++++++++++++++++ src/generic/grid.cpp | 53 ++++++++++++-------------------------- 3 files changed, 86 insertions(+), 37 deletions(-) diff --git a/include/wx/generic/grid.h b/include/wx/generic/grid.h index ff744330d7..345bf6adcb 100644 --- a/include/wx/generic/grid.h +++ b/include/wx/generic/grid.h @@ -206,6 +206,9 @@ public: virtual wxGridCellRenderer *Clone() const = 0; }; +// Smart pointer to wxGridCellRenderer, calling DecRef() on it automatically. +typedef wxObjectDataPtr wxGridCellRendererPtr; + // ---------------------------------------------------------------------------- // wxGridCellEditor: This class is responsible for providing and manipulating // the in-place edit controls for the grid. Instances of wxGridCellEditor @@ -333,6 +336,9 @@ protected: wxDECLARE_NO_COPY_CLASS(wxGridCellEditor); }; +// Smart pointer to wxGridCellEditor, calling DecRef() on it automatically. +typedef wxObjectDataPtr wxGridCellEditorPtr; + // ---------------------------------------------------------------------------- // wxGridHeaderRenderer and company: like wxGridCellRenderer but for headers // ---------------------------------------------------------------------------- @@ -573,8 +579,18 @@ public: // whether the cell will draw the overflowed text to neighbour cells // currently only left aligned cells can overflow bool CanOverflow() const; + wxGridCellRenderer *GetRenderer(const wxGrid* grid, int row, int col) const; + wxGridCellRendererPtr GetRendererPtr(const wxGrid* grid, int row, int col) const + { + return wxGridCellRendererPtr(GetRenderer(grid, row, col)); + } + wxGridCellEditor *GetEditor(const wxGrid* grid, int row, int col) const; + wxGridCellEditorPtr GetEditorPtr(const wxGrid* grid, int row, int col) const + { + return wxGridCellEditorPtr(GetEditor(grid, row, col)); + } bool IsReadOnly() const { return m_isReadOnly == wxGridCellAttr::ReadOnly; } diff --git a/interface/wx/grid.h b/interface/wx/grid.h index 612d936ed9..3030c3da86 100644 --- a/interface/wx/grid.h +++ b/interface/wx/grid.h @@ -95,6 +95,20 @@ protected: virtual ~wxGridCellRenderer(); }; +/** + Smart pointer wrapping wxGridCellRenderer. + + wxGridCellRendererPtr takes ownership of wxGridCellRenderer passed to it on + construction and calls DecRef() on it automatically when it is destroyed. + It also provides transparent access to wxGridCellRenderer methods by allowing + to use objects of this class as if they were wxGridCellRenderer pointers. + + @since 3.1.4 + + @category{grid} +*/ +typedef wxObjectDataPtr wxGridCellRendererPtr; + /** @class wxGridCellAutoWrapStringRenderer @@ -591,6 +605,20 @@ protected: virtual ~wxGridCellEditor(); }; +/** + Smart pointer wrapping wxGridCellEditor. + + wxGridCellEditorPtr takes ownership of wxGridCellEditor passed to it on + construction and calls DecRef() on it automatically when it is destroyed. + It also provides transparent access to wxGridCellEditor methods by allowing + to use objects of this class as if they were wxGridCellEditor pointers. + + @since 3.1.4 + + @category{grid} +*/ +typedef wxObjectDataPtr wxGridCellEditorPtr; + /** @class wxGridCellAutoWrapStringEditor @@ -1058,9 +1086,22 @@ public: /** Returns the cell editor. + + The caller is responsible for calling DecRef() on the returned pointer, + use GetEditorPtr() to do it automatically. */ wxGridCellEditor* GetEditor(const wxGrid* grid, int row, int col) const; + /** + Returns the cell editor. + + This method is identical to GetEditor(), but returns a smart pointer, + which frees the caller from the need to call DecRef() manually. + + @since 3.1.4 + */ + wxGridCellEditorPtr GetEditorPtr(const wxGrid* grid, int row, int col) const; + /** Returns the font. */ @@ -1091,9 +1132,22 @@ public: /** Returns the cell renderer. + + The caller is responsible for calling DecRef() on the returned pointer, + use GetRendererPtr() to do it automatically. */ wxGridCellRenderer* GetRenderer(const wxGrid* grid, int row, int col) const; + /** + Returns the cell editor. + + This method is identical to GetRenderer(), but returns a smart pointer, + which frees the caller from the need to call DecRef() manually. + + @since 3.1.4 + */ + wxGridCellRendererPtr GetRendererPtr(const wxGrid* grid, int row, int col) const; + /** Returns the text colour. */ diff --git a/src/generic/grid.cpp b/src/generic/grid.cpp index c0aff5fc4c..54259e898b 100644 --- a/src/generic/grid.cpp +++ b/src/generic/grid.cpp @@ -2838,7 +2838,7 @@ void wxGrid::CalcDimensions() // how big is the editor wxGridCellAttrPtr attr = GetCellAttrPtr(r, c); - wxGridCellEditor* editor = attr->GetEditor(this, r, c); + wxGridCellEditorPtr editor = attr->GetEditorPtr(this, r, c); editor->GetWindow()->GetSize(&w2, &h2); w2 += x; h2 += y; @@ -2846,7 +2846,6 @@ void wxGrid::CalcDimensions() w = w2; if ( h2 > h ) h = h2; - editor->DecRef(); } wxPoint offset = GetGridWindowOffset(m_gridWin); @@ -4484,9 +4483,8 @@ wxGrid::DoGridCellLeftUp(wxMouseEvent& event, EnableCellEditControl(); wxGridCellAttrPtr attr = GetCellAttrPtr(coords); - wxGridCellEditor *editor = attr->GetEditor(this, coords.GetRow(), coords.GetCol()); + wxGridCellEditorPtr editor = attr->GetEditorPtr(this, coords.GetRow(), coords.GetCol()); editor->StartingClick(); - editor->DecRef(); m_waitForSlowClick = false; } @@ -5728,7 +5726,7 @@ void wxGrid::OnChar( wxKeyEvent& event ) int row = m_currentCellCoords.GetRow(); int col = m_currentCellCoords.GetCol(); wxGridCellAttrPtr attr = GetCellAttrPtr(row, col); - wxGridCellEditor *editor = attr->GetEditor(this, row, col); + wxGridCellEditorPtr editor = attr->GetEditorPtr(this, row, col); // is special and will always start editing, for // other keys - ask the editor itself @@ -5750,8 +5748,6 @@ void wxGrid::OnChar( wxKeyEvent& event ) { event.Skip(); } - - editor->DecRef(); } else { @@ -6179,16 +6175,13 @@ void wxGrid::DrawCell( wxDC& dc, const wxGridCellCoords& coords ) // Note: However, only if it is really _shown_, i.e. not hidden! if ( isCurrent && IsCellEditControlShown() ) { - wxGridCellEditor *editor = attr->GetEditor(this, row, col); - editor->PaintBackground(dc, rect, *attr); - editor->DecRef(); + attr->GetEditorPtr(this, row, col)->PaintBackground(dc, rect, *attr); } else { // but all the rest is drawn by the cell renderer and hence may be customized - wxGridCellRenderer *renderer = attr->GetRenderer(this, row, col); - renderer->Draw(*this, *attr, dc, rect, row, col, IsInSelection(coords)); - renderer->DecRef(); + attr->GetRendererPtr(this, row, col) + ->Draw(*this, *attr, dc, rect, row, col, IsInSelection(coords)); } } @@ -7084,16 +7077,13 @@ bool wxGrid::IsCellEditControlShown() const { int row = m_currentCellCoords.GetRow(); int col = m_currentCellCoords.GetCol(); - wxGridCellEditor* editor = GetCellAttrPtr(row, col)->GetEditor(this, row, col); - + wxGridCellEditorPtr editor = GetCellAttrPtr(row, col)->GetEditorPtr(this, row, col); if ( editor ) { if ( editor->IsCreated() ) { isShown = editor->GetWindow()->IsShown(); } - - editor->DecRef(); } } @@ -7153,11 +7143,11 @@ void wxGrid::ShowCellEditControl() rect.Deflate(1, 1); #endif - wxGridCellEditor* editor = attr->GetEditor(this, row, col); + wxGridCellEditorPtr editor = attr->GetEditorPtr(this, row, col); if ( !editor->IsCreated() ) { editor->Create(gridWindow, wxID_ANY, - new wxGridCellEditorEvtHandler(this, editor)); + new wxGridCellEditorEvtHandler(this, editor.get())); // Ensure the editor window has wxWANTS_CHARS flag, so that it // gets Tab, Enter and Esc keys, which need to be processed @@ -7235,8 +7225,6 @@ void wxGrid::ShowCellEditControl() editor->BeginEdit(row, col, this); editor->SetCellAttr(NULL); - - editor->DecRef(); } } } @@ -7249,14 +7237,13 @@ void wxGrid::HideCellEditControl() int col = m_currentCellCoords.GetCol(); wxGridCellAttrPtr attr = GetCellAttrPtr(row, col); - wxGridCellEditor *editor = attr->GetEditor(this, row, col); + wxGridCellEditorPtr editor = attr->GetEditorPtr(this, row, col); const bool editorHadFocus = editor->GetWindow()->IsDescendant(FindFocus()); if ( editor->GetWindow()->GetParent() != m_gridWin ) editor->GetWindow()->Reparent(m_gridWin); editor->Show( false ); - editor->DecRef(); wxGridWindow *gridWindow = CellToGridWindow(row, col); // return the focus to the grid itself if the editor had it @@ -7312,7 +7299,7 @@ void wxGrid::DoSaveEditControlValue() wxString oldval = GetCellValue(row, col); wxGridCellAttrPtr attr = GetCellAttrPtr(row, col); - wxGridCellEditor* editor = attr->GetEditor(this, row, col); + wxGridCellEditorPtr editor = attr->GetEditorPtr(this, row, col); wxString newval; bool changed = editor->EndEdit(row, col, this, oldval, &newval); @@ -7330,8 +7317,6 @@ void wxGrid::DoSaveEditControlValue() SetCellValue(row, col, oldval); } } - - editor->DecRef(); } void wxGrid::OnHideEditor(wxCommandEvent& WXUNUSED(event)) @@ -9475,7 +9460,7 @@ wxGrid::AutoSizeColOrRow(int colOrRow, bool setAsMin, wxGridDirection direction) // get cell ( main cell if CellSpan_Inside ) renderer best size wxGridCellAttrPtr attr = GetCellAttrPtr(row, col); - wxGridCellRenderer *renderer = attr->GetRenderer(this, row, col); + wxGridCellRendererPtr renderer = attr->GetRendererPtr(this, row, col); if ( renderer ) { extent = column @@ -9498,8 +9483,6 @@ wxGrid::AutoSizeColOrRow(int colOrRow, bool setAsMin, wxGridDirection direction) if ( extent > extentMax ) extentMax = extent; - - renderer->DecRef(); } } @@ -10396,15 +10379,11 @@ int wxGridTypeRegistry::FindOrCloneDataType(const wxString& typeName) return wxNOT_FOUND; } - wxGridCellRenderer *renderer = GetRenderer(index); - wxGridCellRenderer *rendererOld = renderer; - renderer = renderer->Clone(); - rendererOld->DecRef(); + wxGridCellRenderer* const + renderer = wxGridCellRendererPtr(GetRenderer(index))->Clone(); - wxGridCellEditor *editor = GetEditor(index); - wxGridCellEditor *editorOld = editor; - editor = editor->Clone(); - editorOld->DecRef(); + wxGridCellEditor* const + editor = wxGridCellEditorPtr(GetEditor(index))->Clone(); // do it even if there are no parameters to reset them to defaults wxString params = typeName.AfterFirst(wxT(':'));