Update my email address in various places since my old one no longer works
[obnox/wireshark/wip.git] / gtk / proto_dlg.c
1 /* proto_dlg.c
2  *
3  * $Id: proto_dlg.c,v 1.25 2003/01/26 19:35:31 deniel Exp $
4  *
5  * Laurent Deniel <laurent.deniel@free.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 <gdk/gdkkeysyms.h>
32 #include <string.h>
33
34 #include "prefs.h"
35 #include "globals.h"
36 #include "main.h"
37 #include "util.h"
38 #include "ui_util.h"
39 #include "dlg_utils.h"
40 #include "proto_dlg.h"
41 #include "compat_macros.h"
42
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);
48
49 #if GTK_MAJOR_VERSION < 2
50 static void show_proto_selection(GtkCList *proto_list);
51 #else
52 static void show_proto_selection(GtkListStore *proto_store);
53 #endif
54 static gboolean set_proto_selection(GtkWidget *);
55 static gboolean revert_proto_selection(void);
56
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,
64                                    gpointer gp);
65 #else
66 static void status_toggled(GtkCellRendererToggle *, gchar *, gpointer);
67 #endif
68
69 static GtkWidget *proto_w = NULL;
70
71 /* list of protocols */
72 static GSList *protocol_list = NULL;
73
74 typedef struct protocol_data {
75   char     *name;
76   char     *abbrev;
77   int      hfinfo_index;
78   gboolean enabled;
79   gboolean was_enabled;
80 #if GTK_MAJOR_VERSION < 2
81   gint     row;
82 #else
83   GtkTreeIter iter;
84 #endif
85 } protocol_data_t;
86
87 #define DISABLED "Disabled"
88 #define STATUS_TXT(x) ((x) ? "" : DISABLED)
89
90 void
91 proto_cb(GtkWidget *w _U_, gpointer data _U_)
92 {
93
94   GtkWidget *main_vb, *bbox, *proto_list, *label, *proto_sw, *proto_frame,
95             *proto_vb, *button;
96   gchar *titles[] = { "Status", "Protocol", "Description" };
97 #if GTK_MAJOR_VERSION < 2
98   gint width;
99 #else
100   GtkListStore *proto_store;
101   GtkCellRenderer *proto_rend;
102   GtkTreeViewColumn *proto_col;
103 #endif
104
105
106   if (proto_w != NULL) {
107     reactivate_window(proto_w);
108     return;
109   }
110
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);
115
116   /* Container for each row of widgets */
117
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);
122
123   /* Protocol selection list ("enable/disable" protocols) */
124
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);
129
130   /* Protocol list */
131   
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);
137   
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);
141
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));
156 #else
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,
162                                        GTK_SORT_ASCENDING);
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,
168                                                     "active", 0, NULL);
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,
173                                                     "text", 1, NULL);
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,
178                                                     "text", 2, NULL);
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));
182 #endif
183   gtk_widget_show(proto_list);
184
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);
190
191
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);
197
198   /* Enable All */
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);
203
204   /* Disable All */
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);
209
210   /* Invert */
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);
215
216
217   /* Ok, Apply, Cancel Buttons */
218
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);
224
225 #if GTK_MAJOR_VERSION < 2
226   button = gtk_button_new_with_label ("OK");
227 #else
228   button = gtk_button_new_from_stock(GTK_STOCK_OK);
229 #endif
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);
235
236 #if GTK_MAJOR_VERSION < 2
237   button = gtk_button_new_with_label ("Apply");
238 #else
239   button = gtk_button_new_from_stock(GTK_STOCK_APPLY);
240 #endif
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);
245
246 #if GTK_MAJOR_VERSION < 2
247   button = gtk_button_new_with_label ("Cancel");
248 #else
249   button = gtk_button_new_from_stock(GTK_STOCK_CANCEL);
250 #endif
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);
255
256   dlg_set_cancel(proto_w, button);
257
258   gtk_quit_add_destroy(gtk_main_level(), GTK_OBJECT(proto_w));
259   gtk_widget_show(proto_w);
260
261 } /* proto_cb */
262
263 #if GTK_MAJOR_VERSION < 2
264 static void
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);
268   
269   if (row < 0 || col < 0)
270     return;
271
272   if (p->enabled)
273     p->enabled = FALSE;
274   else
275     p->enabled = TRUE;
276
277   gtk_clist_set_text(proto_list, row, 0, STATUS_TXT(p->enabled) );
278 } /* proto_list_select_cb */
279
280 static gboolean
281 proto_list_keypress_cb(GtkWidget *pl, GdkEventKey *ev, gpointer gp _U_) {
282   GtkCList *proto_list = GTK_CLIST(pl);
283   
284   if (ev->keyval == GDK_space) {
285     proto_list_select_cb(proto_list, proto_list->focus_row, 0, NULL, gp);
286   }
287   return TRUE;
288 }
289
290 #else
291 static void
292 status_toggled(GtkCellRendererToggle *cell _U_, gchar *path_str, gpointer data)
293 {
294   GtkTreeModel    *model = (GtkTreeModel *)data;
295   GtkTreeIter      iter;
296   GtkTreePath     *path = gtk_tree_path_new_from_string(path_str);
297   protocol_data_t *p;
298
299   gtk_tree_model_get_iter(model, &iter, path);
300   gtk_tree_model_get(model, &iter, 3, &p, -1);
301
302   if (p->enabled)
303     p->enabled = FALSE;
304   else
305     p->enabled = TRUE;
306
307   gtk_list_store_set(GTK_LIST_STORE(model), &iter, 0, p->enabled, -1);
308
309   gtk_tree_path_free(path);
310 } /* status toggled */
311 #endif
312
313 /* XXX - We need callbacks for Gtk2 */
314
315
316 /* Toggle All */
317 static void
318 toggle_all_cb(GtkWidget *button _U_, gpointer pl)
319 {
320   GSList *entry;
321 #if GTK_MAJOR_VERSION < 2
322   GtkCList *proto_list = GTK_CLIST(pl);
323 #else
324   GtkListStore *s = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(pl)));
325 #endif
326
327   for (entry = protocol_list; entry != NULL; entry = g_slist_next(entry)) {
328     protocol_data_t *p = entry->data;
329
330     if (p->enabled)
331       p->enabled = FALSE;
332     else
333       p->enabled = TRUE;
334     
335 #if GTK_MAJOR_VERSION < 2
336     gtk_clist_set_text(proto_list, p->row, 0, STATUS_TXT(p->enabled) );
337 #else
338     gtk_list_store_set(s, &p->iter, 0, p->enabled, -1);
339 #endif
340   }
341 }
342
343 /* Enable/Disable All Helper */
344 static void
345 set_active_all(GtkWidget *w, gboolean new_state)
346 {
347
348 #if GTK_MAJOR_VERSION < 2
349   GtkCList *proto_list = GTK_CLIST(w);
350 #else
351   GtkListStore *s = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(w)));
352 #endif
353   GSList *entry;
354
355 #if GTK_MAJOR_VERSION < 2
356   gtk_clist_freeze(proto_list);
357 #endif
358   for (entry = protocol_list; entry != NULL; entry = g_slist_next(entry)) {
359     protocol_data_t *p = entry->data;
360     
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) );
364 #else
365     gtk_list_store_set(s, &p->iter, 0, new_state, -1);
366 #endif
367   }
368 #if GTK_MAJOR_VERSION < 2
369   gtk_clist_thaw(proto_list);
370 #endif
371 }
372
373 /* Enable All */
374 static void
375 enable_all_cb(GtkWidget *button _U_, gpointer pl)
376 {
377         set_active_all(pl, TRUE);
378 }
379
380 /* Disable All */
381 static void
382 disable_all_cb(GtkWidget *button _U_, gpointer pl)
383 {
384         set_active_all(pl, FALSE);
385 }
386
387 static void
388 proto_destroy_cb(GtkWidget *w _U_, gpointer data _U_)
389 {
390   GSList *entry;
391
392   if (proto_w)
393     gtk_widget_destroy(proto_w);
394   proto_w = NULL;
395
396   /* remove protocol list */
397   if (protocol_list) {
398     for (entry = protocol_list; entry != NULL; entry = g_slist_next(entry)) {
399       g_free(entry->data);
400     }
401     g_slist_free(protocol_list);
402     protocol_list = NULL;
403   }
404 }
405
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"? */
410 static gboolean
411 proto_delete_cb(GtkWidget *proto_w, gpointer dummy _U_)
412 {
413   proto_cancel_cb(NULL, proto_w);
414   return FALSE;
415 }
416
417 static void
418 proto_ok_cb(GtkWidget *ok_bt _U_, gpointer parent_w)
419 {
420   gboolean redissect;
421
422   redissect = set_proto_selection(GTK_WIDGET(parent_w));
423   gtk_widget_destroy(GTK_WIDGET(parent_w));
424   if (redissect)
425     redissect_packets(&cfile);
426 }
427
428 static void
429 proto_apply_cb(GtkWidget *apply_bt _U_, gpointer parent_w)
430 {
431   if (set_proto_selection(GTK_WIDGET(parent_w)))
432     redissect_packets(&cfile);
433 }
434
435 static void
436 proto_cancel_cb(GtkWidget *cancel_bt _U_, gpointer parent_w)
437 {
438   gboolean redissect;
439
440   redissect = revert_proto_selection();
441   gtk_widget_destroy(GTK_WIDGET(parent_w));
442   if (redissect)
443     redissect_packets(&cfile);
444 }
445
446 static gboolean
447 set_proto_selection(GtkWidget *parent_w _U_)
448 {
449   GSList *entry;
450   gboolean need_redissect = FALSE;
451
452   for (entry = protocol_list; entry != NULL; entry = g_slist_next(entry)) {
453     protocol_data_t *p = entry->data;
454
455     if (proto_is_protocol_enabled(p->hfinfo_index) != p->enabled) {
456       proto_set_decoding(p->hfinfo_index, p->enabled);
457       need_redissect = TRUE;
458     }
459   }
460
461   return need_redissect;
462
463 } /* set_proto_selection */
464
465 static gboolean
466 revert_proto_selection(void)
467 {
468   GSList *entry;
469   gboolean need_redissect = FALSE;
470
471   /*
472    * Undo all the changes we've made to protocol enable flags.
473    */
474   for (entry = protocol_list; entry != NULL; entry = g_slist_next(entry)) {
475     protocol_data_t *p = entry->data;
476
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;
480     }
481   }
482
483   return need_redissect;
484
485 } /* revert_proto_selection */
486
487 gint
488 protocol_data_compare(gconstpointer a, gconstpointer b)
489 {
490   const protocol_data_t *ap = (const protocol_data_t *)a;
491   const protocol_data_t *bp = (const protocol_data_t *)b;
492
493   return strcmp(ap->abbrev, bp->abbrev);
494 }
495
496 static void
497 #if GTK_MAJOR_VERSION < 2
498 show_proto_selection(GtkCList *proto_list)
499 #else
500 show_proto_selection(GtkListStore *proto_store)
501 #endif
502 {
503   GSList *entry;
504   gint i;
505   void *cookie;
506   protocol_data_t *p;
507 #if GTK_MAJOR_VERSION < 2
508   gchar *proto_text[3];
509 #endif
510
511   /* Iterate over all the protocols */
512
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);
519         p->hfinfo_index = 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);
524       }
525   }
526
527   for (entry = protocol_list; entry != NULL; entry = g_slist_next(entry)) {
528     p = entry->data;
529
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.
535      */        
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);
541 #else
542     gtk_list_store_append(proto_store, &p->iter);
543     gtk_list_store_set(proto_store, &p->iter,
544                        0, p->enabled,
545                        1, p->abbrev,
546                        2, p->name,
547                        3, p,
548                       -1);
549 #endif
550   }
551
552 } /* show_proto_selection */