tests_gtk4/main.c

229 lines
6.8 KiB
C
Raw Normal View History

2024-02-11 17:05:35 +01:00
#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>
// 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;
}
}
// 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;
2024-02-12 12:52:38 +01:00
GtkStringList *list = NULL;
2024-02-11 17:05:35 +01:00
if (parent) {
2024-02-12 11:40:46 +01:00
printf("[get_children_model] here is %s content : ", parent->text);
2024-02-11 17:05:35 +01:00
child = parent->child;
2024-02-12 12:52:38 +01:00
if (child) {
list = gtk_string_list_new(NULL);
}
2024-02-11 17:05:35 +01:00
while(child) {
gtk_string_list_append(list, child->text);
2024-02-12 11:40:46 +01:00
printf("%s ", child->text);
2024-02-11 17:05:35 +01:00
child = child->next;
}
}
2024-02-12 11:40:46 +01:00
printf("\n");
2024-02-11 17:05:35 +01:00
return G_LIST_MODEL(list);
}
// GtkTreeListModelCreateModelFunc callback implementation
GListModel* create_model_func(GObject *item, gpointer user_data)
{
struct TreeNode_t *cur = root;
2024-02-12 17:54:44 +01:00
struct TreeNode_t *parent = NULL;
struct TreeNode_t *res = NULL;
2024-02-11 17:05:35 +01:00
gchar *string = gtk_string_object_get_string(GTK_STRING_OBJECT(item));
2024-02-12 17:54:44 +01:00
parent = root;
while (cur) {
if (strcmp(string, cur->text) == NULL) {
res = cur;
break;
}
cur = cur->next;
if (cur == NULL) {
cur = parent->child;
parent = cur;
}
2024-02-11 17:05:35 +01:00
}
2024-02-12 11:40:46 +01:00
printf("[create_model_func] here is %s item\n", string);
2024-02-11 17:05:35 +01:00
return get_children_model(cur);
}
2024-02-12 16:10:28 +01:00
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
}
2024-02-12 17:54:44 +01:00
void on_setup_factory (GtkListItemFactory *factory, GtkListItem *list_item, gpointer user_data)
2024-02-11 17:05:35 +01:00
{
2024-02-12 11:40:46 +01:00
GtkWidget* expander = gtk_expander_new (NULL);
gtk_list_item_set_child (list_item, expander);
2024-02-12 17:54:44 +01:00
printf("[on_setup_factory] here is an expander\n");
2024-02-11 17:05:35 +01:00
}
2024-02-12 17:54:44 +01:00
void on_bind_factory (GtkListItemFactory *factory, GtkListItem *list_item, gpointer user_data)
2024-02-11 17:05:35 +01:00
{
2024-02-12 11:40:46 +01:00
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);
2024-02-12 12:52:38 +01:00
2024-02-12 16:10:28 +01:00
// 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);
2024-02-12 12:52:38 +01:00
gtk_widget_set_margin_start(expander, gtk_tree_list_row_get_depth(row)*20);
2024-02-12 17:54:44 +01:00
gboolean is_expanded = gtk_tree_list_row_get_expanded(row);
//gtk_tree_list_row_set_expanded(row, !is_expanded);
2024-02-12 11:40:46 +01:00
} else {
2024-02-12 17:54:44 +01:00
printf("[on_bind_factory] here is NON %s content\n", text);
2024-02-12 11:40:46 +01:00
}
2024-02-11 17:05:35 +01:00
}
// 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);
2024-02-12 11:40:46 +01:00
// Create and setup the list view and item factory
GtkListItemFactory *factory = gtk_signal_list_item_factory_new ();
2024-02-12 17:54:44 +01:00
g_signal_connect (factory, "setup", G_CALLBACK(on_setup_factory), NULL);
g_signal_connect (factory, "bind", G_CALLBACK(on_bind_factory), NULL);
2024-02-12 11:40:46 +01:00
2024-02-11 17:05:35 +01:00
// Create a GtkTreeListModel
GtkTreeListModel *tree_model = gtk_tree_list_model_new(
G_LIST_MODEL(model),
2024-02-12 11:40:46 +01:00
FALSE, // Passthrough - False in actual usage with dynamic children retrieval
FALSE, // autoexpand
2024-02-11 17:05:35 +01:00
(GtkTreeListModelCreateModelFunc)create_model_func,
NULL,
2024-02-12 17:54:44 +01:00
(GDestroyNotify)g_object_unref //(GDestroyNotify)free_tree_node
2024-02-11 17:05:35 +01:00
);
2024-02-12 11:40:46 +01:00
GtkNoSelection *selection_model = gtk_no_selection_new (
G_LIST_MODEL (tree_model));
2024-02-11 17:05:35 +01:00
2024-02-12 11:40:46 +01:00
GtkWidget *list_view = gtk_list_view_new (selection_model, factory);
2024-02-11 17:05:35 +01:00
gtk_window_set_child(GTK_WINDOW(window), list_view);
2024-02-12 11:40:46 +01:00
2024-02-11 17:05:35 +01:00
gtk_widget_set_visible(window, TRUE);
}
int main (int argc, char **argv)
{
// Create a simple tree structure
root = create_tree_node("Root");
2024-02-12 17:54:44 +01:00
struct TreeNode_t *A = create_tree_node("A");
struct TreeNode_t *B = create_tree_node("B");
struct TreeNode_t *C = create_tree_node("C");
struct TreeNode_t *D = create_tree_node("D");
struct TreeNode_t *E = create_tree_node("E");
struct TreeNode_t *F = create_tree_node("F");
struct TreeNode_t *G = create_tree_node("G");
add_child_node(root, A);
add_child_node(A, B);
add_child_node(B, C);
add_child_node(C, D);
add_child_node(E, F);
add_child_node(F, G);
2024-02-12 12:52:38 +01:00
2024-02-11 17:05:35 +01:00
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;
}