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