replace "RTT Graph" by "Round Trip Time Graph" as this is more descriptive
[obnox/wireshark/wip.git] / gtk / menu.c
1 /* menu.c
2  * Menu routines
3  *
4  * $Id: menu.c,v 1.155 2004/02/01 15:36:15 ulfl Exp $
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@ethereal.com>
8  * Copyright 1998 Gerald Combs
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <gtk/gtk.h>
30
31 #include <string.h>
32 #include <stdio.h>
33
34 #include "main.h"
35 #include "menu.h"
36 #include <epan/packet.h>
37 #include <epan/resolv.h>
38 #include "prefs.h"
39 #include "capture_dlg.h"
40 #include "color_dlg.h"
41 #include "filter_prefs.h"
42 #include "file_dlg.h"
43 #include "find_dlg.h"
44 #include "goto_dlg.h"
45 #include "summary_dlg.h"
46 #include "prefs_dlg.h"
47 #include "packet_win.h"
48 #include "print.h"
49 #include "follow_dlg.h"
50 #include "decode_as_dlg.h"
51 #include "help_dlg.h"
52 #include "supported_protos_dlg.h"
53 #include "proto_dlg.h"
54 #include "proto_hier_stats_dlg.h"
55 #include "keys.h"
56 #include <epan/plugins.h>
57 #include "tcp_graph.h"
58 #include <epan/epan_dissect.h>
59 #include "compat_macros.h"
60 #include "toolbar.h"
61 #include "gtkglobals.h"
62 #include "register.h"
63 #include "../tap.h"
64 #include "../menu.h"
65 #include "../ipproto.h"
66 #include "packet_list.h"
67 #include "ethclist.h"
68 #include "recent.h"
69 #include "../ui_util.h"
70 #include "proto_draw.h"
71 #include "simple_dialog.h"
72
73 GtkWidget *popup_menu_object;
74
75 extern void savehex_cb(GtkWidget * w, gpointer data _U_);
76
77 static void
78 clear_menu_recent_capture_file_cmd_cb(GtkWidget *w, gpointer unused _U_);
79
80 #define GTK_MENU_FUNC(a) ((GtkItemFactoryCallback)(a))
81
82 static void menus_init(void);
83 static void set_menu_sensitivity (GtkItemFactory *, const gchar *, gint);
84 static void main_toolbar_show_cb(GtkWidget *w _U_, gpointer d _U_);
85 static void filter_toolbar_show_cb(GtkWidget *w _U_, gpointer d _U_);
86 static void packet_list_show_cb(GtkWidget *w _U_, gpointer d _U_);
87 static void tree_view_show_cb(GtkWidget *w _U_, gpointer d _U_);
88 static void byte_view_show_cb(GtkWidget *w _U_, gpointer d _U_);
89 static void statusbar_show_cb(GtkWidget *w _U_, gpointer d _U_);
90 static void timestamp_absolute_cb(GtkWidget *w _U_, gpointer d _U_);
91 static void timestamp_absolute_date_cb(GtkWidget *w _U_, gpointer d _U_);
92 static void timestamp_relative_cb(GtkWidget *w _U_, gpointer d _U_);
93 static void timestamp_delta_cb(GtkWidget *w _U_, gpointer d _U_);
94 static void name_resolution_mac_cb(GtkWidget *w _U_, gpointer d _U_);
95 static void name_resolution_network_cb(GtkWidget *w _U_, gpointer d _U_);
96 static void name_resolution_transport_cb(GtkWidget *w _U_, gpointer d _U_);
97 static void auto_scroll_live_cb(GtkWidget *w _U_, gpointer d _U_);
98
99 /* This is the GtkItemFactoryEntry structure used to generate new menus.
100        Item 1: The menu path. The letter after the underscore indicates an
101                accelerator key once the menu is open.
102        Item 2: The accelerator key for the entry
103        Item 3: The callback function.
104        Item 4: The callback action.  This changes the parameters with
105                which the function is called.  The default is 0.
106        Item 5: The item type, used to define what kind of an item it is.
107                Here are the possible values:
108
109                NULL               -> "<Item>"
110                ""                 -> "<Item>"
111                "<Title>"          -> create a title item
112                "<Item>"           -> create a simple item
113                "<ImageItem>"      -> create an item holding an image (gtk2)
114                "<StockItem>"      -> create an item holding a stock image (gtk2)
115                "<CheckItem>"      -> create a check item
116                "<ToggleItem>"     -> create a toggle item
117                "<RadioItem>"      -> create a radio item
118                <path>             -> path of a radio item to link against
119                "<Separator>"      -> create a separator
120                "<Tearoff>"        -> create a tearoff separator (gtk2)
121                "<Branch>"         -> create an item to hold sub items (optional)
122                "<LastBranch>"     -> create a right justified branch
123        Item 6: extra data needed for ImageItem and StockItem (gtk2)
124     */
125
126 /* main menu */
127 static GtkItemFactoryEntry menu_items[] =
128 {
129     ITEM_FACTORY_ENTRY("/_File", NULL, NULL, 0, "<Branch>", NULL),
130     ITEM_FACTORY_STOCK_ENTRY("/File/_Open...", "<control>O", file_open_cmd_cb,
131                              0, GTK_STOCK_OPEN),
132     ITEM_FACTORY_ENTRY("/File/Open _Recent", NULL, NULL, 0, "<Branch>", NULL),
133     ITEM_FACTORY_STOCK_ENTRY("/File/_Close", "<control>W", file_close_cmd_cb,
134                              0, GTK_STOCK_CLOSE),
135     ITEM_FACTORY_ENTRY("/File/<separator>", NULL, NULL, 0, "<Separator>", NULL),
136     ITEM_FACTORY_STOCK_ENTRY("/File/_Save", "<control>S", file_save_cmd_cb,
137                              0, GTK_STOCK_SAVE),
138     ITEM_FACTORY_STOCK_ENTRY("/File/Save _As...", "<shift><control>S", file_save_as_cmd_cb,
139                              0, GTK_STOCK_SAVE_AS),
140     ITEM_FACTORY_ENTRY("/File/<separator>", NULL, NULL, 0, "<Separator>", NULL),
141     ITEM_FACTORY_ENTRY("/File/_Export", NULL, NULL, 0, "<Branch>", NULL),
142     ITEM_FACTORY_ENTRY("/File/_Export/_Selected Packet Bytes...", "<control>H", savehex_cb,
143                              0, NULL, NULL),
144     ITEM_FACTORY_ENTRY("/File/<separator>", NULL, NULL, 0, "<Separator>", NULL),
145     ITEM_FACTORY_STOCK_ENTRY("/File/_Print...", "<control>P", file_print_cmd_cb,
146                              0, GTK_STOCK_PRINT),
147     ITEM_FACTORY_ENTRY("/File/<separator>", NULL, NULL, 0, "<Separator>", NULL),
148     ITEM_FACTORY_STOCK_ENTRY("/File/_Quit", "<control>Q", file_quit_cmd_cb,
149                              0, GTK_STOCK_QUIT),
150     ITEM_FACTORY_ENTRY("/_Edit", NULL, NULL, 0, "<Branch>", NULL),
151 #if 0
152     /* Un-#if this when we actually implement Cut/Copy/Paste. */
153     ITEM_FACTORY_STOCK_ENTRY("/Edit/Cut", "<control>X", NULL,
154                              0, GTK_STOCK_CUT),
155     ITEM_FACTORY_STOCK_ENTRY("/Edit/Copy", "<control>C", NULL,
156                              0, GTK_STOCK_COPY),
157     ITEM_FACTORY_STOCK_ENTRY("/Edit/Paste", "<control>V", NULL,
158                              0, GTK_STOCK_PASTE),
159     ITEM_FACTORY_ENTRY("/Edit/<separator>", NULL, NULL, 0, "<Separator>"),
160 #endif
161     ITEM_FACTORY_STOCK_ENTRY("/Edit/_Find Packet...", "<control>F",
162                              find_frame_cb, 0, GTK_STOCK_FIND),
163     ITEM_FACTORY_STOCK_ENTRY("/Edit/Find Ne_xt", "<control>N", find_next_cb,
164                              0, GTK_STOCK_GO_FORWARD),
165     ITEM_FACTORY_STOCK_ENTRY("/Edit/Find Pre_vious", "<control>B",
166                              find_previous_cb, 0, GTK_STOCK_GO_BACK),
167     ITEM_FACTORY_ENTRY("/Edit/<separator>", NULL, NULL, 0, "<Separator>", NULL),
168     ITEM_FACTORY_STOCK_ENTRY("/Edit/Go To Firs_t Packet", NULL,
169                              goto_top_frame_cb, 0, GTK_STOCK_GOTO_TOP),
170     ITEM_FACTORY_STOCK_ENTRY("/Edit/Go To _Last Packet", NULL,
171                              goto_bottom_frame_cb, 0, GTK_STOCK_GOTO_BOTTOM),
172     ITEM_FACTORY_STOCK_ENTRY("/Edit/_Go To Packet...", "<control>G",
173                              goto_frame_cb, 0, GTK_STOCK_JUMP_TO),
174     ITEM_FACTORY_ENTRY("/Edit/Go To _Corresponding Packet", NULL, goto_framenum_cb,
175                        0, NULL, NULL),
176     ITEM_FACTORY_ENTRY("/Edit/<separator>", NULL, NULL, 0, "<Separator>", NULL),
177     ITEM_FACTORY_ENTRY("/Edit/Time _Reference", NULL, NULL, 0, "<Branch>", NULL),
178     ITEM_FACTORY_ENTRY("/Edit/Time Reference/Set Time Reference (toggle)", "<control>T", reftime_frame_cb, 0, NULL, NULL),
179     ITEM_FACTORY_ENTRY("/Edit/Time Reference/Find Next", NULL, reftime_frame_cb, 1, NULL, NULL),
180     ITEM_FACTORY_ENTRY("/Edit/Time Reference/Find Previous", NULL, reftime_frame_cb, 2, NULL, NULL),
181     ITEM_FACTORY_ENTRY("/Edit/_Mark Packet", "<control>M", mark_frame_cb,
182                        0, NULL, NULL),
183     ITEM_FACTORY_ENTRY("/Edit/Mark _All Packets", NULL, mark_all_frames_cb,
184                        0, NULL, NULL),
185     ITEM_FACTORY_ENTRY("/Edit/_Unmark All Packets", NULL, unmark_all_frames_cb,
186                        0, NULL, NULL),
187     ITEM_FACTORY_ENTRY("/Edit/<separator>", NULL, NULL, 0, "<Separator>", NULL),
188     ITEM_FACTORY_STOCK_ENTRY("/Edit/_Preferences...", "<shift><control>P", prefs_cb,
189                              0, GTK_STOCK_PREFERENCES),
190     ITEM_FACTORY_ENTRY("/_View", NULL, NULL, 0, "<Branch>", NULL),
191     ITEM_FACTORY_ENTRY("/View/_Show", NULL, NULL, 0, "<Branch>", NULL),
192     ITEM_FACTORY_ENTRY("/View/Show/Main Toolbar", NULL, main_toolbar_show_cb, 0, "<CheckItem>", NULL),
193     ITEM_FACTORY_ENTRY("/View/Show/Filter Toolbar", NULL, filter_toolbar_show_cb, 0, "<CheckItem>", NULL),
194     ITEM_FACTORY_ENTRY("/View/Show/<separator>", NULL, NULL, 0, "<Separator>", NULL),
195     ITEM_FACTORY_ENTRY("/View/Show/Packet List", NULL, packet_list_show_cb, 0, "<CheckItem>", NULL),
196     ITEM_FACTORY_ENTRY("/View/Show/Packet Details", NULL, tree_view_show_cb, 0, "<CheckItem>", NULL),
197     ITEM_FACTORY_ENTRY("/View/Show/Packet Bytes", NULL, byte_view_show_cb, 0, "<CheckItem>", NULL),
198     ITEM_FACTORY_ENTRY("/View/Show/<separator>", NULL, NULL, 0, "<Separator>", NULL),
199     ITEM_FACTORY_ENTRY("/View/Show/Status Bar", NULL, statusbar_show_cb, 0, "<CheckItem>", NULL),
200     ITEM_FACTORY_ENTRY("/View/_Time Display Format", NULL, NULL, 0, "<Branch>", NULL),
201     ITEM_FACTORY_ENTRY("/View/Time Display Format/Time of Day", NULL, timestamp_absolute_cb, 
202                         0, "<RadioItem>", NULL),
203     ITEM_FACTORY_ENTRY("/View/Time Display Format/Date and Time of Day", NULL, timestamp_absolute_date_cb, 
204                         0, "/View/Time Display Format/Time of Day", NULL),
205     ITEM_FACTORY_ENTRY("/View/Time Display Format/Seconds Since Beginning of Capture", NULL, timestamp_relative_cb, 
206                         0, "/View/Time Display Format/Time of Day", NULL),
207     ITEM_FACTORY_ENTRY("/View/Time Display Format/Seconds Since Previous Packet", NULL, timestamp_delta_cb, 
208                         0, "/View/Time Display Format/Time of Day", NULL),
209     ITEM_FACTORY_ENTRY("/View/_Name Resolution", NULL, NULL, 0, "<Branch>", NULL),
210     ITEM_FACTORY_ENTRY("/View/Name Resolution/Enable for _MAC Layer", NULL, name_resolution_mac_cb, 0, "<CheckItem>", NULL),
211     ITEM_FACTORY_ENTRY("/View/Name Resolution/Enable for _Network Layer", NULL, name_resolution_mac_cb, 0, "<CheckItem>", NULL),
212     ITEM_FACTORY_ENTRY("/View/Name Resolution/Enable for _Transport Layer", NULL, name_resolution_transport_cb, 0, "<CheckItem>", NULL),
213     ITEM_FACTORY_ENTRY("/View/Auto Scroll in Live Capture", NULL, auto_scroll_live_cb, 0, "<CheckItem>", NULL),
214     ITEM_FACTORY_ENTRY("/View/<separator>", NULL, NULL, 0, "<Separator>", NULL),
215     ITEM_FACTORY_STOCK_ENTRY("/View/Zoom In", "<control>plus", view_zoom_in_cb,
216                              0, GTK_STOCK_ZOOM_IN),
217     ITEM_FACTORY_STOCK_ENTRY("/View/Zoom Out", "<control>minus", view_zoom_out_cb,
218                              0, GTK_STOCK_ZOOM_OUT),
219     ITEM_FACTORY_STOCK_ENTRY("/View/Normal Size", "<control>equal", view_zoom_100_cb,
220                              0, GTK_STOCK_ZOOM_100),
221     ITEM_FACTORY_ENTRY("/View/<separator>", NULL, NULL, 0, "<Separator>", NULL),
222     ITEM_FACTORY_ENTRY("/View/Collapse _All", NULL, collapse_all_cb,
223                        0, NULL, NULL),
224     ITEM_FACTORY_ENTRY("/View/_Expand All", NULL, expand_all_cb,
225                        0, NULL, NULL),
226     ITEM_FACTORY_ENTRY("/View/<separator>", NULL, NULL, 0, "<Separator>", NULL),
227     ITEM_FACTORY_STOCK_ENTRY("/View/_Coloring Rules...", NULL, color_display_cb,
228                        0, GTK_STOCK_SELECT_COLOR),
229     ITEM_FACTORY_ENTRY("/View/<separator>", NULL, NULL, 0, "<Separator>", NULL),
230     ITEM_FACTORY_ENTRY("/View/_Show Packet In New Window", NULL,
231                        new_window_cb, 0, NULL, NULL),
232     ITEM_FACTORY_STOCK_ENTRY("/View/_Reload", "<control>R", file_reload_cmd_cb,
233                              0, GTK_STOCK_REFRESH),
234 #ifdef HAVE_LIBPCAP
235     ITEM_FACTORY_ENTRY("/_Capture", NULL, NULL, 0, "<Branch>", NULL),
236     ITEM_FACTORY_STOCK_ENTRY("/Capture/_Start...", "<control>K",
237                              capture_prep_cb, 0, ETHEREAL_STOCK_CAPTURE_START),
238     ITEM_FACTORY_STOCK_ENTRY("/Capture/S_top", "<control>E", capture_stop_cb,
239                              0, GTK_STOCK_STOP),
240     ITEM_FACTORY_STOCK_ENTRY("/Capture/_Capture Filters...", NULL, cfilter_dialog_cb,
241                        0, ETHEREAL_STOCK_CAPTURE_FILTER),
242 #endif /* HAVE_LIBPCAP */
243     ITEM_FACTORY_ENTRY("/_Analyze", NULL, NULL, 0, "<Branch>", NULL),
244     ITEM_FACTORY_STOCK_ENTRY("/Analyze/_Display Filters...", NULL, dfilter_dialog_cb,
245                        0, ETHEREAL_STOCK_DISPLAY_FILTER),
246     ITEM_FACTORY_ENTRY("/Analyze/_Match", NULL, NULL, 0, "<Branch>", NULL),
247     ITEM_FACTORY_ENTRY("/Analyze/Match/_Selected", NULL,
248                        match_selected_cb_replace_ptree, 0, NULL, NULL),
249     ITEM_FACTORY_ENTRY("/Analyze/Match/_Not Selected", NULL,
250                        match_selected_cb_not_ptree, 0, NULL, NULL),
251     ITEM_FACTORY_ENTRY("/Analyze/Match/_And Selected", NULL,
252                        match_selected_cb_and_ptree, 0, NULL, NULL),
253     ITEM_FACTORY_ENTRY("/Analyze/Match/_Or Selected", NULL,
254                        match_selected_cb_or_ptree, 0, NULL, NULL),
255     ITEM_FACTORY_ENTRY("/Analyze/Match/A_nd Not Selected", NULL,
256                        match_selected_cb_and_ptree_not, 0, NULL, NULL),
257     ITEM_FACTORY_ENTRY("/Analyze/Match/O_r Not Selected", NULL,
258                        match_selected_cb_or_ptree_not, 0, NULL, NULL),
259     ITEM_FACTORY_ENTRY("/Analyze/_Prepare", NULL, NULL, 0, "<Branch>", NULL),
260     ITEM_FACTORY_ENTRY("/Analyze/Prepare/_Selected", NULL,
261                        prepare_selected_cb_replace_ptree, 0, NULL, NULL),
262     ITEM_FACTORY_ENTRY("/Analyze/Prepare/_Not Selected", NULL,
263                        prepare_selected_cb_not_ptree, 0, NULL, NULL),
264     ITEM_FACTORY_ENTRY("/Analyze/Prepare/_And Selected", NULL,
265                        prepare_selected_cb_and_ptree, 0, NULL, NULL),
266     ITEM_FACTORY_ENTRY("/Analyze/Prepare/_Or Selected", NULL,
267                        prepare_selected_cb_or_ptree, 0, NULL, NULL),
268     ITEM_FACTORY_ENTRY("/Analyze/Prepare/A_nd Not Selected", NULL,
269                        prepare_selected_cb_and_ptree_not, 0, NULL, NULL),
270     ITEM_FACTORY_ENTRY("/Analyze/Prepare/O_r Not Selected", NULL,
271                        prepare_selected_cb_or_ptree_not, 0, NULL, NULL),
272     ITEM_FACTORY_ENTRY("/Analyze/<separator>", NULL, NULL, 0, "<Separator>", NULL),
273     ITEM_FACTORY_ENTRY("/Analyze/_Enabled Protocols...", "<shift><control>R", proto_cb, 0, NULL, NULL),
274     ITEM_FACTORY_ENTRY("/Analyze/Decode _As...", NULL, decode_as_cb,
275                        0, NULL, NULL),
276     ITEM_FACTORY_ENTRY("/Analyze/_User Specified Decodes...", NULL,
277                        decode_show_cb, 0, NULL, NULL),
278     ITEM_FACTORY_ENTRY("/Analyze/<separator>", NULL, NULL, 0, "<Separator>", NULL),
279     ITEM_FACTORY_ENTRY("/Analyze/_Follow TCP Stream", NULL, follow_stream_cb,
280                        0, NULL, NULL),
281 /*  {"/Analyze/Graph", NULL, NULL, 0, NULL}, future use */
282     ITEM_FACTORY_ENTRY("/Analyze/_TCP Stream Analysis", NULL, NULL,
283                        0, "<Branch>", NULL),
284     ITEM_FACTORY_ENTRY("/Analyze/TCP Stream Analysis/Time-Sequence Graph (Stevens)",
285                        NULL, tcp_graph_cb, 0, NULL, NULL),
286     ITEM_FACTORY_ENTRY("/Analyze/TCP Stream Analysis/Time-Sequence Graph (tcptrace)",
287                        NULL, tcp_graph_cb, 1, NULL, NULL),
288     ITEM_FACTORY_ENTRY("/Analyze/TCP Stream Analysis/Throughput Graph", NULL,
289                        tcp_graph_cb, 2, NULL, NULL),
290     ITEM_FACTORY_ENTRY("/Analyze/TCP Stream Analysis/Round Trip Time Graph", NULL,
291                        tcp_graph_cb, 3, NULL, NULL),
292     ITEM_FACTORY_ENTRY("/Analyze/<separator>", NULL, NULL, 0, "<Separator>", NULL),
293     ITEM_FACTORY_ENTRY("/Analyze/Summar_y", NULL, summary_open_cb, 0, NULL, NULL),
294     ITEM_FACTORY_ENTRY("/Analyze/Protocol _Hierarchy Statistics", NULL,
295                        proto_hier_stats_cb, 0, NULL, NULL),
296     ITEM_FACTORY_ENTRY("/_Help", NULL, NULL, 0, "<Branch>", NULL),
297     ITEM_FACTORY_STOCK_ENTRY("/Help/_Contents", "F1", help_cb, 0, GTK_STOCK_HELP),
298     ITEM_FACTORY_ENTRY("/Help/_Supported Protocols", NULL, supported_cb, 0, NULL, NULL),
299     ITEM_FACTORY_ENTRY("/Help/<separator>", NULL, NULL, 0, "<Separator>", NULL),
300 #ifdef HAVE_PLUGINS
301     ITEM_FACTORY_ENTRY("/Help/About _Plugins", NULL, tools_plugins_cmd_cb,
302                        0, NULL, NULL),
303 #endif /* HAVE_PLUGINS */
304     ITEM_FACTORY_ENTRY("/Help/_About Ethereal", NULL, about_ethereal,
305                        0, NULL, NULL)
306 };
307
308
309 /* calculate the number of menu_items */
310 static int nmenu_items = sizeof(menu_items) / sizeof(menu_items[0]);
311
312 /* packet list popup */
313 static GtkItemFactoryEntry packet_list_menu_items[] =
314 {
315     ITEM_FACTORY_ENTRY("/Follow TCP Stream", NULL, follow_stream_cb,
316                        0, NULL, NULL),
317     ITEM_FACTORY_ENTRY("/Decode As...", NULL, decode_as_cb, 0, NULL, NULL),
318     ITEM_FACTORY_ENTRY("/Display Filters...", NULL, dfilter_dialog_cb,
319                        0, NULL, NULL),
320     ITEM_FACTORY_ENTRY("/<separator>", NULL, NULL, 0, "<Separator>", NULL),
321     ITEM_FACTORY_ENTRY("/Mark Packet", NULL, mark_frame_cb, 0, NULL, NULL),
322     ITEM_FACTORY_ENTRY("/Time Reference", NULL, NULL, 0, "<Branch>", NULL),
323     ITEM_FACTORY_ENTRY("/Time Reference/Set Time Reference (toggle)", NULL, reftime_frame_cb, 0, NULL, NULL),
324     ITEM_FACTORY_ENTRY("/Time Reference/Find Next", NULL, reftime_frame_cb, 1, NULL, NULL),
325     ITEM_FACTORY_ENTRY("/Time Reference/Find Previous", NULL, reftime_frame_cb, 2, NULL, NULL),
326     ITEM_FACTORY_ENTRY("/Match", NULL, NULL, 0, "<Branch>", NULL),
327     ITEM_FACTORY_ENTRY("/Match/_Selected", NULL,
328                        match_selected_cb_replace_plist, 0, NULL, NULL),
329     ITEM_FACTORY_ENTRY("/Match/_Not Selected", NULL,
330                        match_selected_cb_not_plist, 0, NULL, NULL),
331     ITEM_FACTORY_ENTRY("/Match/_And Selected", NULL,
332                        match_selected_cb_and_plist, 0, NULL, NULL),
333     ITEM_FACTORY_ENTRY("/Match/_Or Selected", NULL, match_selected_cb_or_plist,
334                        0, NULL, NULL),
335     ITEM_FACTORY_ENTRY("/Match/A_nd Not Selected", NULL,
336                        match_selected_cb_and_plist_not, 0, NULL, NULL),
337     ITEM_FACTORY_ENTRY("/Match/O_r Not Selected", NULL,
338                        match_selected_cb_or_plist_not, 0, NULL, NULL),
339     ITEM_FACTORY_ENTRY("/Prepare", NULL, NULL, 0, "<Branch>", NULL),
340     ITEM_FACTORY_ENTRY("/Prepare/_Selected", NULL,
341                        prepare_selected_cb_replace_plist, 0, NULL, NULL),
342     ITEM_FACTORY_ENTRY("/Prepare/_Not Selected", NULL,
343                        prepare_selected_cb_not_plist, 0, NULL, NULL),
344     ITEM_FACTORY_ENTRY("/Prepare/_And Selected", NULL,
345                        prepare_selected_cb_and_plist, 0, NULL, NULL),
346     ITEM_FACTORY_ENTRY("/Prepare/_Or Selected", NULL,
347                        prepare_selected_cb_or_plist, 0, NULL, NULL),
348     ITEM_FACTORY_ENTRY("/Prepare/A_nd Not Selected", NULL,
349                        prepare_selected_cb_and_plist_not, 0, NULL, NULL),
350     ITEM_FACTORY_ENTRY("/Prepare/O_r Not Selected", NULL,
351                        prepare_selected_cb_or_plist_not, 0, NULL, NULL),
352     ITEM_FACTORY_ENTRY("/<separator>", NULL, NULL, 0, "<Separator>", NULL),
353     ITEM_FACTORY_ENTRY("/Coloring Rules...", NULL, color_display_cb,
354                        0, NULL, NULL),
355     ITEM_FACTORY_ENTRY("/Print...", NULL, file_print_cmd_cb, 0, NULL, NULL),
356     ITEM_FACTORY_ENTRY("/Show Packet In New Window", NULL, new_window_cb,
357                        0, NULL, NULL),
358 };
359
360 static GtkItemFactoryEntry tree_view_menu_items[] =
361 {
362     ITEM_FACTORY_ENTRY("/Follow TCP Stream", NULL, follow_stream_cb,
363                        0, NULL, NULL),
364     ITEM_FACTORY_ENTRY("/Decode As...", NULL, decode_as_cb, 0, NULL, NULL),
365     ITEM_FACTORY_ENTRY("/Display Filters...", NULL, dfilter_dialog_cb,
366                        0, NULL, NULL),
367     ITEM_FACTORY_ENTRY("/<separator>", NULL, NULL, 0, "<Separator>", NULL),
368     ITEM_FACTORY_ENTRY("/_Resolve Name", NULL, resolve_name_cb, 0, NULL, NULL),
369     ITEM_FACTORY_ENTRY("/_Go To Corresponding Packet", NULL, goto_framenum_cb, 0, NULL, NULL),
370     ITEM_FACTORY_ENTRY("/_Export Selected Packet Bytes...", NULL, savehex_cb,
371                        0, NULL, NULL),
372     ITEM_FACTORY_ENTRY("/Protocol Properties...", NULL, properties_cb,
373                        0, NULL, NULL),
374     ITEM_FACTORY_ENTRY("/Match", NULL, NULL, 0, "<Branch>", NULL),
375     ITEM_FACTORY_ENTRY("/Match/_Selected", NULL,
376                        match_selected_cb_replace_ptree, 0, NULL, NULL),
377     ITEM_FACTORY_ENTRY("/Match/_Not Selected", NULL,
378                        match_selected_cb_not_ptree, 0, NULL, NULL),
379     ITEM_FACTORY_ENTRY("/Match/_And Selected", NULL,
380                        match_selected_cb_and_ptree, 0, NULL, NULL),
381     ITEM_FACTORY_ENTRY("/Match/_Or Selected", NULL, match_selected_cb_or_ptree,
382                        0, NULL, NULL),
383     ITEM_FACTORY_ENTRY("/Match/A_nd Not Selected", NULL,
384                        match_selected_cb_and_ptree_not, 0, NULL, NULL),
385     ITEM_FACTORY_ENTRY("/Match/O_r Not Selected", NULL,
386                        match_selected_cb_or_ptree_not, 0, NULL, NULL),
387     ITEM_FACTORY_ENTRY("/Prepare", NULL, NULL, 0, "<Branch>", NULL),
388     ITEM_FACTORY_ENTRY("/Prepare/_Selected", NULL,
389                        prepare_selected_cb_replace_ptree, 0, NULL, NULL),
390     ITEM_FACTORY_ENTRY("/Prepare/_Not Selected", NULL,
391                        prepare_selected_cb_not_ptree, 0, NULL, NULL),
392     ITEM_FACTORY_ENTRY("/Prepare/_And Selected", NULL,
393                        prepare_selected_cb_and_ptree, 0, NULL, NULL),
394     ITEM_FACTORY_ENTRY("/Prepare/_Or Selected", NULL,
395                        prepare_selected_cb_or_ptree, 0, NULL, NULL),
396     ITEM_FACTORY_ENTRY("/Prepare/A_nd Not Selected", NULL,
397                        prepare_selected_cb_and_ptree_not, 0, NULL, NULL),
398     ITEM_FACTORY_ENTRY("/Prepare/O_r Not Selected", NULL,
399                        prepare_selected_cb_or_ptree_not, 0, NULL, NULL),
400     ITEM_FACTORY_ENTRY("/<separator>", NULL, NULL, 0, "<Separator>", NULL),
401     ITEM_FACTORY_ENTRY("/Collapse All", NULL, collapse_all_cb, 0, NULL, NULL),
402     ITEM_FACTORY_ENTRY("/Expand All", NULL, expand_all_cb, 0, NULL, NULL)
403 };
404
405 static GtkItemFactoryEntry hexdump_menu_items[] =
406 {
407     ITEM_FACTORY_ENTRY("/Follow TCP Stream", NULL, follow_stream_cb,
408                        0, NULL, NULL),
409     ITEM_FACTORY_ENTRY("/Decode As...", NULL, decode_as_cb, 0, NULL, NULL),
410     ITEM_FACTORY_ENTRY("/Display Filters...", NULL, dfilter_dialog_cb,
411                        0, NULL, NULL),
412     ITEM_FACTORY_ENTRY("/Export Selected Packet Bytes...", NULL, savehex_cb,
413                        0, NULL, NULL)
414 };
415
416 static int initialize = TRUE;
417 static GtkItemFactory *main_menu_factory = NULL;
418 static GtkItemFactory *packet_list_menu_factory = NULL;
419 static GtkItemFactory *tree_view_menu_factory = NULL;
420 static GtkItemFactory *hexdump_menu_factory = NULL;
421
422 static GSList *popup_menu_list = NULL;
423
424 static GtkAccelGroup *grp;
425
426 GtkWidget *
427 main_menu_new(GtkAccelGroup ** table) {
428   GtkWidget *menubar;
429
430   grp = gtk_accel_group_new();
431
432   if (initialize)
433     menus_init();
434
435   menubar = main_menu_factory->widget;
436
437   if (table)
438     *table = grp;
439
440   return menubar;
441 }
442
443 static void
444 menus_init(void) {
445
446   if (initialize) {
447     initialize = FALSE;
448
449     /* popup */
450     packet_list_menu_factory = gtk_item_factory_new(GTK_TYPE_MENU, "<main>", NULL);
451     popup_menu_object = gtk_menu_new();
452     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, popup_menu_object, 2);
453     OBJECT_SET_DATA(popup_menu_object, PM_PACKET_LIST_KEY,
454                     packet_list_menu_factory->widget);
455     popup_menu_list = g_slist_append((GSList *)popup_menu_list, packet_list_menu_factory);
456
457     tree_view_menu_factory = gtk_item_factory_new(GTK_TYPE_MENU, "<main>", NULL);
458     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, popup_menu_object, 2);
459     OBJECT_SET_DATA(popup_menu_object, PM_TREE_VIEW_KEY,
460                     tree_view_menu_factory->widget);
461     popup_menu_list = g_slist_append((GSList *)popup_menu_list, tree_view_menu_factory);
462
463     hexdump_menu_factory = gtk_item_factory_new(GTK_TYPE_MENU, "<main>", NULL);
464     gtk_item_factory_create_items_ac(hexdump_menu_factory, sizeof(hexdump_menu_items)/sizeof(hexdump_menu_items[0]), hexdump_menu_items, popup_menu_object, 2);
465     OBJECT_SET_DATA(popup_menu_object, PM_HEXDUMP_KEY,
466                     hexdump_menu_factory->widget);
467     popup_menu_list = g_slist_append((GSList *)popup_menu_list, hexdump_menu_factory);
468
469     /* main */
470     main_menu_factory = gtk_item_factory_new(GTK_TYPE_MENU_BAR, "<main>", grp);
471     gtk_item_factory_create_items_ac(main_menu_factory, nmenu_items, menu_items, NULL, 2);
472     register_all_tap_menus();   /* must be done after creating the main menu */
473
474     /* Initialize enabled/disabled state of menu items */
475     set_menus_for_unsaved_capture_file(FALSE);
476     set_menus_for_capture_file(FALSE);
477 #if 0
478     /* Un-#if this when we actually implement Cut/Copy/Paste.
479        Then make sure you enable them when they can be done. */
480     set_menu_sensitivity(main_menu_factory, "/Edit/Cut", FALSE);
481     set_menu_sensitivity(main_menu_factory, "/Edit/Copy", FALSE);
482     set_menu_sensitivity(main_menu_factory, "/Edit/Paste", FALSE);
483 #endif
484
485     set_menus_for_captured_packets(FALSE);
486     set_menus_for_selected_packet(&cfile);
487     set_menus_for_selected_tree_row(&cfile);
488
489     /* init with an empty recent files list */
490     clear_menu_recent_capture_file_cmd_cb(NULL, NULL);
491   }
492 }
493
494 typedef struct _menu_item {
495         char    *name;
496         gboolean enabled;
497         gboolean (*selected_packet_enabled)(frame_data *, epan_dissect_t *);
498         gboolean (*selected_tree_row_enabled)(field_info *);
499         struct _menu_item *parent;
500         struct _menu_item *children;
501         struct _menu_item *next;
502 } menu_item_t;
503
504 static menu_item_t tap_menu_tree_root;
505
506 /*
507  * Add a new menu item for a tap.
508  * This must be called after we've created the main menu, so it can't
509  * be called from the routine that registers taps - we have to introduce
510  * another per-tap registration routine.
511  *
512  * "callback" gets called when the menu item is selected; it should do
513  * the work of creating the tap window.
514  *
515  * "selected_packet_enabled" gets called by "set_menus_for_selected_packet()";
516  * it's passed a Boolean that's TRUE if a packet is selected and FALSE
517  * otherwise, and should return TRUE if the tap will work now (which
518  * might depend on whether a packet is selected and, if one is, on the
519  * packet) and FALSE if not.
520  *
521  * "selected_tree_row_enabled" gets called by
522  * "set_menus_for_selected_tree_row()"; it's passed a Boolean that's TRUE if
523  * a protocol tree row is selected and FALSE otherwise, and should return
524  * TRUE if the tap will work now (which might depend on whether a tree row
525  * is selected and, if one is, on the tree row) and FALSE if not.
526  */
527 void
528 register_tap_menu_item(char *name, GtkItemFactoryCallback callback,
529     gboolean (*selected_packet_enabled)(frame_data *, epan_dissect_t *),
530     gboolean (*selected_tree_row_enabled)(field_info *),
531     gpointer callback_data)
532 {
533         static const char toolspath[] = "/Analyze/";
534         char *p;
535         char *menupath;
536         size_t menupathlen;
537         GtkItemFactoryEntry *entry;
538         menu_item_t *curnode, *child;
539
540         /*
541          * The menu path must be relative.
542          */
543         g_assert(*name != '/');
544
545         /*
546          * Create any submenus required.
547          */
548         curnode = &tap_menu_tree_root;
549         p = name;
550         while ((p = strchr(p, '/')) != NULL) {
551                 /*
552                  * OK, everything between "name" and "p" is
553                  * a menu relative subtree into which the menu item
554                  * will be placed.
555                  *
556                  * Construct the absolute path name of that subtree.
557                  */
558                 menupathlen = sizeof toolspath + (p - name);
559                 menupath = g_malloc(menupathlen);
560                 strcpy(menupath, toolspath);
561                 strncat(menupath, name, p - name);
562
563                 /*
564                  * Does there exist an entry with that path at this
565                  * level of the Analyze menu tree?
566                  */
567                 for (child = curnode->children; child != NULL;
568                     child = child->next) {
569                         if (strcmp(child->name, menupath) == 0)
570                                 break;
571                 }
572                 if (child == NULL) {
573                         /*
574                          * No.  Create such an item as a subtree, and
575                          * add it to the Tools menu tree.
576                          */
577                         entry = g_malloc0(sizeof (GtkItemFactoryEntry));
578                         entry->path = menupath;
579                         entry->item_type = "<Branch>";
580                         gtk_item_factory_create_item(main_menu_factory, entry,
581                             NULL, 2);
582                         set_menu_sensitivity(main_menu_factory, menupath,
583                             FALSE);     /* no children yet */
584                         child = g_malloc(sizeof (menu_item_t));
585                         child->name = menupath;
586                         child->selected_packet_enabled = NULL;
587                         child->selected_tree_row_enabled = NULL;
588                         child->enabled = FALSE; /* no children yet */
589                         child->parent = curnode;
590                         child->children = NULL;
591                         child->next = curnode->children;
592                         curnode->children = child;
593                 } else {
594                         /*
595                          * Yes.  We don't need "menupath" any more.
596                          */
597                         g_free(menupath);
598                 }
599                 curnode = child;
600
601                 /*
602                  * Skip over the '/' we found.
603                  */
604                 p++;
605         }
606
607         /*
608          * Construct the main menu path for the menu item.
609          *
610          * "sizeof toolspath" includes the trailing '\0', so the sum
611          * of that and the length of "name" is enough to hold a string
612          * containing their concatenation.
613          */
614         menupathlen = sizeof toolspath + strlen(name);
615         menupath = g_malloc(menupathlen);
616         strcpy(menupath, toolspath);
617         strcat(menupath, name);
618
619         /*
620          * Construct an item factory entry for the item, and add it to
621          * the main menu.
622          */
623         entry = g_malloc0(sizeof (GtkItemFactoryEntry));
624         entry->path = menupath;
625         entry->callback = callback;
626         gtk_item_factory_create_item(main_menu_factory, entry, callback_data, 2);
627         set_menu_sensitivity(main_menu_factory, menupath, FALSE); /* no capture file yet */
628         child = g_malloc(sizeof (menu_item_t));
629         child->name = menupath;
630         child->enabled = FALSE; /* no capture file yet, hence no taps yet */
631         child->selected_packet_enabled = selected_packet_enabled;
632         child->selected_tree_row_enabled = selected_tree_row_enabled;
633         child->parent = curnode;
634         child->children = NULL;
635         child->next = curnode->children;
636         curnode->children = child;
637 }
638
639 /*
640  * Enable/disable menu sensitivity.
641  */
642 static void
643 set_menu_sensitivity(GtkItemFactory *ifactory, const gchar *path, gint val)
644 {
645   GSList *menu_list;
646   GtkWidget *menu_item;
647   gchar *dup;
648   gchar *dest;
649
650
651   /* the underscore character regularly confuses things, as it will prevent finding 
652    * the menu_item, so it has to be removed first */
653   dup = g_strdup(path);
654   dest = dup;
655   while(*path) {
656       if (*path != '_') {
657         *dest = *path;
658         dest++;
659       }
660       path++;
661   }
662   *dest = '\0';
663
664   if (ifactory == NULL) {
665     /*
666      * Do it for all pop-up menus.
667      */
668     for (menu_list = popup_menu_list; menu_list != NULL;
669          menu_list = g_slist_next(menu_list))
670       set_menu_sensitivity(menu_list->data, dup, val);
671   } else {
672     /*
673      * Do it for that particular menu.
674      */
675     if ((menu_item = gtk_item_factory_get_widget(ifactory, dup)) != NULL) {
676       if (GTK_IS_MENU(menu_item)) {
677         /*
678          * "dup" refers to a submenu; "gtk_item_factory_get_widget()"
679          * gets the menu, not the item that, when selected, pops up
680          * the submenu.
681          *
682          * We have to change the latter item's sensitivity, so that
683          * it shows up normally if sensitive and grayed-out if
684          * insensitive.
685          */
686         menu_item = gtk_menu_get_attach_widget(GTK_MENU(menu_item));
687       }
688       gtk_widget_set_sensitive(menu_item, val);
689     } else{
690       /* be sure this menu item *is* existing */
691       g_assert_not_reached();
692     }
693   }
694
695   g_free(dup);
696 }
697
698 void
699 set_menu_object_data_meat(GtkItemFactory *ifactory, gchar *path, gchar *key, gpointer data)
700 {
701         GtkWidget *menu = NULL;
702
703         if ((menu = gtk_item_factory_get_widget(ifactory, path)) != NULL)
704                 OBJECT_SET_DATA(menu, key, data);
705 }
706
707 void
708 set_menu_object_data (gchar *path, gchar *key, gpointer data) {
709   GSList *menu_list = popup_menu_list;
710   gchar *shortpath = strrchr(path, '/');
711
712   set_menu_object_data_meat(main_menu_factory, path, key, data);
713   while (menu_list != NULL) {
714         set_menu_object_data_meat(menu_list->data, shortpath, key, data);
715         menu_list = g_slist_next(menu_list);
716   }
717 }
718
719
720 /* Recently used capture files submenu: 
721  * Submenu containing the recently used capture files.
722  * The capture filenames are always kept with the absolute path, to be independant
723  * of the current path. 
724  * They are only stored inside the labels of the submenu (no separate list). */
725
726 #define MENU_RECENT_FILES_PATH "/File/Open Recent"
727 #define MENU_RECENT_FILES_KEY "Recent File Name"
728
729 void
730 update_menu_recent_capture_file1(GtkWidget *widget, gpointer cnt) {
731     gchar *widget_cf_name;
732
733     widget_cf_name = OBJECT_GET_DATA(widget, MENU_RECENT_FILES_KEY);
734
735     /* if this menu item is a file, count it */
736     if (widget_cf_name) {
737         (*(guint *)cnt)++;
738     }
739 }
740
741
742 /* update the menu */
743 void
744 update_menu_recent_capture_file(GtkWidget *submenu_recent_files) {
745     guint cnt = 0;
746
747     gtk_container_foreach(GTK_CONTAINER(submenu_recent_files), 
748                 update_menu_recent_capture_file1, &cnt);
749
750     /* make parent menu item sensitive only, if we have any valid files in the list */
751     set_menu_sensitivity(main_menu_factory, MENU_RECENT_FILES_PATH, cnt);
752 }
753
754
755 /* remove the capture filename from the "Recent Files" menu */
756 void
757 remove_menu_recent_capture_file(GtkWidget *widget, gpointer unused _U_) {
758     GtkWidget *submenu_recent_files;
759     gchar *widget_cf_name;
760
761
762     widget_cf_name = OBJECT_GET_DATA(widget, MENU_RECENT_FILES_KEY);
763     g_free(widget_cf_name);
764
765     /* get the submenu container item */
766     submenu_recent_files = gtk_item_factory_get_widget(main_menu_factory, MENU_RECENT_FILES_PATH);
767
768     /* XXX: is this all we need to do, to free the menu item and its label?
769        The reference count of widget will go to 0, so it'll be freed;
770        will that free the label? */
771     gtk_container_remove(GTK_CONTAINER(submenu_recent_files), widget);
772 }
773
774
775 /* callback, if the user pushed the <clear file list> item */
776 static void
777 clear_menu_recent_capture_file_cmd_cb(GtkWidget *w _U_, gpointer unused _U_) {
778     GtkWidget *submenu_recent_files;
779
780
781     submenu_recent_files = gtk_item_factory_get_widget(main_menu_factory, MENU_RECENT_FILES_PATH);
782
783     gtk_container_foreach(GTK_CONTAINER(submenu_recent_files), 
784                 remove_menu_recent_capture_file, NULL);
785
786     update_menu_recent_capture_file(submenu_recent_files);
787 }
788
789
790 /* callback, if the user pushed a recent file submenu item */
791 void
792 menu_open_recent_file_cmd(GtkWidget *w)
793 {
794         GtkWidget *submenu_recent_files;
795         GtkWidget *menu_item_child;
796         gchar     *cf_name;
797         int       err;
798
799         submenu_recent_files = gtk_item_factory_get_widget(main_menu_factory, MENU_RECENT_FILES_PATH);
800
801         /* get capture filename from the menu item label */
802         menu_item_child = (GTK_BIN(w))->child;
803         gtk_label_get(GTK_LABEL(menu_item_child), &cf_name);
804
805         /* open and read the capture file (this will close an existing file) */
806         if ((err = cf_open(cf_name, FALSE, &cfile)) == 0) {
807                 cf_read(&cfile);
808         } else {
809                 /* the capture file isn't existing any longer, remove menu item */
810                 /* XXX: ask user to remove item, it's maybe only a temporary problem */
811                 remove_menu_recent_capture_file(w, NULL);
812         }
813
814         update_menu_recent_capture_file(submenu_recent_files);
815 }
816
817 static void menu_open_recent_file_answered_cb(gpointer dialog _U_, gint btn, gpointer data _U_)
818 {
819     switch(btn) {
820     case(ESD_BTN_YES):
821         /* save file first */
822         file_save_as_cmd(after_save_open_recent_file, data);
823         break;
824     case(ESD_BTN_NO):
825         menu_open_recent_file_cmd(data);
826         break;
827     case(ESD_BTN_CANCEL):
828         break;
829     default:
830         g_assert_not_reached();
831     }
832 }
833
834 void
835 menu_open_recent_file_cmd_cb(GtkWidget *widget, gpointer data _U_) {
836   gpointer  dialog;
837
838
839   if((cfile.state != FILE_CLOSED) && !cfile.user_saved) {
840     /* user didn't saved his current file, ask him */
841     dialog = simple_dialog(ESD_TYPE_WARN | ESD_TYPE_MODAL, 
842                 ESD_BTN_YES | ESD_BTN_NO | ESD_BTN_CANCEL, 
843                 PRIMARY_TEXT_START "Save capture file before opening a new one?" PRIMARY_TEXT_END "\n\n"
844                 "If you open a new capture file without saving, your current capture data will be discarded.");
845     simple_dialog_set_cb(dialog, menu_open_recent_file_answered_cb, widget);
846   } else {
847     /* unchanged file */
848     menu_open_recent_file_cmd(widget);
849   }
850 }
851
852 /* add the capture filename (with an absolute path) to the "Recent Files" menu */
853 void
854 add_menu_recent_capture_file_absolute(gchar *cf_name) {
855         GtkWidget *submenu_recent_files;
856         GList *menu_item_list;
857         GList *li;
858         gchar *widget_cf_name;
859         GtkWidget *menu_item;
860         guint cnt;
861
862
863         /* get the submenu container item */
864         submenu_recent_files = gtk_item_factory_get_widget(main_menu_factory, MENU_RECENT_FILES_PATH);
865
866         /* convert container to a GList */
867         menu_item_list = gtk_container_children(GTK_CONTAINER(submenu_recent_files));
868
869         /* iterate through list items of menu_item_list, 
870          * removing special items, a maybe duplicate entry and every item above count_max */
871         cnt = 1;
872         for (li = g_list_first(menu_item_list); li; li = li->next, cnt++) {
873                 /* get capture filename */
874                 menu_item = GTK_WIDGET(li->data);
875                 widget_cf_name = OBJECT_GET_DATA(menu_item, MENU_RECENT_FILES_KEY);
876
877                 /* if this element string is one of our special items or
878                  * already in the list or 
879                  * this element is above maximum count (too old), remove it */
880                 if (!widget_cf_name ||
881                     strncmp(widget_cf_name, cf_name, 1000) == 0 ||
882                     cnt >= prefs.gui_recent_files_count_max) {
883                         remove_menu_recent_capture_file(li->data, NULL);
884                         cnt--;
885                 }
886         }
887
888         g_list_free(menu_item_list);
889
890         /* add new item at latest position */
891         menu_item = gtk_menu_item_new_with_label(cf_name);
892         widget_cf_name = g_strdup(cf_name);
893         OBJECT_SET_DATA(menu_item, MENU_RECENT_FILES_KEY, widget_cf_name);
894         gtk_menu_prepend (GTK_MENU(submenu_recent_files), menu_item);
895         SIGNAL_CONNECT_OBJECT(GTK_OBJECT(menu_item), "activate", 
896                 menu_open_recent_file_cmd_cb, (GtkObject *) menu_item);
897         gtk_widget_show (menu_item);
898
899         /* add seperator at last position */
900         menu_item = gtk_menu_item_new();
901         gtk_menu_append (GTK_MENU(submenu_recent_files), menu_item);
902         gtk_widget_show (menu_item);
903
904         /* add new "clear list" item at last position */
905         menu_item = gtk_menu_item_new_with_label("<clear file list>");
906         gtk_menu_append (GTK_MENU(submenu_recent_files), menu_item);
907         SIGNAL_CONNECT_OBJECT(GTK_OBJECT(menu_item), "activate", 
908                 clear_menu_recent_capture_file_cmd_cb, (GtkObject *) menu_item);
909         gtk_widget_show (menu_item);
910
911         update_menu_recent_capture_file(submenu_recent_files);
912 }
913
914
915 /* add the capture filename to the "Recent Files" menu */
916 /* (will change nothing, if this filename is already in the menu) */
917 void
918 add_menu_recent_capture_file(gchar *cf_name) {
919         gchar *curr;
920         gchar *absolute;
921         
922         
923         /* if this filename is an absolute path, we can use it directly */
924         if (g_path_is_absolute(cf_name)) {
925                 add_menu_recent_capture_file_absolute(cf_name);
926                 return;
927         }
928
929         /* this filename is not an absolute path, prepend the current dir */
930         curr = g_get_current_dir();
931         absolute = g_strdup_printf("%s%s%s", curr, G_DIR_SEPARATOR_S, cf_name);
932         add_menu_recent_capture_file_absolute(absolute);
933         g_free(curr);
934         g_free(absolute);
935 }
936
937
938
939 /* write a single menu item widget label to the user's recent file */
940 /* helper, for menu_recent_file_write_all() */
941 void menu_recent_file_write(GtkWidget *widget, gpointer data) {
942         gchar     *cf_name;
943         FILE      *rf = (FILE *) data;
944
945
946         /* get capture filename from the menu item label */
947         cf_name = OBJECT_GET_DATA(widget, MENU_RECENT_FILES_KEY);
948
949         if (cf_name) {
950             fprintf (rf, RECENT_KEY_CAPTURE_FILE ": %s\n", cf_name);
951         }
952 }
953
954
955 /* write all capture filenames of the menu to the user's recent file */
956 void
957 menu_recent_file_write_all(FILE *rf) {
958         GtkWidget *submenu_recent_files;
959
960
961         submenu_recent_files = gtk_item_factory_get_widget(main_menu_factory, MENU_RECENT_FILES_PATH);
962
963         gtk_container_foreach(GTK_CONTAINER(submenu_recent_files), 
964                 menu_recent_file_write, rf);
965 }
966
967
968 static void
969 main_toolbar_show_cb(GtkWidget *w _U_, gpointer d _U_)
970 {
971
972     /* save current setting in recent */
973     recent.main_toolbar_show = GTK_CHECK_MENU_ITEM(w)->active;
974
975     main_widgets_rearrange();
976 }
977
978
979 static void
980 filter_toolbar_show_cb(GtkWidget *w _U_, gpointer d _U_)
981 {
982
983     /* save current setting in recent */
984     recent.filter_toolbar_show = GTK_CHECK_MENU_ITEM(w)->active;
985
986     main_widgets_rearrange();
987 }
988
989
990 static void
991 packet_list_show_cb(GtkWidget *w _U_, gpointer d _U_)
992 {
993
994     /* save current setting in recent */
995     recent.packet_list_show = GTK_CHECK_MENU_ITEM(w)->active;
996
997     main_widgets_rearrange();
998 }
999
1000
1001 static void
1002 tree_view_show_cb(GtkWidget *w _U_, gpointer d _U_)
1003 {
1004
1005     /* save current setting in recent */
1006     recent.tree_view_show = GTK_CHECK_MENU_ITEM(w)->active;
1007
1008     main_widgets_rearrange();
1009 }
1010
1011
1012 static void
1013 byte_view_show_cb(GtkWidget *w _U_, gpointer d _U_)
1014 {
1015
1016     /* save current setting in recent */
1017     recent.byte_view_show = GTK_CHECK_MENU_ITEM(w)->active;
1018
1019     main_widgets_rearrange();
1020 }
1021
1022
1023 static void
1024 statusbar_show_cb(GtkWidget *w _U_, gpointer d _U_)
1025 {
1026
1027     /* save current setting in recent */
1028     recent.statusbar_show = GTK_CHECK_MENU_ITEM(w)->active;
1029
1030     main_widgets_rearrange();
1031 }
1032
1033
1034 static void 
1035 timestamp_absolute_cb(GtkWidget *w _U_, gpointer d _U_)
1036 {
1037     if (recent.gui_time_format != TS_ABSOLUTE) {
1038         timestamp_type          = TS_ABSOLUTE;
1039         recent.gui_time_format  = timestamp_type;
1040         change_time_formats(&cfile);
1041     }
1042 }
1043
1044 static void 
1045 timestamp_absolute_date_cb(GtkWidget *w _U_, gpointer d _U_)
1046 {
1047     if (recent.gui_time_format != TS_ABSOLUTE_WITH_DATE) {
1048         timestamp_type          = TS_ABSOLUTE_WITH_DATE;
1049         recent.gui_time_format  = timestamp_type;
1050         change_time_formats(&cfile);
1051     }
1052 }
1053
1054 static void 
1055 timestamp_relative_cb(GtkWidget *w _U_, gpointer d _U_)
1056 {
1057     if (recent.gui_time_format != TS_RELATIVE) {
1058         timestamp_type          = TS_RELATIVE;
1059         recent.gui_time_format  = timestamp_type;
1060         change_time_formats(&cfile);
1061     }
1062 }
1063
1064 static void 
1065 timestamp_delta_cb(GtkWidget *w _U_, gpointer d _U_)
1066 {
1067     if (recent.gui_time_format != TS_DELTA) {
1068         timestamp_type          = TS_DELTA;
1069         recent.gui_time_format  = timestamp_type;
1070         change_time_formats(&cfile);
1071     }
1072 }
1073
1074 static void 
1075 name_resolution_mac_cb(GtkWidget *w _U_, gpointer d _U_)
1076 {
1077     if (GTK_CHECK_MENU_ITEM(w)->active) {
1078         g_resolv_flags |= RESOLV_MAC;
1079     } else {
1080         g_resolv_flags &= ~RESOLV_MAC;
1081     }
1082 }
1083
1084 static void 
1085 name_resolution_network_cb(GtkWidget *w _U_, gpointer d _U_)
1086 {
1087     if (GTK_CHECK_MENU_ITEM(w)->active) {
1088         g_resolv_flags |= RESOLV_NETWORK;
1089     } else {
1090         g_resolv_flags &= ~RESOLV_NETWORK;
1091     }
1092 }
1093
1094 static void 
1095 name_resolution_transport_cb(GtkWidget *w _U_, gpointer d _U_)
1096 {
1097     if (GTK_CHECK_MENU_ITEM(w)->active) {
1098         g_resolv_flags |= RESOLV_TRANSPORT;
1099     } else {
1100         g_resolv_flags &= ~RESOLV_TRANSPORT;
1101     }
1102 }
1103
1104 static void 
1105 auto_scroll_live_cb(GtkWidget *w _U_, gpointer d _U_)
1106 {
1107     auto_scroll_live = GTK_CHECK_MENU_ITEM(w)->active;
1108 }
1109
1110
1111 /* the recent file read has finished, update the menu corresponding */
1112 void
1113 menu_recent_read_finished(void) {
1114     GtkWidget *menu = NULL;
1115
1116     menu = gtk_item_factory_get_widget(main_menu_factory, "/View/Show/Main Toolbar");
1117     gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menu), recent.main_toolbar_show);
1118
1119     menu = gtk_item_factory_get_widget(main_menu_factory, "/View/Show/Filter Toolbar");
1120     gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menu), recent.filter_toolbar_show);
1121
1122     menu = gtk_item_factory_get_widget(main_menu_factory, "/View/Show/Packet List");
1123     gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menu), recent.packet_list_show);
1124
1125     menu = gtk_item_factory_get_widget(main_menu_factory, "/View/Show/Packet Details");
1126     gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menu), recent.tree_view_show);
1127
1128     menu = gtk_item_factory_get_widget(main_menu_factory, "/View/Show/Packet Bytes");
1129     gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menu), recent.byte_view_show);
1130
1131     menu = gtk_item_factory_get_widget(main_menu_factory, "/View/Show/Status Bar");
1132     gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menu), recent.statusbar_show);
1133
1134     menu = gtk_item_factory_get_widget(main_menu_factory, "/View/Name Resolution/Enable for MAC Layer");
1135     gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menu), g_resolv_flags & RESOLV_MAC);
1136
1137     menu = gtk_item_factory_get_widget(main_menu_factory, "/View/Name Resolution/Enable for Network Layer");
1138     gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menu), g_resolv_flags & RESOLV_NETWORK);
1139
1140     menu = gtk_item_factory_get_widget(main_menu_factory, "/View/Name Resolution/Enable for Transport Layer");
1141     gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menu), g_resolv_flags & RESOLV_TRANSPORT);
1142
1143     menu = gtk_item_factory_get_widget(main_menu_factory, "/View/Auto Scroll in Live Capture");
1144     gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menu), auto_scroll_live);
1145
1146     main_widgets_rearrange();
1147
1148     /* don't change the time format, if we had a command line value */
1149     if (timestamp_type != TS_NOT_SET) {
1150         recent.gui_time_format = timestamp_type;
1151     }
1152
1153     switch(recent.gui_time_format) {
1154     case(TS_ABSOLUTE):
1155         menu = gtk_item_factory_get_widget(main_menu_factory, 
1156             "/View/Time Display Format/Time of Day");
1157         /* set_active will not trigger the callback when activating an active item! */
1158         recent.gui_time_format = -1;
1159         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menu), FALSE);
1160         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menu), TRUE);
1161         break;
1162     case(TS_ABSOLUTE_WITH_DATE):
1163         menu = gtk_item_factory_get_widget(main_menu_factory, 
1164             "/View/Time Display Format/Date and Time of Day");
1165         recent.gui_time_format = -1;
1166         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menu), TRUE);
1167         break;
1168     case(TS_RELATIVE):
1169         menu = gtk_item_factory_get_widget(main_menu_factory, 
1170             "/View/Time Display Format/Seconds Since Beginning of Capture");
1171         recent.gui_time_format = -1;
1172         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menu), TRUE);
1173         break;
1174     case(TS_DELTA):
1175         menu = gtk_item_factory_get_widget(main_menu_factory, 
1176             "/View/Time Display Format/Seconds Since Previous Packet");
1177         recent.gui_time_format = -1;
1178         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menu), TRUE);
1179         break;
1180     default:
1181         g_assert_not_reached();
1182     }
1183 }
1184
1185
1186 gint
1187 popup_menu_handler(GtkWidget *widget, GdkEvent *event, gpointer data)
1188 {
1189     GtkWidget *menu = (GtkWidget *)data;
1190     GdkEventButton *event_button = NULL;
1191     gint row, column;
1192
1193     if(widget == NULL || event == NULL || data == NULL) {
1194         return FALSE;
1195     }
1196
1197     /*
1198      * If we ever want to make the menu differ based on what row
1199      * and/or column we're above, we'd use "eth_clist_get_selection_info()"
1200      * to find the row and column number for the coordinates; a CTree is,
1201      * I guess, like a CList with one column(?) and the expander widget
1202      * as a pixmap.
1203      */
1204     /* Check if we are on packet_list object */
1205     if (widget == OBJECT_GET_DATA(popup_menu_object, E_MPACKET_LIST_KEY)) {
1206         if (packet_list_get_event_row_column(widget, (GdkEventButton *)event,
1207                                              &row, &column)) {
1208             OBJECT_SET_DATA(popup_menu_object, E_MPACKET_LIST_ROW_KEY,
1209                             GINT_TO_POINTER(row));
1210             OBJECT_SET_DATA(popup_menu_object, E_MPACKET_LIST_COL_KEY,
1211                             GINT_TO_POINTER(column));
1212             packet_list_set_selected_row(row);
1213         }
1214     }
1215
1216     /* Check if we are on tree_view object */
1217     if (widget == tree_view) {
1218         tree_view_select(widget, (GdkEventButton *) event);
1219     }
1220
1221     /* Check if we are on byte_view object */
1222     if(widget == get_notebook_bv_ptr(byte_nb_ptr)) {
1223         byte_view_select(widget, (GdkEventButton *) event);
1224     }
1225
1226     if(event->type == GDK_BUTTON_PRESS) {
1227         event_button = (GdkEventButton *) event;
1228
1229         /* To qoute the "Gdk Event Structures" doc:
1230          * "Normally button 1 is the left mouse button, 2 is the middle button, and 3 is the right button" */
1231         if(event_button->button == 3) {
1232             gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL,
1233                            event_button->button,
1234                            event_button->time);
1235             SIGNAL_EMIT_STOP_BY_NAME(widget, "button_press_event");
1236             return TRUE;
1237         }
1238     }
1239 #if GTK_MAJOR_VERSION >= 2
1240     /* GDK_2BUTTON_PRESS is a doubleclick -> expand/collapse tree row */
1241     /* GTK version 1 seems to be doing this automatically */
1242     if (widget == tree_view && event->type == GDK_2BUTTON_PRESS) {
1243         GtkTreePath      *path;
1244
1245         if (gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(widget),
1246                                           (gint) (((GdkEventButton *)event)->x),
1247                                           (gint) (((GdkEventButton *)event)->y),
1248                                           &path, NULL, NULL, NULL))
1249         {
1250             if (gtk_tree_view_row_expanded(GTK_TREE_VIEW(widget), path))
1251                 gtk_tree_view_collapse_row(GTK_TREE_VIEW(widget), path);
1252             else
1253                 gtk_tree_view_expand_row(GTK_TREE_VIEW(widget), path,
1254                                          FALSE);
1255             gtk_tree_path_free(path);
1256         }
1257     }
1258 #endif
1259     return FALSE;
1260 }
1261
1262 /* Enable or disable menu items based on whether you have a capture file
1263    you've finished reading. */
1264 void
1265 set_menus_for_capture_file(gboolean have_capture_file)
1266 {
1267   set_menu_sensitivity(main_menu_factory, "/File/Open...", have_capture_file);
1268   set_menu_sensitivity(main_menu_factory, "/File/Open Recent", have_capture_file);
1269   set_menu_sensitivity(main_menu_factory, "/File/Close", have_capture_file);
1270   set_menu_sensitivity(main_menu_factory, "/File/Save As...",
1271       have_capture_file);
1272   set_menu_sensitivity(main_menu_factory, "/File/Export", have_capture_file);
1273   set_menu_sensitivity(main_menu_factory, "/View/Reload", have_capture_file);
1274   set_toolbar_for_capture_file(have_capture_file);
1275 }
1276
1277 /* Enable or disable menu items based on whether you have an unsaved
1278    capture file you've finished reading. */
1279 void
1280 set_menus_for_unsaved_capture_file(gboolean have_unsaved_capture_file)
1281 {
1282   set_menu_sensitivity(main_menu_factory, "/File/Save",
1283       have_unsaved_capture_file);
1284   set_toolbar_for_unsaved_capture_file(have_unsaved_capture_file);
1285 }
1286
1287 /* Enable or disable menu items based on whether there's a capture in
1288    progress. */
1289 void
1290 set_menus_for_capture_in_progress(gboolean capture_in_progress)
1291 {
1292   set_menu_sensitivity(main_menu_factory, "/File/Open...",
1293       !capture_in_progress);
1294   set_menu_sensitivity(main_menu_factory, "/File/Open Recent", 
1295       !capture_in_progress);
1296 #ifdef HAVE_LIBPCAP
1297   set_menu_sensitivity(main_menu_factory, "/Capture/Start...",
1298       !capture_in_progress);
1299   set_menu_sensitivity(main_menu_factory, "/Capture/Stop",
1300       capture_in_progress);
1301 #endif /* HAVE_LIBPCAP */
1302   set_toolbar_for_capture_in_progress(capture_in_progress);
1303 }
1304
1305 /* Enable or disable menu items based on whether you have some captured
1306    packets. */
1307 static gboolean
1308 walk_menu_tree_for_captured_packets(menu_item_t *node,
1309     gboolean have_captured_packets)
1310 {
1311         gboolean is_enabled;
1312         menu_item_t *child;
1313
1314         /*
1315          * Is this a leaf node or an interior node?
1316          */
1317         if (node->children == NULL) {
1318                 /*
1319                  * It's a leaf node.
1320                  *
1321                  * If it has no "selected_packet_enabled()" or
1322                  * "selected_tree_row_enabled()" routines, we enable
1323                  * it.  This allows tap windows to be popped up even
1324                  * if you have no capture file; this is done to let
1325                  * the user pop up multiple tap windows before reading
1326                  * in a capture file, so that they can be processed in
1327                  * parallel while the capture file is being read rather
1328                  * than one at at time as you pop up the windows, and to
1329                  * let the user pop up tap windows before starting an
1330                  * "Update list of packets in real time" capture, so that
1331                  * the statistics can be displayed while the capture is
1332                  * in progress.
1333                  *
1334                  * If it has either of those routines, we disable it for
1335                  * now - as long as, when a capture is first available,
1336                  * we don't get called after a packet or tree row is
1337                  * selected, that's OK.
1338                  * XXX - that should be done better.
1339                  */
1340                 if (node->selected_packet_enabled == NULL &&
1341                     node->selected_tree_row_enabled == NULL)
1342                         node->enabled = TRUE;
1343                 else
1344                         node->enabled = FALSE;
1345         } else {
1346                 /*
1347                  * It's an interior node; call
1348                  * "walk_menu_tree_for_captured_packets()" on all its
1349                  * children and, if any of them are enabled, enable
1350                  * this node, otherwise disable it.
1351                  *
1352                  * XXX - should we just leave all interior nodes enabled?
1353                  * Which is a better UI choice?
1354                  */
1355                 is_enabled = FALSE;
1356                 for (child = node->children; child != NULL; child =
1357                     child->next) {
1358                         if (walk_menu_tree_for_captured_packets(child,
1359                             have_captured_packets))
1360                                 is_enabled = TRUE;
1361                 }
1362                 node->enabled = is_enabled;
1363         }
1364
1365         /*
1366          * The root node doesn't correspond to a menu tree item; it
1367          * has a null name pointer.
1368          */
1369         if (node->name != NULL) {
1370                 set_menu_sensitivity(main_menu_factory, node->name,
1371                     node->enabled);
1372         }
1373         return node->enabled;
1374 }
1375
1376 void
1377 set_menus_for_captured_packets(gboolean have_captured_packets)
1378 {
1379   set_menu_sensitivity(main_menu_factory, "/File/Print...",
1380       have_captured_packets);
1381   set_menu_sensitivity(packet_list_menu_factory, "/Print...",
1382       have_captured_packets);
1383   set_menu_sensitivity(main_menu_factory, "/Edit/Find Packet...",
1384       have_captured_packets);
1385   set_menu_sensitivity(main_menu_factory, "/Edit/Find Next",
1386       have_captured_packets);
1387   set_menu_sensitivity(main_menu_factory, "/Edit/Find Previous",
1388       have_captured_packets);
1389   set_menu_sensitivity(main_menu_factory, "/Edit/Go To First Packet",
1390       have_captured_packets);
1391   set_menu_sensitivity(main_menu_factory, "/Edit/Go To Last Packet",
1392       have_captured_packets);
1393   set_menu_sensitivity(main_menu_factory, "/Edit/Go To Packet...",
1394       have_captured_packets);
1395   set_menu_sensitivity(main_menu_factory, "/View/Zoom In",
1396       have_captured_packets);
1397   set_menu_sensitivity(main_menu_factory, "/View/Zoom Out",
1398       have_captured_packets);
1399   set_menu_sensitivity(main_menu_factory, "/View/Normal Size",
1400       have_captured_packets);
1401   set_menu_sensitivity(main_menu_factory, "/View/Coloring Rules...",
1402       have_captured_packets);
1403   set_menu_sensitivity(packet_list_menu_factory, "/Coloring Rules...",
1404       have_captured_packets);
1405   set_menu_sensitivity(main_menu_factory, "/Analyze/Summary",
1406       have_captured_packets);
1407   set_menu_sensitivity(main_menu_factory, "/Analyze/Protocol Hierarchy Statistics", 
1408       have_captured_packets);
1409       
1410   walk_menu_tree_for_captured_packets(&tap_menu_tree_root,
1411       have_captured_packets);
1412   set_toolbar_for_captured_packets(have_captured_packets);
1413 }
1414
1415 /* Enable or disable menu items based on whether a packet is selected and,
1416    if so, on the properties of the packet. */
1417 static gboolean
1418 walk_menu_tree_for_selected_packet(menu_item_t *node, frame_data *fd,
1419     epan_dissect_t *edt)
1420 {
1421         gboolean is_enabled;
1422         menu_item_t *child;
1423
1424         /*
1425          * Is this a leaf node or an interior node?
1426          */
1427         if (node->children == NULL) {
1428                 /*
1429                  * It's a leaf node.
1430                  *
1431                  * If it has no "selected_packet_enabled()" routine,
1432                  * leave its enabled/disabled status alone - it
1433                  * doesn't depend on whether we have a packet selected
1434                  * or not or on the selected packet.
1435                  *
1436                  * If it has a "selected_packet_enabled()" routine,
1437                  * call it and set the item's enabled/disabled status
1438                  * based on its return value.
1439                  */
1440                 if (node->selected_packet_enabled != NULL)
1441                         node->enabled = node->selected_packet_enabled(fd, edt);
1442         } else {
1443                 /*
1444                  * It's an interior node; call
1445                  * "walk_menu_tree_for_selected_packet()" on all its
1446                  * children and, if any of them are enabled, enable
1447                  * this node, otherwise disable it.
1448                  *
1449                  * XXX - should we just leave all interior nodes enabled?
1450                  * Which is a better UI choice?
1451                  */
1452                 is_enabled = FALSE;
1453                 for (child = node->children; child != NULL; child =
1454                     child->next) {
1455                         if (walk_menu_tree_for_selected_packet(child, fd, edt))
1456                                 is_enabled = TRUE;
1457                 }
1458                 node->enabled = is_enabled;
1459         }
1460
1461         /*
1462          * The root node doesn't correspond to a menu tree item; it
1463          * has a null name pointer.
1464          */
1465         if (node->name != NULL) {
1466                 set_menu_sensitivity(main_menu_factory, node->name,
1467                     node->enabled);
1468         }
1469         return node->enabled;
1470 }
1471
1472 void
1473 set_menus_for_selected_packet(capture_file *cf)
1474 {
1475   set_menu_sensitivity(main_menu_factory, "/Edit/Mark Packet",
1476       cf->current_frame != NULL);
1477   set_menu_sensitivity(packet_list_menu_factory, "/Mark Packet",
1478       cf->current_frame != NULL);
1479   set_menu_sensitivity(main_menu_factory, "/Edit/Time Reference",
1480       cf->current_frame != NULL);
1481   set_menu_sensitivity(packet_list_menu_factory, "/Time Reference",
1482       cf->current_frame != NULL);
1483   set_menu_sensitivity(main_menu_factory, "/Edit/Mark All Packets",
1484       cf->current_frame != NULL);
1485   set_menu_sensitivity(main_menu_factory, "/Edit/Unmark All Packets",
1486       cf->current_frame != NULL);
1487   set_menu_sensitivity(main_menu_factory, "/View/Collapse All",
1488       cf->current_frame != NULL);
1489   set_menu_sensitivity(tree_view_menu_factory, "/Collapse All",
1490       cf->current_frame != NULL);
1491   set_menu_sensitivity(main_menu_factory, "/View/Expand All",
1492       cf->current_frame != NULL);
1493   set_menu_sensitivity(tree_view_menu_factory, "/Expand All",
1494       cf->current_frame != NULL);
1495   set_menu_sensitivity(main_menu_factory, "/View/Show Packet In New Window",
1496       cf->current_frame != NULL);
1497   set_menu_sensitivity(packet_list_menu_factory, "/Show Packet In New Window",
1498       cf->current_frame != NULL);
1499   set_menu_sensitivity(main_menu_factory, "/Analyze/Follow TCP Stream",
1500       cf->current_frame != NULL ? (cf->edt->pi.ipproto == IP_PROTO_TCP) : FALSE);
1501   set_menu_sensitivity(NULL, "/Follow TCP Stream",
1502       cf->current_frame != NULL ? (cf->edt->pi.ipproto == IP_PROTO_TCP) : FALSE);
1503   set_menu_sensitivity(main_menu_factory, "/Analyze/Decode As...",
1504       cf->current_frame != NULL && decode_as_ok());
1505   set_menu_sensitivity(NULL, "/Decode As...",
1506       cf->current_frame != NULL && decode_as_ok());
1507   set_menu_sensitivity(tree_view_menu_factory, "/Resolve Name",
1508       cf->current_frame != NULL && g_resolv_flags == 0);
1509   set_menu_sensitivity(main_menu_factory, "/Analyze/TCP Stream Analysis",
1510       cf->current_frame != NULL ? (cf->edt->pi.ipproto == IP_PROTO_TCP) : FALSE);
1511   set_menu_sensitivity(packet_list_menu_factory, "/Match",
1512       cf->current_frame != NULL);
1513   set_menu_sensitivity(packet_list_menu_factory, "/Prepare",
1514       cf->current_frame != NULL);
1515
1516   walk_menu_tree_for_selected_packet(&tap_menu_tree_root, cf->current_frame,
1517       cf->edt);
1518 }
1519
1520 /* Enable or disable menu items based on whether a tree row is selected
1521    and, if so, on the properties of the tree row. */
1522 static gboolean
1523 walk_menu_tree_for_selected_tree_row(menu_item_t *node, field_info *fi)
1524 {
1525         gboolean is_enabled;
1526         menu_item_t *child;
1527
1528         /*
1529          * Is this a leaf node or an interior node?
1530          */
1531         if (node->children == NULL) {
1532                 /*
1533                  * It's a leaf node.
1534                  *
1535                  * If it has no "selected_tree_row_enabled()" routine,
1536                  * leave its enabled/disabled status alone - it
1537                  * doesn't depend on whether we have a tree row selected
1538                  * or not or on the selected tree row.
1539                  *
1540                  * If it has a "selected_tree_row_enabled()" routine,
1541                  * call it and set the item's enabled/disabled status
1542                  * based on its return value.
1543                  */
1544                 if (node->selected_tree_row_enabled != NULL)
1545                         node->enabled = node->selected_tree_row_enabled(fi);
1546         } else {
1547                 /*
1548                  * It's an interior node; call
1549                  * "walk_menu_tree_for_selected_tree_row()" on all its
1550                  * children and, if any of them are enabled, enable
1551                  * this node, otherwise disable it.
1552                  *
1553                  * XXX - should we just leave all interior nodes enabled?
1554                  * Which is a better UI choice?
1555                  */
1556                 is_enabled = FALSE;
1557                 for (child = node->children; child != NULL; child =
1558                     child->next) {
1559                         if (walk_menu_tree_for_selected_tree_row(child, fi))
1560                                 is_enabled = TRUE;
1561                 }
1562                 node->enabled = is_enabled;
1563         }
1564
1565         /*
1566          * The root node doesn't correspond to a menu tree item; it
1567          * has a null name pointer.
1568          */
1569         if (node->name != NULL) {
1570                 set_menu_sensitivity(main_menu_factory, node->name,
1571                     node->enabled);
1572         }
1573         return node->enabled;
1574 }
1575
1576 void
1577 set_menus_for_selected_tree_row(capture_file *cf)
1578 {
1579   gboolean properties;
1580
1581
1582   set_menu_sensitivity(main_menu_factory, "/File/Export/Selected Packet Bytes...", 
1583       cf->finfo_selected != NULL);  
1584   set_menu_sensitivity(tree_view_menu_factory, "/Export Selected Packet Bytes...", 
1585       cf->finfo_selected != NULL);
1586   set_menu_sensitivity(hexdump_menu_factory, "/Export Selected Packet Bytes...", 
1587       cf->finfo_selected != NULL);
1588   
1589   if (cf->finfo_selected != NULL) {
1590         header_field_info *hfinfo = cf->finfo_selected->hfinfo;
1591         if (hfinfo->parent == -1) {
1592           properties = prefs_is_registered_protocol(hfinfo->abbrev);
1593         } else {
1594           properties = prefs_is_registered_protocol(proto_registrar_get_abbrev(hfinfo->parent));
1595         }
1596         set_menu_sensitivity(main_menu_factory,
1597           "/Edit/Go To Corresponding Packet", hfinfo->type == FT_FRAMENUM);
1598         set_menu_sensitivity(tree_view_menu_factory,
1599           "/Go To Corresponding Packet", hfinfo->type == FT_FRAMENUM);
1600         set_menu_sensitivity(main_menu_factory, "/Analyze/Match",
1601           proto_can_match_selected(cf->finfo_selected, cf->edt));
1602         set_menu_sensitivity(tree_view_menu_factory, "/Match",
1603           proto_can_match_selected(cf->finfo_selected, cf->edt));
1604         set_menu_sensitivity(main_menu_factory, "/Analyze/Prepare",
1605           proto_can_match_selected(cf->finfo_selected, cf->edt));
1606         set_menu_sensitivity(tree_view_menu_factory, "/Prepare",
1607           proto_can_match_selected(cf->finfo_selected, cf->edt));
1608         set_menu_sensitivity(tree_view_menu_factory, "/Protocol Properties...",
1609           properties);
1610   } else {
1611         set_menu_sensitivity(main_menu_factory,
1612             "/Edit/Go To Corresponding Packet", FALSE);
1613         set_menu_sensitivity(tree_view_menu_factory,
1614             "/Go To Corresponding Packet", FALSE);
1615         set_menu_sensitivity(main_menu_factory, "/Analyze/Match", FALSE);
1616         set_menu_sensitivity(tree_view_menu_factory, "/Match", FALSE);
1617         set_menu_sensitivity(main_menu_factory, "/Analyze/Prepare", FALSE);
1618         set_menu_sensitivity(tree_view_menu_factory, "/Prepare", FALSE);
1619         set_menu_sensitivity(tree_view_menu_factory, "/Protocol Properties...",
1620           FALSE);
1621   }
1622
1623   walk_menu_tree_for_selected_tree_row(&tap_menu_tree_root, cf->finfo_selected);
1624 }