Merge gtk and gtk2 directories.
[obnox/wireshark/wip.git] / gtk / summary_dlg.c
1 /* summary_dlg.c
2  * Routines for capture file summary window
3  *
4  * $Id: summary_dlg.c,v 1.16 2002/11/03 17:38:34 oabad Exp $
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@zing.org>
8  * Copyright 1998 Gerald Combs
9  *
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  */
25
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <gtk/gtk.h>
32
33 #include <wtap.h>
34
35 #ifdef NEED_SNPRINTF_H
36 # include "snprintf.h"
37 #endif
38
39 #include "summary.h"
40 #include "summary_dlg.h"
41 #include "dlg_utils.h"
42 #include "ui_util.h"
43
44 #define SUM_STR_MAX 1024
45
46
47 static void
48 add_string_to_box(gchar *str, GtkWidget *box)
49 {
50   GtkWidget *lb;
51   lb = gtk_label_new(str);
52   gtk_misc_set_alignment(GTK_MISC(lb), 0.0, 0.5);
53   gtk_box_pack_start(GTK_BOX(box), lb,FALSE,FALSE, 0);
54   gtk_widget_show(lb);
55 }
56
57
58 void
59 summary_open_cb(GtkWidget *w _U_, gpointer d _U_)
60 {
61   summary_tally summary;
62   GtkWidget     *sum_open_w,
63                 *main_vb, *file_fr, *data_fr, *capture_fr, *file_box,
64                 *data_box, *capture_box, *bbox, *close_bt;
65
66   gchar          string_buff[SUM_STR_MAX];
67
68   double         seconds;
69
70  /* initialize the tally */
71   summary_fill_in(&summary);
72
73   /* initial compututations */
74   seconds = summary.stop_time - summary.start_time;
75   sum_open_w = gtk_window_new(GTK_WINDOW_TOPLEVEL);
76   gtk_window_set_title(GTK_WINDOW(sum_open_w), "Ethereal: Summary");
77 #if GTK_MAJOR_VERSION < 2
78   gtk_signal_connect(GTK_OBJECT(sum_open_w), "realize",
79                      GTK_SIGNAL_FUNC(window_icon_realize_cb), NULL);
80 #else
81   g_signal_connect(G_OBJECT(sum_open_w), "realize",
82                    G_CALLBACK(window_icon_realize_cb), NULL);
83 #endif
84
85   /* Container for each row of widgets */
86   main_vb = gtk_vbox_new(FALSE, 3);
87   gtk_container_border_width(GTK_CONTAINER(main_vb), 5);
88   gtk_container_add(GTK_CONTAINER(sum_open_w), main_vb);
89   gtk_widget_show(main_vb);
90
91   /* File frame */
92   file_fr = gtk_frame_new("File");
93   gtk_container_add(GTK_CONTAINER(main_vb), file_fr);
94   gtk_widget_show(file_fr);
95
96   file_box = gtk_vbox_new(FALSE, 3);
97   gtk_container_add(GTK_CONTAINER(file_fr), file_box);
98   gtk_widget_show(file_box);
99
100   /* filename */
101   snprintf(string_buff, SUM_STR_MAX, "Name: %s", summary.filename);
102   add_string_to_box(string_buff, file_box);
103
104   /* length */
105   snprintf(string_buff, SUM_STR_MAX, "Length: %lu", summary.file_length);
106   add_string_to_box(string_buff, file_box);
107
108   /* format */
109   snprintf(string_buff, SUM_STR_MAX, "Format: %s", wtap_file_type_string(summary.encap_type));
110   add_string_to_box(string_buff, file_box);
111
112   if (summary.has_snap) {
113     /* snapshot length */
114     snprintf(string_buff, SUM_STR_MAX, "Snapshot length: %u", summary.snap);
115     add_string_to_box(string_buff, file_box);
116   }
117
118   /* Data frame */
119   data_fr = gtk_frame_new("Data");
120   gtk_container_add(GTK_CONTAINER(main_vb), data_fr);
121   gtk_widget_show(data_fr);
122
123   data_box = gtk_vbox_new(FALSE, 3);
124   gtk_container_add(GTK_CONTAINER(data_fr), data_box);
125   gtk_widget_show(data_box);
126
127   /* seconds */
128   snprintf(string_buff, SUM_STR_MAX, "Elapsed time: %.3f seconds", summary.elapsed_time);
129   add_string_to_box(string_buff, data_box);
130
131   snprintf(string_buff, SUM_STR_MAX, "Between first and last packet: %.3f seconds", seconds);
132   add_string_to_box(string_buff, data_box);
133
134   /* Packet count */
135   snprintf(string_buff, SUM_STR_MAX, "Packet count: %i", summary.packet_count);
136   add_string_to_box(string_buff, data_box);
137
138   /* Filtered Packet count */
139   snprintf(string_buff, SUM_STR_MAX, "Filtered packet count: %i", summary.filtered_count);
140   add_string_to_box(string_buff, data_box);
141
142   /* Marked Packet count */
143   snprintf(string_buff, SUM_STR_MAX, "Marked packet count: %i", summary.marked_count);
144   add_string_to_box(string_buff, data_box);
145
146   /* Packets per second */
147   if (seconds > 0){
148     snprintf(string_buff, SUM_STR_MAX, "Avg. packets/sec: %.3f", summary.packet_count/seconds);
149     add_string_to_box(string_buff, data_box);
150   }
151
152   /* Dropped count */
153   if (summary.drops_known) {
154     snprintf(string_buff, SUM_STR_MAX, "Dropped packets: %u", summary.drops);
155     add_string_to_box(string_buff, data_box);
156   }
157
158   /* Byte count */
159   snprintf(string_buff, SUM_STR_MAX, "Bytes of traffic: %d", summary.bytes);
160   add_string_to_box(string_buff, data_box);
161
162   /* Bytes per second */
163   if (seconds > 0){
164     snprintf(string_buff, SUM_STR_MAX, "Avg. bytes/sec: %.3f", summary.bytes/seconds);
165     add_string_to_box(string_buff, data_box);
166
167     /* MBit per second */
168     snprintf(string_buff, SUM_STR_MAX, "Avg. Mbit/sec: %.3f",
169              summary.bytes * 8.0 / (seconds * 1000.0 * 1000.0));
170     add_string_to_box(string_buff, data_box);
171   }
172
173   /* Capture frame */
174   capture_fr = gtk_frame_new("Capture");
175   gtk_container_add(GTK_CONTAINER(main_vb), capture_fr);
176   gtk_widget_show(capture_fr);
177
178   capture_box = gtk_vbox_new(FALSE, 3);
179   gtk_container_add(GTK_CONTAINER(capture_fr), capture_box);
180   gtk_widget_show(capture_box);
181
182
183   /* interface */
184   if (summary.iface) {
185     snprintf(string_buff, SUM_STR_MAX, "Interface: %s", summary.iface);
186   } else {
187     sprintf(string_buff, "Interface: unknown");
188   }
189   add_string_to_box(string_buff, capture_box);
190
191   /* Display filter */
192   if (summary.dfilter) {
193     snprintf(string_buff, SUM_STR_MAX, "Display filter: %s", summary.dfilter);
194   } else {
195     sprintf(string_buff, "Display filter: none");
196   }
197   add_string_to_box(string_buff, capture_box);
198
199 #ifdef HAVE_LIBPCAP
200   /* Capture filter */
201   if (summary.cfilter && summary.cfilter[0] != '\0') {
202     snprintf(string_buff, SUM_STR_MAX, "Capture filter: %s", summary.cfilter);
203   } else {
204     sprintf(string_buff, "Capture filter: none");
205   }
206   add_string_to_box(string_buff, capture_box);
207 #endif
208
209   /* Button row: close button.
210      (We put it in an HButtonBox, even though there's only one of them,
211      so that it doesn't expand to the width of the window.  */
212   bbox = gtk_hbutton_box_new();
213   gtk_button_box_set_spacing(GTK_BUTTON_BOX(bbox), 5);
214   gtk_container_add(GTK_CONTAINER(main_vb), bbox);
215   gtk_widget_show(bbox);
216
217   /* Create Close Button */
218 #if GTK_MAJOR_VERSION < 2
219   close_bt = gtk_button_new_with_label("Close");
220   gtk_signal_connect_object(GTK_OBJECT(close_bt), "clicked",
221                             GTK_SIGNAL_FUNC(gtk_widget_destroy),
222                             GTK_OBJECT(sum_open_w));
223 #else
224   close_bt = gtk_button_new_from_stock(GTK_STOCK_CLOSE);
225   g_signal_connect_swapped(G_OBJECT(close_bt), "clicked",
226                            G_CALLBACK(gtk_widget_destroy),
227                            G_OBJECT(sum_open_w));
228 #endif
229   GTK_WIDGET_SET_FLAGS(close_bt, GTK_CAN_DEFAULT);
230   gtk_box_pack_start(GTK_BOX(bbox), close_bt, FALSE,FALSE, 0);
231   gtk_widget_grab_default(close_bt);
232   gtk_widget_show(close_bt);
233
234   /* Catch the "key_press_event" signal in the window, so that we can catch
235      the ESC key being pressed and act as if the "Close" button had
236      been selected. */
237   dlg_set_cancel(sum_open_w, close_bt);
238
239   gtk_window_set_position(GTK_WINDOW(sum_open_w), GTK_WIN_POS_MOUSE);
240   gtk_widget_show(sum_open_w);
241 }