From 7a0abbc5b31015624c32d30420cb15c641d352fd Mon Sep 17 00:00:00 2001 From: Jean Sirmai Date: Wed, 20 Dec 2023 18:52:19 +0100 Subject: [PATCH] WIP: reading https://developer-old.gnome.org/gtk4/stable/GtkTreeView.html --- demos/gtk-demo/tree_store.c | 64 +++++++++++++++++++++---------------- 1 file changed, 36 insertions(+), 28 deletions(-) diff --git a/demos/gtk-demo/tree_store.c b/demos/gtk-demo/tree_store.c index 53ed5a1..2a1d72e 100644 --- a/demos/gtk-demo/tree_store.c +++ b/demos/gtk-demo/tree_store.c @@ -49,6 +49,12 @@ enum NUM_COLUMNS }; +static void iterating_a_model_in_a_depth_first_fashion (GtkTreeModel *model) +{ + g_print ("iterating_a_model_in_a_depth_first_fashion (GtkTreeModel *model) is not yet implemented (line 54)\n"); + // gtk_tree_model_foreach (model, TRUE, my_user_data); https://developer-old.gnome.org/gtk4/stable/GtkTreeModel.html#GtkTreeModelForeachFunc + // gboolean (*GtkTreeModelForeachFunc) (GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpointer data); +} static void /* https://developer-old.gnome.org/gtk4/stable/GtkTreeModel.html#GtkTreePath-struct and #gtk-tree-row-reference-new */ edit_a_cell (GtkTreeModel *my_tree_model, @@ -81,38 +87,35 @@ edit_a_cell (GtkTreeModel *my_tree_model, // Three ways of getting the iter pointing to the location https://developer-old.gnome.org/gtk4/stable/GtkTreeModel.html#gtk-tree-row-reference-new static void acquiring_a_GtkTreeIter (GtkTreeModel *my_tree_model, GtkTreePath *my_tree_path) { - GtkTreeIter iter, parent_iter; // get the iterator from a string + GtkTreeIter iter; // get the iterator from a string gtk_tree_model_get_iter_from_string (my_tree_model, &iter, "3:2:5"); + g_print ("acquiring_a_GtkTreeIter from path : [ %s ]\n", gtk_tree_path_to_string (my_tree_path)); my_tree_path = gtk_tree_path_new_from_string ("3:2:5"); // get the iterator from a path gtk_tree_model_get_iter (my_tree_model, &iter, my_tree_path); gtk_tree_path_free (my_tree_path); - gtk_tree_model_iter_nth_child (my_tree_model, &iter, NULL, 3); // walk the tree to find the iterator - parent_iter = iter; gtk_tree_model_iter_nth_child (my_tree_model, &iter, &parent_iter, 2); - parent_iter = iter; gtk_tree_model_iter_nth_child (my_tree_model, &iter, &parent_iter, 5); + /* GtkTreeIter parent_iter; gtk_tree_model_iter_nth_child (my_tree_model, &iter, NULL, 3); // walk the tree to find the iterator */ + /* parent_iter = iter; gtk_tree_model_iter_nth_child (my_tree_model, &iter, &parent_iter, 2); */ + /* parent_iter = iter; gtk_tree_model_iter_nth_child (my_tree_model, &iter, &parent_iter, 5); */ } static void reading_data_from_a_GtkTreeModel(GtkTreeModel *my_tree_model) { GtkTreeIter iter; // Get the first iter in the list, check it is valid and walk through the list, reading each row. - gboolean valid = gtk_tree_model_get_iter_first (my_tree_model, &iter); int row_count = 0; - while (valid) - { - char *str_data; - int int_data; + char *my_str_data; - // Make sure you terminate calls to gtk_tree_model_get() with a “-1” value - gtk_tree_model_get (my_tree_model, &iter, STRING_COLUMN, &str_data, -1); + gboolean valid = gtk_tree_model_get_iter_first (my_tree_model, &iter); - // Do something with the data - g_print ("Row %d: (%s,%d)\n", row_count, str_data, int_data); - g_free (str_data); + while (valid) { // Make sure you terminate calls to gtk_tree_model_get() with a “-1” value + gtk_tree_model_get (my_tree_model, &iter, STRING_COLUMN, &my_str_data, -1); + g_print ("Row %d: (%s)\n", row_count, my_str_data); + valid = gtk_tree_model_iter_next (my_tree_model, &iter); // ? if (gtk_tree_model_iter_next (my_tree_model, &iter)) row_count++; ? + row_count++; + } - valid = gtk_tree_model_iter_next (my_tree_model, &iter); - row_count++; - } + g_free (my_str_data); // ? g_free (iter); } @@ -168,9 +171,9 @@ static GtkTreeModel *create_node_recursive (GtkTreeStore *model, GtkWidget * do_tree_store (GtkWidget *do_widget) { - static GtkWidget *window = NULL; + static GtkWidget *my_window = NULL; - if (!window) + if (! my_window) { GtkWidget *vbox; GtkWidget *sw; // sw : 'scrolled_window' @@ -179,16 +182,16 @@ do_tree_store (GtkWidget *do_widget) GtkTreeStore *my_tree_store = NULL; // The GtkTreeStore is used to store data in tree form, // to be used later on by a GtkTreeView to display it. /* create window, etc */ - window = gtk_window_new (); - gtk_window_set_title (GTK_WINDOW (window), "Tree Store"); - g_object_add_weak_pointer (G_OBJECT (window), (gpointer *)&window); + my_window = gtk_window_new (); + gtk_window_set_title (GTK_WINDOW (my_window), "Tree Store"); + g_object_add_weak_pointer (G_OBJECT (my_window), (gpointer *)&my_window); vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 8); gtk_widget_set_margin_start (vbox, 8); gtk_widget_set_margin_end (vbox, 8); gtk_widget_set_margin_top (vbox, 8); gtk_widget_set_margin_bottom (vbox, 8); - gtk_window_set_child (GTK_WINDOW (window), vbox); + gtk_window_set_child (GTK_WINDOW (my_window), vbox); gtk_box_append (GTK_BOX (vbox), gtk_label_new ("Learning GTK trees")); @@ -202,10 +205,15 @@ do_tree_store (GtkWidget *do_widget) /* create tree_model */ my_tree_model = create_node_recursive (my_tree_store, O, NULL, 0); reading_data_from_a_GtkTreeModel(my_tree_model); + iterating_a_model_in_a_depth_first_fashion (my_tree_model); + GtkTreePath *my_path = gtk_tree_path_new_from_string ("1:1:1"); + acquiring_a_GtkTreeIter (my_tree_model, my_path); + // GtkTreeIter *my_iter; edit_a_cell (my_tree_model, NULL, my_iter, NULL, my_path); /* create tree view */ treeview = gtk_tree_view_new_with_model (my_tree_model); gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (treeview), FALSE); + gtk_tree_view_set_enable_tree_lines (GTK_TREE_VIEW (treeview), TRUE); gtk_widget_set_vexpand (treeview, TRUE); gtk_tree_selection_set_mode (gtk_tree_view_get_selection (GTK_TREE_VIEW (treeview)), GTK_SELECTION_MULTIPLE); @@ -222,13 +230,13 @@ do_tree_store (GtkWidget *do_widget) /* expand all rows after the treeview widget has been realized */ g_signal_connect (treeview, "realize", G_CALLBACK (gtk_tree_view_expand_all), NULL); - gtk_window_set_default_size (GTK_WINDOW (window), 200, 400); + gtk_window_set_default_size (GTK_WINDOW (my_window), 200, 400); } - if (!gtk_widget_get_visible (window)) - gtk_widget_set_visible (window, TRUE); + if (!gtk_widget_get_visible (my_window)) + gtk_widget_set_visible (my_window, TRUE); else - gtk_window_destroy (GTK_WINDOW (window)); + gtk_window_destroy (GTK_WINDOW (my_window)); - return window; + return my_window; }