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

static GtkWidget *window;
static GtkWidget *outer_vbox;
static GtkWidget *button_box;
static GtkWidget *hide_all_button;
static GtkWidget *show_1_button;
static GtkWidget *show_2_button;
static GtkWidget *frame;
static GtkWidget *vbox_1, *vbox_2;

static int shown_part = 1;

static void
destroy (void)
{
    gtk_widget_unref (vbox_1);
    gtk_widget_unref (vbox_2);
    gtk_exit (0);
}

static void
hide_all (void)
{
    switch (shown_part) {
        case 1:
            gtk_container_remove (GTK_CONTAINER (frame), vbox_1);
            break;

        case 2:
            gtk_container_remove (GTK_CONTAINER (frame), vbox_2);
            break;
    }
    shown_part = 0;
}

static void
show_1 (void)
{
    if (shown_part != 1) {
        hide_all ();
        shown_part = 1;
        gtk_container_add (GTK_CONTAINER (frame), vbox_1);
    }
}

static void
show_2 (void)
{
    if (shown_part != 2) {
        hide_all ();
        shown_part = 2;
        gtk_container_add (GTK_CONTAINER (frame), vbox_2);
    }
}

static GtkWidget *
create_text_area (const gchar *label_text)
{
    static const gchar init_text[] =
        "\n\nThe quick brown fox jumps over the lazy dog.";
    GtkWidget *vbox, *entry, *scrolled_window, *text;
    GtkScrolledWindow *sw;

    vbox = gtk_vbox_new (FALSE, 8);
    gtk_container_border_width (GTK_CONTAINER (vbox), 8);

    entry = gtk_entry_new ();
    gtk_entry_set_text (GTK_ENTRY (entry), label_text);
    gtk_box_pack_start (GTK_BOX (vbox), entry, FALSE, FALSE, 0);
    gtk_widget_show (entry);

    scrolled_window = gtk_scrolled_window_new (NULL, NULL);

    sw = GTK_SCROLLED_WINDOW (scrolled_window);
    text = gtk_text_new (gtk_scrolled_window_get_hadjustment (sw),
                         gtk_scrolled_window_get_vadjustment (sw));
    gtk_text_insert (GTK_TEXT (text), NULL, NULL, NULL,
                     label_text, strlen (label_text));
    gtk_text_insert (GTK_TEXT (text), NULL, NULL, NULL,
                     init_text, sizeof (init_text) - 1);
    gtk_container_add (GTK_CONTAINER (scrolled_window), text);
    gtk_widget_show (text);

    gtk_box_pack_start (GTK_BOX (vbox), scrolled_window, TRUE, TRUE, 0);
    gtk_widget_show (scrolled_window);

    gtk_widget_show (vbox);
    return vbox;
}

static void
create_test_selection_window (void)
{
    /* Create constant part of the layout */
    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_signal_connect (GTK_OBJECT (window), "destroy",
                        GTK_SIGNAL_FUNC (destroy), NULL);
    gtk_container_border_width (GTK_CONTAINER (window), 8);

    outer_vbox = gtk_vbox_new (FALSE, 8);

    button_box = gtk_hbutton_box_new ();

    show_1_button = gtk_button_new_with_label ("Show text 1");
    gtk_signal_connect (GTK_OBJECT (show_1_button), "clicked",
                        GTK_SIGNAL_FUNC (show_1), NULL);
    gtk_container_add (GTK_CONTAINER (button_box), show_1_button);
    gtk_widget_show (show_1_button);

    show_2_button = gtk_button_new_with_label ("Show text 2");
    gtk_signal_connect (GTK_OBJECT (show_2_button), "clicked",
                        GTK_SIGNAL_FUNC (show_2), NULL);
    gtk_container_add (GTK_CONTAINER (button_box), show_2_button);
    gtk_widget_show (show_2_button);

    hide_all_button = gtk_button_new_with_label ("Hide all");
    gtk_signal_connect (GTK_OBJECT (hide_all_button), "clicked",
                        GTK_SIGNAL_FUNC (hide_all), NULL);
    gtk_container_add (GTK_CONTAINER (button_box), hide_all_button);
    gtk_widget_show (hide_all_button);

    gtk_box_pack_start (GTK_BOX (outer_vbox), button_box, FALSE, FALSE, 0);
    gtk_widget_show (button_box);

    frame = gtk_frame_new ("Edit area");
    gtk_box_pack_start (GTK_BOX (outer_vbox), frame, TRUE, TRUE, 0);
    gtk_widget_show (frame);

    gtk_container_add (GTK_CONTAINER (window), outer_vbox);
    gtk_widget_show (outer_vbox);

    /* Create the first text area */
    vbox_1 = create_text_area ("This is text 1");
    gtk_widget_ref (vbox_1);

    /* Create the second text area */
    vbox_2 = create_text_area ("This is text 2");
    gtk_widget_ref (vbox_2);

    /* Insert the first text area into the frame */
    gtk_container_add (GTK_CONTAINER (frame), vbox_1);

    /* Show the window */
    gtk_widget_show (window);
}

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

    create_test_selection_window ();

    gtk_main ();

    return 0;
}

/* vim: set sw=4 smarttab expandtab cinoptions=(0 : */
