Add a new tool which summarizes packet counts by protocols, but
[obnox/wireshark/wip.git] / gtk / menu.c
1 /* menu.c
2  * Menu routines
3  *
4  * $Id: menu.c,v 1.50 2001/03/22 23:54:47 gram Exp $
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@zing.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * 
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.
15  * 
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.
20  * 
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.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <gtk/gtk.h>
31 #include <glib.h>
32
33 #include <string.h>
34 #include <stdio.h>
35
36 #ifdef HAVE_SYS_TYPES_H
37 # include <sys/types.h>
38 #endif
39
40 #include "../menu.h"
41
42 #include "main.h"
43 #include "menu.h"
44 #include "packet.h"
45 #include "resolv.h"
46 #include "capture_dlg.h"
47 #include "color_dlg.h"
48 #include "file_dlg.h"
49 #include "filter_prefs.h"
50 #include "find_dlg.h"
51 #include "goto_dlg.h"
52 #include "summary_dlg.h"
53 #include "display_opts.h"
54 #include "prefs_dlg.h"
55 #include "packet_win.h"
56 #include "print.h"
57 #include "follow_dlg.h"
58 #include "decode_as_dlg.h"
59 #include "help_dlg.h"
60 #include "proto_dlg.h"
61 #include "proto_hier_stats_dlg.h"
62 #include "keys.h"
63 #include "plugins.h"
64
65 GtkWidget *popup_menu_object;
66
67 #define GTK_MENU_FUNC(a) ((GtkItemFactoryCallback)(a))
68
69 static void menus_init(void);
70 static void set_menu_sensitivity (gchar *, gint);
71
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:
81
82                NULL               -> "<Item>"
83                ""                 -> "<Item>"
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 
93     */
94
95 /* main menu */
96 static GtkItemFactoryEntry menu_items[] =
97 {
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>" },
110 #if 0
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>"},
116 #endif
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},
128 #ifdef HAVE_LIBPCAP
129   {"/_Capture", NULL, NULL, 0, "<Branch>" },
130   {"/Capture/_Start...", "<control>K", GTK_MENU_FUNC(capture_prep_cb), 0, NULL},
131   /*
132    * XXX - this doesn't yet work in Win32.
133    */
134 #ifndef _WIN32
135   {"/Capture/S_top", "<control>E", GTK_MENU_FUNC(capture_stop_cb), 0, NULL},
136 #endif /* _WIN32 */
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>" },
147 #ifdef HAVE_PLUGINS
148   {"/Tools/_Plugins...", NULL, GTK_MENU_FUNC(tools_plugins_cmd_cb), 0, NULL},
149 #endif
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}
159 };
160
161 /* calculate the number of menu_items */
162 static int nmenu_items = sizeof(menu_items) / sizeof(menu_items[0]);
163
164 /* packet list popup */
165 static GtkItemFactoryEntry packet_list_menu_items[] =
166 {
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}, 
175 };
176
177 static GtkItemFactoryEntry tree_view_menu_items[] =
178 {
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}
189 };
190
191 static GtkItemFactoryEntry hexdump_menu_items[] =
192 {
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}
196 };
197
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;
203
204 static GSList *popup_menu_list = NULL;
205
206 static GtkAccelGroup *grp;
207
208 void
209 get_main_menu(GtkWidget ** menubar, GtkAccelGroup ** table) {
210
211   grp = gtk_accel_group_new();
212
213   if (initialize) {
214     popup_menu_object = gtk_widget_new(GTK_TYPE_WIDGET, NULL);
215     menus_init();
216   }
217
218   if (menubar)
219     *menubar = factory->widget;
220
221   if (table)
222     *table = grp;
223 }
224
225 static void
226 menus_init(void) {
227
228   if (initialize) {
229     initialize = FALSE;
230
231     /* popup */
232
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);
237
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);
242
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);
247     
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);
252 #if 0
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);
258 #endif
259     set_menus_for_captured_packets(FALSE);
260     set_menus_for_selected_packet(FALSE);
261     set_menus_for_selected_tree_row(FALSE);
262   }
263 }
264
265 void
266 set_menu_sensitivity_meat(GtkItemFactory *ifactory, gchar *path, gint val) {
267         GtkWidget *menu = NULL;
268         
269         if((menu = gtk_item_factory_get_widget(ifactory, path)) != NULL) {
270                 gtk_widget_set_sensitive(menu,val);
271         }
272 }
273
274 static void
275 set_menu_sensitivity (gchar *path, gint val) {
276   GSList *menu_list = popup_menu_list;
277   gchar *shortpath = strrchr(path, '/');
278
279   set_menu_sensitivity_meat(factory, path, val);
280
281   while (menu_list != NULL) {
282         set_menu_sensitivity_meat(menu_list->data, shortpath, val);
283         menu_list = g_slist_next(menu_list);
284   }
285   
286 }
287
288 void
289 set_menu_object_data_meat(GtkItemFactory *ifactory, gchar *path, gchar *key, gpointer data)
290 {
291         GtkWidget *menu = NULL;
292         
293         if ((menu = gtk_item_factory_get_widget(ifactory, path)) != NULL)
294                 gtk_object_set_data(GTK_OBJECT(menu), key, data);
295 }
296
297 void
298 set_menu_object_data (gchar *path, gchar *key, gpointer data) {
299   GSList *menu_list = popup_menu_list;
300   gchar *shortpath = strrchr(path, '/');
301   
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);
306   }
307 }
308
309 gint
310 popup_menu_handler(GtkWidget *widget, GdkEvent *event, gpointer data)
311 {
312         GtkWidget *menu = NULL;
313         GdkEventButton *event_button = NULL;
314
315         if(widget == NULL || event == NULL || data == NULL) {
316                 return FALSE;
317         }
318         
319         /*
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
324          * as a pixmap.
325          */
326         menu = (GtkWidget *)data;
327         if(event->type == GDK_BUTTON_PRESS) {
328                 event_button = (GdkEventButton *) event;
329                 
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");
333                         return TRUE;
334                 }
335         }
336         return FALSE;
337 }
338
339 /* Enable or disable menu items based on whether you have a capture file
340    you've finished reading. */
341 void
342 set_menus_for_capture_file(gboolean have_capture_file)
343 {
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);
348 }
349
350 /* Enable or disable menu items based on whether you have an unsaved
351    capture file you've finished reading. */
352 void
353 set_menus_for_unsaved_capture_file(gboolean have_unsaved_capture_file)
354 {
355   set_menu_sensitivity("/File/Save", have_unsaved_capture_file);
356 }
357
358 /* Enable or disable menu items based on whether there's a capture in
359    progress. */
360 void
361 set_menus_for_capture_in_progress(gboolean capture_in_progress)
362 {
363   set_menu_sensitivity("/File/Open...", !capture_in_progress);
364   set_menu_sensitivity("/Capture/Start...", !capture_in_progress);
365   /*
366    * XXX - this doesn't yet work in Win32.
367    */
368 #ifndef _WIN32
369   set_menu_sensitivity("/Capture/Stop", capture_in_progress);
370 #endif
371 }
372
373 /* Enable or disable menu items based on whether you have some captured
374    packets. */
375 void
376 set_menus_for_captured_packets(gboolean have_captured_packets)
377 {
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);
385 }
386
387 /* Enable or disable menu items based on whether a packet is selected. */
388 void
389 set_menus_for_selected_packet(gboolean have_selected_packet)
390 {
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 && !g_resolving_actif);  
404 }
405
406 /* Enable or disable menu items based on whether a tree row is selected. */
407 void
408 set_menus_for_selected_tree_row(gboolean have_selected_tree)
409 {
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);
415         } else {
416           properties = prefs_is_registered_protocol(proto_registrar_get_abbrev(hfinfo->parent));
417         }
418   }
419   set_menu_sensitivity("/Protocol Properties...", have_selected_tree && properties);
420 }