From 4e616b638133d4cb80df5c1fe0f14ce87ecc5bd6 Mon Sep 17 00:00:00 2001 From: Jean Sirmai Date: Mon, 18 Dec 2023 16:31:04 +0100 Subject: [PATCH] R E C U R S I V E T R E E (1st version) OK --- demos/gtk-demo/tree_store.c | 39 +++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/demos/gtk-demo/tree_store.c b/demos/gtk-demo/tree_store.c index d7c56b9..728a02c 100644 --- a/demos/gtk-demo/tree_store.c +++ b/demos/gtk-demo/tree_store.c @@ -89,22 +89,33 @@ static TreeItem A[] = {{ "C", C }, { "D", D }, { NULL }}; static TreeItem R[] = {{ "A", A }, { "B", B }, { NULL }}; // ROOT static TreeItem O[] = {{ "R", R }, { NULL }}; // Artefact added for symmetry -static GtkTreeStore create_node_recursive (GtkTreeStore *model, TreeItem *current_item) +static GtkTreeModel *create_node_recursive (GtkTreeStore *model, + TreeItem *current_item, + GtkTreeIter *iter_parent, + int depth) { - TreeItem *item_children; - GtkTreeIter iter_children, iter_parent; + GtkTreeIter iter; - if (current_item->children) { - item_children = current_item->children; + if (model == NULL) + model = gtk_tree_store_new (NUM_COLUMNS, G_TYPE_STRING); - while (item_children->label) { - gtk_tree_store_append (model, &iter_children, &iter_parent); - gtk_tree_store_set (model, &iter_children, COLUMN_0, item_children->label, -1); - item_children++; - create_node_recursive (model, item_children); - } + while (current_item->label) { + if (0) printf("[%d] Current label : %s\n", depth, current_item->label); + gtk_tree_store_append (model, &iter, iter_parent); + gtk_tree_store_set (model, &iter, COLUMN_0, current_item->label, -1); + + if (current_item->children) + create_node_recursive (model, current_item->children, &iter, depth+1); + else + break; + + current_item++; } - return *model; + + if (depth == 0) + return GTK_TREE_MODEL(model); + else + return NULL; } static void create_node (GtkTreeStore *model, @@ -209,9 +220,7 @@ do_tree_store (GtkWidget *do_widget) gtk_box_append (GTK_BOX (vbox), sw); /* create tree_model */ - tree_model = create_model (); // GtkTreeStore *model = gtk_tree_store_new (NUM_COLUMNS, G_TYPE_STRING); - /* tree_store = gtk_tree_store_new (NUM_COLUMNS, G_TYPE_STRING); */ - /* tree_model = create_node_recursive (*tree_store, O); */ + tree_model = create_node_recursive (tree_store, R, NULL, 0); /* create tree view */ treeview = gtk_tree_view_new_with_model (tree_model);