2023-12-12 19:49:20 +01:00
|
|
|
/************************************************** R E M E M B E R ! *****************************************************/
|
|
|
|
/* */
|
|
|
|
/* jean@Project:~/Gem-Graph/gem-graph-client/tree (learning)/The_Gnome_way/gtk$ guix shell -m manifest.scm --check */
|
|
|
|
/* guix shell: vérification des variables d'environnement visibles depuis le shell « /gnu/store/ --- (a commit) --- /bin/bash »… */
|
|
|
|
/* guix shell: Tout va bien ! Le shell a les bonnes variables d'environnement. */
|
|
|
|
/* jean@Project:~/Gem-Graph/gem-graph-client/tree (learning)/The_Gnome_way/gtk [env] $ cd builddir/ */
|
|
|
|
/* jean@Project:~/Gem-Graph/gem-graph-client/tree (learning)/The_Gnome_way/gtk/builddir [env] $ */
|
|
|
|
/* jean@Project:~/Gem-Graph/gem-graph-client/tree...way/gtk/builddir [env] $ clear && meson compile && demos/gtk-demo/gtk4-demo */
|
|
|
|
/* */
|
|
|
|
/* -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- A Session -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- */
|
|
|
|
/* */
|
|
|
|
/* jean@Project:~/Gem-Graph/gem-graph-client/tree (learning)/The_Gnome_way/gtk/builddir [env] $ ctrl D (avant de fermer builder) */
|
|
|
|
/* */
|
|
|
|
/*************************************************************************************************************************************/
|
|
|
|
|
2023-12-16 08:30:28 +01:00
|
|
|
|
2023-12-15 10:12:13 +01:00
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
|
|
|
|
|
2023-12-16 08:30:28 +01:00
|
|
|
|
2023-12-15 10:12:13 +01:00
|
|
|
/************************************************** <> *****************************************************/
|
|
|
|
/* */
|
|
|
|
/* https://developer-old.gnome.org/gtk4/stable/GtkTreeView.html */
|
|
|
|
/* GtkTreeViewColumn, GtkTreeSelection, GtkTreeModel, GtkTreeView drag-and-drop, GtkTreeSortable, GtkTreeModelSort, GtkListStore, */
|
|
|
|
/* GtkTreeStore, GtkCellRenderer, GtkCellEditable, GtkCellRendererPixbuf, GtkCellRendererText, GtkCellRendererToggle */
|
|
|
|
/* */
|
2023-12-18 08:49:36 +01:00
|
|
|
/* https://developer-old.gnome.org/gtk4/stable/GtkTreeView.html#gtk-tree-view-get-path-at-pos */
|
|
|
|
/* Finds the path at the point (x , y ), relative to bin_window coordinates. */
|
|
|
|
/* Widget-relative coordinates must be converted using gtk_tree_view_convert_widget_to_bin_window_coords(). */
|
|
|
|
/* */
|
2023-12-15 10:12:13 +01:00
|
|
|
/* https://www.gnu.org/software/guile-gnome/docs/gtk/html/GtkCellRenderer.html */
|
|
|
|
/* */
|
|
|
|
/*************************************************************************************************************************************/
|
2023-12-12 19:49:20 +01:00
|
|
|
|
|
|
|
|
2023-12-16 18:19:54 +01:00
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
|
|
|
|
|
|
|
|
|
|
|
|
/******************************** S O M E L I T T L E T H I N G S T O K N O W **********************************/
|
|
|
|
/* */
|
|
|
|
/* gtk_tree_view_set_show_expanders (GTK_TREE_VIEW (treeview), TRUE); */
|
|
|
|
/* gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (treeview), TRUE); */
|
|
|
|
/* gtk_widget_set_vexpand (treeview, TRUE); */
|
|
|
|
/* gtk_tree_view_set_reorderable (GTK_TREE_VIEW (treeview), TRUE); */
|
|
|
|
/* */
|
|
|
|
/*************************************************************************************************************************************/
|
|
|
|
|
|
|
|
|
2023-12-13 09:30:00 +01:00
|
|
|
/* The GtkTreeStore is used to store data in tree form, to be used later on by a GtkTreeView to display it.
|
|
|
|
* This demo builds a simple GtkTreeStore and displays it.
|
|
|
|
* If you're new to the GtkTreeView widgets and associates, look into the GtkListStore example first. */
|
|
|
|
|
2023-12-12 11:36:42 +01:00
|
|
|
|
|
|
|
#include <gtk/gtk.h>
|
2023-12-12 18:40:36 +01:00
|
|
|
#include <stdio.h>
|
2023-12-12 11:36:42 +01:00
|
|
|
|
|
|
|
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
|
|
|
|
|
2023-12-12 12:47:28 +01:00
|
|
|
|
2023-12-12 11:36:42 +01:00
|
|
|
/* TreeItem structure */
|
|
|
|
typedef struct _TreeItem TreeItem;
|
|
|
|
struct _TreeItem
|
|
|
|
{
|
|
|
|
const char *label;
|
|
|
|
TreeItem *children;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* columns */
|
|
|
|
enum
|
|
|
|
{
|
2023-12-13 11:53:11 +01:00
|
|
|
COLUMN_0,
|
2023-12-12 11:36:42 +01:00
|
|
|
NUM_COLUMNS
|
|
|
|
};
|
|
|
|
|
|
|
|
/* tree data */
|
2023-12-18 08:49:36 +01:00
|
|
|
static TreeItem N[] = { NULL };
|
|
|
|
static TreeItem M[] = { NULL };
|
|
|
|
static TreeItem L[] = {{ "M", M }, { "N", N }, { NULL }};
|
2023-12-15 23:11:47 +01:00
|
|
|
static TreeItem K[] = { NULL };
|
2023-12-18 08:49:36 +01:00
|
|
|
static TreeItem J[] = {{ "L", L }, { NULL }};
|
2023-12-15 23:11:47 +01:00
|
|
|
static TreeItem I[] = { NULL };
|
|
|
|
static TreeItem H[] = { NULL };
|
|
|
|
static TreeItem G[] = { NULL };
|
|
|
|
static TreeItem F[] = { NULL };
|
|
|
|
static TreeItem E[] = { NULL };
|
|
|
|
static TreeItem D[] = {{ "I", I }, { "J", J }, { "K", K }, { NULL }};
|
|
|
|
static TreeItem C[] = {{ "H", H }, { NULL }};
|
|
|
|
static TreeItem B[] = {{ "E", E }, { "F", F }, { "G", G }, { NULL }};
|
2023-12-15 21:49:58 +01:00
|
|
|
static TreeItem A[] = {{ "C", C }, { "D", D }, { NULL }};
|
2023-12-18 08:49:36 +01:00
|
|
|
static TreeItem R[] = {{ "A", A }, { "B", B }, { NULL }}; // ROOT
|
2023-12-16 13:16:41 +01:00
|
|
|
static TreeItem O[] = {{ "R", R }, { NULL }}; // Artefact added for symmetry
|
2023-12-12 11:36:42 +01:00
|
|
|
|
2023-12-18 16:31:04 +01:00
|
|
|
static GtkTreeModel *create_node_recursive (GtkTreeStore *model,
|
|
|
|
TreeItem *current_item,
|
|
|
|
GtkTreeIter *iter_parent,
|
|
|
|
int depth)
|
2023-12-18 09:38:59 +01:00
|
|
|
{
|
2023-12-18 16:31:04 +01:00
|
|
|
GtkTreeIter iter;
|
2023-12-18 09:38:59 +01:00
|
|
|
|
2023-12-18 16:31:04 +01:00
|
|
|
if (model == NULL)
|
|
|
|
model = gtk_tree_store_new (NUM_COLUMNS, G_TYPE_STRING);
|
2023-12-18 09:38:59 +01:00
|
|
|
|
2023-12-18 16:31:04 +01:00
|
|
|
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++;
|
2023-12-18 09:38:59 +01:00
|
|
|
}
|
2023-12-18 16:31:04 +01:00
|
|
|
|
|
|
|
if (depth == 0)
|
|
|
|
return GTK_TREE_MODEL(model);
|
|
|
|
else
|
|
|
|
return NULL;
|
2023-12-18 09:38:59 +01:00
|
|
|
}
|
|
|
|
|
2023-12-18 15:46:50 +01:00
|
|
|
static void create_node (GtkTreeStore *model,
|
|
|
|
GtkTreeIter *iter_children,
|
|
|
|
TreeItem *item_children,
|
|
|
|
GtkTreeIter *iter_parent)
|
|
|
|
{
|
|
|
|
gtk_tree_store_append (model, iter_children, iter_parent);
|
|
|
|
gtk_tree_store_set (model, iter_children, COLUMN_0, item_children->label, -1);
|
|
|
|
}
|
|
|
|
|
2023-12-12 11:36:42 +01:00
|
|
|
static GtkTreeModel *
|
|
|
|
create_model (void)
|
|
|
|
{
|
2023-12-18 08:49:36 +01:00
|
|
|
TreeItem *item_0, *item_1, *item_2, *item_3, *item_4;
|
|
|
|
GtkTreeIter iter_0, iter_1, iter_2, iter_3, iter_4;
|
2023-12-16 13:11:50 +01:00
|
|
|
|
2023-12-16 08:30:28 +01:00
|
|
|
GtkTreeStore *model = gtk_tree_store_new (NUM_COLUMNS, G_TYPE_STRING);
|
2023-12-12 11:36:42 +01:00
|
|
|
|
2023-12-16 13:11:50 +01:00
|
|
|
if (O->children) {
|
|
|
|
item_0 = O->children;
|
|
|
|
|
|
|
|
while (item_0->label) {
|
2023-12-18 08:49:36 +01:00
|
|
|
create_node(model, &iter_0, item_0, NULL);
|
2023-12-15 22:52:41 +01:00
|
|
|
|
2023-12-16 13:11:50 +01:00
|
|
|
if (item_0->children) {
|
|
|
|
item_1 = item_0->children;
|
|
|
|
|
|
|
|
while (item_1->label) {
|
2023-12-18 08:49:36 +01:00
|
|
|
create_node(model, &iter_1, item_1, &iter_0);
|
2023-12-16 13:11:50 +01:00
|
|
|
|
|
|
|
if (item_1->children) {
|
|
|
|
item_2 = item_1->children;
|
|
|
|
|
|
|
|
while (item_2->label) {
|
2023-12-18 08:49:36 +01:00
|
|
|
create_node(model, &iter_2, item_2, &iter_1);
|
|
|
|
|
|
|
|
if (item_2->children) {
|
|
|
|
item_3 = item_2->children;
|
|
|
|
|
|
|
|
while (item_3->label) {
|
|
|
|
create_node(model, &iter_3, item_3, &iter_2);
|
|
|
|
|
|
|
|
if (item_3->children) {
|
|
|
|
item_4 = item_3->children;
|
|
|
|
|
|
|
|
while (item_4->label) {
|
|
|
|
create_node(model, &iter_4, item_4, &iter_3);
|
|
|
|
item_4++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
item_3++;
|
|
|
|
}
|
|
|
|
}
|
2023-12-16 08:45:54 +01:00
|
|
|
item_2++;
|
2023-12-16 13:11:50 +01:00
|
|
|
}
|
2023-12-15 22:52:41 +01:00
|
|
|
}
|
2023-12-13 11:53:11 +01:00
|
|
|
item_1++;
|
2023-12-12 11:36:42 +01:00
|
|
|
}
|
2023-12-16 13:11:50 +01:00
|
|
|
}
|
2023-12-13 11:53:11 +01:00
|
|
|
item_0++;
|
2023-12-12 11:36:42 +01:00
|
|
|
}
|
2023-12-16 13:11:50 +01:00
|
|
|
}
|
2023-12-12 11:36:42 +01:00
|
|
|
|
|
|
|
return GTK_TREE_MODEL (model);
|
|
|
|
}
|
|
|
|
|
|
|
|
GtkWidget *
|
|
|
|
do_tree_store (GtkWidget *do_widget)
|
|
|
|
{
|
|
|
|
static GtkWidget *window = NULL;
|
|
|
|
|
|
|
|
if (!window)
|
|
|
|
{
|
|
|
|
GtkWidget *vbox;
|
|
|
|
GtkWidget *sw;
|
|
|
|
GtkWidget *treeview;
|
2023-12-18 10:17:55 +01:00
|
|
|
GtkTreeModel *tree_model;
|
2023-12-18 15:46:50 +01:00
|
|
|
GtkTreeStore *tree_store;
|
2023-12-12 11:36:42 +01:00
|
|
|
|
|
|
|
/* create window, etc */
|
|
|
|
window = gtk_window_new ();
|
2023-12-12 18:40:36 +01:00
|
|
|
//gtk_window_set_display (GTK_WINDOW (window),
|
|
|
|
// gtk_widget_get_display (do_widget));
|
2023-12-12 11:36:42 +01:00
|
|
|
gtk_window_set_title (GTK_WINDOW (window), "Tree Store");
|
|
|
|
g_object_add_weak_pointer (G_OBJECT (window), (gpointer *)&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);
|
|
|
|
|
2023-12-15 10:12:13 +01:00
|
|
|
gtk_box_append (GTK_BOX (vbox), gtk_label_new ("Learning GTK trees"));
|
2023-12-12 11:36:42 +01:00
|
|
|
|
|
|
|
sw = gtk_scrolled_window_new ();
|
|
|
|
gtk_scrolled_window_set_has_frame (GTK_SCROLLED_WINDOW (sw), TRUE);
|
|
|
|
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
|
|
|
|
GTK_POLICY_AUTOMATIC,
|
|
|
|
GTK_POLICY_AUTOMATIC);
|
|
|
|
gtk_box_append (GTK_BOX (vbox), sw);
|
|
|
|
|
2023-12-18 10:17:55 +01:00
|
|
|
/* create tree_model */
|
2023-12-18 16:31:04 +01:00
|
|
|
tree_model = create_node_recursive (tree_store, R, NULL, 0);
|
2023-12-12 11:36:42 +01:00
|
|
|
|
|
|
|
/* create tree view */
|
2023-12-18 15:46:50 +01:00
|
|
|
treeview = gtk_tree_view_new_with_model (tree_model);
|
2023-12-12 11:36:42 +01:00
|
|
|
gtk_widget_set_vexpand (treeview, TRUE);
|
2023-12-18 08:49:36 +01:00
|
|
|
gtk_tree_view_set_reorderable (GTK_TREE_VIEW (treeview), TRUE);
|
2023-12-18 15:46:50 +01:00
|
|
|
g_object_unref (tree_model);
|
2023-12-12 11:36:42 +01:00
|
|
|
gtk_tree_selection_set_mode (gtk_tree_view_get_selection (GTK_TREE_VIEW (treeview)),
|
|
|
|
GTK_SELECTION_MULTIPLE);
|
|
|
|
|
2023-12-18 08:49:36 +01:00
|
|
|
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);
|
2023-12-12 11:36:42 +01:00
|
|
|
|
|
|
|
gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (sw), treeview);
|
|
|
|
|
|
|
|
/* expand all rows after the treeview widget has been realized */
|
|
|
|
g_signal_connect (treeview, "realize",
|
|
|
|
G_CALLBACK (gtk_tree_view_expand_all), NULL);
|
2023-12-18 08:49:36 +01:00
|
|
|
gtk_window_set_default_size (GTK_WINDOW (window), 200, 380);
|
2023-12-12 11:36:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!gtk_widget_get_visible (window))
|
|
|
|
gtk_widget_set_visible (window, TRUE);
|
|
|
|
else
|
|
|
|
gtk_window_destroy (GTK_WINDOW (window));
|
2023-12-12 18:40:36 +01:00
|
|
|
|
2023-12-12 11:36:42 +01:00
|
|
|
return window;
|
|
|
|
}
|