3 * $Id: proto_dlg.c,v 1.25 2003/01/26 19:35:31 deniel Exp $
5 * Laurent Deniel <laurent.deniel@free.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.
31 #include <gdk/gdkkeysyms.h>
39 #include "dlg_utils.h"
40 #include "proto_dlg.h"
41 #include "compat_macros.h"
43 static gboolean proto_delete_cb(GtkWidget *, gpointer);
44 static void proto_ok_cb(GtkWidget *, gpointer);
45 static void proto_apply_cb(GtkWidget *, gpointer);
46 static void proto_cancel_cb(GtkWidget *, gpointer);
47 static void proto_destroy_cb(GtkWidget *, gpointer);
49 #if GTK_MAJOR_VERSION < 2
50 static void show_proto_selection(GtkCList *proto_list);
52 static void show_proto_selection(GtkListStore *proto_store);
54 static gboolean set_proto_selection(GtkWidget *);
55 static gboolean revert_proto_selection(void);
57 static void toggle_all_cb(GtkWidget *button, gpointer parent_w);
58 static void enable_all_cb(GtkWidget *button, gpointer parent_w);
59 static void disable_all_cb(GtkWidget *button, gpointer parent_w);
60 #if GTK_MAJOR_VERSION < 2
61 static void proto_list_select_cb(GtkCList *proto_list, gint row, gint col,
62 GdkEventButton *ev, gpointer gp);
63 static gboolean proto_list_keypress_cb(GtkWidget *pl, GdkEventKey *ev,
66 static void status_toggled(GtkCellRendererToggle *, gchar *, gpointer);
69 static GtkWidget *proto_w = NULL;
71 /* list of protocols */
72 static GSList *protocol_list = NULL;
74 typedef struct protocol_data {
80 #if GTK_MAJOR_VERSION < 2
87 #define DISABLED "Disabled"
88 #define STATUS_TXT(x) ((x) ? "" : DISABLED)
91 proto_cb(GtkWidget *w _U_, gpointer data _U_)
94 GtkWidget *main_vb, *bbox, *proto_list, *label, *proto_sw, *proto_frame,
96 gchar *titles[] = { "Status", "Protocol", "Description" };
97 #if GTK_MAJOR_VERSION < 2
100 GtkListStore *proto_store;
101 GtkCellRenderer *proto_rend;
102 GtkTreeViewColumn *proto_col;
106 if (proto_w != NULL) {
107 reactivate_window(proto_w);
111 proto_w = dlg_window_new("Ethereal: Enabled Protocols");
112 SIGNAL_CONNECT(proto_w, "delete_event", proto_delete_cb, NULL);
113 SIGNAL_CONNECT(proto_w, "destroy", proto_destroy_cb, NULL);
114 WIDGET_SET_SIZE(proto_w, DEF_WIDTH * 2/3, DEF_HEIGHT * 2/3);
116 /* Container for each row of widgets */
118 main_vb = gtk_vbox_new(FALSE, 0);
119 gtk_container_border_width(GTK_CONTAINER(main_vb), 1);
120 gtk_container_add(GTK_CONTAINER(proto_w), main_vb);
121 gtk_widget_show(main_vb);
123 /* Protocol selection list ("enable/disable" protocols) */
125 proto_frame = gtk_frame_new("Enabled Protocols");
126 gtk_box_pack_start(GTK_BOX(main_vb), proto_frame, TRUE, TRUE, 0);
127 gtk_container_border_width(GTK_CONTAINER(proto_frame), 5);
128 gtk_widget_show(proto_frame);
132 proto_vb = gtk_vbox_new(FALSE, 0);
133 gtk_container_border_width(GTK_CONTAINER(proto_vb), 1);
134 gtk_container_add(GTK_CONTAINER(proto_frame), proto_vb);
135 gtk_container_border_width(GTK_CONTAINER(proto_vb), 5);
136 gtk_widget_show(proto_vb);
138 proto_sw = gtk_scrolled_window_new(NULL, NULL);
139 gtk_box_pack_start(GTK_BOX(proto_vb), proto_sw, TRUE, TRUE, 0);
140 gtk_widget_show(proto_sw);
142 #if GTK_MAJOR_VERSION < 2
143 proto_list = gtk_clist_new_with_titles(3, titles);
144 gtk_container_add(GTK_CONTAINER(proto_sw), proto_list);
145 gtk_clist_set_selection_mode(GTK_CLIST(proto_list), GTK_SELECTION_BROWSE);
146 gtk_clist_column_titles_passive(GTK_CLIST(proto_list));
147 gtk_clist_column_titles_show(GTK_CLIST(proto_list));
148 gtk_clist_set_column_auto_resize(GTK_CLIST(proto_list), 0, FALSE);
149 gtk_clist_set_column_auto_resize(GTK_CLIST(proto_list), 1, TRUE);
150 gtk_clist_set_column_auto_resize(GTK_CLIST(proto_list), 2, TRUE);
151 width = gdk_string_width(proto_list->style->font, DISABLED);
152 gtk_clist_set_column_width(GTK_CLIST(proto_list), 0, width);
153 SIGNAL_CONNECT(proto_list, "select-row", proto_list_select_cb, NULL);
154 SIGNAL_CONNECT(proto_list, "key-press-event", proto_list_keypress_cb, NULL);
155 show_proto_selection(GTK_CLIST(proto_list));
157 proto_store = gtk_list_store_new(4, G_TYPE_BOOLEAN, G_TYPE_STRING,
158 G_TYPE_STRING, G_TYPE_POINTER);
159 show_proto_selection(proto_store);
160 /* default sort on "abbrev" column */
161 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(proto_store), 1,
163 proto_list = tree_view_new(GTK_TREE_MODEL(proto_store));
164 gtk_container_add(GTK_CONTAINER(proto_sw), proto_list);
165 proto_rend = gtk_cell_renderer_toggle_new();
166 SIGNAL_CONNECT(proto_rend, "toggled", status_toggled, proto_store);
167 proto_col = gtk_tree_view_column_new_with_attributes(titles[0], proto_rend,
169 gtk_tree_view_column_set_sort_column_id(proto_col, 0);
170 gtk_tree_view_append_column(GTK_TREE_VIEW(proto_list), proto_col);
171 proto_rend = gtk_cell_renderer_text_new();
172 proto_col = gtk_tree_view_column_new_with_attributes(titles[1], proto_rend,
174 gtk_tree_view_column_set_sort_column_id(proto_col, 1);
175 gtk_tree_view_append_column(GTK_TREE_VIEW(proto_list), proto_col);
176 proto_rend = gtk_cell_renderer_text_new();
177 proto_col = gtk_tree_view_column_new_with_attributes(titles[2], proto_rend,
179 gtk_tree_view_column_set_sort_column_id(proto_col, 2);
180 gtk_tree_view_append_column(GTK_TREE_VIEW(proto_list), proto_col);
181 g_object_unref(G_OBJECT(proto_store));
183 gtk_widget_show(proto_list);
185 label = gtk_label_new("Disabling a protocol prevents higher "
186 "layer protocols from being displayed");
187 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
188 gtk_widget_show(label);
189 gtk_box_pack_start(GTK_BOX(proto_vb), label, FALSE, FALSE, 5);
192 bbox = gtk_hbutton_box_new();
193 gtk_button_box_set_layout(GTK_BUTTON_BOX(bbox), GTK_BUTTONBOX_END);
194 gtk_button_box_set_spacing(GTK_BUTTON_BOX(bbox), 5);
195 gtk_box_pack_start(GTK_BOX(proto_vb), bbox, FALSE, FALSE, 0);
196 gtk_widget_show(bbox);
199 button = gtk_button_new_with_label("Enable All");
200 SIGNAL_CONNECT(button, "clicked", enable_all_cb, proto_list);
201 gtk_box_pack_start(GTK_BOX(bbox), button, TRUE, TRUE, 0);
202 gtk_widget_show(button);
205 button = gtk_button_new_with_label("Disable All");
206 SIGNAL_CONNECT(button, "clicked", disable_all_cb, proto_list);
207 gtk_box_pack_start(GTK_BOX(bbox), button, TRUE, TRUE, 0);
208 gtk_widget_show(button);
211 button = gtk_button_new_with_label("Invert");
212 SIGNAL_CONNECT(button, "clicked", toggle_all_cb, proto_list);
213 gtk_box_pack_start(GTK_BOX(bbox), button, TRUE, TRUE, 0);
214 gtk_widget_show(button);
217 /* Ok, Apply, Cancel Buttons */
219 bbox = gtk_hbutton_box_new();
220 gtk_button_box_set_layout(GTK_BUTTON_BOX(bbox), GTK_BUTTONBOX_END);
221 gtk_button_box_set_spacing(GTK_BUTTON_BOX(bbox), 5);
222 gtk_box_pack_start(GTK_BOX(main_vb), bbox, FALSE, FALSE, 0);
223 gtk_widget_show(bbox);
225 #if GTK_MAJOR_VERSION < 2
226 button = gtk_button_new_with_label ("OK");
228 button = gtk_button_new_from_stock(GTK_STOCK_OK);
230 SIGNAL_CONNECT(button, "clicked", proto_ok_cb, proto_w);
231 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
232 gtk_box_pack_start(GTK_BOX (bbox), button, TRUE, TRUE, 0);
233 gtk_widget_grab_default(button);
234 gtk_widget_show(button);
236 #if GTK_MAJOR_VERSION < 2
237 button = gtk_button_new_with_label ("Apply");
239 button = gtk_button_new_from_stock(GTK_STOCK_APPLY);
241 SIGNAL_CONNECT(button, "clicked", proto_apply_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 #if GTK_MAJOR_VERSION < 2
247 button = gtk_button_new_with_label ("Cancel");
249 button = gtk_button_new_from_stock(GTK_STOCK_CANCEL);
251 SIGNAL_CONNECT(button, "clicked", proto_cancel_cb, proto_w);
252 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
253 gtk_box_pack_start(GTK_BOX (bbox), button, TRUE, TRUE, 0);
254 gtk_widget_show(button);
256 dlg_set_cancel(proto_w, button);
258 gtk_quit_add_destroy(gtk_main_level(), GTK_OBJECT(proto_w));
259 gtk_widget_show(proto_w);
263 #if GTK_MAJOR_VERSION < 2
265 proto_list_select_cb(GtkCList *proto_list, gint row, gint col,
266 GdkEventButton *ev _U_, gpointer gp _U_) {
267 protocol_data_t *p = gtk_clist_get_row_data(proto_list, row);
269 if (row < 0 || col < 0)
277 gtk_clist_set_text(proto_list, row, 0, STATUS_TXT(p->enabled) );
278 } /* proto_list_select_cb */
281 proto_list_keypress_cb(GtkWidget *pl, GdkEventKey *ev, gpointer gp _U_) {
282 GtkCList *proto_list = GTK_CLIST(pl);
284 if (ev->keyval == GDK_space) {
285 proto_list_select_cb(proto_list, proto_list->focus_row, 0, NULL, gp);
292 status_toggled(GtkCellRendererToggle *cell _U_, gchar *path_str, gpointer data)
294 GtkTreeModel *model = (GtkTreeModel *)data;
296 GtkTreePath *path = gtk_tree_path_new_from_string(path_str);
299 gtk_tree_model_get_iter(model, &iter, path);
300 gtk_tree_model_get(model, &iter, 3, &p, -1);
307 gtk_list_store_set(GTK_LIST_STORE(model), &iter, 0, p->enabled, -1);
309 gtk_tree_path_free(path);
310 } /* status toggled */
313 /* XXX - We need callbacks for Gtk2 */
318 toggle_all_cb(GtkWidget *button _U_, gpointer pl)
321 #if GTK_MAJOR_VERSION < 2
322 GtkCList *proto_list = GTK_CLIST(pl);
324 GtkListStore *s = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(pl)));
327 for (entry = protocol_list; entry != NULL; entry = g_slist_next(entry)) {
328 protocol_data_t *p = entry->data;
335 #if GTK_MAJOR_VERSION < 2
336 gtk_clist_set_text(proto_list, p->row, 0, STATUS_TXT(p->enabled) );
338 gtk_list_store_set(s, &p->iter, 0, p->enabled, -1);
343 /* Enable/Disable All Helper */
345 set_active_all(GtkWidget *w, gboolean new_state)
348 #if GTK_MAJOR_VERSION < 2
349 GtkCList *proto_list = GTK_CLIST(w);
351 GtkListStore *s = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(w)));
355 #if GTK_MAJOR_VERSION < 2
356 gtk_clist_freeze(proto_list);
358 for (entry = protocol_list; entry != NULL; entry = g_slist_next(entry)) {
359 protocol_data_t *p = entry->data;
361 p->enabled = new_state;
362 #if GTK_MAJOR_VERSION < 2
363 gtk_clist_set_text(proto_list, p->row, 0, STATUS_TXT(new_state) );
365 gtk_list_store_set(s, &p->iter, 0, new_state, -1);
368 #if GTK_MAJOR_VERSION < 2
369 gtk_clist_thaw(proto_list);
375 enable_all_cb(GtkWidget *button _U_, gpointer pl)
377 set_active_all(pl, TRUE);
382 disable_all_cb(GtkWidget *button _U_, gpointer pl)
384 set_active_all(pl, FALSE);
388 proto_destroy_cb(GtkWidget *w _U_, gpointer data _U_)
393 gtk_widget_destroy(proto_w);
396 /* remove protocol list */
398 for (entry = protocol_list; entry != NULL; entry = g_slist_next(entry)) {
401 g_slist_free(protocol_list);
402 protocol_list = NULL;
406 /* Treat this as a cancel, by calling "proto_cancel_cb()".
407 XXX - that'll destroy the Protocols dialog; will that upset
408 a higher-level handler that says "OK, we've been asked to delete
409 this, so destroy it"? */
411 proto_delete_cb(GtkWidget *proto_w, gpointer dummy _U_)
413 proto_cancel_cb(NULL, proto_w);
418 proto_ok_cb(GtkWidget *ok_bt _U_, gpointer parent_w)
422 redissect = set_proto_selection(GTK_WIDGET(parent_w));
423 gtk_widget_destroy(GTK_WIDGET(parent_w));
425 redissect_packets(&cfile);
429 proto_apply_cb(GtkWidget *apply_bt _U_, gpointer parent_w)
431 if (set_proto_selection(GTK_WIDGET(parent_w)))
432 redissect_packets(&cfile);
436 proto_cancel_cb(GtkWidget *cancel_bt _U_, gpointer parent_w)
440 redissect = revert_proto_selection();
441 gtk_widget_destroy(GTK_WIDGET(parent_w));
443 redissect_packets(&cfile);
447 set_proto_selection(GtkWidget *parent_w _U_)
450 gboolean need_redissect = FALSE;
452 for (entry = protocol_list; entry != NULL; entry = g_slist_next(entry)) {
453 protocol_data_t *p = entry->data;
455 if (proto_is_protocol_enabled(p->hfinfo_index) != p->enabled) {
456 proto_set_decoding(p->hfinfo_index, p->enabled);
457 need_redissect = TRUE;
461 return need_redissect;
463 } /* set_proto_selection */
466 revert_proto_selection(void)
469 gboolean need_redissect = FALSE;
472 * Undo all the changes we've made to protocol enable flags.
474 for (entry = protocol_list; entry != NULL; entry = g_slist_next(entry)) {
475 protocol_data_t *p = entry->data;
477 if (proto_is_protocol_enabled(p->hfinfo_index) != p->was_enabled) {
478 proto_set_decoding(p->hfinfo_index, p->was_enabled);
479 need_redissect = TRUE;
483 return need_redissect;
485 } /* revert_proto_selection */
488 protocol_data_compare(gconstpointer a, gconstpointer b)
490 const protocol_data_t *ap = (const protocol_data_t *)a;
491 const protocol_data_t *bp = (const protocol_data_t *)b;
493 return strcmp(ap->abbrev, bp->abbrev);
497 #if GTK_MAJOR_VERSION < 2
498 show_proto_selection(GtkCList *proto_list)
500 show_proto_selection(GtkListStore *proto_store)
507 #if GTK_MAJOR_VERSION < 2
508 gchar *proto_text[3];
511 /* Iterate over all the protocols */
513 for (i = proto_get_first_protocol(&cookie); i != -1;
514 i = proto_get_next_protocol(&cookie)) {
515 if (proto_can_disable_protocol(i)) {
516 p = g_malloc(sizeof(protocol_data_t));
517 p->name = proto_get_protocol_name(i);
518 p->abbrev = proto_get_protocol_short_name(i);
520 p->enabled = proto_is_protocol_enabled(i);
521 p->was_enabled = p->enabled;
522 protocol_list = g_slist_insert_sorted(protocol_list,
523 p, protocol_data_compare);
527 for (entry = protocol_list; entry != NULL; entry = g_slist_next(entry)) {
530 #if GTK_MAJOR_VERSION < 2
531 /* XXX - The preferred way to do this would be to have a check box
532 * in the first column. GtkClists don't let us put arbitrary widgets
533 * in a cell, so we use the word "Disabled" instead. We should be
534 * able to use check boxes in Gtk2, however.
536 proto_text[0] = STATUS_TXT (p->enabled);
537 proto_text[1] = p->abbrev;
538 proto_text[2] = p->name;
539 p->row = gtk_clist_append(proto_list, proto_text);
540 gtk_clist_set_row_data(proto_list, p->row, p);
542 gtk_list_store_append(proto_store, &p->iter);
543 gtk_list_store_set(proto_store, &p->iter,
552 } /* show_proto_selection */