/**
 * hildon_helloworld-4.c
 *
 * This maemo code example is licensed under a MIT-style license,
 * that can be found in the file called "License" in the same
 * directory as this file.
 * Copyright (c) 2007-2008 Nokia Corporation. All rights reserved.
 *
 * Adds a toolbar and a color chooser.
 *
 * Look for lines with "NEW" or "MODIFIED" in them.
 */

#include <stdlib.h>
#include <gtk/gtk.h>
#include <hildon/hildon-program.h>
/* New Hildon widget. */
#include <hildon/hildon-color-button.h>

  /*... Listing cut for brevity ...*/

/**
 * NEW
 *
 * Invoked by "clicked" signal from Hildon Color Button when the user
 * closes the dialog (even when user closes the dialog via cancel).
 * This is because the "clicked" is sent by the GtkButton-code, so it
 * will not know that the color hasn't actually changed at all.
 */
static void cbColorChanged(HildonColorButton* colorButton,
                           gpointer dummy) {
  /* Initialize one GdkColor structure on the stack with zero. */
  GdkColor color = {};

  /* Get the new color information from the button and print it out.
     Note that colors are represented as 16-bit unsigned integers in
     GDK. */
  hildon_color_button_get_color(colorButton, &color);
  g_print("Got new color: r=%d g=%d b=%d\n", color.red, color.green,
          color.blue);
}

/**
 * NEW
 *
 * Utility function that will create the toolbar for us.
 */
static GtkWidget* buildToolbar(void) {

  GtkToolbar* toolbar;
  /* We use GtkToolItems here since that is the type that is returned
     by the gtk_tool_* functions. */
  GtkToolItem* tbOpen;
  GtkToolItem* tbSave;
  GtkToolItem* tbSep;
  GtkToolItem* tbFind;
  /* We'll use one Hildon-widget in this example. */
  GtkWidget*   colorButton;
  /* Since we cannot just put GtkWidgets into the toolbar we'll need
     to create a holder (GtkToolItem) for the button. */
  GtkToolItem* tbColorButton;

  /* GTK+ includes a facility using which we may reuse existing icons
     in our buttons and other widgets (that support icons). These are
     called "stock items" and a referenced via defines in GTK+.

     Note also that gtk_tool_button_new_from_stock is a convenience
     function that will create a GtkToolItem and add a widget into it
     (in this case a GtkButton which will contain a GtkImage). */
  tbOpen = gtk_tool_button_new_from_stock(GTK_STOCK_OPEN);
  tbSave = gtk_tool_button_new_from_stock(GTK_STOCK_SAVE);
  /* Separator between the file operations and others. */
  tbSep  = gtk_separator_tool_item_new();
  tbFind = gtk_tool_button_new_from_stock(GTK_STOCK_FIND);
  /* Create a ToolItem and put the color button inside of it.*/
  tbColorButton = gtk_tool_item_new();
  colorButton = hildon_color_button_new();
  gtk_container_add(GTK_CONTAINER(tbColorButton), colorButton);

  /* Create the toolbar. */
  toolbar = GTK_TOOLBAR(gtk_toolbar_new());

  /* Add the tool items to the toolbar.
     NOTE:
       We could use gtk_container_add as well, but we'd need to use
       even more casting macros, so we'll use this API function
       instead. */
  gtk_toolbar_insert(toolbar, tbOpen, -1);
  gtk_toolbar_insert(toolbar, tbSave, -1);
  gtk_toolbar_insert(toolbar, tbSep,  -1);
  gtk_toolbar_insert(toolbar, tbFind, -1);
  gtk_toolbar_insert(toolbar, tbColorButton, -1);

  /* Connect the signals from the tool items.
     NOTE:
       We are a bit evil here since we assume that the callback
       function will not assume or use the widget pointer that is
       passed with these events. In our callback we don't use it for
       open/save so it's safe in this case. */
  g_signal_connect(G_OBJECT(tbOpen), "clicked",
                   G_CALLBACK(cbActivation),
                   GINT_TO_POINTER(MENU_FILE_OPEN));
  g_signal_connect(G_OBJECT(tbSave), "clicked",
                   G_CALLBACK(cbActivation),
                   GINT_TO_POINTER(MENU_FILE_SAVE));

  /* We also connect a signal from the colorbutton when a user has
     selected a color. Note that we connect the button itself, not
     the toolitem that is holding it inside toolbar (it doesn't emit
     signals). */
  g_signal_connect(G_OBJECT(colorButton), "clicked",
                   G_CALLBACK(cbColorChanged), NULL);

  /* Return the toolbar as a GtkWidget*. */
  return GTK_WIDGET(toolbar);
}

  /*... Listing cut for brevity ...*/

/**
 * MODIFIED
 *
 * We use the toolbar creation code and add it to our program.
 */
int main(int argc, char** argv) {

  HildonProgram* program;
  HildonWindow* window;
  GtkWidget* label;
  GtkWidget* vbox;

  /* Initialize the GTK+. */
  gtk_init(&argc, &argv);

  /* Create the Hildon program. */
  program = HILDON_PROGRAM(hildon_program_get_instance());
  /* Set the application title using an accessor function. */
  g_set_application_name("Hello Hildon!");
  /* Create a window that will handle our layout and menu. */
  window = HILDON_WINDOW(hildon_window_new());
  /* Bind the HildonWindow to HildonProgram. */
  hildon_program_add_window(program, HILDON_WINDOW(window));

  /* Create the label widget. */
  label = gtk_label_new("Hello Hildon (with toolbar)!");

  /* Build the menu and attach it to the HildonProgram. */
  buildMenu(program);

  /* Create a layout box for the window. */
  vbox = gtk_vbox_new(FALSE, 0);

  /* Add the vbox as a child to the Window. */
  gtk_container_add(GTK_CONTAINER(window), vbox);

  /* Pack the label into the VBox. */
  gtk_box_pack_end(GTK_BOX(vbox), GTK_WIDGET(label), TRUE, TRUE, 0);

  /* Add the toolbar to the Hildon window (NEW). */
  hildon_window_add_toolbar(HILDON_WINDOW(window),
                            GTK_TOOLBAR(buildToolbar()));

  /* Connect the termination signals. */
  g_signal_connect(G_OBJECT(window), "delete-event",
                   G_CALLBACK(delete_event), NULL);
  g_signal_connect(G_OBJECT(window), "destroy",
                   G_CALLBACK(end_program), NULL);

  /* Show all widgets that are contained by the Window. */
  gtk_widget_show_all(GTK_WIDGET(window));

  /* Start the main event loop. */
  g_print("main: calling gtk_main\n");
  gtk_main();

  g_print("main: returned from gtk_main and exiting with success\n");

  return EXIT_SUCCESS;
}
