2 * packet list related functions 2002 Olivier Abad
4 * $Id: packet_list.c,v 1.13 2004/01/28 10:19:36 guy Exp $
6 * Ethereal - Network traffic analyzer
7 * By Gerald Combs <gerald@ethereal.com>
8 * Copyright 1998 Gerald Combs
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.
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.
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.
33 #include "gtkglobals.h"
34 #include "epan/epan.h"
36 #include "../ui_util.h"
39 #include "color_utils.h"
41 #include "epan/column_info.h"
42 #include "compat_macros.h"
45 #include "packet_list.h"
48 #include "image/clist_ascend.xpm"
49 #include "image/clist_descend.xpm"
53 * This lets us use GtkCList in GTK+ 1.3[.x] and later, and EthCList on
54 * GTK+ 1.2[.x], at least until we either use GTK+ 2.x's native widgets
55 * or make EthCList work on 1.3[.x] and 2.x.
57 #if GTK_MAJOR_VERSION >= 2 || GTK_MINOR_VERSION >= 3
58 #define EthCList GtkCList
59 #define EthCListRow GtkCListRow
60 #define eth_clist_append gtk_clist_append
61 #define eth_clist_clear gtk_clist_clear
62 #define eth_clist_column_titles_show gtk_clist_column_titles_show
63 #define eth_clist_find_row_from_data gtk_clist_find_row_from_data
64 #define eth_clist_freeze gtk_clist_freeze
65 #define eth_clist_get_row_data gtk_clist_get_row_data
66 #define eth_clist_get_selection_info gtk_clist_get_selection_info
67 #define eth_clist_moveto gtk_clist_moveto
68 #define eth_clist_new gtk_clist_new
69 #define eth_clist_row_is_visible gtk_clist_row_is_visible
70 #define eth_clist_select_row gtk_clist_select_row
71 #define eth_clist_set_background gtk_clist_set_background
72 #define eth_clist_set_column_auto_resize gtk_clist_set_column_auto_resize
73 #define eth_clist_set_column_justification gtk_clist_set_column_justification
74 #define eth_clist_set_column_resizeable gtk_clist_set_column_resizeable
75 #define eth_clist_set_column_width gtk_clist_set_column_width
76 #define eth_clist_set_column_widget gtk_clist_set_column_widget
77 #define eth_clist_set_compare_func gtk_clist_set_compare_func
78 #define eth_clist_set_foreground gtk_clist_set_foreground
79 #define eth_clist_set_row_data gtk_clist_set_row_data
80 #define eth_clist_set_selection_mode gtk_clist_set_selection_mode
81 #define eth_clist_set_sort_column gtk_clist_set_sort_column
82 #define eth_clist_set_text gtk_clist_set_text
83 #define eth_clist_sort gtk_clist_sort
84 #define eth_clist_thaw gtk_clist_thaw
85 #define ETH_CLIST GTK_CLIST
90 typedef struct column_arrows {
93 GtkWidget *descend_pm;
96 GtkWidget *packet_list;
98 /* GTKClist compare routine, overrides default to allow numeric comparison */
101 #define COMPARE_NUM(n1, n2) (((n1) < (n2)) ? -1 : \
102 ((n1) > (n2)) ? 1 : \
105 /* Compare time stamps.
106 A packet whose time is a reference time is considered to have
107 a lower time stamp than any frame with a non-reference time;
108 if both packets' times are reference times, we compare the
109 times of the packets. */
110 #define COMPARE_TS(ts1r, ts1s, ts1u, ts2r, ts2s, ts2u) \
111 (((ts1r) && !(ts2r)) ? -1 : \
112 (!(ts1r) && (ts2r)) ? 1 : \
113 ((ts1s) < (ts2s)) ? -1 : \
114 ((ts1s) > (ts2s)) ? 1 : \
115 ((ts1u) < (ts2u)) ? -1 :\
116 ((ts1u) > (ts2u)) ? 1 : \
119 packet_list_compare(EthCList *clist, gconstpointer ptr1, gconstpointer ptr2)
121 /* Get row data structures */
122 const EthCListRow *row1 = (const EthCListRow *)ptr1;
123 const EthCListRow *row2 = (const EthCListRow *)ptr2;
125 /* Get the frame data structures for the rows */
126 const frame_data *fdata1 = row1->data;
127 const frame_data *fdata2 = row2->data;
129 /* Get row text strings */
130 const char *text1 = GTK_CELL_TEXT (row1->cell[clist->sort_column])->text;
131 const char *text2 = GTK_CELL_TEXT (row2->cell[clist->sort_column])->text;
133 /* Attempt to convert to numbers */
137 gint col_fmt = cfile.cinfo.col_fmt[clist->sort_column];
142 return COMPARE_NUM(fdata1->num, fdata2->num);
145 switch (timestamp_type) {
148 case TS_ABSOLUTE_WITH_DATE:
149 return COMPARE_TS(fdata1->flags.ref_time, fdata1->abs_secs,
151 fdata2->flags.ref_time, fdata2->abs_secs,
155 return COMPARE_TS(fdata1->flags.ref_time, fdata1->rel_secs,
157 fdata2->flags.ref_time, fdata2->rel_secs,
161 return COMPARE_TS(fdata1->flags.ref_time, fdata1->del_secs,
163 fdata2->flags.ref_time, fdata2->del_secs,
169 return COMPARE_TS(fdata1->flags.ref_time, fdata1->abs_secs,
171 fdata2->flags.ref_time, fdata2->abs_secs,
175 return COMPARE_TS(fdata1->flags.ref_time, fdata1->rel_secs,
177 fdata2->flags.ref_time, fdata2->rel_secs,
181 return COMPARE_TS(fdata1->flags.ref_time, fdata1->del_secs,
183 fdata2->flags.ref_time, fdata2->del_secs,
187 case COL_PACKET_LENGTH:
188 return COMPARE_NUM(fdata1->pkt_len, fdata2->pkt_len);
190 case COL_CULMULATIVE_BYTES:
191 return COMPARE_NUM(fdata1->cul_bytes, fdata2->cul_bytes);
196 if ((col_fmt == COL_UNRES_SRC_PORT) || (col_fmt == COL_UNRES_DST_PORT) ||
197 ((num1 != 0) && (num2 != 0) && ((col_fmt == COL_DEF_SRC_PORT) || (col_fmt == COL_RES_SRC_PORT) ||
198 (col_fmt == COL_DEF_DST_PORT) || (col_fmt == COL_RES_DST_PORT)))) {
200 /* Compare numeric column */
204 else if (num1 > num2)
212 /* Compare text column */
214 return (text1 != NULL);
219 return strcmp(text1, text2);
224 /* What to do when a column is clicked */
226 packet_list_click_column_cb(EthCList *clist, gint column, gpointer data)
228 column_arrows *col_arrows = (column_arrows *) data;
231 eth_clist_freeze(clist);
233 for (i = 0; i < cfile.cinfo.num_cols; i++) {
234 gtk_widget_hide(col_arrows[i].ascend_pm);
235 gtk_widget_hide(col_arrows[i].descend_pm);
238 if (column == clist->sort_column) {
239 if (clist->sort_type == GTK_SORT_ASCENDING) {
240 clist->sort_type = GTK_SORT_DESCENDING;
241 gtk_widget_show(col_arrows[column].descend_pm);
243 clist->sort_type = GTK_SORT_ASCENDING;
244 gtk_widget_show(col_arrows[column].ascend_pm);
248 clist->sort_type = GTK_SORT_ASCENDING;
249 gtk_widget_show(col_arrows[column].ascend_pm);
250 eth_clist_set_sort_column(clist, column);
252 eth_clist_thaw(clist);
254 eth_clist_sort(clist);
257 /* What to do when a list item is selected/unselected */
259 packet_list_select_cb(GtkWidget *w _U_, gint row, gint col _U_, gpointer evt _U_) {
261 /* Remove the hex display tabbed pages */
262 while( (gtk_notebook_get_nth_page( GTK_NOTEBOOK(byte_nb_ptr), 0)))
263 gtk_notebook_remove_page( GTK_NOTEBOOK(byte_nb_ptr), 0);
265 select_packet(&cfile, row);
266 gtk_widget_grab_focus(packet_list);
270 packet_list_unselect_cb(GtkWidget *w _U_, gint row _U_, gint col _U_, gpointer evt _U_) {
272 unselect_packet(&cfile);
277 set_frame_mark(gboolean set, frame_data *frame, gint row) {
283 mark_frame(&cfile, frame);
284 color_t_to_gdkcolor(&fg, &prefs.gui_marked_fg);
285 color_t_to_gdkcolor(&bg, &prefs.gui_marked_bg);
286 eth_clist_set_foreground(ETH_CLIST(packet_list), row, &fg);
287 eth_clist_set_background(ETH_CLIST(packet_list), row, &bg);
289 color_filter_t *cfilter = frame->color_filter;
291 unmark_frame(&cfile, frame);
292 /* Restore the color from the matching color filter if any */
293 if (cfilter) { /* The packet matches a color filter */
294 color_t_to_gdkcolor(&fg, &cfilter->fg_color);
295 color_t_to_gdkcolor(&bg, &cfilter->bg_color);
296 eth_clist_set_foreground(ETH_CLIST(packet_list), row, &fg);
297 eth_clist_set_background(ETH_CLIST(packet_list), row, &bg);
298 } else { /* No color filter match */
299 eth_clist_set_foreground(ETH_CLIST(packet_list), row, NULL);
300 eth_clist_set_background(ETH_CLIST(packet_list), row, NULL);
303 file_set_save_marked_sensitive();
306 void mark_frame_cb(GtkWidget *w _U_, gpointer data _U_) {
307 if (cfile.current_frame) {
308 /* XXX hum, should better have a "cfile->current_row" here ... */
309 set_frame_mark(!cfile.current_frame->flags.marked,
311 eth_clist_find_row_from_data(ETH_CLIST(packet_list),
312 cfile.current_frame));
316 static void mark_all_frames(gboolean set) {
319 cfile.marked_count = 0;
320 for (fdata = cfile.plist; fdata != NULL; fdata = fdata->next) {
323 eth_clist_find_row_from_data(ETH_CLIST(packet_list), fdata));
327 void update_marked_frames(void) {
329 if (cfile.plist == NULL) return;
330 for (fdata = cfile.plist; fdata != NULL; fdata = fdata->next) {
331 if (fdata->flags.marked)
334 eth_clist_find_row_from_data(ETH_CLIST(packet_list),
339 void mark_all_frames_cb(GtkWidget *w _U_, gpointer data _U_) {
340 mark_all_frames(TRUE);
343 void unmark_all_frames_cb(GtkWidget *w _U_, gpointer data _U_) {
344 mark_all_frames(FALSE);
348 packet_list_get_event_row_column(GtkWidget *w, GdkEventButton *event_button,
349 gint *row, gint *column)
351 return eth_clist_get_selection_info(ETH_CLIST(w),
352 (gint) event_button->x, (gint) event_button->y,
356 #if GTK_MAJOR_VERSION < 2
358 packet_list_button_pressed_cb(GtkWidget *w, GdkEvent *event, gpointer data _U_)
360 GdkEventButton *event_button = (GdkEventButton *)event;
363 if (w == NULL || event == NULL)
366 if (event->type == GDK_BUTTON_PRESS && event_button->button == 2 &&
367 event_button->window == ETH_CLIST(w)->clist_window &&
368 packet_list_get_event_row_column(w, event_button, &row, &column)) {
369 frame_data *fdata = (frame_data *) eth_clist_get_row_data(ETH_CLIST(w),
371 set_frame_mark(!fdata->flags.marked, fdata, row);
376 packet_list_button_pressed_cb(GtkWidget *w, GdkEvent *event, gpointer data _U_)
378 GdkEventButton *event_button = (GdkEventButton *)event;
381 if (w == NULL || event == NULL)
384 if (event->type == GDK_BUTTON_PRESS && event_button->button == 2 &&
385 event_button->window == ETH_CLIST(w)->clist_window &&
386 eth_clist_get_selection_info(ETH_CLIST(w), (gint) event_button->x,
387 (gint) event_button->y, &row, &column)) {
388 frame_data *fdata = (frame_data *)eth_clist_get_row_data(ETH_CLIST(w),
390 set_frame_mark(!fdata->flags.marked, fdata, row);
397 /* Set the selection mode of the packet list window. */
399 set_plist_sel_browse(gboolean val)
401 GtkSelectionMode new_mode;
402 /* initialize with a mode we don't use, so that the mode == new_mode
403 * test will fail the first time */
404 static GtkSelectionMode mode = GTK_SELECTION_MULTIPLE;
406 /* Yeah, GTK uses "browse" in the case where we do not, but oh well. I
407 * think "browse" in Ethereal makes more sense than "SINGLE" in GTK+ */
408 new_mode = val ? GTK_SELECTION_SINGLE : GTK_SELECTION_BROWSE;
410 if (mode == new_mode) {
412 * The mode isn't changing, so don't do anything.
413 * In particular, don't gratuitiously unselect the
416 * XXX - why do we have to unselect the current packet
417 * ourselves? The documentation for the GtkCList at
419 * http://developer.gnome.org/doc/API/gtk/gtkclist.html
421 * says "Note that setting the widget's selection mode to
422 * one of GTK_SELECTION_BROWSE or GTK_SELECTION_SINGLE will
423 * cause all the items in the GtkCList to become deselected."
428 if (cfile.finfo_selected)
429 unselect_packet(&cfile);
432 eth_clist_set_selection_mode(ETH_CLIST(packet_list), mode);
435 /* Set the font of the packet list window. */
436 #if GTK_MAJOR_VERSION < 2
438 set_plist_font(GdkFont *font)
441 set_plist_font(PangoFontDescription *font)
446 #if GTK_MAJOR_VERSION < 2
449 style = gtk_style_new();
450 gdk_font_unref(style->font);
454 gtk_widget_set_style(packet_list, style);
458 gtk_widget_modify_font(packet_list, font);
461 /* Compute default column sizes. */
462 for (i = 0; i < cfile.cinfo.num_cols; i++) {
463 #if GTK_MAJOR_VERSION < 2
464 col_width = gdk_string_width(font,
465 get_column_longest_string(get_column_format(i)));
467 layout = gtk_widget_create_pango_layout(packet_list,
468 get_column_longest_string(get_column_format(i)));
469 pango_layout_get_pixel_size(layout, &col_width, NULL);
470 g_object_unref(G_OBJECT(layout));
472 eth_clist_set_column_width(ETH_CLIST(packet_list), i,
478 packet_list_new(e_prefs *prefs)
480 GtkWidget *pkt_scrollw;
484 pkt_scrollw = scrolled_window_new(NULL, NULL);
485 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW(pkt_scrollw),
486 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
488 packet_list = eth_clist_new(cfile.cinfo.num_cols);
489 /* Column titles are filled in below */
490 gtk_container_add(GTK_CONTAINER(pkt_scrollw), packet_list);
492 set_plist_sel_browse(prefs->gui_plist_sel_browse);
493 set_plist_font(m_r_font);
494 gtk_widget_set_name(packet_list, "packet list");
495 SIGNAL_CONNECT(packet_list, "select-row", packet_list_select_cb, NULL);
496 SIGNAL_CONNECT(packet_list, "unselect-row", packet_list_unselect_cb, NULL);
497 for (i = 0; i < cfile.cinfo.num_cols; i++) {
498 /* Columns do not automatically resize, but are resizeable by
500 eth_clist_set_column_auto_resize(ETH_CLIST(packet_list), i, FALSE);
501 eth_clist_set_column_resizeable(ETH_CLIST(packet_list), i, TRUE);
503 /* Right-justify the packet number column. */
504 if (cfile.cinfo.col_fmt[i] == COL_NUMBER)
505 eth_clist_set_column_justification(ETH_CLIST(packet_list), i,
508 SIGNAL_CONNECT(packet_list, "button_press_event", popup_menu_handler,
509 OBJECT_GET_DATA(popup_menu_object, PM_PACKET_LIST_KEY));
510 SIGNAL_CONNECT(packet_list, "button_press_event",
511 packet_list_button_pressed_cb, NULL);
512 eth_clist_set_compare_func(ETH_CLIST(packet_list), packet_list_compare);
513 gtk_widget_show(packet_list);
519 packet_list_set_column_titles(void)
522 GdkPixmap *ascend_pm, *descend_pm;
523 GdkBitmap *ascend_bm, *descend_bm;
524 column_arrows *col_arrows;
526 GtkWidget *column_lb;
528 win_style = gtk_widget_get_style(top_level);
529 ascend_pm = gdk_pixmap_create_from_xpm_d(top_level->window, &ascend_bm,
530 &win_style->bg[GTK_STATE_NORMAL],
531 (gchar **)clist_ascend_xpm);
532 descend_pm = gdk_pixmap_create_from_xpm_d(top_level->window, &descend_bm,
533 &win_style->bg[GTK_STATE_NORMAL],
534 (gchar **)clist_descend_xpm);
536 col_arrows = (column_arrows *) g_malloc(sizeof(column_arrows) *
537 cfile.cinfo.num_cols);
538 for (i = 0; i < cfile.cinfo.num_cols; i++) {
539 col_arrows[i].table = gtk_table_new(2, 2, FALSE);
540 gtk_table_set_col_spacings(GTK_TABLE(col_arrows[i].table), 5);
541 column_lb = gtk_label_new(cfile.cinfo.col_title[i]);
542 gtk_table_attach(GTK_TABLE(col_arrows[i].table), column_lb, 0, 1, 0, 2,
543 GTK_SHRINK, GTK_SHRINK, 0, 0);
544 gtk_widget_show(column_lb);
545 col_arrows[i].ascend_pm = gtk_pixmap_new(ascend_pm, ascend_bm);
546 gtk_table_attach(GTK_TABLE(col_arrows[i].table),
547 col_arrows[i].ascend_pm,
548 1, 2, 1, 2, GTK_SHRINK, GTK_SHRINK, 0, 0);
550 gtk_widget_show(col_arrows[i].ascend_pm);
552 col_arrows[i].descend_pm = gtk_pixmap_new(descend_pm, descend_bm);
553 gtk_table_attach(GTK_TABLE(col_arrows[i].table),
554 col_arrows[i].descend_pm,
555 1, 2, 0, 1, GTK_SHRINK, GTK_SHRINK, 0, 0);
556 eth_clist_set_column_widget(ETH_CLIST(packet_list), i,
557 col_arrows[i].table);
558 gtk_widget_show(col_arrows[i].table);
560 eth_clist_column_titles_show(ETH_CLIST(packet_list));
561 SIGNAL_CONNECT(packet_list, "click-column", packet_list_click_column_cb,
566 packet_list_clear(void)
568 eth_clist_clear(ETH_CLIST(packet_list));
572 packet_list_freeze(void)
574 eth_clist_freeze(ETH_CLIST(packet_list));
578 packet_list_thaw(void)
580 eth_clist_thaw(ETH_CLIST(packet_list));
584 packet_list_select_row(gint row)
586 SIGNAL_EMIT_BY_NAME1(packet_list, "select_row", row);
590 packet_list_moveto_end(void)
592 eth_clist_moveto(ETH_CLIST(packet_list),
593 ETH_CLIST(packet_list)->rows - 1, -1, 1.0, 1.0);
597 packet_list_append(gchar *text[], gpointer data)
601 row = eth_clist_append(ETH_CLIST(packet_list), text);
602 eth_clist_set_row_data(ETH_CLIST(packet_list), row, data);
607 packet_list_set_colors(gint row, color_t *fg, color_t *bg)
609 GdkColor gdkfg, gdkbg;
613 color_t_to_gdkcolor(&gdkfg, fg);
614 eth_clist_set_foreground(ETH_CLIST(packet_list), row, &gdkfg);
618 color_t_to_gdkcolor(&gdkbg, bg);
619 eth_clist_set_background(ETH_CLIST(packet_list), row, &gdkbg);
624 packet_list_find_row_from_data(gpointer data)
626 return eth_clist_find_row_from_data(ETH_CLIST(packet_list), data);
630 packet_list_set_text(gint row, gint column, const gchar *text)
632 eth_clist_set_text(ETH_CLIST(packet_list), row, column, text);
635 /* Set the column widths of those columns that show the time in
636 * "command-line-specified" format. */
638 packet_list_set_cls_time_width(gint column)
641 #if GTK_MAJOR_VERSION < 2
644 pl_style = gtk_widget_get_style(packet_list);
645 width = gdk_string_width(pl_style->font,
646 get_column_longest_string(COL_CLS_TIME));
650 layout = gtk_widget_create_pango_layout(packet_list,
651 get_column_longest_string(COL_CLS_TIME));
652 pango_layout_get_pixel_size(layout, &width, NULL);
653 g_object_unref(G_OBJECT(layout));
655 eth_clist_set_column_width(ETH_CLIST(packet_list), column, width);
659 packet_list_get_row_data(gint row)
661 return eth_clist_get_row_data(ETH_CLIST(packet_list), row);
664 /* Set the selected row and the focus row of the packet list to the specified
665 * row, and make it visible if it's not currently visible. */
667 packet_list_set_selected_row(gint row)
669 if (eth_clist_row_is_visible(ETH_CLIST(packet_list), row) !=
671 eth_clist_moveto(ETH_CLIST(packet_list), row, -1, 0.0, 0.0);
673 /* XXX - why is there no "eth_clist_set_focus_row()", so that we
674 * can make the row for the frame we found the focus row?
676 * See http://www.gnome.org/mailing-lists/archives/gtk-list/2000-January/0038.shtml
678 ETH_CLIST(packet_list)->focus_row = row;
680 eth_clist_select_row(ETH_CLIST(packet_list), row, -1);
683 /* Return the column number that the clist is currently sorted by */
685 packet_list_get_sort_column(void)
687 return ETH_CLIST(packet_list)->sort_column;