diff --git a/gtk_treeview_example b/gtk_treeview_example index 7cf9d97..4f010f5 100755 Binary files a/gtk_treeview_example and b/gtk_treeview_example differ diff --git a/main.c b/main.c index b2f108b..34a7cd7 100644 --- a/main.c +++ b/main.c @@ -78,12 +78,17 @@ GListModel* get_children_model (struct TreeNode_t *parent) GtkStringList *list = gtk_string_list_new(NULL); if (parent) { + printf("[get_children_model] here is %s content : ", parent->text); + child = parent->child; while(child) { gtk_string_list_append(list, child->text); + printf("%s ", child->text); child = child->next; } } + printf("\n"); + return G_LIST_MODEL(list); } @@ -93,30 +98,47 @@ 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")) { + if (strcmp(string, "Root") == NULL) { + cur = root; + } + else if (strcmp(string, "Child 1") == NULL) { cur = root->child; } - else if (strcmp(string, "Child 2")) { + else if (strcmp(string, "Child 2") == NULL) { cur = root->child->next; } else { cur = NULL; } + printf("[create_model_func] here is %s item\n", string); + 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); + GtkWidget* expander = gtk_expander_new (NULL); + 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) { - 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); + GObject *item; + const gchar *text; + GtkTreeListRow *row = gtk_list_item_get_item(list_item); + + 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 @@ -129,24 +151,28 @@ void app_activate (GApplication *app, gpointer user_data) 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(setup_factory), NULL); + g_signal_connect (factory, "bind", G_CALLBACK(bind_factory), NULL); + // 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 + FALSE, // Passthrough - False in actual usage with dynamic children retrieval + FALSE, // autoexpand (GtkTreeListModelCreateModelFunc)create_model_func, NULL, - NULL //(GDestroyNotify)free_tree_node + 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); + GtkNoSelection *selection_model = gtk_no_selection_new ( + G_LIST_MODEL (tree_model)); - GtkWidget *list_view = gtk_list_view_new(GTK_SELECTION_MODEL(gtk_no_selection_new(tree_model)), factory); + 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); }