Inactivate the "Decode As" menu item if the "Decode As" dialog box
[obnox/wireshark/wip.git] / gtk / menu.c
1 /* menu.c
2  * Menu routines
3  *
4  * $Id: menu.c,v 1.49 2001/02/11 23:02:05 guy 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 "keys.h"
62 #include "plugins.h"
63
64 GtkWidget *popup_menu_object;
65
66 #define GTK_MENU_FUNC(a) ((GtkItemFactoryCallback)(a))
67
68 static void menus_init(void);
69 static void set_menu_sensitivity (gchar *, gint);
70
71 /* This is the GtkItemFactoryEntry structure used to generate new menus.
72        Item 1: The menu path. The letter after the underscore indicates an
73                accelerator key once the menu is open.
74        Item 2: The accelerator key for the entry
75        Item 3: The callback function.
76        Item 4: The callback action.  This changes the parameters with
77                which the function is called.  The default is 0.
78        Item 5: The item type, used to define what kind of an item it is.
79                Here are the possible values:
80
81                NULL               -> "<Item>"
82                ""                 -> "<Item>"
83                "<Title>"          -> create a title item
84                "<Item>"           -> create a simple item
85                "<CheckItem>"      -> create a check item
86                "<ToggleItem>"     -> create a toggle item
87                "<RadioItem>"      -> create a radio item
88                <path>             -> path of a radio item to link against
89                "<Separator>"      -> create a separator
90                "<Branch>"         -> create an item to hold sub items (optional)
91                "<LastBranch>"     -> create a right justified branch 
92     */
93
94 /* main menu */
95 static GtkItemFactoryEntry menu_items[] =
96 {
97   {"/_File", NULL, NULL, 0, "<Branch>" },
98   {"/File/_Open...", "<control>O", GTK_MENU_FUNC(file_open_cmd_cb), 0, NULL},
99   {"/File/_Close", "<control>W", GTK_MENU_FUNC(file_close_cmd_cb), 0, NULL},
100   {"/File/_Save", "<control>S", GTK_MENU_FUNC(file_save_cmd_cb), 0, NULL},
101   {"/File/Save _As...", NULL, GTK_MENU_FUNC(file_save_as_cmd_cb), 0, NULL},
102   {"/File/_Reload", "<control>R", GTK_MENU_FUNC(file_reload_cmd_cb), 0, NULL},
103   {"/File/<separator>", NULL, NULL, 0, "<Separator>"},
104   {"/File/_Print...", NULL, GTK_MENU_FUNC(file_print_cmd_cb), 0, NULL},
105   {"/File/Print Pac_ket", "<control>P", GTK_MENU_FUNC(file_print_packet_cmd_cb), 0, NULL},
106   {"/File/<separator>", NULL, NULL, 0, "<Separator>"},
107   {"/File/_Quit", "<control>Q", GTK_MENU_FUNC(file_quit_cmd_cb), 0, NULL},
108   {"/_Edit", NULL, NULL, 0, "<Branch>" },
109 #if 0
110   /* Un-#if this when we actually implement Cut/Copy/Paste. */
111   {"/Edit/Cut", "<control>X", NULL, 0, NULL},
112   {"/Edit/Copy", "<control>C", NULL, 0, NULL},
113   {"/Edit/Paste", "<control>V", NULL, 0, NULL},
114   {"/Edit/<separator>", NULL, NULL, 0, "<Separator>"},
115 #endif
116   {"/Edit/_Find Frame...", "<control>F", GTK_MENU_FUNC(find_frame_cb), 0, NULL},
117   {"/Edit/_Go To Frame...", "<control>G", GTK_MENU_FUNC(goto_frame_cb), 0, NULL},
118   {"/Edit/<separator>", NULL, NULL, 0, "<Separator>"},
119   {"/Edit/_Mark Frame", "<control>M", GTK_MENU_FUNC(mark_frame_cb), 0, NULL},
120   {"/Edit/Mark _All Frames", NULL, GTK_MENU_FUNC(mark_all_frames_cb), 0, NULL},
121   {"/Edit/_Unmark All Frames", NULL, GTK_MENU_FUNC(unmark_all_frames_cb), 0, NULL},
122   {"/Edit/<separator>", NULL, NULL, 0, "<Separator>"},
123   {"/Edit/_Preferences...", NULL, GTK_MENU_FUNC(prefs_cb), 0, NULL},
124   {"/Edit/_Capture Filters...", NULL, GTK_MENU_FUNC(cfilter_dialog_cb), 0, NULL},
125   {"/Edit/_Display Filters...", NULL, GTK_MENU_FUNC(dfilter_dialog_cb), 0, NULL},
126   {"/Edit/P_rotocols...", NULL, GTK_MENU_FUNC(proto_cb), 0, NULL},
127 #ifdef HAVE_LIBPCAP
128   {"/_Capture", NULL, NULL, 0, "<Branch>" },
129   {"/Capture/_Start...", "<control>K", GTK_MENU_FUNC(capture_prep_cb), 0, NULL},
130   /*
131    * XXX - this doesn't yet work in Win32.
132    */
133 #ifndef _WIN32
134   {"/Capture/S_top", "<control>E", GTK_MENU_FUNC(capture_stop_cb), 0, NULL},
135 #endif /* _WIN32 */
136 #endif /* HAVE_LIBPCAP */
137   {"/_Display", NULL, NULL, 0, "<Branch>" },
138   {"/Display/_Options...", NULL, GTK_MENU_FUNC(display_opt_cb), 0, NULL},
139   {"/Display/_Match Selected", NULL, GTK_MENU_FUNC(match_selected_cb), 0, NULL},
140   {"/Display/_Colorize Display...", NULL, GTK_MENU_FUNC(color_display_cb), 0, NULL},
141   {"/Display/Collapse _All", NULL, GTK_MENU_FUNC(collapse_all_cb), 0, NULL},
142   {"/Display/_Expand All", NULL, GTK_MENU_FUNC(expand_all_cb), 0, NULL},
143   {"/Display/_Show Packet In New Window", NULL, GTK_MENU_FUNC(new_window_cb), 0, NULL},
144   {"/Display/User Specified Decodes...", NULL, GTK_MENU_FUNC(decode_show_cb), 0, NULL},
145   {"/_Tools", NULL, NULL, 0, "<Branch>" },
146 #ifdef HAVE_PLUGINS
147   {"/Tools/_Plugins...", NULL, GTK_MENU_FUNC(tools_plugins_cmd_cb), 0, NULL},
148 #endif
149   {"/Tools/_Follow TCP Stream", NULL, GTK_MENU_FUNC(follow_stream_cb), 0, NULL},
150   {"/Tools/_Decode As...", NULL, GTK_MENU_FUNC(decode_as_cb), 0, NULL},
151 /*  {"/Tools/Graph", NULL, NULL, 0, NULL}, future use */
152   {"/Tools/_Summary", NULL, GTK_MENU_FUNC(summary_open_cb), 0, NULL},
153   {"/_Help", NULL, NULL, 0, "<LastBranch>" },
154   {"/Help/_Help", NULL, GTK_MENU_FUNC(help_cb), 0, NULL},
155   {"/Help/<separator>", NULL, NULL, 0, "<Separator>"},
156   {"/Help/_About Ethereal...", NULL, GTK_MENU_FUNC(about_ethereal), 0, NULL}
157 };
158
159 /* calculate the number of menu_items */
160 static int nmenu_items = sizeof(menu_items) / sizeof(menu_items[0]);
161
162 /* packet list popup */
163 static GtkItemFactoryEntry packet_list_menu_items[] =
164 {
165         {"/Follow TCP Stream", NULL, GTK_MENU_FUNC(follow_stream_cb), 0, NULL},
166         {"/Decode As...", NULL, GTK_MENU_FUNC(decode_as_cb), 0, NULL},
167         {"/Display Filters...", NULL, GTK_MENU_FUNC(dfilter_dialog_cb), 0, NULL},
168         {"/<separator>", NULL, NULL, 0, "<Separator>"},
169         {"/Colorize Display...", NULL, GTK_MENU_FUNC(color_display_cb), 0, NULL},
170         {"/Print...", NULL, GTK_MENU_FUNC(file_print_cmd_cb), 0, NULL},
171         {"/Print Packet", NULL, GTK_MENU_FUNC(file_print_packet_cmd_cb), 0, NULL},
172         {"/Show Packet In New Window", NULL, GTK_MENU_FUNC(new_window_cb), 0, NULL}, 
173 };
174
175 static GtkItemFactoryEntry tree_view_menu_items[] =
176 {
177         {"/Follow TCP Stream", NULL, GTK_MENU_FUNC(follow_stream_cb), 0, NULL},
178         {"/Decode As...", NULL, GTK_MENU_FUNC(decode_as_cb), 0, NULL},
179         {"/Display Filters...", NULL, GTK_MENU_FUNC(dfilter_dialog_cb), 0, NULL},
180         {"/<separator>", NULL, NULL, 0, "<Separator>"},
181         {"/Resolve Name", NULL, GTK_MENU_FUNC(resolve_name_cb), 0, NULL},
182         {"/Protocol Properties...", NULL, GTK_MENU_FUNC(properties_cb), 0, NULL},
183         {"/Match Selected", NULL, GTK_MENU_FUNC(match_selected_cb), 0, NULL},
184         {"/<separator>", NULL, NULL, 0, "<Separator>"},
185         {"/Collapse All", NULL, GTK_MENU_FUNC(collapse_all_cb), 0, NULL},
186         {"/Expand All", NULL, GTK_MENU_FUNC(expand_all_cb), 0, NULL}
187 };
188
189 static GtkItemFactoryEntry hexdump_menu_items[] =
190 {
191         {"/Follow TCP Stream", NULL, GTK_MENU_FUNC(follow_stream_cb), 0, NULL},
192         {"/Decode As...", NULL, GTK_MENU_FUNC(decode_as_cb), 0, NULL},
193         {"/Display Filters...", NULL, GTK_MENU_FUNC(dfilter_dialog_cb), 0, NULL}
194 };
195
196 static int initialize = TRUE;
197 static GtkItemFactory *factory = NULL;
198 static GtkItemFactory *packet_list_menu_factory = NULL;
199 static GtkItemFactory *tree_view_menu_factory = NULL;
200 static GtkItemFactory *hexdump_menu_factory = NULL;
201
202 static GSList *popup_menu_list = NULL;
203
204 static GtkAccelGroup *grp;
205
206 void
207 get_main_menu(GtkWidget ** menubar, GtkAccelGroup ** table) {
208
209   grp = gtk_accel_group_new();
210
211   if (initialize) {
212     popup_menu_object = gtk_widget_new(GTK_TYPE_WIDGET, NULL);
213     menus_init();
214   }
215
216   if (menubar)
217     *menubar = factory->widget;
218
219   if (table)
220     *table = grp;
221 }
222
223 static void
224 menus_init(void) {
225
226   if (initialize) {
227     initialize = FALSE;
228
229     /* popup */
230
231     packet_list_menu_factory = gtk_item_factory_new(GTK_TYPE_MENU, "<main>", NULL);
232     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);
233     gtk_object_set_data(GTK_OBJECT(popup_menu_object), PM_PACKET_LIST_KEY, packet_list_menu_factory->widget);
234     popup_menu_list = g_slist_append((GSList *)popup_menu_list, packet_list_menu_factory);
235
236     tree_view_menu_factory = gtk_item_factory_new(GTK_TYPE_MENU, "<main>", NULL);
237     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);
238     gtk_object_set_data(GTK_OBJECT(popup_menu_object), PM_TREE_VIEW_KEY, tree_view_menu_factory->widget);
239     popup_menu_list = g_slist_append((GSList *)popup_menu_list, tree_view_menu_factory);
240
241     hexdump_menu_factory = gtk_item_factory_new(GTK_TYPE_MENU, "<main>", NULL);
242     gtk_item_factory_create_items_ac(hexdump_menu_factory, sizeof(hexdump_menu_items)/sizeof(hexdump_menu_items[0]), hexdump_menu_items, NULL, 2);
243     gtk_object_set_data(GTK_OBJECT(popup_menu_object), PM_HEXDUMP_KEY, hexdump_menu_factory->widget);
244     popup_menu_list = g_slist_append((GSList *)popup_menu_list, hexdump_menu_factory);
245     
246     factory = gtk_item_factory_new(GTK_TYPE_MENU_BAR, "<main>", grp);
247     gtk_item_factory_create_items_ac(factory, nmenu_items, menu_items, NULL,2);
248     set_menus_for_unsaved_capture_file(FALSE);
249     set_menus_for_capture_file(FALSE);
250 #if 0
251     /* Un-#if this when we actually implement Cut/Copy/Paste.
252        Then make sure you enable them when they can be done. */
253     set_menu_sensitivity("/Edit/Cut", FALSE);
254     set_menu_sensitivity("/Edit/Copy", FALSE);
255     set_menu_sensitivity("/Edit/Paste", FALSE);
256 #endif
257     set_menus_for_captured_packets(FALSE);
258     set_menus_for_selected_packet(FALSE);
259     set_menus_for_selected_tree_row(FALSE);
260   }
261 }
262
263 void
264 set_menu_sensitivity_meat(GtkItemFactory *ifactory, gchar *path, gint val) {
265         GtkWidget *menu = NULL;
266         
267         if((menu = gtk_item_factory_get_widget(ifactory, path)) != NULL) {
268                 gtk_widget_set_sensitive(menu,val);
269         }
270 }
271
272 static void
273 set_menu_sensitivity (gchar *path, gint val) {
274   GSList *menu_list = popup_menu_list;
275   gchar *shortpath = strrchr(path, '/');
276
277   set_menu_sensitivity_meat(factory, path, val);
278
279   while (menu_list != NULL) {
280         set_menu_sensitivity_meat(menu_list->data, shortpath, val);
281         menu_list = g_slist_next(menu_list);
282   }
283   
284 }
285
286 void
287 set_menu_object_data_meat(GtkItemFactory *ifactory, gchar *path, gchar *key, gpointer data)
288 {
289         GtkWidget *menu = NULL;
290         
291         if ((menu = gtk_item_factory_get_widget(ifactory, path)) != NULL)
292                 gtk_object_set_data(GTK_OBJECT(menu), key, data);
293 }
294
295 void
296 set_menu_object_data (gchar *path, gchar *key, gpointer data) {
297   GSList *menu_list = popup_menu_list;
298   gchar *shortpath = strrchr(path, '/');
299   
300   set_menu_object_data_meat(factory, path, key, data);
301   while (menu_list != NULL) {
302         set_menu_object_data_meat(menu_list->data, shortpath, key, data);
303         menu_list = g_slist_next(menu_list);
304   }
305 }
306
307 gint
308 popup_menu_handler(GtkWidget *widget, GdkEvent *event, gpointer data)
309 {
310         GtkWidget *menu = NULL;
311         GdkEventButton *event_button = NULL;
312
313         if(widget == NULL || event == NULL || data == NULL) {
314                 return FALSE;
315         }
316         
317         /*
318          * If we ever want to make the menu differ based on what row
319          * and/or column we're above, we'd use "gtk_clist_get_selection_info()"
320          * to find the row and column number for the coordinates; a CTree is,
321          * I guess, like a CList with one column(?) and the expander widget
322          * as a pixmap.
323          */
324         menu = (GtkWidget *)data;
325         if(event->type == GDK_BUTTON_PRESS) {
326                 event_button = (GdkEventButton *) event;
327                 
328                 if(event_button->button == 3) {
329                         gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL, event_button->button, event_button->time);
330                         gtk_signal_emit_stop_by_name(GTK_OBJECT(widget), "button_press_event");
331                         return TRUE;
332                 }
333         }
334         return FALSE;
335 }
336
337 /* Enable or disable menu items based on whether you have a capture file
338    you've finished reading. */
339 void
340 set_menus_for_capture_file(gboolean have_capture_file)
341 {
342   set_menu_sensitivity("/File/Open...", have_capture_file);
343   set_menu_sensitivity("/File/Save As...", have_capture_file);
344   set_menu_sensitivity("/File/Close", have_capture_file);
345   set_menu_sensitivity("/File/Reload", have_capture_file);
346 }
347
348 /* Enable or disable menu items based on whether you have an unsaved
349    capture file you've finished reading. */
350 void
351 set_menus_for_unsaved_capture_file(gboolean have_unsaved_capture_file)
352 {
353   set_menu_sensitivity("/File/Save", have_unsaved_capture_file);
354 }
355
356 /* Enable or disable menu items based on whether there's a capture in
357    progress. */
358 void
359 set_menus_for_capture_in_progress(gboolean capture_in_progress)
360 {
361   set_menu_sensitivity("/File/Open...", !capture_in_progress);
362   set_menu_sensitivity("/Capture/Start...", !capture_in_progress);
363   /*
364    * XXX - this doesn't yet work in Win32.
365    */
366 #ifndef _WIN32
367   set_menu_sensitivity("/Capture/Stop", capture_in_progress);
368 #endif
369 }
370
371 /* Enable or disable menu items based on whether you have some captured
372    packets. */
373 void
374 set_menus_for_captured_packets(gboolean have_captured_packets)
375 {
376   set_menu_sensitivity("/File/Print...", have_captured_packets);
377   set_menu_sensitivity("/Edit/Find Frame...", have_captured_packets);
378   set_menu_sensitivity("/Edit/Go To Frame...", have_captured_packets);
379   set_menu_sensitivity("/Display/Match Selected", have_captured_packets);
380   set_menu_sensitivity("/Display/Colorize Display...", have_captured_packets);
381   set_menu_sensitivity("/Tools/Summary", have_captured_packets);
382 }
383
384 /* Enable or disable menu items based on whether a packet is selected. */
385 void
386 set_menus_for_selected_packet(gboolean have_selected_packet)
387 {
388   set_menu_sensitivity("/File/Print Packet", have_selected_packet);
389   set_menu_sensitivity("/Edit/Mark Frame", have_selected_packet);
390   set_menu_sensitivity("/Edit/Mark All Frames", have_selected_packet);
391   set_menu_sensitivity("/Edit/Unmark All Frames", have_selected_packet);
392   set_menu_sensitivity("/Display/Collapse All", have_selected_packet);
393   set_menu_sensitivity("/Display/Expand All", have_selected_packet);
394   set_menu_sensitivity("/Display/Show Packet In New Window", have_selected_packet);
395   set_menu_sensitivity("/Tools/Follow TCP Stream",
396       have_selected_packet ? (pi.ipproto == 6) : FALSE);
397   set_menu_sensitivity("/Tools/Decode As...",
398       have_selected_packet && decode_as_ok());
399   set_menu_sensitivity("/Resolve Name", 
400       have_selected_packet && !g_resolving_actif);  
401 }
402
403 /* Enable or disable menu items based on whether a tree row is selected. */
404 void
405 set_menus_for_selected_tree_row(gboolean have_selected_tree)
406 {
407   gboolean properties = FALSE;
408   if (finfo_selected) {
409         header_field_info *hfinfo = finfo_selected->hfinfo;
410         if (hfinfo->parent == -1) {
411           properties = prefs_is_registered_protocol(hfinfo->abbrev);
412         } else {
413           properties = prefs_is_registered_protocol(proto_registrar_get_abbrev(hfinfo->parent));
414         }
415   }
416   set_menu_sensitivity("/Protocol Properties...", have_selected_tree && properties);
417 }