From Jakub Zawadzki via bug 4273:
[obnox/wireshark/wip.git] / gtk / prefs_stream.c
1 /* stream_prefs.c
2  * Dialog boxes for preferences for the stream window
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 <epan/prefs.h>
32
33 #include "../color.h"
34 #include "../globals.h"
35 #include "../print.h"
36
37 #include "gtk/color_utils.h"
38 #include "gtk/prefs_stream.h"
39 #include "gtk/keys.h"
40 #include "gtk/follow_tcp.h"
41 #include "gtk/main_packet_list.h"
42
43
44 #define SAMPLE_MARKED_TEXT  "Sample marked packet text\n"
45 #define SAMPLE_IGNORED_TEXT "Sample ignored packet text\n"
46 #define SAMPLE_CLIENT_TEXT  "Sample TCP stream client text\n"
47 #define SAMPLE_SERVER_TEXT  "Sample TCP stream server text\n"
48 #define MFG_IDX 0
49 #define MBG_IDX 1
50 #define IFG_IDX 2
51 #define IBG_IDX 3
52 #define CFG_IDX 4
53 #define CBG_IDX 5
54 #define SFG_IDX 6
55 #define SBG_IDX 7
56 #define MAX_IDX 8 /* set this to the number of IDX values */
57 #define STREAM_SAMPLE_KEY "stream_entry"
58 #define STREAM_CS_KEY "stream_colorselection"
59 #define CS_RED 0
60 #define CS_GREEN 1
61 #define CS_BLUE 2
62 #define CS_OPACITY 3
63
64 static void update_text_color(GtkWidget *, gpointer);
65 static void update_current_color(GtkWidget *, gpointer);
66
67 static GdkColor tcolors[MAX_IDX], *curcolor = NULL;
68
69 GtkWidget *
70 stream_prefs_show()
71 {
72   GtkWidget *main_vb, *main_tb, *label, *combo_box;
73   GtkWidget *sample, *colorsel;
74   int        width, height, i;
75   const gchar     *mt[] = { 
76           "Marked packet foreground",           /* MFG_IDX 0*/
77           "Marked packet background",           /* MBG_IDX 1*/
78           "Ignored packet foreground",          /* IFG_IDX 2*/
79           "Ignored packet background",          /* IBG_IDX 3*/
80           "TCP stream client foreground",       /* CFG_IDX 4*/
81           "TCP stream client background",       /* CBG_IDX 5*/
82           "TCP stream server foreground",       /* SFG_IDX 6*/
83           "TCP stream server background"        /* SBG_IDX 7*/
84   };
85   int mcount = sizeof(mt) / sizeof (gchar *);
86   GtkTextBuffer *buf;
87   GtkTextIter    iter;
88   PangoLayout   *layout;
89
90   color_t_to_gdkcolor(&tcolors[MFG_IDX], &prefs.gui_marked_fg);
91   color_t_to_gdkcolor(&tcolors[MBG_IDX], &prefs.gui_marked_bg);
92   color_t_to_gdkcolor(&tcolors[IFG_IDX], &prefs.gui_ignored_fg);
93   color_t_to_gdkcolor(&tcolors[IBG_IDX], &prefs.gui_ignored_bg);
94   color_t_to_gdkcolor(&tcolors[CFG_IDX], &prefs.st_client_fg);
95   color_t_to_gdkcolor(&tcolors[CBG_IDX], &prefs.st_client_bg);
96   color_t_to_gdkcolor(&tcolors[SFG_IDX], &prefs.st_server_fg);
97   color_t_to_gdkcolor(&tcolors[SBG_IDX], &prefs.st_server_bg);
98
99   curcolor = &tcolors[CFG_IDX];
100
101   /* Enclosing containers for each row of widgets */
102   main_vb = gtk_vbox_new(FALSE, 5);
103   gtk_container_set_border_width(GTK_CONTAINER(main_vb), 5);
104
105   main_tb = gtk_table_new(3, 3, FALSE);
106   gtk_box_pack_start(GTK_BOX(main_vb), main_tb, FALSE, FALSE, 0);
107   gtk_table_set_row_spacings(GTK_TABLE(main_tb), 10);
108   gtk_table_set_col_spacings(GTK_TABLE(main_tb), 15);
109   gtk_widget_show(main_tb);
110
111   label = gtk_label_new("Set:");
112   gtk_misc_set_alignment(GTK_MISC(label), 1.0f, 0.5f);
113   gtk_table_attach_defaults(GTK_TABLE(main_tb), label, 0, 1, 0, 1);
114   gtk_widget_show(label);
115
116   /* We have to create this now, and configure it below. */
117   colorsel = gtk_color_selection_new();
118
119   combo_box = gtk_combo_box_new_text ();
120   for (i = 0; i < mcount; i++){
121           gtk_combo_box_append_text (GTK_COMBO_BOX (combo_box), mt[i]);
122   }
123   gtk_combo_box_set_active(GTK_COMBO_BOX(combo_box), CFG_IDX);
124   g_signal_connect(combo_box, "changed", G_CALLBACK(update_current_color), colorsel);
125   gtk_table_attach(GTK_TABLE(main_tb), combo_box, 1, 2, 0, 1, GTK_SHRINK, GTK_SHRINK, 0, 0);
126
127   gtk_widget_show(combo_box);
128
129   sample = gtk_text_view_new();
130   layout = gtk_widget_create_pango_layout(sample, SAMPLE_SERVER_TEXT);
131   pango_layout_get_pixel_size(layout, &width, &height);
132   g_object_unref(G_OBJECT(layout));
133   gtk_widget_set_size_request(sample, width, height * 2);
134   gtk_text_view_set_editable(GTK_TEXT_VIEW(sample), FALSE);
135   buf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(sample));
136   gtk_text_buffer_get_start_iter(buf, &iter);
137   gtk_text_buffer_create_tag(buf, "marked",
138                              "foreground-gdk", &tcolors[MFG_IDX],
139                              "background-gdk", &tcolors[MBG_IDX], NULL);
140   gtk_text_buffer_create_tag(buf, "ignored",
141                              "foreground-gdk", &tcolors[IFG_IDX],
142                              "background-gdk", &tcolors[IBG_IDX], NULL);
143   gtk_text_buffer_create_tag(buf, "client",
144                              "foreground-gdk", &tcolors[CFG_IDX],
145                              "background-gdk", &tcolors[CBG_IDX], NULL);
146   gtk_text_buffer_create_tag(buf, "server",
147                              "foreground-gdk", &tcolors[SFG_IDX],
148                              "background-gdk", &tcolors[SBG_IDX], NULL);
149   gtk_text_buffer_insert_with_tags_by_name(buf, &iter, SAMPLE_MARKED_TEXT, -1,
150                                            "marked", NULL);
151   gtk_text_buffer_insert_with_tags_by_name(buf, &iter, SAMPLE_IGNORED_TEXT, -1,
152                                            "ignored", NULL);
153   gtk_text_buffer_insert_with_tags_by_name(buf, &iter, SAMPLE_CLIENT_TEXT, -1,
154                                            "client", NULL);
155   gtk_text_buffer_insert_with_tags_by_name(buf, &iter, SAMPLE_SERVER_TEXT, -1,
156                                            "server", NULL);
157   gtk_table_attach_defaults(GTK_TABLE(main_tb), sample, 2, 3, 0, 2);
158   gtk_widget_show(sample);
159
160   gtk_color_selection_set_current_color(GTK_COLOR_SELECTION(colorsel),
161                                         curcolor);
162   gtk_table_attach(GTK_TABLE(main_tb), colorsel, 0, 3, 2, 3,
163                   GTK_SHRINK, GTK_SHRINK, 0, 0);
164
165   g_object_set_data(G_OBJECT(colorsel), STREAM_SAMPLE_KEY, sample);
166   g_signal_connect(colorsel, "color-changed", G_CALLBACK(update_text_color), NULL);
167   gtk_widget_show(colorsel);
168
169   gtk_widget_show(main_vb);
170   return(main_vb);
171 }
172
173 static void
174 update_text_color(GtkWidget *w, gpointer data _U_) {
175   GtkTextView *sample = g_object_get_data(G_OBJECT(w), STREAM_SAMPLE_KEY);
176   GtkTextBuffer *buf;
177   GtkTextTag    *tag;
178
179   gtk_color_selection_get_current_color(GTK_COLOR_SELECTION(w), curcolor);
180
181   buf = gtk_text_view_get_buffer(sample);
182   tag = gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(buf), "marked");
183   g_object_set(tag, "foreground-gdk", &tcolors[MFG_IDX], "background-gdk",
184                &tcolors[MBG_IDX], NULL);
185   tag = gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(buf), "ignored");
186   g_object_set(tag, "foreground-gdk", &tcolors[IFG_IDX], "background-gdk",
187                &tcolors[IBG_IDX], NULL);
188   tag = gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(buf), "client");
189   g_object_set(tag, "foreground-gdk", &tcolors[CFG_IDX], "background-gdk",
190                &tcolors[CBG_IDX], NULL);
191   tag = gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(buf), "server");
192   g_object_set(tag, "foreground-gdk", &tcolors[SFG_IDX], "background-gdk",
193                &tcolors[SBG_IDX], NULL);
194 }
195
196 static void
197 update_current_color(GtkWidget *combo_box, gpointer data)
198 {
199   GtkColorSelection *colorsel = data;
200   int i;
201
202   i = gtk_combo_box_get_active (GTK_COMBO_BOX(combo_box));
203   curcolor = &tcolors[i];
204
205   gtk_color_selection_set_current_color(colorsel, curcolor);
206 }
207
208 void
209 stream_prefs_fetch(GtkWidget *w _U_)
210 {
211   gdkcolor_to_color_t(&prefs.gui_marked_fg, &tcolors[MFG_IDX]);
212   gdkcolor_to_color_t(&prefs.gui_marked_bg, &tcolors[MBG_IDX]);
213   gdkcolor_to_color_t(&prefs.gui_ignored_fg, &tcolors[IFG_IDX]);
214   gdkcolor_to_color_t(&prefs.gui_ignored_bg, &tcolors[IBG_IDX]);
215   gdkcolor_to_color_t(&prefs.st_client_fg, &tcolors[CFG_IDX]);
216   gdkcolor_to_color_t(&prefs.st_client_bg, &tcolors[CBG_IDX]);
217   gdkcolor_to_color_t(&prefs.st_server_fg, &tcolors[SFG_IDX]);
218   gdkcolor_to_color_t(&prefs.st_server_bg, &tcolors[SBG_IDX]);
219 }
220
221 void
222 stream_prefs_apply(GtkWidget *w _U_)
223 {
224         follow_tcp_redraw_all();
225
226 #ifndef NEW_PACKET_LIST
227         packet_list_update_marked_frames();
228         packet_list_update_ignored_frames();
229 #endif
230 }
231
232 void
233 stream_prefs_destroy(GtkWidget *w _U_)
234 {
235 }