tests_gtk4/main.c

167 lines
4.7 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;
GtkStringList *list = gtk_string_list_new(NULL);
if (parent) {
child = parent->child;
while(child) {
gtk_string_list_append(list, child->text);
child = child->next;
}
}
return G_LIST_MODEL(list);
}
// GtkTreeListModelCreateModelFunc callback implementation
GListModel* create_model_func(GObject *item, gpointer user_data)
{
struct TreeNode_t *cur = root;
gchar *string = gtk_string_object_get_string(GTK_STRING_OBJECT(item));
if (strcmp(string, "Child 1")) {
cur = root->child;
}
else if (strcmp(string, "Child 2")) {
cur = root->child->next;
} else {
cur = NULL;
}
return get_children_model(cur);
}
void setup_factory (GtkListItemFactory *factory, GtkListItem *list_item, gpointer user_data)
{
GtkWidget *label = gtk_label_new(NULL);
gtk_list_item_set_child(list_item, label);
}
void bind_factory (GtkListItemFactory *factory, GtkListItem *list_item, gpointer user_data)
{
GObject *item = gtk_list_item_get_item(list_item);
const gchar *text = gtk_string_object_get_string(GTK_STRING_OBJECT(item));
GtkWidget *label = gtk_list_item_get_child(list_item);
gtk_label_set_text(GTK_LABEL(label), text);
}
// 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 a GtkTreeListModel
GtkTreeListModel *tree_model = gtk_tree_list_model_new(
G_LIST_MODEL(model),
TRUE, // Passthrough - False in actual usage with dynamic children retrieval
TRUE, // autoexpand
(GtkTreeListModelCreateModelFunc)create_model_func,
NULL,
NULL //(GDestroyNotify)free_tree_node
);
// Create and setup the list view and item factory
GtkListItemFactory *factory = gtk_signal_list_item_factory_new();
g_signal_connect(factory, "setup", G_CALLBACK(setup_factory), NULL);
g_signal_connect(factory, "bind", G_CALLBACK(bind_factory), NULL);
GtkWidget *list_view = gtk_list_view_new(GTK_SELECTION_MODEL(gtk_no_selection_new(tree_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");
add_child_node(root, create_tree_node("Child 1"));
add_child_node(root, create_tree_node("Child 2"));
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;
}