From 95ba651873ef5e263139a46ec76cb26b6cc79647 Mon Sep 17 00:00:00 2001 From: Chris Toshok Date: Tue, 22 Aug 2000 00:42:14 +0000 Subject: we can remove nodes with children now. 2000-08-21 Chris Toshok * e-tree-example-1.c (remove_node): we can remove nodes with children now. * e-tree-model.h: add prototype for e_tree_model_node_sort. * e-tree-model.c (etree_set_expanded): if the node is invisible, just set its expanded flag and return. (e_tree_model_root_node_set_visible): call set_expanded before we remove it from the row array or else the aforementioned change will result in nothing happening. (e_tree_model_node_insert): use a position of -1 as "append". (e_tree_model_node_insert): if the model was marked with root_visible == FALSE, make sure to set it's expanded flag to TRUE when the root node is inserted. (e_tree_model_node_sort): new function. svn path=/trunk/; revision=4912 --- widgets/table/e-tree-model.c | 54 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 49 insertions(+), 5 deletions(-) (limited to 'widgets/table/e-tree-model.c') diff --git a/widgets/table/e-tree-model.c b/widgets/table/e-tree-model.c index fefe6c68da..943f190732 100644 --- a/widgets/table/e-tree-model.c +++ b/widgets/table/e-tree-model.c @@ -138,7 +138,11 @@ etree_set_expanded (ETreeModel *etm, ETreePath* node, gboolean expanded) enode->expanded = expanded; - row = e_tree_model_row_of_node (etm, node) + 1; + /* if the node wasn't visible at present */ + if ((row = e_tree_model_row_of_node (etm, node)) == -1) + return; + + row++; if (expanded) { GNode *parent; @@ -445,6 +449,8 @@ e_tree_model_row_of_node (ETreeModel *etree, ETreePath *node) if (g_array_index (etree->row_array, GNode*, i) == node) return i; + g_warning ("e_tree_model_row_of_node failed for node %p\n", node); + return -1; } @@ -459,8 +465,8 @@ e_tree_model_root_node_set_visible (ETreeModel *etm, gboolean visible) } else { ETreePath *root_path = e_tree_model_get_root (etm); - etm->row_array = g_array_remove_index (etm->row_array, 0); e_tree_model_node_set_expanded (etm, root_path, TRUE); + etm->row_array = g_array_remove_index (etm->row_array, 0); } e_table_model_changed (E_TABLE_MODEL (etm)); @@ -597,17 +603,20 @@ e_tree_model_node_insert (ETreeModel *tree_model, if (e_tree_model_node_is_visible (tree_model, new_path)) { int parent_row; - GNode *node; + GNode *n; /* we need to iterate back up to the root, incrementing the number of visible descendents */ - for (node = parent_path; node; node = node->parent) { - ENode *parent_enode = (ENode*)node->data; + for (n = parent_path; n; n = n->parent) { + ENode *parent_enode = (ENode*)n->data; parent_enode->visible_descendents ++; } /* finally, insert a row into the table */ + if (position == -1) + position = e_tree_model_node_num_visible_descendents (tree_model, parent_path) - 1; + parent_row = e_tree_model_row_of_node (tree_model, parent_path); tree_model->row_array = g_array_insert_val (tree_model->row_array, @@ -622,6 +631,11 @@ e_tree_model_node_insert (ETreeModel *tree_model, tree_model->row_array = g_array_insert_val (tree_model->row_array, 0, tree_model->root); e_table_model_row_inserted (E_TABLE_MODEL (tree_model), 0); } + else { + /* need to mark the new node as expanded or + we'll never see it's children */ + node->expanded = TRUE; + } new_path = tree_model->root; } @@ -713,3 +727,33 @@ add_visible_descendents_to_array (ETreeModel *etm, GNode *gnode, int *row, int * } } } + +void +e_tree_model_node_sort (ETreeModel *tree_model, + ETreePath *node, + GCompareFunc compare) +{ + int num_nodes = g_node_n_children (node); + ETreePath **path_array; + int i; + + if (num_nodes == 0) + return; + + path_array = g_new (ETreePath*, num_nodes); + + for (i = 0; i < num_nodes; i ++) { + path_array[i] = g_node_first_child(node); + g_node_unlink (path_array[i]); + } + + qsort (path_array, num_nodes, sizeof(ETreePath*), compare); + + for (i = 0; i < num_nodes; i ++) { + g_node_append (node, path_array[i]); + } + + g_free (path_array); + + e_table_model_changed (E_TABLE_MODEL (tree_model)); +} -- cgit