diff --git a/gtk_treeview_example b/gtk_treeview_example index b431899..3d28613 100755 Binary files a/gtk_treeview_example and b/gtk_treeview_example differ diff --git a/main.c b/main.c index 76168d9..da9ed8b 100644 --- a/main.c +++ b/main.c @@ -100,18 +100,23 @@ GListModel* get_children_model (struct TreeNode_t *parent) 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)); - if (strcmp(string, "Root") == NULL) { - cur = root; - } - else if (strcmp(string, "Child 1") == NULL) { - cur = root->child; - } - else if (strcmp(string, "Child 2") == NULL) { - cur = root->child->next; - } else { - cur = NULL; + 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); @@ -128,34 +133,24 @@ void on_expander_toggled(GtkExpander *expander, gpointer user_data) { // Update your model here based on the new state } -void setup_factory (GtkListItemFactory *factory, GtkListItem *list_item, gpointer user_data) +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("[setup_factory] here is an expander\n"); + printf("[on_setup_factory] here is an expander\n"); } -void bind_factory (GtkListItemFactory *factory, GtkListItem *list_item, gpointer user_data) +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) { - 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); - - //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); - } else { - 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); @@ -163,10 +158,10 @@ void bind_factory (GtkListItemFactory *factory, GtkListItem *list_item, gpointer 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 + gboolean is_expanded = gtk_tree_list_row_get_expanded(row); + //gtk_tree_list_row_set_expanded(row, !is_expanded); } else { - printf("[bind_factory] here is NON %s content\n", text); + printf("[on_bind_factory] here is NON %s content\n", text); } } @@ -182,8 +177,8 @@ void app_activate (GApplication *app, gpointer user_data) // 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); + 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( @@ -192,7 +187,7 @@ void app_activate (GApplication *app, gpointer user_data) FALSE, // autoexpand (GtkTreeListModelCreateModelFunc)create_model_func, NULL, - NULL //(GDestroyNotify)free_tree_node + (GDestroyNotify)g_object_unref //(GDestroyNotify)free_tree_node ); GtkNoSelection *selection_model = gtk_no_selection_new ( @@ -209,8 +204,19 @@ 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")); + 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);