gem-graph-client/tree.c

253 lines
8.2 KiB
C
Raw Normal View History

2024-05-15 06:19:48 +02:00
#include <stdio.h>
#include <gtk-4.0/gtk/gtk.h>
#include "hot.h"
2024-05-15 06:19:48 +02:00
#include "warm.h"
#include "cold.h"
#include "display.h"
#include "contain.h"
#include "texts.h"
// https://docs.gtk.org/gtk4/visual_index.html < widgets gallery
// https://docs.gtk.org/gtk4/section-text-widget.html
// https://docs.gtk.org/gtk4/class.Widget.html#height-for-width-geometry-management
// GTK_ORIENTATION_VERTICAL GTK_ORIENTATION_HORIZONTAL
// Simplified TreeNode structure for demonstration purposes
// --------------------------------------------------------
/*
* Tree-related signals
*/
void on_tree_expander_toggled(GtkExpander *expander, gpointer user_data)
{
// This is a conceptual callback for when an expander is toggled
GtkTreeListRow *row = GTK_TREE_LIST_ROW(user_data);
gboolean is_expanded = gtk_tree_list_row_get_expanded(row);
gtk_tree_list_row_set_expanded(row, !is_expanded);
}
void on_tree_setup_factory (GtkSignalListItemFactory *factory,
GObject* object,
gpointer user_data)
{
GtkWidget* expander = gtk_expander_new (NULL);
gtk_list_item_set_child (GTK_LIST_ITEM(object), expander);
printf("[on_tree_setup_factory] here is an expander\n");
}
void on_tree_bind_factory (GtkSignalListItemFactory *factory,
GObject* object,
gpointer user_data)
{
// GObject *item;
const gchar *text;
GtkTreeListRow *row;
GtkListItem *list_item;
GtkWidget *expander;
list_item= GTK_LIST_ITEM(object);
row = gtk_list_item_get_item(list_item);
if (row != NULL) {
text = gtk_string_object_get_string(GTK_STRING_OBJECT(gtk_tree_list_row_get_item(row)));
expander = gtk_list_item_get_child(list_item);
gtk_expander_set_label(GTK_EXPANDER(expander), text);
// Disconnect previous signal handlers to avoid stacking them
g_signal_handlers_disconnect_by_func(expander, G_CALLBACK(on_tree_expander_toggled), row);
// Connect the signal handler
g_signal_connect(expander, "activate", G_CALLBACK(on_tree_expander_toggled), row);
gtk_widget_set_margin_start(expander, gtk_tree_list_row_get_depth(row)*20);
gboolean is_expanded = gtk_tree_list_row_get_expanded(row);
printf("[on_tree_bind_factory] here is %s content and expander is %d\n",
text,
is_expanded);
} else {
printf("[on_tree_bind_factory] here is NON content\n");
}
}
void on_tree_selection_changed (GtkSelectionModel* self, guint position,
guint n_items, gpointer user_data)
{
printf("[on_tree_selection_changed]\n");
}
struct TreeNode_t
{
gchar *text;
struct TreeNode_t *child;
struct TreeNode_t *next;
};
// Function to create a new TreeNode instance
static struct TreeNode_t *create_tree_node (const gchar* text)
{
struct TreeNode_t *node = g_malloc0 (sizeof(struct TreeNode_t));
node->text = g_strdup(text);
node->child = NULL;
return node;
}
// Function to add a child node to a parent node
static void add_child_node (struct TreeNode_t *parent, struct TreeNode_t *child)
{
struct TreeNode_t *cur;
if (parent->child) {
cur = parent->child;
while (cur && cur->next) {
cur = cur->next;
}
cur->next = child;
} else {
parent->child = child;
}
}
// Recursive function to free a TreeNode and its children
// attention: « free_tree_node » défini mais pas utilisé <<< TODO
static void free_tree_node (struct TreeNode_t *node)
{
struct TreeNode_t *cur;
struct TreeNode_t *tmp;
if (!node) return;
// free siblings
cur = node;
while (cur) {
tmp = cur->next;
g_free(cur);
cur = tmp;
}
// recursive free
free_tree_node(node->child);
g_free(node->text);
g_free(node);
}
// Function to simulate getting a GListModel of children for a given TreeNode
GListModel* ui_tree_get_children_model (struct TreeNode_t *parent)
{
struct TreeNode_t *child;
GtkStringList *list = NULL;
if (parent) {
printf("[ui_tree_get_children_model] here is %s content : ", parent->text);
child = parent->child;
if (child) {
list = gtk_string_list_new(NULL);
}
while(child) {
gtk_string_list_append(list, child->text);
printf("%s ", child->text);
child = child->next;
}
}
printf("\n");
return G_LIST_MODEL(list);
}
// GtkTreeListModelCreateModelFunc callback implementation
GListModel* ui_tree_create_model_func(GObject *item, gpointer root)
{
struct TreeNode_t *cur = (struct TreeNode_t *)root;
struct TreeNode_t *parent = NULL;
const gchar *string = gtk_string_object_get_string(GTK_STRING_OBJECT(item));
parent = root;
while (cur) {
if (strcmp(string, cur->text) == 0) {
break;
}
cur = cur->next;
if (cur == NULL) {
cur = parent->child;
parent = cur;
}
}
printf("[ui_tree_create_model_func] looked for %s in %s item\n",
cur->text,
string);
return ui_tree_get_children_model(cur);
}
// Application activation callback
void ui_create_tree (GtkWidget *target_widget)
2024-05-15 06:19:48 +02:00
{
GtkStringList *model;
GtkTreeListModel *tree_model;
GtkSignalListItemFactory *factory;
GtkSingleSelection *selection_model;
GtkWidget *list_view;
GtkScrolledWindow *scrolled_window;
assert(target_widget);
struct TreeNode_t *tree_root = create_tree_node("Root"); // AD HOC XXX & no free()
struct TreeNode_t *ATP = create_tree_node("ATP"); add_child_node(tree_root, ATP);
struct TreeNode_t *ADP = create_tree_node("ADP"); add_child_node(tree_root, ADP);
struct TreeNode_t *AMP = create_tree_node("AMP"); add_child_node(tree_root, AMP);
struct TreeNode_t *adenine = create_tree_node("adénine"); add_child_node(ATP, adenine);
struct TreeNode_t *ribose = create_tree_node("ribose"); add_child_node(adenine, ribose);
struct TreeNode_t *PO4H3_1 = create_tree_node("phosphate 1"); add_child_node(ribose, PO4H3_1);
struct TreeNode_t *PO4H3_2 = create_tree_node("phosphate 2"); add_child_node(PO4H3_1, PO4H3_2);
struct TreeNode_t *PO4H3_3 = create_tree_node("phosphate 3"); add_child_node(PO4H3_2, PO4H3_3);
2024-05-15 06:19:48 +02:00
model = gtk_string_list_new(NULL);
gtk_string_list_append(model, tree_root->text);
// Create and setup the list view and item factory
factory = GTK_SIGNAL_LIST_ITEM_FACTORY (gtk_signal_list_item_factory_new ());
g_signal_connect (factory, "setup", G_CALLBACK(on_tree_setup_factory), NULL);
g_signal_connect (factory, "bind", G_CALLBACK(on_tree_bind_factory), NULL);
// Create a GtkTreeListModel
tree_model = gtk_tree_list_model_new(
G_LIST_MODEL(model),
FALSE, // Passthrough - False in actual usage with dynamic children retrieval
FALSE, // autoexpand
(GtkTreeListModelCreateModelFunc)ui_tree_create_model_func,
tree_root,
NULL //(GDestroyNotify)free_tree_node
);
selection_model = gtk_single_selection_new (
G_LIST_MODEL (tree_model));
gtk_single_selection_set_autoselect (selection_model, FALSE);
gtk_single_selection_set_can_unselect (selection_model, TRUE);
/* g_signal_connect (selection_model, "selection-changed", */
/* G_CALLBACK(on_tree_selection_changed), NULL); */
list_view = gtk_list_view_new (GTK_SELECTION_MODEL (selection_model),
GTK_LIST_ITEM_FACTORY (factory));
scrolled_window = GTK_SCROLLED_WINDOW (gtk_scrolled_window_new());
gtk_scrolled_window_set_child(scrolled_window, list_view);
gtk_scrolled_window_set_policy (scrolled_window,
GTK_POLICY_AUTOMATIC,
GTK_POLICY_AUTOMATIC);
gtk_widget_set_vexpand(GTK_WIDGET(scrolled_window), TRUE);
gtk_box_append(GTK_BOX(target_widget), GTK_WIDGET(scrolled_window));
gtk_widget_set_visible(GTK_WIDGET(scrolled_window), TRUE);
gtk_widget_set_visible(GTK_WIDGET(list_view), TRUE);
}