#include <gtk/gtk.h>

/* EvWindow */

struct _EvWindow {
    GtkApplicationWindow base_instance;
    GtkWidget *sidebar;
};

#define EV_TYPE_WINDOW (ev_window_get_type ())
G_DECLARE_FINAL_TYPE (EvWindow, ev_window, EV, WINDOW, GtkApplicationWindow);
G_DEFINE_TYPE (EvWindow, ev_window, GTK_TYPE_APPLICATION_WINDOW);

static void
ev_window_class_init (EvWindowClass *ev_window_class)
{
}

static void
ev_window_init (EvWindow *ev_window)
{
    GtkWidget *paned;
    GtkWidget *label1, *label2;

    paned = gtk_paned_new (GTK_ORIENTATION_HORIZONTAL);
    gtk_container_add (GTK_CONTAINER (ev_window), paned);
    gtk_widget_show (paned);

    ev_window->sidebar = gtk_scrolled_window_new (NULL, NULL);
    gtk_paned_pack1 (GTK_PANED (paned), ev_window->sidebar, FALSE, FALSE);
    /* sidebar is initially hidden */
    gtk_widget_hide (ev_window->sidebar);

    label1 = gtk_label_new ("Hello");
    gtk_container_add (GTK_CONTAINER (ev_window->sidebar), label1);
    gtk_widget_show (label1);

    label2 = gtk_label_new ("World");
    gtk_paned_pack2 (GTK_PANED (paned), label2, FALSE, FALSE);
    gtk_widget_show (label2);
}

GtkWidget *
ev_window_new ()
{
    return GTK_WIDGET (g_object_new (EV_TYPE_WINDOW,
		                     "type", GTK_WINDOW_TOPLEVEL,
				     "application", g_application_get_default (),
				     NULL));
}

/**
 * Called after file is loaded.  Show hidden sidebar.
 */
static gboolean
ev_window_load_cb (gpointer data)
{
    EvWindow *ev_window = EV_WINDOW (data);

    gtk_widget_show (ev_window->sidebar);
    return FALSE;
}

/**
 * Simuluate loading a file in background.  Callback will be called when load
 * is completed.
 */
void
ev_window_open_uri (EvWindow *ev_window)
{
    g_idle_add (ev_window_load_cb, ev_window);
}

/* Application */

void
activate (GtkApplication *application, gpointer data)
{
    GtkWidget *window;

    window = ev_window_new ();
    gtk_application_add_window (application, GTK_WINDOW (window));
    ev_window_open_uri (EV_WINDOW (window));
    gtk_window_present (GTK_WINDOW (window));
}

int
main (int argc, char *argv[])
{
    GtkApplication *application;

    application = gtk_application_new (NULL, G_APPLICATION_FLAGS_NONE);
    g_signal_connect (application, "activate", G_CALLBACK (activate), NULL);
    g_application_register (G_APPLICATION (application), NULL, NULL);
    return g_application_run (G_APPLICATION (application), argc, argv);
}
