Learning_GTK4_tree/demos/gtk-demo/tree_store.c

179 lines
9.3 KiB
C

/************************************************** 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) */
/* */
/*************************************************************************************************************************************/
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
/************************************************** <> *****************************************************/
/* */
/* 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 */
/* */
/* 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(). */
/* */
/* https://www.gnu.org/software/guile-gnome/docs/gtk/html/GtkCellRenderer.html */
/* */
/*************************************************************************************************************************************/
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
/******************************** 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); */
/* */
/*************************************************************************************************************************************/
/* 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. */
#include <gtk/gtk.h>
#include <stdio.h>
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
/* TreeItem structure */
typedef struct _TreeItem TreeItem;
struct _TreeItem
{
const char *label;
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};
static TreeItem L[] = {{"M", M}, {"N", N}, {NULL }}, J[] = {{"L", L}, {NULL}};
static TreeItem D[] = {{"I", I}, {"J", J}, {"K", K}, {NULL}};
static TreeItem C[] = {{"F", F}, {"G", G}, {"H", H}, {NULL}};
static TreeItem A[] = {{"D", D}, {NULL}}, B[] = {{"E", E}, {NULL}};
static TreeItem R[] = {{"A", A}, {"B", B}, {"C", C}, {NULL}};
static TreeItem O[] = {{"ROOT", R}, {NULL}}; // Artefact added for symmetry
static GtkTreeModel *create_node_recursive (GtkTreeStore *model,
TreeItem *current_item,
GtkTreeIter *iter_parent,
int depth)
{
GtkTreeIter iter;
if (model == NULL)
model = gtk_tree_store_new (NUM_COLUMNS, G_TYPE_STRING);
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++;
}
if (depth == 0)
return GTK_TREE_MODEL(model);
else
return NULL;
}
GtkWidget *
do_tree_store (GtkWidget *do_widget)
{
static GtkWidget *window = NULL;
if (!window)
{
GtkWidget *vbox;
GtkWidget *sw;
GtkWidget *treeview;
GtkTreeModel *tree_model;
GtkTreeStore *tree_store = NULL;
/* 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);
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_box_append (GTK_BOX (vbox), gtk_label_new ("Learning GTK trees"));
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);
/* create tree_model */
tree_model = create_node_recursive (tree_store, O, NULL, 0);
/* create tree view */
treeview = gtk_tree_view_new_with_model (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);
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);
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);
gtk_window_set_default_size (GTK_WINDOW (window), 200, 400);
}
if (!gtk_widget_get_visible (window))
gtk_widget_set_visible (window, TRUE);
else
gtk_window_destroy (GTK_WINDOW (window));
return window;
}