From: Robert Roebling Date: Mon, 17 Dec 2007 11:28:02 +0000 (+0000) Subject: Added wxDataViewListIndexModel::RowsDeleted() and various related corrections X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/8b6cf9bf10a43343734d1bb22f2fae3ade2f4a67 Added wxDataViewListIndexModel::RowsDeleted() and various related corrections git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@50764 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/docs/latex/wx/dataviewindexlistmodel.tex b/docs/latex/wx/dataviewindexlistmodel.tex index c334f0e6a0..489ff84848 100644 --- a/docs/latex/wx/dataviewindexlistmodel.tex +++ b/docs/latex/wx/dataviewindexlistmodel.tex @@ -91,6 +91,14 @@ Call this after a row has been changed. Call this after a row has been deleted. +\membersection{wxDataViewIndexListModel::RowsDeleted}\label{wxdataviewindexlistmodelrowdeleted} + +\func{void}{RowsDeleted}{\param{const wxArrayInt &}{rows}} + +Call this after rows have been deleted. The array will internally +get copied and sorted in descending order so that the rows with +the highest position will be deleted first. + \membersection{wxDataViewIndexListModel::RowInserted}\label{wxdataviewindexlistmodelrowinserted} \func{void}{RowInserted}{\param{unsigned int }{before}} diff --git a/include/wx/dataview.h b/include/wx/dataview.h index c4d5874937..0f64e7f156 100644 --- a/include/wx/dataview.h +++ b/include/wx/dataview.h @@ -245,6 +245,7 @@ public: void RowInserted( unsigned int before ); void RowAppended(); void RowDeleted( unsigned int row ); + void RowsDeleted( const wxArrayInt &rows ); void RowChanged( unsigned int row ); void RowValueChanged( unsigned int row, unsigned int col ); diff --git a/samples/dataview/dataview.cpp b/samples/dataview/dataview.cpp index c852284476..bfa398f454 100644 --- a/samples/dataview/dataview.cpp +++ b/samples/dataview/dataview.cpp @@ -353,6 +353,17 @@ private: bool m_classicalMusicIsKnownToControl; }; + +static int my_sort_reverse( int *v1, int *v2 ) +{ + return *v2-*v1; +} + +static int my_sort( int *v1, int *v2 ) +{ + return *v1-*v2; +} + class MyListModel: public wxDataViewIndexListModel { public: @@ -388,6 +399,34 @@ public: m_array.RemoveAt( row ); RowDeleted( row ); } + + void DeleteItems( const wxDataViewItemArray &items ) + { + wxArrayInt rows; + unsigned int i; + for (i = 0; i < items.GetCount(); i++) + { + unsigned int row = GetRow( items[i] ); + rows.Add( row ); + } + + // Sort in descending order so that the last + // row will be deleted first. Otherwise the + // remaining indeces would all be wrong. + rows.Sort( my_sort_reverse ); + for (i = 0; i < rows.GetCount(); i++) + m_array.RemoveAt( rows[i] ); + + // This is just to test if wxDataViewCtrl can + // cope with removing rows not sorted in + // descending order + rows.Sort( my_sort ); + RowsDeleted( rows ); + } + + void AddMany() + { + } // implementation of base class virtuals to define model @@ -522,6 +561,7 @@ public: void OnRightClick( wxMouseEvent &event ); void OnGoto( wxCommandEvent &event); + void OnAddMany( wxCommandEvent &event); private: wxDataViewCtrl* m_musicCtrl; @@ -552,7 +592,7 @@ bool MyApp::OnInit(void) // build the first frame MyFrame *frame = - new MyFrame(NULL, wxT("wxDataViewCtrl feature test"), 40, 40, 800, 440); + new MyFrame(NULL, wxT("wxDataViewCtrl feature test"), 40, 40, 800, 540); frame->Show(true); SetTopWindow(frame); @@ -583,7 +623,8 @@ enum ID_PREPEND_LIST = 200, ID_DELETE_LIST = 201, - ID_GOTO = 202 + ID_GOTO = 202, + ID_ADD_MANY = 203 }; BEGIN_EVENT_TABLE(MyFrame, wxFrame) @@ -595,6 +636,7 @@ BEGIN_EVENT_TABLE(MyFrame, wxFrame) EVT_BUTTON( ID_PREPEND_LIST, MyFrame::OnPrependList ) EVT_BUTTON( ID_DELETE_LIST, MyFrame::OnDeleteList ) EVT_BUTTON( ID_GOTO, MyFrame::OnGoto) + EVT_BUTTON( ID_ADD_MANY, MyFrame::OnAddMany) EVT_DATAVIEW_ITEM_VALUE_CHANGED( ID_MUSIC_CTRL, MyFrame::OnValueChanged ) @@ -696,9 +738,12 @@ MyFrame::MyFrame(wxFrame *frame, const wxString &title, int x, int y, int w, int button_sizer->Add( new wxButton( this, ID_DELETE_MUSIC, "Delete selected"), 0, wxALL, 10 ); button_sizer->Add( new wxButton( this, ID_DELETE_YEAR, "Delete \"Year\" column"), 0, wxALL, 10 ); button_sizer->Add( 10, 10, 1 ); - button_sizer->Add( new wxButton( this, ID_PREPEND_LIST, "Prepend"), 0, wxALL, 10 ); - button_sizer->Add( new wxButton( this, ID_DELETE_LIST, "Delete selected"), 0, wxALL, 10 ); - button_sizer->Add( new wxButton( this, ID_GOTO, "Goto 50"), 0, wxALL, 10 ); + wxFlexGridSizer *grid_sizer = new wxFlexGridSizer( 2, 2 ); + grid_sizer->Add( new wxButton( this, ID_PREPEND_LIST, "Prepend"), 0, wxALL, 2 ); + grid_sizer->Add( new wxButton( this, ID_DELETE_LIST, "Delete selected"), 0, wxALL, 2 ); + grid_sizer->Add( new wxButton( this, ID_GOTO, "Goto 50"), 0, wxALL, 2 ); + grid_sizer->Add( new wxButton( this, ID_ADD_MANY, "Add 1000"), 0, wxALL, 2 ); + button_sizer->Add( grid_sizer, 0, wxALL, 10 ); main_sizer->Add( button_sizer, 0, wxGROW, 0 ); @@ -783,9 +828,8 @@ void MyFrame::OnDeleteList( wxCommandEvent& WXUNUSED(event) ) { wxDataViewItemArray items; int len = m_listCtrl->GetSelections( items ); - for( int i = 0; i < len; i ++ ) - if (items[i].IsOk()) - m_list_model->DeleteItem( items[i] ); + if (len > 0) + m_list_model->DeleteItems( items ); } void MyFrame::OnValueChanged( wxDataViewEvent &event ) @@ -925,6 +969,12 @@ void MyFrame::OnGoto(wxCommandEvent& WXUNUSED(event)) m_listCtrl->EnsureVisible(item,m_col); } +void MyFrame::OnAddMany(wxCommandEvent& WXUNUSED(event)) +{ + m_list_model->AddMany(); +} + + void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) ) { wxAboutDialogInfo info; diff --git a/src/common/datavcmn.cpp b/src/common/datavcmn.cpp index a95f2d7563..f487ba4d0c 100644 --- a/src/common/datavcmn.cpp +++ b/src/common/datavcmn.cpp @@ -385,6 +385,8 @@ void wxDataViewIndexListModel::RowDeleted( unsigned int row ) { if (m_useHash) { + m_ordered = false; + wxDataViewItem item( m_hash[row] ); wxDataViewModel::ItemDeleted( wxDataViewItem(0), item ); m_hash.RemoveAt( row ); @@ -397,6 +399,47 @@ void wxDataViewIndexListModel::RowDeleted( unsigned int row ) } } +static int my_sort( int *v1, int *v2 ) +{ + return *v2-*v1; +} + +void wxDataViewIndexListModel::RowsDeleted( const wxArrayInt &rows ) +{ + wxArrayInt sorted = rows; + sorted.Sort( my_sort ); + + if (m_useHash) + { + m_ordered = false; + + wxDataViewItemArray array; + unsigned int i; + for (i = 0; i < rows.GetCount(); i++) + { + wxDataViewItem item( m_hash[rows[i]] ); + array.Add( item ); + } + wxDataViewModel::ItemsDeleted( wxDataViewItem(0), array ); + + for (i = 0; i < sorted.GetCount(); i++) + m_hash.RemoveAt( sorted[i] ); + } + else + { + wxDataViewItemArray array; + unsigned int i; + for (i = 0; i < sorted.GetCount(); i++) + { + wxDataViewItem item( (void*) sorted[i] ); + array.Add( item ); + } + wxDataViewModel::ItemsDeleted( wxDataViewItem(0), array ); + + m_lastIndex -= rows.GetCount(); + } +} + void wxDataViewIndexListModel::RowChanged( unsigned int row ) { wxDataViewModel::ItemChanged( GetItem(row) ); diff --git a/src/generic/datavgen.cpp b/src/generic/datavgen.cpp index d7b2598ad1..5930e26a9f 100644 --- a/src/generic/datavgen.cpp +++ b/src/generic/datavgen.cpp @@ -2048,6 +2048,13 @@ bool Walker( wxDataViewTreeNode * node, DoJob & func ) bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem & parent, const wxDataViewItem & item) { + if (!m_root) + { + m_count++; + UpdateDisplay(); + return true; + } + SortPrepare(); wxDataViewTreeNode * node; @@ -2081,6 +2088,19 @@ void DestroyTreeHelper( wxDataViewTreeNode * node); bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem& parent, const wxDataViewItem& item) { + if (!m_root) + { + m_count--; + if( m_currentRow > GetRowCount() ) + m_currentRow = m_count - 1; + + m_selection.Empty(); + + UpdateDisplay(); + + return true; + } + wxDataViewTreeNode * node = FindNode(parent); wxCHECK_MSG( node != NULL, false, "item not found" );