1 /* supported_protos_dlg.c
3 * Laurent Deniel <laurent.deniel@free.fr>
7 * Wireshark - Network traffic analyzer
8 * By Gerald Combs <gerald@wireshark.org>
9 * Copyright 2000 Gerald Combs
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
33 #include "supported_protos_dlg.h"
34 #include <epan/prefs.h>
36 #include "gtkglobals.h"
37 #include "gui_utils.h"
38 #include "compat_macros.h"
39 #include "dlg_utils.h"
40 #include "font_utils.h"
44 static const char *proto_supported =
45 "The following %d protocols (and packet types) are currently\n"
46 "supported by Wireshark:\n\n";
48 static const char *dfilter_supported =
49 "The following per-protocol fields are currently supported by\n"
50 "Wireshark and can be used in display filters:\n";
59 static void supported_destroy_cb(GtkWidget *w, gpointer data);
60 static void insert_text(GtkWidget *w, const char *buffer, int nchars);
61 static void set_supported_text(GtkWidget *w, supported_type_t type);
64 * Keep a static pointer to the current "Supported" window, if any, so that
65 * if somebody tries to do "Help->Supported" while there's already a
66 * "Supported" window up, we just pop up the existing one, rather than
69 static GtkWidget *supported_w = NULL;
72 * Keep static pointers to the text widgets as well (for text format changes).
74 static GtkWidget *proto_text, *dfilter_text;
78 void supported_cb(GtkWidget *w _U_, gpointer data _U_)
81 GtkWidget *main_vb, *bbox, *supported_nb, *ok_bt, *label, *txt_scrollw,
83 #if GTK_MAJOR_VERSION < 2
84 *dfilter_tb, *dfilter_vsb;
89 if (supported_w != NULL) {
90 /* There's already a "Supported" dialog box; reactivate it. */
91 reactivate_window(supported_w);
95 supported_w = window_new(GTK_WINDOW_TOPLEVEL, "Wireshark: Supported Protocols");
96 gtk_window_set_default_size(GTK_WINDOW(supported_w), DEF_WIDTH * 2/3, DEF_HEIGHT * 2/3);
97 gtk_container_border_width(GTK_CONTAINER(supported_w), 2);
99 /* Container for each row of widgets */
100 main_vb = gtk_vbox_new(FALSE, 1);
101 gtk_container_border_width(GTK_CONTAINER(main_vb), 1);
102 gtk_container_add(GTK_CONTAINER(supported_w), main_vb);
103 gtk_widget_show(main_vb);
105 /* supported topics container */
106 supported_nb = gtk_notebook_new();
107 gtk_container_add(GTK_CONTAINER(main_vb), supported_nb);
110 /* humm, gtk 1.2 does not support horizontal scrollbar for text widgets */
113 proto_vb = gtk_vbox_new(FALSE, 0);
114 gtk_container_border_width(GTK_CONTAINER(proto_vb), 1);
116 txt_scrollw = scrolled_window_new(NULL, NULL);
117 #if GTK_MAJOR_VERSION >= 2
118 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(txt_scrollw),
121 gtk_box_pack_start(GTK_BOX(proto_vb), txt_scrollw, TRUE, TRUE, 0);
122 #if GTK_MAJOR_VERSION < 2
123 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(txt_scrollw),
126 proto_text = gtk_text_new(NULL, NULL);
127 gtk_text_set_editable(GTK_TEXT(proto_text), FALSE);
128 gtk_text_set_line_wrap(GTK_TEXT(proto_text), FALSE);
129 set_supported_text(proto_text, PROTOCOL_SUPPORTED);
130 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(txt_scrollw),
133 proto_text = gtk_text_view_new();
134 gtk_text_view_set_editable(GTK_TEXT_VIEW(proto_text), FALSE);
135 set_supported_text(proto_text, PROTOCOL_SUPPORTED);
136 gtk_container_add(GTK_CONTAINER(txt_scrollw), proto_text);
138 gtk_widget_show(txt_scrollw);
139 gtk_widget_show(proto_text);
140 gtk_widget_show(proto_vb);
141 label = gtk_label_new("Protocols");
142 gtk_notebook_append_page(GTK_NOTEBOOK(supported_nb), proto_vb, label);
144 /* display filter fields */
145 #if GTK_MAJOR_VERSION < 2
146 /* X windows have a maximum size of 32767. Since the height can easily
147 exceed this, we have to jump through some hoops to have a functional
148 vertical scroll bar. */
150 dfilter_tb = gtk_table_new(2, 2, FALSE);
151 gtk_table_set_col_spacing (GTK_TABLE (dfilter_tb), 0, 3);
152 gtk_table_set_row_spacing (GTK_TABLE (dfilter_tb), 0, 3);
153 gtk_container_border_width(GTK_CONTAINER(dfilter_tb), 1);
155 txt_scrollw = scrolled_window_new(NULL, NULL);
156 #if GTK_MAJOR_VERSION >= 2
157 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(txt_scrollw),
160 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(txt_scrollw),
163 dfilter_text = gtk_text_new(NULL, NULL);
164 dfilter_vsb = gtk_vscrollbar_new(GTK_TEXT(dfilter_text)->vadj);
165 if (prefs.gui_scrollbar_on_right) {
166 gtk_table_attach (GTK_TABLE (dfilter_tb), txt_scrollw, 0, 1, 0, 1,
167 GTK_EXPAND | GTK_SHRINK | GTK_FILL,
168 GTK_EXPAND | GTK_SHRINK | GTK_FILL, 0, 0);
169 gtk_table_attach (GTK_TABLE (dfilter_tb), dfilter_vsb, 1, 2, 0, 1,
170 GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
172 gtk_table_attach (GTK_TABLE (dfilter_tb), txt_scrollw, 1, 2, 0, 1,
173 GTK_EXPAND | GTK_SHRINK | GTK_FILL,
174 GTK_EXPAND | GTK_SHRINK | GTK_FILL, 0, 0);
175 gtk_table_attach (GTK_TABLE (dfilter_tb), dfilter_vsb, 0, 1, 0, 1,
176 GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
178 gtk_text_set_editable(GTK_TEXT(dfilter_text), FALSE);
179 gtk_text_set_line_wrap(GTK_TEXT(dfilter_text), FALSE);
180 set_supported_text(dfilter_text, DFILTER_SUPPORTED);
181 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(txt_scrollw),
184 dfilter_vb = gtk_vbox_new(FALSE, 0);
185 gtk_container_border_width(GTK_CONTAINER(dfilter_vb), 1);
187 txt_scrollw = scrolled_window_new(NULL, NULL);
188 #if GTK_MAJOR_VERSION >= 2
189 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(txt_scrollw),
192 gtk_box_pack_start(GTK_BOX(dfilter_vb), txt_scrollw, TRUE, TRUE, 0);
193 dfilter_text = gtk_text_view_new();
194 if (prefs.gui_scrollbar_on_right) {
195 gtk_scrolled_window_set_placement(GTK_SCROLLED_WINDOW(txt_scrollw),
196 GTK_CORNER_TOP_LEFT);
199 gtk_scrolled_window_set_placement(GTK_SCROLLED_WINDOW(txt_scrollw),
200 GTK_CORNER_TOP_RIGHT);
202 gtk_text_view_set_editable(GTK_TEXT_VIEW(dfilter_text), FALSE);
203 set_supported_text(dfilter_text, DFILTER_SUPPORTED);
204 gtk_container_add(GTK_CONTAINER(txt_scrollw), dfilter_text);
206 gtk_widget_show(txt_scrollw);
207 gtk_widget_show(dfilter_text);
208 #if GTK_MAJOR_VERSION < 2
209 gtk_widget_show(dfilter_tb);
210 gtk_widget_show(dfilter_vsb);
212 gtk_widget_show(dfilter_vb);
214 label = gtk_label_new("Display Filter Fields");
215 #if GTK_MAJOR_VERSION < 2
216 gtk_notebook_append_page(GTK_NOTEBOOK(supported_nb), dfilter_tb, label);
218 gtk_notebook_append_page(GTK_NOTEBOOK(supported_nb), dfilter_vb, label);
221 /* XXX add other panels here ... */
223 gtk_widget_show(supported_nb);
226 bbox = dlg_button_row_new(GTK_STOCK_OK, NULL);
227 gtk_box_pack_end(GTK_BOX(main_vb), bbox, FALSE, FALSE, 0);
228 gtk_widget_show(bbox);
230 ok_bt = OBJECT_GET_DATA(bbox, GTK_STOCK_OK);
231 window_set_cancel_button(supported_w, ok_bt, window_cancel_button_cb);
233 gtk_quit_add_destroy(gtk_main_level(), GTK_OBJECT(supported_w));
235 SIGNAL_CONNECT(supported_w, "delete_event", window_delete_event_cb, NULL);
236 SIGNAL_CONNECT(supported_w, "destroy", supported_destroy_cb, NULL);
238 gtk_widget_show(supported_w);
239 window_present(supported_w);
242 static void supported_destroy_cb(GtkWidget *w _U_, gpointer data _U_)
244 /* Note that we no longer have a Help window. */
248 static void insert_text(GtkWidget *w, const char *buffer, int nchars)
250 #if GTK_MAJOR_VERSION < 2
251 gtk_text_insert(GTK_TEXT(w), user_font_get_regular(), NULL, NULL, buffer, nchars);
253 GtkTextBuffer *buf= gtk_text_view_get_buffer(GTK_TEXT_VIEW(w));
256 gtk_text_buffer_get_end_iter(buf, &iter);
257 gtk_widget_modify_font(w, user_font_get_regular());
258 if (!g_utf8_validate(buffer, -1, NULL))
259 printf("Invalid utf8 encoding: %s\n", buffer);
260 gtk_text_buffer_insert(buf, &iter, buffer, nchars);
265 static void set_supported_text(GtkWidget *w, supported_type_t type)
268 #define BUFF_LEN 4096
270 char buffer[BUFF_LEN];
271 header_field_info *hfinfo;
272 int i, len, maxlen = 0, maxlen2 = 0, maxlen4 = 0;
273 #if GTK_MAJOR_VERSION < 2
274 int maxlen3 = 0, nb_lines = 0;
277 const char *type_name;
278 void *cookie, *cookie2;
279 protocol_t *protocol;
280 const char *name, *short_name, *filter_name;
281 int namel = 0, short_namel = 0, filter_namel = 0;
287 * the width and height computations are performed to make the
288 * horizontal scrollbar work in gtk1.2. This is only necessary for the
289 * PROTOCOL_SUPPORTED and DFILTER_SUPPORTED windows since all others should
290 * not have any horizontal scrollbar (line wrapping enabled).
294 #if GTK_MAJOR_VERSION < 2
295 gtk_text_freeze(GTK_TEXT(w));
300 case PROTOCOL_SUPPORTED :
301 /* first pass to know the maximum length of first field */
303 for (i = proto_get_first_protocol(&cookie); i != -1;
304 i = proto_get_next_protocol(&cookie)) {
306 protocol = find_protocol_by_id(i);
307 name = proto_get_protocol_name(i);
308 short_name = proto_get_protocol_short_name(protocol);
309 filter_name = proto_get_protocol_filter_name(i);
310 if ((len = strlen(name)) > namel)
312 if ((len = strlen(short_name)) > short_namel)
314 if ((len = strlen(filter_name)) > filter_namel)
317 maxlen = namel + short_namel + filter_namel;
319 len = g_snprintf(buffer, BUFF_LEN, proto_supported, count);
320 #if GTK_MAJOR_VERSION < 2
322 width = gdk_string_width(user_font_get_regular(), buffer);
323 insert_text(w, buffer, maxlen2);
325 insert_text(w, buffer, len);
328 /* ok, display the correctly aligned strings */
329 for (i = proto_get_first_protocol(&cookie); i != -1;
330 i = proto_get_next_protocol(&cookie)) {
331 protocol = find_protocol_by_id(i);
332 name = proto_get_protocol_name(i);
333 short_name = proto_get_protocol_short_name(protocol);
334 filter_name = proto_get_protocol_filter_name(i);
336 /* the name used for sorting in the left column */
337 len = g_snprintf(buffer, BUFF_LEN, "%*s %*s %*s\n",
338 -short_namel, short_name,
340 -filter_namel, filter_name);
341 #if GTK_MAJOR_VERSION < 2
344 if ((len = gdk_string_width(user_font_get_regular(), buffer)) > width)
347 insert_text(w, buffer, strlen(buffer));
350 insert_text(w, buffer, strlen(buffer));
354 #if GTK_MAJOR_VERSION < 2
355 height = (3 + nb_lines) * user_font_get_regular_height();
356 WIDGET_SET_SIZE(w, 20 + width, 20 + height);
360 case DFILTER_SUPPORTED :
362 /* XXX we should display hinfo->blurb instead of name (if not empty) */
364 /* first pass to know the maximum length of first and second fields */
365 for (i = proto_get_first_protocol(&cookie); i != -1;
366 i = proto_get_next_protocol(&cookie)) {
368 for (hfinfo = proto_get_first_protocol_field(i, &cookie2); hfinfo != NULL;
369 hfinfo = proto_get_next_protocol_field(&cookie2)) {
371 if (hfinfo->same_name_prev != NULL) /* ignore duplicate names */
374 if ((len = strlen(hfinfo->abbrev)) > maxlen)
376 if ((len = strlen(hfinfo->name)) > maxlen2)
378 if (hfinfo->blurb != NULL) {
379 if ((len = strlen(hfinfo->blurb)) > maxlen4)
385 #if GTK_MAJOR_VERSION < 2
386 maxlen3 = strlen(dfilter_supported);
387 width = gdk_string_width(user_font_get_regular(), dfilter_supported);
388 insert_text(w, dfilter_supported, maxlen3);
390 insert_text(w, dfilter_supported, strlen(dfilter_supported));
394 for (i = proto_get_first_protocol(&cookie); i != -1;
395 i = proto_get_next_protocol(&cookie)) {
396 protocol = find_protocol_by_id(i);
397 name = proto_get_protocol_name(i);
398 short_name = proto_get_protocol_short_name(protocol);
399 filter_name = proto_get_protocol_filter_name(i);
402 for (hfinfo = proto_get_first_protocol_field(i, &cookie2); hfinfo != NULL;
403 hfinfo = proto_get_next_protocol_field(&cookie2)) {
405 if (hfinfo->same_name_prev != NULL) /* ignore duplicate names */
411 len = g_snprintf(buffer, BUFF_LEN, "\n%s - %s (%s) [%d fields]:\n",
412 short_name, name, filter_name, count);
413 insert_text(w, buffer, len);
415 for (hfinfo = proto_get_first_protocol_field(i, &cookie2); hfinfo != NULL;
416 hfinfo = proto_get_next_protocol_field(&cookie2)) {
418 if (hfinfo->same_name_prev != NULL) /* ignore duplicate names */
421 type_name = ftype_pretty_name(hfinfo->type);
422 if (hfinfo->blurb != NULL && hfinfo->blurb[0] != '\0') {
423 len = g_snprintf(buffer, BUFF_LEN, "%*s %*s %*s (%s)\n",
424 -maxlen, hfinfo->abbrev,
425 -maxlen2, hfinfo->name,
426 -maxlen4, hfinfo->blurb,
429 len = g_snprintf(buffer, BUFF_LEN, "%*s %*s (%s)\n",
430 -maxlen, hfinfo->abbrev,
431 -maxlen2, hfinfo->name,
434 #if GTK_MAJOR_VERSION < 2
437 if ((len = gdk_string_width(user_font_get_regular(), buffer)) > width)
440 insert_text(w, buffer, strlen(buffer));
443 insert_text(w, buffer, strlen(buffer));
447 len = g_snprintf(buffer, BUFF_LEN, "\n-- Total %d fields\n", fcount);
448 insert_text(w, buffer, len);
450 #if GTK_MAJOR_VERSION < 2
451 height = (5 + nb_lines) * user_font_get_regular_height();
452 WIDGET_SET_SIZE(w, 20 + width, 20 + height);
456 g_assert_not_reached();
459 #if GTK_MAJOR_VERSION < 2
460 gtk_text_thaw(GTK_TEXT(w));
462 } /* set_supported_text */
465 static void clear_supported_text(GtkWidget *w)
467 #if GTK_MAJOR_VERSION < 2
468 GtkText *txt = GTK_TEXT(w);
470 gtk_text_set_point(txt, 0);
471 /* Keep GTK+ 1.2.3 through 1.2.6 from dumping core - see
472 http://www.ethereal.com/lists/ethereal-dev/199912/msg00312.html and
473 http://www.gnome.org/mailing-lists/archives/gtk-devel-list/1999-October/0051.shtml
474 for more information */
475 gtk_adjustment_set_value(txt->vadj, 0.0);
476 gtk_text_forward_delete(txt, gtk_text_get_length(txt));
478 GtkTextBuffer *buf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(w));
480 gtk_text_buffer_set_text(buf, "", 0);
485 /* Redraw all the text widgets, to use a new font. */
486 void supported_redraw(void)
488 if (supported_w != NULL) {
489 #if GTK_MAJOR_VERSION < 2
490 gtk_text_freeze(GTK_TEXT(proto_text));
492 clear_supported_text(proto_text);
493 set_supported_text(proto_text, PROTOCOL_SUPPORTED);
494 #if GTK_MAJOR_VERSION < 2
495 gtk_text_thaw(GTK_TEXT(proto_text));
497 gtk_text_freeze(GTK_TEXT(dfilter_text));
499 clear_supported_text(dfilter_text);
500 set_supported_text(dfilter_text, DFILTER_SUPPORTED);
501 #if GTK_MAJOR_VERSION < 2
502 gtk_text_thaw(GTK_TEXT(dfilter_text));