Check on every iteration of a loop whether to pop up a dialog box,
[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  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@ethereal.com>
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 <gtk/gtk.h>
31 #include <string.h>
32
33 #include "supported_protos_dlg.h"
34 #include <epan/prefs.h>
35 #include "globals.h"
36 #include "gtkglobals.h"
37 #include "gui_utils.h"
38 #include "compat_macros.h"
39 #include "dlg_utils.h"
40 #include "font_utils.h"
41
42
43
44 static const char *proto_supported =
45 "The following %d protocols (and packet types) are currently\n"
46 "supported by Ethereal:\n\n";
47
48 static const char *dfilter_supported =
49 "The following per-protocol fields are currently supported by\n"
50 "Ethereal and can be used in display filters:\n";
51
52
53
54 typedef enum {
55   PROTOCOL_SUPPORTED,
56   DFILTER_SUPPORTED
57 } supported_type_t;
58
59 static void supported_destroy_cb(GtkWidget *w, gpointer data);
60 static void insert_text(GtkWidget *w, const char *buffer, int nchars);
61 static void set_supported_text(GtkWidget *w, supported_type_t type);
62
63 /*
64  * Keep a static pointer to the current "Supported" window, if any, so that
65  * if somebody tries to do "Help->Supported" while there's already a
66  * "Supported" window up, we just pop up the existing one, rather than
67  * creating a new one.
68 */
69 static GtkWidget *supported_w = NULL;
70
71 /*
72  * Keep static pointers to the text widgets as well (for text format changes).
73  */
74 static GtkWidget *proto_text, *dfilter_text;
75
76
77
78 void supported_cb(GtkWidget *w _U_, gpointer data _U_)
79 {
80
81   GtkWidget *main_vb, *bbox, *supported_nb, *ok_bt, *label, *txt_scrollw,
82     *proto_vb,
83 #if GTK_MAJOR_VERSION < 2
84     *dfilter_tb, *dfilter_vsb;
85 #else
86     *dfilter_vb;
87 #endif
88
89   if (supported_w != NULL) {
90     /* There's already a "Supported" dialog box; reactivate it. */
91     reactivate_window(supported_w);
92     return;
93   }
94
95   supported_w = window_new(GTK_WINDOW_TOPLEVEL, "Ethereal: Supported Protocols");
96   gtk_window_set_default_size(GTK_WINDOW(supported_w), DEF_WIDTH * 2/3, DEF_HEIGHT * 2/3);
97   gtk_container_border_width(GTK_CONTAINER(supported_w), 2);
98
99   /* Container for each row of widgets */
100   main_vb = gtk_vbox_new(FALSE, 1);
101   gtk_container_border_width(GTK_CONTAINER(main_vb), 1);
102   gtk_container_add(GTK_CONTAINER(supported_w), main_vb);
103   gtk_widget_show(main_vb);
104
105   /* supported topics container */
106   supported_nb = gtk_notebook_new();
107   gtk_container_add(GTK_CONTAINER(main_vb), supported_nb);
108
109
110   /* humm, gtk 1.2 does not support horizontal scrollbar for text widgets */
111
112   /* protocol list */
113   proto_vb = gtk_vbox_new(FALSE, 0);
114   gtk_container_border_width(GTK_CONTAINER(proto_vb), 1);
115
116   txt_scrollw = scrolled_window_new(NULL, NULL);
117 #if GTK_MAJOR_VERSION >= 2
118   gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(txt_scrollw), 
119                                    GTK_SHADOW_IN);
120 #endif
121   gtk_box_pack_start(GTK_BOX(proto_vb), txt_scrollw, TRUE, TRUE, 0);
122 #if GTK_MAJOR_VERSION < 2
123   gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(txt_scrollw),
124                                  GTK_POLICY_ALWAYS,
125                                  GTK_POLICY_ALWAYS);
126   proto_text = gtk_text_new(NULL, NULL);
127   gtk_text_set_editable(GTK_TEXT(proto_text), FALSE);
128   gtk_text_set_line_wrap(GTK_TEXT(proto_text), FALSE);
129   set_supported_text(proto_text, PROTOCOL_SUPPORTED);
130   gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(txt_scrollw),
131                                         proto_text);
132 #else
133   proto_text = gtk_text_view_new();
134   gtk_text_view_set_editable(GTK_TEXT_VIEW(proto_text), FALSE);
135   set_supported_text(proto_text, PROTOCOL_SUPPORTED);
136   gtk_container_add(GTK_CONTAINER(txt_scrollw), proto_text);
137 #endif
138   gtk_widget_show(txt_scrollw);
139   gtk_widget_show(proto_text);
140   gtk_widget_show(proto_vb);
141   label = gtk_label_new("Protocols");
142   gtk_notebook_append_page(GTK_NOTEBOOK(supported_nb), proto_vb, label);
143
144   /* display filter fields */
145 #if GTK_MAJOR_VERSION < 2
146   /* X windows have a maximum size of 32767.  Since the height can easily
147      exceed this, we have to jump through some hoops to have a functional
148      vertical scroll bar. */
149
150   dfilter_tb = gtk_table_new(2, 2, FALSE);
151   gtk_table_set_col_spacing (GTK_TABLE (dfilter_tb), 0, 3);
152   gtk_table_set_row_spacing (GTK_TABLE (dfilter_tb), 0, 3);
153   gtk_container_border_width(GTK_CONTAINER(dfilter_tb), 1);
154
155   txt_scrollw = scrolled_window_new(NULL, NULL);
156 #if GTK_MAJOR_VERSION >= 2
157   gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(txt_scrollw), 
158                                    GTK_SHADOW_IN);
159 #endif
160   gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(txt_scrollw),
161                                  GTK_POLICY_ALWAYS,
162                                  GTK_POLICY_NEVER);
163   dfilter_text = gtk_text_new(NULL, NULL);
164   dfilter_vsb = gtk_vscrollbar_new(GTK_TEXT(dfilter_text)->vadj);
165   if (prefs.gui_scrollbar_on_right) {
166     gtk_table_attach (GTK_TABLE (dfilter_tb), txt_scrollw, 0, 1, 0, 1,
167                             GTK_EXPAND | GTK_SHRINK | GTK_FILL,
168                             GTK_EXPAND | GTK_SHRINK | GTK_FILL, 0, 0);
169     gtk_table_attach (GTK_TABLE (dfilter_tb), dfilter_vsb, 1, 2, 0, 1,
170                             GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
171   } else {
172     gtk_table_attach (GTK_TABLE (dfilter_tb), txt_scrollw, 1, 2, 0, 1,
173                             GTK_EXPAND | GTK_SHRINK | GTK_FILL,
174                             GTK_EXPAND | GTK_SHRINK | GTK_FILL, 0, 0);
175     gtk_table_attach (GTK_TABLE (dfilter_tb), dfilter_vsb, 0, 1, 0, 1,
176                             GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
177   }
178   gtk_text_set_editable(GTK_TEXT(dfilter_text), FALSE);
179   gtk_text_set_line_wrap(GTK_TEXT(dfilter_text), FALSE);
180   set_supported_text(dfilter_text, DFILTER_SUPPORTED);
181   gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(txt_scrollw),
182                                         dfilter_text);
183 #else
184   dfilter_vb = gtk_vbox_new(FALSE, 0);
185   gtk_container_border_width(GTK_CONTAINER(dfilter_vb), 1);
186
187   txt_scrollw = scrolled_window_new(NULL, NULL);
188 #if GTK_MAJOR_VERSION >= 2
189     gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(txt_scrollw), 
190                                    GTK_SHADOW_IN);
191 #endif
192   gtk_box_pack_start(GTK_BOX(dfilter_vb), txt_scrollw, TRUE, TRUE, 0);
193   dfilter_text = gtk_text_view_new();
194   if (prefs.gui_scrollbar_on_right) {
195     gtk_scrolled_window_set_placement(GTK_SCROLLED_WINDOW(txt_scrollw),
196                                       GTK_CORNER_TOP_LEFT);
197   }
198   else {
199     gtk_scrolled_window_set_placement(GTK_SCROLLED_WINDOW(txt_scrollw),
200                                       GTK_CORNER_TOP_RIGHT);
201   }
202   gtk_text_view_set_editable(GTK_TEXT_VIEW(dfilter_text), FALSE);
203   set_supported_text(dfilter_text, DFILTER_SUPPORTED);
204   gtk_container_add(GTK_CONTAINER(txt_scrollw), dfilter_text);
205 #endif
206   gtk_widget_show(txt_scrollw);
207   gtk_widget_show(dfilter_text);
208 #if GTK_MAJOR_VERSION < 2
209   gtk_widget_show(dfilter_tb);
210   gtk_widget_show(dfilter_vsb);
211 #else
212   gtk_widget_show(dfilter_vb);
213 #endif
214   label = gtk_label_new("Display Filter Fields");
215 #if GTK_MAJOR_VERSION < 2
216   gtk_notebook_append_page(GTK_NOTEBOOK(supported_nb), dfilter_tb, label);
217 #else
218   gtk_notebook_append_page(GTK_NOTEBOOK(supported_nb), dfilter_vb, label);
219 #endif
220
221   /* XXX add other panels here ... */
222
223   gtk_widget_show(supported_nb);
224
225   /* Button row */
226   bbox = dlg_button_row_new(GTK_STOCK_OK, NULL);
227   gtk_box_pack_end(GTK_BOX(main_vb), bbox, FALSE, FALSE, 0);
228   gtk_widget_show(bbox);
229
230   ok_bt = OBJECT_GET_DATA(bbox, GTK_STOCK_OK);
231   window_set_cancel_button(supported_w, ok_bt, window_cancel_button_cb);
232
233   gtk_quit_add_destroy(gtk_main_level(), GTK_OBJECT(supported_w));
234
235   SIGNAL_CONNECT(supported_w, "delete_event", window_delete_event_cb, NULL);
236   SIGNAL_CONNECT(supported_w, "destroy", supported_destroy_cb, NULL);
237
238   gtk_widget_show(supported_w);
239   window_present(supported_w);
240 } /* supported_cb */
241
242 static void supported_destroy_cb(GtkWidget *w _U_, gpointer data _U_)
243 {
244   /* Note that we no longer have a Help window. */
245   supported_w = NULL;
246 }
247
248 static void insert_text(GtkWidget *w, const char *buffer, int nchars)
249 {
250 #if GTK_MAJOR_VERSION < 2
251     gtk_text_insert(GTK_TEXT(w), user_font_get_regular(), NULL, NULL, buffer, nchars);
252 #else
253     GtkTextBuffer *buf= gtk_text_view_get_buffer(GTK_TEXT_VIEW(w));
254     GtkTextIter    iter;
255
256     gtk_text_buffer_get_end_iter(buf, &iter);
257     gtk_widget_modify_font(w, user_font_get_regular());
258     if (!g_utf8_validate(buffer, -1, NULL))
259         printf("Invalid utf8 encoding: %s\n", buffer);
260     gtk_text_buffer_insert(buf, &iter, buffer, nchars);
261 #endif
262 }
263
264
265 static void set_supported_text(GtkWidget *w, supported_type_t type)
266 {
267
268 #define BUFF_LEN 4096
269 #define B_LEN    256
270   char buffer[BUFF_LEN];
271   header_field_info *hfinfo;
272   int i, len, maxlen = 0, maxlen2 = 0, maxlen4 = 0;
273 #if GTK_MAJOR_VERSION < 2
274   int maxlen3 = 0, nb_lines = 0;
275   int width, height;
276 #endif
277   const char *type_name;
278   void *cookie, *cookie2;
279   protocol_t *protocol;
280   const char *name, *short_name, *filter_name;
281   int namel = 0, short_namel = 0, filter_namel = 0;
282   int count, fcount;
283
284
285   /*
286    * XXX quick hack:
287    * the width and height computations are performed to make the
288    * horizontal scrollbar work in gtk1.2. This is only necessary for the
289    * PROTOCOL_SUPPORTED and DFILTER_SUPPORTED windows since all others should
290    * not have any horizontal scrollbar (line wrapping enabled).
291    */
292
293
294 #if GTK_MAJOR_VERSION < 2
295   gtk_text_freeze(GTK_TEXT(w));
296 #endif
297
298   switch(type) {
299
300   case PROTOCOL_SUPPORTED :
301     /* first pass to know the maximum length of first field */
302     count = 0;
303     for (i = proto_get_first_protocol(&cookie); i != -1;
304          i = proto_get_next_protocol(&cookie)) {
305             count++;
306             protocol = find_protocol_by_id(i);
307             name = proto_get_protocol_name(i);
308             short_name = proto_get_protocol_short_name(protocol);
309             filter_name = proto_get_protocol_filter_name(i);
310             if ((len = strlen(name)) > namel)
311                     namel = len;
312             if ((len = strlen(short_name)) > short_namel)
313                     short_namel = len;
314             if ((len = strlen(filter_name)) > filter_namel)
315                     filter_namel = len;
316     }
317     maxlen = namel + short_namel + filter_namel;
318
319     len = g_snprintf(buffer, BUFF_LEN, proto_supported, count);
320 #if GTK_MAJOR_VERSION < 2
321     maxlen2 = len;
322     width = gdk_string_width(user_font_get_regular(), buffer);
323     insert_text(w, buffer, maxlen2);
324 #else
325     insert_text(w, buffer, len);
326 #endif
327
328     /* ok, display the correctly aligned strings */
329     for (i = proto_get_first_protocol(&cookie); i != -1;
330          i = proto_get_next_protocol(&cookie)) {
331             protocol = find_protocol_by_id(i);
332             name = proto_get_protocol_name(i);
333             short_name = proto_get_protocol_short_name(protocol);
334             filter_name = proto_get_protocol_filter_name(i);
335  
336             /* the name used for sorting in the left column */
337             len = g_snprintf(buffer, BUFF_LEN, "%*s %*s %*s\n",
338                            -short_namel,  short_name,
339                            -namel,        name,
340                            -filter_namel, filter_name);
341 #if GTK_MAJOR_VERSION < 2
342             if (len > maxlen2) {
343                     maxlen2 = len;
344                     if ((len = gdk_string_width(user_font_get_regular(), buffer)) > width)
345                             width = len;
346             }
347             insert_text(w, buffer, strlen(buffer));
348             nb_lines++;
349 #else
350             insert_text(w, buffer, strlen(buffer));
351 #endif
352     }
353
354 #if GTK_MAJOR_VERSION < 2
355     height = (3 + nb_lines) * user_font_get_regular_height();
356     WIDGET_SET_SIZE(w, 20 + width, 20 + height);
357 #endif
358     break;
359
360   case DFILTER_SUPPORTED  :
361
362     /* XXX we should display hinfo->blurb instead of name (if not empty) */
363
364     /* first pass to know the maximum length of first and second fields */
365     for (i = proto_get_first_protocol(&cookie); i != -1;
366          i = proto_get_next_protocol(&cookie)) {
367
368             for (hfinfo = proto_get_first_protocol_field(i, &cookie2); hfinfo != NULL;
369                  hfinfo = proto_get_next_protocol_field(&cookie2)) {
370
371                     if (hfinfo->same_name_prev != NULL) /* ignore duplicate names */
372                             continue;
373
374                     if ((len = strlen(hfinfo->abbrev)) > maxlen)
375                             maxlen = len;
376                     if ((len = strlen(hfinfo->name)) > maxlen2)
377                             maxlen2 = len;
378                     if ((len = strlen(hfinfo->blurb)) > maxlen4)
379                             maxlen4 = len;
380             }
381     }
382
383 #if GTK_MAJOR_VERSION < 2
384     maxlen3 = strlen(dfilter_supported);
385     width = gdk_string_width(user_font_get_regular(), dfilter_supported);
386     insert_text(w, dfilter_supported, maxlen3);
387 #else
388     insert_text(w, dfilter_supported, strlen(dfilter_supported));
389 #endif
390
391     fcount = 0;
392     for (i = proto_get_first_protocol(&cookie); i != -1;
393          i = proto_get_next_protocol(&cookie)) {
394             protocol = find_protocol_by_id(i);
395             name = proto_get_protocol_name(i);
396             short_name = proto_get_protocol_short_name(protocol);
397             filter_name = proto_get_protocol_filter_name(i);
398
399             count = 0;
400             for (hfinfo = proto_get_first_protocol_field(i, &cookie2); hfinfo != NULL;
401                  hfinfo = proto_get_next_protocol_field(&cookie2)) {
402
403                     if (hfinfo->same_name_prev != NULL) /* ignore duplicate names */
404                             continue;
405                     count++;
406             }
407             fcount += count;
408
409             len = g_snprintf(buffer, BUFF_LEN, "\n%s - %s (%s) [%d fields]:\n",
410                            short_name, name, filter_name, count);
411             insert_text(w, buffer, len);
412
413             for (hfinfo = proto_get_first_protocol_field(i, &cookie2); hfinfo != NULL;
414                  hfinfo = proto_get_next_protocol_field(&cookie2)) {
415
416                     if (hfinfo->same_name_prev != NULL) /* ignore duplicate names */
417                             continue;
418
419                     type_name = ftype_pretty_name(hfinfo->type);
420                     len = g_snprintf(buffer, BUFF_LEN, "%*s %*s %*s (%s)\n",
421                                    -maxlen,  hfinfo->abbrev,
422                                    -maxlen2, hfinfo->name,
423                                    -maxlen4, hfinfo->blurb,
424                                    type_name);
425 #if GTK_MAJOR_VERSION < 2
426                     if (len > maxlen3) {
427                             maxlen3 = len;
428                             if ((len = gdk_string_width(user_font_get_regular(), buffer)) > width)
429                                     width = len;
430                     }
431                     insert_text(w, buffer, strlen(buffer));
432                     nb_lines ++;
433 #else
434                     insert_text(w, buffer, strlen(buffer));
435 #endif
436             }
437     }
438     len = g_snprintf(buffer, BUFF_LEN, "\n-- Total %d fields\n", fcount);
439     insert_text(w, buffer, len);
440
441 #if GTK_MAJOR_VERSION < 2
442     height = (5 + nb_lines) * user_font_get_regular_height();
443     WIDGET_SET_SIZE(w, 20 + width, 20 + height);
444 #endif
445     break;
446   default :
447     g_assert_not_reached();
448     break;
449   } /* switch(type) */
450 #if GTK_MAJOR_VERSION < 2
451   gtk_text_thaw(GTK_TEXT(w));
452 #endif
453 } /* set_supported_text */
454
455
456 static void clear_supported_text(GtkWidget *w)
457 {
458 #if GTK_MAJOR_VERSION < 2
459   GtkText *txt = GTK_TEXT(w);
460
461   gtk_text_set_point(txt, 0);
462   /* Keep GTK+ 1.2.3 through 1.2.6 from dumping core - see
463      http://www.ethereal.com/lists/ethereal-dev/199912/msg00312.html and
464      http://www.gnome.org/mailing-lists/archives/gtk-devel-list/1999-October/0051.shtml
465      for more information */
466   gtk_adjustment_set_value(txt->vadj, 0.0);
467   gtk_text_forward_delete(txt, gtk_text_get_length(txt));
468 #else
469   GtkTextBuffer *buf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(w));
470
471   gtk_text_buffer_set_text(buf, "", 0);
472 #endif
473 }
474
475
476 /* Redraw all the text widgets, to use a new font. */
477 void supported_redraw(void)
478 {
479   if (supported_w != NULL) {
480 #if GTK_MAJOR_VERSION < 2
481     gtk_text_freeze(GTK_TEXT(proto_text));
482 #endif
483     clear_supported_text(proto_text);
484     set_supported_text(proto_text, PROTOCOL_SUPPORTED);
485 #if GTK_MAJOR_VERSION < 2
486     gtk_text_thaw(GTK_TEXT(proto_text));
487
488     gtk_text_freeze(GTK_TEXT(dfilter_text));
489 #endif
490     clear_supported_text(dfilter_text);
491     set_supported_text(dfilter_text, DFILTER_SUPPORTED);
492 #if GTK_MAJOR_VERSION < 2
493     gtk_text_thaw(GTK_TEXT(dfilter_text));
494 #endif
495   }
496 }