3 * $Id: proto_dlg.c,v 1.21 2002/12/01 22:51:56 gerald Exp $
5 * Laurent Deniel <deniel@worldnet.fr>
7 * Ethereal - Network traffic analyzer
8 * By Gerald Combs <gerald@ethereal.com>
9 * Copyright 2000 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.
38 #include "dlg_utils.h"
39 #include "proto_dlg.h"
40 #include "compat_macros.h"
42 static gboolean proto_delete_cb(GtkWidget *, gpointer);
43 static void proto_ok_cb(GtkWidget *, gpointer);
44 static void proto_apply_cb(GtkWidget *, gpointer);
45 static void proto_cancel_cb(GtkWidget *, gpointer);
46 static void proto_destroy_cb(GtkWidget *, gpointer);
48 #if GTK_MAJOR_VERSION < 2
49 static void show_proto_selection(GtkCList *proto_list);
51 static void show_proto_selection(GtkListStore *proto_store);
53 static gboolean set_proto_selection(GtkWidget *);
54 static gboolean revert_proto_selection(void);
56 static void toggle_all_cb(GtkWidget *button, gpointer parent_w);
57 static void enable_all_cb(GtkWidget *button, gpointer parent_w);
58 static void disable_all_cb(GtkWidget *button, gpointer parent_w);
59 #if GTK_MAJOR_VERSION < 2
60 static void proto_list_select_cb(GtkCList *proto_list, gint row, gint col,
61 GdkEventButton *ev, gpointer gp);
64 static GtkWidget *proto_w = NULL;
66 /* list of protocols */
67 static GSList *protocol_list = NULL;
69 typedef struct protocol_data {
75 #if GTK_MAJOR_VERSION < 2
82 #define DISABLED "Disabled"
83 #define STATUS_TXT(x) ((x) ? "" : DISABLED)
86 proto_cb(GtkWidget *w _U_, gpointer data _U_)
89 GtkWidget *main_vb, *bbox, *proto_list, *label, *proto_sw, *proto_frame,
91 gchar *titles[] = { "Status", "Protocol", "Description" };
93 #if GTK_MAJOR_VERSION >= 2
94 GtkListStore *proto_store;
95 GtkCellRenderer *proto_rend;
96 GtkTreeViewColumn *proto_col;
100 if (proto_w != NULL) {
101 reactivate_window(proto_w);
105 proto_w = dlg_window_new("Ethereal: Enabled Protocols");
106 SIGNAL_CONNECT(proto_w, "delete_event", proto_delete_cb, NULL);
107 SIGNAL_CONNECT(proto_w, "destroy", proto_destroy_cb, NULL);
108 WIDGET_SET_SIZE(proto_w, DEF_WIDTH * 2/3, DEF_HEIGHT * 2/3);
110 /* Container for each row of widgets */
112 main_vb = gtk_vbox_new(FALSE, 0);
113 gtk_container_border_width(GTK_CONTAINER(main_vb), 1);
114 gtk_container_add(GTK_CONTAINER(proto_w), main_vb);
115 gtk_widget_show(main_vb);
117 /* Protocol selection list ("enable/disable" protocols) */
119 proto_frame = gtk_frame_new("Enabled Protocols");
120 gtk_box_pack_start(GTK_BOX(main_vb), proto_frame, TRUE, TRUE, 0);
121 gtk_container_border_width(GTK_CONTAINER(proto_frame), 5);
122 gtk_widget_show(proto_frame);
126 proto_vb = gtk_vbox_new(FALSE, 0);
127 gtk_container_border_width(GTK_CONTAINER(proto_vb), 1);
128 gtk_container_add(GTK_CONTAINER(proto_frame), proto_vb);
129 gtk_container_border_width(GTK_CONTAINER(proto_vb), 5);
130 gtk_widget_show(proto_vb);
132 proto_sw = gtk_scrolled_window_new(NULL, NULL);
133 gtk_box_pack_start(GTK_BOX(proto_vb), proto_sw, TRUE, TRUE, 0);
134 gtk_widget_show(proto_sw);
136 #if GTK_MAJOR_VERSION < 2
137 proto_list = gtk_clist_new_with_titles(3, titles);
138 gtk_container_add(GTK_CONTAINER(proto_sw), proto_list);
139 gtk_clist_set_selection_mode(GTK_CLIST(proto_list), GTK_SELECTION_BROWSE);
140 gtk_clist_column_titles_passive(GTK_CLIST(proto_list));
141 gtk_clist_column_titles_show(GTK_CLIST(proto_list));
142 gtk_clist_set_column_auto_resize(GTK_CLIST(proto_list), 0, FALSE);
143 gtk_clist_set_column_auto_resize(GTK_CLIST(proto_list), 1, TRUE);
144 gtk_clist_set_column_auto_resize(GTK_CLIST(proto_list), 2, TRUE);
145 width = gdk_string_width(proto_list->style->font, DISABLED);
146 gtk_clist_set_column_width(GTK_CLIST(proto_list), 0, width);
147 SIGNAL_CONNECT(proto_list, "select-row", proto_list_select_cb, NULL);
148 show_proto_selection(GTK_CLIST(proto_list));
149 gtk_widget_show(proto_list);
151 proto_store = gtk_list_store_new(3, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
152 show_proto_selection(proto_store);
153 proto_list = tree_view_new(GTK_TREE_MODEL(proto_store));
154 gtk_tree_view_set_search_column(GTK_TREE_VIEW(proto_list), 0);
155 g_object_unref(G_OBJECT(proto_store));
156 gtk_container_add(GTK_CONTAINER(proto_sw), proto_list);
157 proto_rend = gtk_cell_renderer_text_new();
158 proto_col = gtk_tree_view_column_new_with_attributes(titles[0], proto_rend,
160 gtk_tree_view_column_set_sort_column_id(proto_col, 0);
161 gtk_tree_view_append_column(GTK_TREE_VIEW(proto_list), proto_col);
162 proto_rend = gtk_cell_renderer_text_new();
163 proto_col = gtk_tree_view_column_new_with_attributes(titles[1], proto_rend,
165 gtk_tree_view_column_set_sort_column_id(proto_col, 1);
166 gtk_tree_view_append_column(GTK_TREE_VIEW(proto_list), proto_col);
167 proto_rend = gtk_cell_renderer_text_new();
168 proto_col = gtk_tree_view_column_new_with_attributes(titles[2], proto_rend,
170 gtk_tree_view_column_set_sort_column_id(proto_col, 2);
171 gtk_tree_view_append_column(GTK_TREE_VIEW(proto_list), proto_col);
175 label = gtk_label_new("Disabling a protocol prevents higher "
176 "layer protocols from being displayed");
177 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
178 gtk_widget_show(label);
179 gtk_box_pack_start(GTK_BOX(proto_vb), label, FALSE, FALSE, 5);
182 bbox = gtk_hbutton_box_new();
183 gtk_button_box_set_layout(GTK_BUTTON_BOX(bbox), GTK_BUTTONBOX_END);
184 gtk_button_box_set_spacing(GTK_BUTTON_BOX(bbox), 5);
185 gtk_box_pack_start(GTK_BOX(proto_vb), bbox, FALSE, FALSE, 0);
186 gtk_widget_show(bbox);
189 button = gtk_button_new_with_label("Enable All");
190 SIGNAL_CONNECT(button, "clicked", enable_all_cb, proto_list);
191 gtk_box_pack_start(GTK_BOX(bbox), button, TRUE, TRUE, 0);
192 gtk_widget_show(button);
195 button = gtk_button_new_with_label("Disable All");
196 SIGNAL_CONNECT(button, "clicked", disable_all_cb, proto_list);
197 gtk_box_pack_start(GTK_BOX(bbox), button, TRUE, TRUE, 0);
198 gtk_widget_show(button);
201 button = gtk_button_new_with_label("Invert");
202 SIGNAL_CONNECT(button, "clicked", toggle_all_cb, proto_list);
203 gtk_box_pack_start(GTK_BOX(bbox), button, TRUE, TRUE, 0);
204 gtk_widget_show(button);
207 /* Ok, Apply, Cancel Buttons */
209 bbox = gtk_hbutton_box_new();
210 gtk_button_box_set_layout(GTK_BUTTON_BOX(bbox), GTK_BUTTONBOX_END);
211 gtk_button_box_set_spacing(GTK_BUTTON_BOX(bbox), 5);
212 gtk_box_pack_start(GTK_BOX(main_vb), bbox, FALSE, FALSE, 0);
213 gtk_widget_show(bbox);
215 #if GTK_MAJOR_VERSION < 2
216 button = gtk_button_new_with_label ("OK");
218 button = gtk_button_new_from_stock(GTK_STOCK_OK);
220 SIGNAL_CONNECT(button, "clicked", proto_ok_cb, proto_w);
221 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
222 gtk_box_pack_start(GTK_BOX (bbox), button, TRUE, TRUE, 0);
223 gtk_widget_grab_default(button);
224 gtk_widget_show(button);
226 #if GTK_MAJOR_VERSION < 2
227 button = gtk_button_new_with_label ("Apply");
229 button = gtk_button_new_from_stock(GTK_STOCK_APPLY);
231 SIGNAL_CONNECT(button, "clicked", proto_apply_cb, proto_w);
232 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
233 gtk_box_pack_start(GTK_BOX (bbox), button, TRUE, TRUE, 0);
234 gtk_widget_show(button);
236 #if GTK_MAJOR_VERSION < 2
237 button = gtk_button_new_with_label ("Cancel");
239 button = gtk_button_new_from_stock(GTK_STOCK_CANCEL);
241 SIGNAL_CONNECT(button, "clicked", proto_cancel_cb, proto_w);
242 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
243 gtk_box_pack_start(GTK_BOX (bbox), button, TRUE, TRUE, 0);
244 gtk_widget_show(button);
246 dlg_set_cancel(proto_w, button);
248 gtk_quit_add_destroy(gtk_main_level(), GTK_OBJECT(proto_w));
249 gtk_widget_show(proto_w);
253 #if GTK_MAJOR_VERSION < 2
255 proto_list_select_cb(GtkCList *proto_list, gint row, gint col,
256 GdkEventButton *ev, gpointer gp _U_) {
257 protocol_data_t *p = gtk_clist_get_row_data(proto_list, row);
259 if (row < 0 || col < 0)
267 gtk_clist_set_text(proto_list, row, 0, STATUS_TXT(p->enabled) );
268 } /* proto_list_select_cb */
271 /* XXX - We need a callback for Gtk2 */
276 toggle_all_cb(GtkWidget *button _U_, gpointer pl)
280 #if GTK_MAJOR_VERSION < 2
281 GtkCList *proto_list = GTK_CLIST(pl);
284 for (entry = protocol_list; entry != NULL; entry = g_slist_next(entry)) {
285 protocol_data_t *p = entry->data;
292 #if GTK_MAJOR_VERSION < 2
293 gtk_clist_set_text(proto_list, p->row, 0, STATUS_TXT(p->enabled) );
298 /* Enable/Disable All Helper */
300 set_active_all(GtkWidget *w, gboolean new_state)
303 #if GTK_MAJOR_VERSION < 2
304 GtkCList *proto_list = GTK_CLIST(w);
308 #if GTK_MAJOR_VERSION < 2
309 gtk_clist_freeze(proto_list);
311 for (entry = protocol_list; entry != NULL; entry = g_slist_next(entry)) {
312 protocol_data_t *p = entry->data;
314 p->enabled = new_state;
315 #if GTK_MAJOR_VERSION < 2
316 gtk_clist_set_text(proto_list, p->row, 0, STATUS_TXT(new_state) );
319 #if GTK_MAJOR_VERSION < 2
320 gtk_clist_thaw(proto_list);
326 enable_all_cb(GtkWidget *button _U_, gpointer pl)
328 set_active_all(pl, TRUE);
333 disable_all_cb(GtkWidget *button _U_, gpointer pl)
335 set_active_all(pl, FALSE);
339 proto_destroy_cb(GtkWidget *w _U_, gpointer data _U_)
344 gtk_widget_destroy(proto_w);
347 /* remove protocol list */
349 for (entry = protocol_list; entry != NULL; entry = g_slist_next(entry)) {
352 g_slist_free(protocol_list);
353 protocol_list = NULL;
357 /* Treat this as a cancel, by calling "proto_cancel_cb()".
358 XXX - that'll destroy the Protocols dialog; will that upset
359 a higher-level handler that says "OK, we've been asked to delete
360 this, so destroy it"? */
362 proto_delete_cb(GtkWidget *proto_w, gpointer dummy _U_)
364 proto_cancel_cb(NULL, proto_w);
369 proto_ok_cb(GtkWidget *ok_bt _U_, gpointer parent_w)
373 redissect = set_proto_selection(GTK_WIDGET(parent_w));
374 gtk_widget_destroy(GTK_WIDGET(parent_w));
376 redissect_packets(&cfile);
380 proto_apply_cb(GtkWidget *apply_bt _U_, gpointer parent_w)
382 if (set_proto_selection(GTK_WIDGET(parent_w)))
383 redissect_packets(&cfile);
387 proto_cancel_cb(GtkWidget *cancel_bt _U_, gpointer parent_w)
391 redissect = revert_proto_selection();
392 gtk_widget_destroy(GTK_WIDGET(parent_w));
394 redissect_packets(&cfile);
398 set_proto_selection(GtkWidget *parent_w _U_)
401 gboolean need_redissect = FALSE;
403 for (entry = protocol_list; entry != NULL; entry = g_slist_next(entry)) {
404 protocol_data_t *p = entry->data;
406 if (proto_is_protocol_enabled(p->hfinfo_index) != p->enabled) {
407 proto_set_decoding(p->hfinfo_index, p->enabled);
408 need_redissect = TRUE;
412 return need_redissect;
414 } /* set_proto_selection */
417 revert_proto_selection(void)
420 gboolean need_redissect = FALSE;
423 * Undo all the changes we've made to protocol enable flags.
425 for (entry = protocol_list; entry != NULL; entry = g_slist_next(entry)) {
426 protocol_data_t *p = entry->data;
428 if (proto_is_protocol_enabled(p->hfinfo_index) != p->was_enabled) {
429 proto_set_decoding(p->hfinfo_index, p->was_enabled);
430 need_redissect = TRUE;
434 return need_redissect;
436 } /* revert_proto_selection */
439 protocol_data_compare(gconstpointer a, gconstpointer b)
441 const protocol_data_t *ap = (const protocol_data_t *)a;
442 const protocol_data_t *bp = (const protocol_data_t *)b;
444 return strcmp(ap->abbrev, bp->abbrev);
448 #if GTK_MAJOR_VERSION < 2
449 show_proto_selection(GtkCList *proto_list)
451 show_proto_selection(GtkListStore *proto_store)
458 #if GTK_MAJOR_VERSION < 2
459 gchar *proto_text[3];
461 GtkTreeIter proto_iter;
464 /* Iterate over all the protocols */
466 for (i = proto_get_first_protocol(&cookie); i != -1;
467 i = proto_get_next_protocol(&cookie)) {
468 if (proto_can_disable_protocol(i)) {
469 p = g_malloc(sizeof(protocol_data_t));
470 p->name = proto_get_protocol_name(i);
471 p->abbrev = proto_get_protocol_short_name(i);
473 p->enabled = proto_is_protocol_enabled(i);
474 p->was_enabled = p->enabled;
475 protocol_list = g_slist_insert_sorted(protocol_list,
476 p, protocol_data_compare);
480 for (entry = protocol_list; entry != NULL; entry = g_slist_next(entry)) {
483 #if GTK_MAJOR_VERSION < 2
484 /* XXX - The preferred way to do this would be to have a check box
485 * in the first column. GtkClists don't let us put arbitrary widgets
486 * in a cell, so we use the word "Disabled" instead. We should be
487 * able to use check boxes in Gtk2, however.
489 proto_text[0] = STATUS_TXT (p->enabled);
490 proto_text[1] = p->abbrev;
491 proto_text[2] = p->name;
492 p->row = gtk_clist_append(proto_list, proto_text);
493 gtk_clist_set_row_data(proto_list, p->row, p);
495 gtk_list_store_append(proto_store, &proto_iter);
496 gtk_list_store_set(proto_store, &proto_iter,
497 0, STATUS_TXT (p->enabled),
504 } /* show_proto_selection */