Jean Sirmai 2023-12-20 18:52:19 +01:00
parent 57bb001d11
commit 7a0abbc5b3
Signed by: jean
GPG Key ID: FB3115C340E057E3
1 changed files with 36 additions and 28 deletions

View File

@ -49,6 +49,12 @@ enum
NUM_COLUMNS 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 */ 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, 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 // 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) 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"); 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 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_model_get_iter (my_tree_model, &iter, my_tree_path);
gtk_tree_path_free (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 /* 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, 2); */
parent_iter = iter; gtk_tree_model_iter_nth_child (my_tree_model, &iter, &parent_iter, 5); /* 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) 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. 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; int row_count = 0;
while (valid) char *my_str_data;
{
char *str_data;
int int_data;
// Make sure you terminate calls to gtk_tree_model_get() with a “-1” value gboolean valid = gtk_tree_model_get_iter_first (my_tree_model, &iter);
gtk_tree_model_get (my_tree_model, &iter, STRING_COLUMN, &str_data, -1);
// Do something with the data while (valid) { // Make sure you terminate calls to gtk_tree_model_get() with a “-1” value
g_print ("Row %d: (%s,%d)\n", row_count, str_data, int_data); gtk_tree_model_get (my_tree_model, &iter, STRING_COLUMN, &my_str_data, -1);
g_free (str_data); 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); g_free (my_str_data); // ? g_free (iter);
row_count++;
}
} }
@ -168,9 +171,9 @@ static GtkTreeModel *create_node_recursive (GtkTreeStore *model,
GtkWidget * GtkWidget *
do_tree_store (GtkWidget *do_widget) do_tree_store (GtkWidget *do_widget)
{ {
static GtkWidget *window = NULL; static GtkWidget *my_window = NULL;
if (!window) if (! my_window)
{ {
GtkWidget *vbox; GtkWidget *vbox;
GtkWidget *sw; // sw : 'scrolled_window' 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, 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. // to be used later on by a GtkTreeView to display it.
/* create window, etc */ /* create window, etc */
window = gtk_window_new (); my_window = gtk_window_new ();
gtk_window_set_title (GTK_WINDOW (window), "Tree Store"); gtk_window_set_title (GTK_WINDOW (my_window), "Tree Store");
g_object_add_weak_pointer (G_OBJECT (window), (gpointer *)&window); g_object_add_weak_pointer (G_OBJECT (my_window), (gpointer *)&my_window);
vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 8); vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 8);
gtk_widget_set_margin_start (vbox, 8); gtk_widget_set_margin_start (vbox, 8);
gtk_widget_set_margin_end (vbox, 8); gtk_widget_set_margin_end (vbox, 8);
gtk_widget_set_margin_top (vbox, 8); gtk_widget_set_margin_top (vbox, 8);
gtk_widget_set_margin_bottom (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")); 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 */ /* create tree_model */
my_tree_model = create_node_recursive (my_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); 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 */ /* create tree view */
treeview = gtk_tree_view_new_with_model (my_tree_model); 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_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_widget_set_vexpand (treeview, TRUE);
gtk_tree_selection_set_mode (gtk_tree_view_get_selection (GTK_TREE_VIEW (treeview)), gtk_tree_selection_set_mode (gtk_tree_view_get_selection (GTK_TREE_VIEW (treeview)),
GTK_SELECTION_MULTIPLE); GTK_SELECTION_MULTIPLE);
@ -222,13 +230,13 @@ do_tree_store (GtkWidget *do_widget)
/* expand all rows after the treeview widget has been realized */ /* expand all rows after the treeview widget has been realized */
g_signal_connect (treeview, "realize", g_signal_connect (treeview, "realize",
G_CALLBACK (gtk_tree_view_expand_all), NULL); 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)) if (!gtk_widget_get_visible (my_window))
gtk_widget_set_visible (window, TRUE); gtk_widget_set_visible (my_window, TRUE);
else else
gtk_window_destroy (GTK_WINDOW (window)); gtk_window_destroy (GTK_WINDOW (my_window));
return window; return my_window;
} }