#include <math.h>
#include <sys/time.h>
#include <unistd.h>

#include <gtk/gtk.h>

#define ROW_QTY 1000

static struct timeval start;

static void
selection_changed (GtkTreeSelection *sel, gpointer data)
{
    /* since I passed the address of "gint dummy", data
     * should not be NULL here
     */

    g_assert (*(gint *)data == 100);
}

gint
main (int argc, char *argv[])
{
	gint i;
	gchar buf[64];
	GtkWidget * window;
	GtkWidget * scrolled_window;
	GtkWidget * tree_view;
	GtkListStore * store;
	GtkCellRenderer * renderer;
	GtkTreeViewColumn * column;
	GtkTreeSelection *selection;
	GtkTreeSortable *sortable;
	gint dummy;

	/* init gtk */
	gtk_init (&argc, &argv);

	/* create a window */
	window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
	gtk_window_set_default_size (GTK_WINDOW(window), 400, 400);
	gtk_window_set_title (GTK_WINDOW(window), "tree_view bug");
	scrolled_window = gtk_scrolled_window_new (NULL, NULL);

	/* make a tree_view with a pre-populated model */
	store = gtk_list_store_new (1, G_TYPE_STRING);
	for (i=0; i<ROW_QTY; ++i) {
		GtkTreeIter iter;
		g_snprintf (buf, sizeof(buf), "%d", i+1);
		gtk_list_store_append (store, &iter);
		gtk_list_store_set (store, &iter, 0, buf, -1);
	}


	gettimeofday (&start, NULL);
	tree_view = gtk_tree_view_new_with_model (GTK_TREE_MODEL(store));
	renderer = gtk_cell_renderer_text_new ();
	column = gtk_tree_view_column_new_with_attributes ("Number", renderer, "text", 0, NULL);
	gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_FIXED);
	gtk_tree_view_column_set_fixed_width (column, 350);
	gtk_tree_view_append_column (GTK_TREE_VIEW(tree_view), column);
	
	/* Set the sort indicator for this column */
	gtk_tree_view_column_set_sort_indicator (column, TRUE);
	gtk_tree_view_column_set_sort_order (column, GTK_SORT_ASCENDING);
    
	/* And sort the column */
	sortable = GTK_TREE_SORTABLE (gtk_tree_view_get_model (GTK_TREE_VIEW (tree_view)));
    
	gtk_tree_sortable_set_sort_column_id (sortable, 0, GTK_SORT_ASCENDING);
	
	selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (tree_view));

	g_signal_connect (G_OBJECT (selection), "changed",
			  G_CALLBACK (selection_changed), &dummy);

	gtk_container_add (GTK_CONTAINER(scrolled_window), tree_view);
	gtk_container_add (GTK_CONTAINER(window), scrolled_window);

	dummy = 100;

	
	gtk_widget_show_all (window);

	gtk_main ();
	return 0;
}
