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