remove some warnings
[obnox/wireshark/wip.git] / gtk / packet_list.c
index 0606738d64c2684f9e26a9f1a3bc375c8fd1f7d6..34c97257781b6788191c66712ece599b72d84e13 100644 (file)
@@ -1,10 +1,10 @@
 /* packet_list.c
  * packet list related functions   2002 Olivier Abad
  *
- * $Id: packet_list.c,v 1.16 2004/01/31 04:26:23 guy Exp $
+ * $Id$
  *
- * Ethereal - Network traffic analyzer
- * By Gerald Combs <gerald@ethereal.com>
+ * Wireshark - Network traffic analyzer
+ * By Gerald Combs <gerald@wireshark.org>
  * Copyright 1998 Gerald Combs
  *
  * This program is free software; you can redistribute it and/or
 #include "gtkglobals.h"
 #include "epan/epan.h"
 #include "color.h"
+#include "color_filters.h"
 #include "../ui_util.h"
-#include "ui_util.h"
+#include "gui_utils.h"
+#include "main.h"
 #include "menu.h"
-#include "color_utils.h"
-#include "column.h"
+#include "colors.h"
+#include <epan/column.h>
 #include "epan/column_info.h"
 #include "compat_macros.h"
-#include "../prefs.h"
-#include "file_dlg.h"
+#include <epan/prefs.h>
+#include "capture_file_dlg.h"
 #include "packet_list.h"
 #include "keys.h"
+#include "font_utils.h"
+#include "packet_history.h"
+
+#include <epan/timestamp.h>
 
 #include "image/clist_ascend.xpm"
 #include "image/clist_descend.xpm"
 
+#include "progress_dlg.h"
+
+#define N_PROGBAR_UPDATES 100
+
+
 /*
  * XXX - gross hack.
  * This lets us use GtkCList in GTK+ 1.3[.x] and later, and EthCList on
@@ -110,13 +121,13 @@ GtkWidget *packet_list;
    a lower time stamp than any frame with a non-reference time;
    if both packets' times are reference times, we compare the
    times of the packets. */
-#define COMPARE_TS(secs, usecs) \
+#define COMPARE_TS(ts) \
                ((fdata1->flags.ref_time && !fdata2->flags.ref_time) ? -1 : \
                 (!fdata1->flags.ref_time && fdata2->flags.ref_time) ? 1 : \
-                (fdata1->secs < fdata2->secs) ? -1 : \
-                (fdata1->secs > fdata2->secs) ? 1 : \
-                (fdata1->usecs < fdata2->usecs) ? -1 :\
-                (fdata1->usecs > fdata2->usecs) ? 1 : \
+                (fdata1->ts.secs < fdata2->ts.secs) ? -1 : \
+                (fdata1->ts.secs > fdata2->ts.secs) ? 1 : \
+                (fdata1->ts.nsecs < fdata2->ts.nsecs) ? -1 :\
+                (fdata1->ts.nsecs > fdata2->ts.nsecs) ? 1 : \
                 COMPARE_FRAME_NUM())
 static gint
 packet_list_compare(EthCList *clist, gconstpointer  ptr1, gconstpointer  ptr2)
@@ -147,28 +158,33 @@ packet_list_compare(EthCList *clist, gconstpointer  ptr1, gconstpointer  ptr2)
     return COMPARE_FRAME_NUM();
 
   case COL_CLS_TIME:
