From: Robert Roebling Date: Sun, 18 Nov 2007 23:54:57 +0000 (+0000) Subject: Implement wxGTK internal short-cut for wxDataViewIndexListModel X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/2056dede02ac6010a35086d9ea6738df9bc49796 Implement wxGTK internal short-cut for wxDataViewIndexListModel git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@50070 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/include/wx/dataview.h b/include/wx/dataview.h index e6a22c780d..01dfc25dc4 100644 --- a/include/wx/dataview.h +++ b/include/wx/dataview.h @@ -210,6 +210,9 @@ public: virtual int Compare( const wxDataViewItem &item1, const wxDataViewItem &item2, unsigned int column, bool ascending ); virtual bool HasDefaultCompare() const { return false; } + + // internal + virtual bool IsIndexListModel() const { return false; } protected: // the user should not delete this class directly: he should use DecRef() instead! @@ -266,10 +269,15 @@ public: virtual bool IsContainer( const wxDataViewItem &item ) const; virtual unsigned int GetChildren( const wxDataViewItem &item, wxDataViewItemArray &children ) const; + // internal + virtual bool IsIndexListModel() const { return true; } + unsigned int GetLastIndex() const { return m_lastIndex; } + private: wxDataViewItemArray m_hash; unsigned int m_lastIndex; bool m_ordered; + bool m_useHash; }; diff --git a/src/common/datavcmn.cpp b/src/common/datavcmn.cpp index fca5b24257..86e70906e8 100644 --- a/src/common/datavcmn.cpp +++ b/src/common/datavcmn.cpp @@ -299,13 +299,27 @@ int wxDataViewModel::Compare( const wxDataViewItem &item1, const wxDataViewItem wxDataViewIndexListModel::wxDataViewIndexListModel( unsigned int initial_size ) { - m_ordered = true; +#ifdef __WXGTK__ + m_useHash = false; +#else + m_useHash = true; +#endif - // build initial index - unsigned int i; - for (i = 1; i < initial_size+1; i++) - m_hash.Add( (void*) i ); - m_lastIndex = initial_size + 1; + if (m_useHash) + { + // IDs are ordered until an item gets deleted or inserted + m_ordered = true; + + // build initial index + unsigned int i; + for (i = 1; i < initial_size+1; i++) + m_hash.Add( (void*) i ); + m_lastIndex = initial_size + 1; + } + else + { + m_lastIndex = initial_size-1; + } } wxDataViewIndexListModel::~wxDataViewIndexListModel() @@ -314,37 +328,73 @@ wxDataViewIndexListModel::~wxDataViewIndexListModel() void wxDataViewIndexListModel::RowPrepended() { - m_ordered = false; + if (m_useHash) + { + m_ordered = false; - unsigned int id = m_lastIndex++; - m_hash.Insert( (void*) id, 0 ); - wxDataViewItem item( (void*) id ); - ItemAdded( wxDataViewItem(0), item ); + unsigned int id = m_lastIndex++; + m_hash.Insert( (void*) id, 0 ); + wxDataViewItem item( (void*) id ); + ItemAdded( wxDataViewItem(0), item ); + } + else + { + m_lastIndex++; + wxDataViewItem item( (void*) 0 ); + ItemAdded( wxDataViewItem(0), item ); + } } void wxDataViewIndexListModel::RowInserted( unsigned int before ) { - m_ordered = false; + if (m_useHash) + { + m_ordered = false; - unsigned int id = m_lastIndex++; - m_hash.Insert( (void*) id, before ); - wxDataViewItem item( (void*) id ); - ItemAdded( wxDataViewItem(0), item ); + unsigned int id = m_lastIndex++; + m_hash.Insert( (void*) id, before ); + wxDataViewItem item( (void*) id ); + ItemAdded( wxDataViewItem(0), item ); + } + else + { + m_lastIndex++; + wxDataViewItem item( (void*) before ); + ItemAdded( wxDataViewItem(0), item ); + } } void wxDataViewIndexListModel::RowAppended() { - unsigned int id = m_lastIndex++; - m_hash.Add( (void*) id ); - wxDataViewItem item( (void*) id ); - ItemAdded( wxDataViewItem(0), item ); + if (m_useHash) + { + unsigned int id = m_lastIndex++; + m_hash.Add( (void*) id ); + wxDataViewItem item( (void*) id ); + ItemAdded( wxDataViewItem(0), item ); + } + else + { + m_lastIndex++; + wxDataViewItem item( (void*) m_lastIndex ); + ItemAdded( wxDataViewItem(0), item ); + } } void wxDataViewIndexListModel::RowDeleted( unsigned int row ) { - wxDataViewItem item( m_hash[row] ); - wxDataViewModel::ItemDeleted( wxDataViewItem(0), item ); - m_hash.RemoveAt( row ); + if (m_useHash) + { + wxDataViewItem item( m_hash[row] ); + wxDataViewModel::ItemDeleted( wxDataViewItem(0), item ); + m_hash.RemoveAt( row ); + } + else + { + wxDataViewItem item( (void*) row ); + wxDataViewModel::ItemDeleted( wxDataViewItem(0), item ); + m_lastIndex++; + } } void wxDataViewIndexListModel::RowChanged( unsigned int row ) @@ -359,20 +409,34 @@ void wxDataViewIndexListModel::RowValueChanged( unsigned int row, unsigned int c unsigned int wxDataViewIndexListModel::GetRow( const wxDataViewItem &item ) const { - if (m_ordered) + if (m_useHash) { - unsigned int pos = wxPtrToUInt(item.GetID()); - return pos-1; - } + if (m_ordered) + { + unsigned int pos = wxPtrToUInt( item.GetID() ); + return pos-1; + } - // assert for not found - return (unsigned int) m_hash.Index( item.GetID() ); + // assert for not found + return (unsigned int) m_hash.Index( item.GetID() ); + } + else + { + return wxPtrToUInt( item.GetID() ); + } } wxDataViewItem wxDataViewIndexListModel::GetItem( unsigned int row ) const { - wxASSERT( row < m_hash.GetCount() ); - return wxDataViewItem( m_hash[row] ); + if (m_useHash) + { + wxASSERT( row < m_hash.GetCount() ); + return wxDataViewItem( m_hash[row] ); + } + else + { + return wxDataViewItem( (void*) row ); + } } bool wxDataViewIndexListModel::HasDefaultCompare() const @@ -385,7 +449,7 @@ int wxDataViewIndexListModel::Compare(const wxDataViewItem& item1, unsigned int WXUNUSED(column), bool ascending) { - if (m_ordered) + if (m_ordered || !m_useHash) { unsigned int pos1 = wxPtrToUInt(item1.GetID()); unsigned int pos2 = wxPtrToUInt(item2.GetID()); @@ -435,6 +499,9 @@ bool wxDataViewIndexListModel::IsContainer( const wxDataViewItem &item ) const unsigned int wxDataViewIndexListModel::GetChildren( const wxDataViewItem &item, wxDataViewItemArray &children ) const { + if (m_useHash) + return 0; // error + if (item.IsOk()) return 0; diff --git a/src/gtk/dataview.cpp b/src/gtk/dataview.cpp index 9ab8f4dfc4..a34303416b 100644 --- a/src/gtk/dataview.cpp +++ b/src/gtk/dataview.cpp @@ -59,6 +59,7 @@ public: wxDataViewCtrlInternal( wxDataViewCtrl *owner, wxDataViewModel *wx_model, GtkWxTreeModel *owner ); ~wxDataViewCtrlInternal(); + GtkTreeModelFlags get_flags(); gboolean get_iter( GtkTreeIter *iter, GtkTreePath *path ); GtkTreePath *get_path( GtkTreeIter *iter); gboolean iter_next( GtkTreeIter *iter ); @@ -431,9 +432,10 @@ wxgtk_tree_model_finalize (GObject *object) static GtkTreeModelFlags wxgtk_tree_model_get_flags (GtkTreeModel *tree_model) { - g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (tree_model), (GtkTreeModelFlags)0 ); + GtkWxTreeModel *wxtree_model = (GtkWxTreeModel *) tree_model; + g_return_val_if_fail (GTK_IS_WX_TREE_MODEL (wxtree_model), (GtkTreeModelFlags)0 ); - return GTK_TREE_MODEL_ITERS_PERSIST; + return wxtree_model->internal->get_flags(); } static gint @@ -938,7 +940,7 @@ gtk_wx_cell_renderer_render (GtkCellRenderer *renderer, wxRect renderrect( rect.x, rect.y, rect.width, rect.height ); wxWindowDC* dc = (wxWindowDC*) cell->GetDC(); #if wxUSE_NEW_DC - wxGTKWindowImplDC *impldc = (wxGTKWindowImplDC *) dc->GetImpl(); + wxGTKWindowDCImpl *impldc = (wxGTKWindowDCImpl *) dc->GetImpl(); if (impldc->m_window == NULL) { impldc->m_window = window; @@ -1661,7 +1663,7 @@ public: wxDataViewCtrlDC( wxDataViewCtrl *window ) { #if wxUSE_NEW_DC - wxGTKWindowImplDC *impl = (wxGTKWindowImplDC*) GetImpl(); + wxGTKWindowDCImpl *impl = (wxGTKWindowDCImpl*) GetImpl(); GtkWidget *widget = window->m_treeview; // Set later @@ -2107,6 +2109,9 @@ static void wxGtkTreeCellDataFunc( GtkTreeViewColumn *column, wxDataViewModel *wx_model = tree_model->internal->GetDataViewModel(); + if (!wx_model->IsIndexListModel()) + { + if (wx_model->IsContainer( item )) { if (wx_model->HasContainerColumns( item ) || (cell->GetOwner()->GetModelColumn() == 0)) @@ -2136,6 +2141,8 @@ static void wxGtkTreeCellDataFunc( GtkTreeViewColumn *column, g_object_set_property( G_OBJECT(renderer), "visible", &gvalue ); g_value_unset( &gvalue ); } + + } wxVariant value; wx_model->GetValue( value, item, cell->GetOwner()->GetModelColumn() ); @@ -2571,7 +2578,9 @@ wxDataViewCtrlInternal::wxDataViewCtrlInternal( wxDataViewCtrl *owner, m_sort_order = GTK_SORT_ASCENDING; m_sort_column = -1; m_dataview_sort_column = NULL; - InitTree(); + + if (!m_wx_model->IsIndexListModel()) + InitTree(); } wxDataViewCtrlInternal::~wxDataViewCtrlInternal() @@ -2610,24 +2619,31 @@ void wxDataViewCtrlInternal::BuildBranch( wxGtkTreeModelNode *node ) void wxDataViewCtrlInternal::Resort() { - m_root->Resort(); + if (!m_wx_model->IsIndexListModel()) + m_root->Resort(); } bool wxDataViewCtrlInternal::ItemAdded( const wxDataViewItem &parent, const wxDataViewItem &item ) { - wxGtkTreeModelNode *parent_node = FindNode( parent ); - if (m_wx_model->IsContainer( item )) - parent_node->AddNode( new wxGtkTreeModelNode( parent_node, item, this ) ); - else - parent_node->AddLeave( item.GetID() ); + if (!m_wx_model->IsIndexListModel()) + { + wxGtkTreeModelNode *parent_node = FindNode( parent ); + if (m_wx_model->IsContainer( item )) + parent_node->AddNode( new wxGtkTreeModelNode( parent_node, item, this ) ); + else + parent_node->AddLeave( item.GetID() ); + } return true; } bool wxDataViewCtrlInternal::ItemDeleted( const wxDataViewItem &parent, const wxDataViewItem &item ) { - wxGtkTreeModelNode *parent_node = FindNode( parent ); - parent_node->DeleteChild( item.GetID() ); + if (!m_wx_model->IsIndexListModel()) + { + wxGtkTreeModelNode *parent_node = FindNode( parent ); + parent_node->DeleteChild( item.GetID() ); + } return true; } @@ -2661,41 +2677,67 @@ bool wxDataViewCtrlInternal::Cleared() return true; } +GtkTreeModelFlags wxDataViewCtrlInternal::get_flags() +{ + if (m_wx_model->IsIndexListModel()) + return GTK_TREE_MODEL_LIST_ONLY; + else + return GTK_TREE_MODEL_ITERS_PERSIST; +} + gboolean wxDataViewCtrlInternal::get_iter( GtkTreeIter *iter, GtkTreePath *path ) { - int depth = gtk_tree_path_get_depth( path ); + if (m_wx_model->IsIndexListModel()) + { + wxDataViewIndexListModel *wx_model = (wxDataViewIndexListModel*) m_wx_model; + + unsigned int i = (unsigned int)gtk_tree_path_get_indices (path)[0]; + + if (i >= wx_model->GetLastIndex()) + return FALSE; - wxGtkTreeModelNode *node = m_root; + iter->stamp = m_gtk_model->stamp; + // user_data is just the index + iter->user_data = (gpointer) i; - int i; - for (i = 0; i < depth; i++) + return TRUE; + } + else { - BuildBranch( node ); + int depth = gtk_tree_path_get_depth( path ); + + wxGtkTreeModelNode *node = m_root; + + int i; + for (i = 0; i < depth; i++) + { + BuildBranch( node ); - gint pos = gtk_tree_path_get_indices (path)[i]; - if (pos < 0) return FALSE; - if ((size_t)pos >= node->GetChildCount()) return FALSE; + gint pos = gtk_tree_path_get_indices (path)[i]; + if (pos < 0) return FALSE; + if ((size_t)pos >= node->GetChildCount()) return FALSE; - void* id = node->GetChildren().Item( (size_t) pos ); + void* id = node->GetChildren().Item( (size_t) pos ); - if (i == depth-1) - { - iter->stamp = m_gtk_model->stamp; - iter->user_data = id; - return TRUE; - } - - size_t count = node->GetNodes().GetCount(); - size_t pos2; - for (pos2 = 0; pos2 < count; pos2++) - { - wxGtkTreeModelNode *child_node = node->GetNodes().Item( pos2 ); - if (child_node->GetItem().GetID() == id) + if (i == depth-1) { - node = child_node; - break; + iter->stamp = m_gtk_model->stamp; + iter->user_data = id; + return TRUE; } - } + + size_t count = node->GetNodes().GetCount(); + size_t pos2; + for (pos2 = 0; pos2 < count; pos2++) + { + wxGtkTreeModelNode *child_node = node->GetNodes().Item( pos2 ); + if (child_node->GetItem().GetID() == id) + { + node = child_node; + break; + } + } + } } return FALSE; @@ -2704,118 +2746,207 @@ gboolean wxDataViewCtrlInternal::get_iter( GtkTreeIter *iter, GtkTreePath *path GtkTreePath *wxDataViewCtrlInternal::get_path( GtkTreeIter *iter ) { GtkTreePath *retval = gtk_tree_path_new (); - void *id = iter->user_data; - wxGtkTreeModelNode *node = FindParentNode( iter ); - while (node) + if (m_wx_model->IsIndexListModel()) + { + // user_data is just the index + int i = (wxUIntPtr) iter->user_data; + gtk_tree_path_append_index (retval, i); + } + else { - int pos = node->GetChildren().Index( id ); + void *id = iter->user_data; + + wxGtkTreeModelNode *node = FindParentNode( iter ); + while (node) + { + int pos = node->GetChildren().Index( id ); - gtk_tree_path_prepend_index( retval, pos ); + gtk_tree_path_prepend_index( retval, pos ); - id = node->GetItem().GetID(); - node = node->GetParent(); + id = node->GetItem().GetID(); + node = node->GetParent(); + } } - + return retval; } gboolean wxDataViewCtrlInternal::iter_next( GtkTreeIter *iter ) { - wxGtkTreeModelNode *parent = FindParentNode( iter ); - if( parent == NULL ) - return FALSE; + if (m_wx_model->IsIndexListModel()) + { + wxDataViewIndexListModel *wx_model = (wxDataViewIndexListModel*) m_wx_model; + + int n = (wxUIntPtr) iter->user_data; - int pos = parent->GetChildren().Index( iter->user_data ); + if (n == -1) + return FALSE; - if (pos == (int) parent->GetChildCount()-1) - return FALSE; + if (n >= (int) wx_model->GetLastIndex()-2) + return FALSE; + + iter->user_data = (gpointer) ++n; + } + else + { + wxGtkTreeModelNode *parent = FindParentNode( iter ); + if( parent == NULL ) + return FALSE; + + int pos = parent->GetChildren().Index( iter->user_data ); + + if (pos == (int) parent->GetChildCount()-1) + return FALSE; - iter->stamp = m_gtk_model->stamp; - iter->user_data = parent->GetChildren().Item( pos+1 ); + iter->stamp = m_gtk_model->stamp; + iter->user_data = parent->GetChildren().Item( pos+1 ); + } return TRUE; } gboolean wxDataViewCtrlInternal::iter_children( GtkTreeIter *iter, GtkTreeIter *parent ) { - wxDataViewItem item( (void*) parent->user_data ); + if (m_wx_model->IsIndexListModel()) + { + // this is a list, nodes have no children + if (parent) + return FALSE; + + iter->stamp = m_gtk_model->stamp; + iter->user_data = (gpointer) -1; + + return TRUE; + } + else + { + wxDataViewItem item( (void*) parent->user_data ); - if (!m_wx_model->IsContainer( item )) - return FALSE; + if (!m_wx_model->IsContainer( item )) + return FALSE; - wxGtkTreeModelNode *parent_node = FindNode( parent ); - BuildBranch( parent_node ); + wxGtkTreeModelNode *parent_node = FindNode( parent ); + BuildBranch( parent_node ); - if (parent_node->GetChildCount() == 0) - return FALSE; + if (parent_node->GetChildCount() == 0) + return FALSE; - iter->stamp = m_gtk_model->stamp; - iter->user_data = (gpointer) parent_node->GetChildren().Item( 0 ); - + iter->stamp = m_gtk_model->stamp; + iter->user_data = (gpointer) parent_node->GetChildren().Item( 0 ); + } + return TRUE; } gboolean wxDataViewCtrlInternal::iter_has_child( GtkTreeIter *iter ) { - wxDataViewItem item( (void*) iter->user_data ); + if (m_wx_model->IsIndexListModel()) + { + // this is a list, nodes have no children + return FALSE; + } + else + { + wxDataViewItem item( (void*) iter->user_data ); - bool is_container = m_wx_model->IsContainer( item ); + bool is_container = m_wx_model->IsContainer( item ); - if (!is_container) - return FALSE; + if (!is_container) + return FALSE; - wxGtkTreeModelNode *node = FindNode( iter ); - BuildBranch( node ); + wxGtkTreeModelNode *node = FindNode( iter ); + BuildBranch( node ); - return (node->GetChildCount() > 0); + return (node->GetChildCount() > 0); + } } gint wxDataViewCtrlInternal::iter_n_children( GtkTreeIter *iter ) { - wxDataViewItem item( (void*) iter->user_data ); - - if (!m_wx_model->IsContainer( item )) + if (m_wx_model->IsIndexListModel()) + { + wxDataViewIndexListModel *wx_model = (wxDataViewIndexListModel*) m_wx_model; + + if (iter == NULL) + return (gint) wx_model->GetLastIndex()-1; + return 0; + } + else + { + wxDataViewItem item( (void*) iter->user_data ); + + if (!m_wx_model->IsContainer( item )) + return 0; - wxGtkTreeModelNode *parent_node = FindNode( iter ); - BuildBranch( parent_node ); + wxGtkTreeModelNode *parent_node = FindNode( iter ); + BuildBranch( parent_node ); - // wxPrintf( "iter_n_children %d\n", parent_node->GetChildCount() ); + // wxPrintf( "iter_n_children %d\n", parent_node->GetChildCount() ); - return parent_node->GetChildCount(); + return parent_node->GetChildCount(); + } } gboolean wxDataViewCtrlInternal::iter_nth_child( GtkTreeIter *iter, GtkTreeIter *parent, gint n ) { - void* id = NULL; - if (parent) id = (void*) parent->user_data; - wxDataViewItem item( id ); + if (m_wx_model->IsIndexListModel()) + { + wxDataViewIndexListModel *wx_model = (wxDataViewIndexListModel*) m_wx_model; + + if (parent) + return FALSE; + + if (n < 0) + return FALSE; + + if (n >= (gint) wx_model->GetLastIndex()-1) + return FALSE; + + iter->stamp = m_gtk_model->stamp; + iter->user_data = (gpointer) n; + + return TRUE; + } + else + { + void* id = NULL; + if (parent) id = (void*) parent->user_data; + wxDataViewItem item( id ); - if (!m_wx_model->IsContainer( item )) - return FALSE; + if (!m_wx_model->IsContainer( item )) + return FALSE; - wxGtkTreeModelNode *parent_node = FindNode( parent ); - BuildBranch( parent_node ); + wxGtkTreeModelNode *parent_node = FindNode( parent ); + BuildBranch( parent_node ); - // wxPrintf( "iter_nth_child %d\n", n ); + // wxPrintf( "iter_nth_child %d\n", n ); - iter->stamp = m_gtk_model->stamp; - iter->user_data = parent_node->GetChildren().Item( n ); + iter->stamp = m_gtk_model->stamp; + iter->user_data = parent_node->GetChildren().Item( n ); - return TRUE; + return TRUE; + } } gboolean wxDataViewCtrlInternal::iter_parent( GtkTreeIter *iter, GtkTreeIter *child ) { - wxGtkTreeModelNode *node = FindParentNode( child ); - if (!node) + if (m_wx_model->IsIndexListModel()) + { return FALSE; + } + else + { + wxGtkTreeModelNode *node = FindParentNode( child ); + if (!node) + return FALSE; - iter->stamp = m_gtk_model->stamp; - iter->user_data = (gpointer) node->GetItem().GetID(); + iter->stamp = m_gtk_model->stamp; + iter->user_data = (gpointer) node->GetItem().GetID(); - return TRUE; + return TRUE; + } } static wxGtkTreeModelNode*