2000-10-18 15:50:13 +00:00
|
|
|
/* Menus
|
|
|
|
*
|
2001-04-18 18:09:18 +00:00
|
|
|
* There are several widgets involved in displaying menus. The
|
|
|
|
* GtkMenuBar widget is a horizontal menu bar, which normally appears
|
|
|
|
* at the top of an application. The GtkMenu widget is the actual menu
|
|
|
|
* that pops up. Both GtkMenuBar and GtkMenu are subclasses of
|
|
|
|
* GtkMenuShell; a GtkMenuShell contains menu items
|
|
|
|
* (GtkMenuItem). Each menu item contains text and/or images and can
|
|
|
|
* be selected by the user.
|
|
|
|
*
|
|
|
|
* There are several kinds of menu item, including plain GtkMenuItem,
|
|
|
|
* GtkCheckMenuItem which can be checked/unchecked, GtkRadioMenuItem
|
|
|
|
* which is a check menu item that's in a mutually exclusive group,
|
|
|
|
* GtkSeparatorMenuItem which is a separator bar, GtkTearoffMenuItem
|
|
|
|
* which allows a GtkMenu to be torn off, and GtkImageMenuItem which
|
|
|
|
* can place a GtkImage or other widget next to the menu text.
|
|
|
|
*
|
|
|
|
* A GtkMenuItem can have a submenu, which is simply a GtkMenu to pop
|
|
|
|
* up when the menu item is selected. Typically, all menu items in a menu bar
|
|
|
|
* have submenus.
|
|
|
|
*
|
2003-10-10 21:04:30 +00:00
|
|
|
* GtkUIManager provides a higher-level interface for creating menu bars
|
2001-04-18 18:09:18 +00:00
|
|
|
* and menus; while you can construct menus manually, most people don't
|
2003-10-10 21:04:30 +00:00
|
|
|
* do that. There's a separate demo for GtkUIManager.
|
2001-04-18 18:09:18 +00:00
|
|
|
*
|
2000-10-18 15:50:13 +00:00
|
|
|
*/
|
|
|
|
|
2004-03-06 03:38:59 +00:00
|
|
|
#include <config.h>
|
2000-10-18 15:50:13 +00:00
|
|
|
#include <gtk/gtk.h>
|
|
|
|
#include <gdk/gdkkeysyms.h>
|
|
|
|
|
2001-05-18 16:28:30 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
static GtkWidget *
|
|
|
|
create_menu (gint depth,
|
|
|
|
gboolean tearoff)
|
2000-10-18 15:50:13 +00:00
|
|
|
{
|
|
|
|
GtkWidget *menu;
|
|
|
|
GtkWidget *menuitem;
|
|
|
|
GSList *group;
|
|
|
|
char buf[32];
|
|
|
|
int i, j;
|
|
|
|
|
|
|
|
if (depth < 1)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
menu = gtk_menu_new ();
|
|
|
|
group = NULL;
|
|
|
|
|
|
|
|
if (tearoff)
|
|
|
|
{
|
|
|
|
menuitem = gtk_tearoff_menu_item_new ();
|
2000-11-09 12:15:40 +00:00
|
|
|
gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
|
2000-10-18 15:50:13 +00:00
|
|
|
gtk_widget_show (menuitem);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0, j = 1; i < 5; i++, j++)
|
|
|
|
{
|
|
|
|
sprintf (buf, "item %2d - %d", depth, j);
|
|
|
|
menuitem = gtk_radio_menu_item_new_with_label (group, buf);
|
2001-08-17 14:11:36 +00:00
|
|
|
group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (menuitem));
|
2001-04-18 18:09:18 +00:00
|
|
|
|
2000-11-09 12:15:40 +00:00
|
|
|
gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
|
2000-10-18 15:50:13 +00:00
|
|
|
gtk_widget_show (menuitem);
|
|
|
|
if (i == 3)
|
|
|
|
gtk_widget_set_sensitive (menuitem, FALSE);
|
|
|
|
|
|
|
|
gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), create_menu (depth - 1, TRUE));
|
|
|
|
}
|
|
|
|
|
|
|
|
return menu;
|
|
|
|
}
|
|
|
|
|
2000-11-18 23:59:30 +00:00
|
|
|
GtkWidget *
|
2003-11-08 22:08:05 +00:00
|
|
|
do_menus (GtkWidget *do_widget)
|
2000-10-18 15:50:13 +00:00
|
|
|
{
|
|
|
|
static GtkWidget *window = NULL;
|
|
|
|
GtkWidget *box1;
|
|
|
|
GtkWidget *box2;
|
|
|
|
GtkWidget *button;
|
|
|
|
|
|
|
|
if (!window)
|
|
|
|
{
|
|
|
|
GtkWidget *menubar;
|
|
|
|
GtkWidget *menu;
|
|
|
|
GtkWidget *menuitem;
|
|
|
|
GtkAccelGroup *accel_group;
|
|
|
|
|
|
|
|
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
|
2003-11-08 22:08:05 +00:00
|
|
|
gtk_window_set_screen (GTK_WINDOW (window),
|
|
|
|
gtk_widget_get_screen (do_widget));
|
2001-10-20 23:39:32 +00:00
|
|
|
g_signal_connect (window, "destroy",
|
|
|
|
G_CALLBACK(gtk_widget_destroyed), &window);
|
|
|
|
g_signal_connect (window, "delete-event",
|
|
|
|
G_CALLBACK (gtk_true), NULL);
|
2000-10-18 15:50:13 +00:00
|
|
|
|
|
|
|
accel_group = gtk_accel_group_new ();
|
2001-11-16 11:50:04 +00:00
|
|
|
gtk_window_add_accel_group (GTK_WINDOW (window), accel_group);
|
2000-10-18 15:50:13 +00:00
|
|
|
|
|
|
|
gtk_window_set_title (GTK_WINDOW (window), "menus");
|
|
|
|
gtk_container_set_border_width (GTK_CONTAINER (window), 0);
|
|
|
|
|
|
|
|
|
|
|
|
box1 = gtk_vbox_new (FALSE, 0);
|
|
|
|
gtk_container_add (GTK_CONTAINER (window), box1);
|
|
|
|
gtk_widget_show (box1);
|
|
|
|
|
|
|
|
menubar = gtk_menu_bar_new ();
|
|
|
|
gtk_box_pack_start (GTK_BOX (box1), menubar, FALSE, TRUE, 0);
|
|
|
|
gtk_widget_show (menubar);
|
|
|
|
|
|
|
|
menu = create_menu (2, TRUE);
|
|
|
|
|
|
|
|
menuitem = gtk_menu_item_new_with_label ("test\nline2");
|
|
|
|
gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), menu);
|
2001-08-23 23:30:43 +00:00
|
|
|
gtk_menu_shell_append (GTK_MENU_SHELL (menubar), menuitem);
|
2000-10-18 15:50:13 +00:00
|
|
|
gtk_widget_show (menuitem);
|
|
|
|
|
|
|
|
menuitem = gtk_menu_item_new_with_label ("foo");
|
|
|
|
gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), create_menu (3, TRUE));
|
2001-08-23 23:30:43 +00:00
|
|
|
gtk_menu_shell_append (GTK_MENU_SHELL (menubar), menuitem);
|
2000-10-18 15:50:13 +00:00
|
|
|
gtk_widget_show (menuitem);
|
|
|
|
|
|
|
|
menuitem = gtk_menu_item_new_with_label ("bar");
|
|
|
|
gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), create_menu (4, TRUE));
|
2001-08-27 15:17:51 +00:00
|
|
|
gtk_menu_item_set_right_justified (GTK_MENU_ITEM (menuitem), TRUE);
|
2001-08-23 23:30:43 +00:00
|
|
|
gtk_menu_shell_append (GTK_MENU_SHELL (menubar), menuitem);
|
2000-10-18 15:50:13 +00:00
|
|
|
gtk_widget_show (menuitem);
|
|
|
|
|
|
|
|
box2 = gtk_vbox_new (FALSE, 10);
|
|
|
|
gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
|
|
|
|
gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, TRUE, 0);
|
|
|
|
gtk_widget_show (box2);
|
|
|
|
|
|
|
|
button = gtk_button_new_with_label ("close");
|
2001-10-20 23:39:32 +00:00
|
|
|
g_signal_connect_swapped (button, "clicked",
|
|
|
|
G_CALLBACK(gtk_widget_destroy), window);
|
2000-10-18 15:50:13 +00:00
|
|
|
gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
|
|
|
|
GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
|
|
|
|
gtk_widget_grab_default (button);
|
|
|
|
gtk_widget_show (button);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!GTK_WIDGET_VISIBLE (window))
|
2000-11-18 23:59:30 +00:00
|
|
|
{
|
|
|
|
gtk_widget_show (window);
|
|
|
|
}
|
2000-10-18 15:50:13 +00:00
|
|
|
else
|
2000-11-18 23:59:30 +00:00
|
|
|
{
|
|
|
|
gtk_widget_destroy (window);
|
|
|
|
window = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return window;
|
2000-10-18 15:50:13 +00:00
|
|
|
}
|