-    switch (timestamp_type) {
+    switch (timestamp_get_type()) {
 
     case TS_ABSOLUTE:
     case TS_ABSOLUTE_WITH_DATE:
-      return COMPARE_TS(abs_secs, abs_usecs);
+    case TS_EPOCH:
+      return COMPARE_TS(abs_ts);
 
     case TS_RELATIVE:
-      return COMPARE_TS(rel_secs, rel_usecs);
+      return COMPARE_TS(rel_ts);
 
     case TS_DELTA:
-      return COMPARE_TS(del_secs, del_usecs);
+      return COMPARE_TS(del_ts);
+
+    case TS_NOT_SET:
+      return 0;
     }
     return 0;
 
   case COL_ABS_TIME:
-    return COMPARE_TS(abs_secs, abs_usecs);
+  case COL_ABS_DATE_TIME:
+    return COMPARE_TS(abs_ts);
 
   case COL_REL_TIME:
-    return COMPARE_TS(rel_secs, rel_usecs);
+    return COMPARE_TS(rel_ts);
 
   case COL_DELTA_TIME:
-    return COMPARE_TS(del_secs, del_usecs);
+    return COMPARE_TS(del_ts);
 
   case COL_PACKET_LENGTH:
     return COMPARE_NUM(pkt_len);
@@ -256,14 +272,15 @@ packet_list_select_cb(GtkWidget *w _U_, gint row, gint col _U_, gpointer evt _U_
   while( (gtk_notebook_get_nth_page( GTK_NOTEBOOK(byte_nb_ptr), 0)))
     gtk_notebook_remove_page( GTK_NOTEBOOK(byte_nb_ptr), 0);
 
-  select_packet(&cfile, row);
+  cf_select_packet(&cfile, row);
   gtk_widget_grab_focus(packet_list);
+  packet_history_add(row);
 }
 
 static void
 packet_list_unselect_cb(GtkWidget *w _U_, gint row _U_, gint col _U_, gpointer evt _U_) {
 
-  unselect_packet(&cfile);
+  cf_unselect_packet(&cfile);
 }
 
 /* mark packets */
@@ -274,7 +291,7 @@ set_frame_mark(gboolean set, frame_data *frame, gint row) {
   if (row == -1)
     return;
   if (set) {
-    mark_frame(&cfile, frame);
+    cf_mark_frame(&cfile, frame);
     color_t_to_gdkcolor(&fg, &prefs.gui_marked_fg);
     color_t_to_gdkcolor(&bg, &prefs.gui_marked_bg);
     eth_clist_set_foreground(ETH_CLIST(packet_list), row, &fg);
@@ -282,7 +299,7 @@ set_frame_mark(gboolean set, frame_data *frame, gint row) {
   } else {
     color_filter_t *cfilter = frame->color_filter;
 
-    unmark_frame(&cfile, frame);
+    cf_unmark_frame(&cfile, frame);
     /* Restore the color from the matching color filter if any */
     if (cfilter) { /* The packet matches a color filter */
       color_t_to_gdkcolor(&fg, &cfilter->fg_color);
@@ -294,33 +311,43 @@ set_frame_mark(gboolean set, frame_data *frame, gint row) {
       eth_clist_set_background(ETH_CLIST(packet_list), row, NULL);
     }
   }
-  file_set_save_marked_sensitive();
 }
 
-void mark_frame_cb(GtkWidget *w _U_, gpointer data _U_) {
+/* call this after last set_frame_mark is done */
+static void mark_frames_ready(void) {
+  file_save_update_dynamics();
+  packets_bar_update();
+}
+
+void packet_list_mark_frame_cb(GtkWidget *w _U_, gpointer data _U_) {
   if (cfile.current_frame) {
     /* XXX hum, should better have a "cfile->current_row" here ... */
     set_frame_mark(!cfile.current_frame->flags.marked,
                   cfile.current_frame,
                   eth_clist_find_row_from_data(ETH_CLIST(packet_list),
                                                cfile.current_frame));
+    mark_frames_ready();
   }
 }
 
 static void mark_all_frames(gboolean set) {
   frame_data *fdata;
-  
-  cfile.marked_count = 0;      
+
+  /* XXX: we might need a progressbar here */
   for (fdata = cfile.plist; fdata != NULL; fdata = fdata->next) {
     set_frame_mark(set,
                   fdata,
                   eth_clist_find_row_from_data(ETH_CLIST(packet_list), fdata));
   }
+  mark_frames_ready();
 }
 
-void update_marked_frames(void) {
+void packet_list_update_marked_frames(void) {
   frame_data *fdata;
+
   if (cfile.plist == NULL) return;
+
+  /* XXX: we might need a progressbar here */
   for (fdata = cfile.plist; fdata != NULL; fdata = fdata->next) {
     if (fdata->flags.marked)
       set_frame_mark(TRUE,
@@ -328,13 +355,14 @@ void update_marked_frames(void) {
                     eth_clist_find_row_from_data(ETH_CLIST(packet_list),
                                                  fdata));
   }
+  mark_frames_ready();
 }
 
-void mark_all_frames_cb(GtkWidget *w _U_, gpointer data _U_) {
+void packet_list_mark_all_frames_cb(GtkWidget *w _U_, gpointer data _U_) {
   mark_all_frames(TRUE);
 }
 
-void unmark_all_frames_cb(GtkWidget *w _U_, gpointer data _U_) {
+void packet_list_unmark_all_frames_cb(GtkWidget *w _U_, gpointer data _U_) {
   mark_all_frames(FALSE);
 }
 
@@ -342,8 +370,8 @@ gboolean
 packet_list_get_event_row_column(GtkWidget *w, GdkEventButton *event_button,
                                 gint *row, gint *column)
 {
-    return eth_clist_get_selection_info(ETH_CLIST(w), 
-                                 (gint) event_button->x, (gint) event_button->y, 
+    return eth_clist_get_selection_info(ETH_CLIST(w),
+                                 (gint) event_button->x, (gint) event_button->y,
                                   row, column);
 }
 
@@ -363,6 +391,7 @@ packet_list_button_pressed_cb(GtkWidget *w, GdkEvent *event, gpointer data _U_)
         frame_data *fdata = (frame_data *) eth_clist_get_row_data(ETH_CLIST(w),
                                                                   row);
         set_frame_mark(!fdata->flags.marked, fdata, row);
+        mark_frames_ready();
     }
 }
 #else
@@ -382,6 +411,7 @@ packet_list_button_pressed_cb(GtkWidget *w, GdkEvent *event, gpointer data _U_)
         frame_data *fdata = (frame_data *)eth_clist_get_row_data(ETH_CLIST(w),
                                                                  row);
         set_frame_mark(!fdata->flags.marked, fdata, row);
+        mark_frames_ready();
         return TRUE;
     }
     return FALSE;
@@ -390,7 +420,7 @@ packet_list_button_pressed_cb(GtkWidget *w, GdkEvent *event, gpointer data _U_)
 
 /* Set the selection mode of the packet list window. */
 void
-set_plist_sel_browse(gboolean val)
+packet_list_set_sel_browse(gboolean val)
 {
         GtkSelectionMode new_mode;
         /* initialize with a mode we don't use, so that the mode == new_mode
@@ -398,7 +428,7 @@ set_plist_sel_browse(gboolean val)
         static GtkSelectionMode mode = GTK_SELECTION_MULTIPLE;
 
         /* Yeah, GTK uses "browse" in the case where we do not, but oh well. I
-         * think "browse" in Ethereal makes more sense than "SINGLE" in GTK+ */
+         * think "browse" in Wireshark makes more sense than "SINGLE" in GTK+ */
         new_mode = val ? GTK_SELECTION_SINGLE : GTK_SELECTION_BROWSE;
 
        if (mode == new_mode) {
@@ -420,20 +450,15 @@ set_plist_sel_browse(gboolean val)
        }
 
        if (cfile.finfo_selected)
-               unselect_packet(&cfile);
+               cf_unselect_packet(&cfile);
 
         mode = new_mode;
         eth_clist_set_selection_mode(ETH_CLIST(packet_list), mode);
 }
 
 /* Set the font of the packet list window. */
-#if GTK_MAJOR_VERSION < 2
-void
-set_plist_font(GdkFont *font)
-#else
 void
-set_plist_font(PangoFontDescription *font)
-#endif
+packet_list_set_font(FONT_TYPE *font)
 {
        int i;
        gint col_width;
@@ -476,26 +501,42 @@ packet_list_new(e_prefs *prefs)
 
     /* Packet list */
     pkt_scrollw = scrolled_window_new(NULL, NULL);
-    gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW(pkt_scrollw),
-                                    GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
+    /* The usual policy for scrolled windows is to set both scrollbars to automatic,
+     * meaning they'll only appear if the content doesn't fit into the window.
+     *
+     * As this doesn't seem to work in some cases for the vertical scrollbar
+     * (see http://bugs.wireshark.org/bugzilla/show_bug.cgi?id=220),
+     * we show that scrollbar always. */
+    gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(pkt_scrollw),
+                                   GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
+#if GTK_MAJOR_VERSION >= 2
+    /* the eth_clist will have it's own GTK_SHADOW_IN, so don't use a shadow
+     * for both widgets */
+    gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(pkt_scrollw),
+                                    GTK_SHADOW_NONE);
+#endif
 
     packet_list = eth_clist_new(cfile.cinfo.num_cols);
     /* Column titles are filled in below */
     gtk_container_add(GTK_CONTAINER(pkt_scrollw), packet_list);
 
-    set_plist_sel_browse(prefs->gui_plist_sel_browse);
-    set_plist_font(m_r_font);
+    packet_list_set_sel_browse(prefs->gui_plist_sel_browse);
+    packet_list_set_font(user_font_get_regular());
     gtk_widget_set_name(packet_list, "packet list");
     SIGNAL_CONNECT(packet_list, "select-row", packet_list_select_cb, NULL);
     SIGNAL_CONNECT(packet_list, "unselect-row", packet_list_unselect_cb, NULL);
     for (i = 0; i < cfile.cinfo.num_cols; i++) {
-        /* Columns do not automatically resize, but are resizeable by
-           the user. */
+        /* For performance reasons, columns do not automatically resize,
+           but are resizeable by the user. */
         eth_clist_set_column_auto_resize(ETH_CLIST(packet_list), i, FALSE);
         eth_clist_set_column_resizeable(ETH_CLIST(packet_list), i, TRUE);
 
-        /* Right-justify the packet number column. */
-        if (cfile.cinfo.col_fmt[i] == COL_NUMBER)
+        /* Right-justify some special columns. */
+        if (cfile.cinfo.col_fmt[i] == COL_NUMBER ||
+            cfile.cinfo.col_fmt[i] == COL_PACKET_LENGTH ||
+            cfile.cinfo.col_fmt[i] == COL_CUMULATIVE_BYTES ||
+            cfile.cinfo.col_fmt[i] == COL_DCE_CALL ||
+            cfile.cinfo.col_fmt[i] == COL_DCE_CTX)
             eth_clist_set_column_justification(ETH_CLIST(packet_list), i,
                                                GTK_JUSTIFY_RIGHT);
     }
@@ -522,10 +563,10 @@ packet_list_set_column_titles(void)
     win_style = gtk_widget_get_style(top_level);
     ascend_pm = gdk_pixmap_create_from_xpm_d(top_level->window, &ascend_bm,
                                              &win_style->bg[GTK_STATE_NORMAL],
-                                             (gchar **)clist_ascend_xpm);
+                                             (gchar **) clist_ascend_xpm);
     descend_pm = gdk_pixmap_create_from_xpm_d(top_level->window, &descend_bm,
                                               &win_style->bg[GTK_STATE_NORMAL],
-                                              (gchar **)clist_descend_xpm);
+                                              (gchar **) clist_descend_xpm);
 
     col_arrows = (column_arrows *) g_malloc(sizeof(column_arrows) *
                                             cfile.cinfo.num_cols);
@@ -559,6 +600,8 @@ packet_list_set_column_titles(void)
 void
 packet_list_clear(void)
 {
+    packet_history_clear();
+
     eth_clist_clear(ETH_CLIST(packet_list));
 }
 
@@ -568,16 +611,121 @@ packet_list_freeze(void)
     eth_clist_freeze(ETH_CLIST(packet_list));
 }
 
+static void
+packet_list_resize_columns(void) {
+    int         i;
+    int         progbar_nextstep;
+    int         progbar_quantum;
+    gboolean    progbar_stop_flag;
+    GTimeVal    progbar_start_time;
+    float       progbar_val;
+    progdlg_t  *progbar = NULL;
+    gchar       status_str[100];
+
+    /* Update the progress bar when it gets to this value. */
+    progbar_nextstep = 0;
+    /* When we reach the value that triggers a progress bar update,
+       bump that value by this amount. */
+    progbar_quantum = cfile.cinfo.num_cols/N_PROGBAR_UPDATES;
+    /* Progress so far. */
+    progbar_val = 0.0;
+
+    progbar_stop_flag = FALSE;
+    g_get_current_time(&progbar_start_time);
+
+
+    main_window_update();
+
+    for (i = 0; i < cfile.cinfo.num_cols; i++) {
+      /* Create the progress bar if necessary.
+         We check on every iteration of the loop, so that it takes no
+         longer than the standard time to create it (otherwise, for a
+         large file, we might take considerably longer than that standard
+         time in order to get to the next progress bar step). */
+      if (progbar == NULL)
+         progbar = delayed_create_progress_dlg("Resizing", "Resize Columns",
+           TRUE, &progbar_stop_flag, &progbar_start_time, progbar_val);
+
+      if (i >= progbar_nextstep) {
+        /* let's not divide by zero. I should never be started
+         * with count == 0, so let's assert that
+         */
+        g_assert(cfile.cinfo.num_cols > 0);
+
+        progbar_val = (gfloat) i / cfile.cinfo.num_cols;
+
+        if (progbar != NULL) {
+          g_snprintf(status_str, sizeof(status_str),
+                     "%u of %u columns (%s)", i+1, cfile.cinfo.num_cols, cfile.cinfo.col_title[i]);
+          update_progress_dlg(progbar, progbar_val, status_str);
+        }
+
+        progbar_nextstep += progbar_quantum;
+      }
+
+      if (progbar_stop_flag) {
+        /* Well, the user decided to abort the resizing... */
+        break;
+      }
+
+      /* auto resize the current column */
+      eth_clist_set_column_auto_resize(ETH_CLIST(packet_list), i, TRUE);
+
+      /* the current column should be resizeable by the user again */
+      /* (will turn off auto resize again) */
+      eth_clist_set_column_resizeable(ETH_CLIST(packet_list), i, TRUE);
+    }
+
+    /* We're done resizing the columns; destroy the progress bar if it
+       was created. */
+    if (progbar != NULL)
+      destroy_progress_dlg(progbar);
+}
+
+void packet_list_resize_columns_cb(GtkWidget *widget _U_, gpointer data _U_)
+{
+    packet_list_resize_columns();
+}
+
 void
 packet_list_thaw(void)
 {
     eth_clist_thaw(ETH_CLIST(packet_list));
+    packets_bar_update();
+    /*packet_list_resize_columns();*/
 }
 
 void
 packet_list_select_row(gint row)
 {
-    SIGNAL_EMIT_BY_NAME1(packet_list, "select_row", row);
+    SIGNAL_EMIT_BY_NAME(packet_list, "select_row", row);
+}
+
+static void
+packet_list_next_prev(gboolean next)
+{
+#if GTK_MAJOR_VERSION >= 2
+    GtkWidget *focus = gtk_window_get_focus(GTK_WINDOW(top_level));
+#endif
+    SIGNAL_EMIT_BY_NAME(packet_list, "scroll_vertical",
+        next ? GTK_SCROLL_STEP_FORWARD : GTK_SCROLL_STEP_BACKWARD, 0.0);
+#if GTK_MAJOR_VERSION >= 2
+    /* Set the focus back where it was */
+    if (focus)
+        gtk_window_set_focus(GTK_WINDOW(top_level), focus);
+#endif
+}
+
+void
+packet_list_next()
+{
+    packet_list_next_prev(TRUE);
+}
+
+void
+packet_list_prev()
+{
+    packet_list_next_prev(FALSE);
 }
 
 void
@@ -588,11 +736,11 @@ packet_list_moveto_end(void)
 }
 
 gint
-packet_list_append(gchar *text[], gpointer data)
+packet_list_append(const gchar *text[], gpointer data)
 {
     gint row;
 
-    row = eth_clist_append(ETH_CLIST(packet_list), text);
+    row = eth_clist_append(ETH_CLIST(packet_list), (gchar **) text);
     eth_clist_set_row_data(ETH_CLIST(packet_list), row, data);
     return row;
 }
@@ -655,14 +803,49 @@ packet_list_get_row_data(gint row)
     return eth_clist_get_row_data(ETH_CLIST(packet_list), row);
 }
 
+
+/* get the first fully visible row number, given row MUST be visible */
+static gint
+packet_list_first_full_visible_row(gint row) {
+
+       g_assert(eth_clist_row_is_visible(ETH_CLIST(packet_list), row) ==
+        GTK_VISIBILITY_FULL);
+
+       while(eth_clist_row_is_visible(ETH_CLIST(packet_list), row) ==
+        GTK_VISIBILITY_FULL) {
+               row--;
+       }
+
+       return ++row;
+}
+
+/* get the last fully visible row number, given row MUST be visible */
+static gint
+packet_list_last_full_visible_row(gint row) {
+
+       g_assert(eth_clist_row_is_visible(ETH_CLIST(packet_list), row) ==
+        GTK_VISIBILITY_FULL);
+
+       while(eth_clist_row_is_visible(ETH_CLIST(packet_list), row) ==
+        GTK_VISIBILITY_FULL) {
+               row++;
+       }
+
+       return --row;
+}
+
 /* Set the selected row and the focus row of the packet list to the specified
  * row, and make it visible if it's not currently visible. */
 void
 packet_list_set_selected_row(gint row)
 {
-    if (eth_clist_row_is_visible(ETH_CLIST(packet_list), row) !=
-        GTK_VISIBILITY_FULL)
-        eth_clist_moveto(ETH_CLIST(packet_list), row, -1, 0.0, 0.0);
+       gint visible_rows;
+       gint first_row;
+       gboolean full_visible;
+
+
+       full_visible = eth_clist_row_is_visible(ETH_CLIST(packet_list), row) ==
+        GTK_VISIBILITY_FULL;
 
     /* XXX - why is there no "eth_clist_set_focus_row()", so that we
      * can make the row for the frame we found the focus row?
@@ -672,6 +855,36 @@ packet_list_set_selected_row(gint row)
     ETH_CLIST(packet_list)->focus_row = row;
 
     eth_clist_select_row(ETH_CLIST(packet_list), row, -1);
+
+    if (!full_visible) {
+
+        eth_clist_freeze(ETH_CLIST(packet_list));
+
+        eth_clist_moveto(ETH_CLIST(packet_list), row, -1, 0.0, 0.0);
+
+               /* even after move still invisible (happens with empty list) -> give up */
+               if(eth_clist_row_is_visible(ETH_CLIST(packet_list), row) !=
+                       GTK_VISIBILITY_FULL) {
+                       eth_clist_thaw(ETH_CLIST(packet_list));
+                       return;
+               }
+
+               /* The now selected row will be the first visible row in the list.
+                * This is inconvenient, as the user is usually interested in some
+                * packets *before* the currently selected one too.
+                *
+                * Try to adjust the visible rows, so the currently selected row will
+                * be shown around the first third of the list screen.
+                *
+                * (This won't even do any harm if the current row is the first or the
+                * last in the list) */
+               visible_rows = packet_list_last_full_visible_row(row) - packet_list_first_full_visible_row(row);
+               first_row = row - visible_rows / 3;
+
+               eth_clist_moveto(ETH_CLIST(packet_list), first_row >= 0 ? first_row : 0, -1, 0.0, 0.0);
+
+               eth_clist_thaw(ETH_CLIST(packet_list));
+       }
 }
 
 /* Return the column number that the clist is currently sorted by */