Fix last minute edit mistake in add_byte_tab.
[obnox/wireshark/wip.git] / gtk / proto_draw.c
1 /* proto_draw.c
2  * Routines for GTK+ packet display
3  *
4  * $Id: proto_draw.c,v 1.30 2001/03/23 17:14:40 jfoster Exp $
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@zing.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * Jeff Foster,    2001/03/12,  added support for displaying named
11  *                              data sources as tabbed hex windows
12  * 
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  * 
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  * 
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
26  */
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #ifdef HAVE_SYS_TYPES_H
33 # include <sys/types.h>
34 #endif
35
36 #include <ctype.h>
37 #include <stdarg.h>
38
39 #include <gtk/gtk.h>
40
41 #ifdef NEED_SNPRINTF_H
42 # include "snprintf.h"
43 #endif
44
45 #include <stdio.h>
46 #include "main.h"
47 #include "packet.h"
48 #include "util.h"
49 #include "menu.h"
50 #include "keys.h"
51
52 #include "colors.h"
53 #include "prefs.h"
54 #include "proto_draw.h"
55 #include "packet_win.h"
56 #include "gtkglobals.h"
57
58 #define BYTE_VIEW_WIDTH    16
59 #define BYTE_VIEW_SEP      8
60
61
62 GtkWidget *byte_nb_ptr;
63
64 static void
65 proto_tree_draw_node(GNode *node, gpointer data);
66
67
68 GtkWidget*
69 get_notebook_bv_ptr(  GtkWidget *nb_ptr){
70
71 /* Get the current text window for the notebook */
72   return gtk_object_get_data(GTK_OBJECT(nb_ptr), E_BYTE_VIEW_TEXT_INFO_KEY);
73 }
74
75
76 int get_byte_view_data( GtkWidget *byte_view_notebook, guint8 **data_ptr) {
77
78 /* get the data pointer and data length for a hex window */
79 /* return the length of the data or -1 on error          */
80
81         GtkWidget *byte_view = get_notebook_bv_ptr( byte_view_notebook);
82
83         if ( !byte_view)
84                 return -1;      
85         if ((*data_ptr = gtk_object_get_data(GTK_OBJECT(byte_view), E_BYTE_VIEW_DATA_PTR_KEY)))
86                 return GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(byte_view),
87                         E_BYTE_VIEW_DATA_LEN_KEY));
88         return -1;
89 }
90
91
92 int get_byte_view_and_data( GtkWidget *byte_view_notebook, GtkWidget **byte_view, guint8 **data_ptr) {
93
94 /* Get both byte_view widget pointer and the data pointer */
95 /* return the data length or -1 if error                  */
96
97         *byte_view = get_notebook_bv_ptr( byte_view_notebook);
98         if ( *byte_view)
99                 return get_byte_view_data( byte_view_notebook, data_ptr);
100         return -1;
101 }
102
103
104 void
105 set_notebook_page(  GtkWidget *nb_ptr, int num){
106
107 /* Set the current text window for the notebook and set the */
108 /* text window pointer storage */
109
110   GtkWidget* child;
111
112   gtk_notebook_set_page ( GTK_NOTEBOOK( nb_ptr), num);
113
114   child = gtk_notebook_get_nth_page( GTK_NOTEBOOK(nb_ptr), num);
115   child = gtk_object_get_data(GTK_OBJECT(child), E_BYTE_VIEW_TEXT_INFO_KEY);
116   gtk_object_set_data(GTK_OBJECT(nb_ptr), E_BYTE_VIEW_TEXT_INFO_KEY, child);
117 }
118
119
120 int find_notebook_page( GtkWidget *nb_ptr, gchar *label){
121
122 /* find the notebook page number for this label */
123
124         int i = -1;
125         gchar *ptr;
126         GtkWidget* child;
127
128         while(( child = gtk_notebook_get_nth_page(GTK_NOTEBOOK(nb_ptr), ++i))){
129                 child = gtk_notebook_get_tab_label(GTK_NOTEBOOK(nb_ptr), child);
130                 gtk_notebook_get_tab_label(GTK_NOTEBOOK(nb_ptr), child);
131                 gtk_label_get(GTK_LABEL(child), &ptr);
132                 if (!strcmp( label, ptr))
133                         return i;
134         }
135         return -1;
136 }
137
138
139 /* Redraw a given byte view window. */
140 void
141 redraw_hex_dump(GtkWidget *nb, frame_data *fd, field_info *finfo)
142 {
143   GtkWidget* bv;
144   guint8* data;
145   int len;
146  
147   len = get_byte_view_and_data( byte_nb_ptr, &bv, &data);
148   if ( bv) 
149     packet_hex_print(GTK_TEXT(bv), data, fd, finfo, len);
150 }
151
152 void
153 redraw_hex_dump_all(void)
154 {
155  
156   if (cfile.current_frame != NULL)
157       redraw_hex_dump( byte_nb_ptr, cfile.current_frame, finfo_selected);
158   
159   redraw_hex_dump_packet_wins();
160 }
161
162
163 static void
164 expand_tree(GtkCTree *ctree, GtkCTreeNode *node, gpointer user_data)
165 {
166         field_info      *finfo;
167         gboolean        *val;
168
169         finfo = gtk_ctree_node_get_row_data( ctree, node);
170         g_assert(finfo);
171
172         val = &tree_is_expanded[finfo->tree_type];
173         *val = TRUE;
174 }
175
176 static void
177 collapse_tree(GtkCTree *ctree, GtkCTreeNode *node, gpointer user_data)
178 {
179         field_info      *finfo;
180         gboolean        *val;
181
182         finfo = gtk_ctree_node_get_row_data( ctree, node);
183         g_assert(finfo);
184
185         val = &tree_is_expanded[finfo->tree_type];
186         *val = FALSE;
187 }
188
189 /* Which byte the offset is referring to. Associates
190  * whitespace with the preceding digits. */
191 static int
192 byte_num(int offset, int start_point)
193 {
194         return (offset - start_point) / 3;
195 }
196
197
198 /* If the user selected a certain byte in the byte view, try to find
199  * the item in the GUI proto_tree that corresponds to that byte, and
200  * select it. */
201 static gint
202 byte_view_select(GtkWidget *widget, GdkEventButton *event, gpointer data)
203 {
204         GtkCTree        *ctree = GTK_CTREE(tree_view);
205         GtkCTreeNode    *node, *parent;
206         field_info      *finfo;
207         GtkText         *bv = GTK_TEXT(widget);
208         int             row, column;
209         int             byte;
210         gchar           *name;
211
212         /* The column of the first hex digit in the first half */
213         const int       digits_start_1 = 6;
214         /* The column of the last hex digit in the first half. */
215         const int       digits_end_1 = 28;
216
217         /* The column of the first hex digit in the second half */
218         const int       digits_start_2 = 31;
219         /* The column of the last hex digit in the second half. */
220         const int       digits_end_2 = 53;
221
222         /* The column of the first "text dump" character in first half. */
223         const int       text_start_1 = 57;
224         /* The column of the last "text dump" character in first half. */
225         const int       text_end_1 = 64;
226
227         /* The column of the first "text dump" character in second half. */
228         const int       text_start_2 = 66;
229         /* The column of the last "text dump" character in second half. */
230         const int       text_end_2 = 73;
231
232         /* Given the mouse (x,y) and the current GtkText (h,v)
233          * adjustments, and the size of the font, figure out
234          * which text column/row the user selected. This could be off
235          * if the bold version of the font is bigger than the
236          * regular version of the font. */
237         column = (bv->hadj->value + event->x) / m_font_width;
238         row = (bv->vadj->value + event->y) / m_font_height;
239
240         /* Given the column and row, determine which byte offset
241          * the user clicked on. */
242         if (column >= digits_start_1 && column <= digits_end_1) {
243                 byte = byte_num(column, digits_start_1);
244                 if (byte == -1) {
245                         return FALSE;
246                 }
247         }
248         else if (column >= digits_start_2 && column <= digits_end_2) {
249                 byte = byte_num(column, digits_start_2);
250                 if (byte == -1) {
251                         return FALSE;
252                 }
253                 byte += 8;
254         }
255         else if (column >= text_start_1 && column <= text_end_1) {
256                 byte = column - text_start_1;
257         }
258         else if (column >= text_start_2 && column <= text_end_2) {
259                 byte = 8 + column - text_start_2;
260         }
261         else {
262                 /* The user didn't select a hex digit or
263                  * text-dump character. */
264                 return FALSE;
265         }
266
267         /* Add the number of bytes from the previous rows. */
268         byte += row * 16;
269
270         /* Get the data source name */
271         name = gtk_object_get_data(GTK_OBJECT(widget), E_BYTE_VIEW_NAME_KEY);
272
273         /* Find the finfo that corresponds to our byte. */
274         finfo = proto_find_field_from_offset(cfile.protocol_tree, byte, name);
275
276         if (!finfo) {
277                 return FALSE;
278         }
279
280         node = gtk_ctree_find_by_row_data(ctree, NULL, finfo);
281         g_assert(node);
282
283         /* Expand and select our field's row */
284         gtk_ctree_expand(ctree, node);
285         gtk_ctree_select(ctree, node);
286         expand_tree(ctree, node, NULL);
287
288         /* ... and its parents */
289         parent = GTK_CTREE_ROW(node)->parent;
290         while (parent) {
291                 gtk_ctree_expand(ctree, parent);
292                 expand_tree(ctree, parent, NULL);
293                 parent = GTK_CTREE_ROW(parent)->parent;
294         }
295
296         /* And position the window so the selection is visible.
297          * Position the selection in the middle of the viewable
298          * pane. */
299         gtk_ctree_node_moveto(ctree, node, 0, .5, 0);
300
301         return FALSE;
302 }
303
304 /* Calls functions for different mouse-button presses. */
305 static gint
306 byte_view_button_press_cb(GtkWidget *widget, GdkEvent *event, gpointer data)
307 {
308         GdkEventButton *event_button = NULL;
309
310         if(widget == NULL || event == NULL || data == NULL) {
311                 return FALSE;
312         }
313         
314         if(event->type == GDK_BUTTON_PRESS) {
315                 event_button = (GdkEventButton *) event;
316
317                 switch(event_button->button) {
318
319                 case 1:
320                         return byte_view_select(widget, event_button, data);
321                 case 3:
322                         return popup_menu_handler(widget, event, data);
323                 default:
324                         return FALSE;
325                 }
326         }
327
328         return FALSE;
329 }
330
331
332 void
333 create_byte_view(gint bv_size, GtkWidget *pane, GtkWidget **byte_nb_p,
334                 GtkWidget **bv_scrollw_p, int pos)
335 {
336   GtkWidget *byte_scrollw, *byte_nb;
337
338   byte_nb = gtk_notebook_new();
339   gtk_notebook_set_tab_pos ( GTK_NOTEBOOK(byte_nb), GTK_POS_BOTTOM);
340
341   gtk_paned_pack2(GTK_PANED(pane), byte_nb, FALSE, FALSE);
342   gtk_widget_show(byte_nb);
343
344   /* Byte view.  Create a scrolled window for the text. */
345   byte_scrollw = gtk_scrolled_window_new(NULL, NULL);
346   *byte_nb_p = byte_nb;
347   *bv_scrollw_p = byte_scrollw;
348 }
349
350
351 void
352 byte_view_realize_cb( GtkWidget *bv, gpointer data){
353
354    guint8* byte_data = gtk_object_get_data(GTK_OBJECT(bv), E_BYTE_VIEW_DATA_PTR_KEY);
355    int     byte_len = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(bv), E_BYTE_VIEW_DATA_LEN_KEY));
356
357    packet_hex_print(GTK_TEXT(bv), byte_data, cfile.current_frame, NULL, byte_len);
358
359 }
360
361
362 GtkWidget *add_byte_tab( GtkWidget *byte_nb, const char *name, const guint8 *data, int len)
363 {
364   GtkWidget *byte_view, *byte_scrollw, *label;
365   gchar *name_ptr;
366
367   /* Byte view.  Create a scrolled window for the text. */
368   byte_scrollw = gtk_scrolled_window_new(NULL, NULL);
369   /* Add scrolled pane to tabbed window */
370   label = gtk_label_new (name);
371   gtk_notebook_append_page (GTK_NOTEBOOK(byte_nb), byte_scrollw, label);
372
373   /* The horizontal scrollbar of the scroll-window doesn't seem
374    * to affect the GtkText widget at all, even when line wrapping
375    * is turned off in the GtkText widget and there is indeed more
376    * horizontal data. */
377   gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(byte_scrollw),
378                         /* Horizontal */GTK_POLICY_NEVER,
379                         /* Vertical*/   GTK_POLICY_ALWAYS);
380   set_scrollbar_placement_scrollw(byte_scrollw,  prefs.gui_scrollbar_on_right);
381   remember_scrolled_window(byte_scrollw);
382   gtk_widget_show(byte_scrollw);
383
384   byte_view = gtk_text_new(NULL, NULL);
385   gtk_text_set_editable(GTK_TEXT(byte_view), FALSE);
386   gtk_text_set_word_wrap(GTK_TEXT(byte_view), FALSE);
387   gtk_text_set_line_wrap(GTK_TEXT(byte_view), FALSE);
388   gtk_object_set_data(GTK_OBJECT(byte_view), E_BYTE_VIEW_DATA_PTR_KEY, (guint8*)data);
389   gtk_object_set_data(GTK_OBJECT(byte_view), E_BYTE_VIEW_DATA_LEN_KEY, GINT_TO_POINTER(len));
390   gtk_label_get(GTK_LABEL(label), &name_ptr);
391   gtk_object_set_data(GTK_OBJECT(byte_view), E_BYTE_VIEW_NAME_KEY, name_ptr);
392   gtk_container_add(GTK_CONTAINER(byte_scrollw), byte_view);
393
394   gtk_signal_connect(GTK_OBJECT(byte_view), "show",
395                      GTK_SIGNAL_FUNC(byte_view_realize_cb), NULL);
396   gtk_signal_connect(GTK_OBJECT(byte_view), "button_press_event",
397                      GTK_SIGNAL_FUNC(byte_view_button_press_cb),
398                      gtk_object_get_data(GTK_OBJECT(popup_menu_object), PM_HEXDUMP_KEY));
399
400   gtk_widget_show(byte_view);
401
402   gtk_object_set_data(GTK_OBJECT(byte_scrollw), E_BYTE_VIEW_TEXT_INFO_KEY,
403             byte_view);
404
405 /* no tabs if this is the first page */
406   if ( !(gtk_notebook_page_num(GTK_NOTEBOOK(byte_nb), byte_scrollw)))
407         gtk_notebook_set_show_tabs ( GTK_NOTEBOOK(byte_nb), FALSE);
408   else
409         gtk_notebook_set_show_tabs ( GTK_NOTEBOOK(byte_nb), TRUE);
410
411   return byte_view;
412 }
413
414
415 int add_byte_view(const char *name, const guint8 *data, int len){
416
417         add_byte_tab( byte_nb_ptr, name, data,len);
418
419         return 0;
420 }
421
422
423 void
424 packet_hex_print_common(GtkText *bv, guint8 *pd, int len, int bstart, int bend, int encoding)
425 {
426   gint     i = 0, j, k, cur;
427   guchar   line[128], hexchars[] = "0123456789abcdef", c = '\0';
428   GdkFont *cur_font, *new_font;
429   GdkColor *fg, *bg;
430   gboolean reverse, newreverse;
431
432   /* Freeze the text for faster display */
433   gtk_text_freeze(bv);
434
435   /* Clear out the text */
436   gtk_text_set_point(bv, 0);
437   /* Keep GTK+ 1.2.3 through 1.2.6 from dumping core - see 
438      http://www.ethereal.com/lists/ethereal-dev/199912/msg00312.html and
439      http://www.gnome.org/mailing-lists/archives/gtk-devel-list/1999-October/0051.shtml
440      for more information */
441   gtk_adjustment_set_value(bv->vadj, 0.0);
442   gtk_text_forward_delete(bv, gtk_text_get_length(bv));
443
444   while (i < len) {
445     /* Print the line number */
446     sprintf(line, "%04x  ", i);
447     
448     /* Display with inverse video ? */
449     if (prefs.gui_hex_dump_highlight_style) {
450       gtk_text_insert(bv, m_r_font, &BLACK, &WHITE, line, -1);
451       /* Do we start in reverse? */
452       reverse = i >= bstart && i < bend;
453       fg = reverse ? &WHITE : &BLACK;
454       bg = reverse ? &BLACK : &WHITE;
455       j   = i;
456       k   = i + BYTE_VIEW_WIDTH;
457       cur = 0;
458       /* Print the hex bit */
459       while (i < k) {
460         if (i < len) {
461           line[cur++] = hexchars[(pd[i] & 0xf0) >> 4];
462           line[cur++] = hexchars[pd[i] & 0x0f];
463         } else {
464           line[cur++] = ' '; line[cur++] = ' ';
465         }
466         i++;
467         newreverse = i >= bstart && i < bend;
468         /* Have we gone from reverse to plain? */
469         if (reverse && (reverse != newreverse)) {
470           gtk_text_insert(bv, m_r_font, fg, bg, line, cur);
471           fg = &BLACK;
472           bg = &WHITE;
473           cur = 0;
474         }
475         /* Inter byte space if not at end of line */
476         if (i < k) {
477           line[cur++] = ' ';
478           /* insert a space every BYTE_VIEW_SEP bytes */
479           if( ( i % BYTE_VIEW_SEP ) == 0 ) {
480             line[cur++] = ' ';
481           }
482         }
483         /* Have we gone from plain to reversed? */
484         if (!reverse && (reverse != newreverse)) {
485           gtk_text_insert(bv, m_r_font, fg, bg, line, cur);
486           fg = &WHITE;
487           bg = &BLACK;
488           cur = 0;
489         }
490         reverse = newreverse;
491       }
492       /* Print remaining part of line */
493       gtk_text_insert(bv, m_r_font, fg, bg, line, cur);
494       cur = 0;
495       /* Print some space at the end of the line */
496       line[cur++] = ' '; line[cur++] = ' '; line[cur++] = ' ';
497       gtk_text_insert(bv, m_r_font, &BLACK, &WHITE, line, cur);
498       cur = 0;
499
500       /* Print the ASCII bit */
501       i = j;
502       /* Do we start in reverse? */
503       reverse = i >= bstart && i < bend;
504       fg = reverse ? &WHITE : &BLACK;
505       bg = reverse ? &BLACK : &WHITE;
506       while (i < k) {
507         if (i < len) {
508           if (encoding == CHAR_ASCII) {
509             c = pd[i];
510           }
511           else if (encoding == CHAR_EBCDIC) {
512             c = EBCDIC_to_ASCII1(pd[i]);
513           }
514           else {
515                   g_assert_not_reached();
516           }
517           line[cur++] = (isprint(c)) ? c : '.';
518         } else {
519           line[cur++] = ' ';
520         }
521         i++;
522         newreverse = i >= bstart && i < bend;
523         /* Have we gone from reverse to plain? */
524         if (reverse && (reverse != newreverse)) {
525           gtk_text_insert(bv, m_r_font, fg, bg, line, cur);
526           fg = &BLACK;
527           bg = &WHITE;
528           cur = 0;
529         }
530         if (i < k) {
531           /* insert a space every BYTE_VIEW_SEP bytes */
532           if( ( i % BYTE_VIEW_SEP ) == 0 ) {
533             line[cur++] = ' ';
534           }
535         }
536         /* Have we gone from plain to reversed? */
537         if (!reverse && (reverse != newreverse)) {
538           gtk_text_insert(bv, m_r_font, fg, bg, line, cur);
539           fg = &WHITE;
540           bg = &BLACK;
541           cur = 0;
542         }
543         reverse = newreverse;
544       }
545       /* Print remaining part of line */
546       gtk_text_insert(bv, m_r_font, fg, bg, line, cur);
547       cur = 0;
548       line[cur++] = '\n';
549       line[cur]   = '\0';
550       gtk_text_insert(bv, m_r_font, &BLACK, &WHITE, line, -1);
551     }
552     else {
553       gtk_text_insert(bv, m_r_font, NULL, NULL, line, -1);
554       /* Do we start in bold? */
555       cur_font = (i >= bstart && i < bend) ? m_b_font : m_r_font;
556       j   = i;
557       k   = i + BYTE_VIEW_WIDTH;
558       cur = 0;
559       /* Print the hex bit */
560       while (i < k) {
561         if (i < len) {
562           line[cur++] = hexchars[(pd[i] & 0xf0) >> 4];
563           line[cur++] = hexchars[pd[i] & 0x0f];
564         } else {
565           line[cur++] = ' '; line[cur++] = ' ';
566         }
567         line[cur++] = ' ';
568         i++;
569         /* insert a space every BYTE_VIEW_SEP bytes */
570         if( ( i % BYTE_VIEW_SEP ) == 0 ) line[cur++] = ' ';
571         /* Did we cross a bold/plain boundary? */
572         new_font = (i >= bstart && i < bend) ? m_b_font : m_r_font;
573         if (cur_font != new_font) {
574           gtk_text_insert(bv, cur_font, NULL, NULL, line, cur);
575           cur_font = new_font;
576           cur = 0;
577         }
578       }
579       line[cur++] = ' ';
580       gtk_text_insert(bv, cur_font, NULL, NULL, line, cur);
581
582       cur = 0;
583       i = j;
584       /* Print the ASCII bit */
585       cur_font = (i >= bstart && i < bend) ? m_b_font : m_r_font;
586       while (i < k) {
587         if (i < len) {
588           if (encoding == CHAR_ASCII) {
589             c = pd[i];
590           }
591           else if (encoding == CHAR_EBCDIC) {
592             c = EBCDIC_to_ASCII1(pd[i]);
593           }
594           else {
595                   g_assert_not_reached();
596           }
597           line[cur++] = (isprint(c)) ? c : '.';
598         } else {
599           line[cur++] = ' ';
600         }
601         i++;
602         /* insert a space every BYTE_VIEW_SEP bytes */
603         if( ( i % BYTE_VIEW_SEP ) == 0 ) line[cur++] = ' ';
604         /* Did we cross a bold/plain boundary? */
605         new_font = (i >= bstart && i < bend) ? m_b_font : m_r_font;
606         if (cur_font != new_font) {
607           gtk_text_insert(bv, cur_font, NULL, NULL, line, cur);
608           cur_font = new_font;
609           cur = 0;
610         }
611       }
612       line[cur++] = '\n';
613       line[cur]   = '\0';
614       gtk_text_insert(bv, cur_font, NULL, NULL, line, -1);
615     }
616   }
617
618   /* scroll text into position */
619   gtk_text_thaw(bv); /* must thaw before adjusting scroll bars */
620   if ( bstart > 0 ) {
621     int lineheight, linenum;
622     float scrollval;
623
624     linenum = bstart / BYTE_VIEW_WIDTH;
625     /* This is the lineheight that the GtkText widget uses when drawing text. */
626     lineheight = m_b_font->ascent + m_b_font->descent;
627     scrollval = MIN(linenum * lineheight,bv->vadj->upper - bv->vadj->page_size);
628
629     gtk_adjustment_set_value(bv->vadj, scrollval);
630   }
631 }
632
633 void
634 packet_hex_print(GtkText *bv, guint8 *pd, frame_data *fd, field_info *finfo, int len){
635
636   /* do the initial printing and save the information needed    */
637   /* to redraw the display if preferences change.               */
638
639   int bstart, bend = -1, blen;
640
641   if (finfo != NULL) {
642     bstart = finfo->start;
643     blen = finfo->length;
644   } else {
645     bstart = -1;
646     blen = -1;
647   }
648   if (bstart >= 0 && blen >= 0) {
649           bend = bstart + blen;
650   }
651
652   /* save the information needed to redraw the text */
653   /* should we save the fd & finfo pointers instead ?? */
654   gtk_object_set_data(GTK_OBJECT(bv),  E_BYTE_VIEW_START_KEY, GINT_TO_POINTER(bend));
655   gtk_object_set_data(GTK_OBJECT(bv),  E_BYTE_VIEW_END_KEY, GINT_TO_POINTER(bstart));
656   gtk_object_set_data(GTK_OBJECT(bv),  E_BYTE_VIEW_ENCODE_KEY, GINT_TO_POINTER(fd->flags.encoding));
657
658   packet_hex_print_common( bv, pd, len, bstart, bend, fd->flags.encoding);
659
660 }
661
662 void
663 packet_hex_reprint(GtkText *bv){
664
665   /* redraw the text using the saved information,       */
666   /* usually called if the preferences haved changed.   */
667
668   int start, end, len, encoding;
669   guint8 *data;
670
671   start = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(bv), E_BYTE_VIEW_START_KEY));
672   end = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(bv), E_BYTE_VIEW_END_KEY));
673   len = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(bv), E_BYTE_VIEW_DATA_LEN_KEY));
674   data =  gtk_object_get_data(GTK_OBJECT(bv), E_BYTE_VIEW_DATA_PTR_KEY);
675   encoding =  GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(bv), E_BYTE_VIEW_ENCODE_KEY));
676
677   packet_hex_print_common( bv, data, len, start, end, encoding);
678 }
679
680
681 /* List of all protocol tree widgets, so we can globally set the selection
682    mode, line style, expander style, and font of all of them. */
683 static GList *ptree_widgets;
684
685 /* Add a protocol tree widget to the list of protocol tree widgets. */
686 static void forget_ptree_widget(GtkWidget *ptreew, gpointer data);
687
688 static void
689 remember_ptree_widget(GtkWidget *ptreew)
690 {
691   ptree_widgets = g_list_append(ptree_widgets, ptreew);
692
693   /* Catch the "destroy" event on the widget, so that we remove it from
694      the list when it's destroyed. */
695   gtk_signal_connect(GTK_OBJECT(ptreew), "destroy",
696                      GTK_SIGNAL_FUNC(forget_ptree_widget), NULL);
697 }
698
699 /* Remove a protocol tree widget from the list of protocol tree widgets. */
700 static void
701 forget_ptree_widget(GtkWidget *ptreew, gpointer data)
702 {
703   ptree_widgets = g_list_remove(ptree_widgets, ptreew);
704 }
705
706 /* Set the selection mode of a given packet tree window. */
707 static void
708 set_ptree_sel_browse(GtkWidget *ptreew, gboolean val)
709 {
710         /* Yeah, GTK uses "browse" in the case where we do not, but oh well.
711            I think "browse" in Ethereal makes more sense than "SINGLE" in
712            GTK+ */
713         if (val) {
714                 gtk_clist_set_selection_mode(GTK_CLIST(ptreew),
715                     GTK_SELECTION_SINGLE);
716         }
717         else {
718                 gtk_clist_set_selection_mode(GTK_CLIST(ptreew),
719                     GTK_SELECTION_BROWSE);
720         }
721 }
722
723 static void
724 set_ptree_sel_browse_cb(gpointer data, gpointer user_data)
725 {
726         set_ptree_sel_browse((GtkWidget *)data, *(gboolean *)user_data);
727 }
728
729 /* Set the selection mode of all packet tree windows. */
730 void
731 set_ptree_sel_browse_all(gboolean val)
732 {
733         g_list_foreach(ptree_widgets, set_ptree_sel_browse_cb, &val);
734 }
735
736 /* Set the line style of a given packet tree window. */
737 static void
738 set_ptree_line_style(GtkWidget *ptreew, gint style)
739 {
740         /* I'm using an assert here since the preferences code limits
741          * the user input, both in the GUI and when reading the preferences file.
742          * If the value is incorrect, it's a program error, not a user-initiated error.
743          */
744         g_assert(style >= GTK_CTREE_LINES_NONE && style <= GTK_CTREE_LINES_TABBED);
745         gtk_ctree_set_line_style(GTK_CTREE(ptreew), style);
746 }
747
748 static void
749 set_ptree_line_style_cb(gpointer data, gpointer user_data)
750 {
751         set_ptree_line_style((GtkWidget *)data, *(gint *)user_data);
752 }
753
754 /* Set the line style of all packet tree window. */
755 void
756 set_ptree_line_style_all(gint style)
757 {
758         g_list_foreach(ptree_widgets, set_ptree_line_style_cb, &style);
759 }
760
761 /* Set the expander style of a given packet tree window. */
762 static void
763 set_ptree_expander_style(GtkWidget *ptreew, gint style)
764 {
765         /* I'm using an assert here since the preferences code limits
766          * the user input, both in the GUI and when reading the preferences file.
767          * If the value is incorrect, it's a program error, not a user-initiated error.
768          */
769         g_assert(style >= GTK_CTREE_EXPANDER_NONE && style <= GTK_CTREE_EXPANDER_CIRCULAR);
770         gtk_ctree_set_expander_style(GTK_CTREE(ptreew), style);
771 }
772
773 static void
774 set_ptree_expander_style_cb(gpointer data, gpointer user_data)
775 {
776         set_ptree_expander_style((GtkWidget *)data, *(gint *)user_data);
777 }
778         
779 void
780 set_ptree_expander_style_all(gint style)
781 {
782         g_list_foreach(ptree_widgets, set_ptree_expander_style_cb, &style);
783 }
784
785 static void
786 set_ptree_style_cb(gpointer data, gpointer user_data)
787 {
788         gtk_widget_set_style((GtkWidget *)data, (GtkStyle *)user_data);
789 }
790         
791 void
792 set_ptree_font_all(GdkFont *font)
793 {
794         GtkStyle *style;
795
796         style = gtk_style_new();
797         gdk_font_unref(style->font);
798         style->font = font;
799         gdk_font_ref(font);
800
801         g_list_foreach(ptree_widgets, set_ptree_style_cb, style);
802
803         /* Now nuke the old style and replace it with the new one. */
804         gtk_style_unref(item_style);
805         item_style = style;
806 }
807
808 void
809 create_tree_view(gint tv_size, e_prefs *prefs, GtkWidget *pane,
810                 GtkWidget **tv_scrollw_p, GtkWidget **tree_view_p, int pos)
811 {
812   GtkWidget *tv_scrollw, *tree_view;
813
814   /* Tree view */
815   tv_scrollw = gtk_scrolled_window_new(NULL, NULL);
816   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW(tv_scrollw),
817     GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
818   set_scrollbar_placement_scrollw(tv_scrollw, pos);
819   remember_scrolled_window(tv_scrollw);
820   gtk_paned_pack1(GTK_PANED(pane), tv_scrollw, TRUE, TRUE);
821   gtk_widget_set_usize(tv_scrollw, -1, tv_size);
822   gtk_widget_show(tv_scrollw);
823   
824   tree_view = gtk_ctree_new(1, 0);
825   /* I need this next line to make the widget work correctly with hidden
826    * column titles and GTK_SELECTION_BROWSE */
827   gtk_clist_set_column_auto_resize( GTK_CLIST(tree_view), 0, TRUE );
828   gtk_container_add( GTK_CONTAINER(tv_scrollw), tree_view );
829   set_ptree_sel_browse(tree_view, prefs->gui_ptree_sel_browse);
830   set_ptree_line_style(tree_view, prefs->gui_ptree_line_style);
831   set_ptree_expander_style(tree_view, prefs->gui_ptree_expander_style);
832   gtk_widget_set_style(tree_view, item_style);
833   remember_ptree_widget(tree_view);
834
835   *tree_view_p = tree_view;
836   *tv_scrollw_p = tv_scrollw;
837 }
838
839 void expand_all_tree(proto_tree *protocol_tree, GtkWidget *tree_view) {
840   int i;
841   for(i=0; i < num_tree_types; i++) {
842     tree_is_expanded[i] = TRUE;
843   }
844   gtk_clist_clear ( GTK_CLIST(tree_view) );
845   proto_tree_draw(protocol_tree, tree_view);
846   gtk_ctree_expand_recursive(GTK_CTREE(tree_view), NULL);
847 }
848
849 void collapse_all_tree(proto_tree *protocol_tree, GtkWidget *tree_view) {
850   int i;
851   for(i=0; i < num_tree_types; i++) {
852     tree_is_expanded[i] = FALSE;
853   }
854   gtk_clist_clear ( GTK_CLIST(tree_view) );
855   proto_tree_draw(protocol_tree, tree_view);
856 }
857
858
859 struct proto_tree_draw_info {
860         GtkCTree        *ctree;
861         GtkCTreeNode    *ctree_node;
862 };
863
864 void
865 proto_tree_draw(proto_tree *protocol_tree, GtkWidget *tree_view)
866 {
867         struct proto_tree_draw_info     info;
868
869         info.ctree = GTK_CTREE(tree_view);
870         info.ctree_node = NULL;
871
872         gtk_clist_freeze ( GTK_CLIST(tree_view) );
873
874         g_node_children_foreach((GNode*) protocol_tree, G_TRAVERSE_ALL,
875                 proto_tree_draw_node, &info);
876
877         gtk_signal_connect( GTK_OBJECT(info.ctree), "tree-expand",
878                 (GtkSignalFunc) expand_tree, NULL );
879         gtk_signal_connect( GTK_OBJECT(info.ctree), "tree-collapse",
880                 (GtkSignalFunc) collapse_tree, NULL );
881
882         gtk_clist_thaw ( GTK_CLIST(tree_view) );
883 }
884
885 static void
886 proto_tree_draw_node(GNode *node, gpointer data)
887 {
888         struct proto_tree_draw_info     info;
889         struct proto_tree_draw_info     *parent_info = (struct proto_tree_draw_info*) data;
890
891         field_info      *fi = (field_info*) (node->data);
892         gchar           label_str[ITEM_LABEL_LENGTH];
893         gchar           *label_ptr;
894         GtkCTreeNode    *parent;
895         gboolean        is_leaf, is_expanded;
896         int i;
897
898         if (!fi->visible)
899                 return;
900         i= find_notebook_page( byte_nb_ptr, fi->ds_name);
901         if ( i < 0) return;     /* no notebook pages ?? */
902         set_notebook_page( byte_nb_ptr, i);
903
904         /* was a free format label produced? */
905         if (fi->representation) {
906                 label_ptr = fi->representation;
907         }
908         else { /* no, make a generic label */
909                 label_ptr = label_str;
910                 proto_item_fill_label(fi, label_str);
911         }
912
913         if (g_node_n_children(node) > 0) {
914                 is_leaf = FALSE;
915                 if (tree_is_expanded[fi->tree_type]) {
916                         is_expanded = TRUE;
917                 }
918                 else {
919                         is_expanded = FALSE;
920                 }
921         }
922         else {
923                 is_leaf = TRUE;
924                 is_expanded = FALSE;
925         }
926         
927         info.ctree = parent_info->ctree;
928         parent = gtk_ctree_insert_node ( info.ctree, parent_info->ctree_node, NULL,
929                         &label_ptr, 5, NULL, NULL, NULL, NULL,
930                         is_leaf, is_expanded );
931
932         gtk_ctree_node_set_row_data( GTK_CTREE(info.ctree), parent, fi );
933
934         if (!is_leaf) {
935                 info.ctree_node = parent;
936                 g_node_children_foreach(node, G_TRAVERSE_ALL,
937                         proto_tree_draw_node, &info);
938         }
939 }