Whole list but not tree

This commit is contained in:
Adrien Bourmault 2024-02-12 11:40:46 +01:00
parent b1853d3dd2
commit 9753e4c6e1
Signed by: neox
GPG Key ID: 2974E1D5F25DFCC8
2 changed files with 42 additions and 16 deletions

Binary file not shown.

64
main.c
View File

@ -78,12 +78,17 @@ GListModel* get_children_model (struct TreeNode_t *parent)
GtkStringList *list = gtk_string_list_new(NULL); GtkStringList *list = gtk_string_list_new(NULL);
if (parent) { if (parent) {
printf("[get_children_model] here is %s content : ", parent->text);
child = parent->child; child = parent->child;
while(child) { while(child) {
gtk_string_list_append(list, child->text); gtk_string_list_append(list, child->text);
printf("%s ", child->text);
child = child->next; child = child->next;
} }
} }
printf("\n");
return G_LIST_MODEL(list); return G_LIST_MODEL(list);
} }
@ -93,30 +98,47 @@ GListModel* create_model_func(GObject *item, gpointer user_data)
struct TreeNode_t *cur = root; struct TreeNode_t *cur = root;
gchar *string = gtk_string_object_get_string(GTK_STRING_OBJECT(item)); gchar *string = gtk_string_object_get_string(GTK_STRING_OBJECT(item));
if (strcmp(string, "Child 1")) { if (strcmp(string, "Root") == NULL) {
cur = root;
}
else if (strcmp(string, "Child 1") == NULL) {
cur = root->child; cur = root->child;
} }
else if (strcmp(string, "Child 2")) { else if (strcmp(string, "Child 2") == NULL) {
cur = root->child->next; cur = root->child->next;
} else { } else {
cur = NULL; cur = NULL;
} }
printf("[create_model_func] here is %s item\n", string);
return get_children_model(cur); return get_children_model(cur);
} }
void setup_factory (GtkListItemFactory *factory, GtkListItem *list_item, gpointer user_data) void setup_factory (GtkListItemFactory *factory, GtkListItem *list_item, gpointer user_data)
{ {
GtkWidget *label = gtk_label_new(NULL); GtkWidget* expander = gtk_expander_new (NULL);
gtk_list_item_set_child(list_item, label); gtk_list_item_set_child (list_item, expander);
printf("[setup_factory] here is an expander\n");
} }
void bind_factory (GtkListItemFactory *factory, GtkListItem *list_item, gpointer user_data) void bind_factory (GtkListItemFactory *factory, GtkListItem *list_item, gpointer user_data)
{ {
GObject *item = gtk_list_item_get_item(list_item); GObject *item;
const gchar *text = gtk_string_object_get_string(GTK_STRING_OBJECT(item)); const gchar *text;
GtkWidget *label = gtk_list_item_get_child(list_item); GtkTreeListRow *row = gtk_list_item_get_item(list_item);
gtk_label_set_text(GTK_LABEL(label), text);
if (row != NULL) {
gboolean is_expanded = gtk_tree_list_row_get_expanded(row);
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);
printf("[bind_factory] here is %s content\n", text);
gtk_tree_list_row_set_expanded(row, !is_expanded);
//g_object_unref(row); // Decrease the reference count when done
} else {
printf("[bind_factory] here is NON %s content\n", text);
}
} }
// Application activation callback // Application activation callback
@ -129,24 +151,28 @@ void app_activate (GApplication *app, gpointer user_data)
GtkStringList *model = gtk_string_list_new(NULL); GtkStringList *model = gtk_string_list_new(NULL);
gtk_string_list_append(model, root->text); 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 // Create and setup the list view and item factory
GtkListItemFactory *factory = gtk_signal_list_item_factory_new (); GtkListItemFactory *factory = gtk_signal_list_item_factory_new ();
g_signal_connect (factory, "setup", G_CALLBACK(setup_factory), NULL); g_signal_connect (factory, "setup", G_CALLBACK(setup_factory), NULL);
g_signal_connect (factory, "bind", G_CALLBACK(bind_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); // 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,
NULL //(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_window_set_child(GTK_WINDOW(window), list_view);
gtk_widget_set_visible(window, TRUE); gtk_widget_set_visible(window, TRUE);
} }