first commit
This commit is contained in:
commit
4b417fbb2b
|
@ -0,0 +1,13 @@
|
|||
CC = gcc
|
||||
CFLAGS = `pkg-config --cflags gtk4`
|
||||
LIBS = `pkg-config --libs gtk4`
|
||||
|
||||
TARGET = gtk_treeview_example
|
||||
|
||||
all: $(TARGET)
|
||||
|
||||
$(TARGET): main.c
|
||||
$(CC) -g $(CFLAGS) -o $(TARGET) main.c $(LIBS)
|
||||
|
||||
clean:
|
||||
rm -f $(TARGET)
|
Binary file not shown.
|
@ -0,0 +1,166 @@
|
|||
#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;
|
||||
}
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
;;
|
||||
;; Dépendances sous GNU Guix
|
||||
;;
|
||||
|
||||
|
||||
(specifications->manifest
|
||||
(list
|
||||
"bash"
|
||||
"coreutils"
|
||||
"gcc-toolchain"
|
||||
"pkg-config"
|
||||
"valgrind"
|
||||
"findutils"
|
||||
"gdb"
|
||||
"make"
|
||||
"gtk"
|
||||
"libxml2"
|
||||
"glu"
|
||||
"glew"
|
||||
"glfw"
|
||||
"cglm"
|
||||
"libepoxy"
|
||||
"pango@1.90.0"
|
||||
"xorgproto"
|
||||
"glib"
|
||||
"mesa-headers"
|
||||
"mesa"
|
||||
"libadwaita"
|
||||
)
|
||||
)
|
Loading…
Reference in New Issue