97a37883d1ed30df86f6753f61246436a8dbbec6
[obnox/wireshark/wip.git] / gtk / menu.c
1 /* menu.c
2  * Menu routines
3  *
4  * $Id: menu.c,v 1.1 1999/09/01 03:04:23 gram 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
32 #include <string.h>
33 #include <stdio.h>
34
35 #ifdef HAVE_SYS_TYPES_H
36 # include <sys/types.h>
37 #endif
38
39 #include "ethereal.h"
40 #include "menu.h"
41 #include "packet.h"
42 #include "capture.h"
43 #include "summary.h"
44 #include "display.h"
45 #include "prefs.h"
46 #include "print.h"
47 #include "follow.h"
48 #include "colors.h"
49
50
51 GtkAccelGroup *grp;
52 /* This is the GtkItemFactoryEntry structure used to generate new menus.
53        Item 1: The menu path. The letter after the underscore indicates an
54                accelerator key once the menu is open.
55        Item 2: The accelerator key for the entry
56        Item 3: The callback function.
57        Item 4: The callback action.  This changes the parameters with
58                which the function is called.  The default is 0.
59        Item 5: The item type, used to define what kind of an item it is.
60                Here are the possible values:
61
62                NULL               -> "<Item>"
63                ""                 -> "<Item>"
64                "<Title>"          -> create a title item
65                "<Item>"           -> create a simple item
66                "<CheckItem>"      -> create a check item
67                "<ToggleItem>"     -> create a toggle item
68                "<RadioItem>"      -> create a radio item
69                <path>             -> path of a radio item to link against
70                "<Separator>"      -> create a separator
71                "<Branch>"         -> create an item to hold sub items (optional)
72                "<LastBranch>"     -> create a right justified branch 
73     */
74
75 static GtkItemFactoryEntry menu_items[] =
76 {
77   {"/_File", NULL, NULL, 0, "<Branch>" },
78   {"/File/_Open...", "<control>O", GTK_MENU_FUNC(file_open_cmd_cb), 0, NULL},
79   {"/File/_Close", "<control>W", GTK_MENU_FUNC(file_close_cmd_cb), 0, NULL},
80   {"/File/_Save", "<control>S", GTK_MENU_FUNC(file_save_cmd_cb), 0, NULL},
81   {"/File/Save _As...", NULL, GTK_MENU_FUNC(file_save_as_cmd_cb), 0, NULL},
82   {"/File/_Reload", "<control>R", GTK_MENU_FUNC(file_reload_cmd_cb), 0, NULL},
83   {"/File/<separator>", NULL, NULL, 0, "<Separator>"},
84   {"/File/Print...", NULL, GTK_MENU_FUNC(file_print_cmd_cb), 0, NULL},
85   {"/File/Print Pac_ket", "<control>P", GTK_MENU_FUNC(file_print_packet_cmd_cb), 0, NULL},
86   {"/File/<separator>", NULL, NULL, 0, "<Separator>"},
87   {"/File/_Quit", "<control>Q", GTK_MENU_FUNC(file_quit_cmd_cb), 0, NULL},
88   {"/_Edit", NULL, NULL, 0, "<Branch>" },
89   {"/Edit/Cut", "<control>X", NULL, 0, NULL},
90   {"/Edit/Copy", "<control>C", NULL, 0, NULL},
91   {"/Edit/Paste", "<control>V", NULL, 0, NULL},
92   {"/Edit/<separator>", NULL, NULL, 0, "<Separator>"},
93   {"/Edit/Find", "<control>F", NULL, 0, NULL},
94   {"/Edit/<separator>", NULL, NULL, 0, "<Separator>"},
95   {"/Edit/_Preferences...", NULL, GTK_MENU_FUNC(prefs_cb), E_PR_PG_NONE, NULL},
96 #ifdef HAVE_LIBPCAP
97   {"/_Capture", NULL, NULL, 0, "<Branch>" },
98   {"/Capture/_Start...", "<control>K", GTK_MENU_FUNC(capture_prep_cb), 0, NULL},
99 #endif
100   {"/_Display", NULL, NULL, 0, "<Branch>" },
101   {"/Display/_Options...", NULL, GTK_MENU_FUNC(display_opt_cb), 0, NULL},
102   {"/Display/_Match Selected", NULL, GTK_MENU_FUNC(match_selected_cb), 0, NULL},
103   {"/Display/_Colorize Display...", NULL, GTK_MENU_FUNC(color_display_cb), 0, NULL},
104   {"/_Tools", NULL, NULL, 0, "<Branch>" },
105   {"/Tools/_Follow TCP Stream", NULL, GTK_MENU_FUNC(follow_stream_cb), 0, NULL},
106 /*  {"/Tools/Graph", NULL, NULL, 0, NULL}, future use */
107   {"/Tools/_Summary", NULL, GTK_MENU_FUNC(summary_prep_cb), 0, NULL},
108   {"/_Help", NULL, NULL, 0, "<LastBranch>" },
109   {"/Help/_About Ethereal...", NULL, GTK_MENU_FUNC(about_ethereal), 0, NULL}
110 };
111
112 /* calculate the number of menu_items */
113 static int nmenu_items = sizeof(menu_items) / sizeof(menu_items[0]);
114
115 static int initialize = TRUE;
116 static GtkItemFactory *factory = NULL;
117
118 void
119 get_main_menu(GtkWidget ** menubar, GtkAccelGroup ** table) {
120
121   grp = gtk_accel_group_new();
122
123   if (initialize)
124     menus_init();
125
126   if (menubar)
127     *menubar = factory->widget;
128
129   if (table)
130     *table = grp;
131 }
132
133 void
134 menus_init(void) {
135
136   if (initialize) {
137     initialize = FALSE;
138
139     factory = gtk_item_factory_new(GTK_TYPE_MENU_BAR, "<main>", grp);
140     gtk_item_factory_create_items_ac(factory, nmenu_items, menu_items, NULL,2);
141     set_menu_sensitivity("/File/Close", FALSE);
142     set_menu_sensitivity("/File/Save", FALSE);
143     set_menu_sensitivity("/File/Save As...", FALSE);
144     set_menu_sensitivity("/File/Reload", FALSE);
145     set_menu_sensitivity("/File/Print...", FALSE);
146     set_menu_sensitivity("/File/Print Packet", FALSE);
147     set_menu_sensitivity("/Edit/Cut", FALSE);
148     set_menu_sensitivity("/Edit/Copy", FALSE);
149     set_menu_sensitivity("/Edit/Paste", FALSE);
150     set_menu_sensitivity("/Edit/Find", FALSE);
151     set_menu_sensitivity("/Tools/Graph", FALSE);
152     set_menu_sensitivity("/Tools/Summary", FALSE);
153   }
154 }
155
156 void
157 set_menu_sensitivity (gchar *path, gint val) {
158   GtkWidget *menu;
159
160   if ((menu = gtk_item_factory_get_widget(factory, path)) != NULL)
161     gtk_widget_set_sensitive(menu, val);
162 }
163
164 void
165 set_menu_object_data (gchar *path, gchar *key, gpointer data) {
166   GtkWidget *menu;
167   
168   if ((menu = gtk_item_factory_get_widget(factory, path)) != NULL)
169     gtk_object_set_data(GTK_OBJECT(menu), key, data);
170 }
171
172