Add a new tool which summarizes packet counts by protocols, but
[obnox/wireshark/wip.git] / gtk / proto_hier_stats_dlg.c
1 /* proto_hier_stats_dlg.c
2  *
3  * $Id: proto_hier_stats_dlg.c,v 1.1 2001/03/22 23:54:47 gram Exp $
4  *
5  * Ethereal - Network traffic analyzer
6  * By Gerald Combs <gerald@zing.org>
7  * Copyright 1998 Gerald Combs
8  *
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
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <gtk/gtk.h>
31
32 #include "proto_hier_stats.h"
33 #include "dlg_utils.h"
34 #include "main.h"
35
36 #define NUM_STAT_COLUMNS 6
37
38 typedef struct {
39
40         GtkCTree        *tree;
41         GtkCTreeNode    *parent;
42         ph_stats_t      *ps;
43
44 } draw_info_t;
45
46
47 #define PCT(x,y) (100.0 * (float)(x) / (float)(y))
48
49 static void
50 fill_in_ctree_node(GNode *node, gpointer data)
51 {
52         ph_stats_node_t *stats = node->data;
53         draw_info_t             *di = data;
54
55         GtkCTree                *tree = di->tree;
56         GtkCTreeNode            *parent = di->parent;
57         ph_stats_t              *ps = di->ps;
58
59         gchar                   *text[NUM_STAT_COLUMNS];
60         gboolean                is_leaf;
61         GtkCTreeNode            *new_node;
62
63         draw_info_t             child_di;
64
65         if (g_node_n_children(node) > 0) {
66                 is_leaf = FALSE;
67         }
68         else {
69                 is_leaf = TRUE;
70         }
71
72         text[0] = stats->hfinfo->name;
73         text[1] = g_strdup_printf("%6.2f%%",
74                         PCT(stats->num_pkts_total, ps->tot_packets));
75         text[2] = g_strdup_printf("%u", stats->num_pkts_total);
76         text[3] = g_strdup_printf("%u", stats->num_bytes_total);
77         text[4] = g_strdup_printf("%u", stats->num_pkts_last);
78         text[5] = g_strdup_printf("%u", stats->num_bytes_last);
79
80         new_node = gtk_ctree_insert_node(tree, parent, NULL, text,
81                         5, NULL, NULL, NULL, NULL,
82                         is_leaf, TRUE);
83
84
85         g_free(text[1]);
86         g_free(text[2]);
87         g_free(text[3]);
88         g_free(text[4]);
89         g_free(text[5]);
90
91         child_di.tree = tree;
92         child_di.parent = new_node;
93         child_di.ps = ps;
94
95         g_node_children_foreach(node, G_TRAVERSE_ALL,
96                         fill_in_ctree_node, &child_di);
97
98 }
99
100
101
102 static void
103 fill_in_ctree(GtkWidget *tree, ph_stats_t *ps)
104 {
105         draw_info_t     di;
106
107         di.tree = GTK_CTREE(tree);
108         di.parent = NULL;
109         di.ps = ps;
110
111         g_node_children_foreach(ps->stats_tree, G_TRAVERSE_ALL,
112                         fill_in_ctree_node, &di);
113 }
114
115 static void
116 create_tree(GtkWidget *container)
117 {
118         GtkWidget       *sw, *tree;
119         ph_stats_t      *ps;
120         int             i, height;
121         gchar           *column_titles[NUM_STAT_COLUMNS] = {
122                 "Protocol",
123                 "Percentage Packets",
124                 "Packets",
125                 "Bytes",
126                 "Last-Protocol Packets",
127                 "Last-Protocol Bytes",
128         };
129
130         ps = ph_stats_new();
131
132         /* Scrolled Window */
133         sw = gtk_scrolled_window_new(NULL, NULL);
134         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw),
135                         GTK_POLICY_AUTOMATIC,
136                         GTK_POLICY_AUTOMATIC);
137         gtk_container_add(GTK_CONTAINER(container), sw);
138
139         tree = gtk_ctree_new_with_titles(NUM_STAT_COLUMNS, 0, column_titles);
140
141         /* XXX - get 'pos' to set vertical scroll-bar placement. */
142         /* XXX - set line style from preferences ???. */
143
144         /* The titel bars do nothing. */
145         gtk_clist_column_titles_passive(GTK_CLIST(tree));
146
147         /* Auto Resize all columns */
148         for (i = 0; i < NUM_STAT_COLUMNS; i++) {
149                 gtk_clist_set_column_auto_resize(GTK_CLIST(tree), i, TRUE);
150         }
151                                 
152
153         /* Right justify numeric columns */
154         for (i = 1; i <= 5; i++) {
155                 gtk_clist_set_column_justification(GTK_CLIST(tree), i,
156                                 GTK_JUSTIFY_RIGHT);
157         }
158
159         /* Fill in the data. */
160         fill_in_ctree(tree, ps);
161
162         /* Try to size the CTree to a good initial size.
163          * 5 is a magic number that I pulled out off my hat.
164          * Using DEF_WIDTH is pretty bogus, too. */
165         height = GTK_CLIST(tree)->rows * (GTK_CLIST(tree)->row_height + 5);
166         gtk_widget_set_usize(tree, DEF_WIDTH, height);
167
168
169         gtk_container_add(GTK_CONTAINER(sw), tree);
170         ph_stats_free(ps);
171 }
172
173 void
174 proto_hier_stats_cb(GtkWidget *w, gpointer d)
175 {
176         GtkWidget       *dlg, *bt, *vbox, *frame, *bbox;
177         const gchar     *wname = "Protocol Hierarchy Statistics";
178
179         dlg = dlg_window_new(wname);
180
181         vbox = gtk_vbox_new(FALSE, 5);
182         gtk_container_border_width(GTK_CONTAINER(vbox), 5);
183         gtk_container_add(GTK_CONTAINER(dlg), vbox);
184
185         frame = gtk_frame_new(wname);
186         /*gtk_container_add(GTK_CONTAINER(vbox), frame);*/
187         gtk_box_pack_start(GTK_BOX(vbox), frame, TRUE, TRUE, 0);
188
189
190         /* Data section */
191         create_tree(frame);
192
193         /* Button row. We put it in an HButtonBox to
194          * keep it from expanding to the width of the window. */
195         bbox = gtk_hbutton_box_new();
196         gtk_button_box_set_spacing(GTK_BUTTON_BOX(bbox), 5);
197         /*gtk_container_add(GTK_CONTAINER(vbox), bbox);*/
198         gtk_box_pack_start(GTK_BOX(vbox), bbox, FALSE, FALSE, 0);
199
200         /* Close button */
201         bt = gtk_button_new_with_label("Close");
202         gtk_signal_connect_object(GTK_OBJECT(bt), "clicked",
203                         GTK_SIGNAL_FUNC(gtk_widget_destroy),
204                         GTK_OBJECT(dlg));
205         gtk_container_add(GTK_CONTAINER(bbox), bt);
206         GTK_WIDGET_SET_FLAGS(bt, GTK_CAN_DEFAULT);
207         gtk_widget_grab_default(bt);
208         dlg_set_cancel(dlg, bt);
209
210         gtk_widget_show_all(dlg);
211
212 }
213