eb6a42a936d7a2b6c1d7fdfef46719ce6571667c
[obnox/wireshark/wip.git] / gtk / color_dlg.c
1 /* color_dlg.c
2  * Definitions for dialog boxes for color filters
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <gtk/gtk.h>
30
31 #include <string.h>
32
33 #include "gtk/main.h"
34 #include <epan/packet.h>
35 #include "color.h"
36 #include "colors.h"
37 #include "color_filters.h"
38 #include "color_dlg.h"
39 #include "file.h"
40 #include <epan/dfilter/dfilter.h>
41 #include "simple_dialog.h"
42 #include "dlg_utils.h"
43 #include "gui_utils.h"
44 #include "dfilter_expr_dlg.h"
45 #include "compat_macros.h"
46 #include "filter_dlg.h"
47 #include "capture_file_dlg.h"
48 #include "gtkglobals.h"
49 #include <epan/prefs.h>
50 #include "help_dlg.h"
51
52 #include "color_edit_dlg.h"
53
54 #if GTK_MAJOR_VERSION >= 2
55 #define BUTTON_SIZE_X -1
56 #define BUTTON_SIZE_Y -1
57 #else
58 #define BUTTON_SIZE_X 50
59 #define BUTTON_SIZE_Y 20
60 #endif
61
62
63 static GtkWidget* colorize_dialog_new(char *filter);
64 static void add_filter_to_list(gpointer filter_arg, gpointer list_arg);
65 static void color_filter_up_cb(GtkButton *button, gpointer user_data);
66 static void color_filter_down_cb(GtkButton *button, gpointer user_data);
67 #if GTK_MAJOR_VERSION < 2
68 static void remember_selected_row(GtkCList *clist, gint row, gint column,
69                                   GdkEvent *event, gpointer user_data);
70 static void unremember_selected_row(GtkCList *clist, gint row, gint column,
71                                     GdkEvent *event, gpointer user_data);
72 #else
73 static void remember_selected_row(GtkTreeSelection *sel, gpointer list);
74 #endif
75 static void color_destroy_cb(GtkButton *button, gpointer user_data);
76 static void destroy_edit_dialog_cb(gpointer filter_arg, gpointer dummy);
77 static void create_new_color_filter(GtkButton *button, const char *filter);
78 static void color_new_cb(GtkButton *button, gpointer user_data);
79 static void color_edit_cb(GtkButton *button, gpointer user_data);
80 static gint color_filters_button_cb(GtkWidget *, GdkEventButton *, gpointer);
81 static void color_disable_cb(GtkWidget *widget, gboolean user_data);
82 static void color_delete_cb(GtkWidget *widget, gpointer user_data);
83 static void color_save_cb(GtkButton *button, gpointer user_data);
84 static void color_ok_cb(GtkButton *button, gpointer user_data);
85 static void color_cancel_cb(GtkWidget *widget, gpointer user_data);
86 static void color_apply_cb(GtkButton *button, gpointer user_data);
87 static void color_clear_cb(GtkWidget *button, gpointer user_data);
88 static void color_export_cb(GtkButton *button, gpointer user_data );
89 static void color_import_cb(GtkButton *button, gpointer user_data );
90
91
92 static GtkWidget *colorize_win;
93 gint      num_of_filters;  /* number of filters being displayed */
94 gint      row_selected;    /* row in color_filters that is selected */
95 #if GTK_MAJOR_VERSION >= 2
96 gboolean  row_is_moving = FALSE;
97 #endif
98
99 /* This is a list of all current color filters in the dialog
100  * (copied from color_filters.c and edited with the dialog).
101  * The color filter items are not identical to the ones used for the
102  * packet list display, so they can be safely edited.
103  *
104  * Keep the temporary filters in a seperate list so that they are
105  * not showed in the edit-dialog
106  *
107  * XXX - use the existing GTK list for this purpose and build temporary copies
108  * e.g. for the save/export functions.
109  * Problem: Don't know when able to safely throw away, e.g. while exporting.
110  */
111 static GSList *color_filter_edit_list = NULL;
112 static GSList *color_filter_tmp_list = NULL;
113
114
115 #define COLOR_UP_LB             "color_up_lb"
116 #define COLOR_DOWN_LB           "color_down_lb"
117 #define COLOR_EDIT_LB           "color_edit_lb"
118 #define COLOR_ENABLE_LB         "color_enable_lb"
119 #define COLOR_DISABLE_LB        "color_disable_lb"
120 #define COLOR_DELETE_LB         "color_delete_lb"
121 #define COLOR_FILTERS_CL        "color_filters_cl"
122 #define COLOR_FILTER_LIST       "color_filter_list"
123
124
125 /* Callback for the "Display:Coloring Rules" menu item. */
126 void
127 color_display_cb(GtkWidget *w _U_, gpointer d _U_)
128 {
129   if (colorize_win != NULL) {
130     /* There's already a color dialog box active; reactivate it. */
131     reactivate_window(colorize_win);
132   } else {
133     /* Create a new "Colorize Display" dialog. */
134     colorize_win = colorize_dialog_new(NULL);
135   }
136 }
137
138 /* this opens the color dialog and presets the filter string */
139 void
140 color_display_with_filter(char *filter)
141 {
142   if (colorize_win != NULL) {
143     /* There's already a color dialog box active; reactivate it. */
144     reactivate_window(colorize_win);
145   } else {
146     /* Create a new "Colorize Display" dialog. */
147     colorize_win = colorize_dialog_new(filter);
148   }
149 }
150
151 /* if this filter is selected - count it in the given int* */
152 static void
153 count_this_select(gpointer filter_arg, gpointer counter_arg)
154 {
155   color_filter_t *colorf = filter_arg;
156   int * cnt = counter_arg;
157
158   if (colorf->selected)
159     (*cnt)++;
160 }
161
162 /* TODO: implement count of selected filters. Plug in to file_dlg update of "export selected" checkbox. */
163 int color_selected_count(void)
164 {
165   int count = 0;
166
167   g_slist_foreach(color_filter_edit_list, count_this_select, &count);
168
169   return count;
170 }
171
172 /* Create the "Coloring Rules" dialog. */
173 static GtkWidget*
174 colorize_dialog_new (char *filter)
175 {
176   GtkWidget *color_win;
177   GtkWidget *dlg_vbox;
178   GtkWidget *main_hbox;
179   GtkWidget *ctrl_vbox;
180   GtkTooltips *tooltips;
181
182   GtkWidget *order_fr;
183   GtkWidget *order_vbox;
184   GtkWidget *color_filter_up;
185   GtkWidget *order_move_label;
186   GtkWidget *color_filter_down;
187
188   GtkWidget *list_fr;
189   GtkWidget *list_vbox;
190   GtkWidget *scrolledwindow1;
191   GtkWidget *color_filters;
192   GtkWidget *list_label;
193
194   GtkWidget *edit_fr;
195   GtkWidget *edit_vbox;
196   GtkWidget *color_new;
197   GtkWidget *color_edit;
198   GtkWidget *color_enable;
199   GtkWidget *color_disable;
200   GtkWidget *color_delete;
201
202   GtkWidget *manage_fr;
203   GtkWidget *manage_vbox;
204   GtkWidget *color_export;
205   GtkWidget *color_import;
206   GtkWidget *color_clear;
207
208   GtkWidget *button_ok_hbox;
209   GtkWidget *color_ok;
210   GtkWidget *color_apply;
211   GtkWidget *color_save;
212   GtkWidget *color_cancel;
213   GtkWidget *color_help;
214
215 #if GTK_MAJOR_VERSION >= 2
216   GtkListStore      *store;
217   GtkCellRenderer   *renderer;
218   GtkTreeViewColumn *column;
219   GtkTreeSelection  *selection;
220 #endif
221   const gchar *titles[] = { "Name", "String" };
222
223
224
225   num_of_filters = 0;
226   row_selected = -1; /* no row selected */
227   tooltips = gtk_tooltips_new ();
228
229   /* Resizing of the dialog window is now reasonably done.
230    * Default size is set so that it should fit into every usual screen resolution.
231    * All other widgets are always packed depending on the current window size. */
232   color_win = dlg_window_new ("Wireshark: Coloring Rules");
233   OBJECT_SET_DATA(color_win, "color_win", color_win);
234   gtk_window_set_default_size(GTK_WINDOW(color_win), DEF_WIDTH, DEF_HEIGHT * 2/3);
235   dlg_vbox = gtk_vbox_new (FALSE, 0);
236   gtk_container_set_border_width  (GTK_CONTAINER (dlg_vbox), 5);
237   gtk_container_add (GTK_CONTAINER (color_win), dlg_vbox);
238
239   main_hbox = gtk_hbox_new (FALSE, 0);
240   gtk_box_pack_start (GTK_BOX (dlg_vbox), main_hbox, TRUE, TRUE, 0);
241
242   ctrl_vbox = gtk_vbox_new (FALSE, 0);
243   gtk_box_pack_start (GTK_BOX (main_hbox), ctrl_vbox, FALSE, FALSE, 0);
244
245   /* edit buttons frame */
246   edit_fr = gtk_frame_new("Edit");
247   gtk_box_pack_start (GTK_BOX (ctrl_vbox), edit_fr, TRUE, TRUE, 0);
248
249   /* edit_vbox is first button column (containing: new, edit and such) */
250   edit_vbox = gtk_vbutton_box_new();
251   gtk_button_box_set_child_size(GTK_BUTTON_BOX(edit_vbox), BUTTON_SIZE_X, BUTTON_SIZE_Y);
252   gtk_container_set_border_width  (GTK_CONTAINER (edit_vbox), 5);
253   gtk_container_add(GTK_CONTAINER(edit_fr), edit_vbox);
254
255   color_new = BUTTON_NEW_FROM_STOCK(GTK_STOCK_NEW);
256 #if GTK_MAJOR_VERSION < 2
257   WIDGET_SET_SIZE(color_new, BUTTON_SIZE_X, BUTTON_SIZE_Y);
258 #endif
259   gtk_box_pack_start (GTK_BOX (edit_vbox), color_new, FALSE, FALSE, 5);
260   gtk_tooltips_set_tip (tooltips, color_new, ("Create a new filter at the end of the list"), NULL);
261
262   color_edit = BUTTON_NEW_FROM_STOCK(WIRESHARK_STOCK_EDIT);
263 #if GTK_MAJOR_VERSION < 2
264   WIDGET_SET_SIZE(color_edit, BUTTON_SIZE_X, BUTTON_SIZE_Y);
265 #endif
266   gtk_box_pack_start (GTK_BOX (edit_vbox), color_edit, FALSE, FALSE, 5);
267   gtk_tooltips_set_tip (tooltips, color_edit, ("Edit the properties of the selected filter."
268       " If more than one filter is selected, edit the first selected one"), NULL);
269   gtk_widget_set_sensitive (color_edit, FALSE);
270
271   color_enable = BUTTON_NEW_FROM_STOCK(WIRESHARK_STOCK_ENABLE);
272   gtk_box_pack_start (GTK_BOX (edit_vbox), color_enable, FALSE, FALSE, 5);
273 #if GTK_MAJOR_VERSION < 2
274   WIDGET_SET_SIZE(color_enable, BUTTON_SIZE_X, BUTTON_SIZE_Y);
275 #endif
276   gtk_tooltips_set_tip (tooltips, color_enable, ("Enable the selected filter(s)"), NULL);
277   gtk_widget_set_sensitive (color_enable, FALSE);
278
279   color_disable = BUTTON_NEW_FROM_STOCK(WIRESHARK_STOCK_DISABLE);
280   gtk_box_pack_start (GTK_BOX (edit_vbox), color_disable, FALSE, FALSE, 5);
281 #if GTK_MAJOR_VERSION < 2
282   WIDGET_SET_SIZE(color_disable, BUTTON_SIZE_X, BUTTON_SIZE_Y);
283 #endif
284   gtk_tooltips_set_tip (tooltips, color_disable, ("Disable the selected filter(s)"), NULL);
285   gtk_widget_set_sensitive (color_disable, FALSE);
286
287   color_delete = BUTTON_NEW_FROM_STOCK(GTK_STOCK_DELETE);
288   gtk_box_pack_start (GTK_BOX (edit_vbox), color_delete, FALSE, FALSE, 5);
289 #if GTK_MAJOR_VERSION < 2
290   WIDGET_SET_SIZE(color_delete, BUTTON_SIZE_X, BUTTON_SIZE_Y);
291 #endif
292   gtk_tooltips_set_tip (tooltips, color_delete, ("Delete the selected filter(s)"), NULL);
293   gtk_widget_set_sensitive (color_delete, FALSE);
294   /* End edit buttons frame */
295
296
297   /* manage buttons frame */
298   manage_fr = gtk_frame_new("Manage");
299   gtk_box_pack_start (GTK_BOX (ctrl_vbox), manage_fr, FALSE, FALSE, 0);
300
301   manage_vbox = gtk_vbox_new (FALSE, 0);
302   gtk_container_set_border_width  (GTK_CONTAINER (manage_vbox), 5);
303   gtk_container_add(GTK_CONTAINER(manage_fr), manage_vbox);
304
305   color_export = BUTTON_NEW_FROM_STOCK(WIRESHARK_STOCK_EXPORT);
306   gtk_box_pack_start (GTK_BOX (manage_vbox), color_export, FALSE, FALSE, 5);
307 #if GTK_MAJOR_VERSION < 2
308   WIDGET_SET_SIZE(color_export, BUTTON_SIZE_X, BUTTON_SIZE_Y);
309 #endif
310   gtk_tooltips_set_tip(tooltips, color_export, ("Save all/selected filters to a file"), NULL);
311
312   color_import = BUTTON_NEW_FROM_STOCK(WIRESHARK_STOCK_IMPORT);
313   gtk_box_pack_start (GTK_BOX (manage_vbox), color_import, FALSE, FALSE, 5);
314 #if GTK_MAJOR_VERSION < 2
315   WIDGET_SET_SIZE(color_import, BUTTON_SIZE_X, BUTTON_SIZE_Y);
316 #endif
317   gtk_tooltips_set_tip(tooltips, color_import, ("Load filters from a file and append them to the list"), NULL);
318
319   color_clear = BUTTON_NEW_FROM_STOCK(GTK_STOCK_CLEAR);
320   gtk_box_pack_start(GTK_BOX (manage_vbox), color_clear, FALSE, FALSE, 5);
321 #if GTK_MAJOR_VERSION < 2
322   WIDGET_SET_SIZE(color_clear, BUTTON_SIZE_X, BUTTON_SIZE_Y);
323 #endif
324   gtk_tooltips_set_tip(tooltips, color_clear, ("Clear the filter list and revert to system-wide default filter set"), NULL);
325
326
327   /* filter list frame */
328   list_fr = gtk_frame_new("Filter");
329   gtk_box_pack_start (GTK_BOX (main_hbox), list_fr, TRUE, TRUE, 0);
330
331   list_vbox = gtk_vbox_new (FALSE, 0);
332   gtk_container_set_border_width  (GTK_CONTAINER (list_vbox), 5);
333   gtk_container_add(GTK_CONTAINER(list_fr), list_vbox);
334
335   list_label = gtk_label_new (("List is processed in order until match is found"));
336   gtk_box_pack_start (GTK_BOX (list_vbox), list_label, FALSE, FALSE, 0);
337
338   /* create the list of filters */
339   scrolledwindow1 = scrolled_window_new(NULL, NULL);
340 #if GTK_MAJOR_VERSION >= 2
341   gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwindow1),
342                                    GTK_SHADOW_IN);
343 #endif
344   gtk_box_pack_start (GTK_BOX (list_vbox), scrolledwindow1, TRUE, TRUE, 0);
345
346 #if GTK_MAJOR_VERSION < 2
347   color_filters = gtk_clist_new_with_titles(2, (gchar **) titles);
348 #else
349   /* the list store contains : filter name, filter string, foreground
350    * color, background color, pointer to color filter */
351   store = gtk_list_store_new(6, G_TYPE_STRING, G_TYPE_STRING,
352                              G_TYPE_STRING, G_TYPE_STRING, 
353                              G_TYPE_BOOLEAN, G_TYPE_POINTER);
354   color_filters = tree_view_new(GTK_TREE_MODEL(store));
355   g_object_unref(store);
356   renderer = gtk_cell_renderer_text_new();
357   column = gtk_tree_view_column_new_with_attributes(titles[0], renderer,
358                                                     "text", 0,
359                                                     "foreground", 2,
360                                                     "background", 3,
361                                                     "strikethrough", 4,
362                                                     NULL);
363   gtk_tree_view_column_set_fixed_width(column, 80);
364   gtk_tree_view_append_column(GTK_TREE_VIEW(color_filters), column);
365   renderer = gtk_cell_renderer_text_new();
366   column = gtk_tree_view_column_new_with_attributes(titles[1], renderer,
367                                                     "text", 1,
368                                                     "foreground", 2,
369                                                     "background", 3,
370                                                     "strikethrough", 4,
371                                                     NULL);
372   gtk_tree_view_column_set_fixed_width(column, 300);
373   gtk_tree_view_append_column(GTK_TREE_VIEW(color_filters), column);
374   gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(color_filters), TRUE);
375   gtk_tree_view_set_headers_clickable(GTK_TREE_VIEW(color_filters), FALSE);
376 #endif
377
378 #if GTK_MAJOR_VERSION < 2
379   gtk_clist_set_selection_mode    (GTK_CLIST (color_filters),GTK_SELECTION_EXTENDED);
380 #else
381   selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(color_filters));
382   gtk_tree_selection_set_mode(selection, GTK_SELECTION_MULTIPLE);
383 #endif
384
385   gtk_container_add (GTK_CONTAINER (scrolledwindow1), color_filters);
386 #if GTK_MAJOR_VERSION < 2
387   gtk_clist_set_column_width (GTK_CLIST (color_filters), 0, 80);
388   gtk_clist_set_column_width (GTK_CLIST (color_filters), 1, 300);
389   gtk_clist_column_titles_show (GTK_CLIST (color_filters));
390 #endif
391
392
393   /* order frame */
394   order_fr = gtk_frame_new("Order");
395   gtk_box_pack_start (GTK_BOX (main_hbox), order_fr, FALSE, FALSE, 0);
396
397   order_vbox = gtk_vbox_new (TRUE, 0);
398   gtk_container_set_border_width  (GTK_CONTAINER (order_vbox), 5);
399   gtk_container_add(GTK_CONTAINER(order_fr), order_vbox);
400
401   color_filter_up = BUTTON_NEW_FROM_STOCK(GTK_STOCK_GO_UP);
402 #if GTK_MAJOR_VERSION < 2
403   WIDGET_SET_SIZE(color_filter_up, BUTTON_SIZE_X, BUTTON_SIZE_Y);
404 #endif
405   gtk_box_pack_start (GTK_BOX (order_vbox), color_filter_up, FALSE, FALSE, 0);
406   gtk_tooltips_set_tip (tooltips, color_filter_up, ("Move filter higher in list"), NULL);
407   gtk_widget_set_sensitive (color_filter_up, FALSE);
408
409   order_move_label = gtk_label_new (("Move\nselected filter\nup or down"));
410   gtk_box_pack_start (GTK_BOX (order_vbox), order_move_label, FALSE, FALSE, 0);
411
412   color_filter_down = BUTTON_NEW_FROM_STOCK(GTK_STOCK_GO_DOWN);
413 #if GTK_MAJOR_VERSION < 2
414   WIDGET_SET_SIZE(color_filter_down, BUTTON_SIZE_X, BUTTON_SIZE_Y);
415 #endif
416   gtk_box_pack_start (GTK_BOX (order_vbox), color_filter_down, FALSE, FALSE, 0);
417   gtk_tooltips_set_tip (tooltips, color_filter_down, ("Move filter lower in list"), NULL);
418   gtk_widget_set_sensitive (color_filter_down, FALSE);
419
420
421   /* Button row: OK and cancel buttons */
422   if(topic_available(HELP_COLORING_RULES_DIALOG)) {
423     button_ok_hbox = dlg_button_row_new(GTK_STOCK_OK, GTK_STOCK_APPLY, GTK_STOCK_SAVE, GTK_STOCK_CANCEL, GTK_STOCK_HELP, NULL);
424   } else {
425     button_ok_hbox = dlg_button_row_new(GTK_STOCK_OK, GTK_STOCK_APPLY, GTK_STOCK_SAVE, GTK_STOCK_CANCEL, NULL);
426   }
427   gtk_box_pack_start (GTK_BOX (dlg_vbox), button_ok_hbox, FALSE, FALSE, 5);
428
429   color_ok = OBJECT_GET_DATA(button_ok_hbox, GTK_STOCK_OK);
430   gtk_tooltips_set_tip (tooltips, color_ok, ("Apply the color filters to the display and close this dialog"), NULL);
431
432   color_apply = OBJECT_GET_DATA(button_ok_hbox, GTK_STOCK_APPLY);
433   gtk_tooltips_set_tip (tooltips, color_apply, ("Apply the color filters to the display and keep this dialog open"), NULL);
434
435   color_save = OBJECT_GET_DATA(button_ok_hbox, GTK_STOCK_SAVE);
436   gtk_tooltips_set_tip (tooltips, color_save, ("Save the color filters permanently and keep this dialog open"), NULL);
437
438   color_cancel = OBJECT_GET_DATA(button_ok_hbox, GTK_STOCK_CANCEL);
439   window_set_cancel_button(color_win, color_cancel, color_cancel_cb);
440   gtk_tooltips_set_tip (tooltips, color_cancel, ("Cancel changes done (since last \"Apply\") and close this dialog"), NULL);
441
442   if(topic_available(HELP_COLORING_RULES_DIALOG)) {
443       color_help = OBJECT_GET_DATA(button_ok_hbox, GTK_STOCK_HELP);
444       gtk_tooltips_set_tip (tooltips, color_help, ("Get help about this dialog"), NULL);
445       SIGNAL_CONNECT(color_help, "clicked", topic_cb, HELP_COLORING_RULES_DIALOG);
446   }
447
448   gtk_widget_grab_default(color_ok);
449
450   /* signals and such */
451   SIGNAL_CONNECT(color_win, "destroy", color_destroy_cb, NULL);
452   OBJECT_SET_DATA(color_filter_up, COLOR_FILTERS_CL, color_filters);
453   SIGNAL_CONNECT(color_filter_up, "clicked", color_filter_up_cb, NULL);
454   OBJECT_SET_DATA(color_filter_down, COLOR_FILTERS_CL, color_filters);
455   SIGNAL_CONNECT(color_filter_down, "clicked", color_filter_down_cb, NULL);
456 #if GTK_MAJOR_VERSION < 2
457   SIGNAL_CONNECT(color_filters, "select_row", remember_selected_row, NULL);
458   SIGNAL_CONNECT(color_filters, "unselect_row", unremember_selected_row, NULL);
459 #else
460   SIGNAL_CONNECT(selection, "changed", remember_selected_row, color_filters);
461 #endif
462   SIGNAL_CONNECT(color_filters, "button_press_event", color_filters_button_cb, NULL);
463   OBJECT_SET_DATA(color_filters, COLOR_UP_LB, color_filter_up);
464   OBJECT_SET_DATA(color_filters, COLOR_DOWN_LB, color_filter_down);
465   OBJECT_SET_DATA(color_filters, COLOR_EDIT_LB, color_edit);
466   OBJECT_SET_DATA(color_filters, COLOR_ENABLE_LB, color_enable);
467   OBJECT_SET_DATA(color_filters, COLOR_DISABLE_LB, color_disable);
468   OBJECT_SET_DATA(color_filters, COLOR_DELETE_LB, color_delete);
469   OBJECT_SET_DATA(color_new, COLOR_FILTERS_CL, color_filters);
470   SIGNAL_CONNECT(color_new, "clicked", color_new_cb, NULL);
471   OBJECT_SET_DATA(color_edit, COLOR_FILTERS_CL, color_filters);
472   SIGNAL_CONNECT(color_edit, "clicked", color_edit_cb, NULL);
473   OBJECT_SET_DATA(color_enable, COLOR_FILTERS_CL, color_filters);
474   SIGNAL_CONNECT(color_enable, "clicked", color_disable_cb, FALSE);
475   OBJECT_SET_DATA(color_disable, COLOR_FILTERS_CL, color_filters);
476   SIGNAL_CONNECT(color_disable, "clicked", color_disable_cb, TRUE);
477   OBJECT_SET_DATA(color_delete, COLOR_EDIT_LB, color_edit);
478   OBJECT_SET_DATA(color_delete, COLOR_FILTERS_CL, color_filters);
479   SIGNAL_CONNECT(color_delete, "clicked", color_delete_cb, NULL);
480   SIGNAL_CONNECT(color_export, "clicked", color_export_cb, NULL);
481   OBJECT_SET_DATA(color_import, COLOR_FILTERS_CL, color_filters);
482   SIGNAL_CONNECT(color_import, "clicked", color_import_cb, NULL);
483   OBJECT_SET_DATA(color_clear, COLOR_FILTERS_CL, color_filters);
484   SIGNAL_CONNECT(color_clear, "clicked", color_clear_cb, NULL);
485   SIGNAL_CONNECT(color_ok, "clicked", color_ok_cb, NULL);
486   SIGNAL_CONNECT(color_apply, "clicked", color_apply_cb, NULL);
487   SIGNAL_CONNECT(color_save, "clicked", color_save_cb, NULL);
488
489   SIGNAL_CONNECT(color_win, "delete_event", window_delete_event_cb, NULL);
490
491   gtk_widget_grab_focus(color_filters);
492
493   /* prepare filter list content */
494   color_filters_clone(color_filters);
495   OBJECT_SET_DATA(color_win, COLOR_FILTER_LIST, &color_filter_edit_list);
496
497   gtk_widget_show_all(color_win);
498
499   /* hide the Save button if the user uses implicit save */
500   if(!prefs.gui_use_pref_save) {
501     gtk_widget_hide(color_save);
502   }
503
504   window_present(color_win);
505
506   if(filter){
507     /* if we specified a preset filter string, open the new dialog and
508        set the filter */
509     create_new_color_filter(GTK_BUTTON(color_new), filter);
510   }
511
512   return color_win;
513 }
514
515 /* move a row in the list +/- one position up/down */
516 static void move_this_row (GtkWidget   *color_filters,
517                      gint         filter_number,
518                      gint         amount)            /* only tested with +1(down) and -1(up) */
519 {
520   color_filter_t *colorf;
521 #if GTK_MAJOR_VERSION < 2
522   gint            lower, higher;
523 #else
524   GtkTreeModel   *model;
525   GtkTreeIter     iter1, iter2;
526   gchar          *name, *string, *fg_str, *bg_str;
527   gboolean        disabled;
528 #endif
529
530   g_assert(amount == +1 || amount == -1);
531   g_assert(amount == +1 || filter_number > 0);
532   g_assert(amount == -1 || filter_number < num_of_filters - 1);
533
534 #if GTK_MAJOR_VERSION < 2
535   if (amount > 0)
536   {
537     lower = filter_number;
538     higher = filter_number + amount;
539   }
540   else
541   {
542     higher = filter_number;
543     lower = filter_number + amount;
544   }
545
546   colorf = gtk_clist_get_row_data(GTK_CLIST(color_filters), filter_number);
547   gtk_clist_swap_rows(GTK_CLIST(color_filters), higher, lower);
548
549   /*
550    * That row is still selected, but it's now moved.
551    */
552   remember_selected_row(GTK_CLIST(color_filters), filter_number + amount, 0, NULL, NULL);
553 #else
554
555   row_is_moving = TRUE;
556   model = gtk_tree_view_get_model(GTK_TREE_VIEW(color_filters));
557   gtk_tree_model_iter_nth_child(model, &iter1, NULL, filter_number);
558   gtk_tree_model_iter_nth_child(model, &iter2, NULL, filter_number + amount);
559
560   gtk_tree_model_get(model, &iter1, 0, &name, 1, &string,
561                      2, &fg_str, 3, &bg_str, 4, &disabled, 5, &colorf, -1);
562   gtk_list_store_remove(GTK_LIST_STORE(model), &iter1);
563   if (amount < 0)
564     gtk_list_store_insert_before(GTK_LIST_STORE(model), &iter1, &iter2);
565   else
566     gtk_list_store_insert_after(GTK_LIST_STORE(model), &iter1, &iter2);
567   gtk_list_store_set(GTK_LIST_STORE(model), &iter1, 0, name, 1, string,
568                      2, fg_str, 3, bg_str, 4, disabled, 5, colorf, -1);
569   g_free(name);
570   g_free(string);
571   g_free(fg_str);
572   g_free(bg_str);
573   row_is_moving = FALSE;
574
575   /*
576    * re-select the initial row
577    */
578   gtk_widget_grab_focus(color_filters);
579   gtk_tree_selection_select_iter(gtk_tree_view_get_selection(GTK_TREE_VIEW(color_filters)), &iter1);
580
581 #endif
582
583   color_filter_edit_list = g_slist_remove(color_filter_edit_list, colorf);
584   color_filter_edit_list = g_slist_insert(color_filter_edit_list, colorf, filter_number + amount);
585 }
586
587 /* User pressed the "Up" button: Move the selected filters up in the list */
588 static void
589 color_filter_up_cb(GtkButton *button, gpointer user_data _U_)
590 {
591   gint amount;
592   gint filter_number;
593   GtkWidget * color_filters;
594   color_filter_t *colorf;
595 #if GTK_MAJOR_VERSION < 2
596 #else
597   GtkTreeIter       iter;
598   GtkTreeModel     *model;
599   GtkTreeSelection *sel;
600 #endif
601
602   amount = -1;
603   color_filters = (GtkWidget *)OBJECT_GET_DATA(button, COLOR_FILTERS_CL);
604
605 #if GTK_MAJOR_VERSION < 2
606   colorf = gtk_clist_get_row_data(GTK_CLIST(color_filters), 0);
607   if (colorf->selected)
608     return;
609 #endif
610
611   for (filter_number = 0; filter_number < num_of_filters; filter_number++)
612   {
613 #if GTK_MAJOR_VERSION < 2
614     colorf = gtk_clist_get_row_data(GTK_CLIST(color_filters), filter_number);
615     if (colorf->selected)
616       move_this_row (color_filters, filter_number, amount);
617 #else
618     model = gtk_tree_view_get_model(GTK_TREE_VIEW(color_filters));
619     gtk_tree_model_iter_nth_child(model, &iter, NULL, filter_number);
620     gtk_tree_model_get(model, &iter, 5, &colorf, -1);
621     sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(color_filters));
622     if (gtk_tree_selection_iter_is_selected(sel, &iter))
623       move_this_row (color_filters, filter_number, amount);
624 #endif
625   }
626 }
627
628 /* User pressed the "Down" button: Move the selected filters down in the list */
629 static void
630 color_filter_down_cb(GtkButton *button, gpointer user_data _U_)
631 {
632   gint amount;
633   gint filter_number;
634   GtkWidget * color_filters;
635   color_filter_t *colorf;
636 #if GTK_MAJOR_VERSION < 2
637 #else
638   GtkTreeIter     iter;
639   GtkTreeModel   *model;
640 #endif
641
642   amount = +1;
643   color_filters = (GtkWidget *)OBJECT_GET_DATA(button, COLOR_FILTERS_CL);
644
645 #if GTK_MAJOR_VERSION < 2
646     colorf = gtk_clist_get_row_data(GTK_CLIST(color_filters), num_of_filters - 1);
647     if (colorf->selected)
648       return;
649 #endif
650
651   for (filter_number = num_of_filters - 1; filter_number >= 0; filter_number--)
652   {
653 #if GTK_MAJOR_VERSION < 2
654     colorf = gtk_clist_get_row_data(GTK_CLIST(color_filters), filter_number);
655 #else
656     model = gtk_tree_view_get_model(GTK_TREE_VIEW(color_filters));
657     gtk_tree_model_iter_nth_child(model, &iter, NULL, filter_number);
658     gtk_tree_model_get(model, &iter, 5, &colorf, -1);
659 #endif
660     if (colorf->selected)
661       move_this_row (color_filters, filter_number, amount);
662   }
663 }
664
665 /* A row was selected; remember its row number */
666 #if GTK_MAJOR_VERSION < 2
667 static void
668 remember_selected_row(GtkCList *clist, gint row, gint column _U_,
669                       GdkEvent *event _U_, gpointer user_data _U_)
670 {
671     GtkWidget    *button;
672     color_filter_t *colorf;
673
674     row_selected = row;
675
676     colorf = gtk_clist_get_row_data(clist, row);
677     colorf->selected = TRUE;
678
679     /*
680      * A row is selected, so we can move it up *if* it's not at the top
681      * and move it down *if* it's not at the bottom.
682      */
683     button = (GtkWidget *)OBJECT_GET_DATA(clist, COLOR_UP_LB);
684     gtk_widget_set_sensitive (button, row > 0);
685     button = (GtkWidget *)OBJECT_GET_DATA(clist, COLOR_DOWN_LB);
686     gtk_widget_set_sensitive(button, row < num_of_filters - 1);
687
688     /*
689      * A row is selected, so we can operate on it.
690      */
691     button = (GtkWidget *)OBJECT_GET_DATA(clist, COLOR_EDIT_LB);
692     gtk_widget_set_sensitive (button, TRUE);
693     button = (GtkWidget *)OBJECT_GET_DATA(clist, COLOR_ENABLE_LB);
694     gtk_widget_set_sensitive(button, colorf->disabled);
695     button = (GtkWidget *)OBJECT_GET_DATA(clist, COLOR_DISABLE_LB);
696     gtk_widget_set_sensitive(button, !colorf->disabled);
697     button = (GtkWidget *)OBJECT_GET_DATA(clist, COLOR_DELETE_LB);
698     gtk_widget_set_sensitive(button, TRUE);
699
700 }
701 #else
702
703 struct remember_data
704 {
705     gint count;               /* count of selected filters */
706     gboolean first_selected;  /* true if the first filter in the list is selected */
707     gboolean last_selected;   /* true if the last filter in the list is selected */
708     gboolean all_enabled;     /* true if all selected coloring rules are enabled */
709     gboolean all_disabled;    /* true if all selected coloring rules are disabled */
710     gpointer color_filters;
711 };
712 /* called for each selected row in the tree.
713 */
714 static void remember_this_row (GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpointer arg)
715 {
716     gint         *path_index;
717     color_filter_t *colorf;
718     struct remember_data *data = arg;
719
720     gtk_tree_model_get(model, iter, 5, &colorf, -1);
721     colorf->selected = TRUE;
722
723     data->all_enabled  &= (!colorf->disabled);
724     data->all_disabled &= colorf->disabled;
725
726     path_index = gtk_tree_path_get_indices(path);   /* not to be freed */
727     if (path_index == NULL)       /* can return NULL according to API doc.*/
728     {
729       return;
730     }
731     row_selected = path_index[0];
732
733     if (row_selected == 0)
734       data->first_selected = TRUE;
735     if (row_selected == num_of_filters - 1)
736       data->last_selected = TRUE;
737
738     data->count++;
739
740     gtk_tree_view_scroll_to_cell(data->color_filters, path, NULL, FALSE, 0.0, 0.0);
741 }
742
743 /* clear the selection flag of this filter */
744 static void
745 clear_select_flag(gpointer filter_arg, gpointer arg _U_)
746 {
747   color_filter_t *colorf = filter_arg;
748
749   colorf->selected = FALSE;
750 }
751
752 /* The gtk+2.0 version gets called for, (maybe multiple,) changes in the selection. */
753 static void
754 remember_selected_row(GtkTreeSelection *sel, gpointer color_filters)
755 {
756     GtkWidget    *button;
757     struct remember_data data;
758
759     data.first_selected = data.last_selected = FALSE;
760     data.all_enabled = data.all_disabled = TRUE;
761     data.count = 0;
762     data.color_filters = color_filters;
763
764     g_slist_foreach(color_filter_edit_list, clear_select_flag, NULL);
765     gtk_tree_selection_selected_foreach(sel,remember_this_row, &data);
766
767     if (data.count > 0)
768     {
769       /*
770        * One or more rows are selected, so we can operate on them.
771       */
772
773       /* We can only edit if there is exactly one filter selected */
774       button = (GtkWidget *)OBJECT_GET_DATA(color_filters, COLOR_EDIT_LB);
775       gtk_widget_set_sensitive (button, data.count == 1);
776
777       /* We can enable any number of filters */
778       button = (GtkWidget *)OBJECT_GET_DATA(color_filters, COLOR_ENABLE_LB);
779       gtk_widget_set_sensitive (button, !data.all_enabled);
780
781       /* We can disable any number of filters */
782       button = (GtkWidget *)OBJECT_GET_DATA(color_filters, COLOR_DISABLE_LB);
783       gtk_widget_set_sensitive (button, !data.all_disabled);
784
785       /* We can delete any number of filters */
786       button = (GtkWidget *)OBJECT_GET_DATA(color_filters, COLOR_DELETE_LB);
787       gtk_widget_set_sensitive (button, TRUE);
788
789       /*
790        * We can move them up *if* one of them isn't the top row,
791        * and move them down *if* one of them isn't the bottom row.
792       */
793       button = (GtkWidget *)OBJECT_GET_DATA(color_filters, COLOR_UP_LB);
794       gtk_widget_set_sensitive(button, !data.first_selected);
795       button = (GtkWidget *)OBJECT_GET_DATA(color_filters, COLOR_DOWN_LB);
796       gtk_widget_set_sensitive(button, !data.last_selected);
797     }
798     else
799     {
800       row_selected = -1;
801
802       /*
803        * No row is selected, so we can't do operations that affect the
804        * selected row.
805        */
806       if (!row_is_moving) {
807         button = (GtkWidget *)OBJECT_GET_DATA(color_filters, COLOR_UP_LB);
808         gtk_widget_set_sensitive (button, FALSE);
809         button = (GtkWidget *)OBJECT_GET_DATA(color_filters, COLOR_DOWN_LB);
810         gtk_widget_set_sensitive (button, FALSE);
811       }
812       button = (GtkWidget *)OBJECT_GET_DATA(color_filters, COLOR_EDIT_LB);
813       gtk_widget_set_sensitive (button, FALSE);
814       button = (GtkWidget *)OBJECT_GET_DATA(color_filters, COLOR_ENABLE_LB);
815       gtk_widget_set_sensitive (button, FALSE);
816       button = (GtkWidget *)OBJECT_GET_DATA(color_filters, COLOR_DISABLE_LB);
817       gtk_widget_set_sensitive (button, FALSE);
818       button = (GtkWidget *)OBJECT_GET_DATA(color_filters, COLOR_DELETE_LB);
819       gtk_widget_set_sensitive (button, FALSE);
820     }
821 }
822 #endif
823
824 #if GTK_MAJOR_VERSION < 2
825 /* A row was unselected; un-remember its row number */
826 static void
827 unremember_selected_row                 (GtkCList        *clist,
828                                          gint             row _U_,
829                                          gint             column _U_,
830                                          GdkEvent        *event _U_,
831                                          gpointer         user_data _U_)
832 {
833   GtkWidget *button;
834   color_filter_t *colorf;
835
836   row_selected = -1;
837
838   colorf = gtk_clist_get_row_data(clist, row);
839   colorf->selected = FALSE;
840
841   if (color_selected_count() == 0)
842   {
843     /*
844      * No row is selected, so we can't do operations that affect the
845      * selected row.
846      */
847     button = (GtkWidget *)OBJECT_GET_DATA(clist, COLOR_UP_LB);
848     gtk_widget_set_sensitive (button, FALSE);
849     button = (GtkWidget *)OBJECT_GET_DATA(clist, COLOR_DOWN_LB);
850     gtk_widget_set_sensitive (button, FALSE);
851     button = (GtkWidget *)OBJECT_GET_DATA(clist, COLOR_EDIT_LB);
852     gtk_widget_set_sensitive (button, FALSE);
853     button = (GtkWidget *)OBJECT_GET_DATA(clist, COLOR_ENABLE_LB);
854     gtk_widget_set_sensitive(button, FALSE);
855     button = (GtkWidget *)OBJECT_GET_DATA(clist, COLOR_DISABLE_LB);
856     gtk_widget_set_sensitive(button, FALSE);
857     button = (GtkWidget *)OBJECT_GET_DATA(clist, COLOR_DELETE_LB);
858     gtk_widget_set_sensitive(button, FALSE);
859   }
860 }
861 #endif
862
863
864
865 /* destroy a single color edit dialog */
866 static void
867 destroy_edit_dialog_cb(gpointer filter_arg, gpointer dummy _U_)
868 {
869   color_filter_t *colorf = (color_filter_t *)filter_arg;
870
871   if (colorf->edit_dialog != NULL)
872     window_destroy(colorf->edit_dialog);
873 }
874
875 /* Called when the dialog box is being destroyed; destroy any edit
876  * dialogs opened from this dialog.
877  */
878 static void
879 color_destroy_cb                       (GtkButton       *button _U_,
880                                         gpointer         user_data _U_)
881 {
882   /* Destroy any edit dialogs we have open. */
883   g_slist_foreach(color_filter_edit_list, destroy_edit_dialog_cb, NULL);
884
885   /* destroy the filter list itself */
886   color_filter_list_delete(&color_filter_edit_list);
887   color_filter_list_delete(&color_filter_tmp_list);
888
889   colorize_win = NULL;
890 }
891
892
893 static void
894 select_row(GtkWidget *color_filters, int row)
895 {
896 #if GTK_MAJOR_VERSION < 2
897 #else
898   GtkTreeModel     *model;
899   gint              num_filters;
900   GtkTreeIter       iter;
901   GtkTreeSelection *sel;
902 #endif
903
904 #if GTK_MAJOR_VERSION < 2
905   /* select the new row */
906   gtk_clist_select_row(GTK_CLIST(color_filters), row, -1);
907 #else
908   /* select the new row */
909   model = gtk_tree_view_get_model(GTK_TREE_VIEW(color_filters));
910   num_filters = gtk_tree_model_iter_n_children(model, NULL);
911   gtk_tree_model_iter_nth_child(model, &iter, NULL, row);
912   sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(color_filters));
913   gtk_tree_selection_select_iter(sel, &iter);
914 #endif
915 }
916
917
918
919 /* add a single color filter to the list */
920 static void
921 add_filter_to_list(gpointer filter_arg, gpointer list_arg)
922 {
923   color_filter_t *colorf = filter_arg;
924 #if GTK_MAJOR_VERSION < 2
925   GtkWidget      *color_filters = list_arg;
926   gchar          *data[2];
927   gint            row;
928   GdkColor        bg, fg;
929
930   /* Only add permanent coloring rules to the edit-list */
931   if( strstr(colorf->filter_name,TEMP_COLOR_PREFIX)==NULL) {
932     data[0] = colorf->filter_name;
933     data[1] = colorf->filter_text;
934     row = gtk_clist_append(GTK_CLIST(color_filters), data);
935
936     color_t_to_gdkcolor(&fg, &colorf->fg_color);
937     color_t_to_gdkcolor(&bg, &colorf->bg_color);
938
939     gtk_clist_set_row_data(GTK_CLIST(color_filters), row, colorf);
940
941     /* XXX Using light-gray on white for disabled coloring-rules is a
942      * workaround to using strikethrough as I don't know how to set
943      * text to strikethrough in GTK1. This needs to be changed to
944      * keep the GTK1 and GTK2 version simular
945      */
946     gtk_clist_set_foreground(GTK_CLIST(color_filters), row,
947               colorf->disabled ? &LTGREY : &fg);
948     gtk_clist_set_background(GTK_CLIST(color_filters), row,
949               colorf->disabled ? &WHITE : &bg);
950   #else
951     gchar           fg_str[14], bg_str[14];
952     GtkListStore   *store;
953     GtkTreeIter     iter;
954
955   if( strstr(colorf->filter_name,TEMP_COLOR_PREFIX)==NULL) {
956     store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(list_arg)));
957     gtk_list_store_append(store, &iter);
958     g_snprintf(fg_str, 14, "#%04X%04X%04X",
959             colorf->fg_color.red, colorf->fg_color.green, colorf->fg_color.blue);
960     g_snprintf(bg_str, 14, "#%04X%04X%04X",
961             colorf->bg_color.red, colorf->bg_color.green, colorf->bg_color.blue);
962     gtk_list_store_set(store, &iter, 0, colorf->filter_name,
963                        1, colorf->filter_text, 2, fg_str, 3, bg_str,
964                        4, colorf->disabled, 5, colorf, -1);
965   #endif
966     color_filter_edit_list = g_slist_append(color_filter_edit_list, colorf);
967     num_of_filters++;
968   } else {
969     /* But keep the temporary ones too, so they can be added again
970      * when the user is done editing */
971     color_filter_tmp_list = g_slist_append(color_filter_tmp_list, colorf);
972   }
973 }
974
975
976 /* a new color filter was read in from a filter file */
977 void
978 color_filter_add_cb(color_filter_t *colorf, gpointer user_data)
979 {
980   GtkWidget        *color_filters = user_data;
981
982   add_filter_to_list(colorf, color_filters);
983
984 #if GTK_MAJOR_VERSION >= 2
985   gtk_widget_grab_focus(color_filters);
986 #endif
987 }
988
989 /* Create a new filter, add it to the list, and pop up an
990    "Edit color filter" dialog box to edit it. */
991 static void
992 create_new_color_filter(GtkButton *button, const char *filter)
993 {
994   color_filter_t   *colorf;
995   GtkStyle         *style;
996   color_t          bg_color, fg_color;
997   GtkWidget        *color_filters;
998 #if GTK_MAJOR_VERSION >= 2
999   GtkTreeSelection *sel;
1000 #endif
1001
1002   color_filters = (GtkWidget *)OBJECT_GET_DATA(button, COLOR_FILTERS_CL);
1003
1004   /* unselect all filters */
1005 #if GTK_MAJOR_VERSION >= 2
1006   sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(color_filters));
1007   gtk_tree_selection_unselect_all (sel);
1008 #else
1009   gtk_clist_unselect_all (GTK_CLIST(color_filters));
1010 #endif
1011
1012   /* Use the default background and foreground colors as the colors. */
1013   style = gtk_widget_get_style(packet_list);
1014   gdkcolor_to_color_t(&bg_color, &style->base[GTK_STATE_NORMAL]);
1015   gdkcolor_to_color_t(&fg_color, &style->text[GTK_STATE_NORMAL]);
1016
1017   colorf = color_filter_new("name", filter, &bg_color, &fg_color, FALSE);
1018   add_filter_to_list(colorf, color_filters);
1019   select_row(color_filters, num_of_filters-1);
1020
1021   /* open the edit dialog */
1022   edit_color_filter_dialog(color_filters, TRUE /* is a new filter */);
1023
1024 #if GTK_MAJOR_VERSION >= 2
1025   gtk_widget_grab_focus(color_filters);
1026 #endif
1027 }
1028
1029 /* User pressed the "New" button: Create a new filter in the list,
1030    and pop up an "Edit color filter" dialog box to edit it. */
1031 static void
1032 color_new_cb(GtkButton *button, gpointer user_data _U_)
1033 {
1034   create_new_color_filter(button, "filter");
1035 }
1036
1037 /* User pressed the "Edit" button: Pop up an "Edit color filter" dialog box
1038  * to edit an existing filter. */
1039 static void
1040 color_edit_cb(GtkButton *button, gpointer user_data _U_)
1041 {
1042   GtkWidget *color_filters;
1043
1044   color_filters = (GtkWidget *)OBJECT_GET_DATA(button, COLOR_FILTERS_CL);
1045   g_assert(row_selected != -1);
1046   edit_color_filter_dialog(color_filters, FALSE /* is not a new filter */);
1047 }
1048
1049 /* User double-clicked on the coloring rule */
1050 static gint
1051 color_filters_button_cb(GtkWidget *list, GdkEventButton *event,
1052                           gpointer data _U_)
1053 {
1054   if (event->type == GDK_2BUTTON_PRESS) {
1055     edit_color_filter_dialog(list, FALSE);
1056   }
1057
1058   return FALSE;
1059 }
1060
1061 /* action_disable==TRUE  ==> User pressed the "Disable" button:
1062  *                           Disable the selected filters in the list.
1063  * action_disable==FALSE ==> User pressed the "Enable" button:
1064  *                           Enable the selected filters in the list.
1065  */
1066 static void
1067 color_disable_cb(GtkWidget *widget, gboolean action_disable)
1068 {
1069   gint filter_number;
1070   GtkWidget *button;
1071   GtkWidget * color_filters;
1072   color_filter_t *colorf;
1073 #if GTK_MAJOR_VERSION < 2
1074   GdkColor          fg, bg;
1075 #else
1076   GtkTreeIter       iter;
1077   GtkTreeModel     *model;
1078   GtkTreeSelection *sel;
1079 #endif
1080
1081   color_filters = (GtkWidget *)OBJECT_GET_DATA(widget, COLOR_FILTERS_CL);
1082
1083   for (filter_number = 0; filter_number < num_of_filters; filter_number++)
1084   {
1085 #if GTK_MAJOR_VERSION < 2
1086     colorf = gtk_clist_get_row_data(GTK_CLIST(color_filters), filter_number);
1087     if (colorf->selected) {
1088       colorf->disabled = action_disable;
1089
1090       color_t_to_gdkcolor(&fg, &colorf->fg_color);
1091       color_t_to_gdkcolor(&bg, &colorf->bg_color);
1092
1093       /* XXX Using light-gray on white for disabled coloring-rules is a
1094        * workaround to using strikethrough as I don't know how to set
1095        * text to strikethrough in GTK1. This needs to be changed to
1096        * keep the GTK1 and GTK2 version simular
1097        */
1098       gtk_clist_set_foreground(GTK_CLIST(color_filters), filter_number, 
1099               colorf->disabled ? &LTGREY : &fg);
1100       gtk_clist_set_background(GTK_CLIST(color_filters), filter_number,
1101               colorf->disabled ? &WHITE : &bg);
1102     }
1103 #else
1104     model = gtk_tree_view_get_model(GTK_TREE_VIEW(color_filters));
1105     gtk_tree_model_iter_nth_child(model, &iter, NULL, filter_number);
1106     gtk_tree_model_get(model, &iter, 5, &colorf, -1);
1107     sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(color_filters));
1108     if (gtk_tree_selection_iter_is_selected(sel, &iter)) {
1109       colorf->disabled = action_disable;
1110       gtk_list_store_set(GTK_LIST_STORE(model), &iter, 4, action_disable, -1);
1111     }
1112 #endif
1113   }
1114   button = (GtkWidget *)OBJECT_GET_DATA(color_filters, COLOR_ENABLE_LB);
1115   gtk_widget_set_sensitive(button, action_disable);
1116   button = (GtkWidget *)OBJECT_GET_DATA(color_filters, COLOR_DISABLE_LB);
1117   gtk_widget_set_sensitive(button, !action_disable);
1118 }
1119
1120 /* Delete a single color filter from the list and elsewhere. */
1121 void
1122 color_delete(gint row, GtkWidget *color_filters)
1123 {
1124     color_filter_t *colorf;
1125
1126 #if GTK_MAJOR_VERSION >= 2
1127     GtkTreeModel     *model;
1128     GtkTreeIter       iter;
1129
1130
1131     model = gtk_tree_view_get_model(GTK_TREE_VIEW(color_filters));
1132     gtk_tree_model_iter_nth_child(model, &iter, NULL, row);
1133     gtk_tree_model_get(model, &iter, 5, &colorf, -1);
1134
1135     /* Remove this color filter from the CList displaying the
1136     color filters. */
1137     gtk_list_store_remove(GTK_LIST_STORE(model), &iter);
1138     num_of_filters--;
1139
1140     /* Destroy any "Edit color filter" dialog boxes editing it. */
1141     if (colorf->edit_dialog != NULL)
1142     window_destroy(colorf->edit_dialog);
1143
1144     /* Delete the color filter from the list of color filters. */
1145     color_filter_edit_list = g_slist_remove(color_filter_edit_list, colorf);
1146     color_filter_delete(colorf);
1147
1148     /* If we grab the focus after updating the selection, the first
1149     * row is always selected, so we do it before */
1150     gtk_widget_grab_focus(color_filters);
1151 #else
1152     colorf = gtk_clist_get_row_data(GTK_CLIST(color_filters), row);
1153
1154     /* Remove this color filter from the CList displaying the
1155        color filters. */
1156     gtk_clist_remove(GTK_CLIST(color_filters), row);
1157     num_of_filters--;
1158
1159     /* Destroy any "Edit color filter" dialog boxes editing it. */
1160     if (colorf->edit_dialog != NULL)
1161         window_destroy(colorf->edit_dialog);
1162
1163     /* Delete the color filter from the list of color filters. */
1164     color_filter_edit_list = g_slist_remove(color_filter_edit_list, colorf);
1165     color_filter_delete(colorf);
1166 #endif
1167 }
1168
1169 /* User pressed the "Delete" button: Delete the selected filters from the list.*/
1170 static void
1171 color_delete_cb(GtkWidget *widget, gpointer user_data _U_)
1172 {
1173   GtkWidget  *color_filters;
1174   gint row, num_filters;
1175 #if GTK_MAJOR_VERSION < 2
1176   color_filter_t *colorf;
1177 #else
1178     GtkTreeModel     *model;
1179     GtkTreeIter       iter;
1180     GtkTreeSelection *sel;
1181 #endif
1182
1183   color_filters = (GtkWidget *)OBJECT_GET_DATA(widget, COLOR_FILTERS_CL);
1184
1185   /* get the number of filters in the list */
1186 #if GTK_MAJOR_VERSION < 2
1187   num_filters = num_of_filters;
1188 #else
1189   model = gtk_tree_view_get_model(GTK_TREE_VIEW(color_filters));
1190   num_filters = gtk_tree_model_iter_n_children(model, NULL);
1191   sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(color_filters));
1192 #endif
1193
1194   /* iterate through the list and delete the selected ones */
1195   for (row = num_filters - 1; row >= 0; row--)
1196   {
1197 #if GTK_MAJOR_VERSION < 2
1198     colorf = gtk_clist_get_row_data(GTK_CLIST(color_filters), row);
1199     if (colorf->selected)
1200       color_delete (row, color_filters);
1201 #else
1202     gtk_tree_model_iter_nth_child(model, &iter, NULL, row);
1203     if (gtk_tree_selection_iter_is_selected(sel, &iter))
1204       color_delete (row, color_filters);
1205 #endif
1206   }
1207 }
1208
1209 /* User pressed "Export": Pop up an "Export color filter" dialog box. */
1210 static void
1211 color_export_cb(GtkButton *button, gpointer data _U_)
1212 {
1213   GtkWidget        *color_filters;
1214
1215   color_filters = (GtkWidget *)OBJECT_GET_DATA(button, COLOR_FILTERS_CL);
1216
1217   file_color_export_cmd_cb(color_filters, color_filter_edit_list);
1218 }
1219
1220 /* User pressed "Import": Pop up an "Import color filter" dialog box. */
1221 static void
1222 color_import_cb(GtkButton *button, gpointer data _U_)
1223 {
1224   GtkWidget        *color_filters;
1225 #if GTK_MAJOR_VERSION >= 2
1226   GtkTreeSelection *sel;
1227 #endif
1228
1229   color_filters = (GtkWidget *)OBJECT_GET_DATA(button, COLOR_FILTERS_CL);
1230
1231 #if GTK_MAJOR_VERSION >= 2
1232   sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(color_filters));
1233   gtk_tree_selection_unselect_all (sel);
1234 #else
1235   gtk_clist_unselect_all (GTK_CLIST(color_filters));
1236 #endif
1237
1238   file_color_import_cmd_cb(color_filters, &color_filter_edit_list);
1239 }
1240
1241 /* User confirmed the clear operation: Remove all user defined color filters and
1242    revert to the global file. */
1243 static void
1244 color_clear_cmd(GtkWidget *widget)
1245 {
1246     GtkWidget * color_filters;
1247
1248     color_filters = (GtkWidget *)OBJECT_GET_DATA(widget, COLOR_FILTERS_CL);
1249
1250     while (num_of_filters > 0)
1251     {
1252         color_delete (num_of_filters-1, color_filters);
1253     }
1254
1255     /* try to read the global filters */
1256     color_filters_read_globals(color_filters);
1257 }
1258
1259 /* Clear button: user responded to question */
1260 static void color_clear_answered_cb(gpointer dialog _U_, gint btn, gpointer data)
1261 {
1262     switch(btn) {
1263     case(ESD_BTN_CLEAR):
1264         color_clear_cmd(data);
1265         break;
1266     case(ESD_BTN_CANCEL):
1267         break;
1268     default:
1269         g_assert_not_reached();
1270     }
1271 }
1272
1273 /* User pressed "clear" button: ask user before really doing it */
1274 void
1275 color_clear_cb(GtkWidget *widget, gpointer data _U_) {
1276     gpointer  dialog;
1277
1278     /* ask user, if he/she is really sure */
1279     dialog = simple_dialog(ESD_TYPE_CONFIRMATION, ESD_BTN_CLEAR | ESD_BTN_CANCEL,
1280                 PRIMARY_TEXT_START "Remove all your personal color settings?" PRIMARY_TEXT_END "\n\n"
1281                 "This will revert the color settings to global defaults.\n\n"
1282                 "Are you really sure?");
1283
1284     simple_dialog_set_cb(dialog, color_clear_answered_cb, widget);
1285 }
1286
1287
1288
1289 /* User pressed "Ok" button: Exit dialog and apply new list of
1290    color filters to the capture. */
1291 static void
1292 color_ok_cb(GtkButton *button _U_, gpointer user_data _U_)
1293 {
1294   /* Apply the new coloring rules... */
1295   color_apply_cb(button,user_data);
1296
1297   /* ... and destroy the dialog box. */
1298   window_destroy(colorize_win);
1299 }
1300
1301 /* User pressed "Apply" button: apply the new list of color filters
1302    to the capture. */
1303 static void
1304 color_apply_cb(GtkButton *button _U_, gpointer user_data _U_)
1305 {
1306   GSList *cfl;
1307
1308   /* if we don't have a Save button, just save the settings now */
1309   if (!prefs.gui_use_pref_save) {
1310       if (!color_filters_write(color_filter_edit_list))
1311             simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
1312                 "Could not open filter file: %s", strerror(errno));
1313   }
1314
1315   /* merge the temporary coloring filters with the ones that just
1316    * have been edited and apply them both */
1317   cfl = g_slist_concat(color_filter_tmp_list, color_filter_edit_list);
1318   color_filters_apply(cfl);
1319
1320   /* colorize list */
1321   cf_colorize_packets(&cfile);
1322 }
1323
1324 /* User pressed the "Save" button: save the color filters to the
1325    color filter file. */
1326 static void
1327 color_save_cb(GtkButton *button _U_, gpointer user_data _U_)
1328 {
1329
1330   if (!color_filters_write(color_filter_edit_list))
1331         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
1332             "Could not open filter file: %s", strerror(errno));
1333 }
1334
1335 /* User pressed "Cancel" button (or "ESC" or the 'X'):
1336    Exit dialog without colorizing packets with the new list. */
1337 static void
1338 color_cancel_cb(GtkWidget *widget _U_, gpointer user_data _U_)
1339 {
1340   /* Destroy the dialog box. */
1341   window_destroy(colorize_win);
1342 }
1343