Fix more "no previous declaration" warnings
[obnox/wireshark/wip.git] / gtk / packet_list.c
1 /* packet_list.c
2  * packet list related functions   2002 Olivier Abad
3  *
4  * $Id$
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@ethereal.com>
8  * Copyright 1998 Gerald Combs
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <gtk/gtk.h>
30 #include <string.h>
31
32 #include "globals.h"
33 #include "gtkglobals.h"
34 #include "epan/epan.h"
35 #include "color.h"
36 #include "color_filters.h"
37 #include "../ui_util.h"
38 #include "ui_util.h"
39 #include "main.h"
40 #include "menu.h"
41 #include "colors.h"
42 #include <epan/column.h>
43 #include "epan/column_info.h"
44 #include "compat_macros.h"
45 #include <epan/prefs.h>
46 #include "file_dlg.h"
47 #include "packet_list.h"
48 #include "keys.h"
49 #include "font_utils.h"
50 #include "packet_history.h"
51
52 #include <epan/timestamp.h>
53
54 #include "image/clist_ascend.xpm"
55 #include "image/clist_descend.xpm"
56
57 #include "progress_dlg.h"
58
59 #define N_PROGBAR_UPDATES 100
60
61
62 /*
63  * XXX - gross hack.
64  * This lets us use GtkCList in GTK+ 1.3[.x] and later, and EthCList on
65  * GTK+ 1.2[.x], at least until we either use GTK+ 2.x's native widgets
66  * or make EthCList work on 1.3[.x] and 2.x.
67  */
68 #if GTK_MAJOR_VERSION >= 2 || GTK_MINOR_VERSION >= 3
69 #define EthCList                                GtkCList
70 #define EthCListRow                             GtkCListRow
71 #define eth_clist_append                        gtk_clist_append
72 #define eth_clist_clear                         gtk_clist_clear
73 #define eth_clist_column_titles_show            gtk_clist_column_titles_show
74 #define eth_clist_find_row_from_data            gtk_clist_find_row_from_data
75 #define eth_clist_freeze                        gtk_clist_freeze
76 #define eth_clist_get_row_data                  gtk_clist_get_row_data
77 #define eth_clist_get_selection_info            gtk_clist_get_selection_info
78 #define eth_clist_moveto                        gtk_clist_moveto
79 #define eth_clist_new                           gtk_clist_new
80 #define eth_clist_row_is_visible                gtk_clist_row_is_visible
81 #define eth_clist_select_row                    gtk_clist_select_row
82 #define eth_clist_set_background                gtk_clist_set_background
83 #define eth_clist_set_column_auto_resize        gtk_clist_set_column_auto_resize
84 #define eth_clist_set_column_justification      gtk_clist_set_column_justification
85 #define eth_clist_set_column_resizeable         gtk_clist_set_column_resizeable
86 #define eth_clist_set_column_width              gtk_clist_set_column_width
87 #define eth_clist_set_column_widget             gtk_clist_set_column_widget
88 #define eth_clist_set_compare_func              gtk_clist_set_compare_func
89 #define eth_clist_set_foreground                gtk_clist_set_foreground
90 #define eth_clist_set_row_data                  gtk_clist_set_row_data
91 #define eth_clist_set_selection_mode            gtk_clist_set_selection_mode
92 #define eth_clist_set_sort_column               gtk_clist_set_sort_column
93 #define eth_clist_set_text                      gtk_clist_set_text
94 #define eth_clist_sort                          gtk_clist_sort
95 #define eth_clist_thaw                          gtk_clist_thaw
96 #define ETH_CLIST                               GTK_CLIST
97 #else
98 #include "ethclist.h"
99 #endif
100
101 typedef struct column_arrows {
102   GtkWidget *table;
103   GtkWidget *ascend_pm;
104   GtkWidget *descend_pm;
105 } column_arrows;
106
107 GtkWidget *packet_list;
108
109 /* EthClist compare routine, overrides default to allow numeric comparison */
110
111 #define COMPARE_FRAME_NUM()     ((fdata1->num < fdata2->num) ? -1 : \
112                                  (fdata1->num > fdata2->num) ? 1 : \
113                                  0)
114
115 #define COMPARE_NUM(f)  ((fdata1->f < fdata2->f) ? -1 : \
116                          (fdata1->f > fdata2->f) ? 1 : \
117                          COMPARE_FRAME_NUM())
118
119 /* Compare time stamps.
120    A packet whose time is a reference time is considered to have
121    a lower time stamp than any frame with a non-reference time;
122    if both packets' times are reference times, we compare the
123    times of the packets. */
124 #define COMPARE_TS(secs, usecs) \
125                 ((fdata1->flags.ref_time && !fdata2->flags.ref_time) ? -1 : \
126                  (!fdata1->flags.ref_time && fdata2->flags.ref_time) ? 1 : \
127                  (fdata1->secs < fdata2->secs) ? -1 : \
128                  (fdata1->secs > fdata2->secs) ? 1 : \
129                  (fdata1->usecs < fdata2->usecs) ? -1 :\
130                  (fdata1->usecs > fdata2->usecs) ? 1 : \
131                  COMPARE_FRAME_NUM())
132 static gint
133 packet_list_compare(EthCList *clist, gconstpointer  ptr1, gconstpointer  ptr2)
134 {
135   /* Get row data structures */
136   const EthCListRow *row1 = (const EthCListRow *)ptr1;
137   const EthCListRow *row2 = (const EthCListRow *)ptr2;
138
139   /* Get the frame data structures for the rows */
140   const frame_data *fdata1 = row1->data;
141   const frame_data *fdata2 = row2->data;
142
143   /* Get row text strings */
144   const char *text1 = GTK_CELL_TEXT (row1->cell[clist->sort_column])->text;
145   const char *text2 = GTK_CELL_TEXT (row2->cell[clist->sort_column])->text;
146
147   /* Attempt to convert to numbers */
148   double  num1;
149   double  num2;
150
151   int ret;
152
153   gint  col_fmt = cfile.cinfo.col_fmt[clist->sort_column];
154
155   switch (col_fmt) {
156
157   case COL_NUMBER:
158     return COMPARE_FRAME_NUM();
159
160   case COL_CLS_TIME:
161     switch (get_timestamp_setting()) {
162
163     case TS_ABSOLUTE:
164     case TS_ABSOLUTE_WITH_DATE:
165       return COMPARE_TS(abs_secs, abs_usecs);
166
167     case TS_RELATIVE:
168       return COMPARE_TS(rel_secs, rel_usecs);
169
170     case TS_DELTA:
171       return COMPARE_TS(del_secs, del_usecs);
172     }
173     return 0;
174
175   case COL_ABS_TIME:
176   case COL_ABS_DATE_TIME:
177     return COMPARE_TS(abs_secs, abs_usecs);
178
179   case COL_REL_TIME:
180     return COMPARE_TS(rel_secs, rel_usecs);
181
182   case COL_DELTA_TIME:
183     return COMPARE_TS(del_secs, del_usecs);
184
185   case COL_PACKET_LENGTH:
186     return COMPARE_NUM(pkt_len);
187
188   case COL_CUMULATIVE_BYTES:
189     return COMPARE_NUM(cum_bytes);
190
191   default:
192     num1 = atof(text1);
193     num2 = atof(text2);
194     if ((col_fmt == COL_UNRES_SRC_PORT) || (col_fmt == COL_UNRES_DST_PORT) ||
195         ((num1 != 0) && (num2 != 0) && ((col_fmt == COL_DEF_SRC_PORT) || (col_fmt == COL_RES_SRC_PORT) ||
196                                       (col_fmt == COL_DEF_DST_PORT) || (col_fmt == COL_RES_DST_PORT)))) {
197
198       /* Compare numeric column */
199
200       if (num1 < num2)
201         return -1;
202       else if (num1 > num2)
203         return 1;
204       else
205         return COMPARE_FRAME_NUM();
206     }
207
208     else {
209
210       /* Compare text column */
211       if (!text2) {
212         if (text1)
213           return 1;
214         else
215           return COMPARE_FRAME_NUM();
216       }
217
218       if (!text1)
219         return -1;
220
221       ret = strcmp(text1, text2);
222       if (ret == 0)
223         return COMPARE_FRAME_NUM();
224       else
225         return ret;
226     }
227   }
228 }
229
230 /* What to do when a column is clicked */
231 static void
232 packet_list_click_column_cb(EthCList *clist, gint column, gpointer data)
233 {
234   column_arrows *col_arrows = (column_arrows *) data;
235   int i;
236
237   eth_clist_freeze(clist);
238
239   for (i = 0; i < cfile.cinfo.num_cols; i++) {
240     gtk_widget_hide(col_arrows[i].ascend_pm);
241     gtk_widget_hide(col_arrows[i].descend_pm);
242   }
243
244   if (column == clist->sort_column) {
245     if (clist->sort_type == GTK_SORT_ASCENDING) {
246       clist->sort_type = GTK_SORT_DESCENDING;
247       gtk_widget_show(col_arrows[column].descend_pm);
248     } else {
249       clist->sort_type = GTK_SORT_ASCENDING;
250       gtk_widget_show(col_arrows[column].ascend_pm);
251     }
252   }
253   else {
254     clist->sort_type = GTK_SORT_ASCENDING;
255     gtk_widget_show(col_arrows[column].ascend_pm);
256     eth_clist_set_sort_column(clist, column);
257   }
258   eth_clist_thaw(clist);
259
260   eth_clist_sort(clist);
261 }
262
263 /* What to do when a list item is selected/unselected */
264 static void
265 packet_list_select_cb(GtkWidget *w _U_, gint row, gint col _U_, gpointer evt _U_) {
266
267 /* Remove the hex display tabbed pages */
268   while( (gtk_notebook_get_nth_page( GTK_NOTEBOOK(byte_nb_ptr), 0)))
269     gtk_notebook_remove_page( GTK_NOTEBOOK(byte_nb_ptr), 0);
270
271   cf_select_packet(&cfile, row);
272   gtk_widget_grab_focus(packet_list);
273   packet_history_add(row);
274 }
275
276 static void
277 packet_list_unselect_cb(GtkWidget *w _U_, gint row _U_, gint col _U_, gpointer evt _U_) {
278
279   cf_unselect_packet(&cfile);
280 }
281
282 /* mark packets */
283 static void
284 set_frame_mark(gboolean set, frame_data *frame, gint row) {
285   GdkColor fg, bg;
286
287   if (row == -1)
288     return;
289   if (set) {
290     cf_mark_frame(&cfile, frame);
291     color_t_to_gdkcolor(&fg, &prefs.gui_marked_fg);
292     color_t_to_gdkcolor(&bg, &prefs.gui_marked_bg);
293     eth_clist_set_foreground(ETH_CLIST(packet_list), row, &fg);
294     eth_clist_set_background(ETH_CLIST(packet_list), row, &bg);
295   } else {
296     color_filter_t *cfilter = frame->color_filter;
297
298     cf_unmark_frame(&cfile, frame);
299     /* Restore the color from the matching color filter if any */
300     if (cfilter) { /* The packet matches a color filter */
301       color_t_to_gdkcolor(&fg, &cfilter->fg_color);
302       color_t_to_gdkcolor(&bg, &cfilter->bg_color);
303       eth_clist_set_foreground(ETH_CLIST(packet_list), row, &fg);
304       eth_clist_set_background(ETH_CLIST(packet_list), row, &bg);
305     } else { /* No color filter match */
306       eth_clist_set_foreground(ETH_CLIST(packet_list), row, NULL);
307       eth_clist_set_background(ETH_CLIST(packet_list), row, NULL);
308     }
309   }
310 }
311
312 /* call this after last set_frame_mark is done */
313 static void mark_frames_ready(void) {
314   file_set_save_marked_sensitive();
315   packets_bar_update();
316 }
317
318 void mark_frame_cb(GtkWidget *w _U_, gpointer data _U_) {
319   if (cfile.current_frame) {
320     /* XXX hum, should better have a "cfile->current_row" here ... */
321     set_frame_mark(!cfile.current_frame->flags.marked,
322                    cfile.current_frame,
323                    eth_clist_find_row_from_data(ETH_CLIST(packet_list),
324                                                 cfile.current_frame));
325     mark_frames_ready();
326   }
327 }
328
329 static void mark_all_frames(gboolean set) {
330   frame_data *fdata;
331   
332   /* XXX: we might need a progressbar here */
333   for (fdata = cfile.plist; fdata != NULL; fdata = fdata->next) {
334     set_frame_mark(set,
335                    fdata,
336                    eth_clist_find_row_from_data(ETH_CLIST(packet_list), fdata));
337   }
338   mark_frames_ready();
339 }
340
341 void update_marked_frames(void) {
342   frame_data *fdata;
343
344   if (cfile.plist == NULL) return;
345
346   /* XXX: we might need a progressbar here */
347   for (fdata = cfile.plist; fdata != NULL; fdata = fdata->next) {
348     if (fdata->flags.marked)
349       set_frame_mark(TRUE,
350                      fdata,
351                      eth_clist_find_row_from_data(ETH_CLIST(packet_list),
352                                                   fdata));
353   }
354   mark_frames_ready();
355 }
356
357 void mark_all_frames_cb(GtkWidget *w _U_, gpointer data _U_) {
358   mark_all_frames(TRUE);
359 }
360
361 void unmark_all_frames_cb(GtkWidget *w _U_, gpointer data _U_) {
362   mark_all_frames(FALSE);
363 }
364
365 gboolean
366 packet_list_get_event_row_column(GtkWidget *w, GdkEventButton *event_button,
367                                  gint *row, gint *column)
368 {
369     return eth_clist_get_selection_info(ETH_CLIST(w), 
370                                  (gint) event_button->x, (gint) event_button->y, 
371                                   row, column);
372 }
373
374 #if GTK_MAJOR_VERSION < 2
375 static void
376 packet_list_button_pressed_cb(GtkWidget *w, GdkEvent *event, gpointer data _U_)
377 {
378     GdkEventButton *event_button = (GdkEventButton *)event;
379     gint row, column;
380
381     if (w == NULL || event == NULL)
382         return;
383
384     if (event->type == GDK_BUTTON_PRESS && event_button->button == 2 &&
385         event_button->window == ETH_CLIST(w)->clist_window &&
386         packet_list_get_event_row_column(w, event_button, &row, &column)) {
387         frame_data *fdata = (frame_data *) eth_clist_get_row_data(ETH_CLIST(w),
388                                                                   row);
389         set_frame_mark(!fdata->flags.marked, fdata, row);
390         mark_frames_ready();
391     }
392 }
393 #else
394 static gint
395 packet_list_button_pressed_cb(GtkWidget *w, GdkEvent *event, gpointer data _U_)
396 {
397     GdkEventButton *event_button = (GdkEventButton *)event;
398     gint row, column;
399
400     if (w == NULL || event == NULL)
401         return FALSE;
402
403     if (event->type == GDK_BUTTON_PRESS && event_button->button == 2 &&
404         event_button->window == ETH_CLIST(w)->clist_window &&
405         eth_clist_get_selection_info(ETH_CLIST(w), (gint) event_button->x,
406                                      (gint) event_button->y, &row, &column)) {
407         frame_data *fdata = (frame_data *)eth_clist_get_row_data(ETH_CLIST(w),
408                                                                  row);
409         set_frame_mark(!fdata->flags.marked, fdata, row);
410         mark_frames_ready();
411         return TRUE;
412     }
413     return FALSE;
414 }
415 #endif
416
417 /* Set the selection mode of the packet list window. */
418 void
419 set_plist_sel_browse(gboolean val)
420 {
421         GtkSelectionMode new_mode;
422         /* initialize with a mode we don't use, so that the mode == new_mode
423          * test will fail the first time */
424         static GtkSelectionMode mode = GTK_SELECTION_MULTIPLE;
425
426         /* Yeah, GTK uses "browse" in the case where we do not, but oh well. I
427          * think "browse" in Ethereal makes more sense than "SINGLE" in GTK+ */
428         new_mode = val ? GTK_SELECTION_SINGLE : GTK_SELECTION_BROWSE;
429
430         if (mode == new_mode) {
431                 /*
432                  * The mode isn't changing, so don't do anything.
433                  * In particular, don't gratuitiously unselect the
434                  * current packet.
435                  *
436                  * XXX - why do we have to unselect the current packet
437                  * ourselves?  The documentation for the GtkCList at
438                  *
439                  *      http://developer.gnome.org/doc/API/gtk/gtkclist.html
440                  *
441                  * says "Note that setting the widget's selection mode to
442                  * one of GTK_SELECTION_BROWSE or GTK_SELECTION_SINGLE will
443                  * cause all the items in the GtkCList to become deselected."
444                  */
445                 return;
446         }
447
448         if (cfile.finfo_selected)
449                 cf_unselect_packet(&cfile);
450
451         mode = new_mode;
452         eth_clist_set_selection_mode(ETH_CLIST(packet_list), mode);
453 }
454
455 /* Set the font of the packet list window. */
456 void
457 set_plist_font(FONT_TYPE *font)
458 {
459         int i;
460         gint col_width;
461 #if GTK_MAJOR_VERSION < 2
462         GtkStyle *style;
463
464         style = gtk_style_new();
465         gdk_font_unref(style->font);
466         style->font = font;
467         gdk_font_ref(font);
468
469         gtk_widget_set_style(packet_list, style);
470 #else
471         PangoLayout *layout;
472
473         gtk_widget_modify_font(packet_list, font);
474 #endif
475
476         /* Compute default column sizes. */
477         for (i = 0; i < cfile.cinfo.num_cols; i++) {
478 #if GTK_MAJOR_VERSION < 2
479                 col_width = gdk_string_width(font,
480                         get_column_longest_string(get_column_format(i)));
481 #else
482                 layout = gtk_widget_create_pango_layout(packet_list,
483                     get_column_longest_string(get_column_format(i)));
484                 pango_layout_get_pixel_size(layout, &col_width, NULL);
485                 g_object_unref(G_OBJECT(layout));
486 #endif
487                 eth_clist_set_column_width(ETH_CLIST(packet_list), i,
488                         col_width);
489         }
490 }
491
492 GtkWidget *
493 packet_list_new(e_prefs *prefs)
494 {
495     GtkWidget *pkt_scrollw;
496     int            i;
497
498     /* Packet list */
499     pkt_scrollw = scrolled_window_new(NULL, NULL);
500     /* The usual policy for scrolled windows is to set both scrollbars to automatic,
501      * meaning they'll only appear if the content doesn't fit into the window.
502      *
503      * As this doesn't seem to work in some cases for the vertical scrollbar
504      * (see http://bugs.ethereal.com/bugzilla/show_bug.cgi?id=220),
505      * we show that scrollbar always. */
506     gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(pkt_scrollw),
507                                    GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
508 #if GTK_MAJOR_VERSION >= 2
509     /* the eth_clist will have it's own GTK_SHADOW_IN, so don't use a shadow 
510      * for both widgets */
511     gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(pkt_scrollw), 
512                                     GTK_SHADOW_NONE);
513 #endif
514
515     packet_list = eth_clist_new(cfile.cinfo.num_cols);
516     /* Column titles are filled in below */
517     gtk_container_add(GTK_CONTAINER(pkt_scrollw), packet_list);
518
519     set_plist_sel_browse(prefs->gui_plist_sel_browse);
520     set_plist_font(user_font_get_regular());
521     gtk_widget_set_name(packet_list, "packet list");
522     SIGNAL_CONNECT(packet_list, "select-row", packet_list_select_cb, NULL);
523     SIGNAL_CONNECT(packet_list, "unselect-row", packet_list_unselect_cb, NULL);
524     for (i = 0; i < cfile.cinfo.num_cols; i++) {
525         /* For performance reasons, columns do not automatically resize, 
526            but are resizeable by the user. */
527         eth_clist_set_column_auto_resize(ETH_CLIST(packet_list), i, FALSE);
528         eth_clist_set_column_resizeable(ETH_CLIST(packet_list), i, TRUE);
529
530         /* Right-justify some special columns. */
531         if (cfile.cinfo.col_fmt[i] == COL_NUMBER ||
532             cfile.cinfo.col_fmt[i] == COL_PACKET_LENGTH ||
533             cfile.cinfo.col_fmt[i] == COL_CUMULATIVE_BYTES ||
534             cfile.cinfo.col_fmt[i] == COL_DCE_CALL)
535             eth_clist_set_column_justification(ETH_CLIST(packet_list), i,
536                                                GTK_JUSTIFY_RIGHT);
537     }
538     SIGNAL_CONNECT(packet_list, "button_press_event", popup_menu_handler,
539                    OBJECT_GET_DATA(popup_menu_object, PM_PACKET_LIST_KEY));
540     SIGNAL_CONNECT(packet_list, "button_press_event",
541                    packet_list_button_pressed_cb, NULL);
542     eth_clist_set_compare_func(ETH_CLIST(packet_list), packet_list_compare);
543     gtk_widget_show(packet_list);
544
545     return pkt_scrollw;
546 }
547
548 void
549 packet_list_set_column_titles(void)
550 {
551     GtkStyle      *win_style;
552     GdkPixmap     *ascend_pm, *descend_pm;
553     GdkBitmap     *ascend_bm, *descend_bm;
554     column_arrows *col_arrows;
555     int            i;
556     GtkWidget     *column_lb;
557
558     win_style = gtk_widget_get_style(top_level);
559     ascend_pm = gdk_pixmap_create_from_xpm_d(top_level->window, &ascend_bm,
560                                              &win_style->bg[GTK_STATE_NORMAL],
561                                              (gchar **)clist_ascend_xpm);
562     descend_pm = gdk_pixmap_create_from_xpm_d(top_level->window, &descend_bm,
563                                               &win_style->bg[GTK_STATE_NORMAL],
564                                               (gchar **)clist_descend_xpm);
565
566     col_arrows = (column_arrows *) g_malloc(sizeof(column_arrows) *
567                                             cfile.cinfo.num_cols);
568     for (i = 0; i < cfile.cinfo.num_cols; i++) {
569         col_arrows[i].table = gtk_table_new(2, 2, FALSE);
570         gtk_table_set_col_spacings(GTK_TABLE(col_arrows[i].table), 5);
571         column_lb = gtk_label_new(cfile.cinfo.col_title[i]);
572         gtk_table_attach(GTK_TABLE(col_arrows[i].table), column_lb, 0, 1, 0, 2,
573                          GTK_SHRINK, GTK_SHRINK, 0, 0);
574         gtk_widget_show(column_lb);
575         col_arrows[i].ascend_pm = gtk_pixmap_new(ascend_pm, ascend_bm);
576         gtk_table_attach(GTK_TABLE(col_arrows[i].table),
577                          col_arrows[i].ascend_pm,
578                          1, 2, 1, 2, GTK_SHRINK, GTK_SHRINK, 0, 0);
579         if (i == 0) {
580             gtk_widget_show(col_arrows[i].ascend_pm);
581         }
582         col_arrows[i].descend_pm = gtk_pixmap_new(descend_pm, descend_bm);
583         gtk_table_attach(GTK_TABLE(col_arrows[i].table),
584                          col_arrows[i].descend_pm,
585                          1, 2, 0, 1, GTK_SHRINK, GTK_SHRINK, 0, 0);
586         eth_clist_set_column_widget(ETH_CLIST(packet_list), i,
587                                     col_arrows[i].table);
588         gtk_widget_show(col_arrows[i].table);
589     }
590     eth_clist_column_titles_show(ETH_CLIST(packet_list));
591     SIGNAL_CONNECT(packet_list, "click-column", packet_list_click_column_cb,
592                    col_arrows);
593 }
594
595 void
596 packet_list_clear(void)
597 {
598     packet_history_clear();
599
600     eth_clist_clear(ETH_CLIST(packet_list));
601 }
602
603 void
604 packet_list_freeze(void)
605 {
606     eth_clist_freeze(ETH_CLIST(packet_list));
607 }
608
609 static void
610 packet_list_resize_columns(void) {
611     int         i;
612     int         progbar_nextstep;
613     int         progbar_quantum;
614     gboolean    stop_flag;
615     GTimeVal    start_time;
616     float       prog_val;
617     progdlg_t  *progbar = NULL;
618     gchar       status_str[100];
619
620
621     progbar_nextstep = 0;
622     /* When we reach the value that triggers a progress bar update,
623        bump that value by this amount. */
624     progbar_quantum = cfile.cinfo.num_cols/N_PROGBAR_UPDATES;
625
626     stop_flag = FALSE;
627     g_get_current_time(&start_time);
628
629
630     main_window_update();
631
632     for (i = 0; i < cfile.cinfo.num_cols; i++) {
633       if (i >= progbar_nextstep) {
634         /* let's not divide by zero. I should never be started
635          * with count == 0, so let's assert that
636          */
637         g_assert(cfile.cinfo.num_cols > 0);
638
639         prog_val = (gfloat) i / cfile.cinfo.num_cols;
640
641         /* Create the progress bar if necessary */
642         if (progbar == NULL)
643            progbar = delayed_create_progress_dlg("Resizing", "Resize Columns", 
644              &stop_flag, &start_time, prog_val);
645
646         if (progbar != NULL) {
647           g_snprintf(status_str, sizeof(status_str),
648                      "%u of %u columns (%s)", i+1, cfile.cinfo.num_cols, cfile.cinfo.col_title[i]);
649           update_progress_dlg(progbar, prog_val, status_str);
650         }
651
652         progbar_nextstep += progbar_quantum;
653       }
654
655       if (stop_flag) {
656         /* Well, the user decided to abort the resizing... */
657         break;
658       }
659
660         /* auto resize the current column */
661         eth_clist_set_column_auto_resize(ETH_CLIST(packet_list), i, TRUE);
662
663         /* the current column should be resizeable by the user again */
664         /* (will turn off auto resize again) */
665         eth_clist_set_column_resizeable(ETH_CLIST(packet_list), i, TRUE);
666     }
667
668     /* We're done resizing the columns; destroy the progress bar if it
669        was created. */
670     if (progbar != NULL)
671       destroy_progress_dlg(progbar);
672 }
673
674 void packet_list_resize_columns_cb(GtkWidget *widget _U_, gpointer data _U_)
675 {
676     packet_list_resize_columns();
677 }
678
679 void
680 packet_list_thaw(void)
681 {
682     eth_clist_thaw(ETH_CLIST(packet_list));
683     packets_bar_update();
684     /*packet_list_resize_columns();*/
685 }
686
687 void
688 packet_list_select_row(gint row)
689 {
690     SIGNAL_EMIT_BY_NAME(packet_list, "select_row", row);
691 }
692
693 void
694 packet_list_moveto_end(void)
695 {
696     eth_clist_moveto(ETH_CLIST(packet_list),
697                      ETH_CLIST(packet_list)->rows - 1, -1, 1.0, 1.0);
698 }
699
700 gint
701 packet_list_append(const gchar *text[], gpointer data)
702 {
703     gint row;
704
705     row = eth_clist_append(ETH_CLIST(packet_list), (gchar **)text);
706     eth_clist_set_row_data(ETH_CLIST(packet_list), row, data);
707     return row;
708 }
709
710 void
711 packet_list_set_colors(gint row, color_t *fg, color_t *bg)
712 {
713     GdkColor gdkfg, gdkbg;
714
715     if (fg)
716     {
717         color_t_to_gdkcolor(&gdkfg, fg);
718         eth_clist_set_foreground(ETH_CLIST(packet_list), row, &gdkfg);
719     }
720     if (bg)
721     {
722         color_t_to_gdkcolor(&gdkbg, bg);
723         eth_clist_set_background(ETH_CLIST(packet_list), row, &gdkbg);
724     }
725 }
726
727 gint
728 packet_list_find_row_from_data(gpointer data)
729 {
730     return eth_clist_find_row_from_data(ETH_CLIST(packet_list), data);
731 }
732
733 void
734 packet_list_set_text(gint row, gint column, const gchar *text)
735 {
736     eth_clist_set_text(ETH_CLIST(packet_list), row, column, text);
737 }
738
739 /* Set the column widths of those columns that show the time in
740  * "command-line-specified" format. */
741 void
742 packet_list_set_cls_time_width(gint column)
743 {
744     gint      width;
745 #if GTK_MAJOR_VERSION < 2
746     GtkStyle *pl_style;
747
748     pl_style = gtk_widget_get_style(packet_list);
749     width = gdk_string_width(pl_style->font,
750                              get_column_longest_string(COL_CLS_TIME));
751 #else
752     PangoLayout  *layout;
753
754     layout = gtk_widget_create_pango_layout(packet_list,
755                  get_column_longest_string(COL_CLS_TIME));
756     pango_layout_get_pixel_size(layout, &width, NULL);
757     g_object_unref(G_OBJECT(layout));
758 #endif
759     eth_clist_set_column_width(ETH_CLIST(packet_list), column, width);
760 }
761
762 gpointer
763 packet_list_get_row_data(gint row)
764 {
765     return eth_clist_get_row_data(ETH_CLIST(packet_list), row);
766 }
767
768 /* Set the selected row and the focus row of the packet list to the specified
769  * row, and make it visible if it's not currently visible. */
770 void
771 packet_list_set_selected_row(gint row)
772 {
773     if (eth_clist_row_is_visible(ETH_CLIST(packet_list), row) !=
774         GTK_VISIBILITY_FULL)
775         eth_clist_moveto(ETH_CLIST(packet_list), row, -1, 0.0, 0.0);
776
777     /* XXX - why is there no "eth_clist_set_focus_row()", so that we
778      * can make the row for the frame we found the focus row?
779      *
780      * See http://www.gnome.org/mailing-lists/archives/gtk-list/2000-January/0038.shtml
781      */
782     ETH_CLIST(packet_list)->focus_row = row;
783
784     eth_clist_select_row(ETH_CLIST(packet_list), row, -1);
785 }
786
787 /* Return the column number that the clist is currently sorted by */
788 gint
789 packet_list_get_sort_column(void)
790 {
791     return ETH_CLIST(packet_list)->sort_column;
792 }