/*  Eric Ritezel :: 2003.05.14 0300PST
 *  Demonstrate the idea of tabs with close buttons
 *    for http://bugzilla.gnome.org/show_bug.cgi?id=112963
 *
 *  To build: 
 *    gcc notebtn.c -o notebtn `pkg-config gtk+-2.0 --cflags --libs`
 */
#include <stdio.h>
#include <gtk/gtk.h>

GtkWidget*
notebook_frame_create (char* framename)
{
    GtkWidget *frame, *label;

    /*  Define a simple frame  */
    frame = gtk_frame_new (framename);
    gtk_container_set_border_width (GTK_CONTAINER (frame), 10);
    gtk_widget_set_size_request (frame, 100, 75);
    gtk_widget_show (frame);

    /*  Define a descriptive label  */
    label = gtk_label_new ("Switch tabs to see the problem.");
    gtk_container_add (GTK_CONTAINER (frame), label);
    gtk_widget_show (label);

    return frame;
}

GtkWidget*
notebook_tab_create (char* tabname)
{
    GtkWidget *hbox;
    GtkWidget *label, *button, *image;

    /*	create a horizontal layout box	*/
    hbox = gtk_hbox_new (FALSE, 4);

    /*	create a new label	*/
    label = gtk_label_new (tabname);
    gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);

    /*	create a button	*/
    button = gtk_button_new ();
    gtk_button_set_relief (GTK_BUTTON (button), GTK_RELIEF_NONE);
    gtk_widget_set_size_request (button, 16, 16);

    /*	pull out the "close" stock icon	*/
   	image = gtk_image_new_from_stock (
            GTK_STOCK_CLOSE, GTK_ICON_SIZE_MENU);

    /*	add the icon to the button	*/
    gtk_container_add (GTK_CONTAINER (button), image);

    /*	layout and display	*/
    gtk_box_pack_start (GTK_BOX (hbox), label, TRUE, TRUE, 0);
    gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
    gtk_widget_show_all(hbox);
	
    return hbox;
}

gint delete( GtkWidget *widget,
             GtkWidget *event,
             gpointer   data )
{
    gtk_main_quit ();
    return FALSE;
}

int main( int argc,
          char *argv[] )
{
    GtkWidget *window, *table, *notebook;

    gtk_init (&argc, &argv);

    /*  Create a new top-level window  */
    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    g_signal_connect (
            G_OBJECT (window), "delete_event",
            G_CALLBACK (delete), NULL);

    /*  Create a new table for layout functions  */
    table = gtk_table_new (3, 6, FALSE);
    gtk_container_set_border_width (GTK_CONTAINER (window), 10);
    gtk_container_add (GTK_CONTAINER (window), table);

    /*  Create a new notebook, place the position of the tabs  */
	notebook = gtk_notebook_new ();
    gtk_notebook_set_tab_pos (GTK_NOTEBOOK (notebook), GTK_POS_TOP);
    gtk_table_attach_defaults (GTK_TABLE (table), notebook, 0, 6, 0, 1);
    gtk_widget_show (notebook);

    /*  create the first tab  */
    gtk_notebook_append_page (
            GTK_NOTEBOOK (notebook),
            notebook_frame_create("First Frame"),
            notebook_tab_create("First Tab"));

    /*  create the second tab  */
    gtk_notebook_append_page (
            GTK_NOTEBOOK (notebook),
            notebook_frame_create("Second Frame"),
            notebook_tab_create("Second Tab"));

	/*  create the third tab  */
    gtk_notebook_append_page (
            GTK_NOTEBOOK (notebook),
            notebook_frame_create("Third Frame"),
            notebook_tab_create("Third Tab"));

    /*  Rotate to second tab  */
    gtk_notebook_set_current_page (GTK_NOTEBOOK (notebook), 1);
	
    /*  Show window and layout table  */
    gtk_widget_show (table);
    gtk_widget_show (window);
    gtk_main ();

    return 0;
}
