Blame examples/menu/menu.c

Packit 98cdb6
Packit 98cdb6
#include <stdio.h>
Packit 98cdb6
#include <gtk/gtk.h>
Packit 98cdb6
Packit 98cdb6
static gboolean button_press (GtkWidget *, GdkEvent *);
Packit 98cdb6
static void menuitem_response (gchar *);
Packit 98cdb6
Packit 98cdb6
int main( int   argc,
Packit 98cdb6
          char *argv[] )
Packit 98cdb6
{
Packit 98cdb6
Packit 98cdb6
    GtkWidget *window;
Packit 98cdb6
    GtkWidget *menu;
Packit 98cdb6
    GtkWidget *menu_bar;
Packit 98cdb6
    GtkWidget *root_menu;
Packit 98cdb6
    GtkWidget *menu_items;
Packit 98cdb6
    GtkWidget *vbox;
Packit 98cdb6
    GtkWidget *button;
Packit 98cdb6
    char buf[128];
Packit 98cdb6
    int i;
Packit 98cdb6
Packit 98cdb6
    gtk_init (&argc, &argv);
Packit 98cdb6
Packit 98cdb6
    /* create a new window */
Packit 98cdb6
    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
Packit 98cdb6
    gtk_widget_set_size_request (GTK_WIDGET (window), 200, 100);
Packit 98cdb6
    gtk_window_set_title (GTK_WINDOW (window), "GTK Menu Test");
Packit 98cdb6
    g_signal_connect (window, "delete-event",
Packit 98cdb6
                      G_CALLBACK (gtk_main_quit), NULL);
Packit 98cdb6
Packit 98cdb6
    /* Init the menu-widget, and remember -- never
Packit 98cdb6
     * gtk_show_widget() the menu widget!!
Packit 98cdb6
     * This is the menu that holds the menu items, the one that
Packit 98cdb6
     * will pop up when you click on the "Root Menu" in the app */
Packit 98cdb6
    menu = gtk_menu_new ();
Packit 98cdb6
Packit 98cdb6
    /* Next we make a little loop that makes three menu-entries for "test-menu".
Packit 98cdb6
     * Notice the call to gtk_menu_shell_append.  Here we are adding a list of
Packit 98cdb6
     * menu items to our menu.  Normally, we'd also catch the "clicked"
Packit 98cdb6
     * signal on each of the menu items and setup a callback for it,
Packit 98cdb6
     * but it's omitted here to save space. */
Packit 98cdb6
Packit 98cdb6
    for (i = 0; i < 3; i++)
Packit 98cdb6
        {
Packit 98cdb6
            /* Copy the names to the buf. */
Packit 98cdb6
            sprintf (buf, "Test-undermenu - %d", i);
Packit 98cdb6
Packit 98cdb6
            /* Create a new menu-item with a name... */
Packit 98cdb6
            menu_items = gtk_menu_item_new_with_label (buf);
Packit 98cdb6
Packit 98cdb6
            /* ...and add it to the menu. */
Packit 98cdb6
            gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_items);
Packit 98cdb6
Packit 98cdb6
	    /* Do something interesting when the menuitem is selected */
Packit 98cdb6
	    g_signal_connect_swapped (menu_items, "activate",
Packit 98cdb6
		                      G_CALLBACK (menuitem_response),
Packit 98cdb6
                                      (gpointer) g_strdup (buf));
Packit 98cdb6
Packit 98cdb6
            /* Show the widget */
Packit 98cdb6
            gtk_widget_show (menu_items);
Packit 98cdb6
        }
Packit 98cdb6
Packit 98cdb6
    /* This is the root menu, and will be the label
Packit 98cdb6
     * displayed on the menu bar.  There won't be a signal handler attached,
Packit 98cdb6
     * as it only pops up the rest of the menu when pressed. */
Packit 98cdb6
    root_menu = gtk_menu_item_new_with_label ("Root Menu");
Packit 98cdb6
Packit 98cdb6
    gtk_widget_show (root_menu);
Packit 98cdb6
Packit 98cdb6
    /* Now we specify that we want our newly created "menu" to be the menu
Packit 98cdb6
     * for the "root menu" */
Packit 98cdb6
    gtk_menu_item_set_submenu (GTK_MENU_ITEM (root_menu), menu);
Packit 98cdb6
Packit 98cdb6
    /* A vbox to put a menu and a button in: */
Packit 98cdb6
    vbox = gtk_vbox_new (FALSE, 0);
Packit 98cdb6
    gtk_container_add (GTK_CONTAINER (window), vbox);
Packit 98cdb6
    gtk_widget_show (vbox);
Packit 98cdb6
Packit 98cdb6
    /* Create a menu-bar to hold the menus and add it to our main window */
Packit 98cdb6
    menu_bar = gtk_menu_bar_new ();
Packit 98cdb6
    gtk_box_pack_start (GTK_BOX (vbox), menu_bar, FALSE, FALSE, 2);
Packit 98cdb6
    gtk_widget_show (menu_bar);
Packit 98cdb6
Packit 98cdb6
    /* Create a button to which to attach menu as a popup */
Packit 98cdb6
    button = gtk_button_new_with_label ("press me");
Packit 98cdb6
    g_signal_connect_swapped (button, "event",
Packit 98cdb6
	                      G_CALLBACK (button_press),
Packit 98cdb6
                              menu);
Packit 98cdb6
    gtk_box_pack_end (GTK_BOX (vbox), button, TRUE, TRUE, 2);
Packit 98cdb6
    gtk_widget_show (button);
Packit 98cdb6
Packit 98cdb6
    /* And finally we append the menu-item to the menu-bar -- this is the
Packit 98cdb6
     * "root" menu-item I have been raving about =) */
Packit 98cdb6
    gtk_menu_shell_append (GTK_MENU_SHELL (menu_bar), root_menu);
Packit 98cdb6
Packit 98cdb6
    /* always display the window as the last step so it all splashes on
Packit 98cdb6
     * the screen at once. */
Packit 98cdb6
    gtk_widget_show (window);
Packit 98cdb6
Packit 98cdb6
    gtk_main ();
Packit 98cdb6
Packit 98cdb6
    return 0;
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
/* Respond to a button-press by posting a menu passed in as widget.
Packit 98cdb6
 *
Packit 98cdb6
 * Note that the "widget" argument is the menu being posted, NOT
Packit 98cdb6
 * the button that was pressed.
Packit 98cdb6
 */
Packit 98cdb6
Packit 98cdb6
static gboolean button_press( GtkWidget *widget,
Packit 98cdb6
                              GdkEvent *event )
Packit 98cdb6
{
Packit 98cdb6
Packit 98cdb6
    if (event->type == GDK_BUTTON_PRESS) {
Packit 98cdb6
        GdkEventButton *bevent = (GdkEventButton *) event;
Packit 98cdb6
        gtk_menu_popup (GTK_MENU (widget), NULL, NULL, NULL, NULL,
Packit 98cdb6
                        bevent->button, bevent->time);
Packit 98cdb6
        /* Tell calling code that we have handled this event; the buck
Packit 98cdb6
         * stops here. */
Packit 98cdb6
        return TRUE;
Packit 98cdb6
    }
Packit 98cdb6
Packit 98cdb6
    /* Tell calling code that we have not handled this event; pass it on. */
Packit 98cdb6
    return FALSE;
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
Packit 98cdb6
/* Print a string when a menu item is selected */
Packit 98cdb6
Packit 98cdb6
static void menuitem_response( gchar *string )
Packit 98cdb6
{
Packit 98cdb6
    printf ("%s\n", string);
Packit 98cdb6
}