Fix build (automake) error: WIRESHARK_CUSTOM_HEADERS is ng but WIRESHARK_CUSTOM_HDRS...
[obnox/wireshark/wip.git] / gtk / supported_protos_dlg.c
1 /* supported_protos_dlg.c
2  *
3  * Laurent Deniel <laurent.deniel@free.fr>
4  *
5  * $Id$
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
9  * Copyright 2000 Gerald Combs
10  *
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.
15  *
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.
20  *
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.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <stdio.h>
31 #include <string.h>
32
33 #include <gtk/gtk.h>
34
35 #include <epan/prefs.h>
36
37 #include "../globals.h"
38
39 #include "gtk/supported_protos_dlg.h"
40 #include "gtk/gtkglobals.h"
41 #include "gtk/gui_utils.h"
42 #include "gtk/dlg_utils.h"
43 #include "gtk/font_utils.h"
44
45
46
47 static const char *proto_supported =
48 "The following %d protocols (and packet types) are currently\n"
49 "supported by Wireshark:\n\n";
50
51 static const char *dfilter_supported =
52 "The following per-protocol fields are currently supported by\n"
53 "Wireshark and can be used in display filters:\n";
54
55
56
57 typedef enum {
58   PROTOCOL_SUPPORTED,
59   DFILTER_SUPPORTED
60 } supported_type_t;
61
62 static void supported_destroy_cb(GtkWidget *w, gpointer data);
63 static void insert_text(GtkWidget *w, const char *buffer, int nchars);
64 static void set_supported_text(GtkWidget *w, supported_type_t type);
65
66 /*
67  * Keep a static pointer to the current "Supported" window, if any, so that
68  * if somebody tries to do "Help->Supported" while there's already a
69  * "Supported" window up, we just pop up the existing one, rather than
70  * creating a new one.
71 */
72 static GtkWidget *supported_w = NULL;
73
74 /*
75  * Keep static pointers to the text widgets as well (for text format changes).
76  */
77 static GtkWidget *proto_text, *dfilter_text;
78
79
80
81 void supported_cb(GtkWidget *w _U_, gpointer data _U_)
82 {
83
84   GtkWidget *main_vb, *bbox, *supported_nb, *ok_bt, *label, *txt_scrollw,
85     *proto_vb,
86     *dfilter_vb;
87
88   if (supported_w != NULL) {
89     /* There's already a "Supported" dialog box; reactivate it. */
90     reactivate_window(supported_w);
91     return;
92   }
93
94   supported_w = window_new(GTK_WINDOW_TOPLEVEL, "Wireshark: Supported Protocols");
95   gtk_window_set_default_size(GTK_WINDOW(supported_w), DEF_WIDTH * 2/3, DEF_HEIGHT * 2/3);
96   gtk_container_set_border_width(GTK_CONTAINER(supported_w), 2);
97
98   /* Container for each row of widgets */
99   main_vb = gtk_vbox_new(FALSE, 1);
100   gtk_container_set_border_width(GTK_CONTAINER(main_vb), 1);
101   gtk_container_add(GTK_CONTAINER(supported_w), main_vb);
102   gtk_widget_show(main_vb);
103
104   /* supported topics container */
105   supported_nb = gtk_notebook_new();
106   gtk_container_add(GTK_CONTAINER(main_vb), supported_nb);
107
108
109   /* protocol list */
110   proto_vb = gtk_vbox_new(FALSE, 0);
111   gtk_container_set_border_width(GTK_CONTAINER(proto_vb), 1);
112
113   txt_scrollw = scrolled_window_new(NULL, NULL);
114   gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(txt_scrollw),
115                                    GTK_SHADOW_IN);
116   gtk_box_pack_start(GTK_BOX(proto_vb), txt_scrollw, TRUE, TRUE, 0);
117   proto_text = gtk_text_view_new();
118   gtk_text_view_set_editable(GTK_TEXT_VIEW(proto_text), FALSE);
119   set_supported_text(proto_text, PROTOCOL_SUPPORTED);
120   gtk_container_add(GTK_CONTAINER(txt_scrollw), proto_text);
121   gtk_widget_show(txt_scrollw);
122   gtk_widget_show(proto_text);
123   gtk_widget_show(proto_vb);
124   label = gtk_label_new("Protocols");
125   gtk_notebook_append_page(GTK_NOTEBOOK(supported_nb), proto_vb, label);
126
127   /* display filter fields */
128   dfilter_vb = gtk_vbox_new(FALSE, 0);
129   gtk_container_set_border_width(GTK_CONTAINER(dfilter_vb), 1);
130
131   txt_scrollw = scrolled_window_new(NULL, NULL);
132     gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(txt_scrollw),
133                                    GTK_SHADOW_IN);
134   gtk_box_pack_start(GTK_BOX(dfilter_vb), txt_scrollw, TRUE, TRUE, 0);
135   dfilter_text = gtk_text_view_new();
136   if (prefs.gui_scrollbar_on_right) {
137     gtk_scrolled_window_set_placement(GTK_SCROLLED_WINDOW(txt_scrollw),
138                                       GTK_CORNER_TOP_LEFT);
139   }
140   else {
141     gtk_scrolled_window_set_placement(GTK_SCROLLED_WINDOW(txt_scrollw),
142                                       GTK_CORNER_TOP_RIGHT);
143   }
144   gtk_text_view_set_editable(GTK_TEXT_VIEW(dfilter_text), FALSE);
145   set_supported_text(dfilter_text, DFILTER_SUPPORTED);
146   gtk_container_add(GTK_CONTAINER(txt_scrollw), dfilter_text);
147   gtk_widget_show(txt_scrollw);
148   gtk_widget_show(dfilter_text);
149   gtk_widget_show(dfilter_vb);
150   label = gtk_label_new("Display Filter Fields");
151   gtk_notebook_append_page(GTK_NOTEBOOK(supported_nb), dfilter_vb, label);
152
153   /* XXX add other panels here ... */
154
155   gtk_widget_show(supported_nb);
156
157   /* Button row */
158   bbox = dlg_button_row_new(GTK_STOCK_OK, NULL);
159   gtk_box_pack_end(GTK_BOX(main_vb), bbox, FALSE, FALSE, 0);
160   gtk_widget_show(bbox);
161
162   ok_bt = g_object_get_data(G_OBJECT(bbox), GTK_STOCK_OK);
163   window_set_cancel_button(supported_w, ok_bt, window_cancel_button_cb);
164
165   g_signal_connect(supported_w, "delete_event", G_CALLBACK(window_delete_event_cb), NULL);
166   g_signal_connect(supported_w, "destroy", G_CALLBACK(supported_destroy_cb), NULL);
167
168   gtk_widget_show(supported_w);
169   window_present(supported_w);
170 } /* supported_cb */
171
172 static void supported_destroy_cb(GtkWidget *w _U_, gpointer data _U_)
173 {
174   /* Note that we no longer have a Help window. */
175   supported_w = NULL;
176 }
177
178 static void insert_text(GtkWidget *w, const char *buffer, int nchars)
179 {
180     GtkTextBuffer *buf= gtk_text_view_get_buffer(GTK_TEXT_VIEW(w));
181     GtkTextIter    iter;
182
183     gtk_text_buffer_get_end_iter(buf, &iter);
184 #if GTK_CHECK_VERSION(3,0,0)
185     gtk_widget_override_font(w, user_font_get_regular());
186 #else
187     gtk_widget_modify_font(w, user_font_get_regular());
188 #endif
189     if (!g_utf8_validate(buffer, -1, NULL))
190         printf("Invalid utf8 encoding: %s\n", buffer);  /* ToDo: Don't use printf ?? */
191     gtk_text_buffer_insert(buf, &iter, buffer, nchars);
192 }
193
194
195 static void set_supported_text(GtkWidget *w, supported_type_t type)
196 {
197
198 #define BUFF_LEN 4096
199 #define B_LEN    256
200   char buffer[BUFF_LEN];
201   header_field_info *hfinfo;
202   int i, len, maxlen = 0, maxlen2 = 0, maxlen4 = 0;
203   const char *type_name;
204   void *cookie, *cookie2;
205   protocol_t *protocol;
206   const char *name, *short_name, *filter_name;
207   int namel = 0, short_namel = 0, filter_namel = 0;
208   int count, fcount;
209
210
211   /*
212    * XXX quick hack:
213    * the width and height computations are performed to make the
214    * horizontal scrollbar work in gtk1.2. This is only necessary for the
215    * PROTOCOL_SUPPORTED and DFILTER_SUPPORTED windows since all others should
216    * not have any horizontal scrollbar (line wrapping enabled).
217    */
218
219
220   switch(type) {
221
222   case PROTOCOL_SUPPORTED :
223     /* first pass to know the maximum length of first field */
224     count = 0;
225     for (i = proto_get_first_protocol(&cookie); i != -1;
226          i = proto_get_next_protocol(&cookie)) {
227             count++;
228             protocol = find_protocol_by_id(i);
229             name = proto_get_protocol_name(i);
230             short_name = proto_get_protocol_short_name(protocol);
231             filter_name = proto_get_protocol_filter_name(i);
232             if ((len = (int) strlen(name)) > namel)
233                     namel = len;
234             if ((len = (int) strlen(short_name)) > short_namel)
235                     short_namel = len;
236             if ((len = (int) strlen(filter_name)) > filter_namel)
237                     filter_namel = len;
238     }
239
240     len = g_snprintf(buffer, BUFF_LEN, proto_supported, count);
241     insert_text(w, buffer, len);
242
243     /* ok, display the correctly aligned strings */
244     for (i = proto_get_first_protocol(&cookie); i != -1;
245          i = proto_get_next_protocol(&cookie)) {
246             protocol = find_protocol_by_id(i);
247             name = proto_get_protocol_name(i);
248             short_name = proto_get_protocol_short_name(protocol);
249             filter_name = proto_get_protocol_filter_name(i);
250
251             /* the name used for sorting in the left column */
252             len = g_snprintf(buffer, BUFF_LEN, "%*s %*s %*s\n",
253                            -short_namel,  short_name,
254                            -namel,        name,
255                            -filter_namel, filter_name);
256             insert_text(w, buffer, len);
257     }
258
259     break;
260
261   case DFILTER_SUPPORTED  :
262
263     /* XXX we should display hinfo->blurb instead of name (if not empty) */
264
265     /* first pass to know the maximum length of first and second fields */
266     for (i = proto_get_first_protocol(&cookie); i != -1;
267          i = proto_get_next_protocol(&cookie)) {
268
269             for (hfinfo = proto_get_first_protocol_field(i, &cookie2); hfinfo != NULL;
270                  hfinfo = proto_get_next_protocol_field(&cookie2)) {
271
272                     if (hfinfo->same_name_prev != NULL) /* ignore duplicate names */
273                             continue;
274
275                     if ((len = (int) strlen(hfinfo->abbrev)) > maxlen)
276                             maxlen = len;
277                     if ((len = (int) strlen(hfinfo->name)) > maxlen2)
278                             maxlen2 = len;
279                     if (hfinfo->blurb != NULL) {
280                             if ((len = (int) strlen(hfinfo->blurb)) > maxlen4)
281                                 maxlen4 = len;
282                     }
283             }
284     }
285
286     insert_text(w, dfilter_supported, (int) strlen(dfilter_supported));
287
288     fcount = 0;
289     for (i = proto_get_first_protocol(&cookie); i != -1;
290          i = proto_get_next_protocol(&cookie)) {
291             protocol = find_protocol_by_id(i);
292             name = proto_get_protocol_name(i);
293             short_name = proto_get_protocol_short_name(protocol);
294             filter_name = proto_get_protocol_filter_name(i);
295
296             count = 0;
297             for (hfinfo = proto_get_first_protocol_field(i, &cookie2); hfinfo != NULL;
298                  hfinfo = proto_get_next_protocol_field(&cookie2)) {
299
300                     if (hfinfo->same_name_prev != NULL) /* ignore duplicate names */
301                             continue;
302                     count++;
303             }
304             fcount += count;
305
306             len = g_snprintf(buffer, BUFF_LEN, "\n%s - %s (%s) [%d fields]:\n",
307                            short_name, name, filter_name, count);
308             insert_text(w, buffer, len);
309
310             for (hfinfo = proto_get_first_protocol_field(i, &cookie2); hfinfo != NULL;
311                  hfinfo = proto_get_next_protocol_field(&cookie2)) {
312
313                     if (hfinfo->same_name_prev != NULL) /* ignore duplicate names */
314                             continue;
315
316                     type_name = ftype_pretty_name(hfinfo->type);
317                     if (hfinfo->blurb != NULL && hfinfo->blurb[0] != '\0') {
318                             len = g_snprintf(buffer, BUFF_LEN, "%*s %*s %*s (%s)\n",
319                                              -maxlen,  hfinfo->abbrev,
320                                              -maxlen2, hfinfo->name,
321                                              -maxlen4, hfinfo->blurb,
322                                              type_name);
323                     } else {
324                             len = g_snprintf(buffer, BUFF_LEN, "%*s %*s (%s)\n",
325                                              -maxlen,  hfinfo->abbrev,
326                                              -maxlen2, hfinfo->name,
327                                              type_name);
328                     }
329                     insert_text(w, buffer, len);
330             }
331     }
332     len = g_snprintf(buffer, BUFF_LEN, "\n-- Total %d fields\n", fcount);
333     insert_text(w, buffer, len);
334
335     break;
336   default :
337     g_assert_not_reached();
338     break;
339   } /* switch(type) */
340 } /* set_supported_text */
341
342
343 static void clear_supported_text(GtkWidget *w)
344 {
345   GtkTextBuffer *buf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(w));
346
347   gtk_text_buffer_set_text(buf, "", 0);
348 }
349
350
351 /* Redraw all the text widgets, to use a new font. */
352 void supported_redraw(void)
353 {
354   if (supported_w != NULL) {
355     clear_supported_text(proto_text);
356     set_supported_text(proto_text, PROTOCOL_SUPPORTED);
357     clear_supported_text(dfilter_text);
358     set_supported_text(dfilter_text, DFILTER_SUPPORTED);
359   }
360 }