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