#include <gtk/gtk.h>

/* re. bug 639875 */

static GtkNotebook *
detach_tab_callback (GtkNotebook *book,
		     GtkWidget *tab,
		     gint x, gint y,
		     gpointer data)
{
    GtkWidget *w, *label;
    gint pgnum;

    pgnum = gtk_notebook_page_num(book, tab);

    w = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_default_size(GTK_WINDOW(w), 150, 50);
    gtk_window_set_title(GTK_WINDOW(w), "detached");

    label = g_object_get_data(G_OBJECT(tab), "label");
    g_object_ref(label);
    gtk_container_remove(GTK_CONTAINER(tab), label);
    gtk_container_add(GTK_CONTAINER(w), label);
    g_object_unref(label);

    gtk_notebook_remove_page(book, pgnum);
    gtk_widget_show_all(w);

    return NULL;
}

int main (int argc, char **argv)
{
    GtkWidget *w, *book;
    GtkWidget *tab, *label;

    gtk_init(&argc, &argv);
 
    w = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_default_size(GTK_WINDOW(w), 200, 80);
    g_signal_connect(G_OBJECT(w), "destroy",
		     gtk_main_quit, NULL);

    book = gtk_notebook_new();
    g_signal_connect(G_OBJECT(book), "create-window",
		     G_CALLBACK(detach_tab_callback), 
		     NULL);

    tab = gtk_vbox_new(FALSE, 5);
    label = gtk_label_new("First tab");
    g_object_set_data(G_OBJECT(tab), "label", label);
    gtk_container_add(GTK_CONTAINER(tab), label);
    gtk_notebook_append_page(GTK_NOTEBOOK(book),
			     tab, NULL);

    tab = gtk_vbox_new(FALSE, 5);
    label = gtk_label_new("Second tab");
    g_object_set_data(G_OBJECT(tab), "label", label);
    gtk_container_add(GTK_CONTAINER(tab), label);
    gtk_notebook_append_page(GTK_NOTEBOOK(book),
			     tab, NULL);
    gtk_notebook_set_tab_detachable(GTK_NOTEBOOK(book), 
				    tab, TRUE);

    gtk_container_add(GTK_CONTAINER(w), book);
    gtk_widget_show_all(w);

    gtk_main();

    return 0;
}
    
