fix doxygen generation
[obnox/wireshark/wip.git] / gtk / stream_prefs.c
1 /* stream_prefs.c
2  * Dialog boxes for preferences for the stream window
3  *
4  * $Id$
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@ethereal.com>
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 "color.h"
32 #include "colors.h"
33 #include "globals.h"
34 #include "stream_prefs.h"
35 #include "keys.h"
36 #include "print.h"
37 #include <epan/prefs.h>
38 #include "compat_macros.h"
39 #include "follow_dlg.h"
40 #include "packet_list.h"
41
42 #define SAMPLE_MARKED_TEXT "Sample marked packet text\n"
43 #define SAMPLE_CLIENT_TEXT "Sample TCP stream client text\n"
44 #define SAMPLE_SERVER_TEXT "Sample TCP stream server text\n"
45 #define MFG_IDX 0
46 #define MBG_IDX 1
47 #define CFG_IDX 2
48 #define CBG_IDX 3
49 #define SFG_IDX 4
50 #define SBG_IDX 5
51 #define MAX_IDX 6 /* set this to the number of IDX values */
52 #define STREAM_SAMPLE_KEY "stream_entry"
53 #define STREAM_CS_KEY "stream_colorselection"
54 #define CS_RED 0
55 #define CS_GREEN 1
56 #define CS_BLUE 2
57 #define CS_OPACITY 3
58
59 static void update_text_color(GtkWidget *, gpointer);
60 static void update_current_color(GtkWidget *, gpointer);
61
62 static GdkColor tcolors[MAX_IDX], *curcolor = NULL;
63
64 GtkWidget *
65 stream_prefs_show()
66 {
67   GtkWidget *main_vb, *main_tb, *label, *optmenu, *menu, *menuitem;
68   GtkWidget *sample, *colorsel;
69   int        width, height, i;
70   gchar     *mt[] = { "Marked packet foreground", "Marked packet background",
71                       "TCP stream client foreground", "TCP stream client background",
72                       "TCP stream server foreground", "TCP stream server background" };
73   int mcount = sizeof(mt) / sizeof (gchar *);
74 #if GTK_MAJOR_VERSION < 2
75   gdouble scolor[4];
76 #else
77   GtkTextBuffer *buf;
78   GtkTextIter    iter;
79   PangoLayout   *layout;
80 #endif
81
82   color_t_to_gdkcolor(&tcolors[MFG_IDX], &prefs.gui_marked_fg);
83   color_t_to_gdkcolor(&tcolors[MBG_IDX], &prefs.gui_marked_bg);
84   color_t_to_gdkcolor(&tcolors[CFG_IDX], &prefs.st_client_fg);
85   color_t_to_gdkcolor(&tcolors[CBG_IDX], &prefs.st_client_bg);
86   color_t_to_gdkcolor(&tcolors[SFG_IDX], &prefs.st_server_fg);
87   color_t_to_gdkcolor(&tcolors[SBG_IDX], &prefs.st_server_bg);
88
89   curcolor = &tcolors[CFG_IDX];
90
91 #if GTK_MAJOR_VERSION < 2
92   scolor[CS_RED]     = (gdouble) (curcolor->red)   / 65535.0;
93   scolor[CS_GREEN]   = (gdouble) (curcolor->green) / 65535.0;
94   scolor[CS_BLUE]    = (gdouble) (curcolor->blue)  / 65535.0;
95   scolor[CS_OPACITY] = 1.0;
96 #endif
97
98   /* Enclosing containers for each row of widgets */
99   main_vb = gtk_vbox_new(FALSE, 5);
100   gtk_container_border_width(GTK_CONTAINER(main_vb), 5);
101
102   main_tb = gtk_table_new(3, 3, FALSE);
103   gtk_box_pack_start(GTK_BOX(main_vb), main_tb, FALSE, FALSE, 0);
104   gtk_table_set_row_spacings(GTK_TABLE(main_tb), 10);
105   gtk_table_set_col_spacings(GTK_TABLE(main_tb), 15);
106   gtk_widget_show(main_tb);
107
108   label = gtk_label_new("Set:");
109   gtk_misc_set_alignment(GTK_MISC(label), 1.0, 0.5);
110   gtk_table_attach_defaults(GTK_TABLE(main_tb), label, 0, 1, 0, 1);
111   gtk_widget_show(label);
112
113   /* We have to create this now, and configure it below. */
114   colorsel = gtk_color_selection_new();
115
116   optmenu = gtk_option_menu_new ();
117   menu = gtk_menu_new ();
118   for (i = 0; i < mcount; i++){
119     menuitem = gtk_menu_item_new_with_label (mt[i]);
120     OBJECT_SET_DATA(menuitem, STREAM_CS_KEY, colorsel);
121     SIGNAL_CONNECT(menuitem, "activate", update_current_color, &tcolors[i]);
122     gtk_widget_show (menuitem);
123     gtk_menu_append (GTK_MENU (menu), menuitem);
124   }
125   gtk_option_menu_set_menu (GTK_OPTION_MENU (optmenu), menu);
126   gtk_table_attach(GTK_TABLE(main_tb), optmenu, 1, 2, 0, 1, GTK_SHRINK, GTK_SHRINK, 0, 0);
127   gtk_widget_show(optmenu);
128
129 #if GTK_MAJOR_VERSION < 2
130   sample = gtk_text_new(FALSE, FALSE);
131   height = (mcount/2+1) * (sample->style->font->ascent + sample->style->font->descent);
132   width = gdk_string_width(sample->style->font, SAMPLE_SERVER_TEXT);
133   WIDGET_SET_SIZE(sample, width, height);
134   gtk_text_set_editable(GTK_TEXT(sample), FALSE);
135   gtk_text_insert(GTK_TEXT(sample), NULL, &tcolors[MFG_IDX], &tcolors[MBG_IDX],
136                   SAMPLE_MARKED_TEXT, -1);
137   gtk_text_insert(GTK_TEXT(sample), NULL, &tcolors[CFG_IDX], &tcolors[CBG_IDX],
138                   SAMPLE_CLIENT_TEXT, -1);
139   gtk_text_insert(GTK_TEXT(sample), NULL, &tcolors[SFG_IDX], &tcolors[SBG_IDX],
140                   SAMPLE_SERVER_TEXT, -1);
141 #else
142   sample = gtk_text_view_new();
143   layout = gtk_widget_create_pango_layout(sample, SAMPLE_SERVER_TEXT);
144   pango_layout_get_pixel_size(layout, &width, &height);
145   g_object_unref(G_OBJECT(layout));
146   WIDGET_SET_SIZE(sample, width, height * 2);
147   gtk_text_view_set_editable(GTK_TEXT_VIEW(sample), FALSE);
148   buf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(sample));
149   gtk_text_buffer_get_start_iter(buf, &iter);
150   gtk_text_buffer_create_tag(buf, "marked",
151                              "foreground-gdk", &tcolors[MFG_IDX],
152                              "background-gdk", &tcolors[MBG_IDX], NULL);
153   gtk_text_buffer_create_tag(buf, "client",
154                              "foreground-gdk", &tcolors[CFG_IDX],
155                              "background-gdk", &tcolors[CBG_IDX], NULL);
156   gtk_text_buffer_create_tag(buf, "server",
157                              "foreground-gdk", &tcolors[SFG_IDX],
158                              "background-gdk", &tcolors[SBG_IDX], NULL);
159   gtk_text_buffer_insert_with_tags_by_name(buf, &iter, SAMPLE_MARKED_TEXT, -1,
160                                            "marked", NULL);
161   gtk_text_buffer_insert_with_tags_by_name(buf, &iter, SAMPLE_CLIENT_TEXT, -1,
162                                            "client", NULL);
163   gtk_text_buffer_insert_with_tags_by_name(buf, &iter, SAMPLE_SERVER_TEXT, -1,
164                                            "server", NULL);
165 #endif
166   gtk_table_attach_defaults(GTK_TABLE(main_tb), sample, 2, 3, 0, 2);
167   gtk_widget_show(sample);
168
169 #if GTK_MAJOR_VERSION < 2
170   gtk_color_selection_set_color(GTK_COLOR_SELECTION(colorsel), &scolor[CS_RED]);
171 #else
172   gtk_color_selection_set_current_color(GTK_COLOR_SELECTION(colorsel),
173                                         curcolor);
174 #endif
175   gtk_table_attach(GTK_TABLE(main_tb), colorsel, 0, 3, 2, 3,
176                   GTK_SHRINK, GTK_SHRINK, 0, 0);
177
178   OBJECT_SET_DATA(colorsel, STREAM_SAMPLE_KEY, sample);
179   SIGNAL_CONNECT(colorsel, "color-changed", update_text_color, NULL);
180   gtk_widget_show(colorsel);
181
182   gtk_widget_show(main_vb);
183   return(main_vb);
184 }
185
186 static void
187 update_text_color(GtkWidget *w, gpointer data _U_) {
188 #if GTK_MAJOR_VERSION < 2
189   GtkText  *sample   = OBJECT_GET_DATA(w, STREAM_SAMPLE_KEY);
190   gdouble   scolor[4];
191 #else
192   GtkTextView *sample = OBJECT_GET_DATA(w, STREAM_SAMPLE_KEY);
193   GtkTextBuffer *buf;
194   GtkTextTag    *tag;
195 #endif
196
197 #if GTK_MAJOR_VERSION < 2
198   gtk_color_selection_get_color(GTK_COLOR_SELECTION(w), &scolor[CS_RED]);
199
200   curcolor->red   = (gushort) (scolor[CS_RED]   * 65535.0);
201   curcolor->green = (gushort) (scolor[CS_GREEN] * 65535.0);
202   curcolor->blue  = (gushort) (scolor[CS_BLUE]  * 65535.0);
203
204   gtk_text_freeze(sample);
205   gtk_text_set_point(sample, 0);
206   gtk_text_forward_delete(sample, gtk_text_get_length(sample));
207   gtk_text_insert(sample, NULL, &tcolors[MFG_IDX], &tcolors[MBG_IDX],
208     SAMPLE_MARKED_TEXT, -1);
209   gtk_text_insert(sample, NULL, &tcolors[CFG_IDX], &tcolors[CBG_IDX],
210     SAMPLE_CLIENT_TEXT, -1);
211   gtk_text_insert(sample, NULL, &tcolors[SFG_IDX], &tcolors[SBG_IDX],
212     SAMPLE_SERVER_TEXT, -1);
213   gtk_text_thaw(sample);
214 #else
215   gtk_color_selection_get_current_color(GTK_COLOR_SELECTION(w), curcolor);
216
217   buf = gtk_text_view_get_buffer(sample);
218   tag = gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(buf), "marked");
219   g_object_set(tag, "foreground-gdk", &tcolors[MFG_IDX], "background-gdk",
220                &tcolors[MBG_IDX], NULL);
221   tag = gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(buf), "client");
222   g_object_set(tag, "foreground-gdk", &tcolors[CFG_IDX], "background-gdk",
223                &tcolors[CBG_IDX], NULL);
224   tag = gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(buf), "server");
225   g_object_set(tag, "foreground-gdk", &tcolors[SFG_IDX], "background-gdk",
226                &tcolors[SBG_IDX], NULL);
227 #endif
228 }
229
230 static void
231 update_current_color(GtkWidget *w, gpointer data)
232 {
233   GtkColorSelection *colorsel;
234 #if GTK_MAJOR_VERSION < 2
235   gdouble            scolor[4];
236 #endif
237
238   colorsel = GTK_COLOR_SELECTION(OBJECT_GET_DATA(w, STREAM_CS_KEY));
239   curcolor = (GdkColor *) data;
240
241 #if GTK_MAJOR_VERSION < 2
242   scolor[CS_RED]     = (gdouble) (curcolor->red)   / 65535.0;
243   scolor[CS_GREEN]   = (gdouble) (curcolor->green) / 65535.0;
244   scolor[CS_BLUE]    = (gdouble) (curcolor->blue)  / 65535.0;
245   scolor[CS_OPACITY] = 1.0;
246
247   gtk_color_selection_set_color(colorsel, &scolor[CS_RED]);
248 #else
249   gtk_color_selection_set_current_color(colorsel, curcolor);
250 #endif
251 }
252
253 void
254 stream_prefs_fetch(GtkWidget *w _U_)
255 {
256   gdkcolor_to_color_t(&prefs.gui_marked_fg, &tcolors[MFG_IDX]);
257   gdkcolor_to_color_t(&prefs.gui_marked_bg, &tcolors[MBG_IDX]);
258   gdkcolor_to_color_t(&prefs.st_client_fg, &tcolors[CFG_IDX]);
259   gdkcolor_to_color_t(&prefs.st_client_bg, &tcolors[CBG_IDX]);
260   gdkcolor_to_color_t(&prefs.st_server_fg, &tcolors[SFG_IDX]);
261   gdkcolor_to_color_t(&prefs.st_server_bg, &tcolors[SBG_IDX]);
262 }
263
264 void
265 stream_prefs_apply(GtkWidget *w _U_)
266 {
267         follow_redraw_all();
268
269         update_marked_frames();
270 }
271
272 void
273 stream_prefs_destroy(GtkWidget *w _U_)
274 {
275 }