Make the routines internal to the filter-editing dialog box static.
[obnox/wireshark/wip.git] / gtk / menu.c
1 /* menu.c
2  * Menu routines
3  *
4  * $Id: menu.c,v 1.22 2000/02/12 06:46:54 guy Exp $
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@zing.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * 
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  * 
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  * 
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <gtk/gtk.h>
31 #include <glib.h>
32
33 #include <string.h>
34 #include <stdio.h>
35
36 #ifdef HAVE_SYS_TYPES_H
37 # include <sys/types.h>
38 #endif
39
40 #include "../menu.h"
41
42 #include "main.h"
43 #include "menu.h"
44 #include "packet.h"
45 #include "capture_dlg.h"
46 #include "filter_prefs.h"
47 #include "find_dlg.h"
48 #include "goto_dlg.h"
49 #include "summary_dlg.h"
50 #include "display_opts.h"
51 #include "prefs_dlg.h"
52 #include "print.h"
53 #include "follow.h"
54 #include "colors.h"
55 #include "keys.h"
56 #include "plugins.h"
57
58 #define GTK_MENU_FUNC(a) ((GtkItemFactoryCallback)(a))
59
60 static void menus_init(void);
61 static void set_menu_sensitivity (gchar *, gint);
62
63 /* This is the GtkItemFactoryEntry structure used to generate new menus.
64        Item 1: The menu path. The letter after the underscore indicates an
65                accelerator key once the menu is open.
66        Item 2: The accelerator key for the entry
67        Item 3: The callback function.
68        Item 4: The callback action.  This changes the parameters with
69                which the function is called.  The default is 0.
70        Item 5: The item type, used to define what kind of an item it is.
71                Here are the possible values:
72
73                NULL               -> "<Item>"
74                ""                 -> "<Item>"
75                "<Title>"          -> create a title item
76                "<Item>"           -> create a simple item
77                "<CheckItem>"      -> create a check item
78                "<ToggleItem>"     -> create a toggle item
79                "<RadioItem>"      -> create a radio item
80                <path>             -> path of a radio item to link against
81                "<Separator>"      -> create a separator
82                "<Branch>"         -> create an item to hold sub items (optional)
83                "<LastBranch>"     -> create a right justified branch 
84     */
85
86 /* main menu */
87 static GtkItemFactoryEntry menu_items[] =
88 {
89   {"/_File", NULL, NULL, 0, "<Branch>" },
90   {"/File/_Open...", "<control>O", GTK_MENU_FUNC(file_open_cmd_cb), 0, NULL},
91   {"/File/_Close", "<control>W", GTK_MENU_FUNC(file_close_cmd_cb), 0, NULL},
92   {"/File/_Save", "<control>S", GTK_MENU_FUNC(file_save_cmd_cb), 0, NULL},
93   {"/File/Save _As...", NULL, GTK_MENU_FUNC(file_save_as_cmd_cb), 0, NULL},
94   {"/File/_Reload", "<control>R", GTK_MENU_FUNC(file_reload_cmd_cb), 0, NULL},
95   {"/File/<separator>", NULL, NULL, 0, "<Separator>"},
96   {"/File/Print...", NULL, GTK_MENU_FUNC(file_print_cmd_cb), 0, NULL},
97   {"/File/Print Pac_ket", "<control>P", GTK_MENU_FUNC(file_print_packet_cmd_cb), 0, NULL},
98   {"/File/<separator>", NULL, NULL, 0, "<Separator>"},
99   {"/File/_Quit", "<control>Q", GTK_MENU_FUNC(file_quit_cmd_cb), 0, NULL},
100   {"/_Edit", NULL, NULL, 0, "<Branch>" },
101   {"/Edit/Cut", "<control>X", NULL, 0, NULL},
102   {"/Edit/Copy", "<control>C", NULL, 0, NULL},
103   {"/Edit/Paste", "<control>V", NULL, 0, NULL},
104   {"/Edit/<separator>", NULL, NULL, 0, "<Separator>"},
105   {"/Edit/_Find Frame...", "<control>F", GTK_MENU_FUNC(find_frame_cb), 0, NULL},
106   {"/Edit/_Go To Frame...", "<control>G", GTK_MENU_FUNC(goto_frame_cb), 0, NULL},
107   {"/Edit/<separator>", NULL, NULL, 0, "<Separator>"},
108   {"/Edit/_Preferences...", NULL, GTK_MENU_FUNC(prefs_cb), E_PR_PG_NONE, NULL},
109   {"/Edit/_Filters...", NULL, GTK_MENU_FUNC(filter_dialog_cb), 0, NULL},
110 #ifdef HAVE_LIBPCAP
111   {"/_Capture", NULL, NULL, 0, "<Branch>" },
112   {"/Capture/_Start...", "<control>K", GTK_MENU_FUNC(capture_prep_cb), 0, NULL},
113 #endif
114   {"/_Display", NULL, NULL, 0, "<Branch>" },
115   {"/Display/_Options...", NULL, GTK_MENU_FUNC(display_opt_cb), 0, NULL},
116   {"/Display/_Match Selected", NULL, GTK_MENU_FUNC(match_selected_cb), 0, NULL},
117   {"/Display/_Colorize Display...", NULL, GTK_MENU_FUNC(color_display_cb), 0, NULL},
118   {"/Display/_Find Frame...", NULL, GTK_MENU_FUNC(find_frame_cb), 0, NULL},
119   {"/Display/_Go To Frame...", NULL, GTK_MENU_FUNC(goto_frame_cb), 0, NULL},
120   {"/Display/Collapse _All", NULL, GTK_MENU_FUNC(collapse_all_cb), 0, NULL},
121   {"/Display/_Expand All", NULL, GTK_MENU_FUNC(expand_all_cb), 0, NULL},
122   {"/_Tools", NULL, NULL, 0, "<Branch>" },
123 #ifdef HAVE_PLUGINS
124   {"/Tools/_Plugins...", NULL, GTK_MENU_FUNC(tools_plugins_cmd_cb), 0, NULL},
125 #endif
126   {"/Tools/_Follow TCP Stream", NULL, GTK_MENU_FUNC(follow_stream_cb), 0, NULL},
127 /*  {"/Tools/Graph", NULL, NULL, 0, NULL}, future use */
128   {"/Tools/_Summary", NULL, GTK_MENU_FUNC(summary_open_cb), 0, NULL},
129   {"/_Help", NULL, NULL, 0, "<LastBranch>" },
130   {"/Help/_About Ethereal...", NULL, GTK_MENU_FUNC(about_ethereal), 0, NULL}
131 };
132
133 /* calculate the number of menu_items */
134 static int nmenu_items = sizeof(menu_items) / sizeof(menu_items[0]);
135
136 /* packet list popup */
137 static GtkItemFactoryEntry packet_list_menu_items[] =
138 {
139         {"/Match Selected", NULL, GTK_MENU_FUNC(match_selected_cb), 0, NULL},
140         {"/Follow TCP Stream", NULL, GTK_MENU_FUNC(follow_stream_cb), 0, NULL},
141         {"/Filters...", NULL, GTK_MENU_FUNC(filter_dialog_cb), 0, NULL},
142         {"/<separator>", NULL, NULL, 0, "<Separator>"},
143         {"/Colorize Display...", NULL, GTK_MENU_FUNC(color_display_cb), 0, NULL},
144         {"/Print...", NULL, GTK_MENU_FUNC(file_print_cmd_cb), 0, NULL},
145         {"/Print Packet", NULL, GTK_MENU_FUNC(file_print_packet_cmd_cb), 0, NULL},
146 };
147
148 static GtkItemFactoryEntry tree_view_menu_items[] =
149 {
150         {"/Match Selected", NULL, GTK_MENU_FUNC(match_selected_cb), 0, NULL},
151         {"/Follow TCP Stream", NULL, GTK_MENU_FUNC(follow_stream_cb), 0, NULL},
152         {"/Filters...", NULL, GTK_MENU_FUNC(filter_dialog_cb), 0, NULL},
153         {"/<separator>", NULL, NULL, 0, "<Separator>"},
154         {"/Collapse All", NULL, GTK_MENU_FUNC(collapse_all_cb), 0, NULL},
155         {"/Expand All", NULL, GTK_MENU_FUNC(expand_all_cb), 0, NULL}
156 };
157
158
159 static int initialize = TRUE;
160 static GtkItemFactory *factory = NULL;
161 static GtkItemFactory *packet_list_menu_factory = NULL;
162 static GtkItemFactory *tree_view_menu_factory = NULL;
163
164 static GSList *popup_menu_list = NULL;
165
166 static GtkAccelGroup *grp;
167
168 void
169 get_main_menu(GtkWidget ** menubar, GtkAccelGroup ** table) {
170
171   grp = gtk_accel_group_new();
172
173   if (initialize) {
174     popup_menu_object = gtk_widget_new(GTK_TYPE_WIDGET, NULL);
175     menus_init();
176   }
177
178   if (menubar)
179     *menubar = factory->widget;
180
181   if (table)
182     *table = grp;
183 }
184
185 static void
186 menus_init(void) {
187
188   if (initialize) {
189     initialize = FALSE;
190
191     /* popup */
192
193     packet_list_menu_factory = gtk_item_factory_new(GTK_TYPE_MENU, "<main>", NULL);
194     gtk_item_factory_create_items_ac(packet_list_menu_factory, sizeof(packet_list_menu_items)/sizeof(packet_list_menu_items[0]), packet_list_menu_items, NULL, 2);
195     gtk_object_set_data(GTK_OBJECT(popup_menu_object), PM_PACKET_LIST_KEY, packet_list_menu_factory->widget);
196     popup_menu_list = g_slist_append((GSList *)popup_menu_list, packet_list_menu_factory);
197
198     tree_view_menu_factory = gtk_item_factory_new(GTK_TYPE_MENU, "<main>", NULL);
199     gtk_item_factory_create_items_ac(tree_view_menu_factory, sizeof(tree_view_menu_items)/sizeof(tree_view_menu_items[0]), tree_view_menu_items, NULL, 2);
200     gtk_object_set_data(GTK_OBJECT(popup_menu_object), PM_TREE_VIEW_KEY, tree_view_menu_factory->widget);
201     popup_menu_list = g_slist_append((GSList *)popup_menu_list, tree_view_menu_factory);
202     
203     factory = gtk_item_factory_new(GTK_TYPE_MENU_BAR, "<main>", grp);
204     gtk_item_factory_create_items_ac(factory, nmenu_items, menu_items, NULL,2);
205     set_menus_for_unsaved_capture_file(FALSE);
206     set_menus_for_capture_file(FALSE);
207     set_menu_sensitivity("/Edit/Cut", FALSE);
208     set_menu_sensitivity("/Edit/Copy", FALSE);
209     set_menu_sensitivity("/Edit/Paste", FALSE);
210     set_menus_for_captured_packets(FALSE);
211     set_menus_for_selected_packet(FALSE);
212   }
213 }
214
215 void
216 set_menu_sensitivity_meat(GtkItemFactory *ifactory, gchar *path, gint val) {
217         GtkWidget *menu = NULL;
218         
219         if((menu = gtk_item_factory_get_widget(ifactory, path)) != NULL) {
220                 gtk_widget_set_sensitive(menu,val);
221         }
222 }
223
224 static void
225 set_menu_sensitivity (gchar *path, gint val) {
226   GSList *menu_list = popup_menu_list;
227   gchar *shortpath = strrchr(path, '/');
228
229   set_menu_sensitivity_meat(factory, path, val);
230
231   while (menu_list != NULL) {
232         set_menu_sensitivity_meat(menu_list->data, shortpath, val);
233         menu_list = g_slist_next(menu_list);
234   }
235   
236 }
237
238 void
239 set_menu_object_data_meat(GtkItemFactory *ifactory, gchar *path, gchar *key, gpointer data)
240 {
241         GtkWidget *menu = NULL;
242         
243         if ((menu = gtk_item_factory_get_widget(ifactory, path)) != NULL)
244                 gtk_object_set_data(GTK_OBJECT(menu), key, data);
245 }
246
247 void
248 set_menu_object_data (gchar *path, gchar *key, gpointer data) {
249   GSList *menu_list = popup_menu_list;
250   gchar *shortpath = strrchr(path, '/');
251   
252   set_menu_object_data_meat(factory, path, key, data);
253   while (menu_list != NULL) {
254         set_menu_object_data_meat(menu_list->data, shortpath, key, data);
255         menu_list = g_slist_next(menu_list);
256   }
257 }
258
259 void
260 popup_menu_handler(GtkWidget *widget, GdkEvent *event)
261 {
262         GtkWidget *menu = NULL;
263         GdkEventButton *event_button = NULL;
264
265         if(widget == NULL || event == NULL) {
266                 return;
267         }
268         
269         menu = widget;
270         if(event->type == GDK_BUTTON_PRESS) {
271                 event_button = (GdkEventButton *) event;
272                 
273                 if(event_button->button == 3) {
274                         gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL, event_button->button, event_button->time);
275                 }
276         }
277 }
278
279 /* Enable or disable menu items based on whether you have a capture file
280    you've finished reading. */
281 void
282 set_menus_for_capture_file(gboolean have_capture_file)
283 {
284   set_menu_sensitivity("/File/Open...", have_capture_file);
285   set_menu_sensitivity("/File/Save As...", have_capture_file);
286   set_menu_sensitivity("/File/Close", have_capture_file);
287   set_menu_sensitivity("/File/Reload", have_capture_file);
288 }
289
290 /* Enable or disable menu items based on whether you have an unsaved
291    capture file you've finished reading. */
292 void
293 set_menus_for_unsaved_capture_file(gboolean have_unsaved_capture_file)
294 {
295   set_menu_sensitivity("/File/Save", have_unsaved_capture_file);
296 }
297
298 /* Enable or disable menu items based on whether there's a capture in
299    progress. */
300 void
301 set_menus_for_capture_in_progress(gboolean capture_in_progress)
302 {
303   set_menu_sensitivity("/File/Open...", !capture_in_progress);
304   set_menu_sensitivity("/Capture/Start...", !capture_in_progress);
305 }
306
307 /* Enable or disable menu items based on whether you have some captured
308    packets. */
309 void
310 set_menus_for_captured_packets(gboolean have_captured_packets)
311 {
312   set_menu_sensitivity("/File/Print...", have_captured_packets);
313   set_menu_sensitivity("/Edit/Find Frame...", have_captured_packets);
314   set_menu_sensitivity("/Edit/Go To Frame...", have_captured_packets);
315   set_menu_sensitivity("/Display/Match Selected", have_captured_packets);
316   set_menu_sensitivity("/Display/Colorize Display...", have_captured_packets);
317   set_menu_sensitivity("/Display/Find Frame...", have_captured_packets);
318   set_menu_sensitivity("/Display/Go To Frame...", have_captured_packets);
319   set_menu_sensitivity("/Tools/Summary", have_captured_packets);
320 }
321
322 /* Enable or disable menu items based on whether a packet is selected. */
323 void
324 set_menus_for_selected_packet(gboolean have_selected_packet)
325 {
326   set_menu_sensitivity("/File/Print Packet", have_selected_packet);
327   set_menu_sensitivity("/Display/Collapse All", have_selected_packet);
328   set_menu_sensitivity("/Display/Expand All", have_selected_packet);
329   set_menu_sensitivity("/Tools/Follow TCP Stream",
330       have_selected_packet ? (pi.ipproto == 6) : FALSE);
331 }