Learning_GTK4_tree/demos/gtk-demo/tree_store.c

277 lines
9.6 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) */
/* */
/*************************************************************************************************************************************/
/* 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;
gboolean AA;
gboolean BB;
gboolean CC;
TreeItem *children;
};
/* columns */
enum
{
COLUMN_0,
COLUMN_1,
COLUMN_2,
VISIBLE_COLUMN,
WORLD_COLUMN,
NUM_COLUMNS
};
/* tree data */
static TreeItem item_name_0[] =
{
{ "A", TRUE, TRUE, TRUE, NULL },
{ "B", FALSE, TRUE, FALSE, NULL },
{ "C", FALSE, TRUE, FALSE, NULL },
{ NULL }
};
static TreeItem item_name_1[] =
{
{ "A", FALSE, TRUE, FALSE, NULL },
{ "B", FALSE, FALSE, FALSE, NULL },
{ "C", FALSE, FALSE, TRUE, NULL },
{ "D", FALSE, TRUE, TRUE, NULL },
{ NULL }
};
static TreeItem item_name_2[] =
{
{ "A", FALSE, FALSE, FALSE, NULL },
{ "B", FALSE, FALSE, TRUE, NULL },
{ NULL }
};
static TreeItem toplevel[] =
{
{ "IT-0", FALSE, FALSE, FALSE, item_name_0 },
{ "IT-1", FALSE, FALSE, FALSE, item_name_1 },
{ "IT-2", FALSE, FALSE, FALSE, item_name_2 },
{ NULL }
};
static GtkTreeModel *
create_model (void)
{
GtkTreeStore *model;
GtkTreeIter iter;
TreeItem *item_0 = toplevel;
/* create tree store */
model = gtk_tree_store_new (NUM_COLUMNS,
G_TYPE_STRING,
G_TYPE_BOOLEAN,
G_TYPE_BOOLEAN,
G_TYPE_BOOLEAN,
G_TYPE_BOOLEAN);
/* add data to the tree store */
while (item_0->label)
{
TreeItem *item_1 = item_0->children;
gtk_tree_store_append (model, &iter, NULL);
gtk_tree_store_set (model, &iter,
COLUMN_0, item_0->label,
COLUMN_1, FALSE,
COLUMN_2, FALSE,
VISIBLE_COLUMN, FALSE,
WORLD_COLUMN, FALSE,
-1);
/* add children */
while (item_1->label)
{
GtkTreeIter child_iter;
gtk_tree_store_append (model, &child_iter, &iter);
gtk_tree_store_set (model, &child_iter,
COLUMN_0, item_1->label,
COLUMN_1, item_1->AA,
COLUMN_2, item_1->BB,
VISIBLE_COLUMN, TRUE,
WORLD_COLUMN, item_1->CC,
-1);
item_1++;
}
item_0++;
}
return GTK_TREE_MODEL (model);
}
static void
item_toggled (GtkCellRendererToggle *cell,
char *path_str,
gpointer data)
{
GtkTreeModel *model = (GtkTreeModel *)data;
GtkTreePath *path = gtk_tree_path_new_from_string (path_str);
GtkTreeIter iter;
gboolean toggle_item;
int *column;
column = g_object_get_data (G_OBJECT (cell), "column");
/* get toggled iter */
gtk_tree_model_get_iter (model, &iter, path);
gtk_tree_model_get (model, &iter, column, &toggle_item, -1);
/* do something with the value */
toggle_item ^= 1;
/* set new value */
gtk_tree_store_set (GTK_TREE_STORE (model), &iter, column,
toggle_item, -1);
/* clean up */
gtk_tree_path_free (path);
}
static void
add_columns (GtkTreeView *treeview)
{
int col_offset;
GtkCellRenderer *renderer;
// GtkTreeViewColumn *column;
GtkTreeModel *model = gtk_tree_view_get_model (treeview);
/* column for item_1 names */
renderer = gtk_cell_renderer_text_new ();
g_object_set (renderer, "xalign", 0.0, NULL);
col_offset = gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (treeview), -1, "Col 0", renderer, "text",
COLUMN_0, NULL);
/* column = */ gtk_tree_view_get_column (GTK_TREE_VIEW (treeview), col_offset - 1);
// gtk_tree_view_column_set_clickable (GTK_TREE_VIEW_COLUMN (column), TRUE);
/* AA column */
renderer = gtk_cell_renderer_toggle_new ();
g_object_set (renderer, "xalign", 0.0, NULL);
g_object_set_data (G_OBJECT (renderer), "column", (int *)COLUMN_1);
g_signal_connect (renderer, "toggled", G_CALLBACK (item_toggled), model);
col_offset = gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (treeview), -1, "Col 1", renderer, "active",
COLUMN_1, "visible", VISIBLE_COLUMN, "activatable", WORLD_COLUMN, NULL);
/* column = */ gtk_tree_view_get_column (GTK_TREE_VIEW (treeview), col_offset - 1);
// gtk_tree_view_column_set_sizing (GTK_TREE_VIEW_COLUMN (column), GTK_TREE_VIEW_COLUMN_FIXED);
// gtk_tree_view_column_set_clickable (GTK_TREE_VIEW_COLUMN (column), TRUE);
/* BB column */
renderer = gtk_cell_renderer_toggle_new ();
g_object_set (renderer, "xalign", 0.0, NULL);
g_object_set_data (G_OBJECT (renderer), "column", (int *)COLUMN_2);
g_signal_connect (renderer, "toggled", G_CALLBACK (item_toggled), model);
col_offset = gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (treeview), -1, "Col 2", renderer, "active",
COLUMN_2, "visible", VISIBLE_COLUMN, NULL);
/* column = */ gtk_tree_view_get_column (GTK_TREE_VIEW (treeview), col_offset - 1);
// gtk_tree_view_column_set_sizing (GTK_TREE_VIEW_COLUMN (column), GTK_TREE_VIEW_COLUMN_FIXED);
// gtk_tree_view_column_set_clickable (GTK_TREE_VIEW_COLUMN (column), TRUE);
}
GtkWidget *
do_tree_store (GtkWidget *do_widget)
{
static GtkWidget *window = NULL;
if (!window)
{
GtkWidget *vbox;
GtkWidget *sw;
GtkWidget *treeview;
GtkTreeModel *model;
/* create window, etc */
window = gtk_window_new ();
//gtk_window_set_display (GTK_WINDOW (window),
// gtk_widget_get_display (do_widget));
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 model */
model = create_model ();
/* create tree view */
treeview = gtk_tree_view_new_with_model (model);
gtk_widget_set_vexpand (treeview, TRUE);
g_object_unref (model);
gtk_tree_selection_set_mode (gtk_tree_view_get_selection (GTK_TREE_VIEW (treeview)),
GTK_SELECTION_MULTIPLE);
add_columns (GTK_TREE_VIEW (treeview));
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, 350);
}
if (!gtk_widget_get_visible (window))
gtk_widget_set_visible (window, TRUE);
else
gtk_window_destroy (GTK_WINDOW (window));
return window;
}