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

int container_type = 1; /* 0 - Table ; 1 - Notebook */
GtkWidget *t1, *t2;
GtkWidget *vbox;
GtkWidget *c = NULL;

static void finish ( GtkWidget *widget, gpointer data )
{
	g_print ("Quit\n");
	gtk_main_quit ();
}

static gboolean delete_event ( GtkWidget *widget, GdkEvent *event, gpointer data )
{
	return FALSE;
}

static void destroy ( GtkWidget *widget, gpointer data )
{
	gtk_main_quit ();
}

/* removes t1 and t2 from c */
void detach_terminals ()
{
	if (container_type == 0) {
		g_print ("detach container 1 c %x t1 %x\n", c, t1);
		gtk_container_remove(GTK_CONTAINER(c), t1);
		g_print ("detach container 2 c %x t2 %x\n", c, t2);
		gtk_container_remove(GTK_CONTAINER(c), t2);
	} else {
		g_print ("detach notebook 1 c %x t1 %x\n", c, t1);
		gtk_notebook_remove_page(GTK_NOTEBOOK(c), -1);
		g_print ("detach notebook 2 c %x t2 %x\n", c, t2);
		gtk_notebook_remove_page(GTK_NOTEBOOK(c), -1);
	}
}


void new_container ()
{
	if (c != NULL) {
		gtk_widget_destroy(c);
		g_print ("c widget destroyed\n");
		c = NULL;
	}

	g_print("Begin new_container ()\n");
	if (container_type == 0) {
		c = gtk_table_new(2, 1, TRUE);
		g_print ("table attach to c %x t1 %x t2 %x\n", c, t1, t2);
		gtk_table_attach_defaults (GTK_TABLE (c), t1, 0, 1, 0, 1);
		gtk_table_attach_defaults (GTK_TABLE (c), t2, 0, 1, 1, 2);
	} else {
		c = gtk_notebook_new ();
		g_print ("notebook attach to c %x t1 %x t2 %x\n", c, t1, t2);
		gtk_notebook_append_page(GTK_NOTEBOOK (c), t1, NULL);
		gtk_notebook_append_page(GTK_NOTEBOOK (c), t2, NULL);
	} 
	g_print("End new_container ()\n");
}

static void change_container ( GtkWidget *widget, gpointer   data )
{
	g_print ("Change %x\n", data);
	detach_terminals(data);
	g_print ("After detaching \n");

	if (container_type == 0)
		container_type = 1;
	else
		container_type = 0;

	new_container ();
	g_print ("c widget created %x\n", c);
	gtk_box_pack_start(GTK_BOX(vbox), c, TRUE, TRUE, 0);
	gtk_box_reorder_child(GTK_BOX(vbox), c, 0);
	gtk_widget_show_all (vbox);
}

int main ( int argc, char *argv[] )
{
	GtkWidget *window;
	GtkWidget *quit;
	GtkWidget *change;

	gtk_init (&argc, &argv);

	window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
	g_signal_connect (G_OBJECT (window), "delete_event", G_CALLBACK (delete_event), NULL);
	g_signal_connect (G_OBJECT (window), "destroy", G_CALLBACK (destroy), NULL);

	quit = gtk_button_new_with_label ("Quit");
	g_signal_connect (G_OBJECT (quit), "clicked", G_CALLBACK (finish), NULL);

	change = gtk_button_new_with_label ("Change container");
	vbox = gtk_vbox_new(FALSE, 0);

	t1 = vte_terminal_new();  
	t2 = vte_terminal_new();

	/*t1 = gtk_label_new("aaaa");
	t2 = gtk_label_new("bbbb");*/
	/* Make sure we don't lose the terminals when we change container */
	g_object_ref(t1);
	g_object_ref(t2);
	new_container();
	g_signal_connect (G_OBJECT (change), "clicked", G_CALLBACK (change_container), NULL);
	
	gtk_box_pack_start (GTK_BOX(vbox), c,      TRUE, TRUE, 0);
	gtk_box_pack_start (GTK_BOX(vbox), change, TRUE, TRUE, 0);
	gtk_box_pack_start (GTK_BOX(vbox), quit,   TRUE, TRUE, 0);
	gtk_container_add (GTK_CONTAINER (window), vbox);
	gtk_widget_show_all (window);
	gtk_main ();

	return 0;
}
