#include <gtk/gtk.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>

enum {
    N_ITEMS = 20000
};

static int n_items;

static gboolean
fill_model (gpointer data)
{
    GtkListStore *store = data;
    GTimer *timer = g_timer_new ();
    g_timer_reset(timer);
    
    while (n_items < N_ITEMS)
    {
	GtkTreeIter iter;

	gtk_list_store_append (store, &iter);

	gtk_list_store_set (store, &iter,
			    0, "asdf",
			    1, "qwer",
			    2, "wert",
			    3, "4356",
			    4, "xcvb",
			    5, "kjhg",
			    6, "uyti",
			    7, "poiu",
			    8, "hgdf",
			    9, "mnbv",
			    -1);

	if (n_items % 5000 == 0)
	{
	    g_print (".");
	    fflush (stdout);
	}
	
	
	++n_items;
    }

    g_print ("\nTime to fill model: %f\n", g_timer_elapsed (timer, NULL));
    
    return FALSE;
}

static GTimer *validation_timer;

static gboolean
quit_after_validation (gpointer data)
{
    g_print ("time to do validation: %f\n",
	     g_timer_elapsed (validation_timer, NULL));
    gtk_main_quit();
    return FALSE;
}

int
main (int argc, char *argv[])
{
    gtk_init (&argc, &argv);
    {
	GtkListStore *store = gtk_list_store_new (
	    10, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING,
	    G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING,
	    G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING,
	    G_TYPE_STRING);
	
	GtkTreeView *view = GTK_TREE_VIEW (
	    gtk_tree_view_new_with_model (GTK_TREE_MODEL (store)));
	GtkWindow *window = GTK_WINDOW (gtk_window_new (GTK_WINDOW_TOPLEVEL));
	GtkWidget *scroll_win = gtk_scrolled_window_new (NULL, NULL);
	
	GtkTreeViewColumn *column;
	GtkCellRenderer *renderer;
	int i;

	for (i = 0; i < 10; ++i)
	{
	    renderer = gtk_cell_renderer_text_new ();
	    column = gtk_tree_view_column_new_with_attributes (
		"Foo", renderer, "text", i, NULL);
	    gtk_tree_view_column_set_resizable (column, TRUE);
	    gtk_tree_view_append_column (view, column);
	}
	
	gtk_container_add (GTK_CONTAINER (window), scroll_win);
	gtk_container_add (GTK_CONTAINER (scroll_win), GTK_WIDGET (view));

	fill_model (store);

#if 0
	return 1;
#endif
	gtk_widget_show_all (GTK_WIDGET (window));
	validation_timer = g_timer_new ();
	g_idle_add_full ((GDK_PRIORITY_REDRAW + 10), /* below tree view
						      * validation
						      */
			 quit_after_validation, NULL, NULL);
	gtk_main ();
    }
    
    return 0;
}
