#include <gtk/gtk.h>
#include <sys/time.h>

/*

 gcc -o t `pkg-config --cflags --libs gtk+-2.0` pr240033-simple.c
 export GTK_MODULES=/usr/local/gtk-2.12.1/lib/gtk-2.0/modules/libgail:/usr/local/gtk-2.12.1/lib/gtk-2.0/modules/libatk-bridge

*/

static int NUM_COLUMNS = 3;
static int NUM_ITEMS = 5;

GtkWidget *window, *button, *table, *vbox;
GtkTreeModel *model;


int buttonClicked (GtkWidget* widget, gpointer data) {
	gint i = 0;
	struct timeval t_start, t_finish;
	double elapsed;
	GtkTreeIter iter;
	
	printf("Start\n");
	gettimeofday(&t_start, NULL);
	for (i = 0; i < 2000; i++) {
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
	}
	gettimeofday(&t_finish, NULL);
	elapsed = t_finish.tv_sec - t_start.tv_sec + (t_finish.tv_usec - t_start.tv_usec) / 1.e6;
	printf("\nEnd time elapsed %.9f (seconds)\n", elapsed);
	return 0;
}

void create_model () {
	gint i = 0;
	GtkTreeIter iter;
	model = GTK_TREE_MODEL(gtk_list_store_new (3, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING));
	for (i = 0; i < NUM_ITEMS; i++) {
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model), &iter, 0, "Test", 1, "TEST", 2, "Test", -1);
	}
}

void add_columns (GtkTreeView *treeview) {
	GtkCellRenderer *renderer;
	GtkTreeViewColumn *column;
	GtkRequisition req; 
	gint i = 0;
	for (i = 0; i < NUM_COLUMNS; i++) {
		renderer = gtk_cell_renderer_text_new ();
		column = gtk_tree_view_column_new_with_attributes ("Column", renderer, "text", i, NULL);
		gtk_tree_view_append_column (treeview, column);
	}
}


int main (int argc, char** argv) {
	gtk_init_check (&argc, &argv);
	window = gtk_window_new (GTK_WINDOW_TOPLEVEL);

	vbox = gtk_vbox_new (FALSE, 8);
	button  = gtk_button_new_with_label ("Test");

	create_model ();
	table = gtk_tree_view_new_with_model (model);
	g_object_unref (model);
	add_columns (GTK_TREE_VIEW (table));
	gtk_widget_set_size_request (table, 400, 400);

	gtk_container_add (GTK_CONTAINER (window), vbox);
	gtk_container_add (GTK_CONTAINER (vbox), table);
	gtk_container_add (GTK_CONTAINER (vbox), button);

	g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK (buttonClicked), table);
	g_signal_connect (G_OBJECT (window), "destroy", G_CALLBACK (gtk_main_quit), NULL);
	g_signal_connect (G_OBJECT (window), "delete_event",G_CALLBACK (gtk_main_quit), NULL);
	gtk_widget_show_all (window);
	gtk_main();
}
