#include <gtkembedmoz/gtkmozembed.h>

static GtkWidget *address_bar = NULL;
static GtkWidget *address_entry = NULL;
static GtkWidget *action_bar = NULL;
static GtkWidget *toolbox = NULL;
static GtkWidget *browser = NULL;
static GtkWidget *status = NULL;

static GtkToolItem *address_item = NULL;
static GtkToolItem *refresh = NULL;
static GtkToolItem *stop = NULL;

static void address_entry_activate(GtkEntry *entry)
{
    const gchar *url = gtk_entry_get_text(entry);
    gtk_moz_embed_load_url(GTK_MOZ_EMBED(browser), url);
}

static void browser_location(GtkMozEmbed *browser)
{
    gchar *url = gtk_moz_embed_get_location(browser);
    gtk_entry_set_text(GTK_ENTRY(address_entry), url);
    g_free(url);
}

static void browser_net_start()
{
    gtk_widget_set_sensitive(GTK_WIDGET(refresh), FALSE);
    gtk_widget_set_sensitive(GTK_WIDGET(stop), TRUE);

    gtk_statusbar_pop(GTK_STATUSBAR(status), 0);

    gchar *url = gtk_moz_embed_get_location(GTK_MOZ_EMBED(browser));
    gchar *message = g_strdup_printf("Loading %s...", url);
    g_free(url);

    gtk_statusbar_push(GTK_STATUSBAR(status), 0, message);
    g_free(message);
}

static void browser_net_stop()
{
    gtk_widget_set_sensitive(GTK_WIDGET(refresh), TRUE);
    gtk_widget_set_sensitive(GTK_WIDGET(stop), FALSE);
    gtk_statusbar_pop(GTK_STATUSBAR(status), 0);
}

static void bookmark_clicked(const gchar *url)
{
    gtk_moz_embed_load_url(GTK_MOZ_EMBED(browser), url);
}

static void size_allocate()
{
    int i = -1, width = 0;
    GtkRequisition req;
    GtkToolItem *item;

    gtk_widget_style_get(action_bar, "internal-padding", &width, NULL);

    while(NULL != (item = gtk_toolbar_get_nth_item(
          GTK_TOOLBAR(action_bar), ++i))) {
        gtk_widget_size_request(GTK_WIDGET(item), &req);

        if (item != address_item)
            width += req.width;
    }

    gtk_widget_size_request(address_bar, &req);

    if (GTK_IS_BOX(address_bar->parent)) {
        if (action_bar->allocation.width - width >= req.width) {
            gtk_widget_reparent(address_bar, GTK_WIDGET(address_item));
        }
    } else {
        if (action_bar->allocation.width < width + req.width) {
            gtk_widget_reparent(address_bar, toolbox);
            gtk_box_reorder_child(GTK_BOX(toolbox), address_bar, 1);
        }
    }
}

static GtkWidget* setup_toolbar()
{
    GtkToolItem *item;

    address_bar = gtk_toolbar_new();
    gtk_toolbar_set_show_arrow(GTK_TOOLBAR(address_bar), FALSE);

    address_entry = gtk_entry_new();
    gtk_widget_set_size_request(address_entry, 300, -1);

    g_signal_connect(address_entry, "activate",
            G_CALLBACK(address_entry_activate),
            NULL);

    item = gtk_tool_item_new();
    gtk_container_add(GTK_CONTAINER(item), address_entry);
    gtk_toolbar_insert(GTK_TOOLBAR(address_bar), item, -1);
    gtk_tool_item_set_expand(item, TRUE);

    item = gtk_tool_button_new(NULL, "Goto");
    gtk_toolbar_insert(GTK_TOOLBAR(address_bar), item, -1);

    g_signal_connect_swapped(item, "clicked",
            G_CALLBACK(address_entry_activate),
            address_entry);

    action_bar = gtk_toolbar_new();
    GtkIconSize s = gtk_toolbar_get_icon_size(GTK_TOOLBAR(action_bar));

    item = gtk_menu_tool_button_new_from_stock(GTK_STOCK_GO_BACK);
    gtk_toolbar_insert(GTK_TOOLBAR(action_bar), item, -1);
    
    item = gtk_menu_tool_button_new_from_stock(GTK_STOCK_GO_FORWARD);
    gtk_toolbar_insert(GTK_TOOLBAR(action_bar), item, -1);

    stop = gtk_tool_button_new_from_stock(GTK_STOCK_STOP);
    gtk_toolbar_insert(GTK_TOOLBAR(action_bar), stop, -1);

    g_signal_connect_swapped(stop, "clicked",
            G_CALLBACK(gtk_moz_embed_stop_load), 
            browser);

    refresh = gtk_tool_button_new_from_stock(GTK_STOCK_REFRESH);
    gtk_toolbar_insert(GTK_TOOLBAR(action_bar), refresh, -1);

    item = gtk_separator_tool_item_new();
    gtk_toolbar_insert(GTK_TOOLBAR(action_bar), item, -1);

    item = gtk_tool_button_new_from_stock(GTK_STOCK_HOME);
    gtk_toolbar_insert(GTK_TOOLBAR(action_bar), item, -1);

    item = gtk_tool_button_new(
        gtk_image_new_from_icon_name("emblem-favorite", s),
        NULL);
    gtk_toolbar_insert(GTK_TOOLBAR(action_bar), item, -1);

    item = gtk_tool_button_new(
        gtk_image_new_from_icon_name("epiphany-history", s),
        NULL);
    gtk_toolbar_insert(GTK_TOOLBAR(action_bar), item, -1);

    item = gtk_separator_tool_item_new();
    gtk_toolbar_insert(GTK_TOOLBAR(action_bar), item, -1);

    item = gtk_tool_button_new_from_stock(GTK_STOCK_ZOOM_IN);
    gtk_toolbar_insert(GTK_TOOLBAR(action_bar), item, -1);

    item = gtk_tool_button_new_from_stock(GTK_STOCK_ZOOM_OUT);
    gtk_toolbar_insert(GTK_TOOLBAR(action_bar), item, -1);

    address_item = gtk_tool_item_new();
    gtk_tool_item_set_expand(address_item, TRUE);
    gtk_toolbar_insert(GTK_TOOLBAR(action_bar), address_item, -1);

    item = gtk_tool_button_new(
        gtk_image_new_from_icon_name("gnome-logo-icon-transparent", s),
        NULL);
    gtk_toolbar_insert(GTK_TOOLBAR(action_bar), item, -1);

    g_signal_connect_swapped(item, "clicked", 
            G_CALLBACK(bookmark_clicked), 
            "http://www.gnome.org/projects/epiphany/");

    toolbox = gtk_vbox_new(FALSE, 0);
    gtk_box_pack_start(GTK_BOX(toolbox), action_bar, TRUE, TRUE, 0);
    gtk_box_pack_start(GTK_BOX(toolbox), address_bar, FALSE, TRUE, 0);

    item = gtk_tool_item_new();
    gtk_container_add(GTK_CONTAINER(item), toolbox);
    gtk_tool_item_set_expand(item, TRUE);

    GtkWidget *t = gtk_toolbar_new();
    gtk_toolbar_set_show_arrow(GTK_TOOLBAR(t), FALSE);
    gtk_toolbar_insert(GTK_TOOLBAR(t), item, 0);

    return t;
}

