From 7b364f06749903e828be6d9c730f00b27459da49 Mon Sep 17 00:00:00 2001 From: Jean Sirmai Date: Mon, 25 Dec 2023 00:23:13 +0100 Subject: [PATCH] WIP: how to walk the tree ? A first step : first row 0: (ROOT) next row 0: (D) (line 103) --- demos/gtk-demo/tree_store.c | 112 +++++++++++++++++++++++++----------- 1 file changed, 80 insertions(+), 32 deletions(-) diff --git a/demos/gtk-demo/tree_store.c b/demos/gtk-demo/tree_store.c index 1920eb1..37ed669 100644 --- a/demos/gtk-demo/tree_store.c +++ b/demos/gtk-demo/tree_store.c @@ -27,6 +27,7 @@ /* _ _ _ _ _ _ _ _ _ _ _ _ _ _ T R E E _ _ _ _ _ _ _ _ _ _ _ _ _ _ */ /* */ /* https://developer-old.gnome.org/gtk4/stable/GtkTreeView.html */ +/* https://en.wikibooks.org/wiki/GTK%2B_By_Example/Tree_View/Custom_Models */ /* https://developer-old.gnome.org/gtk4/stable/GtkTreeSelection.html#GtkTreeSelection-struct */ /* https://developer-old.gnome.org/gtk4/stable/GtkTreeView.html#gtk-tree-view-get-path-at-pos << get-path-at-pos */ /* Finds the path at the point (x , y ), relative to bin_window coordinates. Use gtk_tree_view_convert_widget_to_bin_window_coords(). */ @@ -40,6 +41,7 @@ #include #include #include "config.h" +#include "custom-list.h" G_GNUC_BEGIN_IGNORE_DEPRECATIONS @@ -49,27 +51,89 @@ enum NUM_COLUMNS }; +static GObjectClass *parent_class = NULL; /* GObject stuff - nothing to worry about */ +static void custom_list_finalize (GObject *object); + +static void +custom_list_class_init (CustomListClass *klass) +{ + GObjectClass *object_class; + + parent_class = (GObjectClass*) g_type_class_peek_parent (klass); + object_class = (GObjectClass*) klass; + + + object_class->finalize = custom_list_finalize; +} + + +static void +custom_list_tree_model_init (GtkTreeModelIface *iface) +{ + /* Here we override the GtkTreeModel + * interface functions that we implement */ + iface->get_flags = NULL; // custom_list_get_flags; + iface->get_n_columns = NULL; // custom_list_get_n_columns; + iface->get_column_type = NULL; // custom_list_get_column_type; + iface->get_iter = NULL; // custom_list_get_iter; + iface->get_path = NULL; // custom_list_get_path; + iface->get_value = NULL; // custom_list_get_value; + iface->iter_next = NULL; // custom_list_iter_next; + iface->iter_children = NULL; // custom_list_iter_children; + iface->iter_has_child = NULL; // custom_list_iter_has_child; + iface->iter_n_children = NULL; // custom_list_iter_n_children; + iface->iter_nth_child = NULL; // custom_list_iter_nth_child; + iface->iter_parent = NULL; // custom_list_iter_parent; +} + +// gtk_tree_model_foreach (model, TRUE, my_user_data); https://developer-old.gnome.org/gtk4/stable/GtkTreeModel.html#GtkTreeModelForeachFunc static void iterating_a_model_in_a_depth_first_fashion (GtkTreeModel *model) { - // 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); - GtkTreeIter iter; - gboolean valid; + GtkTreeIter iter, iter_parent; + gboolean valid = 0; + char *str_data; int row_count = 0; + valid = gtk_tree_model_get_iter_first (model, &iter); - while (valid) - { - char *str_data; - // Make sure you terminate calls to gtk_tree_model_get() with a “-1” value - gtk_tree_model_get (model, &iter, STRING_COLUMN, &str_data, -1); - g_print ("Row %d: (%s) <> gtk_tree_model_get_iter_first (model, &iter) &iter = [%s] (line 67) ", row_count, str_data, &iter); - g_free (str_data); - valid = gtk_tree_model_iter_next (model, &iter); - // valid = gtk_tree_model_iter_children (model, &iter, parent); << how to get the parent ?? - g_print ("valid = %d = gtk_tree_model_iter_next (model, &iter) %s (line 71)\n",\ - valid, gtk_tree_model_get_string_from_iter (model, &iter)); - row_count++; - } + if (valid) gtk_tree_model_get (model, &iter, STRING_COLUMN, &str_data, -1); + g_print ("first row %d: (%s)\n", row_count, str_data); + + valid = gtk_tree_model_iter_children (model, &iter_parent, &iter); + valid = gtk_tree_model_iter_children (model, &iter, &iter_parent); + //gtk_tree_model_iter_next (model, &iter); + if (valid) gtk_tree_model_get (model, &iter, STRING_COLUMN, &str_data, -1); + g_print ("next row %d: (%s)\n", row_count, str_data); + + /* while (valid) first row 0: (ROOT) next row 0: (D)*/ + /* { */ + /* char *str_data; */ + /* // Make sure you terminate calls to gtk_tree_model_get() with a “-1” value */ + /* gtk_tree_model_get (model, &iter, STRING_COLUMN, &str_data, -1); */ + /* g_print ("Row %d: (%s) <> gtk_tree_model_get_iter_first (model, &iter) &iter = [%s] (line 67) ", row_count, str_data, "xxx");//&iter); */ + /* g_free (str_data); */ + /* valid = gtk_tree_model_iter_next (model, &iter); */ + /* // valid = gtk_tree_model_iter_children (model, &iter, parent); << how to get the parent ?? */ + /* g_print ("valid = %d = gtk_tree_model_iter_next (model, &iter) %s (line 69)\n",\ */ + /* valid, gtk_tree_model_get_string_from_iter (model, &iter)); */ + /* row_count++; */ + /* } */ +} + +// 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; // get the iterator from a string + gtk_tree_model_get_iter_from_string (my_tree_model, &iter, gtk_tree_path_to_string (my_tree_path)); + g_print ("acquiring_a_GtkTreeIter from path : [ %s ] (lines 225 & 108)\n", gtk_tree_path_to_string (my_tree_path)); + + my_tree_path = gtk_tree_path_new_from_string (gtk_tree_path_to_string (my_tree_path)); // 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); + + /* 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 /* https://developer-old.gnome.org/gtk4/stable/GtkTreeModel.html#GtkTreePath-struct and #gtk-tree-row-reference-new */ @@ -100,22 +164,6 @@ edit_a_cell (GtkTreeModel *my_tree_model, gtk_tree_model_row_inserted (my_tree_model, path, iter); // Emits the “row-inserted” signal on 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; // 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 ] (lines 225 & 108)\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); - - /* 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); */ -} - /* TreeItem structure */ typedef struct _TreeItem TreeItem; struct _TreeItem