Followed is the code referring to very old gtk+1.x FAQ. The purpose is to get the selection of a GtkList
. I know GtkList is deprecated and should be replaced with GtkTreeView + GtkListStore. I do know how to use GtkListStore. However, I do want to use GtkList in current situation since GtkListStore is more complex.
The problem
for the code below is that it doesn't print anything no matter how many items I select. Please help. My gtk+ version is 2.10.11.
#include <gtk/gtk.h>
void list_changed (GtkList *list, GtkWidget *widget, gpointer user_data)
{
GList *items;
items=GTK_LIST(list)->selection;
printf("Selected Items: ");
while (items)
{
if (GTK_IS_LIST_ITEM(items->data))
printf("%d ", (guint)
gtk_object_get_user_data(items->data));
items=items->next;
}
printf("\n");
}
int main(int argc, char *argv[])
{
GtkWidget *window, *list_item, *list;
guint i;
gchar *list_items[]={"Item0", "Item1", "foo", "last Item",};
guint nlist_items=sizeof(list_items)/sizeof(list_items[0]);
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), window);
gtk_window_set_title(GTK_WINDOW(window), "GtkList");
gtk_widget_show_all(window);
list=gtk_list_new();
gtk_list_set_selection_mode(GTK_LIST(list), GTK_SELECTION_MULTIPLE);
gtk_container_add(GTK_CONTAINER(window), list);
gtk_widget_show (list);
g_signal_connect(G_OBJECT(list), "select-child", G_CALLBACK(list_changed), window);
for (i = 0; i < nlist_items; i++)
{
list_item=gtk_list_item_new_with_label(list_items[i]);
gtk_object_set_user_data(GTK_OBJECT(list_item), (gpointer)i);
gtk_container_add(GTK_CONTAINER(list), list_item);
gtk_widget_show(list_item);
}
gtk_main();
return 0;
}