/*
 * GTK Column Test Program
 */

#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <gtk/gtk.h>
#include <gdk/gdk.h>

static GtkWidget *window_main = NULL;
static GtkWidget *list_frame = NULL;
static GtkWidget *list_store = NULL;
static GtkWidget *list_tree = NULL;

static GtkCellRenderer *col1_renderer = NULL;
static GtkTreeViewColumn *col1_column = NULL;
static GtkCellRenderer *col2_renderer = NULL;
static GtkTreeViewColumn *col2_column = NULL;
static GtkCellRenderer *col3_renderer = NULL;
static GtkTreeViewColumn *col3_column = NULL;
static GtkCellRenderer *tick_renderer = NULL;
static GtkTreeViewColumn *tick_column = NULL;

enum {
	TEST_POINTERCOL,
	TEST_TEXTCOL1,
	TEST_TEXTCOL2,
	TEST_TEXTCOL3,
	TEST_NCOLUMNS
};


static void
edit_callback(GtkCellRendererText *renderer,
                             gchar *path,
                             gchar *new_text,
                             gpointer column)
{
	GtkTreeIter iter;
	gpointer gptr;
	gint columnindex;
	
	/* Update the model with the current edit change */
	columnindex = *(gint *)column;
	gtk_tree_model_get_iter_from_string(GTK_TREE_MODEL(list_store), &iter, path);
	gtk_list_store_set(list_store, &iter, columnindex, new_text, -1);
}


static void
tickbox_callback(GtkCellRendererToggle *renderer,
                               gchar *path,
                               gpointer user_data)
{
	fprintf(stderr, "######### Firing tickbox!\n");
}


static void
create_gui()
{
	GtkTreeIter iter;
	gint *column_indexes;
	
	/* Main Window */
	window_main = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	gtk_window_set_default_size(GTK_WINDOW(window_main), 640, 480);
	gtk_window_set_position(GTK_WINDOW(window_main), GTK_WIN_POS_CENTER_ALWAYS);
	
	/* Add a treeview */
	list_frame = gtk_frame_new("Test ListView");
	
	/* Add some columns... */
	list_store = gtk_list_store_new(TEST_NCOLUMNS,
					 G_TYPE_POINTER,
	                                 G_TYPE_STRING,
	                                 G_TYPE_STRING,
	                                 G_TYPE_STRING,
					 G_TYPE_BOOLEAN);

	/* Create the view and such */
	list_tree = gtk_tree_view_new_with_model(GTK_TREE_MODEL(list_store));
	
	/* Create the column indexes */
	column_indexes = g_malloc(sizeof(gint) * TEST_NCOLUMNS);
	
	/* Textcol 1 */
	col1_renderer = gtk_cell_renderer_text_new();
	g_object_set(col1_renderer, "editable", TRUE, NULL);
	column_indexes[TEST_TEXTCOL1] = TEST_TEXTCOL1;
	g_signal_connect(G_OBJECT(col1_renderer), "edited", G_CALLBACK(edit_callback), &column_indexes[TEST_TEXTCOL1]);
	col1_column = gtk_tree_view_column_new_with_attributes("Textcol1",
	                  col1_renderer,
	                  "text",
	                  TEST_TEXTCOL1,
	                  NULL);
	//g_object_set(col1_column, "resizable", TRUE, "expand", TRUE, NULL);
	g_object_set(col1_column, "resizable", TRUE, "expand", TRUE, "sizing", GTK_TREE_VIEW_COLUMN_AUTOSIZE, NULL);
	gtk_tree_view_append_column(GTK_TREE_VIEW(list_tree), col1_column);

	/* Textcol 2 */
	col2_renderer = gtk_cell_renderer_text_new();
	g_object_set(col2_renderer, "editable", TRUE, NULL);
	column_indexes[TEST_TEXTCOL2] = TEST_TEXTCOL2;
	g_signal_connect(G_OBJECT(col2_renderer), "edited", G_CALLBACK(edit_callback), &column_indexes[TEST_TEXTCOL2]);
	col2_column = gtk_tree_view_column_new_with_attributes("Textcol2",
	                  col2_renderer,
	                  "text",
	                  TEST_TEXTCOL2,
	                  NULL);
	//g_object_set(col2_column, "resizable", TRUE, "expand", TRUE, NULL);
	g_object_set(col2_column, "resizable", TRUE, "expand", TRUE, "sizing", GTK_TREE_VIEW_COLUMN_AUTOSIZE, NULL);
	gtk_tree_view_append_column(GTK_TREE_VIEW(list_tree), col2_column);
	
	
	/* Textcol 3 */
	col3_renderer = gtk_cell_renderer_text_new();
	g_object_set(col3_renderer, "editable", TRUE, NULL);
	column_indexes[TEST_TEXTCOL3] = TEST_TEXTCOL3;
	g_signal_connect(G_OBJECT(col3_renderer), "edited", G_CALLBACK(edit_callback), &column_indexes[TEST_TEXTCOL3]);
	col3_column = gtk_tree_view_column_new_with_attributes("Textcol3",
	                  col3_renderer,
	                  "text",
	                  TEST_TEXTCOL3,
	                  NULL);
	//g_object_set(col3_column, "resizable", TRUE, "expand", TRUE, NULL);
	g_object_set(col3_column, "resizable", TRUE, "expand", TRUE, "sizing", GTK_TREE_VIEW_COLUMN_AUTOSIZE, NULL);
	gtk_tree_view_append_column(GTK_TREE_VIEW(list_tree), col3_column);
	
	
	/* Tick column */
	tick_renderer = gtk_cell_renderer_toggle_new();
	g_object_set(tick_renderer, "activatable", TRUE, NULL);	
	g_signal_connect(G_OBJECT(tick_renderer), "toggled", G_CALLBACK (tickbox_callback), NULL);
	tick_column = gtk_tree_view_column_new_with_attributes("TickCol",
	                tick_renderer, NULL);
	g_object_set(tick_column, "resizable", FALSE, "expand", FALSE, "fixed-width", 64, "sizing", GTK_TREE_VIEW_COLUMN_FIXED, NULL);
	gtk_tree_view_append_column(GTK_TREE_VIEW(list_tree), tick_column);
	
	
	/* Add some lovely data */
	gtk_list_store_insert_before(list_store, &iter, NULL);
	gtk_list_store_set(list_store, &iter,
			   TEST_TEXTCOL1, "Column1",
	                   TEST_TEXTCOL2, "Column2",
			   TEST_TEXTCOL3, "Column3",
	                   -1);	
	
	/* Bind it all together */
	gtk_container_add(GTK_CONTAINER (list_frame), list_tree);
	gtk_container_add(GTK_CONTAINER (window_main), list_frame);
	
	gtk_widget_show_all(window_main);
}

int
main(int argc, char *argv[])
{
	printf("Test Program\n");
	
	gtk_init(&argc, &argv);
	create_gui();
	gtk_main();
}
