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