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