Fix the last of the Win64 problems in the gtk directory.
[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 #include <string.h>
30
31 #include <gtk/gtk.h>
32
33 #include <epan/prefs.h>
34
35 #include "../globals.h"
36
37 #include "gtk/supported_protos_dlg.h"
38 #include "gtk/gtkglobals.h"
39 #include "gtk/gui_utils.h"
40 #include "gtk/dlg_utils.h"
41 #include "gtk/font_utils.h"
42
43
44
45 static const char *proto_supported =
46 "The following %d protocols (and packet types) are currently\n"
47 "supported by Wireshark:\n\n";
48
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";
52
53
54
55 typedef enum {
56   PROTOCOL_SUPPORTED,
57   DFILTER_SUPPORTED
58 } supported_type_t;
59
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);
63
64 /*
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.
69 */
70 static GtkWidget *supported_w = NULL;
71
72 /*
73  * Keep static pointers to the text widgets as well (for text format changes).
74  */
75 static GtkWidget *proto_text, *dfilter_text;
76
77
78
79 void supported_cb(GtkWidget *w _U_, gpointer data _U_)
80 {
81
82   GtkWidget *main_vb, *bbox, *supported_nb, *ok_bt, *label, *txt_scrollw,
83     *proto_vb,
84     *dfilter_vb;
85
86   if (supported_w != NULL) {
87     /* There's already a "Supported" dialog box; reactivate it. */
88     reactivate_window(supported_w);
89     return;
90   }
91
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);
95
96   /* Container for each row of widgets */
97   main_vb = gtk_vbox_new(FALSE, 1);
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);
101
102   /* supported topics container */
103   supported_nb = gtk_notebook_new();
104   gtk_container_add(GTK_CONTAINER(main_vb), supported_nb);
105
106
107   /* humm, gtk 1.2 does not support horizontal scrollbar for text widgets */
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   gtk_quit_add_destroy(gtk_main_level(), GTK_OBJECT(supported_w));
166
167   g_signal_connect(supported_w, "delete_event", G_CALLBACK(window_delete_event_cb), NULL);
168   g_signal_connect(supported_w, "destroy", G_CALLBACK(supported_destroy_cb), NULL);
169
170   gtk_widget_show(supported_w);
171   window_present(supported_w);
172 } /* supported_cb */
173
174 static void supported_destroy_cb(GtkWidget *w _U_, gpointer data _U_)
175 {
176   /* Note that we no longer have a Help window. */
177   supported_w = NULL;
178 }
179
180 static void insert_text(GtkWidget *w, const char *buffer, int nchars)
181 {
182     GtkTextBuffer *buf= gtk_text_view_get_buffer(GTK_TEXT_VIEW(w));
183     GtkTextIter    iter;
184
185     gtk_text_buffer_get_end_iter(buf, &iter);
186     gtk_widget_modify_font(w, user_font_get_regular());
187     if (!g_utf8_validate(buffer, -1, NULL))
188         printf("Invalid utf8 encoding: %s\n", buffer);
189     gtk_text_buffer_insert(buf, &iter, buffer, nchars);
190 }
191
192
193 static void set_supported_text(GtkWidget *w, supported_type_t type)
194 {
195
196 #define BUFF_LEN 4096
197 #define B_LEN    256
198   char buffer[BUFF_LEN];
199   header_field_info *hfinfo;
200   int i, len, maxlen = 0, maxlen2 = 0, maxlen4 = 0;
201   const char *type_name;
202   void *cookie, *cookie2;
203   protocol_t *protocol;
204   const char *name, *short_name, *filter_name;
205   int namel = 0, short_namel = 0, filter_namel = 0;
206   int count, fcount;
207
208
209   /*
210    * XXX quick hack:
211    * the width and height computations are performed to make the
212    * horizontal scrollbar work in gtk1.2. This is only necessary for the
213    * PROTOCOL_SUPPORTED and DFILTER_SUPPORTED windows since all others should
214    * not have any horizontal scrollbar (line wrapping enabled).
215    */
216
217
218   switch(type) {
219
220   case PROTOCOL_SUPPORTED :
221     /* first pass to know the maximum length of first field */
222     count = 0;
223     for (i = proto_get_first_protocol(&cookie); i != -1;
224          i = proto_get_next_protocol(&cookie)) {
225             count++;
226             protocol = find_protocol_by_id(i);
227             name = proto_get_protocol_name(i);
228             short_name = proto_get_protocol_short_name(protocol);
229             filter_name = proto_get_protocol_filter_name(i);
230             if ((len = (int) strlen(name)) > namel)
231                     namel = len;
232             if ((len = (int) strlen(short_name)) > short_namel)
233                     short_namel = len;
234             if ((len = (int) strlen(filter_name)) > filter_namel)
235                     filter_namel = len;
236     }
237     maxlen = namel + short_namel + filter_namel;
238
239     len = g_snprintf(buffer, BUFF_LEN, proto_supported, count);
240     insert_text(w, buffer, len);
241
242     /* ok, display the correctly aligned strings */
243     for (i = proto_get_first_protocol(&cookie); i != -1;
244          i = proto_get_next_protocol(&cookie)) {
245             protocol = find_protocol_by_id(i);
246             name = proto_get_protocol_name(i);
247             short_name = proto_get_protocol_short_name(protocol);
248             filter_name = proto_get_protocol_filter_name(i);
249  
250             /* the name used for sorting in the left column */
251             len = g_snprintf(buffer, BUFF_LEN, "%*s %*s %*s\n",
252                            -short_namel,  short_name,
253                            -namel,        name,
254                            -filter_namel, filter_name);
255             insert_text(w, buffer, (int) strlen(buffer));
256     }
257
258     break;
259
260   case DFILTER_SUPPORTED  :
261
262     /* XXX we should display hinfo->blurb instead of name (if not empty) */
263
264     /* first pass to know the maximum length of first and second fields */
265     for (i = proto_get_first_protocol(&cookie); i != -1;
266          i = proto_get_next_protocol(&cookie)) {
267
268             for (hfinfo = proto_get_first_protocol_field(i, &cookie2); hfinfo != NULL;
269                  hfinfo = proto_get_next_protocol_field(&cookie2)) {
270
271                     if (hfinfo->same_name_prev != NULL) /* ignore duplicate names */
272                             continue;
273
274                     if ((len = (int) strlen(hfinfo->abbrev)) > maxlen)
275                             maxlen = len;
276                     if ((len = (int) strlen(hfinfo->name)) > maxlen2)
277                             maxlen2 = len;
278                     if (hfinfo->blurb != NULL) {
279                             if ((len = (int) strlen(hfinfo->blurb)) > maxlen4)
280                                 maxlen4 = len;
281                     }
282             }
283     }
284
285     insert_text(w, dfilter_supported, (int) strlen(dfilter_supported));
286
287     fcount = 0;
288     for (i = proto_get_first_protocol(&cookie); i != -1;
289          i = proto_get_next_protocol(&cookie)) {
290             protocol = find_protocol_by_id(i);
291             name = proto_get_protocol_name(i);
292             short_name = proto_get_protocol_short_name(protocol);
293             filter_name = proto_get_protocol_filter_name(i);
294
295             count = 0;
296             for (hfinfo = proto_get_first_protocol_field(i, &cookie2); hfinfo != NULL;
297                  hfinfo = proto_get_next_protocol_field(&cookie2)) {
298
299                     if (hfinfo->same_name_prev != NULL) /* ignore duplicate names */
300                             continue;
301                     count++;
302             }
303             fcount += count;
304
305             len = g_snprintf(buffer, BUFF_LEN, "\n%s - %s (%s) [%d fields]:\n",
306                            short_name, name, filter_name, count);
307             insert_text(w, buffer, len);
308
309             for (hfinfo = proto_get_first_protocol_field(i, &cookie2); hfinfo != NULL;
310                  hfinfo = proto_get_next_protocol_field(&cookie2)) {
311
312                     if (hfinfo->same_name_prev != NULL) /* ignore duplicate names */
313                             continue;
314
315                     type_name = ftype_pretty_name(hfinfo->type);
316                     if (hfinfo->blurb != NULL && hfinfo->blurb[0] != '\0') {
317                             len = g_snprintf(buffer, BUFF_LEN, "%*s %*s %*s (%s)\n",
318                                              -maxlen,  hfinfo->abbrev,
319                                              -maxlen2, hfinfo->name,
320                                              -maxlen4, hfinfo->blurb,
321                                              type_name);
322                     } else {
323                             len = g_snprintf(buffer, BUFF_LEN, "%*s %*s (%s)\n",
324                                              -maxlen,  hfinfo->abbrev,
325                                              -maxlen2, hfinfo->name,
326                                              type_name);
327                     }
328                     insert_text(w, buffer, (int) strlen(buffer));
329             }
330     }
331     len = g_snprintf(buffer, BUFF_LEN, "\n-- Total %d fields\n", fcount);
332     insert_text(w, buffer, len);
333
334     break;
335   default :
336     g_assert_not_reached();
337     break;
338   } /* switch(type) */
339 } /* set_supported_text */
340
341
342 static void clear_supported_text(GtkWidget *w)
343 {
344   GtkTextBuffer *buf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(w));
345
346   gtk_text_buffer_set_text(buf, "", 0);
347 }
348
349
350 /* Redraw all the text widgets, to use a new font. */
351 void supported_redraw(void)
352 {
353   if (supported_w != NULL) {
354     clear_supported_text(proto_text);
355     set_supported_text(proto_text, PROTOCOL_SUPPORTED);
356     clear_supported_text(dfilter_text);
357     set_supported_text(dfilter_text, DFILTER_SUPPORTED);
358   }
359 }