/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
 * main.c
 * Copyright (C) Granjoux Sébastien 2008 <seb@localhost>
 * 
 * main.c is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * main.c is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <gtk/gtk.h>

GtkWidget *lab1 = NULL;
GtkWidget *notebook = NULL;
GtkWidget *vbox = NULL;

static void on_crash_clicked (GtkButton* button, GtkWidget* window);

static GdkWindow*
get_parent_window_qdata (GtkWidget *widget)
{
	GdkWindow *parent;
	GdkWindow *qdata;
	
	if (widget->parent == NULL) return NULL;
	parent = widget->parent->window;
	widget->parent->window = NULL;
	qdata = gtk_widget_get_parent_window (widget);
	widget->parent->window = parent;
	
	return qdata;
}

/* Create a window with a notebook and two buttons */

static void
create_window (void)
{
	GtkWidget *button1;
	GtkWidget *button2;
	GtkWidget *window;
	
	/* Create notebook if necessary */
	if (notebook == NULL)
	{
		GtkWidget *text1;
		GtkWidget *text2;
		GtkTextBuffer *buffer;
		
		/* Create text view */
		text1 = gtk_text_view_new ();
		buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (text1));
		gtk_text_buffer_insert_at_cursor (buffer, "To crash GTK drag and drop one tab, then click on Crash button below",-1);
		lab1 = gtk_label_new ("Page 1");
		
		/* Create another text view */
		text2 = gtk_text_view_new ();
		
		notebook = gtk_notebook_new ();
		gtk_notebook_append_page (GTK_NOTEBOOK (notebook),text1, lab1);
		gtk_notebook_set_tab_reorderable (GTK_NOTEBOOK (notebook), text1, TRUE);
		gtk_notebook_append_page (GTK_NOTEBOOK (notebook),text2, NULL);	
		gtk_notebook_set_tab_reorderable (GTK_NOTEBOOK (notebook), text2, TRUE);
		g_object_ref (notebook);
	}

	/* Create button */
	button1 = gtk_button_new_with_mnemonic ("_Crash");

	/* Create button */
	button2 = gtk_button_new_with_mnemonic ("_Quit");
	
	/* Create box grouping all widgets */
	vbox = gtk_vbox_new(FALSE, 5);
	gtk_box_pack_start_defaults (GTK_BOX (vbox), notebook);
	gtk_box_pack_start_defaults (GTK_BOX (vbox), button1);
	gtk_box_pack_start_defaults (GTK_BOX (vbox), button2);
	
	/* Create top level window */
	window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
	gtk_container_add (GTK_CONTAINER (window), vbox);

	g_signal_connect (button1, "clicked", G_CALLBACK (on_crash_clicked), window);
	g_signal_connect (button2, "clicked", G_CALLBACK (gtk_main_quit), NULL);
	
	gtk_widget_show_all (window);
	
	g_message ("after creation:  notebook %p parent %p lab1 %p parent_window %p qdata %p", notebook, lab1->parent, lab1, lab1->parent != NULL ? lab1->parent->window : NULL, get_parent_window_qdata (lab1));
}

static void
on_crash_clicked (GtkButton* button, GtkWidget* window)
{
	g_message ("before destroy:  notebook %p parent %p lab1 %p parent_window %p qdata %p", notebook, lab1->parent, lab1, lab1->parent->window, get_parent_window_qdata (lab1));
	
	/* Remove notebook from window */
	gtk_container_remove (GTK_CONTAINER (vbox), notebook);

	g_message ("after remove:    notebook %p parent %p lab1 %p parent_window %p qdata %p", notebook, lab1->parent, lab1, lab1->parent->window, get_parent_window_qdata (lab1));
	
	/* Destroy window */
	gtk_widget_destroy (window);
	
	/* Recreate window, keeping the notebook */
	create_window();
}

int
main (int   argc,
      char *argv[])
{
	gtk_init (&argc, &argv);

	create_window ();
	
 	gtk_main ();
	
	return 0;
}
