781cb665b7b1912905a65e4bc0246a9508cf4bfa
[obnox/wireshark/wip.git] / gtk / menu.c
1 /* menu.c
2  * Menu routines
3  *
4  * $Id: menu.c,v 1.166 2004/02/20 18:43:59 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
33 #include "main.h"
34 #include "menu.h"
35 #include "tap_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_network_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         cf_close(&cfile);
826         menu_open_recent_file_cmd(data);
827         break;
828     case(ESD_BTN_CANCEL):
829         break;
830     default:
831         g_assert_not_reached();
832     }
833 }
834
835 void
836 menu_open_recent_file_cmd_cb(GtkWidget *widget, gpointer data _U_) {
837   gpointer  dialog;
838
839
840   if((cfile.state != FILE_CLOSED) && !cfile.user_saved) {
841     /* user didn't saved his current file, ask him */
842     dialog = simple_dialog(ESD_TYPE_CONFIRMATION, ESD_BTNS_YES_NO_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         gchar *normalized_cf_name;
860         GtkWidget *menu_item;
861         guint cnt;
862
863
864
865         normalized_cf_name = g_strdup(cf_name);
866 #ifdef WIN32
867     /* replace all slashes by backslashes */
868     g_strdelimit(normalized_cf_name, "/", '\\');
869 #endif
870
871         /* get the submenu container item */
872         submenu_recent_files = gtk_item_factory_get_widget(main_menu_factory, MENU_RECENT_FILES_PATH);
873
874         /* convert container to a GList */
875         menu_item_list = gtk_container_children(GTK_CONTAINER(submenu_recent_files));
876
877         /* iterate through list items of menu_item_list, 
878          * removing special items, a maybe duplicate entry and every item above count_max */
879         cnt = 1;
880         for (li = g_list_first(menu_item_list); li; li = li->next, cnt++) {
881                 /* get capture filename */
882                 menu_item = GTK_WIDGET(li->data);
883                 widget_cf_name = OBJECT_GET_DATA(menu_item, MENU_RECENT_FILES_KEY);
884
885                 /* if this element string is one of our special items (seperator, ...) or
886                  * already in the list or 
887                  * this element is above maximum count (too old), remove it */
888                 if (!widget_cf_name ||
889 #ifdef WIN32
890             /* do a case insensitive compare on win32 */
891 #if GLIB_MAJOR_VERSION < 2
892             g_strncasecmp(widget_cf_name, normalized_cf_name, 1000) == 0 ||
893 #else
894             g_ascii_strncasecmp(widget_cf_name, normalized_cf_name, 1000) == 0 ||
895 #endif
896 #else   /* WIN32 */
897             /* do a case sensitive compare on unix */
898                     strncmp(widget_cf_name, normalized_cf_name, 1000) == 0 ||
899 #endif
900                     cnt >= prefs.gui_recent_files_count_max) {
901                         remove_menu_recent_capture_file(li->data, NULL);
902                         cnt--;
903                 }
904         }
905
906         g_list_free(menu_item_list);
907
908         /* add new item at latest position */
909         menu_item = gtk_menu_item_new_with_label(normalized_cf_name);
910         OBJECT_SET_DATA(menu_item, MENU_RECENT_FILES_KEY, normalized_cf_name);
911         gtk_menu_prepend (GTK_MENU(submenu_recent_files), menu_item);
912         SIGNAL_CONNECT_OBJECT(GTK_OBJECT(menu_item), "activate", 
913                 menu_open_recent_file_cmd_cb, (GtkObject *) menu_item);
914         gtk_widget_show (menu_item);
915
916         /* add seperator at last position */
917         menu_item = gtk_menu_item_new();
918         gtk_menu_append (GTK_MENU(submenu_recent_files), menu_item);
919         gtk_widget_show (menu_item);
920
921         /* add new "clear list" item at last position */
922         menu_item = gtk_menu_item_new_with_label("<clear file list>");
923         gtk_menu_append (GTK_MENU(submenu_recent_files), menu_item);
924         SIGNAL_CONNECT_OBJECT(GTK_OBJECT(menu_item), "activate", 
925                 clear_menu_recent_capture_file_cmd_cb, (GtkObject *) menu_item);
926         gtk_widget_show (menu_item);
927
928         update_menu_recent_capture_file(submenu_recent_files);
929 }
930
931
932 /* add the capture filename to the "Recent Files" menu */
933 /* (will change nothing, if this filename is already in the menu) */
934 void
935 add_menu_recent_capture_file(gchar *cf_name) {
936         gchar *curr;
937         gchar *absolute;
938         
939         
940         /* if this filename is an absolute path, we can use it directly */
941         if (g_path_is_absolute(cf_name)) {
942                 add_menu_recent_capture_file_absolute(cf_name);
943                 return;
944         }
945
946         /* this filename is not an absolute path, prepend the current dir */
947         curr = g_get_current_dir();
948         absolute = g_strdup_printf("%s%s%s", curr, G_DIR_SEPARATOR_S, cf_name);
949         add_menu_recent_capture_file_absolute(absolute);
950         g_free(curr);
951         g_free(absolute);
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     GList       *children;
960         GList       *child;
961         gchar       *cf_name;
962
963
964         submenu_recent_files = gtk_item_factory_get_widget(main_menu_factory, MENU_RECENT_FILES_PATH);
965
966     /* we have to iterate backwards through the children's list,
967      * so we get the latest item last in the file.
968      * (don't use gtk_container_foreach() here, it will return the wrong iteration order) */
969     children = gtk_container_children(GTK_CONTAINER(submenu_recent_files));
970     child = g_list_last(children);
971     while(child != NULL) {
972             /* get capture filename from the menu item label */
973             cf_name = OBJECT_GET_DATA(child->data, MENU_RECENT_FILES_KEY);
974             if (cf_name) {
975                 fprintf (rf, RECENT_KEY_CAPTURE_FILE ": %s\n", cf_name);
976             }
977
978         child = g_list_previous(child);
979     }
980
981         g_list_free(children);
982 }
983
984
985 static void
986 main_toolbar_show_cb(GtkWidget *w _U_, gpointer d _U_)
987 {
988
989     /* save current setting in recent */
990     recent.main_toolbar_show = GTK_CHECK_MENU_ITEM(w)->active;
991
992     main_widgets_rearrange();
993 }
994
995
996 static void
997 filter_toolbar_show_cb(GtkWidget *w _U_, gpointer d _U_)
998 {
999
1000     /* save current setting in recent */
1001     recent.filter_toolbar_show = GTK_CHECK_MENU_ITEM(w)->active;
1002
1003     main_widgets_rearrange();
1004 }
1005
1006
1007 static void
1008 packet_list_show_cb(GtkWidget *w _U_, gpointer d _U_)
1009 {
1010
1011     /* save current setting in recent */
1012     recent.packet_list_show = GTK_CHECK_MENU_ITEM(w)->active;
1013
1014     main_widgets_rearrange();
1015 }
1016
1017
1018 static void
1019 tree_view_show_cb(GtkWidget *w _U_, gpointer d _U_)
1020 {
1021
1022     /* save current setting in recent */
1023     recent.tree_view_show = GTK_CHECK_MENU_ITEM(w)->active;
1024
1025     main_widgets_rearrange();
1026 }
1027
1028
1029 static void
1030 byte_view_show_cb(GtkWidget *w _U_, gpointer d _U_)
1031 {
1032
1033     /* save current setting in recent */
1034     recent.byte_view_show = GTK_CHECK_MENU_ITEM(w)->active;
1035
1036     main_widgets_rearrange();
1037 }
1038
1039
1040 static void
1041 statusbar_show_cb(GtkWidget *w _U_, gpointer d _U_)
1042 {
1043
1044     /* save current setting in recent */
1045     recent.statusbar_show = GTK_CHECK_MENU_ITEM(w)->active;
1046
1047     main_widgets_rearrange();
1048 }
1049
1050
1051 static void 
1052 timestamp_absolute_cb(GtkWidget *w _U_, gpointer d _U_)
1053 {
1054     if (recent.gui_time_format != TS_ABSOLUTE) {
1055         timestamp_type          = TS_ABSOLUTE;
1056         recent.gui_time_format  = timestamp_type;
1057         change_time_formats(&cfile);
1058     }
1059 }
1060
1061 static void 
1062 timestamp_absolute_date_cb(GtkWidget *w _U_, gpointer d _U_)
1063 {
1064     if (recent.gui_time_format != TS_ABSOLUTE_WITH_DATE) {
1065         timestamp_type          = TS_ABSOLUTE_WITH_DATE;
1066         recent.gui_time_format  = timestamp_type;
1067         change_time_formats(&cfile);
1068     }
1069 }
1070
1071 static void 
1072 timestamp_relative_cb(GtkWidget *w _U_, gpointer d _U_)
1073 {
1074     if (recent.gui_time_format != TS_RELATIVE) {
1075         timestamp_type          = TS_RELATIVE;
1076         recent.gui_time_format  = timestamp_type;
1077         change_time_formats(&cfile);
1078     }
1079 }
1080
1081 static void 
1082 timestamp_delta_cb(GtkWidget *w _U_, gpointer d _U_)
1083 {
1084     if (recent.gui_time_format != TS_DELTA) {
1085         timestamp_type          = TS_DELTA;
1086         recent.gui_time_format  = timestamp_type;
1087         change_time_formats(&cfile);
1088     }
1089 }
1090
1091 static void 
1092 name_resolution_mac_cb(GtkWidget *w _U_, gpointer d _U_)
1093 {
1094     if (GTK_CHECK_MENU_ITEM(w)->active) {
1095         g_resolv_flags |= RESOLV_MAC;
1096     } else {
1097         g_resolv_flags &= ~RESOLV_MAC;
1098     }
1099 }
1100
1101 static void 
1102 name_resolution_network_cb(GtkWidget *w _U_, gpointer d _U_)
1103 {
1104     if (GTK_CHECK_MENU_ITEM(w)->active) {
1105         g_resolv_flags |= RESOLV_NETWORK;
1106     } else {
1107         g_resolv_flags &= ~RESOLV_NETWORK;
1108     }
1109 }
1110
1111 static void 
1112 name_resolution_transport_cb(GtkWidget *w _U_, gpointer d _U_)
1113 {
1114     if (GTK_CHECK_MENU_ITEM(w)->active) {
1115         g_resolv_flags |= RESOLV_TRANSPORT;
1116     } else {
1117         g_resolv_flags &= ~RESOLV_TRANSPORT;
1118     }
1119 }
1120
1121 static void 
1122 auto_scroll_live_cb(GtkWidget *w _U_, gpointer d _U_)
1123 {
1124     auto_scroll_live = GTK_CHECK_MENU_ITEM(w)->active;
1125 }
1126
1127
1128 /* the recent file read has finished, update the menu corresponding */
1129 void
1130 menu_recent_read_finished(void) {
1131     GtkWidget *menu = NULL;
1132
1133     menu = gtk_item_factory_get_widget(main_menu_factory, "/View/Show/Main Toolbar");
1134     gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menu), recent.main_toolbar_show);
1135
1136     menu = gtk_item_factory_get_widget(main_menu_factory, "/View/Show/Filter Toolbar");
1137     gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menu), recent.filter_toolbar_show);
1138
1139     menu = gtk_item_factory_get_widget(main_menu_factory, "/View/Show/Packet List");
1140     gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menu), recent.packet_list_show);
1141
1142     menu = gtk_item_factory_get_widget(main_menu_factory, "/View/Show/Packet Details");
1143     gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menu), recent.tree_view_show);
1144
1145     menu = gtk_item_factory_get_widget(main_menu_factory, "/View/Show/Packet Bytes");
1146     gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menu), recent.byte_view_show);
1147
1148     menu = gtk_item_factory_get_widget(main_menu_factory, "/View/Show/Status Bar");
1149     gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menu), recent.statusbar_show);
1150
1151     menu = gtk_item_factory_get_widget(main_menu_factory, "/View/Name Resolution/Enable for MAC Layer");
1152     gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menu), g_resolv_flags & RESOLV_MAC);
1153
1154     menu = gtk_item_factory_get_widget(main_menu_factory, "/View/Name Resolution/Enable for Network Layer");
1155     gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menu), g_resolv_flags & RESOLV_NETWORK);
1156
1157     menu = gtk_item_factory_get_widget(main_menu_factory, "/View/Name Resolution/Enable for Transport Layer");
1158     gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menu), g_resolv_flags & RESOLV_TRANSPORT);
1159
1160     menu = gtk_item_factory_get_widget(main_menu_factory, "/View/Auto Scroll in Live Capture");
1161     gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menu), auto_scroll_live);
1162
1163     main_widgets_rearrange();
1164
1165     /* don't change the time format, if we had a command line value */
1166     if (timestamp_type != TS_NOT_SET) {
1167         recent.gui_time_format = timestamp_type;
1168     }
1169
1170     switch(recent.gui_time_format) {
1171     case(TS_ABSOLUTE):
1172         menu = gtk_item_factory_get_widget(main_menu_factory, 
1173             "/View/Time Display Format/Time of Day");
1174         /* set_active will not trigger the callback when activating an active item! */
1175         recent.gui_time_format = -1;
1176         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menu), FALSE);
1177         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menu), TRUE);
1178         break;
1179     case(TS_ABSOLUTE_WITH_DATE):
1180         menu = gtk_item_factory_get_widget(main_menu_factory, 
1181             "/View/Time Display Format/Date and Time of Day");
1182         recent.gui_time_format = -1;
1183         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menu), TRUE);
1184         break;
1185     case(TS_RELATIVE):
1186         menu = gtk_item_factory_get_widget(main_menu_factory, 
1187             "/View/Time Display Format/Seconds Since Beginning of Capture");
1188         recent.gui_time_format = -1;
1189         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menu), TRUE);
1190         break;
1191     case(TS_DELTA):
1192         menu = gtk_item_factory_get_widget(main_menu_factory, 
1193             "/View/Time Display Format/Seconds Since Previous Packet");
1194         recent.gui_time_format = -1;
1195         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menu), TRUE);
1196         break;
1197     default:
1198         g_assert_not_reached();
1199     }
1200 }
1201
1202
1203 gint
1204 popup_menu_handler(GtkWidget *widget, GdkEvent *event, gpointer data)
1205 {
1206     GtkWidget *menu = (GtkWidget *)data;
1207     GdkEventButton *event_button = NULL;
1208     gint row, column;
1209
1210     if(widget == NULL || event == NULL || data == NULL) {
1211         return FALSE;
1212     }
1213
1214     /*
1215      * If we ever want to make the menu differ based on what row
1216      * and/or column we're above, we'd use "eth_clist_get_selection_info()"
1217      * to find the row and column number for the coordinates; a CTree is,
1218      * I guess, like a CList with one column(?) and the expander widget
1219      * as a pixmap.
1220      */
1221     /* Check if we are on packet_list object */
1222     if (widget == OBJECT_GET_DATA(popup_menu_object, E_MPACKET_LIST_KEY)) {
1223         if (packet_list_get_event_row_column(widget, (GdkEventButton *)event,
1224                                              &row, &column)) {
1225             OBJECT_SET_DATA(popup_menu_object, E_MPACKET_LIST_ROW_KEY,
1226                             GINT_TO_POINTER(row));
1227             OBJECT_SET_DATA(popup_menu_object, E_MPACKET_LIST_COL_KEY,
1228                             GINT_TO_POINTER(column));
1229             packet_list_set_selected_row(row);
1230         }
1231     }
1232
1233     /* Check if we are on tree_view object */
1234     if (widget == tree_view) {
1235         tree_view_select(widget, (GdkEventButton *) event);
1236     }
1237
1238     /* Check if we are on byte_view object */
1239     if(widget == get_notebook_bv_ptr(byte_nb_ptr)) {
1240         byte_view_select(widget, (GdkEventButton *) event);
1241     }
1242
1243     if(event->type == GDK_BUTTON_PRESS) {
1244         event_button = (GdkEventButton *) event;
1245
1246         /* To qoute the "Gdk Event Structures" doc:
1247          * "Normally button 1 is the left mouse button, 2 is the middle button, and 3 is the right button" */
1248         if(event_button->button == 3) {
1249             gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL,
1250                            event_button->button,
1251                            event_button->time);
1252             SIGNAL_EMIT_STOP_BY_NAME(widget, "button_press_event");
1253             return TRUE;
1254         }
1255     }
1256 #if GTK_MAJOR_VERSION >= 2
1257     /* GDK_2BUTTON_PRESS is a doubleclick -> expand/collapse tree row */
1258     /* GTK version 1 seems to be doing this automatically */
1259     if (widget == tree_view && event->type == GDK_2BUTTON_PRESS) {
1260         GtkTreePath      *path;
1261
1262         if (gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(widget),
1263                                           (gint) (((GdkEventButton *)event)->x),
1264                                           (gint) (((GdkEventButton *)event)->y),
1265                                           &path, NULL, NULL, NULL))
1266         {
1267             if (gtk_tree_view_row_expanded(GTK_TREE_VIEW(widget), path))
1268                 gtk_tree_view_collapse_row(GTK_TREE_VIEW(widget), path);
1269             else
1270                 gtk_tree_view_expand_row(GTK_TREE_VIEW(widget), path,
1271                                          FALSE);
1272             gtk_tree_path_free(path);
1273         }
1274     }
1275 #endif
1276     return FALSE;
1277 }
1278
1279 /* Enable or disable menu items based on whether you have a capture file
1280    you've finished reading. */
1281 void
1282 set_menus_for_capture_file(gboolean have_capture_file)
1283 {
1284   set_menu_sensitivity(main_menu_factory, "/File/Open...", have_capture_file);
1285   set_menu_sensitivity(main_menu_factory, "/File/Open Recent", have_capture_file);
1286   set_menu_sensitivity(main_menu_factory, "/File/Close", have_capture_file);
1287   set_menu_sensitivity(main_menu_factory, "/File/Save As...",
1288       have_capture_file);
1289   set_menu_sensitivity(main_menu_factory, "/File/Export", have_capture_file);
1290   set_menu_sensitivity(main_menu_factory, "/View/Reload", have_capture_file);
1291   set_toolbar_for_capture_file(have_capture_file);
1292   packets_bar_update();
1293 }
1294
1295 /* Enable or disable menu items based on whether you have an unsaved
1296    capture file you've finished reading. */
1297 void
1298 set_menus_for_unsaved_capture_file(gboolean have_unsaved_capture_file)
1299 {
1300   set_menu_sensitivity(main_menu_factory, "/File/Save",
1301       have_unsaved_capture_file);
1302   set_toolbar_for_unsaved_capture_file(have_unsaved_capture_file);
1303 }
1304
1305 /* Enable or disable menu items based on whether there's a capture in
1306    progress. */
1307 void
1308 set_menus_for_capture_in_progress(gboolean capture_in_progress)
1309 {
1310   set_menu_sensitivity(main_menu_factory, "/File/Open...",
1311       !capture_in_progress);
1312   set_menu_sensitivity(main_menu_factory, "/File/Open Recent", 
1313       !capture_in_progress);
1314 #ifdef HAVE_LIBPCAP
1315   set_menu_sensitivity(main_menu_factory, "/Capture/Start...",
1316       !capture_in_progress);
1317   set_menu_sensitivity(main_menu_factory, "/Capture/Stop",
1318       capture_in_progress);
1319 #endif /* HAVE_LIBPCAP */
1320   set_toolbar_for_capture_in_progress(capture_in_progress);
1321 }
1322
1323 /* Enable or disable menu items based on whether you have some captured
1324    packets. */
1325 static gboolean
1326 walk_menu_tree_for_captured_packets(menu_item_t *node,
1327     gboolean have_captured_packets)
1328 {
1329         gboolean is_enabled;
1330         menu_item_t *child;
1331
1332         /*
1333          * Is this a leaf node or an interior node?
1334          */
1335         if (node->children == NULL) {
1336                 /*
1337                  * It's a leaf node.
1338                  *
1339                  * If it has no "selected_packet_enabled()" or
1340                  * "selected_tree_row_enabled()" routines, we enable
1341                  * it.  This allows tap windows to be popped up even
1342                  * if you have no capture file; this is done to let
1343                  * the user pop up multiple tap windows before reading
1344                  * in a capture file, so that they can be processed in
1345                  * parallel while the capture file is being read rather
1346                  * than one at at time as you pop up the windows, and to
1347                  * let the user pop up tap windows before starting an
1348                  * "Update list of packets in real time" capture, so that
1349                  * the statistics can be displayed while the capture is
1350                  * in progress.
1351                  *
1352                  * If it has either of those routines, we disable it for
1353                  * now - as long as, when a capture is first available,
1354                  * we don't get called after a packet or tree row is
1355                  * selected, that's OK.
1356                  * XXX - that should be done better.
1357                  */
1358                 if (node->selected_packet_enabled == NULL &&
1359                     node->selected_tree_row_enabled == NULL)
1360                         node->enabled = TRUE;
1361                 else
1362                         node->enabled = FALSE;
1363         } else {
1364                 /*
1365                  * It's an interior node; call
1366                  * "walk_menu_tree_for_captured_packets()" on all its
1367                  * children and, if any of them are enabled, enable
1368                  * this node, otherwise disable it.
1369                  *
1370                  * XXX - should we just leave all interior nodes enabled?
1371                  * Which is a better UI choice?
1372                  */
1373                 is_enabled = FALSE;
1374                 for (child = node->children; child != NULL; child =
1375                     child->next) {
1376                         if (walk_menu_tree_for_captured_packets(child,
1377                             have_captured_packets))
1378                                 is_enabled = TRUE;
1379                 }
1380                 node->enabled = is_enabled;
1381         }
1382
1383         /*
1384          * The root node doesn't correspond to a menu tree item; it
1385          * has a null name pointer.
1386          */
1387         if (node->name != NULL) {
1388                 set_menu_sensitivity(main_menu_factory, node->name,
1389                     node->enabled);
1390         }
1391         return node->enabled;
1392 }
1393
1394 void
1395 set_menus_for_captured_packets(gboolean have_captured_packets)
1396 {
1397   set_menu_sensitivity(main_menu_factory, "/File/Print...",
1398       have_captured_packets);
1399   set_menu_sensitivity(packet_list_menu_factory, "/Print...",
1400       have_captured_packets);
1401   set_menu_sensitivity(main_menu_factory, "/Edit/Find Packet...",
1402       have_captured_packets);
1403   set_menu_sensitivity(main_menu_factory, "/Edit/Find Next",
1404       have_captured_packets);
1405   set_menu_sensitivity(main_menu_factory, "/Edit/Find Previous",
1406       have_captured_packets);
1407   set_menu_sensitivity(main_menu_factory, "/Edit/Go To First Packet",
1408       have_captured_packets);
1409   set_menu_sensitivity(main_menu_factory, "/Edit/Go To Last Packet",
1410       have_captured_packets);
1411   set_menu_sensitivity(main_menu_factory, "/Edit/Go To Packet...",
1412       have_captured_packets);
1413   set_menu_sensitivity(main_menu_factory, "/View/Zoom In",
1414       have_captured_packets);
1415   set_menu_sensitivity(main_menu_factory, "/View/Zoom Out",
1416       have_captured_packets);
1417   set_menu_sensitivity(main_menu_factory, "/View/Normal Size",
1418       have_captured_packets);
1419   set_menu_sensitivity(main_menu_factory, "/View/Coloring Rules...",
1420       have_captured_packets);
1421   set_menu_sensitivity(packet_list_menu_factory, "/Coloring Rules...",
1422       have_captured_packets);
1423   set_menu_sensitivity(main_menu_factory, "/Analyze/Summary",
1424       have_captured_packets);
1425   set_menu_sensitivity(main_menu_factory, "/Analyze/Protocol Hierarchy Statistics", 
1426       have_captured_packets);
1427       
1428   walk_menu_tree_for_captured_packets(&tap_menu_tree_root,
1429       have_captured_packets);
1430   set_toolbar_for_captured_packets(have_captured_packets);
1431   packets_bar_update();
1432 }
1433
1434 /* Enable or disable menu items based on whether a packet is selected and,
1435    if so, on the properties of the packet. */
1436 static gboolean
1437 walk_menu_tree_for_selected_packet(menu_item_t *node, frame_data *fd,
1438     epan_dissect_t *edt)
1439 {
1440         gboolean is_enabled;
1441         menu_item_t *child;
1442
1443         /*
1444          * Is this a leaf node or an interior node?
1445          */
1446         if (node->children == NULL) {
1447                 /*
1448                  * It's a leaf node.
1449                  *
1450                  * If it has no "selected_packet_enabled()" routine,
1451                  * leave its enabled/disabled status alone - it
1452                  * doesn't depend on whether we have a packet selected
1453                  * or not or on the selected packet.
1454                  *
1455                  * If it has a "selected_packet_enabled()" routine,
1456                  * call it and set the item's enabled/disabled status
1457                  * based on its return value.
1458                  */
1459                 if (node->selected_packet_enabled != NULL)
1460                         node->enabled = node->selected_packet_enabled(fd, edt);
1461         } else {
1462                 /*
1463                  * It's an interior node; call
1464                  * "walk_menu_tree_for_selected_packet()" on all its
1465                  * children and, if any of them are enabled, enable
1466                  * this node, otherwise disable it.
1467                  *
1468                  * XXX - should we just leave all interior nodes enabled?
1469                  * Which is a better UI choice?
1470                  */
1471                 is_enabled = FALSE;
1472                 for (child = node->children; child != NULL; child =
1473                     child->next) {
1474                         if (walk_menu_tree_for_selected_packet(child, fd, edt))
1475                                 is_enabled = TRUE;
1476                 }
1477                 node->enabled = is_enabled;
1478         }
1479
1480         /*
1481          * The root node doesn't correspond to a menu tree item; it
1482          * has a null name pointer.
1483          */
1484         if (node->name != NULL) {
1485                 set_menu_sensitivity(main_menu_factory, node->name,
1486                     node->enabled);
1487         }
1488         return node->enabled;
1489 }
1490
1491 void
1492 set_menus_for_selected_packet(capture_file *cf)
1493 {
1494   set_menu_sensitivity(main_menu_factory, "/Edit/Mark Packet",
1495       cf->current_frame != NULL);
1496   set_menu_sensitivity(packet_list_menu_factory, "/Mark Packet",
1497       cf->current_frame != NULL);
1498   set_menu_sensitivity(main_menu_factory, "/Edit/Time Reference",
1499       cf->current_frame != NULL);
1500   set_menu_sensitivity(packet_list_menu_factory, "/Time Reference",
1501       cf->current_frame != NULL);
1502   set_menu_sensitivity(main_menu_factory, "/Edit/Mark All Packets",
1503       cf->current_frame != NULL);
1504   set_menu_sensitivity(main_menu_factory, "/Edit/Unmark All Packets",
1505       cf->current_frame != NULL);
1506   set_menu_sensitivity(main_menu_factory, "/View/Collapse All",
1507       cf->current_frame != NULL);
1508   set_menu_sensitivity(tree_view_menu_factory, "/Collapse All",
1509       cf->current_frame != NULL);
1510   set_menu_sensitivity(main_menu_factory, "/View/Expand All",
1511       cf->current_frame != NULL);
1512   set_menu_sensitivity(tree_view_menu_factory, "/Expand All",
1513       cf->current_frame != NULL);
1514   set_menu_sensitivity(main_menu_factory, "/View/Show Packet In New Window",
1515       cf->current_frame != NULL);
1516   set_menu_sensitivity(packet_list_menu_factory, "/Show Packet In New Window",
1517       cf->current_frame != NULL);
1518   set_menu_sensitivity(main_menu_factory, "/Analyze/Follow TCP Stream",
1519       cf->current_frame != NULL ? (cf->edt->pi.ipproto == IP_PROTO_TCP) : FALSE);
1520   set_menu_sensitivity(NULL, "/Follow TCP Stream",
1521       cf->current_frame != NULL ? (cf->edt->pi.ipproto == IP_PROTO_TCP) : FALSE);
1522   set_menu_sensitivity(main_menu_factory, "/Analyze/Decode As...",
1523       cf->current_frame != NULL && decode_as_ok());
1524   set_menu_sensitivity(NULL, "/Decode As...",
1525       cf->current_frame != NULL && decode_as_ok());
1526   set_menu_sensitivity(tree_view_menu_factory, "/Resolve Name",
1527       cf->current_frame != NULL && (g_resolv_flags & RESOLV_ALL_ADDRS) != RESOLV_ALL_ADDRS);
1528   set_menu_sensitivity(main_menu_factory, "/Analyze/TCP Stream Analysis",
1529       cf->current_frame != NULL ? (cf->edt->pi.ipproto == IP_PROTO_TCP) : FALSE);
1530   set_menu_sensitivity(packet_list_menu_factory, "/Match",
1531       cf->current_frame != NULL);
1532   set_menu_sensitivity(packet_list_menu_factory, "/Prepare",
1533       cf->current_frame != NULL);
1534
1535   walk_menu_tree_for_selected_packet(&tap_menu_tree_root, cf->current_frame,
1536       cf->edt);
1537   packets_bar_update();
1538 }
1539
1540 /* Enable or disable menu items based on whether a tree row is selected
1541    and, if so, on the properties of the tree row. */
1542 static gboolean
1543 walk_menu_tree_for_selected_tree_row(menu_item_t *node, field_info *fi)
1544 {
1545         gboolean is_enabled;
1546         menu_item_t *child;
1547
1548         /*
1549          * Is this a leaf node or an interior node?
1550          */
1551         if (node->children == NULL) {
1552                 /*
1553                  * It's a leaf node.
1554                  *
1555                  * If it has no "selected_tree_row_enabled()" routine,
1556                  * leave its enabled/disabled status alone - it
1557                  * doesn't depend on whether we have a tree row selected
1558                  * or not or on the selected tree row.
1559                  *
1560                  * If it has a "selected_tree_row_enabled()" routine,
1561                  * call it and set the item's enabled/disabled status
1562                  * based on its return value.
1563                  */
1564                 if (node->selected_tree_row_enabled != NULL)
1565                         node->enabled = node->selected_tree_row_enabled(fi);
1566         } else {
1567                 /*
1568                  * It's an interior node; call
1569                  * "walk_menu_tree_for_selected_tree_row()" on all its
1570                  * children and, if any of them are enabled, enable
1571                  * this node, otherwise disable it.
1572                  *
1573                  * XXX - should we just leave all interior nodes enabled?
1574                  * Which is a better UI choice?
1575                  */
1576                 is_enabled = FALSE;
1577                 for (child = node->children; child != NULL; child =
1578                     child->next) {
1579                         if (walk_menu_tree_for_selected_tree_row(child, fi))
1580                                 is_enabled = TRUE;
1581                 }
1582                 node->enabled = is_enabled;
1583         }
1584
1585         /*
1586          * The root node doesn't correspond to a menu tree item; it
1587          * has a null name pointer.
1588          */
1589         if (node->name != NULL) {
1590                 set_menu_sensitivity(main_menu_factory, node->name,
1591                     node->enabled);
1592         }
1593         return node->enabled;
1594 }
1595
1596 void
1597 set_menus_for_selected_tree_row(capture_file *cf)
1598 {
1599   gboolean properties;
1600
1601
1602   set_menu_sensitivity(main_menu_factory, "/File/Export/Selected Packet Bytes...", 
1603       cf->finfo_selected != NULL);  
1604   set_menu_sensitivity(tree_view_menu_factory, "/Export Selected Packet Bytes...", 
1605       cf->finfo_selected != NULL);
1606   set_menu_sensitivity(hexdump_menu_factory, "/Export Selected Packet Bytes...", 
1607       cf->finfo_selected != NULL);
1608   
1609   if (cf->finfo_selected != NULL) {
1610         header_field_info *hfinfo = cf->finfo_selected->hfinfo;
1611         if (hfinfo->parent == -1) {
1612           properties = prefs_is_registered_protocol(hfinfo->abbrev);
1613         } else {
1614           properties = prefs_is_registered_protocol(proto_registrar_get_abbrev(hfinfo->parent));
1615         }
1616         set_menu_sensitivity(main_menu_factory,
1617           "/Edit/Go To Corresponding Packet", hfinfo->type == FT_FRAMENUM);
1618         set_menu_sensitivity(tree_view_menu_factory,
1619           "/Go To Corresponding Packet", hfinfo->type == FT_FRAMENUM);
1620         set_menu_sensitivity(main_menu_factory, "/Analyze/Match",
1621           proto_can_match_selected(cf->finfo_selected, cf->edt));
1622         set_menu_sensitivity(tree_view_menu_factory, "/Match",
1623           proto_can_match_selected(cf->finfo_selected, cf->edt));
1624         set_menu_sensitivity(main_menu_factory, "/Analyze/Prepare",
1625           proto_can_match_selected(cf->finfo_selected, cf->edt));
1626         set_menu_sensitivity(tree_view_menu_factory, "/Prepare",
1627           proto_can_match_selected(cf->finfo_selected, cf->edt));
1628         set_menu_sensitivity(tree_view_menu_factory, "/Protocol Properties...",
1629           properties);
1630   } else {
1631         set_menu_sensitivity(main_menu_factory,
1632             "/Edit/Go To Corresponding Packet", FALSE);
1633         set_menu_sensitivity(tree_view_menu_factory,
1634             "/Go To Corresponding Packet", FALSE);
1635         set_menu_sensitivity(main_menu_factory, "/Analyze/Match", FALSE);
1636         set_menu_sensitivity(tree_view_menu_factory, "/Match", FALSE);
1637         set_menu_sensitivity(main_menu_factory, "/Analyze/Prepare", FALSE);
1638         set_menu_sensitivity(tree_view_menu_factory, "/Prepare", FALSE);
1639         set_menu_sensitivity(tree_view_menu_factory, "/Protocol Properties...",
1640           FALSE);
1641   }
1642
1643   walk_menu_tree_for_selected_tree_row(&tap_menu_tree_root, cf->finfo_selected);
1644 }