1 /* supported_protos_dlg.c
3 * Laurent Deniel <laurent.deniel@free.fr>
5 * $Id: supported_protos_dlg.c,v 1.11 2004/05/23 23:24:06 ulfl Exp $
7 * Ethereal - Network traffic analyzer
8 * By Gerald Combs <gerald@ethereal.com>
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"
36 #include "gtkglobals.h"
38 #include "compat_macros.h"
39 #include "dlg_utils.h"
43 static const char *proto_supported =
44 "The following %d protocols (and packet types) are currently\n"
45 "supported by Ethereal:\n\n";
47 static const char *dfilter_supported =
48 "The following per-protocol fields are currently supported by\n"
49 "Ethereal and can be used in display filters:\n";
58 static void supported_destroy_cb(GtkWidget *w, gpointer data);
59 static void insert_text(GtkWidget *w, const char *buffer, int nchars);
60 static void set_supported_text(GtkWidget *w, supported_type_t type);
63 * Keep a static pointer to the current "Supported" window, if any, so that
64 * if somebody tries to do "Help->Supported" while there's already a
65 * "Supported" window up, we just pop up the existing one, rather than
68 static GtkWidget *supported_w = NULL;
71 * Keep static pointers to the text widgets as well (for text format changes).
73 static GtkWidget *proto_text, *dfilter_text;
77 void supported_cb(GtkWidget *w _U_, gpointer data _U_)
80 GtkWidget *main_vb, *bbox, *supported_nb, *ok_bt, *label, *txt_scrollw,
82 #if GTK_MAJOR_VERSION < 2
83 *dfilter_tb, *dfilter_vsb;
88 if (supported_w != NULL) {
89 /* There's already a "Supported" dialog box; reactivate it. */
90 reactivate_window(supported_w);
94 supported_w = window_new(GTK_WINDOW_TOPLEVEL, "Ethereal: Supported Protocols");
95 gtk_window_set_default_size(GTK_WINDOW(supported_w), DEF_WIDTH * 2/3, DEF_HEIGHT * 2/3);
96 gtk_container_border_width(GTK_CONTAINER(supported_w), 2);
98 /* Container for each row of widgets */
99 main_vb = gtk_vbox_new(FALSE, 1);
100 gtk_container_border_width(GTK_CONTAINER(main_vb), 1);
101 gtk_container_add(GTK_CONTAINER(supported_w), main_vb);
102 gtk_widget_show(main_vb);
104 /* supported topics container */
105 supported_nb = gtk_notebook_new();
106 gtk_container_add(GTK_CONTAINER(main_vb), supported_nb);
109 /* humm, gtk 1.2 does not support horizontal scrollbar for text widgets */
112 proto_vb = gtk_vbox_new(FALSE, 0);
113 gtk_container_border_width(GTK_CONTAINER(proto_vb), 1);
115 txt_scrollw = scrolled_window_new(NULL, NULL);
116 #if GTK_MAJOR_VERSION >= 2
117 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(txt_scrollw),
120 gtk_box_pack_start(GTK_BOX(proto_vb), txt_scrollw, TRUE, TRUE, 0);
121 #if GTK_MAJOR_VERSION < 2
122 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(txt_scrollw),
125 proto_text = gtk_text_new(NULL, NULL);
126 gtk_text_set_editable(GTK_TEXT(proto_text), FALSE);
127 gtk_text_set_line_wrap(GTK_TEXT(proto_text), FALSE);
128 set_supported_text(proto_text, PROTOCOL_SUPPORTED);
129 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(txt_scrollw),
132 proto_text = gtk_text_view_new();
133 gtk_text_view_set_editable(GTK_TEXT_VIEW(proto_text), FALSE);
134 set_supported_text(proto_text, PROTOCOL_SUPPORTED);
135 gtk_container_add(GTK_CONTAINER(txt_scrollw), proto_text);
137 gtk_widget_show(txt_scrollw);
138 gtk_widget_show(proto_text);
139 gtk_widget_show(proto_vb);
140 label = gtk_label_new("Protocols");
141 gtk_notebook_append_page(GTK_NOTEBOOK(supported_nb), proto_vb, label);
143 /* display filter fields */
144 #if GTK_MAJOR_VERSION < 2
145 /* X windows have a maximum size of 32767. Since the height can easily
146 exceed this, we have to jump through some hoops to have a functional
147 vertical scroll bar. */
149 dfilter_tb = gtk_table_new(2, 2, FALSE);
150 gtk_table_set_col_spacing (GTK_TABLE (dfilter_tb), 0, 3);
151 gtk_table_set_row_spacing (GTK_TABLE (dfilter_tb), 0, 3);
152 gtk_container_border_width(GTK_CONTAINER(dfilter_tb), 1);
154 txt_scrollw = scrolled_window_new(NULL, NULL);
155 #if GTK_MAJOR_VERSION >= 2
156 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(txt_scrollw),
159 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(txt_scrollw),
162 dfilter_text = gtk_text_new(NULL, NULL);
163 dfilter_vsb = gtk_vscrollbar_new(GTK_TEXT(dfilter_text)->vadj);
164 if (prefs.gui_scrollbar_on_right) {
165 gtk_table_attach (GTK_TABLE (dfilter_tb), txt_scrollw, 0, 1, 0, 1,
166 GTK_EXPAND | GTK_SHRINK | GTK_FILL,
167 GTK_EXPAND | GTK_SHRINK | GTK_FILL, 0, 0);
168 gtk_table_attach (GTK_TABLE (dfilter_tb), dfilter_vsb, 1, 2, 0, 1,
169 GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
171 gtk_table_attach (GTK_TABLE (dfilter_tb), txt_scrollw, 1, 2, 0, 1,
172 GTK_EXPAND | GTK_SHRINK | GTK_FILL,
173 GTK_EXPAND | GTK_SHRINK | GTK_FILL, 0, 0);
174 gtk_table_attach (GTK_TABLE (dfilter_tb), dfilter_vsb, 0, 1, 0, 1,
175 GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
177 gtk_text_set_editable(GTK_TEXT(dfilter_text), FALSE);
178 gtk_text_set_line_wrap(GTK_TEXT(dfilter_text), FALSE);
179 set_supported_text(dfilter_text, DFILTER_SUPPORTED);
180 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(txt_scrollw),
183 dfilter_vb = gtk_vbox_new(FALSE, 0);
184 gtk_container_border_width(GTK_CONTAINER(dfilter_vb), 1);
186 txt_scrollw = scrolled_window_new(NULL, NULL);
187 #if GTK_MAJOR_VERSION >= 2
188 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(txt_scrollw),
191 gtk_box_pack_start(GTK_BOX(dfilter_vb), txt_scrollw, TRUE, TRUE, 0);
192 dfilter_text = gtk_text_view_new();
193 if (prefs.gui_scrollbar_on_right) {
194 gtk_scrolled_window_set_placement(GTK_SCROLLED_WINDOW(txt_scrollw),
195 GTK_CORNER_TOP_LEFT);
198 gtk_scrolled_window_set_placement(GTK_SCROLLED_WINDOW(txt_scrollw),
199 GTK_CORNER_TOP_RIGHT);
201 gtk_text_view_set_editable(GTK_TEXT_VIEW(dfilter_text), FALSE);
202 set_supported_text(dfilter_text, DFILTER_SUPPORTED);
203 gtk_container_add(GTK_CONTAINER(txt_scrollw), dfilter_text);
205 gtk_widget_show(txt_scrollw);
206 gtk_widget_show(dfilter_text);
207 #if GTK_MAJOR_VERSION < 2
208 gtk_widget_show(dfilter_tb);
209 gtk_widget_show(dfilter_vsb);
211 gtk_widget_show(dfilter_vb);
213 label = gtk_label_new("Display Filter Fields");
214 #if GTK_MAJOR_VERSION < 2
215 gtk_notebook_append_page(GTK_NOTEBOOK(supported_nb), dfilter_tb, label);
217 gtk_notebook_append_page(GTK_NOTEBOOK(supported_nb), dfilter_vb, label);
220 /* XXX add other panels here ... */
222 gtk_widget_show(supported_nb);
225 bbox = dlg_button_row_new(GTK_STOCK_OK, NULL);
226 gtk_box_pack_end(GTK_BOX(main_vb), bbox, FALSE, FALSE, 0);
227 gtk_widget_show(bbox);
229 ok_bt = OBJECT_GET_DATA(bbox, GTK_STOCK_OK);
230 window_set_cancel_button(supported_w, ok_bt, window_cancel_button_cb);
232 gtk_quit_add_destroy(gtk_main_level(), GTK_OBJECT(supported_w));
234 SIGNAL_CONNECT(supported_w, "delete_event", window_delete_event_cb, NULL);
235 SIGNAL_CONNECT(supported_w, "destroy", supported_destroy_cb, NULL);
237 gtk_widget_show(supported_w);
238 window_present(supported_w);
241 static void supported_destroy_cb(GtkWidget *w _U_, gpointer data _U_)
243 /* Note that we no longer have a Help window. */
247 static void insert_text(GtkWidget *w, const char *buffer, int nchars)
249 #if GTK_MAJOR_VERSION < 2
250 gtk_text_insert(GTK_TEXT(w), m_r_font, NULL, NULL, buffer, nchars);
252 GtkTextBuffer *buf= gtk_text_view_get_buffer(GTK_TEXT_VIEW(w));
255 gtk_text_buffer_get_end_iter(buf, &iter);
256 gtk_widget_modify_font(w, m_r_font);
257 if (!g_utf8_validate(buffer, -1, NULL))
258 printf("Invalid utf8 encoding: %s\n", buffer);
259 gtk_text_buffer_insert(buf, &iter, buffer, nchars);
264 static void set_supported_text(GtkWidget *w, supported_type_t type)
267 #define BUFF_LEN 4096
269 char buffer[BUFF_LEN];
270 header_field_info *hfinfo;
271 int i, len, maxlen = 0, maxlen2 = 0, maxlen4 = 0;
272 #if GTK_MAJOR_VERSION < 2
273 int maxlen3 = 0, nb_lines = 0;
276 const char *type_name;
277 void *cookie, *cookie2;
278 protocol_t *protocol;
279 char *name, *short_name, *filter_name;
280 int namel = 0, short_namel = 0, filter_namel = 0;
286 * the width and height computations are performed to make the
287 * horizontal scrollbar work in gtk1.2. This is only necessary for the
288 * PROTOCOL_SUPPORTED and DFILTER_SUPPORTED windows since all others should
289 * not have any horizontal scrollbar (line wrapping enabled).
293 #if GTK_MAJOR_VERSION < 2
294 gtk_text_freeze(GTK_TEXT(w));
299 case PROTOCOL_SUPPORTED :
300 /* first pass to know the maximum length of first field */
302 for (i = proto_get_first_protocol(&cookie); i != -1;
303 i = proto_get_next_protocol(&cookie)) {
305 protocol = find_protocol_by_id(i);
306 name = proto_get_protocol_name(i);
307 short_name = proto_get_protocol_short_name(protocol);
308 filter_name = proto_get_protocol_filter_name(i);
309 if ((len = strlen(name)) > namel)
311 if ((len = strlen(short_name)) > short_namel)
313 if ((len = strlen(filter_name)) > filter_namel)
316 maxlen = namel + short_namel + filter_namel;
318 len = g_snprintf(buffer, BUFF_LEN, proto_supported, count);
319 #if GTK_MAJOR_VERSION < 2
321 width = gdk_string_width(m_r_font, buffer);
322 insert_text(w, buffer, maxlen2);
324 insert_text(w, buffer, len);
327 /* ok, display the correctly aligned strings */
328 for (i = proto_get_first_protocol(&cookie); i != -1;
329 i = proto_get_next_protocol(&cookie)) {
330 protocol = find_protocol_by_id(i);
331 name = proto_get_protocol_name(i);
332 short_name = proto_get_protocol_short_name(protocol);
333 filter_name = proto_get_protocol_filter_name(i);
335 /* the name used for sorting in the left column */
336 len = g_snprintf(buffer, BUFF_LEN, "%*s %*s %*s\n",
337 -short_namel, short_name,
339 -filter_namel, filter_name);
340 #if GTK_MAJOR_VERSION < 2
343 if ((len = gdk_string_width(m_r_font, buffer)) > width)
346 insert_text(w, buffer, strlen(buffer));
349 insert_text(w, buffer, strlen(buffer));
353 #if GTK_MAJOR_VERSION < 2
354 height = (3 + nb_lines) * m_font_height;
355 WIDGET_SET_SIZE(w, 20 + width, 20 + height);
359 case DFILTER_SUPPORTED :
361 /* XXX we should display hinfo->blurb instead of name (if not empty) */
363 /* first pass to know the maximum length of first and second fields */
364 for (i = proto_get_first_protocol(&cookie); i != -1;
365 i = proto_get_next_protocol(&cookie)) {
367 for (hfinfo = proto_get_first_protocol_field(i, &cookie2); hfinfo != NULL;
368 hfinfo = proto_get_next_protocol_field(&cookie2)) {
370 if (hfinfo->same_name_prev != NULL) /* ignore duplicate names */
373 if ((len = strlen(hfinfo->abbrev)) > maxlen)
375 if ((len = strlen(hfinfo->name)) > maxlen2)
377 if ((len = strlen(hfinfo->blurb)) > maxlen4)
382 #if GTK_MAJOR_VERSION < 2
383 maxlen3 = strlen(dfilter_supported);
384 width = gdk_string_width(m_r_font, dfilter_supported);
385 insert_text(w, dfilter_supported, maxlen3);
387 insert_text(w, dfilter_supported, strlen(dfilter_supported));
391 for (i = proto_get_first_protocol(&cookie); i != -1;
392 i = proto_get_next_protocol(&cookie)) {
393 protocol = find_protocol_by_id(i);
394 name = proto_get_protocol_name(i);
395 short_name = proto_get_protocol_short_name(protocol);
396 filter_name = proto_get_protocol_filter_name(i);
399 for (hfinfo = proto_get_first_protocol_field(i, &cookie2); hfinfo != NULL;
400 hfinfo = proto_get_next_protocol_field(&cookie2)) {
402 if (hfinfo->same_name_prev != NULL) /* ignore duplicate names */
408 len = g_snprintf(buffer, BUFF_LEN, "\n%s - %s (%s) [%d fields]:\n",
409 short_name, name, filter_name, count);
410 insert_text(w, buffer, len);
412 for (hfinfo = proto_get_first_protocol_field(i, &cookie2); hfinfo != NULL;
413 hfinfo = proto_get_next_protocol_field(&cookie2)) {
415 if (hfinfo->same_name_prev != NULL) /* ignore duplicate names */
418 type_name = ftype_pretty_name(hfinfo->type);
419 len = g_snprintf(buffer, BUFF_LEN, "%*s %*s %*s (%s)\n",
420 -maxlen, hfinfo->abbrev,
421 -maxlen2, hfinfo->name,
422 -maxlen4, hfinfo->blurb,
424 #if GTK_MAJOR_VERSION < 2
427 if ((len = gdk_string_width(m_r_font, buffer)) > width)
430 insert_text(w, buffer, strlen(buffer));
433 insert_text(w, buffer, strlen(buffer));
437 len = g_snprintf(buffer, BUFF_LEN, "\n-- Total %d fields\n", fcount);
438 insert_text(w, buffer, len);
440 #if GTK_MAJOR_VERSION < 2
441 height = (5 + nb_lines) * m_font_height;
442 WIDGET_SET_SIZE(w, 20 + width, 20 + height);
446 g_assert_not_reached();
449 #if GTK_MAJOR_VERSION < 2
450 gtk_text_thaw(GTK_TEXT(w));
452 } /* set_supported_text */
455 static void clear_supported_text(GtkWidget *w)
457 #if GTK_MAJOR_VERSION < 2
458 GtkText *txt = GTK_TEXT(w);
460 gtk_text_set_point(txt, 0);
461 /* Keep GTK+ 1.2.3 through 1.2.6 from dumping core - see
462 http://www.ethereal.com/lists/ethereal-dev/199912/msg00312.html and
463 http://www.gnome.org/mailing-lists/archives/gtk-devel-list/1999-October/0051.shtml
464 for more information */
465 gtk_adjustment_set_value(txt->vadj, 0.0);
466 gtk_text_forward_delete(txt, gtk_text_get_length(txt));
468 GtkTextBuffer *buf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(w));
470 gtk_text_buffer_set_text(buf, "", 0);
475 /* Redraw all the text widgets, to use a new font. */
476 void supported_redraw(void)
478 if (supported_w != NULL) {
479 #if GTK_MAJOR_VERSION < 2
480 gtk_text_freeze(GTK_TEXT(proto_text));
482 clear_supported_text(proto_text);
483 set_supported_text(proto_text, PROTOCOL_SUPPORTED);
484 #if GTK_MAJOR_VERSION < 2
485 gtk_text_thaw(GTK_TEXT(proto_text));
487 gtk_text_freeze(GTK_TEXT(dfilter_text));
489 clear_supported_text(dfilter_text);
490 set_supported_text(dfilter_text, DFILTER_SUPPORTED);
491 #if GTK_MAJOR_VERSION < 2
492 gtk_text_thaw(GTK_TEXT(dfilter_text));