Enable/disable protocols with a single click. Fix the behavior of
[obnox/wireshark/wip.git] / gtk / proto_dlg.c
1 /* proto_dlg.c
2  *
3  * $Id: proto_dlg.c,v 1.21 2002/12/01 22:51:56 gerald Exp $
4  *
5  * Laurent Deniel <deniel@worldnet.fr>
6  *
7  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@ethereal.com>
9  * Copyright 2000 Gerald Combs
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 #include <string.h>
32
33 #include "prefs.h"
34 #include "globals.h"
35 #include "main.h"
36 #include "util.h"
37 #include "ui_util.h"
38 #include "dlg_utils.h"
39 #include "proto_dlg.h"
40 #include "compat_macros.h"
41
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);
47
48 #if GTK_MAJOR_VERSION < 2
49 static void show_proto_selection(GtkCList *proto_list);
50 #else
51 static void show_proto_selection(GtkListStore *proto_store);
52 #endif
53 static gboolean set_proto_selection(GtkWidget *);
54 static gboolean revert_proto_selection(void);
55
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);
62 #endif
63
64 static GtkWidget *proto_w = NULL;
65
66 /* list of protocols */
67 static GSList *protocol_list = NULL;
68
69 typedef struct protocol_data {
70   char     *name;
71   char     *abbrev;
72   int      hfinfo_index;
73   gboolean enabled;
74   gboolean was_enabled;
75 #if GTK_MAJOR_VERSION < 2
76   gint     row;
77 #else
78   GtkTreeIter iter;
79 #endif
80 } protocol_data_t;
81
82 #define DISABLED "Disabled"
83 #define STATUS_TXT(x) ((x) ? "" : DISABLED)
84
85 void
86 proto_cb(GtkWidget *w _U_, gpointer data _U_)
87 {
88
89   GtkWidget *main_vb, *bbox, *proto_list, *label, *proto_sw, *proto_frame,
90             *proto_vb, *button;
91   gchar *titles[] = { "Status", "Protocol", "Description" };
92   gint width;
93 #if GTK_MAJOR_VERSION >= 2
94   GtkListStore *proto_store;
95   GtkCellRenderer *proto_rend;
96   GtkTreeViewColumn *proto_col;
97 #endif
98
99
100   if (proto_w != NULL) {
101     reactivate_window(proto_w);
102     return;
103   }
104
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);
109
110   /* Container for each row of widgets */
111
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);
116
117   /* Protocol selection list ("enable/disable" protocols) */
118
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);
123
124   /* Protocol list */
125   
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);
131   
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);
135
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);
150 #else
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,
159                                                     "status", 0, NULL);
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,
164                                                     "abbrev", 1, NULL);
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,
169                                                     "name", 2, NULL);
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);
172 #endif
173
174
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);
180
181
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);
187
188   /* Enable All */
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);
193
194   /* Disable All */
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);
199
200   /* Invert */
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);
205
206
207   /* Ok, Apply, Cancel Buttons */
208
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);
214
215 #if GTK_MAJOR_VERSION < 2
216   button = gtk_button_new_with_label ("OK");
217 #else
218   button = gtk_button_new_from_stock(GTK_STOCK_OK);
219 #endif
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);
225
226 #if GTK_MAJOR_VERSION < 2
227   button = gtk_button_new_with_label ("Apply");
228 #else
229   button = gtk_button_new_from_stock(GTK_STOCK_APPLY);
230 #endif
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);
235
236 #if GTK_MAJOR_VERSION < 2
237   button = gtk_button_new_with_label ("Cancel");
238 #else
239   button = gtk_button_new_from_stock(GTK_STOCK_CANCEL);
240 #endif
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);
245
246   dlg_set_cancel(proto_w, button);
247
248   gtk_quit_add_destroy(gtk_main_level(), GTK_OBJECT(proto_w));
249   gtk_widget_show(proto_w);
250
251 } /* proto_cb */
252
253 #if GTK_MAJOR_VERSION < 2
254 static void
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);
258   
259   if (row < 0 || col < 0)
260     return;
261
262   if (p->enabled)
263     p->enabled = FALSE;
264   else
265     p->enabled = TRUE;
266
267   gtk_clist_set_text(proto_list, row, 0, STATUS_TXT(p->enabled) );
268 } /* proto_list_select_cb */
269 #endif
270
271 /* XXX - We need a callback for Gtk2 */
272
273
274 /* Toggle All */
275 static void
276 toggle_all_cb(GtkWidget *button _U_, gpointer pl)
277 {
278
279   GSList *entry;
280 #if GTK_MAJOR_VERSION < 2
281   GtkCList *proto_list = GTK_CLIST(pl);
282 #endif
283
284   for (entry = protocol_list; entry != NULL; entry = g_slist_next(entry)) {
285     protocol_data_t *p = entry->data;
286
287     if (p->enabled)
288       p->enabled = FALSE;
289     else
290       p->enabled = TRUE;
291     
292 #if GTK_MAJOR_VERSION < 2
293     gtk_clist_set_text(proto_list, p->row, 0, STATUS_TXT(p->enabled) );
294 #endif
295   }
296 }
297
298 /* Enable/Disable All Helper */
299 static void
300 set_active_all(GtkWidget *w, gboolean new_state)
301 {
302
303 #if GTK_MAJOR_VERSION < 2
304   GtkCList *proto_list = GTK_CLIST(w);
305 #endif
306   GSList *entry;
307
308 #if GTK_MAJOR_VERSION < 2
309   gtk_clist_freeze(proto_list);
310 #endif
311   for (entry = protocol_list; entry != NULL; entry = g_slist_next(entry)) {
312     protocol_data_t *p = entry->data;
313     
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) );
317 #endif
318   }
319 #if GTK_MAJOR_VERSION < 2
320   gtk_clist_thaw(proto_list);
321 #endif
322 }
323
324 /* Enable All */
325 static void
326 enable_all_cb(GtkWidget *button _U_, gpointer pl)
327 {
328         set_active_all(pl, TRUE);
329 }
330
331 /* Disable All */
332 static void
333 disable_all_cb(GtkWidget *button _U_, gpointer pl)
334 {
335         set_active_all(pl, FALSE);
336 }
337
338 static void
339 proto_destroy_cb(GtkWidget *w _U_, gpointer data _U_)
340 {
341   GSList *entry;
342
343   if (proto_w)
344     gtk_widget_destroy(proto_w);
345   proto_w = NULL;
346
347   /* remove protocol list */
348   if (protocol_list) {
349     for (entry = protocol_list; entry != NULL; entry = g_slist_next(entry)) {
350       g_free(entry->data);
351     }
352     g_slist_free(protocol_list);
353     protocol_list = NULL;
354   }
355 }
356
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"? */
361 static gboolean
362 proto_delete_cb(GtkWidget *proto_w, gpointer dummy _U_)
363 {
364   proto_cancel_cb(NULL, proto_w);
365   return FALSE;
366 }
367
368 static void
369 proto_ok_cb(GtkWidget *ok_bt _U_, gpointer parent_w)
370 {
371   gboolean redissect;
372
373   redissect = set_proto_selection(GTK_WIDGET(parent_w));
374   gtk_widget_destroy(GTK_WIDGET(parent_w));
375   if (redissect)
376     redissect_packets(&cfile);
377 }
378
379 static void
380 proto_apply_cb(GtkWidget *apply_bt _U_, gpointer parent_w)
381 {
382   if (set_proto_selection(GTK_WIDGET(parent_w)))
383     redissect_packets(&cfile);
384 }
385
386 static void
387 proto_cancel_cb(GtkWidget *cancel_bt _U_, gpointer parent_w)
388 {
389   gboolean redissect;
390
391   redissect = revert_proto_selection();
392   gtk_widget_destroy(GTK_WIDGET(parent_w));
393   if (redissect)
394     redissect_packets(&cfile);
395 }
396
397 static gboolean
398 set_proto_selection(GtkWidget *parent_w _U_)
399 {
400   GSList *entry;
401   gboolean need_redissect = FALSE;
402
403   for (entry = protocol_list; entry != NULL; entry = g_slist_next(entry)) {
404     protocol_data_t *p = entry->data;
405
406     if (proto_is_protocol_enabled(p->hfinfo_index) != p->enabled) {
407       proto_set_decoding(p->hfinfo_index, p->enabled);
408       need_redissect = TRUE;
409     }
410   }
411
412   return need_redissect;
413
414 } /* set_proto_selection */
415
416 static gboolean
417 revert_proto_selection(void)
418 {
419   GSList *entry;
420   gboolean need_redissect = FALSE;
421
422   /*
423    * Undo all the changes we've made to protocol enable flags.
424    */
425   for (entry = protocol_list; entry != NULL; entry = g_slist_next(entry)) {
426     protocol_data_t *p = entry->data;
427
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;
431     }
432   }
433
434   return need_redissect;
435
436 } /* revert_proto_selection */
437
438 gint
439 protocol_data_compare(gconstpointer a, gconstpointer b)
440 {
441   const protocol_data_t *ap = (const protocol_data_t *)a;
442   const protocol_data_t *bp = (const protocol_data_t *)b;
443
444   return strcmp(ap->abbrev, bp->abbrev);
445 }
446
447 static void
448 #if GTK_MAJOR_VERSION < 2
449 show_proto_selection(GtkCList *proto_list)
450 #else
451 show_proto_selection(GtkListStore *proto_store)
452 #endif
453 {
454   GSList *entry;
455   gint i;
456   void *cookie;
457   protocol_data_t *p;
458 #if GTK_MAJOR_VERSION < 2
459   gchar *proto_text[3];
460 #else
461   GtkTreeIter proto_iter;
462 #endif
463
464   /* Iterate over all the protocols */
465
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);
472         p->hfinfo_index = 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);
477       }
478   }
479
480   for (entry = protocol_list; entry != NULL; entry = g_slist_next(entry)) {
481     p = entry->data;
482
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.
488      */        
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);
494 #else
495     gtk_list_store_append(proto_store, &proto_iter);
496     gtk_list_store_set(proto_store, &proto_iter,
497                        0, STATUS_TXT (p->enabled),
498                        1, p->abbrev,
499                        2, p->name,
500                       -1);
501 #endif
502   }
503
504 } /* show_proto_selection */