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
|
2005-07-05 16:18:40 +00:00
|
|
|
* GtkMenuBar widget is a menu bar, which normally appears horizontally
|
2009-08-31 05:08:31 +00:00
|
|
|
* at the top of an application, but can also be layed out vertically.
|
|
|
|
* 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
|
2005-07-05 16:18:40 +00:00
|
|
|
* and can be selected by the user.
|
2001-04-18 18:09:18 +00:00
|
|
|
*
|
|
|
|
* 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.
|
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 *
|
2015-06-20 01:59:15 +00:00
|
|
|
create_menu (gint depth)
|
2000-10-18 15:50:13 +00:00
|
|
|
{
|
|
|
|
GtkWidget *menu;
|
2015-03-22 05:57:57 +00:00
|
|
|
GtkRadioMenuItem *last_item;
|
2000-10-18 15:50:13 +00:00
|
|
|
char buf[32];
|
|
|
|
int i, j;
|
|
|
|
|
|
|
|
if (depth < 1)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
menu = gtk_menu_new ();
|
2015-03-22 05:57:57 +00:00
|
|
|
last_item = NULL;
|
2000-10-18 15:50:13 +00:00
|
|
|
|
|
|
|
for (i = 0, j = 1; i < 5; i++, j++)
|
|
|
|
{
|
2015-03-22 05:57:57 +00:00
|
|
|
GtkWidget *menu_item;
|
|
|
|
|
2000-10-18 15:50:13 +00:00
|
|
|
sprintf (buf, "item %2d - %d", depth, j);
|
2001-04-18 18:09:18 +00:00
|
|
|
|
2015-03-22 05:57:57 +00:00
|
|
|
menu_item = gtk_radio_menu_item_new_with_label_from_widget (NULL, buf);
|
|
|
|
gtk_radio_menu_item_join_group (GTK_RADIO_MENU_ITEM (menu_item), last_item);
|
|
|
|
last_item = GTK_RADIO_MENU_ITEM (menu_item);
|
|
|
|
|
|
|
|
gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
|
|
|
|
gtk_widget_show (menu_item);
|
2000-10-18 15:50:13 +00:00
|
|
|
if (i == 3)
|
2015-03-22 05:57:57 +00:00
|
|
|
gtk_widget_set_sensitive (menu_item, FALSE);
|
2000-10-18 15:50:13 +00:00
|
|
|
|
2015-03-22 05:57:57 +00:00
|
|
|
gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_item), create_menu (depth - 1));
|
2000-10-18 15:50:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return menu;
|
|
|
|
}
|
|
|
|
|
2005-07-07 00:25:18 +00:00
|
|
|
static void
|
2005-07-05 16:18:40 +00:00
|
|
|
change_orientation (GtkWidget *button,
|
|
|
|
GtkWidget *menubar)
|
|
|
|
{
|
|
|
|
GtkWidget *parent;
|
2011-02-17 21:40:00 +00:00
|
|
|
GtkOrientation orientation;
|
2005-07-05 16:18:40 +00:00
|
|
|
|
|
|
|
parent = gtk_widget_get_parent (menubar);
|
2011-02-17 21:40:00 +00:00
|
|
|
orientation = gtk_orientable_get_orientation (GTK_ORIENTABLE (parent));
|
|
|
|
gtk_orientable_set_orientation (GTK_ORIENTABLE (parent), 1 - orientation);
|
2005-07-05 16:18:40 +00:00
|
|
|
|
2011-02-17 21:40:00 +00:00
|
|
|
if (orientation == GTK_ORIENTATION_VERTICAL)
|
|
|
|
g_object_set (menubar, "pack-direction", GTK_PACK_DIRECTION_TTB, NULL);
|
2005-07-05 16:18:40 +00:00
|
|
|
else
|
2011-02-17 21:40:00 +00:00
|
|
|
g_object_set (menubar, "pack-direction", GTK_PACK_DIRECTION_LTR, NULL);
|
|
|
|
|
2005-07-05 16:18:40 +00:00
|
|
|
}
|
|
|
|
|
2009-08-31 05:08:31 +00:00
|
|
|
static GtkWidget *window = NULL;
|
|
|
|
|
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
|
|
|
{
|
2005-07-05 16:18:40 +00:00
|
|
|
GtkWidget *box;
|
2000-10-18 15:50:13 +00:00
|
|
|
GtkWidget *box1;
|
|
|
|
GtkWidget *box2;
|
|
|
|
GtkWidget *button;
|
2009-08-31 05:08:31 +00:00
|
|
|
|
2000-10-18 15:50:13 +00:00
|
|
|
if (!window)
|
|
|
|
{
|
|
|
|
GtkWidget *menubar;
|
|
|
|
GtkWidget *menu;
|
|
|
|
GtkWidget *menuitem;
|
|
|
|
GtkAccelGroup *accel_group;
|
2009-08-31 05:08:31 +00:00
|
|
|
|
2000-10-18 15:50:13 +00:00
|
|
|
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
|
2017-10-31 06:41:15 +00:00
|
|
|
gtk_window_set_display (GTK_WINDOW (window),
|
|
|
|
gtk_widget_get_display (do_widget));
|
2009-08-31 05:08:31 +00:00
|
|
|
gtk_window_set_title (GTK_WINDOW (window), "Menus");
|
2001-10-20 23:39:32 +00:00
|
|
|
g_signal_connect (window, "destroy",
|
2011-09-02 03:55:47 +00:00
|
|
|
G_CALLBACK(gtk_widget_destroyed), &window);
|
2009-08-31 05:08:31 +00:00
|
|
|
|
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
|
|
|
|
2010-10-31 17:07:20 +00:00
|
|
|
box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
|
2005-07-05 16:18:40 +00:00
|
|
|
gtk_container_add (GTK_CONTAINER (window), box);
|
|
|
|
gtk_widget_show (box);
|
|
|
|
|
2010-10-31 17:07:20 +00:00
|
|
|
box1 = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
|
2005-07-05 16:18:40 +00:00
|
|
|
gtk_container_add (GTK_CONTAINER (box), box1);
|
2000-10-18 15:50:13 +00:00
|
|
|
gtk_widget_show (box1);
|
2009-08-31 05:08:31 +00:00
|
|
|
|
2000-10-18 15:50:13 +00:00
|
|
|
menubar = gtk_menu_bar_new ();
|
2015-06-20 01:59:15 +00:00
|
|
|
gtk_widget_set_hexpand (menubar, TRUE);
|
2017-04-21 20:34:36 +00:00
|
|
|
gtk_box_pack_start (GTK_BOX (box1), menubar);
|
2000-10-18 15:50:13 +00:00
|
|
|
gtk_widget_show (menubar);
|
2009-08-31 05:08:31 +00:00
|
|
|
|
2011-10-01 03:49:00 +00:00
|
|
|
menu = create_menu (2);
|
2009-08-31 05:08:31 +00:00
|
|
|
|
2000-10-18 15:50:13 +00:00
|
|
|
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);
|
2009-08-31 05:08:31 +00:00
|
|
|
|
2000-10-18 15:50:13 +00:00
|
|
|
menuitem = gtk_menu_item_new_with_label ("foo");
|
2011-10-01 03:49:00 +00:00
|
|
|
gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), create_menu (3));
|
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");
|
2011-10-01 03:49:00 +00:00
|
|
|
gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), create_menu (4));
|
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);
|
2009-08-31 05:08:31 +00:00
|
|
|
|
2010-10-31 17:07:20 +00:00
|
|
|
box2 = gtk_box_new (GTK_ORIENTATION_VERTICAL, 10);
|
2017-04-21 20:34:36 +00:00
|
|
|
gtk_box_pack_start (GTK_BOX (box1), box2);
|
2000-10-18 15:50:13 +00:00
|
|
|
gtk_widget_show (box2);
|
|
|
|
|
2005-07-05 16:18:40 +00:00
|
|
|
button = gtk_button_new_with_label ("Flip");
|
|
|
|
g_signal_connect (button, "clicked",
|
2011-09-02 03:55:47 +00:00
|
|
|
G_CALLBACK (change_orientation), menubar);
|
2017-04-21 20:34:36 +00:00
|
|
|
gtk_box_pack_start (GTK_BOX (box2), button);
|
2005-07-05 16:18:40 +00:00
|
|
|
gtk_widget_show (button);
|
|
|
|
|
|
|
|
button = gtk_button_new_with_label ("Close");
|
2001-10-20 23:39:32 +00:00
|
|
|
g_signal_connect_swapped (button, "clicked",
|
2011-09-02 03:55:47 +00:00
|
|
|
G_CALLBACK(gtk_widget_destroy), window);
|
2017-04-21 20:34:36 +00:00
|
|
|
gtk_box_pack_start (GTK_BOX (box2), button);
|
2010-03-02 06:58:05 +00:00
|
|
|
gtk_widget_set_can_default (button, TRUE);
|
2000-10-18 15:50:13 +00:00
|
|
|
gtk_widget_grab_default (button);
|
|
|
|
gtk_widget_show (button);
|
|
|
|
}
|
|
|
|
|
2010-03-01 06:47:38 +00:00
|
|
|
if (!gtk_widget_get_visible (window))
|
2015-06-28 13:23:28 +00:00
|
|
|
gtk_widget_show (window);
|
2000-10-18 15:50:13 +00:00
|
|
|
else
|
2015-06-28 13:23:28 +00:00
|
|
|
gtk_widget_destroy (window);
|
2000-11-18 23:59:30 +00:00
|
|
|
|
|
|
|
return window;
|
2000-10-18 15:50:13 +00:00
|
|
|
}
|