Witness: enum witness_notifyResponse_type
[wireshark-wip.git] / ui / gtk / supported_protos_dlg.c
blobff325eb128955d03059eff248af2f3d9154d0540
1 /* supported_protos_dlg.c
3 * Laurent Deniel <laurent.deniel@free.fr>
5 * $Id$
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include "config.h"
28 #include <stdio.h>
29 #include <string.h>
31 #include <gtk/gtk.h>
33 #include <epan/prefs.h>
35 #include "../globals.h"
37 #include "ui/gtk/supported_protos_dlg.h"
38 #include "ui/gtk/gtkglobals.h"
39 #include "ui/gtk/gui_utils.h"
40 #include "ui/gtk/dlg_utils.h"
41 #include "ui/gtk/font_utils.h"
45 static const char *proto_supported =
46 "The following %d protocols (and packet types) are currently\n"
47 "supported by Wireshark:\n\n";
49 static const char *dfilter_supported =
50 "The following per-protocol fields are currently supported by\n"
51 "Wireshark and can be used in display filters:\n";
55 typedef enum {
56 PROTOCOL_SUPPORTED,
57 DFILTER_SUPPORTED
58 } supported_type_t;
60 static void supported_destroy_cb(GtkWidget *w, gpointer data);
61 static void insert_text(GtkWidget *w, const char *buffer, int nchars);
62 static void set_supported_text(GtkWidget *w, supported_type_t type);
65 * Keep a static pointer to the current "Supported" window, if any, so that
66 * if somebody tries to do "Help->Supported" while there's already a
67 * "Supported" window up, we just pop up the existing one, rather than
68 * creating a new one.
70 static GtkWidget *supported_w = NULL;
73 * Keep static pointers to the text widgets as well (for text format changes).
75 static GtkWidget *proto_text, *dfilter_text;
79 void supported_cb(GtkWidget *w _U_, gpointer data _U_)
82 GtkWidget *main_vb, *bbox, *supported_nb, *ok_bt, *label, *txt_scrollw,
83 *proto_vb,
84 *dfilter_vb;
86 if (supported_w != NULL) {
87 /* There's already a "Supported" dialog box; reactivate it. */
88 reactivate_window(supported_w);
89 return;
92 supported_w = window_new(GTK_WINDOW_TOPLEVEL, "Wireshark: Supported Protocols");
93 gtk_window_set_default_size(GTK_WINDOW(supported_w), DEF_WIDTH * 2/3, DEF_HEIGHT * 2/3);
94 gtk_container_set_border_width(GTK_CONTAINER(supported_w), 2);
96 /* Container for each row of widgets */
97 main_vb = ws_gtk_box_new(GTK_ORIENTATION_VERTICAL, 1, FALSE);
98 gtk_container_set_border_width(GTK_CONTAINER(main_vb), 1);
99 gtk_container_add(GTK_CONTAINER(supported_w), main_vb);
100 gtk_widget_show(main_vb);
102 /* supported topics container */
103 supported_nb = gtk_notebook_new();
104 gtk_box_pack_start(GTK_BOX(main_vb), supported_nb, TRUE, TRUE, 0);
107 /* protocol list */
108 proto_vb = ws_gtk_box_new(GTK_ORIENTATION_VERTICAL, 0, FALSE);
109 gtk_container_set_border_width(GTK_CONTAINER(proto_vb), 1);
111 txt_scrollw = scrolled_window_new(NULL, NULL);
112 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(txt_scrollw),
113 GTK_SHADOW_IN);
114 gtk_box_pack_start(GTK_BOX(proto_vb), txt_scrollw, TRUE, TRUE, 0);
115 proto_text = gtk_text_view_new();
116 gtk_text_view_set_editable(GTK_TEXT_VIEW(proto_text), FALSE);
117 set_supported_text(proto_text, PROTOCOL_SUPPORTED);
118 gtk_container_add(GTK_CONTAINER(txt_scrollw), proto_text);
119 gtk_widget_show(txt_scrollw);
120 gtk_widget_show(proto_text);
121 gtk_widget_show(proto_vb);
122 label = gtk_label_new("Protocols");
123 gtk_notebook_append_page(GTK_NOTEBOOK(supported_nb), proto_vb, label);
125 /* display filter fields */
126 dfilter_vb = ws_gtk_box_new(GTK_ORIENTATION_VERTICAL, 0, FALSE);
127 gtk_container_set_border_width(GTK_CONTAINER(dfilter_vb), 1);
129 txt_scrollw = scrolled_window_new(NULL, NULL);
130 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(txt_scrollw),
131 GTK_SHADOW_IN);
132 gtk_box_pack_start(GTK_BOX(dfilter_vb), txt_scrollw, TRUE, TRUE, 0);
133 dfilter_text = gtk_text_view_new();
134 gtk_text_view_set_editable(GTK_TEXT_VIEW(dfilter_text), FALSE);
135 set_supported_text(dfilter_text, DFILTER_SUPPORTED);
136 gtk_container_add(GTK_CONTAINER(txt_scrollw), dfilter_text);
137 gtk_widget_show(txt_scrollw);
138 gtk_widget_show(dfilter_text);
139 gtk_widget_show(dfilter_vb);
140 label = gtk_label_new("Display Filter Fields");
141 gtk_notebook_append_page(GTK_NOTEBOOK(supported_nb), dfilter_vb, label);
143 /* XXX add other panels here ... */
145 gtk_widget_show(supported_nb);
147 /* Button row */
148 bbox = dlg_button_row_new(GTK_STOCK_OK, NULL);
149 gtk_box_pack_end(GTK_BOX(main_vb), bbox, FALSE, FALSE, 0);
150 gtk_widget_show(bbox);
152 ok_bt = (GtkWidget *)g_object_get_data(G_OBJECT(bbox), GTK_STOCK_OK);
153 window_set_cancel_button(supported_w, ok_bt, window_cancel_button_cb);
155 g_signal_connect(supported_w, "delete_event", G_CALLBACK(window_delete_event_cb), NULL);
156 g_signal_connect(supported_w, "destroy", G_CALLBACK(supported_destroy_cb), NULL);
158 gtk_widget_show(supported_w);
159 window_present(supported_w);
160 } /* supported_cb */
162 static void supported_destroy_cb(GtkWidget *w _U_, gpointer data _U_)
164 /* Note that we no longer have a Help window. */
165 supported_w = NULL;
168 static void insert_text(GtkWidget *w, const char *buffer, int nchars)
170 GtkTextBuffer *buf= gtk_text_view_get_buffer(GTK_TEXT_VIEW(w));
171 GtkTextIter iter;
173 gtk_text_buffer_get_end_iter(buf, &iter);
174 #if GTK_CHECK_VERSION(3,0,0)
175 gtk_widget_override_font(w, user_font_get_regular());
176 #else
177 gtk_widget_modify_font(w, user_font_get_regular());
178 #endif
179 if (!g_utf8_validate(buffer, -1, NULL))
180 printf("Invalid utf8 encoding: %s\n", buffer); /* ToDo: Don't use printf ?? */
181 gtk_text_buffer_insert(buf, &iter, buffer, nchars);
185 static void set_supported_text(GtkWidget *w, supported_type_t type)
188 #define BUFF_LEN 4096
189 #define B_LEN 256
190 char buffer[BUFF_LEN];
191 header_field_info *hfinfo;
192 int i, len, maxlen = 0, maxlen2 = 0, maxlen4 = 0;
193 const char *type_name;
194 void *cookie, *cookie2;
195 protocol_t *protocol;
196 const char *name, *short_name, *filter_name;
197 int namel = 0, short_namel = 0, filter_namel = 0;
198 int count, fcount;
202 * XXX quick hack:
203 * the width and height computations are performed to make the
204 * horizontal scrollbar work in gtk1.2. This is only necessary for the
205 * PROTOCOL_SUPPORTED and DFILTER_SUPPORTED windows since all others should
206 * not have any horizontal scrollbar (line wrapping enabled).
210 switch(type) {
212 case PROTOCOL_SUPPORTED :
213 /* first pass to know the maximum length of first field */
214 count = 0;
215 for (i = proto_get_first_protocol(&cookie); i != -1;
216 i = proto_get_next_protocol(&cookie)) {
217 count++;
218 protocol = find_protocol_by_id(i);
219 name = proto_get_protocol_name(i);
220 short_name = proto_get_protocol_short_name(protocol);
221 filter_name = proto_get_protocol_filter_name(i);
222 if ((len = (int) strlen(name)) > namel)
223 namel = len;
224 if ((len = (int) strlen(short_name)) > short_namel)
225 short_namel = len;
226 if ((len = (int) strlen(filter_name)) > filter_namel)
227 filter_namel = len;
230 len = g_snprintf(buffer, BUFF_LEN, proto_supported, count);
231 insert_text(w, buffer, len);
233 /* ok, display the correctly aligned strings */
234 for (i = proto_get_first_protocol(&cookie); i != -1;
235 i = proto_get_next_protocol(&cookie)) {
236 protocol = find_protocol_by_id(i);
237 name = proto_get_protocol_name(i);
238 short_name = proto_get_protocol_short_name(protocol);
239 filter_name = proto_get_protocol_filter_name(i);
241 /* the name used for sorting in the left column */
242 len = g_snprintf(buffer, BUFF_LEN, "%*s %*s %*s\n",
243 -short_namel, short_name,
244 -namel, name,
245 -filter_namel, filter_name);
246 insert_text(w, buffer, len);
249 break;
251 case DFILTER_SUPPORTED :
253 /* XXX we should display hinfo->blurb instead of name (if not empty) */
255 /* first pass to know the maximum length of first and second fields */
256 for (i = proto_get_first_protocol(&cookie); i != -1;
257 i = proto_get_next_protocol(&cookie)) {
259 for (hfinfo = proto_get_first_protocol_field(i, &cookie2); hfinfo != NULL;
260 hfinfo = proto_get_next_protocol_field(&cookie2)) {
262 if (hfinfo->same_name_prev_id != -1) /* ignore duplicate names */
263 continue;
265 if ((len = (int) strlen(hfinfo->abbrev)) > maxlen)
266 maxlen = len;
267 if ((len = (int) strlen(hfinfo->name)) > maxlen2)
268 maxlen2 = len;
269 if (hfinfo->blurb != NULL) {
270 if ((len = (int) strlen(hfinfo->blurb)) > maxlen4)
271 maxlen4 = len;
276 insert_text(w, dfilter_supported, (int) strlen(dfilter_supported));
278 fcount = 0;
279 for (i = proto_get_first_protocol(&cookie); i != -1;
280 i = proto_get_next_protocol(&cookie)) {
281 protocol = find_protocol_by_id(i);
282 name = proto_get_protocol_name(i);
283 short_name = proto_get_protocol_short_name(protocol);
284 filter_name = proto_get_protocol_filter_name(i);
286 count = 0;
287 for (hfinfo = proto_get_first_protocol_field(i, &cookie2); hfinfo != NULL;
288 hfinfo = proto_get_next_protocol_field(&cookie2)) {
290 if (hfinfo->same_name_prev_id != -1) /* ignore duplicate names */
291 continue;
292 count++;
294 fcount += count;
296 len = g_snprintf(buffer, BUFF_LEN, "\n%s - %s (%s) [%d fields]:\n",
297 short_name, name, filter_name, count);
298 insert_text(w, buffer, len);
300 for (hfinfo = proto_get_first_protocol_field(i, &cookie2); hfinfo != NULL;
301 hfinfo = proto_get_next_protocol_field(&cookie2)) {
303 if (hfinfo->same_name_prev_id != -1) /* ignore duplicate names */
304 continue;
306 type_name = ftype_pretty_name(hfinfo->type);
307 if (hfinfo->blurb != NULL && hfinfo->blurb[0] != '\0') {
308 len = g_snprintf(buffer, BUFF_LEN, "%*s %*s %*s (%s)\n",
309 -maxlen, hfinfo->abbrev,
310 -maxlen2, hfinfo->name,
311 -maxlen4, hfinfo->blurb,
312 type_name);
313 } else {
314 len = g_snprintf(buffer, BUFF_LEN, "%*s %*s (%s)\n",
315 -maxlen, hfinfo->abbrev,
316 -maxlen2, hfinfo->name,
317 type_name);
319 insert_text(w, buffer, len);
322 len = g_snprintf(buffer, BUFF_LEN, "\n-- Total %d fields\n", fcount);
323 insert_text(w, buffer, len);
325 break;
326 default :
327 g_assert_not_reached();
328 break;
329 } /* switch(type) */
330 } /* set_supported_text */
333 static void clear_supported_text(GtkWidget *w)
335 GtkTextBuffer *buf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(w));
337 gtk_text_buffer_set_text(buf, "", 0);
341 /* Redraw all the text widgets, to use a new font. */
342 void supported_redraw(void)
344 if (supported_w != NULL) {
345 clear_supported_text(proto_text);
346 set_supported_text(proto_text, PROTOCOL_SUPPORTED);
347 clear_supported_text(dfilter_text);
348 set_supported_text(dfilter_text, DFILTER_SUPPORTED);