/* 
 * Compilation instruction:
 * gcc -o treeview-bug treeview-bug.c `pkg-config --cflags --libs gtk+-2.0`
 * 
 * Bug reproduction:
 * Hold <Ctrl> or <Shift> and click mouse buttons to select columns.
 * Compare the number of selected columns you see with the print out
 * from the "cursor-changed" callback.
 */

#include <stdio.h>
#include <gtk/gtk.h>

static void show_rows(GtkTreeView *treeview, gpointer data) {
  GtkTreeSelection *selection = gtk_tree_view_get_selection(treeview);
  int count = gtk_tree_selection_count_selected_rows(selection);
  printf("Selected %d rows\n", count);
}

int main(int argc, char *argv[]) {
  GtkWidget *window;
  GtkListStore *liststore;
  GtkTreeIter iter;
  GtkCellRenderer *renderer;
  GtkTreeViewColumn *column;
  GtkWidget *treeview;
  char what[10];
  char where[18];
  int i;

  gtk_set_locale ();
  gtk_init (&argc, &argv);

  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title(GTK_WINDOW(window), "Bug report");
  g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);

  liststore = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_STRING);
  for(i=0; i<10; i++) {
    sprintf(what, "Person #%d", i);
    sprintf(where, "Far away place #%d", 9-i);
    gtk_list_store_append(liststore, &iter);
    gtk_list_store_set(liststore, &iter, 0, what, 1, where, -1);
  }

  treeview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(liststore));
  g_signal_connect(treeview, "cursor_changed", G_CALLBACK(show_rows), NULL);
  gtk_tree_selection_set_mode(gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview)),
			      GTK_SELECTION_MULTIPLE);

  renderer = gtk_cell_renderer_text_new ();
  column = gtk_tree_view_column_new_with_attributes("Who",
						    renderer,
						    "text", 0,
						    NULL);
  gtk_tree_view_append_column (GTK_TREE_VIEW(treeview), column);
  renderer = gtk_cell_renderer_text_new ();
  column = gtk_tree_view_column_new_with_attributes("Where",
						    renderer,
						    "text", 1,
						    NULL);
  gtk_tree_view_append_column (GTK_TREE_VIEW(treeview), column);

  gtk_container_add(GTK_CONTAINER(window), treeview);
  gtk_widget_show_all(window);
  gtk_main();

  return 0;
}
