tests_gtk4/main.c

245 lines
7.6 KiB
C

#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <glib-2.0/glib.h>
#include <gtk-4.0/gtk/gtk.h>
#define verb 0
// Simplified TreeNode structure for demonstration purposes
struct TreeNode_t
{
gchar *text;
struct TreeNode_t *child;
struct TreeNode_t *next;
};
struct TreeNode_t *root = NULL;
// Function to create a new TreeNode instance
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
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;
}
}
static void insert_data (struct TreeNode_t *root)
{
struct TreeNode_t *A = create_tree_node("A"); add_child_node(root, A);
struct TreeNode_t *B = create_tree_node("B"); add_child_node(A, B);
struct TreeNode_t *C = create_tree_node("C"); add_child_node(B, C);
struct TreeNode_t *D = create_tree_node("D"); add_child_node(C, D);
struct TreeNode_t *E = create_tree_node("E"); add_child_node(D, E);
struct TreeNode_t *F = create_tree_node("F"); add_child_node(E, E);
struct TreeNode_t *G = create_tree_node("G"); add_child_node(E, A);
}
// Recursive function to free a TreeNode and its children
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* get_children_model (struct TreeNode_t *parent)
{
struct TreeNode_t *child;
GtkStringList *list = NULL;
if (parent) {
if (verb) printf("[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);
if (verb) printf("%s ", child->text);
child = child->next;
}
}
if (verb) printf("\n");
return G_LIST_MODEL(list);
}
// GtkTreeListModelCreateModelFunc callback implementation
GListModel* create_model_func(GObject *item, gpointer user_data)
{
struct TreeNode_t *cur = root;
struct TreeNode_t *parent = NULL;
struct TreeNode_t *res = NULL;
const char* string = gtk_string_object_get_string(GTK_STRING_OBJECT(item));
parent = root;
while (cur) {
if (! strcmp(string, cur->text)) {
res = cur;
break;
}
cur = cur->next;
if (cur == NULL) {
cur = parent->child;
parent = cur;
}
}
if (verb) printf("[create_model_func] here is %s item\n", string);
return get_children_model(cur);
}
void on_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);
// Assuming you have a way to update your model's is_expanded property
// Update your model here based on the new state
}
void on_setup_factory (GtkListItemFactory *factory, GtkListItem *list_item, gpointer user_data)
{
GtkWidget* expander = gtk_expander_new (NULL);
gtk_list_item_set_child (list_item, expander);
if (verb) printf("[on_setup_factory] here is an expander\n");
}
void on_bind_factory (GtkListItemFactory *factory, GtkListItem *list_item, gpointer user_data)
{
GObject *item;
const gchar *text;
GtkTreeListRow *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)));
GtkWidget *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_expander_toggled), row);
// Connect the signal handler
g_signal_connect(expander, "activate", G_CALLBACK(on_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);
//gtk_tree_list_row_set_expanded(row, !is_expanded);
} else {
if (verb) printf("[on_bind_factory] here is NON %s content\n", text);
}
}
void on_selection_changed (GtkSelectionModel* self, guint position,
guint n_items, gpointer user_data)
{
if (verb) printf("Cc\n");
}
// Application activation callback
void app_activate (GApplication *app, gpointer user_data)
{
GtkWidget *window = gtk_application_window_new(GTK_APPLICATION(app));
gtk_window_set_title(GTK_WINDOW(window), "GTK4 Tree Example");
gtk_window_set_default_size(GTK_WINDOW(window), 400, 300);
GtkStringList *model = gtk_string_list_new(NULL);
gtk_string_list_append(model, root->text);
// Create and setup the list view and item factory
GtkListItemFactory *factory = gtk_signal_list_item_factory_new ();
g_signal_connect (factory, "setup", G_CALLBACK(on_setup_factory), NULL);
g_signal_connect (factory, "bind", G_CALLBACK(on_bind_factory), NULL);
// Create a GtkTreeListModel
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)create_model_func,
NULL,
(GDestroyNotify)g_object_unref //(GDestroyNotify)free_tree_node
);
GtkSingleSelection *selection_model = gtk_single_selection_new ( // GtkSelectionModel
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_selection_changed), NULL);
GtkWidget *list_view = gtk_list_view_new (GTK_SELECTION_MODEL(selection_model), factory);
gtk_window_set_child(GTK_WINDOW(window), list_view);
gtk_widget_set_visible(window, TRUE);
}
int main (int argc, char **argv)
{
// Create a simple tree structure
root = create_tree_node("Root");
insert_data(root);
GtkApplication *app = gtk_application_new("org.gtk.example", G_APPLICATION_FLAGS_NONE);
g_signal_connect(app, "activate", G_CALLBACK(app_activate), NULL);
int status = g_application_run(G_APPLICATION(app), argc, argv);
g_object_unref(app);
free_tree_node(root); // Ensure to free the tree structure after the app has finished
return status;
}
//----------------------------------------------------------------------------------------
// guix shell -m manifest.scm && pkg-config --libs gtk4
// make clean && clear && make && ./gtk_treeview_example