/*
 * This file is part of Maemo Examples
 *
 * Copyright (C) 2005 Nokia Corporation.
 *
 *
 * This software is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

#include <hildon-widgets/hildon-program.h>
#include <gtk/gtk.h>
#include <libosso.h>

#include <osso-helplib.h>

#define OSSO_EXAMPLE_NAME    "example_help_framework"

#define OSSO_HELP_TOPIC_EXAMPLE "osso_example_help"

/* Application UI data struct */
typedef struct {
    HildonProgram *program;
    HildonWindow *window;
    osso_context_t *osso_context;
} AppData;

AppData appdata;

/* Handlers */
void help_activated(GtkWidget *win, gchar *help_id)
{
    osso_return_t retval;

    if (!help_id) {
        return;
    }

    retval = ossohelp_show(appdata.osso_context, help_id, OSSO_HELP_SHOW_DIALOG);
}

void button_clicked(GtkWidget *widget, gpointer data) {
    GtkWidget *dialog;
    guint result;

    dialog = GTK_WIDGET(hildon_color_selector_new(GTK_WIDGET(appdata.window)));

    /* enable help system on dialog */
    ossohelp_dialog_help_enable(
        GTK_DIALOG(dialog),
        OSSO_HELP_TOPIC_EXAMPLE,
        appdata.osso_context
    );

    result = gtk_dialog_run(GTK_DIALOG(dialog));
    gtk_widget_destroy(dialog);
    return;
}

/* Main application */
int main(int argc, char *argv[])
{
    /* Create needed variables */
    GtkWidget *main_vbox;
    GtkWidget *button;
    GtkWidget *help_button;

    /* Initialize the GTK. */
    gtk_init(&argc, &argv);
    
    /* Initialize maemo application */
    appdata.osso_context = osso_initialize(OSSO_EXAMPLE_NAME, "0.0.1", TRUE, NULL);

    /* Check that initialization was ok */
    if (appdata.osso_context == NULL) {
        return OSSO_ERROR;
    }

    /* Create the hildon application and setup the title */
    appdata.program = HILDON_PROGRAM(hildon_program_get_instance());
    g_set_application_name("App Title");

    /* Create HildonWindow and set it to HildonProgram */
    appdata.window = HILDON_WINDOW(hildon_window_new());
    hildon_program_add_window(appdata.program, appdata.window);

    /* Add vbox to window */
    main_vbox = gtk_vbox_new(FALSE, 0);
    gtk_container_add(GTK_CONTAINER(appdata.window), main_vbox);

    /* Add button to vbox */
    button = gtk_button_new_with_label("Dialog");
    g_signal_connect(G_OBJECT(button), "clicked",
                     G_CALLBACK(button_clicked), NULL);
    gtk_box_pack_start(GTK_BOX(main_vbox), button, FALSE, TRUE, 0);
    
    /* Add help_button to vbox */
    help_button = gtk_button_new_with_label("Help");
    g_signal_connect(G_OBJECT(help_button), "clicked",
                     G_CALLBACK(help_activated), OSSO_HELP_TOPIC_EXAMPLE);
    gtk_box_pack_start(GTK_BOX(main_vbox), help_button, FALSE, TRUE, 0);

    /* Begin the main application */
    gtk_widget_show_all(GTK_WIDGET(appdata.window));

    /* Connect signal to X in the upper corner */
    g_signal_connect(G_OBJECT(appdata.window), "delete_event",
      G_CALLBACK(gtk_main_quit), NULL);

    gtk_main();

    /* Deinitialize OSSO */
    osso_deinitialize(appdata.osso_context);

    /* Exit */
    return 0;
}

/* vim:ts=4:sw=4:et:ai:si:showmatch
 */
