menus.c 5.58 KB
Newer Older
1 2
/* Menus
 *
3
 * There are several widgets involved in displaying menus. The
4
 * GtkMenuBar widget is a menu bar, which normally appears horizontally
Matthias Clasen's avatar
Matthias Clasen committed
5 6 7 8
 * 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
9
 * and can be selected by the user.
10 11 12 13 14 15 16 17 18 19 20 21
 *
 * 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.
 *
22
 * GtkUIManager provides a higher-level interface for creating menu bars
23
 * and menus; while you can construct menus manually, most people don't
24
 * do that. There's a separate demo for GtkUIManager.
25 26 27 28 29
 */

#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>

30 31 32 33 34
#include <stdio.h>

static GtkWidget *
create_menu (gint     depth,
	     gboolean tearoff)
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
{
  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 ();
51
      gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
52 53 54 55 56 57 58
      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);
59
      group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (menuitem));
60

61
      gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
62 63 64 65 66 67 68 69 70 71
      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;
}

72
static void
73 74 75 76
change_orientation (GtkWidget *button,
                    GtkWidget *menubar)
{
  GtkWidget *parent;
77
  GtkOrientation orientation;
78 79

  parent = gtk_widget_get_parent (menubar);
80 81
  orientation = gtk_orientable_get_orientation (GTK_ORIENTABLE (parent));
  gtk_orientable_set_orientation (GTK_ORIENTABLE (parent), 1 - orientation);
82

83 84
  if (orientation == GTK_ORIENTATION_VERTICAL)
    g_object_set (menubar, "pack-direction", GTK_PACK_DIRECTION_TTB, NULL);
85
  else
86 87
    g_object_set (menubar, "pack-direction", GTK_PACK_DIRECTION_LTR, NULL);

88 89
}

Matthias Clasen's avatar
Matthias Clasen committed
90 91
static GtkWidget *window = NULL;

92
GtkWidget *
93
do_menus (GtkWidget *do_widget)
94
{
95
  GtkWidget *box;
96 97 98
  GtkWidget *box1;
  GtkWidget *box2;
  GtkWidget *button;
Matthias Clasen's avatar
Matthias Clasen committed
99

100 101 102 103 104 105
  if (!window)
    {
      GtkWidget *menubar;
      GtkWidget *menu;
      GtkWidget *menuitem;
      GtkAccelGroup *accel_group;
Matthias Clasen's avatar
Matthias Clasen committed
106

107
      window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
108 109
      gtk_window_set_screen (GTK_WINDOW (window),
			     gtk_widget_get_screen (do_widget));
Matthias Clasen's avatar
Matthias Clasen committed
110
      gtk_window_set_title (GTK_WINDOW (window), "Menus");
111 112
      g_signal_connect (window, "destroy",
			G_CALLBACK(gtk_widget_destroyed), &window);
Matthias Clasen's avatar
Matthias Clasen committed
113

114
      accel_group = gtk_accel_group_new ();
115
      gtk_window_add_accel_group (GTK_WINDOW (window), accel_group);
116 117

      gtk_container_set_border_width (GTK_CONTAINER (window), 0);
Matthias Clasen's avatar
Matthias Clasen committed
118

119
      box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
120 121 122
      gtk_container_add (GTK_CONTAINER (window), box);
      gtk_widget_show (box);

123
      box1 = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
124
      gtk_container_add (GTK_CONTAINER (box), box1);
125
      gtk_widget_show (box1);
Matthias Clasen's avatar
Matthias Clasen committed
126

127 128 129
      menubar = gtk_menu_bar_new ();
      gtk_box_pack_start (GTK_BOX (box1), menubar, FALSE, TRUE, 0);
      gtk_widget_show (menubar);
Matthias Clasen's avatar
Matthias Clasen committed
130

131
      menu = create_menu (2, TRUE);
Matthias Clasen's avatar
Matthias Clasen committed
132

133 134
      menuitem = gtk_menu_item_new_with_label ("test\nline2");
      gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), menu);
135
      gtk_menu_shell_append (GTK_MENU_SHELL (menubar), menuitem);
136
      gtk_widget_show (menuitem);
Matthias Clasen's avatar
Matthias Clasen committed
137

138 139
      menuitem = gtk_menu_item_new_with_label ("foo");
      gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), create_menu (3, TRUE));
140
      gtk_menu_shell_append (GTK_MENU_SHELL (menubar), menuitem);
141 142 143 144
      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));
145
      gtk_menu_shell_append (GTK_MENU_SHELL (menubar), menuitem);
146
      gtk_widget_show (menuitem);
Matthias Clasen's avatar
Matthias Clasen committed
147

148
      box2 = gtk_box_new (GTK_ORIENTATION_VERTICAL, 10);
149 150 151 152
      gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
      gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, TRUE, 0);
      gtk_widget_show (box2);

153 154 155 156 157 158 159
      button = gtk_button_new_with_label ("Flip");
      g_signal_connect (button, "clicked",
			G_CALLBACK (change_orientation), menubar);
      gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
      gtk_widget_show (button);

      button = gtk_button_new_with_label ("Close");
160 161
      g_signal_connect_swapped (button, "clicked",
				G_CALLBACK(gtk_widget_destroy), window);
162
      gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
163
      gtk_widget_set_can_default (button, TRUE);
164 165 166 167
      gtk_widget_grab_default (button);
      gtk_widget_show (button);
    }

168
  if (!gtk_widget_get_visible (window))
169 170 171
    {
      gtk_widget_show (window);
    }
172
  else
173 174 175 176 177 178
    {
      gtk_widget_destroy (window);
      window = NULL;
    }

  return window;
179
}