From 2bad2b6d608e1b4dfb16fd97aa1bfaa855aa81d2 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Wed, 31 Aug 2011 17:22:11 +0000 Subject: [PATCH] Fix deleting and clearing wxTreeListCtrl columns. The model columns were not updated before, resulting in a mismatch between view and model columns if ClearColumns() followed by AppendColumn() was called. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@68966 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- src/generic/treelist.cpp | 76 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 73 insertions(+), 3 deletions(-) diff --git a/src/generic/treelist.cpp b/src/generic/treelist.cpp index 123a498365..2bcfaeb48b 100644 --- a/src/generic/treelist.cpp +++ b/src/generic/treelist.cpp @@ -160,6 +160,37 @@ public: } } + void OnDeleteColumn(unsigned col, unsigned numColumns) + { + wxASSERT_MSG( col, "Shouldn't be called for the first column" ); + + if ( !m_columnsTexts ) + return; + + wxScopedArray oldTexts(m_columnsTexts); + m_columnsTexts = new wxString[numColumns - 2]; + for ( unsigned n = 1, m = 1; n < numColumns - 1; n++, m++ ) + { + if ( n == col ) + { + n--; + } + else // Not the deleted column. + { + m_columnsTexts[n - 1] = oldTexts[m - 1]; + } + } + } + + void OnClearColumns() + { + if ( m_columnsTexts ) + { + delete [] m_columnsTexts; + m_columnsTexts = NULL; + } + } + // Functions for modifying the tree. @@ -306,6 +337,8 @@ public: // Methods called by wxTreeListCtrl. void InsertColumn(unsigned col); + void DeleteColumn(unsigned col); + void ClearColumns(); Node* InsertItem(Node* parent, Node* previous, @@ -583,6 +616,32 @@ void wxTreeListModel::InsertColumn(unsigned col) } } +void wxTreeListModel::DeleteColumn(unsigned col) +{ + wxCHECK_RET( col < m_numColumns, "Invalid column index" ); + + // Update all the items to remove the text for the non first columns. + if ( col > 0 ) + { + for ( Node* node = m_root->GetChild(); node; node = node->NextInTree() ) + { + node->OnDeleteColumn(col, m_numColumns); + } + } + + m_numColumns--; +} + +void wxTreeListModel::ClearColumns() +{ + m_numColumns = 0; + + for ( Node* node = m_root->GetChild(); node; node = node->NextInTree() ) + { + node->OnClearColumns(); + } +} + wxTreeListModelNode* wxTreeListModel::InsertItem(Node* parent, Node* previous, @@ -1026,13 +1085,24 @@ bool wxTreeListCtrl::DeleteColumn(unsigned col) { wxCHECK_MSG( col < GetColumnCount(), false, "Invalid column index" ); - return m_view->DeleteColumn(m_view->GetColumn(col)); + if ( !m_view->DeleteColumn(m_view->GetColumn(col)) ) + return false; + + m_model->DeleteColumn(col); + + return true; } void wxTreeListCtrl::ClearColumns() { - if ( m_view ) - m_view->ClearColumns(); + // Don't assert here, clearing columns of the control before it's created + // can be considered valid (just useless). + if ( !m_model ) + return; + + m_view->ClearColumns(); + + m_model->ClearColumns(); } void wxTreeListCtrl::SetColumnWidth(unsigned col, int width)