/* example-start helloworld helloworld.c */

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

/* This is a callback function. The data arguments are ignored
 * in this example. More on callbacks below. */
void hello( GtkWidget *widget, gpointer   data )
{
  g_print ("Hello World\n");
}

gint delete_event( GtkWidget *widget,
                          GdkEvent  *event,
                          gpointer   data )
{
 /* If you return FALSE in the "delete_event" signal handler,
  * GTK will emit the "destroy" signal. Returning TRUE means
  * you don't want the window to be destroyed.
  * This is useful for popping up 'are you sure you want to quit?'
  * type dialogs. */

  g_print ("delete event occurred\n");

 /* Change TRUE to FALSE and the main window will be destroyed with
  * a "delete_event". */

  return(TRUE);
}

/* Another callback */
void destroy( GtkWidget *widget, gpointer   data )
{
  gtk_main_quit();
}

char *events [] = {
  "GDK_DELETE", //            = 0,
  "GDK_DESTROY", //           = 1,
  "GDK_EXPOSE", //            = 2,
  "GDK_MOTION_NOTIFY", //     = 3,
  "GDK_BUTTON_PRESS", //      = 4,
  "GDK_2BUTTON_PRESS", //     = 5,
  "GDK_3BUTTON_PRESS", //     = 6,
  "GDK_BUTTON_RELEASE", //    = 7,
  "GDK_KEY_PRESS", //         = 8,
  "GDK_KEY_RELEASE", //       = 9,
  "GDK_ENTER_NOTIFY", //      = 10,
  "GDK_LEAVE_NOTIFY", //      = 11,
  "GDK_FOCUS_CHANGE", //      = 12,
  "GDK_CONFIGURE", //         = 13,
  "GDK_MAP", //               = 14,
  "GDK_UNMAP", //             = 15,
  "GDK_PROPERTY_NOTIFY", //   = 16,
  "GDK_SELECTION_CLEAR", //   = 17,
  "GDK_SELECTION_REQUEST", // = 18,
  "GDK_SELECTION_NOTIFY", //  = 19,
  "GDK_PROXIMITY_IN", //      = 20,
  "GDK_PROXIMITY_OUT", //     = 21,
  "GDK_DRAG_ENTER", //        = 22,
  "GDK_DRAG_LEAVE", //        = 23,
  "GDK_DRAG_MOTION", //       = 24,
  "GDK_DRAG_STATUS", //       = 25,
  "GDK_DROP_START", //        = 26,
  "GDK_DROP_FINISHED", //     = 27,
  "GDK_CLIENT_EVENT", //      = 28,
  "GDK_VISIBILITY_NOTIFY", // = 29,
  "GDK_NO_EXPOSE", //         = 30
};


gint genSignal ( GtkWidget *widget, GdkEvent *event, gpointer data)
{
  g_print ("Messaggio %s\n",events [event -> type]);
  return FALSE;
}

/* Print a string when a menu item is selected */

static void menuitem_response( gchar *string )
{
  printf ("%s\n", string);
}

