#include #include #include #include #include #include #include #include #include #include #include #include #include // 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 = NULL; if (parent) { 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); printf("%s ", child->text); child = child->next; } } 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; gchar *string = gtk_string_object_get_string(GTK_STRING_OBJECT(item)); parent = root; while (cur) { if (strcmp(string, cur->text) == NULL) { res = cur; break; } cur = cur->next; if (cur == NULL) { cur = parent->child; parent = cur; } } 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); 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 { printf("[on_bind_factory] here is NON %s content\n", 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 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 ); GtkNoSelection *selection_model = gtk_no_selection_new ( G_LIST_MODEL (tree_model)); GtkWidget *list_view = gtk_list_view_new (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"); 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); 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; }