static GtkWidget* setup_mockup()
{
    static const gchar *menu_items[] = {
        "_File", "_Edit", "_View", "_Goto", "_Bookmarks", 
        "_Tools", "T_abs", "_Help", NULL
    };

    static const gchar *bookmarks[] = {
        "Planet GNOME", "http://planet.gnome.org/",
        "Slashdot", "http://www.slashdot.org/",
        "Google", "http://www.google.com/",
        "eBay", "http://my.ebay.com/",
        NULL
    };

    GtkWidget *m = gtk_menu_bar_new();

    for(const gchar **l = menu_items; *l; ++l) {
        GtkWidget *i = gtk_menu_item_new_with_mnemonic(*l);
        gtk_container_add(GTK_CONTAINER(m), i);
    }

    browser = gtk_moz_embed_new();
    g_signal_connect(browser, "location", G_CALLBACK(browser_location), NULL);
    g_signal_connect(browser, "net-start", G_CALLBACK(browser_net_start), NULL);
    g_signal_connect(browser, "net-stop", G_CALLBACK(browser_net_stop), NULL);
    gtk_moz_embed_load_url(GTK_MOZ_EMBED(browser), "about:");

    GtkWidget *t = setup_toolbar();

    GtkWidget *b = gtk_toolbar_new();
    
    for(const gchar **l = bookmarks; *l; l += 2) {
        GtkToolItem *i = gtk_tool_button_new(NULL, l[0]);
        g_signal_connect_swapped(i, "clicked",
                G_CALLBACK(bookmark_clicked),
                (gpointer)l[1]);
        gtk_toolbar_insert(GTK_TOOLBAR(b), i, -1);
    }

    status = gtk_statusbar_new();
    gtk_statusbar_push(GTK_STATUSBAR(status), 0, "Hello World...");

    GtkWidget *v = gtk_vbox_new(FALSE, 0);
    gtk_box_pack_start(GTK_BOX(v), m, FALSE, TRUE, 0);
    gtk_box_pack_start(GTK_BOX(v), t, FALSE, TRUE, 0);
    gtk_box_pack_start(GTK_BOX(v), b, FALSE, TRUE, 0);
    gtk_box_pack_start(GTK_BOX(v), browser, TRUE, TRUE, 0);
    gtk_box_pack_end(GTK_BOX(v), status, FALSE, TRUE, 0);
    gtk_widget_show_all(v);

    GtkWidget *w = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_icon_name(GTK_WINDOW(w), "web-browser");
    gtk_window_set_title(GTK_WINDOW(w), "Dynamic Toolbar Mockup");
    gtk_window_set_default_size(GTK_WINDOW(w), 600, 400);
    g_signal_connect(w, "size_allocate", G_CALLBACK(size_allocate), NULL);
    g_signal_connect(w, "delete-event", G_CALLBACK(gtk_main_quit), NULL);
    gtk_container_add(GTK_CONTAINER(w), v);

    return w;
}

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

    gtk_rc_parse_string("                               \
        style \"nested-toolbar\" {                      \
            GtkToolbar::shadow-type = none              \
        }                                               \
                                                        \
        widget_class \"*<GtkToolbar>*GtkToolbar\"       \
        style \"nested-toolbar\"                        \
    ");

    gtk_icon_theme_append_search_path(
            gtk_icon_theme_get_default(),
            EPIPHANY_DATADIR "/art");
    
    gtk_widget_show(setup_mockup());
    gtk_main();

    return 0;
}

/* vim: set sw=4 sta et: */