int main( int   argc, char *argv[] )
{
 /* GtkWidget is the storage type for widgets */
GtkWidget *window;
GtkWidget *button;
GtkWidget *menu;
GtkWidget *menu_bar;
GtkWidget *root_menu;
GtkWidget *menu_items;
GtkWidget *vbox;
char buf[128];
int i;

  /* This is called in all GTK applications. Arguments are parsed
   * from the command line and are returned to the application. */
  gtk_init(&argc, &argv);
          
  /* create a new window */
  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);

  gtk_widget_set_usize (GTK_WIDGET (window), 200, 100);
  gtk_window_set_title (GTK_WINDOW (window), "GTK Menu Test");
           
  /* When the window is given the "delete_event" signal (this is given
   * by the window manager, usually by the "close" option, or on the
   * titlebar), we ask it to call the delete_event () function
   * as defined above. The data passed to the callback
   * function is NULL and is ignored in the callback function. */
  gtk_signal_connect (GTK_OBJECT (window), "delete_event",
    GTK_SIGNAL_FUNC (delete_event), NULL);
           
  /* Here we connect the "destroy" event to a signal handler.  
   * This event occurs when we call gtk_widget_destroy() on the window,
   * or if we return FALSE in the "delete_event" callback. */
  gtk_signal_connect (GTK_OBJECT (window), "destroy",
    GTK_SIGNAL_FUNC (destroy), NULL);

  gtk_signal_connect (GTK_OBJECT (window), "event",
    GTK_SIGNAL_FUNC (genSignal), NULL);

  /* Init the menu-widget, and remember -- never
   * gtk_show_widget() the menu widget!! 
   * This is the menu that holds the menu items, the one that
   * will pop up when you click on the "Root Menu" in the app */
  menu = gtk_menu_new ();

  /* Next we make a little loop that makes three menu-entries for "test-menu".
   * Notice the call to gtk_menu_append.  Here we are adding a list of
   * menu items to our menu.  Normally, we'd also catch the "clicked"
   * signal on each of the menu items and setup a callback for it,
   * but it's omitted here to save space. */

  for (i = 0; i < 3; i++)
  {
    /* Copy the names to the buf. */
    sprintf (buf, "Test-undermenu - %d", i);

    /* Create a new menu-item with a name... */
    menu_items = gtk_menu_item_new_with_label (buf);

    /* ...and add it to the menu. */
    gtk_menu_append (GTK_MENU (menu), menu_items);

    /* Do something interesting when the menuitem is selected */
    gtk_signal_connect_object (GTK_OBJECT (menu_items), "activate",
    GTK_SIGNAL_FUNC (menuitem_response), (gpointer) g_strdup (buf));

    /* Show the widget */
    gtk_widget_show (menu_items);
  }

  /* This is the root menu, and will be the label
   * displayed on the menu bar.  There won't be a signal handler attached,
   * as it only pops up the rest of the menu when pressed. */
  root_menu = gtk_menu_item_new_with_label ("Root Menu");

  gtk_widget_show (root_menu);

  /* Now we specify that we want our newly created "menu" to be the menu
   * for the "root menu" */
  gtk_menu_item_set_submenu (GTK_MENU_ITEM (root_menu), menu);

  /* A vbox to put a menu and a button in: */
  vbox = gtk_vbox_new (FALSE, 0);
  gtk_container_add (GTK_CONTAINER (window), vbox);
  gtk_widget_show (vbox);

  /* Create a menu-bar to hold the menus and add it to our main window */
  menu_bar = gtk_menu_bar_new ();
  gtk_box_pack_start (GTK_BOX (vbox), menu_bar, FALSE, FALSE, 2);
  gtk_widget_show (menu_bar);

  /* Create a button to which to attach menu as a popup */
  button = gtk_button_new_with_label ("press me");
           
  /* When the button receives the "clicked" signal, it will call the
   * function hello() passing it NULL as its argument.  The hello()
   * function is defined above. */
  gtk_signal_connect (GTK_OBJECT (button), "clicked",
    GTK_SIGNAL_FUNC (hello), NULL);
           
  /* This will cause the window to be destroyed by calling
   * gtk_widget_destroy(window) when "clicked".  Again, the destroy
   * signal could come from here, or the window manager. */
  gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
    GTK_SIGNAL_FUNC (gtk_widget_destroy),
      GTK_OBJECT (window));


  gtk_box_pack_end (GTK_BOX (vbox), button, TRUE, TRUE, 2);
  gtk_widget_show (button);

  /* And finally we append the menu-item to the menu-bar -- this is the
   * "root" menu-item I have been raving about =) */
  gtk_menu_bar_append (GTK_MENU_BAR (menu_bar), root_menu);


  /* This packs the button into the window (a gtk container). */
  gtk_container_add (GTK_CONTAINER (window), vbox);
           
  /* and the window */
  gtk_widget_show (window);
           
  gtk_widget_show (vbox);

  /* All GTK applications must have a gtk_main(). Control ends here
   * and waits for an event to occur (like a key press or
   * mouse event). */
  gtk_main ();
           
  return(0);
}