In GTK+ 2.x, "gtk_entry_get_text()" returns a "const gchar *"; assign
[obnox/wireshark/wip.git] / gtk / smb_stat.c
index b7c9cc64bedf53e67d0593f51bca707c07837063..8806f4bc8e8352d12b5c2d8037bb18b17a5af59d 100644 (file)
@@ -1,7 +1,7 @@
 /* smb_stat.c
  * smb_stat   2003 Ronnie Sahlberg
  *
- * $Id: smb_stat.c,v 1.7 2003/06/21 06:40:48 sahlberg Exp $
+ * $Id: smb_stat.c,v 1.25 2003/12/04 00:45:39 guy Exp $
  *
  * Ethereal - Network traffic analyzer
  * By Gerald Combs <gerald@ethereal.com>
@@ -34,8 +34,9 @@
 
 #include <gtk/gtk.h>
 #include <string.h>
-#include "menu.h"
 #include "../epan/packet_info.h"
+#include "../epan/epan.h"
+#include "menu.h"
 #include "../tap.h"
 #include "../epan/value_string.h"
 #include "../smb.h"
 #include "../timestats.h"
 #include "compat_macros.h"
 #include "../simple_dialog.h"
+#include "dlg_utils.h"
 #include "../file.h"
 #include "../globals.h"
+#include "filter_prefs.h"
 #include "service_response_time_table.h"
 
+extern GtkWidget   *main_display_filter_widget;
+
 /* used to keep track of the statistics for an entire program interface */
 typedef struct _smbstat_t {
        GtkWidget *win;
@@ -55,7 +60,16 @@ typedef struct _smbstat_t {
        srt_stat_table nt_trans_srt_table;
 } smbstat_t;
 
+static void
+smbstat_set_title(smbstat_t *ss)
+{
+       char *title;
 
+       title = g_strdup_printf("SMB Service Response Time statistics: %s",
+           cf_get_display_name(&cfile));
+       gtk_window_set_title(GTK_WINDOW(ss->win), title);
+       g_free(title);
+}
 
 static void
 smbstat_reset(void *pss)
@@ -65,6 +79,7 @@ smbstat_reset(void *pss)
        reset_srt_table_data(&ss->smb_srt_table);
        reset_srt_table_data(&ss->trans2_srt_table);
        reset_srt_table_data(&ss->nt_trans_srt_table);
+       smbstat_set_title(ss);
 }
 
 static int
@@ -87,11 +102,15 @@ smbstat_packet(void *pss, packet_info *pinfo, epan_dissect_t *edt _U_, void *psi
        if(si->cmd==0xA0){
                smb_nt_transact_info_t *sti=(smb_nt_transact_info_t *)si->sip->extra_info;
 
-               add_srt_table_data(&ss->nt_trans_srt_table, sti->subcmd, &si->sip->req_time, pinfo);
+               if(sti){
+                       add_srt_table_data(&ss->nt_trans_srt_table, sti->subcmd, &si->sip->req_time, pinfo);
+               }
        } else if(si->cmd==0x32){
                smb_transact2_info_t *st2i=(smb_transact2_info_t *)si->sip->extra_info;
 
-               add_srt_table_data(&ss->trans2_srt_table, st2i->subcmd, &si->sip->req_time, pinfo);
+               if(st2i){
+                       add_srt_table_data(&ss->trans2_srt_table, st2i->subcmd, &si->sip->req_time, pinfo);
+               }
        }
 
        return 1;
@@ -148,8 +167,8 @@ gtk_smbstat_init(char *optarg)
        ss=g_malloc(sizeof(smbstat_t));
 
        ss->win=gtk_window_new(GTK_WINDOW_TOPLEVEL);
-       gtk_window_set_default_size(ss->win, 550, 600);
-       gtk_window_set_title(GTK_WINDOW(ss->win), "SMB Service Response Time statistics");
+       gtk_window_set_default_size(GTK_WINDOW(ss->win), 550, 600);
+       smbstat_set_title(ss);
        SIGNAL_CONNECT(ss->win, "destroy", win_destroy_cb, ss);
 
        vbox=gtk_vbox_new(FALSE, 0);
@@ -170,7 +189,11 @@ gtk_smbstat_init(char *optarg)
        label=gtk_label_new("SMB Commands");
        gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
        gtk_widget_show(label);
-       init_srt_table(&ss->smb_srt_table, 256, vbox);
+
+       /* We must display TOP LEVEL Widget before calling init_srt_table() */
+       gtk_widget_show(ss->win);
+
+       init_srt_table(&ss->smb_srt_table, 256, vbox, "smb.cmd");
        for(i=0;i<256;i++){
                init_srt_table_row(&ss->smb_srt_table, i, val_to_str(i, smb_cmd_vals, "Unknown(0x%02x)"));
        }
@@ -179,7 +202,7 @@ gtk_smbstat_init(char *optarg)
        label=gtk_label_new("Transaction2 Sub-Commands");
        gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
        gtk_widget_show(label);
-       init_srt_table(&ss->trans2_srt_table, 256, vbox);
+       init_srt_table(&ss->trans2_srt_table, 256, vbox, "smb.trans2.cmd");
        for(i=0;i<256;i++){
                init_srt_table_row(&ss->trans2_srt_table, i, val_to_str(i, trans2_cmd_vals, "Unknown(0x%02x)"));
        }
@@ -188,7 +211,7 @@ gtk_smbstat_init(char *optarg)
        label=gtk_label_new("NT Transaction Sub-Commands");
        gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
        gtk_widget_show(label);
-       init_srt_table(&ss->nt_trans_srt_table, 256, vbox);
+       init_srt_table(&ss->nt_trans_srt_table, 256, vbox, "smb.nt.function");
        for(i=0;i<256;i++){
                init_srt_table_row(&ss->nt_trans_srt_table, i, val_to_str(i, nt_cmd_vals, "Unknown(0x%02x)"));
        }
@@ -208,10 +231,8 @@ gtk_smbstat_init(char *optarg)
 
 
 
-static GtkWidget *dlg=NULL, *dlg_box;
-static GtkWidget *filter_box;
-static GtkWidget *filter_label, *filter_entry;
-static GtkWidget *start_button;
+static GtkWidget *dlg=NULL;
+static GtkWidget *filter_entry;
 
 static void
 dlg_destroy_cb(void)
@@ -219,61 +240,122 @@ dlg_destroy_cb(void)
        dlg=NULL;
 }
 
