From 57bb001d111664b6eedd52a8eedc09f472057a75 Mon Sep 17 00:00:00 2001 From: Jean Sirmai Date: Wed, 20 Dec 2023 13:57:03 +0100 Subject: [PATCH] WIP: reading https://developer-old.gnome.org/gtk4/stable/GtkTreeModel.html#gtk-tree-row-reference-new --- demos/gtk-demo/tree_store.c | 98 ++++++++++++++++++++++++++++++------- 1 file changed, 81 insertions(+), 17 deletions(-) diff --git a/demos/gtk-demo/tree_store.c b/demos/gtk-demo/tree_store.c index 7459a36..53ed5a1 100644 --- a/demos/gtk-demo/tree_store.c +++ b/demos/gtk-demo/tree_store.c @@ -21,7 +21,8 @@ /* */ /* https://developer-old.gnome.org/gtk4/stable/ch03s02.html Which widget should I use ?... */ /* https://developer-old.gnome.org/gtk4/stable/GtkCellEditable.html#GtkCellEditable-struct */ -/* */ +/* The GtkCellEditable interface must be implemented for widgets to be usable to edit the contents of a GtkTreeView cell. */ +/* It provides a way to specify how temporary widgets should be configured for editing, get the new value, etc. */ /* _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ */ /* _ _ _ _ _ _ _ _ _ _ _ _ _ _ T R E E _ _ _ _ _ _ _ _ _ _ _ _ _ _ */ /* */ @@ -42,8 +43,77 @@ G_GNUC_BEGIN_IGNORE_DEPRECATIONS +enum +{ + STRING_COLUMN, + NUM_COLUMNS +}; -/* The GtkTreeStore is used to store data in tree form, to be used later on by a GtkTreeView to display it. */ + +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, + GtkCellEditable *cell_editable, + GtkTreeIter *iter, + GdkEvent *event, + GtkTreePath *path) +{ + GtkTreePath *any_path = gtk_tree_path_new_first (); // Creates and returns a new GtkTreePath. + // The string representation of this path is “0”. + const char *any_path_string = gtk_tree_path_to_string (any_path); // Generates a string representation of the path. + // Path is expected to be a colon separated list of numbers. For example, the string “10:4:0”. + // This string would create a path of depth 3 pointing to the 11th child of the root node, + // the 5th child of that 11th child, and the 1st child of that 5th child. + // If the path has depth 0, NULL is returned. + any_path = gtk_tree_path_new_from_indices (10,4,0); // Creates a new path with first_index and varargs as indices. + gtk_tree_model_get_iter_from_string (my_tree_model, iter, any_path_string); // Sets iter to a valid iterator pointing to path. + // If path does not exist, iter is set to an invalid iterator and FALSE is returned. + + // gpointer *data; gboolean b = (*GtkTreeModelForeachFunc) (my_tree_model, path, iter, data); + // Type of the callback passed to gtk_tree_model_foreach() to iterate over the rows in a tree model. + + gtk_tree_model_get (my_tree_model, iter); // Gets the value of one or more cells in the row referenced by iter + gtk_cell_editable_start_editing (cell_editable, event); // Begins editing on a cell_editable . + gtk_cell_editable_editing_done (cell_editable); // Emits the “editing-done” signal. + gtk_tree_model_row_deleted (my_tree_model, path); // Emits the “row-deleted” signal on 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, parent_iter; // get the iterator from a string + gtk_tree_model_get_iter_from_string (my_tree_model, &iter, "3:2:5"); + + 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); +} + +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; + + // 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); + + // Do something with the data + g_print ("Row %d: (%s,%d)\n", row_count, str_data, int_data); + g_free (str_data); + + valid = gtk_tree_model_iter_next (my_tree_model, &iter); + row_count++; + } +} /* TreeItem structure */ @@ -54,13 +124,6 @@ struct _TreeItem TreeItem *children; }; -/* columns */ -enum -{ - COLUMN_0, - NUM_COLUMNS -}; - /* tree data */ static TreeItem E[] = {NULL}, F[] = {NULL}, G[] = {NULL}, H[] = {NULL}; static TreeItem I[] = {NULL}, K[] = {NULL}, N[] = {NULL}, M[] = {NULL}; @@ -85,7 +148,7 @@ static GtkTreeModel *create_node_recursive (GtkTreeStore *model, 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); + gtk_tree_store_set (model, &iter, STRING_COLUMN, current_item->label, -1); if (current_item->children) create_node_recursive (model, current_item->children, &iter, depth + 1); @@ -112,9 +175,9 @@ do_tree_store (GtkWidget *do_widget) GtkWidget *vbox; GtkWidget *sw; // sw : 'scrolled_window' GtkWidget *treeview; - GtkTreeModel *tree_model; - GtkTreeStore *tree_store = NULL; - + GtkTreeModel *my_tree_model; + 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"); @@ -137,21 +200,22 @@ do_tree_store (GtkWidget *do_widget) gtk_box_append (GTK_BOX (vbox), sw); /* create tree_model */ - tree_model = create_node_recursive (tree_store, O, NULL, 0); + my_tree_model = create_node_recursive (my_tree_store, O, NULL, 0); + reading_data_from_a_GtkTreeModel(my_tree_model); /* create tree view */ - treeview = gtk_tree_view_new_with_model (tree_model); + treeview = gtk_tree_view_new_with_model (my_tree_model); gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (treeview), FALSE); gtk_widget_set_vexpand (treeview, TRUE); gtk_tree_selection_set_mode (gtk_tree_view_get_selection (GTK_TREE_VIEW (treeview)), GTK_SELECTION_MULTIPLE); gtk_tree_view_set_reorderable (GTK_TREE_VIEW (treeview), TRUE); - g_object_unref (tree_model); + g_object_unref (my_tree_model); GtkCellRenderer *renderer; renderer = gtk_cell_renderer_text_new (); g_object_set (renderer, "xalign", 0.0, NULL); gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (treeview), -1, - "Col 0", renderer, "text", COLUMN_0, NULL); + "Col 0", renderer, "text", STRING_COLUMN, NULL); gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (sw), treeview);