6762b142efe867dcba03afce89290542a5913f36
[obnox/wireshark/wip.git] / gtk / color_edit_dlg.c
1 /* color_edit_dlg.c
2  * Definitions for single color filter edit dialog boxes
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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <string.h>
30
31 #include <gtk/gtk.h>
32
33 #include <epan/packet.h>
34
35 #include "../color.h"
36 #include "../color_filters.h"
37 #include "../simple_dialog.h"
38
39 #include "gtk/color_utils.h"
40 #include "gtk/dlg_utils.h"
41 #include "gtk/gui_utils.h"
42 #include "gtk/stock_icons.h"
43 #include "gtk/filter_dlg.h"
44 #include "gtk/dfilter_expr_dlg.h"
45 #include "gtk/color_edit_dlg.h"
46
47
48 #define BUTTON_SIZE_X -1
49 #define BUTTON_SIZE_Y -1
50
51
52 static void edit_color_filter_destroy_cb(GObject *object, gpointer user_data);
53 static void edit_color_filter_fg_cb(GtkButton *button, gpointer user_data);
54 static void edit_color_filter_bg_cb(GtkButton *button, gpointer user_data);
55 /*
56 static void edit_disabled_cb_cb(GtkButton *button, gpointer user_data);
57 */
58 static void edit_color_filter_ok_cb(GtkButton *button, gpointer user_data);
59 static void edit_new_color_filter_cancel_cb(GtkButton *button, gpointer user_data);
60
61 static GtkWidget* color_sel_win_new(color_filter_t *colorf, gboolean);
62 static void color_sel_ok_cb(GtkButton *button, gpointer user_data);
63 static void color_sel_cancel_cb(GtkObject *object, gpointer user_data);
64
65
66 #define COLOR_FILTERS_CL        "color_filters_cl"
67 #define COLOR_FILTER            "color_filter"
68 #define COLOR_FILTER_NAME_TE    "color_filter_name_te"
69 #define COLOR_FILTER_TEXT_TE    "color_filter_text_te"
70 #define COLOR_SELECTION_FG      "color_selection_fg"
71 #define COLOR_SELECTION_BG      "color_selection_bg"
72 #define COLOR_SELECTION_PARENT  "color_selection_parent"
73
74 /* XXX - we don't forbid having more than one "Edit color filter" dialog
75    open, so these shouldn't be global. */
76 static GtkWidget *filt_name_entry;
77 static GtkWidget *filt_text_entry;
78 static GtkWidget *disabled_cb;
79
80
81 static void
82 filter_expr_cb(GtkWidget *w _U_, gpointer filter_te)
83 {
84
85         dfilter_expr_dlg_new(GTK_WIDGET(filter_te));
86 }
87
88
89 /* Create an "Edit Color Filter" dialog for a given color filter, and
90    associate it with that color filter. */
91 void
92 edit_color_filter_dialog(GtkWidget *color_filters,
93                          gboolean is_new_filter)
94 {
95     color_filter_t *colorf;
96     GtkWidget      *edit_dialog;
97     GtkWidget      *dialog_vbox;
98     GtkTooltips    *tooltips;
99     GtkStyle       *style;
100
101     GtkWidget *filter_fr;
102     GtkWidget *filter_fr_vbox;
103     GtkWidget *filter_name_hbox;
104     GtkWidget *color_filter_name;
105     GtkWidget *filter_string_hbox;
106     GtkWidget *add_expression_bt;
107     GtkWidget *color_filter_text;
108
109     GtkWidget *settings_hbox;
110
111     GtkWidget *colorize_fr;
112     GtkWidget *colorize_hbox;
113     GtkWidget *colorize_filter_fg;
114     GtkWidget *colorize_filter_bg;
115
116     GtkWidget *status_fr;
117     GtkWidget *status_vbox;
118
119     GtkWidget *bbox;
120     GtkWidget *edit_color_filter_ok;
121     GtkWidget *edit_color_filter_cancel;
122
123     GtkTreeModel     *model;
124     GtkTreeIter       iter;
125
126     model = gtk_tree_view_get_model(GTK_TREE_VIEW(color_filters));
127
128     gtk_tree_model_iter_nth_child(model, &iter, NULL, row_selected);
129     gtk_tree_model_get(model, &iter, 5, &colorf, -1);
130
131     if (colorf->edit_dialog != NULL) {
132         /* There's already an edit box open for this filter; reactivate it. */
133         reactivate_window(colorf->edit_dialog);
134         return;
135     }
136
137     tooltips = gtk_tooltips_new ();
138
139     /* dialog window */
140     edit_dialog = dlg_conf_window_new ("Wireshark: Edit Color Filter");
141     gtk_window_set_default_size(GTK_WINDOW(edit_dialog), 500, -1);
142     g_object_set_data(G_OBJECT(edit_dialog), "edit_dialog", edit_dialog);
143     colorf->edit_dialog = edit_dialog;
144
145     dialog_vbox = gtk_vbox_new (FALSE, 0);
146     gtk_container_set_border_width  (GTK_CONTAINER (dialog_vbox), 5);
147     gtk_container_add (GTK_CONTAINER (edit_dialog), dialog_vbox);
148
149     /* Filter frame */
150     filter_fr = gtk_frame_new("Filter");
151     gtk_box_pack_start (GTK_BOX (dialog_vbox), filter_fr, FALSE, FALSE, 0);
152
153     filter_fr_vbox = gtk_vbox_new (FALSE, 0);
154     gtk_container_set_border_width  (GTK_CONTAINER (filter_fr_vbox), 5);
155     gtk_container_add(GTK_CONTAINER(filter_fr), filter_fr_vbox);
156
157     /* filter name hbox */
158     filter_name_hbox = gtk_hbox_new (FALSE, 0);
159     gtk_box_pack_start (GTK_BOX (filter_fr_vbox), filter_name_hbox, TRUE, FALSE, 3);
160
161     color_filter_name = gtk_label_new (("Name: "));
162     gtk_box_pack_start (GTK_BOX (filter_name_hbox), color_filter_name, FALSE, FALSE, 0);
163
164     filt_name_entry = gtk_entry_new ();
165     gtk_entry_set_text(GTK_ENTRY(filt_name_entry), colorf->filter_name);
166
167     style = gtk_style_copy(gtk_widget_get_style(filt_name_entry));
168     color_t_to_gdkcolor(&style->base[GTK_STATE_NORMAL], &colorf->bg_color);
169     color_t_to_gdkcolor(&style->text[GTK_STATE_NORMAL], &colorf->fg_color);
170     gtk_widget_set_style(filt_name_entry, style);
171
172     gtk_box_pack_start (GTK_BOX (filter_name_hbox), filt_name_entry, TRUE, TRUE, 0);
173     gtk_tooltips_set_tip (tooltips, filt_name_entry, ("This is the editable name of the filter. (No @ characters allowed.)"), NULL);
174
175
176     /* filter string hbox */
177     filter_string_hbox = gtk_hbox_new (FALSE, 0);
178     gtk_box_pack_start (GTK_BOX (filter_fr_vbox), filter_string_hbox, TRUE, FALSE, 3);
179
180     color_filter_text = gtk_label_new (("String: "));
181     gtk_box_pack_start (GTK_BOX (filter_string_hbox), color_filter_text, FALSE, FALSE, 0);
182
183     filt_text_entry = gtk_entry_new ();
184     g_signal_connect(filt_text_entry, "changed", G_CALLBACK(filter_te_syntax_check_cb), NULL);
185     gtk_entry_set_text(GTK_ENTRY(filt_text_entry), colorf->filter_text);
186
187     gtk_style_unref(style);
188     gtk_box_pack_start (GTK_BOX (filter_string_hbox), filt_text_entry, TRUE, TRUE, 0);
189     gtk_tooltips_set_tip (tooltips, filt_text_entry, ("This is the editable text of the filter"), NULL);
190
191     /* Create the "Add Expression..." button, to pop up a dialog
192        for constructing filter comparison expressions. */
193     add_expression_bt = gtk_button_new_from_stock(WIRESHARK_STOCK_ADD_EXPRESSION);
194     g_signal_connect(add_expression_bt, "clicked", G_CALLBACK(filter_expr_cb), filt_text_entry);
195     gtk_box_pack_start (GTK_BOX(filter_string_hbox), add_expression_bt, FALSE, FALSE, 3);
196     gtk_tooltips_set_tip (tooltips, add_expression_bt, ("Add an expression to the filter string"), NULL);
197
198     /* Show the (in)validity of the default filter string */
199     filter_te_syntax_check_cb(filt_text_entry);
200
201     /* settings-hbox for "choose color frame" and "status frame" */
202     settings_hbox = gtk_hbox_new (FALSE, 0);
203     gtk_box_pack_start (GTK_BOX (dialog_vbox), settings_hbox, FALSE, FALSE, 0);
204
205     /* choose color frame */
206     colorize_fr = gtk_frame_new("Display Colors");
207     gtk_box_pack_start (GTK_BOX (settings_hbox), colorize_fr, TRUE, TRUE, 0);
208
209     colorize_hbox = gtk_hbox_new (FALSE, 0);
210     gtk_container_set_border_width  (GTK_CONTAINER (colorize_hbox), 5);
211     gtk_container_add(GTK_CONTAINER(colorize_fr), colorize_hbox);
212
213     colorize_filter_fg = gtk_button_new_with_label (("Foreground Color..."));
214     gtk_box_pack_start (GTK_BOX (colorize_hbox), colorize_filter_fg, TRUE, FALSE, 0);
215     gtk_tooltips_set_tip (tooltips, colorize_filter_fg, ("Select foreground color for data display"), NULL);
216
217     colorize_filter_bg = gtk_button_new_with_label (("Background Color..."));
218     gtk_box_pack_start (GTK_BOX (colorize_hbox), colorize_filter_bg, TRUE, FALSE, 0);
219     gtk_tooltips_set_tip (tooltips, colorize_filter_bg, ("Select background color for data display"), NULL);
220
221     /* status frame */
222     status_fr = gtk_frame_new("Status");
223     gtk_box_pack_start (GTK_BOX (settings_hbox), status_fr, TRUE, TRUE, 0);
224
225     status_vbox = gtk_vbox_new (FALSE, 0);
226     gtk_container_set_border_width  (GTK_CONTAINER (status_vbox), 5);
227     gtk_container_add(GTK_CONTAINER(status_fr), status_vbox);
228
229     disabled_cb = gtk_check_button_new_with_label("Disabled");
230     gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(disabled_cb), colorf->disabled);
231     gtk_box_pack_start (GTK_BOX (status_vbox), disabled_cb, TRUE, FALSE, 0);
232     gtk_tooltips_set_tip (tooltips, disabled_cb, ("Color rule won't be checked if this box is selected"), NULL);
233     
234     /* button box */
235     bbox = dlg_button_row_new(GTK_STOCK_OK, GTK_STOCK_CANCEL, NULL);
236         gtk_box_pack_start(GTK_BOX(dialog_vbox), bbox, FALSE, FALSE, 0);
237     gtk_container_set_border_width  (GTK_CONTAINER (bbox), 0);
238
239     edit_color_filter_ok = g_object_get_data(G_OBJECT(bbox), GTK_STOCK_OK);
240     gtk_tooltips_set_tip (tooltips, edit_color_filter_ok, ("Accept filter color change"), NULL);
241
242     edit_color_filter_cancel = g_object_get_data(G_OBJECT(bbox), GTK_STOCK_CANCEL);
243     gtk_tooltips_set_tip (tooltips, edit_color_filter_cancel, ("Reject filter color change"), NULL);
244
245     gtk_widget_grab_default(edit_color_filter_ok);
246
247     /* signals and such */
248     g_object_set_data(G_OBJECT(edit_dialog), COLOR_FILTER, colorf);
249     g_signal_connect(edit_dialog, "destroy", G_CALLBACK(edit_color_filter_destroy_cb), NULL);
250     g_object_set_data(G_OBJECT(colorize_filter_fg), COLOR_FILTER, colorf);
251     g_signal_connect(colorize_filter_fg, "clicked", G_CALLBACK(edit_color_filter_fg_cb), NULL);
252     g_object_set_data(G_OBJECT(colorize_filter_bg), COLOR_FILTER, colorf);
253     g_signal_connect(colorize_filter_bg, "clicked", G_CALLBACK(edit_color_filter_bg_cb), NULL);
254     g_object_set_data(G_OBJECT(disabled_cb), COLOR_FILTER, colorf);
255 /*    g_signal_connect(disabled_cb, "clicked", edit_disabled_cb_cb, NULL);*/
256     g_object_set_data(G_OBJECT(edit_color_filter_ok), COLOR_FILTERS_CL, color_filters);
257     g_object_set_data(G_OBJECT(edit_color_filter_ok), COLOR_FILTER, colorf);
258     g_signal_connect(edit_color_filter_ok, "clicked", G_CALLBACK(edit_color_filter_ok_cb), edit_dialog);
259
260     /* set callback to delete new filters if cancel chosen */
261     if (is_new_filter)
262     {
263         g_object_set_data(G_OBJECT(edit_color_filter_cancel), COLOR_FILTERS_CL, color_filters);
264         g_signal_connect(edit_color_filter_cancel, "clicked",
265                        G_CALLBACK(edit_new_color_filter_cancel_cb), edit_dialog);
266     }
267     /* escape will select cancel */
268     window_set_cancel_button(edit_dialog, edit_color_filter_cancel, window_cancel_button_cb);
269
270     g_signal_connect(edit_dialog, "delete_event", G_CALLBACK(window_delete_event_cb), NULL);
271
272     gtk_widget_show_all(edit_dialog);
273     window_present(edit_dialog);
274 }
275
276 /* Called when the dialog box is being destroyed; destroy any color
277    selection dialogs opened from this dialog, and null out the pointer
278    to this dialog. */
279 static void
280 edit_color_filter_destroy_cb(GObject *object, gpointer user_data _U_)
281 {
282   color_filter_t *colorf;
283   GtkWidget *color_sel;
284
285   colorf = (color_filter_t *)g_object_get_data(G_OBJECT(object), COLOR_FILTER);
286   colorf->edit_dialog = NULL;
287
288   /* Destroy any color selection dialogs this dialog had open. */
289   color_sel = (GtkWidget *)g_object_get_data(G_OBJECT(object), COLOR_SELECTION_FG);
290   if (color_sel != NULL)
291     window_destroy(color_sel);
292   color_sel = (GtkWidget *)g_object_get_data(G_OBJECT(object), COLOR_SELECTION_BG);
293   if (color_sel != NULL)
294     window_destroy(color_sel);
295 }
296
297 /* Pop up a color selection box to choose the foreground color. */
298 static void
299 edit_color_filter_fg_cb(GtkButton *button, gpointer user_data _U_)
300 {
301   color_filter_t *colorf;
302   GtkWidget *color_selection_fg;
303
304   colorf = (color_filter_t *)g_object_get_data(G_OBJECT(button), COLOR_FILTER);
305   /* Do we already have one open for this dialog? */
306   color_selection_fg = g_object_get_data(G_OBJECT(colorf->edit_dialog), COLOR_SELECTION_FG);
307   if (color_selection_fg != NULL) {
308     /* Yes.  Just reactivate it. */
309     reactivate_window(color_selection_fg);
310   } else {
311     /* No.  Create a new color selection box, and associate it with
312        this dialog. */
313     color_selection_fg = color_sel_win_new(colorf, FALSE);
314     g_object_set_data(G_OBJECT(colorf->edit_dialog), COLOR_SELECTION_FG, color_selection_fg);
315     g_object_set_data(G_OBJECT(color_selection_fg), COLOR_SELECTION_PARENT, colorf->edit_dialog);
316   }
317 }
318
319 /* Pop up a color selection box to choose the background color. */
320 static void
321 edit_color_filter_bg_cb                (GtkButton       *button,
322                                         gpointer         user_data _U_)
323 {
324   color_filter_t *colorf;
325   GtkWidget *color_selection_bg;
326
327   colorf = (color_filter_t *)g_object_get_data(G_OBJECT(button), COLOR_FILTER);
328   /* Do we already have one open for this dialog? */
329   color_selection_bg = g_object_get_data(G_OBJECT(colorf->edit_dialog), COLOR_SELECTION_BG);
330   if (color_selection_bg != NULL) {
331     /* Yes.  Just reactivate it. */
332     reactivate_window(color_selection_bg);
333   } else {
334     /* No.  Create a new color selection box, and associate it with
335        this dialog. */
336     color_selection_bg = color_sel_win_new(colorf, TRUE);
337     g_object_set_data(G_OBJECT(colorf->edit_dialog), COLOR_SELECTION_BG, color_selection_bg);
338     g_object_set_data(G_OBJECT(color_selection_bg), COLOR_SELECTION_PARENT, colorf->edit_dialog);
339   }
340 }
341
342 /* Toggle the disabled flag */
343 /*
344 static void
345 edit_disabled_cb_cb                    (GtkButton       *button,
346                                         gpointer         user_data _U_)
347 {
348   color_filter_t *colorf;
349
350   colorf = (color_filter_t *)g_object_get_data(G_OBJECT(button), COLOR_FILTER);
351   colorf->disabled = GTK_TOGGLE_BUTTON (button)->active;
352
353   printf("Colorfilter %s is now %s\n",colorf->filter_name,colorf->disabled?"disabled":"enabled");
354 }
355 */
356
357 /* accept color (and potential content) change */
358 static void
359 edit_color_filter_ok_cb                (GtkButton       *button,
360                                         gpointer         user_data)
361 {
362     GtkWidget      *dialog;
363     GtkStyle       *style;
364     GdkColor        new_fg_color;
365     GdkColor        new_bg_color;
366     gchar          *filter_name;
367     gchar          *filter_text;
368     gboolean        filter_disabled;
369     color_filter_t *colorf;
370     dfilter_t      *compiled_filter;
371     GtkWidget      *color_filters;
372     GtkTreeModel   *model;
373     GtkTreeIter     iter;
374     gchar           fg_str[14], bg_str[14];
375
376     dialog = (GtkWidget *)user_data;
377
378     style = gtk_widget_get_style(filt_name_entry);
379     new_bg_color = style->base[GTK_STATE_NORMAL];
380     new_fg_color = style->text[GTK_STATE_NORMAL];
381
382     filter_name = g_strdup(gtk_entry_get_text(GTK_ENTRY(filt_name_entry)));
383     filter_text = g_strdup(gtk_entry_get_text(GTK_ENTRY(filt_text_entry)));
384     filter_disabled = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(disabled_cb));
385
386     if(strchr(filter_name,'@') || strchr(filter_text,'@')){
387         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
388                       "Filter names and strings must not"
389                       " use the '@' character. Filter unchanged.");
390         g_free(filter_name);
391         g_free(filter_text);
392         return;
393     }
394
395     if(!dfilter_compile(filter_text, &compiled_filter)) {
396         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
397                       "Filter \"%s\" didn't compile correctly.\n"
398                       " Please try again. Filter unchanged.\n%s\n", filter_name,
399                       dfilter_error_msg);
400     } else {
401         color_filters = (GtkWidget *)g_object_get_data(G_OBJECT(button), COLOR_FILTERS_CL);
402         colorf = (color_filter_t *)g_object_get_data(G_OBJECT(button), COLOR_FILTER);
403
404         if (colorf->filter_name != NULL)
405             g_free(colorf->filter_name);
406         colorf->filter_name = filter_name;
407         if (colorf->filter_text != NULL)
408             g_free(colorf->filter_text);
409         colorf->filter_text = filter_text;
410         colorf->disabled = filter_disabled;
411         gdkcolor_to_color_t(&colorf->fg_color, &new_fg_color);
412         gdkcolor_to_color_t(&colorf->bg_color, &new_bg_color);
413         g_snprintf(fg_str, 14, "#%04X%04X%04X",
414                 new_fg_color.red, new_fg_color.green, new_fg_color.blue);
415         g_snprintf(bg_str, 14, "#%04X%04X%04X",
416                 new_bg_color.red, new_bg_color.green, new_bg_color.blue);
417         model = gtk_tree_view_get_model(GTK_TREE_VIEW(color_filters));
418         gtk_tree_model_iter_nth_child(model, &iter, NULL, row_selected);
419         gtk_list_store_set(GTK_LIST_STORE(model), &iter, 0, filter_name,
420                            1, filter_text, 2, fg_str, 3, bg_str,
421                            4, filter_disabled, -1);
422         if(colorf->c_colorfilter != NULL)
423             dfilter_free(colorf->c_colorfilter);
424         colorf->c_colorfilter = compiled_filter;
425
426         /* Destroy the dialog box. */
427         window_destroy(dialog);
428     }
429 }
430
431 /* reject new color filter addition */
432 static void
433 edit_new_color_filter_cancel_cb(GtkButton *button, gpointer user_data _U_)
434 {
435     /* Delete the entry. As a side effect this destroys the edit_dialog window. */
436     color_delete(num_of_filters-1, (GtkWidget*)g_object_get_data(G_OBJECT(button), COLOR_FILTERS_CL));
437 }
438
439 static GtkWidget*
440 color_sel_win_new(color_filter_t *colorf, gboolean is_bg)
441 {
442   gchar *title;
443   GtkWidget *color_sel_win;
444   color_t   *color;
445   GdkColor   gcolor;
446   GtkWidget *color_sel_ok;
447   GtkWidget *color_sel_cancel;
448   GtkWidget *color_sel_help;
449
450   if (is_bg) {
451     color = &colorf->bg_color;
452     title = g_strdup_printf("Wireshark: Choose background color for \"%s\"",
453         colorf->filter_name);
454   } else {
455     color = &colorf->fg_color;
456     title = g_strdup_printf("Wireshark: Choose foreground color for \"%s\"",
457         colorf->filter_name);
458   }
459   color_sel_win = gtk_color_selection_dialog_new(title);
460   g_free(title);
461   g_object_set_data(G_OBJECT(color_sel_win), "color_sel_win", color_sel_win);
462   gtk_container_set_border_width (GTK_CONTAINER (color_sel_win), 10);
463
464   if (color != NULL) {
465     color_t_to_gdkcolor(&gcolor, color);
466     gtk_color_selection_set_current_color(
467                     GTK_COLOR_SELECTION(
468                             GTK_COLOR_SELECTION_DIALOG(color_sel_win)->colorsel), &gcolor);
469   }
470
471   color_sel_ok = GTK_COLOR_SELECTION_DIALOG (color_sel_win)->ok_button;
472   g_object_set_data(G_OBJECT(color_sel_win), "color_sel_ok", color_sel_ok);
473   GTK_WIDGET_SET_FLAGS (color_sel_ok, GTK_CAN_DEFAULT);
474
475   color_sel_cancel = GTK_COLOR_SELECTION_DIALOG (color_sel_win)->cancel_button;
476   g_object_set_data(G_OBJECT(color_sel_win), "color_sel_cancel", color_sel_cancel);
477   GTK_WIDGET_SET_FLAGS (color_sel_cancel, GTK_CAN_DEFAULT);
478   window_set_cancel_button(color_sel_win, color_sel_cancel, NULL); /* ensure esc does req'd local cxl action.    */
479                                                                    /* esc as handled by the                      */
480                                                                    /* gtk_color_selection_dialog widget          */
481                                                                    /*  doesn't result in this happening.         */
482
483   color_sel_help = GTK_COLOR_SELECTION_DIALOG (color_sel_win)->help_button;
484   g_object_set_data(G_OBJECT(color_sel_win), "color_sel_help", color_sel_help);
485
486
487   GTK_WIDGET_SET_FLAGS (color_sel_help, GTK_CAN_DEFAULT);
488
489   g_signal_connect(color_sel_ok, "clicked", G_CALLBACK(color_sel_ok_cb), color_sel_win);
490   g_signal_connect(color_sel_cancel, "clicked", G_CALLBACK(color_sel_cancel_cb), color_sel_win);
491
492   gtk_widget_show_all(color_sel_win);
493   return color_sel_win;
494 }
495
496 static void
497 color_sel_win_destroy(GtkWidget *sel_win)
498 {
499   GtkWidget *parent;
500   GtkWidget *color_selection_fg, *color_selection_bg;
501
502   /* Find the "Edit color filter" dialog box with which this is associated. */
503   parent = (GtkWidget *)g_object_get_data(G_OBJECT(sel_win), COLOR_SELECTION_PARENT);
504
505   /* Find that dialog box's foreground and background color selection
506      boxes, if any. */
507   color_selection_fg = g_object_get_data(G_OBJECT(parent), COLOR_SELECTION_FG);
508   color_selection_bg = g_object_get_data(G_OBJECT(parent), COLOR_SELECTION_BG);
509
510   if (sel_win == color_selection_fg) {
511     /* This was its foreground color selection box; it isn't, anymore. */
512     g_object_set_data(G_OBJECT(parent), COLOR_SELECTION_FG, NULL);
513   }
514   if (sel_win == color_selection_bg) {
515     /* This was its background color selection box; it isn't, anymore. */
516     g_object_set_data(G_OBJECT(parent), COLOR_SELECTION_BG, NULL);
517   }
518
519   /* Now destroy it. */
520   window_destroy(sel_win);
521 }
522
523 /* Retrieve selected color */
524 static void
525 color_sel_ok_cb                        (GtkButton       *button _U_,
526                                         gpointer         user_data)
527 {
528   GdkColor new_color; /* Color from color selection dialog */
529   GtkWidget *color_dialog;
530   GtkStyle  *style;
531   GtkWidget *parent;
532   GtkWidget *color_selection_fg, *color_selection_bg;
533   gboolean is_bg;
534
535   color_dialog = (GtkWidget *)user_data;
536
537   gtk_color_selection_get_current_color(GTK_COLOR_SELECTION(
538    GTK_COLOR_SELECTION_DIALOG(color_dialog)->colorsel), &new_color);
539
540   if ( ! get_color(&new_color) ){
541         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
542                       "Could not allocate color.  Try again.");
543   } else {
544         /* Find the "Edit color filter" dialog box with which this is
545            associated. */
546         parent = (GtkWidget *)g_object_get_data(G_OBJECT(color_dialog), COLOR_SELECTION_PARENT);
547
548         /* Find that dialog box's foreground and background color selection
549            boxes, if any. */
550         color_selection_fg = g_object_get_data(G_OBJECT(parent), COLOR_SELECTION_FG);
551         color_selection_bg = g_object_get_data(G_OBJECT(parent), COLOR_SELECTION_BG);
552         is_bg = (color_dialog == color_selection_bg);
553
554         color_sel_win_destroy(color_dialog);
555
556         /* now apply the change to the fore/background */
557
558         style = gtk_style_copy(gtk_widget_get_style(filt_name_entry));
559         if (is_bg)
560           style->base[GTK_STATE_NORMAL] = new_color;
561         else
562           style->text[GTK_STATE_NORMAL] = new_color;
563
564         gtk_widget_set_style(filt_name_entry, style);
565         gtk_style_unref(style);
566   }
567 }
568
569 /* Don't choose the selected color as the foreground or background
570    color for the filter. */
571 static void
572 color_sel_cancel_cb                    (GtkObject       *object _U_,
573                                         gpointer         user_data)
574 {
575   GtkWidget *color_dialog;
576   color_dialog = (GtkWidget *)user_data;
577   /* nothing to change here.  Just get rid of the dialog box. */
578
579   color_sel_win_destroy(color_dialog);
580 }