4 * $Id: menu.c,v 1.3 1998/09/17 03:12:27 gerald Exp $
6 * Ethereal - Network traffic analyzer
7 * By Gerald Combs <gerald@zing.org>
8 * Copyright 1998 Gerald Combs
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
45 /* Much of this was take from the GTK+ tuturial at http://www.gtk.org */
47 static void menus_remove_accel (GtkWidget *, gchar *, gchar *);
48 static gint menus_install_accel (GtkWidget *, gchar *, gchar, gchar, gchar *);
50 /* this is the GtkMenuEntry structure used to create new menus. The
51 * first member is the menu definition string. The second, the
52 * default accelerator key used to access this menu function with
53 * the keyboard. The third is the callback function to call when
54 * this menu item is selected (by the accelerator key, or with the
55 * mouse.) The last member is the data to pass to your callback function.
58 static GtkMenuEntry menu_items[] =
60 {"<Main>/File/Open", "<control>O", file_open_cmd_cb, NULL},
61 {"<Main>/File/Close", "<control>W", file_close_cmd_cb, NULL},
62 {"<Main>/File/Save", "<control>S", NULL, NULL},
63 {"<Main>/File/Save as", NULL, NULL, NULL},
64 {"<Main>/File/<separator>", NULL, NULL, NULL},
65 {"<Main>/File/Print Packet", "<control>P", file_print_cmd_cb, NULL},
66 {"<Main>/File/<separator>", NULL, NULL, NULL},
67 {"<Main>/File/Quit", "<control>Q", file_quit_cmd_cb, NULL},
68 {"<Main>/Edit/Cut", "<control>X", NULL, NULL},
69 {"<Main>/Edit/Copy", "<control>C", NULL, NULL},
70 {"<Main>/Edit/Paste", "<control>V", NULL, NULL},
71 {"<Main>/Edit/<separator>", NULL, NULL, NULL},
72 {"<Main>/Edit/Find", "<control>F", NULL, NULL},
73 {"<Main>/Edit/<separator>", NULL, NULL, NULL},
74 {"<Main>/Edit/Printer Options", NULL, printer_opts_cb, NULL},
75 {"<Main>/Tools/Capture", "<control>K", capture_prep_cb, NULL},
76 {"<Main>/Tools/Filter", NULL, filter_sel_cb, NULL},
77 {"<Main>/Tools/Follow TCP Stream", NULL, follow_stream_cb, NULL},
78 {"<Main>/Tools/Graph", NULL, NULL, NULL},
79 {"<Main>/Help/About Ethereal", NULL, NULL, NULL}
82 /* calculate the number of menu_items */
83 static int nmenu_items = sizeof(menu_items) / sizeof(menu_items[0]);
85 static int initialize = TRUE;
86 static GtkMenuFactory *factory = NULL;
87 static GtkMenuFactory *subfactory[1];
88 static GHashTable *entry_ht = NULL;
91 get_main_menu(GtkWidget ** menubar, GtkAcceleratorTable ** table) {
96 *menubar = subfactory[0]->widget;
98 *table = subfactory[0]->table;
108 factory = gtk_menu_factory_new(GTK_MENU_FACTORY_MENU_BAR);
109 subfactory[0] = gtk_menu_factory_new(GTK_MENU_FACTORY_MENU_BAR);
111 gtk_menu_factory_add_subfactory(factory, subfactory[0], "<Main>");
112 menus_create(menu_items, nmenu_items);
114 set_menu_sensitivity("<Main>/File/Close", FALSE);
115 set_menu_sensitivity("<Main>/File/Save", FALSE);
116 set_menu_sensitivity("<Main>/File/Save as", FALSE);
117 set_menu_sensitivity("<Main>/Edit/Cut", FALSE);
118 set_menu_sensitivity("<Main>/Edit/Copy", FALSE);
119 set_menu_sensitivity("<Main>/Edit/Paste", FALSE);
120 set_menu_sensitivity("<Main>/Edit/Find", FALSE);
121 set_menu_sensitivity("<Main>/Tools/Graph", FALSE);
122 set_menu_sensitivity("<Main>/Help/About Ethereal", FALSE);
123 if ((mp = gtk_menu_factory_find(factory, "<Main>/Help")) != NULL) {
124 gtk_menu_item_right_justify((GtkMenuItem *) mp->widget);
130 set_menu_sensitivity (gchar *path, gint val) {
133 if ((mp = gtk_menu_factory_find(factory, path)) != NULL) {
134 gtk_widget_set_sensitive(mp->widget, val);
139 menus_create(GtkMenuEntry * entries, int nmenu_entries) {
147 for (i = 0; i < nmenu_entries; i++) {
148 accelerator = g_hash_table_lookup(entry_ht, entries[i].path);
150 if (accelerator[0] == '\0')
151 entries[i].accelerator = NULL;
153 entries[i].accelerator = accelerator;
156 gtk_menu_factory_add_entries(factory, entries, nmenu_entries);
158 for (i = 0; i < nmenu_entries; i++)
159 if (entries[i].widget) {
160 gtk_signal_connect(GTK_OBJECT(entries[i].widget), "install_accelerator",
161 (GtkSignalFunc) menus_install_accel, entries[i].path);
162 gtk_signal_connect(GTK_OBJECT(entries[i].widget), "remove_accelerator",
163 (GtkSignalFunc) menus_remove_accel, entries[i].path);
168 menus_install_accel(GtkWidget * widget, gchar * signal_name, gchar key, gchar modifiers, gchar * path) {
173 if (modifiers & GDK_CONTROL_MASK)
174 strcat(accel, "<control>");
175 if (modifiers & GDK_SHIFT_MASK)
176 strcat(accel, "<shift>");
177 if (modifiers & GDK_MOD1_MASK)
178 strcat(accel, "<alt>");
185 t1 = g_hash_table_lookup(entry_ht, path);
188 entry_ht = g_hash_table_new(g_str_hash, g_str_equal);
190 g_hash_table_insert(entry_ht, path, g_strdup(accel));
196 menus_remove_accel(GtkWidget * widget, gchar * signal_name, gchar * path) {
200 t = g_hash_table_lookup(entry_ht, path);
203 g_hash_table_insert(entry_ht, path, g_strdup(""));
208 menus_set_sensitive(char *path, int sensitive) {
209 GtkMenuPath *menu_path;
214 menu_path = gtk_menu_factory_find(factory, path);
216 gtk_widget_set_sensitive(menu_path->widget, sensitive);
218 g_warning("Unable to set sensitivity for menu which doesn't exist: %s", path);