GNOME Bugzilla – Bug 119364
list fires two selections if items added after list shown
Last modified: 2004-12-22 21:47:04 UTC
In the following snippet the list's items are added after the list is shown (they're added at "position 2"), so no item is initially selected. The problem is that selecting a list item then causes 2 selection events to fire instead of the expected 1 selection event. Moving the item-creation lines from "position 2" to "position 1" makes the problem go away, presumably because the first item is initially selected for free. #include <gtk/gtk.h> void changed (int a, int b) { g_print("selection\n"); } int main (int argc, char *argv[]) { GtkWidget *window, *sw, *box; GtkTreeViewColumn *column; GtkTreeIter iter1, iter2; GtkListStore *store; GtkWidget *tree; GtkCellRenderer *renderer; GtkTreeSelection *selection; gtk_init (&argc, &argv); window = gtk_window_new (GTK_WINDOW_TOPLEVEL); gtk_window_set_default_size (GTK_WINDOW (window), 200, 200); box = gtk_hbox_new (FALSE, 0); gtk_container_add (GTK_CONTAINER(window), box); sw = gtk_scrolled_window_new (NULL, NULL); store = gtk_list_store_new (1, G_TYPE_STRING); tree = gtk_tree_view_new_with_model (GTK_TREE_MODEL (store)); g_object_unref (G_OBJECT (store)); renderer = gtk_cell_renderer_text_new (); column = gtk_tree_view_column_new_with_attributes ( 0, renderer, "text", 0, NULL); gtk_tree_view_append_column (GTK_TREE_VIEW (tree), column); selection = gtk_tree_view_get_selection(GTK_TREE_VIEW (tree)); gtk_tree_selection_set_mode (selection, GTK_SELECTION_BROWSE); g_signal_connect (selection, "changed", G_CALLBACK(changed), NULL); gtk_box_pack_start (GTK_BOX(box), sw, TRUE, TRUE, 0); gtk_container_add ((GtkContainer *)sw, tree); /* position 1 */ gtk_widget_show_all(window); /* position 2 */ gtk_list_store_append (store, &iter1); gtk_list_store_set (store, &iter1, 0, "item 1", -1); gtk_list_store_append (store, &iter2); gtk_list_store_set (store, &iter2, 0, "item 2", -1); gtk_main(); return 0; }
As a side note: the same problem also happens if TreeStore is used instead of ListStore.
*** This bug has been marked as a duplicate of 94837 ***