4 * $Id: menu.c,v 1.51 2001/04/13 14:59:30 jfoster 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.
36 #ifdef HAVE_SYS_TYPES_H
37 # include <sys/types.h>
46 #include "capture_dlg.h"
47 #include "color_dlg.h"
49 #include "filter_prefs.h"
52 #include "summary_dlg.h"
53 #include "display_opts.h"
54 #include "prefs_dlg.h"
55 #include "packet_win.h"
57 #include "follow_dlg.h"
58 #include "decode_as_dlg.h"
60 #include "proto_dlg.h"
61 #include "proto_hier_stats_dlg.h"
65 GtkWidget *popup_menu_object;
67 #define GTK_MENU_FUNC(a) ((GtkItemFactoryCallback)(a))
69 static void menus_init(void);
70 static void set_menu_sensitivity (gchar *, gint);
72 /* This is the GtkItemFactoryEntry structure used to generate new menus.
73 Item 1: The menu path. The letter after the underscore indicates an
74 accelerator key once the menu is open.
75 Item 2: The accelerator key for the entry
76 Item 3: The callback function.
77 Item 4: The callback action. This changes the parameters with
78 which the function is called. The default is 0.
79 Item 5: The item type, used to define what kind of an item it is.
80 Here are the possible values:
84 "<Title>" -> create a title item
85 "<Item>" -> create a simple item
86 "<CheckItem>" -> create a check item
87 "<ToggleItem>" -> create a toggle item
88 "<RadioItem>" -> create a radio item
89 <path> -> path of a radio item to link against
90 "<Separator>" -> create a separator
91 "<Branch>" -> create an item to hold sub items (optional)
92 "<LastBranch>" -> create a right justified branch
96 static GtkItemFactoryEntry menu_items[] =
98 {"/_File", NULL, NULL, 0, "<Branch>" },
99 {"/File/_Open...", "<control>O", GTK_MENU_FUNC(file_open_cmd_cb), 0, NULL},
100 {"/File/_Close", "<control>W", GTK_MENU_FUNC(file_close_cmd_cb), 0, NULL},
101 {"/File/_Save", "<control>S", GTK_MENU_FUNC(file_save_cmd_cb), 0, NULL},
102 {"/File/Save _As...", NULL, GTK_MENU_FUNC(file_save_as_cmd_cb), 0, NULL},
103 {"/File/_Reload", "<control>R", GTK_MENU_FUNC(file_reload_cmd_cb), 0, NULL},
104 {"/File/<separator>", NULL, NULL, 0, "<Separator>"},
105 {"/File/_Print...", NULL, GTK_MENU_FUNC(file_print_cmd_cb), 0, NULL},
106 {"/File/Print Pac_ket", "<control>P", GTK_MENU_FUNC(file_print_packet_cmd_cb), 0, NULL},
107 {"/File/<separator>", NULL, NULL, 0, "<Separator>"},
108 {"/File/_Quit", "<control>Q", GTK_MENU_FUNC(file_quit_cmd_cb), 0, NULL},
109 {"/_Edit", NULL, NULL, 0, "<Branch>" },
111 /* Un-#if this when we actually implement Cut/Copy/Paste. */
112 {"/Edit/Cut", "<control>X", NULL, 0, NULL},
113 {"/Edit/Copy", "<control>C", NULL, 0, NULL},
114 {"/Edit/Paste", "<control>V", NULL, 0, NULL},
115 {"/Edit/<separator>", NULL, NULL, 0, "<Separator>"},
117 {"/Edit/_Find Frame...", "<control>F", GTK_MENU_FUNC(find_frame_cb), 0, NULL},
118 {"/Edit/_Go To Frame...", "<control>G", GTK_MENU_FUNC(goto_frame_cb), 0, NULL},
119 {"/Edit/<separator>", NULL, NULL, 0, "<Separator>"},
120 {"/Edit/_Mark Frame", "<control>M", GTK_MENU_FUNC(mark_frame_cb), 0, NULL},
121 {"/Edit/Mark _All Frames", NULL, GTK_MENU_FUNC(mark_all_frames_cb), 0, NULL},
122 {"/Edit/_Unmark All Frames", NULL, GTK_MENU_FUNC(unmark_all_frames_cb), 0, NULL},
123 {"/Edit/<separator>", NULL, NULL, 0, "<Separator>"},
124 {"/Edit/_Preferences...", NULL, GTK_MENU_FUNC(prefs_cb), 0, NULL},
125 {"/Edit/_Capture Filters...", NULL, GTK_MENU_FUNC(cfilter_dialog_cb), 0, NULL},
126 {"/Edit/_Display Filters...", NULL, GTK_MENU_FUNC(dfilter_dialog_cb), 0, NULL},
127 {"/Edit/P_rotocols...", NULL, GTK_MENU_FUNC(proto_cb), 0, NULL},
129 {"/_Capture", NULL, NULL, 0, "<Branch>" },
130 {"/Capture/_Start...", "<control>K", GTK_MENU_FUNC(capture_prep_cb), 0, NULL},
132 * XXX - this doesn't yet work in Win32.
135 {"/Capture/S_top", "<control>E", GTK_MENU_FUNC(capture_stop_cb), 0, NULL},
137 #endif /* HAVE_LIBPCAP */
138 {"/_Display", NULL, NULL, 0, "<Branch>" },
139 {"/Display/_Options...", NULL, GTK_MENU_FUNC(display_opt_cb), 0, NULL},
140 {"/Display/_Match Selected", NULL, GTK_MENU_FUNC(match_selected_cb), 0, NULL},
141 {"/Display/_Colorize Display...", NULL, GTK_MENU_FUNC(color_display_cb), 0, NULL},
142 {"/Display/Collapse _All", NULL, GTK_MENU_FUNC(collapse_all_cb), 0, NULL},
143 {"/Display/_Expand All", NULL, GTK_MENU_FUNC(expand_all_cb), 0, NULL},
144 {"/Display/_Show Packet In New Window", NULL, GTK_MENU_FUNC(new_window_cb), 0, NULL},
145 {"/Display/User Specified Decodes...", NULL, GTK_MENU_FUNC(decode_show_cb), 0, NULL},
146 {"/_Tools", NULL, NULL, 0, "<Branch>" },
148 {"/Tools/_Plugins...", NULL, GTK_MENU_FUNC(tools_plugins_cmd_cb), 0, NULL},
150 {"/Tools/_Follow TCP Stream", NULL, GTK_MENU_FUNC(follow_stream_cb), 0, NULL},
151 {"/Tools/_Decode As...", NULL, GTK_MENU_FUNC(decode_as_cb), 0, NULL},
152 /* {"/Tools/Graph", NULL, NULL, 0, NULL}, future use */
153 {"/Tools/_Summary", NULL, GTK_MENU_FUNC(summary_open_cb), 0, NULL},
154 {"/Tools/Protocol Hierarchy Statistics", NULL, GTK_MENU_FUNC(proto_hier_stats_cb), 0, NULL},
155 {"/_Help", NULL, NULL, 0, "<LastBranch>" },
156 {"/Help/_Help", NULL, GTK_MENU_FUNC(help_cb), 0, NULL},
157 {"/Help/<separator>", NULL, NULL, 0, "<Separator>"},
158 {"/Help/_About Ethereal...", NULL, GTK_MENU_FUNC(about_ethereal), 0, NULL}
161 /* calculate the number of menu_items */
162 static int nmenu_items = sizeof(menu_items) / sizeof(menu_items[0]);
164 /* packet list popup */
165 static GtkItemFactoryEntry packet_list_menu_items[] =
167 {"/Follow TCP Stream", NULL, GTK_MENU_FUNC(follow_stream_cb), 0, NULL},
168 {"/Decode As...", NULL, GTK_MENU_FUNC(decode_as_cb), 0, NULL},
169 {"/Display Filters...", NULL, GTK_MENU_FUNC(dfilter_dialog_cb), 0, NULL},
170 {"/<separator>", NULL, NULL, 0, "<Separator>"},
171 {"/Colorize Display...", NULL, GTK_MENU_FUNC(color_display_cb), 0, NULL},
172 {"/Print...", NULL, GTK_MENU_FUNC(file_print_cmd_cb), 0, NULL},
173 {"/Print Packet", NULL, GTK_MENU_FUNC(file_print_packet_cmd_cb), 0, NULL},
174 {"/Show Packet In New Window", NULL, GTK_MENU_FUNC(new_window_cb), 0, NULL},
177 static GtkItemFactoryEntry tree_view_menu_items[] =
179 {"/Follow TCP Stream", NULL, GTK_MENU_FUNC(follow_stream_cb), 0, NULL},
180 {"/Decode As...", NULL, GTK_MENU_FUNC(decode_as_cb), 0, NULL},
181 {"/Display Filters...", NULL, GTK_MENU_FUNC(dfilter_dialog_cb), 0, NULL},
182 {"/<separator>", NULL, NULL, 0, "<Separator>"},
183 {"/Resolve Name", NULL, GTK_MENU_FUNC(resolve_name_cb), 0, NULL},
184 {"/Protocol Properties...", NULL, GTK_MENU_FUNC(properties_cb), 0, NULL},
185 {"/Match Selected", NULL, GTK_MENU_FUNC(match_selected_cb), 0, NULL},
186 {"/<separator>", NULL, NULL, 0, "<Separator>"},
187 {"/Collapse All", NULL, GTK_MENU_FUNC(collapse_all_cb), 0, NULL},
188 {"/Expand All", NULL, GTK_MENU_FUNC(expand_all_cb), 0, NULL}
191 static GtkItemFactoryEntry hexdump_menu_items[] =
193 {"/Follow TCP Stream", NULL, GTK_MENU_FUNC(follow_stream_cb), 0, NULL},
194 {"/Decode As...", NULL, GTK_MENU_FUNC(decode_as_cb), 0, NULL},
195 {"/Display Filters...", NULL, GTK_MENU_FUNC(dfilter_dialog_cb), 0, NULL}
198 static int initialize = TRUE;
199 static GtkItemFactory *factory = NULL;
200 static GtkItemFactory *packet_list_menu_factory = NULL;
201 static GtkItemFactory *tree_view_menu_factory = NULL;
202 static GtkItemFactory *hexdump_menu_factory = NULL;
204 static GSList *popup_menu_list = NULL;
206 static GtkAccelGroup *grp;
209 get_main_menu(GtkWidget ** menubar, GtkAccelGroup ** table) {
211 grp = gtk_accel_group_new();
214 popup_menu_object = gtk_widget_new(GTK_TYPE_WIDGET, NULL);
219 *menubar = factory->widget;
233 packet_list_menu_factory = gtk_item_factory_new(GTK_TYPE_MENU, "<main>", NULL);
234 gtk_item_factory_create_items_ac(packet_list_menu_factory, sizeof(packet_list_menu_items)/sizeof(packet_list_menu_items[0]), packet_list_menu_items, NULL, 2);
235 gtk_object_set_data(GTK_OBJECT(popup_menu_object), PM_PACKET_LIST_KEY, packet_list_menu_factory->widget);
236 popup_menu_list = g_slist_append((GSList *)popup_menu_list, packet_list_menu_factory);
238 tree_view_menu_factory = gtk_item_factory_new(GTK_TYPE_MENU, "<main>", NULL);
239 gtk_item_factory_create_items_ac(tree_view_menu_factory, sizeof(tree_view_menu_items)/sizeof(tree_view_menu_items[0]), tree_view_menu_items, NULL, 2);
240 gtk_object_set_data(GTK_OBJECT(popup_menu_object), PM_TREE_VIEW_KEY, tree_view_menu_factory->widget);
241 popup_menu_list = g_slist_append((GSList *)popup_menu_list, tree_view_menu_factory);
243 hexdump_menu_factory = gtk_item_factory_new(GTK_TYPE_MENU, "<main>", NULL);
244 gtk_item_factory_create_items_ac(hexdump_menu_factory, sizeof(hexdump_menu_items)/sizeof(hexdump_menu_items[0]), hexdump_menu_items, NULL, 2);
245 gtk_object_set_data(GTK_OBJECT(popup_menu_object), PM_HEXDUMP_KEY, hexdump_menu_factory->widget);
246 popup_menu_list = g_slist_append((GSList *)popup_menu_list, hexdump_menu_factory);
248 factory = gtk_item_factory_new(GTK_TYPE_MENU_BAR, "<main>", grp);
249 gtk_item_factory_create_items_ac(factory, nmenu_items, menu_items, NULL,2);
250 set_menus_for_unsaved_capture_file(FALSE);
251 set_menus_for_capture_file(FALSE);
253 /* Un-#if this when we actually implement Cut/Copy/Paste.
254 Then make sure you enable them when they can be done. */
255 set_menu_sensitivity("/Edit/Cut", FALSE);
256 set_menu_sensitivity("/Edit/Copy", FALSE);
257 set_menu_sensitivity("/Edit/Paste", FALSE);
259 set_menus_for_captured_packets(FALSE);
260 set_menus_for_selected_packet(FALSE);
261 set_menus_for_selected_tree_row(FALSE);
266 set_menu_sensitivity_meat(GtkItemFactory *ifactory, gchar *path, gint val) {
267 GtkWidget *menu = NULL;
269 if((menu = gtk_item_factory_get_widget(ifactory, path)) != NULL) {
270 gtk_widget_set_sensitive(menu,val);
275 set_menu_sensitivity (gchar *path, gint val) {
276 GSList *menu_list = popup_menu_list;
277 gchar *shortpath = strrchr(path, '/');
279 set_menu_sensitivity_meat(factory, path, val);
281 while (menu_list != NULL) {
282 set_menu_sensitivity_meat(menu_list->data, shortpath, val);
283 menu_list = g_slist_next(menu_list);
289 set_menu_object_data_meat(GtkItemFactory *ifactory, gchar *path, gchar *key, gpointer data)
291 GtkWidget *menu = NULL;
293 if ((menu = gtk_item_factory_get_widget(ifactory, path)) != NULL)
294 gtk_object_set_data(GTK_OBJECT(menu), key, data);
298 set_menu_object_data (gchar *path, gchar *key, gpointer data) {
299 GSList *menu_list = popup_menu_list;
300 gchar *shortpath = strrchr(path, '/');
302 set_menu_object_data_meat(factory, path, key, data);
303 while (menu_list != NULL) {
304 set_menu_object_data_meat(menu_list->data, shortpath, key, data);
305 menu_list = g_slist_next(menu_list);
310 popup_menu_handler(GtkWidget *widget, GdkEvent *event, gpointer data)
312 GtkWidget *menu = NULL;
313 GdkEventButton *event_button = NULL;
315 if(widget == NULL || event == NULL || data == NULL) {
320 * If we ever want to make the menu differ based on what row
321 * and/or column we're above, we'd use "gtk_clist_get_selection_info()"
322 * to find the row and column number for the coordinates; a CTree is,
323 * I guess, like a CList with one column(?) and the expander widget
326 menu = (GtkWidget *)data;
327 if(event->type == GDK_BUTTON_PRESS) {
328 event_button = (GdkEventButton *) event;
330 if(event_button->button == 3) {
331 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL, event_button->button, event_button->time);
332 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget), "button_press_event");
339 /* Enable or disable menu items based on whether you have a capture file
340 you've finished reading. */
342 set_menus_for_capture_file(gboolean have_capture_file)
344 set_menu_sensitivity("/File/Open...", have_capture_file);
345 set_menu_sensitivity("/File/Save As...", have_capture_file);
346 set_menu_sensitivity("/File/Close", have_capture_file);
347 set_menu_sensitivity("/File/Reload", have_capture_file);
350 /* Enable or disable menu items based on whether you have an unsaved
351 capture file you've finished reading. */
353 set_menus_for_unsaved_capture_file(gboolean have_unsaved_capture_file)
355 set_menu_sensitivity("/File/Save", have_unsaved_capture_file);
358 /* Enable or disable menu items based on whether there's a capture in
361 set_menus_for_capture_in_progress(gboolean capture_in_progress)
363 set_menu_sensitivity("/File/Open...", !capture_in_progress);
364 set_menu_sensitivity("/Capture/Start...", !capture_in_progress);
366 * XXX - this doesn't yet work in Win32.
369 set_menu_sensitivity("/Capture/Stop", capture_in_progress);
373 /* Enable or disable menu items based on whether you have some captured
376 set_menus_for_captured_packets(gboolean have_captured_packets)
378 set_menu_sensitivity("/File/Print...", have_captured_packets);
379 set_menu_sensitivity("/Edit/Find Frame...", have_captured_packets);
380 set_menu_sensitivity("/Edit/Go To Frame...", have_captured_packets);
381 set_menu_sensitivity("/Display/Match Selected", have_captured_packets);
382 set_menu_sensitivity("/Display/Colorize Display...", have_captured_packets);
383 set_menu_sensitivity("/Tools/Summary", have_captured_packets);
384 set_menu_sensitivity("/Tools/Protocol Hierarchy Statistics", have_captured_packets);
387 /* Enable or disable menu items based on whether a packet is selected. */
389 set_menus_for_selected_packet(gboolean have_selected_packet)
391 set_menu_sensitivity("/File/Print Packet", have_selected_packet);
392 set_menu_sensitivity("/Edit/Mark Frame", have_selected_packet);
393 set_menu_sensitivity("/Edit/Mark All Frames", have_selected_packet);
394 set_menu_sensitivity("/Edit/Unmark All Frames", have_selected_packet);
395 set_menu_sensitivity("/Display/Collapse All", have_selected_packet);
396 set_menu_sensitivity("/Display/Expand All", have_selected_packet);
397 set_menu_sensitivity("/Display/Show Packet In New Window", have_selected_packet);
398 set_menu_sensitivity("/Tools/Follow TCP Stream",
399 have_selected_packet ? (pi.ipproto == 6) : FALSE);
400 set_menu_sensitivity("/Tools/Decode As...",
401 have_selected_packet && decode_as_ok());
402 set_menu_sensitivity("/Resolve Name",
403 have_selected_packet && !prefs.capture_name_resolve);
406 /* Enable or disable menu items based on whether a tree row is selected. */
408 set_menus_for_selected_tree_row(gboolean have_selected_tree)
410 gboolean properties = FALSE;
411 if (finfo_selected) {
412 header_field_info *hfinfo = finfo_selected->hfinfo;
413 if (hfinfo->parent == -1) {
414 properties = prefs_is_registered_protocol(hfinfo->abbrev);
416 properties = prefs_is_registered_protocol(proto_registrar_get_abbrev(hfinfo->parent));
419 set_menu_sensitivity("/Protocol Properties...", have_selected_tree && properties);