We have a tree that can collapse!

This commit is contained in:
Adrien Bourmault 2024-02-12 16:10:28 +01:00
parent 5dffcee4a1
commit b8802b2918
Signed by: neox
GPG Key ID: 2974E1D5F25DFCC8
2 changed files with 16 additions and 1 deletions

Binary file not shown.

17
main.c
View File

@ -119,6 +119,15 @@ GListModel* create_model_func(GObject *item, gpointer user_data)
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 setup_factory (GtkListItemFactory *factory, GtkListItem *list_item, gpointer user_data)
{
GtkWidget* expander = gtk_expander_new (NULL);
@ -139,7 +148,7 @@ void bind_factory (GtkListItemFactory *factory, GtkListItem *list_item, gpointer
gtk_expander_set_label(GTK_EXPANDER(expander), text);
gtk_tree_list_row_set_expanded(row, !is_expanded);
//gtk_tree_list_row_set_expanded(row, !is_expanded);
if (gtk_tree_list_row_get_children(row)) {
printf("[bind_factory] here is %s content (childs)\n", text);
@ -147,6 +156,12 @@ void bind_factory (GtkListItemFactory *factory, GtkListItem *list_item, gpointer
printf("[bind_factory] here is %s content (no childs)\n", 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);
//g_object_unref(row); // Decrease the reference count when done