Initial revision
[obnox/wireshark/wip.git] / menu.c
1 /* menu.c
2  * Menu routines
3  *
4  * Ethereal - Network traffic analyzer
5  * By Gerald Combs <gerald@zing.org>
6  * Copyright 1998 Gerald Combs
7  *
8  * 
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  * 
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  * 
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  */
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include <glib.h>
29
30 #include <gtk/gtk.h>
31 #include <pcap.h>
32
33 #include <strings.h>
34
35 #include "menu.h"
36 #include "ethereal.h"
37 #include "capture.h"
38 #include "filter.h"
39 #include "packet.h"
40 #include "print.h"
41
42 /* Much of this was take from the GTK+ tuturial at http://www.gtk.org */
43
44 static void menus_remove_accel (GtkWidget *, gchar *, gchar *);
45 static gint menus_install_accel (GtkWidget *, gchar *, gchar, gchar, gchar *);
46
47 /* this is the GtkMenuEntry structure used to create new menus.  The
48  * first member is the menu definition string.  The second, the
49  * default accelerator key used to access this menu function with
50  * the keyboard.  The third is the callback function to call when
51  * this menu item is selected (by the accelerator key, or with the
52  * mouse.) The last member is the data to pass to your callback function.
53  */
54
55 static GtkMenuEntry menu_items[] =
56 {
57   {"<Main>/File/Open", "<control>O", file_open_cmd_cb, NULL},
58   {"<Main>/File/Close", "<control>W", file_close_cmd_cb, NULL},
59   {"<Main>/File/Save", "<control>S", NULL, NULL},
60   {"<Main>/File/Save as", NULL, NULL, NULL},
61   {"<Main>/File/<separator>", NULL, NULL, NULL},
62   {"<Main>/File/Print Packet", "<control>P", file_print_cmd_cb, NULL},
63   {"<Main>/File/<separator>", NULL, NULL, NULL},
64   {"<Main>/File/Quit", "<control>Q", file_quit_cmd_cb, NULL},
65   {"<Main>/Edit/Cut", "<control>X", NULL, NULL},
66   {"<Main>/Edit/Copy", "<control>C", NULL, NULL},
67   {"<Main>/Edit/Paste", "<control>V", NULL, NULL},
68   {"<Main>/Edit/<separator>", NULL, NULL, NULL},
69   {"<Main>/Edit/Find", "<control>F", NULL, NULL},
70   {"<Main>/Edit/<separator>", NULL, NULL, NULL},
71   {"<Main>/Edit/Printer Options", NULL, printer_opts_cb, NULL},
72   {"<Main>/Tools/Capture", "<control>K", capture_prep_cb, NULL},
73   {"<Main>/Tools/Filter", NULL, filter_sel_cb, NULL},
74   {"<Main>/Tools/Graph", NULL, NULL, NULL},
75   {"<Main>/Help/About Ethereal", NULL, NULL, NULL}
76 };
77
78 /* calculate the number of menu_items */
79 static int nmenu_items = sizeof(menu_items) / sizeof(menu_items[0]);
80
81 static int initialize = TRUE;
82 static GtkMenuFactory *factory = NULL;
83 static GtkMenuFactory *subfactory[1];
84 static GHashTable *entry_ht = NULL;
85
86 void
87 get_main_menu(GtkWidget ** menubar, GtkAcceleratorTable ** table) {
88   if (initialize)
89     menus_init();
90
91   if (menubar)
92     *menubar = subfactory[0]->widget;
93   if (table)
94     *table = subfactory[0]->table;
95 }
96
97 void
98 menus_init(void) {
99   GtkMenuPath *mp;
100
101   if (initialize) {
102     initialize = FALSE;
103
104     factory = gtk_menu_factory_new(GTK_MENU_FACTORY_MENU_BAR);
105     subfactory[0] = gtk_menu_factory_new(GTK_MENU_FACTORY_MENU_BAR);
106
107     gtk_menu_factory_add_subfactory(factory, subfactory[0], "<Main>");
108     menus_create(menu_items, nmenu_items);
109
110     set_menu_sensitivity("<Main>/File/Close", FALSE);
111     set_menu_sensitivity("<Main>/File/Save", FALSE);
112     set_menu_sensitivity("<Main>/File/Save as", FALSE);
113     set_menu_sensitivity("<Main>/Edit/Cut", FALSE);
114     set_menu_sensitivity("<Main>/Edit/Copy", FALSE);
115     set_menu_sensitivity("<Main>/Edit/Paste", FALSE);
116     set_menu_sensitivity("<Main>/Edit/Find", FALSE);
117     set_menu_sensitivity("<Main>/Tools/Graph", FALSE);
118     set_menu_sensitivity("<Main>/Help/About Ethereal", FALSE);
119     if ((mp = gtk_menu_factory_find(factory, "<Main>/Help")) != NULL) {
120       gtk_menu_item_right_justify((GtkMenuItem *) mp->widget);
121     }
122   }
123 }
124
125 void
126 set_menu_sensitivity (gchar *path, gint val) {
127   GtkMenuPath *mp;
128   
129   if ((mp = gtk_menu_factory_find(factory, path)) != NULL) {
130     gtk_widget_set_sensitive(mp->widget, val);
131   }
132 }
133
134 void
135 menus_create(GtkMenuEntry * entries, int nmenu_entries) {
136   char *accelerator;
137   int i;
138
139   if (initialize)
140     menus_init();
141
142   if (entry_ht)
143     for (i = 0; i < nmenu_entries; i++) {
144       accelerator = g_hash_table_lookup(entry_ht, entries[i].path);
145       if (accelerator) {
146         if (accelerator[0] == '\0')
147           entries[i].accelerator = NULL;
148         else
149           entries[i].accelerator = accelerator;
150       }
151     }
152   gtk_menu_factory_add_entries(factory, entries, nmenu_entries);
153
154   for (i = 0; i < nmenu_entries; i++)
155     if (entries[i].widget) {
156       gtk_signal_connect(GTK_OBJECT(entries[i].widget), "install_accelerator",
157          (GtkSignalFunc) menus_install_accel, entries[i].path);
158       gtk_signal_connect(GTK_OBJECT(entries[i].widget), "remove_accelerator",
159         (GtkSignalFunc) menus_remove_accel, entries[i].path);
160   }
161 }
162
163 static gint
164 menus_install_accel(GtkWidget * widget, gchar * signal_name, gchar key, gchar modifiers, gchar * path) {
165   char accel[64];
166   char *t1, t2[2];
167
168   accel[0] = '\0';
169   if (modifiers & GDK_CONTROL_MASK)
170     strcat(accel, "<control>");
171   if (modifiers & GDK_SHIFT_MASK)
172     strcat(accel, "<shift>");
173   if (modifiers & GDK_MOD1_MASK)
174     strcat(accel, "<alt>");
175
176   t2[0] = key;
177   t2[1] = '\0';
178   strcat(accel, t2);
179
180   if (entry_ht) {
181     t1 = g_hash_table_lookup(entry_ht, path);
182     g_free(t1);
183   } else
184     entry_ht = g_hash_table_new(g_str_hash, g_str_equal);
185
186   g_hash_table_insert(entry_ht, path, g_strdup(accel));
187
188   return TRUE;
189 }
190
191 static void
192 menus_remove_accel(GtkWidget * widget, gchar * signal_name, gchar * path) {
193   char *t;
194
195   if (entry_ht) {
196     t = g_hash_table_lookup(entry_ht, path);
197     g_free(t);
198
199     g_hash_table_insert(entry_ht, path, g_strdup(""));
200   }
201 }
202
203 void
204 menus_set_sensitive(char *path, int sensitive) {
205   GtkMenuPath *menu_path;
206
207   if (initialize)
208     menus_init();
209
210   menu_path = gtk_menu_factory_find(factory, path);
211   if (menu_path)
212     gtk_widget_set_sensitive(menu_path->widget, sensitive);
213   else
214     g_warning("Unable to set sensitivity for menu which doesn't exist: %s", path);
215 }