3 * Copyright 2003, Ulf Lamping <ulf.lamping@web.de>
5 * $Id: toolbar.c,v 1.28 2004/02/09 18:32:41 ulfl Exp $
7 * Ethereal - Network traffic analyzer
8 * By Gerald Combs <gerald@ethereal.com>
9 * Copyright 1998 Gerald Combs
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.
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.
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.
27 * This file implements a "main" toolbar for Ethereal (suitable for gtk1 and
30 * As it is desirable to have the same toolbar implementation for gtk1 and gtk2
31 * in Ethereal, only those library calls available in the gtk1 libraries
32 * are used inside this file.
34 * Hint: gtk2 in comparison to gtk1 has a better way to handle with "common"
35 * icons; gtk2 calls this kind of icons "stock-icons"
36 * (stock-icons including: icons for "open", "save", "print", ...).
37 * The gtk2 version of this code uses them.
47 #include "capture_dlg.h"
48 #endif /* HAVE_LIBPCAP */
49 #include "filter_prefs.h"
54 #include "color_dlg.h"
56 #include "prefs_dlg.h"
59 #include "gtkglobals.h"
62 #include "compat_macros.h"
65 /* All of the icons used here are coming (or are derived) from GTK2 stock icons.
66 * They were converted using "The Gimp" with standard conversion from png to xpm.
67 * All stock icons can be (currently) found at:
68 * "ftp://ftp.gtk.org/pub/gtk/v2.0/gtk+-2.0.6.tar.bz2"
69 * in the directory "gtk+-2.0.6\gtk\stock-icons" */
70 #if GTK_MAJOR_VERSION < 2
72 #include "../image/toolbar/stock_stop_24.xpm"
73 #endif /* HAVE_LIBPCAP */
74 #include "../image/toolbar/stock_save_as_24.xpm"
75 #include "../image/toolbar/stock_close_24.xpm"
76 #include "../image/toolbar/stock_refresh_24.xpm"
77 #include "../image/toolbar/stock_print_24.xpm"
78 #include "../image/toolbar/stock_search_24.xpm"
79 #include "../image/toolbar/stock_left_arrow_24.xpm"
80 #include "../image/toolbar/stock_right_arrow_24.xpm"
81 #include "../image/toolbar/stock_jump_to_24.xpm"
82 #include "../image/toolbar/stock_top_24.xpm"
83 #include "../image/toolbar/stock_bottom_24.xpm"
84 #include "../image/toolbar/stock_zoom_in_24.xpm"
85 #include "../image/toolbar/stock_zoom_out_24.xpm"
86 #include "../image/toolbar/stock_zoom_1_24.xpm"
87 #include "../image/toolbar/stock_colorselector_24.xpm"
88 #include "../image/toolbar/stock_help_24.xpm"
89 #endif /* GTK_MAJOR_VERSION */
91 /* these icons are derived from the original stock icons */
93 #include "../image/toolbar/capture_24.xpm"
94 #include "../image/toolbar/cfilter_24.xpm"
95 #endif /* HAVE_LIBPCAP */
96 #include "../image/toolbar/dfilter_24.xpm"
97 /* these icons are standard stock icons, but used for ethereal specific stock icon labels */
98 #if GTK_MAJOR_VERSION >= 2
99 #include "../image/toolbar/stock_add_24.xpm"
101 #include "../image/toolbar/stock_open_24.xpm"
102 #if GTK_MAJOR_VERSION >= 2
103 #include "../image/toolbar/stock_ok_20.xpm"
105 #include "../image/toolbar/stock_save_24.xpm"
106 #include "../image/toolbar/stock_preferences_24.xpm"
107 #if GTK_MAJOR_VERSION >= 2
108 #include "../image/toolbar/stock_properties_24.xpm"
112 /* XXX: add this key to some .h file, as it adds a key to the top level Widget? */
113 #define E_TB_MAIN_KEY "toolbar_main"
116 static gboolean toolbar_init = FALSE;
119 static GtkWidget *new_button, *stop_button;
120 static GtkWidget *capture_filter_button;
121 #endif /* HAVE_LIBPCAP */
122 static GtkWidget *open_button, *save_button, *save_as_button, *close_button, *reload_button;
123 static GtkWidget *print_button, *find_button, *find_next_button, *find_prev_button;
124 static GtkWidget *go_to_button, *go_to_top_button, *go_to_bottom_button;
125 static GtkWidget *display_filter_button;
126 static GtkWidget *zoom_in_button, *zoom_out_button, *zoom_100_button;
127 static GtkWidget *color_display_button, *prefs_button, *help_button;
129 #if GTK_MAJOR_VERSION >= 2
130 typedef struct stock_pixmap_tag{
135 /* generate application specific stock items */
136 void ethereal_stock_icons(void) {
137 GtkIconFactory * factory;
141 /* register non-standard pixmaps with the gtk-stock engine */
142 static const GtkStockItem stock_items[] = {
144 { ETHEREAL_STOCK_CAPTURE_START, ETHEREAL_STOCK_LABEL_CAPTURE_START, 0, 0, NULL },
145 { ETHEREAL_STOCK_CAPTURE_FILTER, ETHEREAL_STOCK_LABEL_CAPTURE_FILTER, 0, 0, NULL },
146 { ETHEREAL_STOCK_CAPTURE_FILTER_ENTRY, ETHEREAL_STOCK_LABEL_CAPTURE_FILTER_ENTRY, 0, 0, NULL },
148 { ETHEREAL_STOCK_DISPLAY_FILTER, ETHEREAL_STOCK_LABEL_DISPLAY_FILTER, 0, 0, NULL },
149 { ETHEREAL_STOCK_DISPLAY_FILTER_ENTRY, ETHEREAL_STOCK_LABEL_DISPLAY_FILTER_ENTRY, 0, 0, NULL },
150 { ETHEREAL_STOCK_PREFS, ETHEREAL_STOCK_LABEL_PREFS, 0, 0, NULL },
151 { ETHEREAL_STOCK_BROWSE, ETHEREAL_STOCK_LABEL_BROWSE, 0, 0, NULL },
152 { ETHEREAL_STOCK_CREATE_STAT, ETHEREAL_STOCK_LABEL_CREATE_STAT, 0, 0, NULL },
153 { ETHEREAL_STOCK_EXPORT, ETHEREAL_STOCK_LABEL_EXPORT, 0, 0, NULL },
154 { ETHEREAL_STOCK_IMPORT, ETHEREAL_STOCK_LABEL_IMPORT, 0, 0, NULL },
155 { ETHEREAL_STOCK_EDIT, ETHEREAL_STOCK_LABEL_EDIT, 0, 0, NULL },
156 { ETHEREAL_STOCK_ADD_EXPRESSION, ETHEREAL_STOCK_LABEL_ADD_EXPRESSION, 0, 0, NULL },
159 static const stock_pixmap_t pixmaps[] = {
161 { ETHEREAL_STOCK_CAPTURE_START, capture_24_xpm },
162 { ETHEREAL_STOCK_CAPTURE_FILTER, cfilter_24_xpm },
163 { ETHEREAL_STOCK_CAPTURE_FILTER_ENTRY, cfilter_24_xpm },
165 { ETHEREAL_STOCK_DISPLAY_FILTER, dfilter_24_xpm },
166 { ETHEREAL_STOCK_DISPLAY_FILTER_ENTRY, dfilter_24_xpm },
167 { ETHEREAL_STOCK_PREFS, stock_preferences_24_xpm },
168 { ETHEREAL_STOCK_BROWSE, stock_open_24_xpm },
169 { ETHEREAL_STOCK_CREATE_STAT, stock_ok_20_xpm },
170 { ETHEREAL_STOCK_EXPORT, stock_save_24_xpm }, /* XXX: needs a better icon */
171 { ETHEREAL_STOCK_IMPORT, stock_save_24_xpm }, /* XXX: needs a better icon */
172 { ETHEREAL_STOCK_EDIT, stock_properties_24_xpm },
173 { ETHEREAL_STOCK_ADD_EXPRESSION, stock_add_24_xpm },
177 /* Register our stock items */
178 gtk_stock_add (stock_items, G_N_ELEMENTS (stock_items));
180 /* Add our custom icon factory to the list of defaults */
181 factory = gtk_icon_factory_new();
182 gtk_icon_factory_add_default(factory);
184 /* Create the stock items to add into our icon factory */
185 for (i = 0; pixmaps[i].name != NULL; i++) {
187 GtkIconSet *icon_set;
189 pixbuf = gdk_pixbuf_new_from_xpm_data((const char **) (pixmaps[i].xpm_data));
191 icon_set = gtk_icon_set_new_from_pixbuf (pixbuf);
192 gtk_icon_factory_add (factory, pixmaps[i].name, icon_set);
193 gtk_icon_set_unref (icon_set);
194 g_object_unref (G_OBJECT (pixbuf));
197 /* Drop our reference to the factory, GTK will hold a reference.*/
198 g_object_unref (G_OBJECT (factory));
204 * Redraw all toolbars (currently only the main toolbar)
207 toolbar_redraw_all(void)
211 main_tb = OBJECT_GET_DATA(top_level, E_TB_MAIN_KEY);
213 gtk_toolbar_set_style(GTK_TOOLBAR(main_tb),
214 prefs.gui_toolbar_main_style);
216 #if GTK_MAJOR_VERSION < 2
217 /* In GTK+ 1.2[.x], the toolbar takes the maximum vertical size it ever
218 * had, even if you change the style in such a way as to reduce its
219 * height, unless we queue a resize (which resizes ALL elements in the
220 * top-level container).
222 * In GTK+ 2.x, this isn't necessary - it does the right thing. */
223 gtk_container_queue_resize(GTK_CONTAINER(top_level));
224 #endif /* GTK_MAJOR_VERSION */
227 /* Enable or disable toolbar items based on whether you have a capture file
228 you've finished reading. */
229 void set_toolbar_for_capture_file(gboolean have_capture_file) {
231 gtk_widget_set_sensitive(save_button, have_capture_file);
232 gtk_widget_set_sensitive(save_as_button, have_capture_file);
233 gtk_widget_set_sensitive(close_button, have_capture_file);
234 gtk_widget_set_sensitive(reload_button, have_capture_file);
238 /* Enable or disable menu items based on whether you have an unsaved
239 capture file you've finished reading. */
240 void set_toolbar_for_unsaved_capture_file(gboolean have_unsaved_capture_file) {
242 if(have_unsaved_capture_file) {
243 gtk_widget_hide(save_as_button);
244 gtk_widget_show(save_button);
246 gtk_widget_show(save_as_button);
247 gtk_widget_hide(save_button);
249 /*gtk_widget_set_sensitive(save_button, have_unsaved_capture_file);
250 gtk_widget_set_sensitive(save_as_button, !have_unsaved_capture_file);*/
254 /* set toolbar state "have a capture in progress" */
255 void set_toolbar_for_capture_in_progress(gboolean capture_in_progress) {
259 gtk_widget_set_sensitive(new_button, !capture_in_progress);
260 if (capture_in_progress) {
261 gtk_widget_hide(new_button);
262 gtk_widget_show(stop_button);
264 gtk_widget_show(new_button);
265 gtk_widget_hide(stop_button);
267 #endif /* HAVE_LIBPCAP */
268 gtk_widget_set_sensitive(open_button, !capture_in_progress);
272 /* set toolbar state "have packets captured" */
273 void set_toolbar_for_captured_packets(gboolean have_captured_packets) {
276 gtk_widget_set_sensitive(print_button, have_captured_packets);
277 gtk_widget_set_sensitive(find_button, have_captured_packets);
278 gtk_widget_set_sensitive(find_next_button, have_captured_packets);
279 gtk_widget_set_sensitive(find_prev_button, have_captured_packets);
280 gtk_widget_set_sensitive(go_to_button, have_captured_packets);
281 gtk_widget_set_sensitive(go_to_top_button, have_captured_packets);
282 gtk_widget_set_sensitive(go_to_bottom_button, have_captured_packets);
283 gtk_widget_set_sensitive(zoom_in_button, have_captured_packets);
284 gtk_widget_set_sensitive(zoom_out_button, have_captured_packets);
285 gtk_widget_set_sensitive(zoom_100_button, have_captured_packets);
286 /* XXX - I don't see a reason why this should be done (as it is in the
288 /* gtk_widget_set_sensitive(color_display_button, have_captured_packets);*/
293 /* helper function: add a separator to the toolbar */
294 static void toolbar_append_separator(GtkWidget *toolbar) {
295 #if GTK_MAJOR_VERSION < 2
296 /* XXX - the usage of a gtk_separator doesn't seem to work for a toolbar.
297 * (at least in the win32 port of gtk 1.3)
298 * So simply add a few spaces */
299 gtk_toolbar_append_space(GTK_TOOLBAR(toolbar)); /* space after item */
300 gtk_toolbar_append_space(GTK_TOOLBAR(toolbar)); /* space after item */
302 /* GTK 2 uses (as it should be) a seperator when adding this space */
303 gtk_toolbar_append_space(GTK_TOOLBAR(toolbar));
304 #endif /* GTK_MAJOR_VERSION */
309 #if GTK_MAJOR_VERSION < 2
310 #define toolbar_item(new_item, window, toolbar, stock, tooltip, xpm, callback) { \
311 icon = gdk_pixmap_create_from_xpm_d(window->window, &mask, &window->style->white, xpm); \
312 iconw = gtk_pixmap_new(icon, mask); \
313 new_item = gtk_toolbar_append_item(GTK_TOOLBAR (toolbar), \
314 stock, tooltip, "Private", iconw, GTK_SIGNAL_FUNC(callback), NULL);\
317 #define toolbar_item(new_item, window, toolbar, stock, tooltip, xpm, callback) { \
318 new_item = gtk_toolbar_insert_stock(GTK_TOOLBAR(toolbar), \
319 stock, tooltip, "Private", G_CALLBACK(callback), NULL, -1);\
321 #endif /* GTK_MAJOR_VERSION */
325 * Create all toolbars (currently only the main toolbar)
331 GtkWidget *window = top_level;
332 #if GTK_MAJOR_VERSION < 2
336 #endif /* GTK_MAJOR_VERSION */
339 #if GTK_MAJOR_VERSION >= 2
340 /* create application specific stock icons */
341 ethereal_stock_icons();
344 /* this function should be only called once! */
345 g_assert(!toolbar_init);
347 /* we need to realize the window because we use pixmaps for
348 * items on the toolbar in the context of it */
349 /* (coming from the gtk example, please don't ask me why ;-) */
350 gtk_widget_realize(window);
352 /* toolbar will be horizontal, with both icons and text (as default here) */
353 /* (this will usually be overwritten by the preferences setting) */
354 #if GTK_MAJOR_VERSION < 2
355 main_tb = gtk_toolbar_new(GTK_ORIENTATION_HORIZONTAL,
357 gtk_toolbar_set_space_size(GTK_TOOLBAR(main_tb), 3);
358 /* the "space lines" in GTK1 looks ugly, so don't use them */
359 /* gtk_toolbar_set_space_style(GTK_TOOLBAR(main_tb), GTK_TOOLBAR_SPACE_LINE); */
361 main_tb = gtk_toolbar_new();
362 gtk_toolbar_set_orientation(GTK_TOOLBAR(main_tb),
363 GTK_ORIENTATION_HORIZONTAL);
364 #endif /* GTK_MAJOR_VERSION */
366 OBJECT_SET_DATA(top_level, E_TB_MAIN_KEY, main_tb);
370 /* either start OR stop button can be valid at a time, so no space
371 * between them is needed here (stop button is hidden by default) */
373 toolbar_item(new_button, window, main_tb,
374 ETHEREAL_STOCK_CAPTURE_START, "Start a new live capture...", capture_24_xpm, capture_prep_cb);
375 toolbar_item(stop_button, window, main_tb,
376 GTK_STOCK_STOP, "Stop the running live capture", stock_stop_24_xpm, capture_stop_cb);
377 toolbar_append_separator(main_tb);
378 #endif /* HAVE_LIBPCAP */
380 toolbar_item(open_button, window, main_tb,
381 GTK_STOCK_OPEN, "Open a capture file...", stock_open_24_xpm, file_open_cmd_cb);
382 toolbar_item(save_button, window, main_tb,
383 GTK_STOCK_SAVE, "Save this capture file...", stock_save_24_xpm, file_save_cmd_cb);
384 toolbar_item(save_as_button, window, main_tb,
385 GTK_STOCK_SAVE_AS, "Save this capture file as...", stock_save_as_24_xpm, file_save_as_cmd_cb);
386 toolbar_item(close_button, window, main_tb,
387 GTK_STOCK_CLOSE, "Close this capture file", stock_close_24_xpm, file_close_cmd_cb);
388 toolbar_item(reload_button, window, main_tb,
389 GTK_STOCK_REFRESH, "Reload this capture file", stock_refresh_24_xpm, file_reload_cmd_cb);
390 toolbar_item(print_button, window, main_tb,
391 GTK_STOCK_PRINT, "Print packet(s)...", stock_print_24_xpm, file_print_cmd_cb);
392 toolbar_append_separator(main_tb);
394 toolbar_item(find_button, window, main_tb,
395 GTK_STOCK_FIND, "Find a packet...", stock_search_24_xpm, find_frame_cb);
396 toolbar_item(find_prev_button, window, main_tb,
397 GTK_STOCK_GO_BACK, "Find the previous matching packet", stock_left_arrow_24_xpm, find_previous_cb);
398 toolbar_item(find_next_button, window, main_tb,
399 GTK_STOCK_GO_FORWARD, "Find the next matching packet", stock_right_arrow_24_xpm, find_next_cb);
400 toolbar_append_separator(main_tb);
402 toolbar_item(go_to_button, window, main_tb,
403 GTK_STOCK_JUMP_TO, "Go to the packet with number...", stock_jump_to_24_xpm, goto_frame_cb);
404 toolbar_item(go_to_top_button, window, main_tb,
405 GTK_STOCK_GOTO_TOP, "Go to the first packet", stock_top_24_xpm, goto_top_frame_cb);
406 toolbar_item(go_to_bottom_button, window, main_tb,
407 GTK_STOCK_GOTO_BOTTOM, "Go to the last packet", stock_bottom_24_xpm, goto_bottom_frame_cb);
408 toolbar_append_separator(main_tb);
410 toolbar_item(zoom_in_button, window, main_tb,
411 GTK_STOCK_ZOOM_IN, "Zoom in", stock_zoom_in_24_xpm, view_zoom_in_cb);
412 toolbar_item(zoom_out_button, window, main_tb,
413 GTK_STOCK_ZOOM_OUT, "Zoom out", stock_zoom_out_24_xpm, view_zoom_out_cb);
414 toolbar_item(zoom_100_button, window, main_tb,
415 GTK_STOCK_ZOOM_100, "Zoom 100%", stock_zoom_1_24_xpm, view_zoom_100_cb);
416 toolbar_append_separator(main_tb);
419 toolbar_item(capture_filter_button, window, main_tb,
420 ETHEREAL_STOCK_CAPTURE_FILTER, "Edit capture filter...", cfilter_24_xpm, cfilter_dialog_cb);
421 #endif /* HAVE_LIBPCAP */
422 toolbar_item(display_filter_button, window, main_tb,
423 ETHEREAL_STOCK_DISPLAY_FILTER, "Edit/apply display filter...", dfilter_24_xpm, dfilter_dialog_cb);
424 toolbar_item(color_display_button, window, main_tb,
425 GTK_STOCK_SELECT_COLOR, "Edit coloring rules...", stock_colorselector_24_xpm, color_display_cb);
426 /* the preference button uses it's own Stock icon label "Prefs", as "Preferences" is too long */
427 toolbar_item(prefs_button, window, main_tb,
428 ETHEREAL_STOCK_PREFS, "Edit preferences...", stock_preferences_24_xpm, prefs_cb);
429 toolbar_append_separator(main_tb);
431 toolbar_item(help_button, window, main_tb,
432 GTK_STOCK_HELP, "Show some help...", stock_help_24_xpm, help_cb);
434 /* disable all "sensitive" items by default */
436 set_toolbar_for_unsaved_capture_file(FALSE);
437 set_toolbar_for_captured_packets(FALSE);
438 set_toolbar_for_capture_file(FALSE);
440 set_toolbar_for_capture_in_progress(FALSE);
441 #endif /* HAVE_LIBPCAP */
443 /* make current preferences effective */
444 toolbar_redraw_all();
450 set_toolbar_object_data(gchar *key, gpointer data)
452 OBJECT_SET_DATA(open_button, key, data);
453 OBJECT_SET_DATA(reload_button, key, data);