+static void
+dlg_cancel_cb(GtkWidget *cancel_bt _U_, gpointer parent_w)
+{
+       gtk_widget_destroy(GTK_WIDGET(parent_w));
+}
+
 static void
 smbstat_start_button_clicked(GtkWidget *item _U_, gpointer data _U_)
 {
+       GString *str;
        char *filter;
-       char str[256];
 
+       str = g_string_new("smb,srt");
        filter=(char *)gtk_entry_get_text(GTK_ENTRY(filter_entry));
-       if(filter[0]==0){
-               gtk_smbstat_init("smb,srt");
-       } else {
-               sprintf(str,"smb,srt,%s", filter);
-               gtk_smbstat_init(str);
+       if(filter[0]!=0){
+               g_string_sprintfa(str,",%s", filter);
        }
+       gtk_smbstat_init(str->str);
+       g_string_free(str, TRUE);
 }
 
 static void
 gtk_smbstat_cb(GtkWidget *w _U_, gpointer d _U_)
 {
+       GtkWidget *dlg_box;
+       GtkWidget *filter_box, *filter_bt;
+       GtkWidget *bbox, *start_button, *cancel_button;
+       const char *filter;
+       static construct_args_t args = {
+         "Service Response Time Statistics Filter",
+         TRUE,
+         FALSE
+       };
+
        /* if the window is already open, bring it to front */
        if(dlg){
                gdk_window_raise(dlg->window);
                return;
        }
 
-       dlg=gtk_window_new(GTK_WINDOW_TOPLEVEL);
-       gtk_window_set_title(GTK_WINDOW(dlg), "SMB Service Response Time statistics");
+       dlg=dlg_window_new("Ethereal: Compute SMB SRT statistics");
        SIGNAL_CONNECT(dlg, "destroy", dlg_destroy_cb, NULL);
-       dlg_box=gtk_vbox_new(FALSE, 0);
+
+       dlg_box=gtk_vbox_new(FALSE, 10);
+       gtk_container_border_width(GTK_CONTAINER(dlg_box), 10);
        gtk_container_add(GTK_CONTAINER(dlg), dlg_box);
        gtk_widget_show(dlg_box);
 
+       /* Filter box */
+       filter_box=gtk_hbox_new(FALSE, 3);
 
-       /* filter box */
-       filter_box=gtk_hbox_new(FALSE, 10);
-       /* Filter label */
-       gtk_container_set_border_width(GTK_CONTAINER(filter_box), 10);
-       filter_label=gtk_label_new("Filter:");
-       gtk_box_pack_start(GTK_BOX(filter_box), filter_label, FALSE, FALSE, 0);
-       gtk_widget_show(filter_label);
+       /* Filter button */
+       filter_bt = gtk_button_new_with_label("Filter:");
+       SIGNAL_CONNECT(filter_bt, "clicked", display_filter_construct_cb, &args);
+       gtk_box_pack_start(GTK_BOX(filter_box), filter_bt, FALSE, TRUE, 0);
+       gtk_widget_show(filter_bt);
 
-       filter_entry=gtk_entry_new_with_max_length(250);
-       gtk_box_pack_start(GTK_BOX(filter_box), filter_entry, FALSE, FALSE, 0);
+       /* Filter entry */
+       filter_entry=gtk_entry_new();
+       gtk_widget_set_usize(filter_entry, 300, -2);
+
+       /* filter prefs dialog */
+       OBJECT_SET_DATA(filter_bt, E_FILT_TE_PTR_KEY, filter_entry);
+       /* filter prefs dialog */
+
+       gtk_box_pack_start(GTK_BOX(filter_box), filter_entry, TRUE, TRUE, 0);
+       filter=gtk_entry_get_text(GTK_ENTRY(main_display_filter_widget));
+       if(filter){
+               gtk_entry_set_text(GTK_ENTRY(filter_entry), filter);
+       }
        gtk_widget_show(filter_entry);
 
        gtk_box_pack_start(GTK_BOX(dlg_box), filter_box, TRUE, TRUE, 0);
        gtk_widget_show(filter_box);
 
+       /* button box */
+       bbox=gtk_hbutton_box_new();
+       gtk_button_box_set_layout (GTK_BUTTON_BOX (bbox), GTK_BUTTONBOX_DEFAULT_STYLE);
+       gtk_button_box_set_spacing(GTK_BUTTON_BOX(bbox), 5);
+       gtk_box_pack_start(GTK_BOX(dlg_box), bbox, FALSE, FALSE, 0);
+       gtk_widget_show(bbox);
 
        /* the start button */
        start_button=gtk_button_new_with_label("Create Stat");
         SIGNAL_CONNECT_OBJECT(start_button, "clicked",
                               smbstat_start_button_clicked, NULL);
-       gtk_box_pack_start(GTK_BOX(dlg_box), start_button, TRUE, TRUE, 0);
+       gtk_box_pack_start(GTK_BOX(bbox), start_button, TRUE, TRUE, 0);
+       GTK_WIDGET_SET_FLAGS(start_button, GTK_CAN_DEFAULT);
+       gtk_widget_grab_default(start_button);
        gtk_widget_show(start_button);
 
+#if GTK_MAJOR_VERSION < 2
+       cancel_button=gtk_button_new_with_label("Cancel");
+#else
+       cancel_button=gtk_button_new_from_stock(GTK_STOCK_CANCEL);
+#endif
+       SIGNAL_CONNECT(cancel_button, "clicked", dlg_cancel_cb, dlg);
+       GTK_WIDGET_SET_FLAGS(cancel_button, GTK_CAN_DEFAULT);
+       gtk_box_pack_start(GTK_BOX(bbox), cancel_button, TRUE, TRUE, 0);
+       gtk_widget_show(cancel_button);
+
+       /* Catch the "activate" signal on the filter text entry, so that
+          if the user types Return there, we act as if the "Create Stat"
+          button had been selected, as happens if Return is typed if some
+          widget that *doesn't* handle the Return key has the input
+          focus. */
+       dlg_set_activate(filter_entry, start_button);
+
+       /* Catch the "key_press_event" signal in the window, so that we can
+          catch the ESC key being pressed and act as if the "Cancel" button
+          had been selected. */
+       dlg_set_cancel(dlg, cancel_button);
+
+       /* Give the initial focus to the "Filter" entry box. */
+       gtk_widget_grab_focus(filter_entry);
+
        gtk_widget_show_all(dlg);
 }
 
@@ -286,5 +368,6 @@ register_tap_listener_gtksmbstat(void)
 void
 register_tap_menu_gtksmbstat(void)
 {
-       register_tap_menu_item("Service Response Time/SMB", gtk_smbstat_cb);
+       register_tap_menu_item("Statistics/Service Response Time/SMB...",
+           gtk_smbstat_cb, NULL, NULL);
 }