Added more SAP types, from the ncpfs source.
[obnox/wireshark/wip.git] / menu.c
1 /* menu.c
2  * Menu routines
3  *
4  * $Id: menu.c,v 1.5 1998/09/26 19:28:49 gerald 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 <glib.h>
31
32 #include <gtk/gtk.h>
33 #include <pcap.h>
34
35 #include <strings.h>
36
37 #include "menu.h"
38 #include "ethereal.h"
39 #include "capture.h"
40 #include "filter.h"
41 #include "packet.h"
42 #include "print.h"
43 #include "follow.h"
44 #include "prefs.h"
45
46 /* Much of this was take from the GTK+ tuturial at http://www.gtk.org */
47
48 static void menus_remove_accel (GtkWidget *, gchar *, gchar *);
49 static gint menus_install_accel (GtkWidget *, gchar *, gchar, gchar, gchar *);
50
51 /* this is the GtkMenuEntry structure used to create new menus.  The
52  * first member is the menu definition string.  The second, the
53  * default accelerator key used to access this menu function with
54  * the keyboard.  The third is the callback function to call when
55  * this menu item is selected (by the accelerator key, or with the
56  * mouse.) The last member is the data to pass to your callback function.
57  */
58
59 static GtkMenuEntry menu_items[] =
60 {
61   {"<Main>/File/Open", "<control>O", file_open_cmd_cb, NULL},
62   {"<Main>/File/Close", "<control>W", file_close_cmd_cb, NULL},
63   {"<Main>/File/Save", "<control>S", NULL, NULL},
64   {"<Main>/File/Save as", NULL, NULL, NULL},
65   {"<Main>/File/<separator>", NULL, NULL, NULL},
66   {"<Main>/File/Print Packet", "<control>P", file_print_cmd_cb, NULL},
67   {"<Main>/File/<separator>", NULL, NULL, NULL},
68   {"<Main>/File/Quit", "<control>Q", file_quit_cmd_cb, NULL},
69   {"<Main>/Edit/Cut", "<control>X", NULL, NULL},
70   {"<Main>/Edit/Copy", "<control>C", NULL, NULL},
71   {"<Main>/Edit/Paste", "<control>V", NULL, NULL},
72   {"<Main>/Edit/<separator>", NULL, NULL, NULL},
73   {"<Main>/Edit/Find", "<control>F", NULL, NULL},
74   {"<Main>/Edit/<separator>", NULL, NULL, NULL},
75   {"<Main>/Edit/Preferences", NULL, prefs_cb, NULL},
76   {"<Main>/Tools/Capture", "<control>K", capture_prep_cb, NULL},
77   {"<Main>/Tools/Filter", NULL, filter_sel_cb, NULL},
78   {"<Main>/Tools/Follow TCP Stream", NULL, follow_stream_cb, NULL},
79   {"<Main>/Tools/Graph", NULL, NULL, NULL},
80   {"<Main>/Help/About Ethereal", NULL, NULL, NULL}
81 };
82
83 /* calculate the number of menu_items */
84 static int nmenu_items = sizeof(menu_items) / sizeof(menu_items[0]);
85
86 static int initialize = TRUE;
87 static GtkMenuFactory *factory = NULL;
88 static GtkMenuFactory *subfactory[1];
89 static GHashTable *entry_ht = NULL;
90
91 void
92 get_main_menu(GtkWidget ** menubar, GtkAcceleratorTable ** table) {
93   if (initialize)
94     menus_init();
95
96   if (menubar)
97     *menubar = subfactory[0]->widget;
98   if (table)
99     *table = subfactory[0]->table;
100 }
101
102 void
103 menus_init(void) {
104   GtkMenuPath *mp;
105
106   if (initialize) {
107     initialize = FALSE;
108
109     factory = gtk_menu_factory_new(GTK_MENU_FACTORY_MENU_BAR);
110     subfactory[0] = gtk_menu_factory_new(GTK_MENU_FACTORY_MENU_BAR);
111
112     gtk_menu_factory_add_subfactory(factory, subfactory[0], "<Main>");
113     menus_create(menu_items, nmenu_items);
114
115     set_menu_sensitivity("<Main>/File/Close", FALSE);
116     set_menu_sensitivity("<Main>/File/Save", FALSE);
117     set_menu_sensitivity("<Main>/File/Save as", FALSE);
118     set_menu_sensitivity("<Main>/Edit/Cut", FALSE);
119     set_menu_sensitivity("<Main>/Edit/Copy", FALSE);
120     set_menu_sensitivity("<Main>/Edit/Paste", FALSE);
121     set_menu_sensitivity("<Main>/Edit/Find", FALSE);
122     set_menu_sensitivity("<Main>/Tools/Graph", FALSE);
123     set_menu_sensitivity("<Main>/Help/About Ethereal", FALSE);
124     if ((mp = gtk_menu_factory_find(factory, "<Main>/Help")) != NULL) {
125       gtk_menu_item_right_justify((GtkMenuItem *) mp->widget);
126     }
127   }
128 }
129
130 void
131 set_menu_sensitivity (gchar *path, gint val) {
132   GtkMenuPath *mp;
133   
134   if ((mp = gtk_menu_factory_find(factory, path)) != NULL) {
135     gtk_widget_set_sensitive(mp->widget, val);
136   }
137 }
138
139 void
140 menus_create(GtkMenuEntry * entries, int nmenu_entries) {
141   char *accelerator;
142   int i;
143
144   if (initialize)
145     menus_init();
146
147   if (entry_ht)
148     for (i = 0; i < nmenu_entries; i++) {
149       accelerator = g_hash_table_lookup(entry_ht, entries[i].path);
150       if (accelerator) {
151         if (accelerator[0] == '\0')
152           entries[i].accelerator = NULL;
153         else
154           entries[i].accelerator = accelerator;
155       }
156     }
157   gtk_menu_factory_add_entries(factory, entries, nmenu_entries);
158
159   for (i = 0; i < nmenu_entries; i++)
160     if (entries[i].widget) {
161       gtk_signal_connect(GTK_OBJECT(entries[i].widget), "install_accelerator",
162          (GtkSignalFunc) menus_install_accel, entries[i].path);
163       gtk_signal_connect(GTK_OBJECT(entries[i].widget), "remove_accelerator",
164         (GtkSignalFunc) menus_remove_accel, entries[i].path);
165   }
166 }
167
168 static gint
169 menus_install_accel(GtkWidget * widget, gchar * signal_name, gchar key, gchar modifiers, gchar * path) {
170   char accel[64];
171   char *t1, t2[2];
172
173   accel[0] = '\0';
174   if (modifiers & GDK_CONTROL_MASK)
175     strcat(accel, "<control>");
176   if (modifiers & GDK_SHIFT_MASK)
177     strcat(accel, "<shift>");
178   if (modifiers & GDK_MOD1_MASK)
179     strcat(accel, "<alt>");
180
181   t2[0] = key;
182   t2[1] = '\0';
183   strcat(accel, t2);
184
185   if (entry_ht) {
186     t1 = g_hash_table_lookup(entry_ht, path);
187     g_free(t1);
188   } else
189     entry_ht = g_hash_table_new(g_str_hash, g_str_equal);
190
191   g_hash_table_insert(entry_ht, path, g_strdup(accel));
192
193   return TRUE;
194 }
195
196 static void
197 menus_remove_accel(GtkWidget * widget, gchar * signal_name, gchar * path) {
198   char *t;
199
200   if (entry_ht) {
201     t = g_hash_table_lookup(entry_ht, path);
202     g_free(t);
203
204     g_hash_table_insert(entry_ht, path, g_strdup(""));
205   }
206 }
207
208 void
209 menus_set_sensitive(char *path, int sensitive) {
210   GtkMenuPath *menu_path;
211
212   if (initialize)
213     menus_init();
214
215   menu_path = gtk_menu_factory_find(factory, path);
216   if (menu_path)
217     gtk_widget_set_sensitive(menu_path->widget, sensitive);
218   else
219     g_warning("Unable to set sensitivity for menu which doesn't exist: %s", path);
220 }