Removed trailing whitespaces from .h and .c files using the
[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.10 2002/08/28 21:03:49 jmayer Exp $
4  *
5  * Ethereal - Network traffic analyzer
6  * By Gerald Combs <gerald@ethereal.com>
7  * Copyright 1998 Gerald Combs
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  */
23
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <gtk/gtk.h>
30
31 #include "proto_hier_stats.h"
32 #include "dlg_utils.h"
33 #include "ui_util.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 #define MAX_DLG_HEIGHT 450
116 #define DEF_DLG_WIDTH  600
117 static void
118 create_tree(GtkWidget *container, ph_stats_t *ps)
119 {
120         GtkWidget       *sw, *tree;
121         int             i, height;
122         gchar           *column_titles[NUM_STAT_COLUMNS] = {
123                 "Protocol",
124                 "% Packets",
125                 "Packets",
126                 "Bytes",
127                 "End Packets",
128                 "End Bytes",
129         };
130
131         /* Scrolled Window */
132         sw = gtk_scrolled_window_new(NULL, NULL);
133         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw),
134                         GTK_POLICY_AUTOMATIC,
135                         GTK_POLICY_AUTOMATIC);
136         gtk_container_add(GTK_CONTAINER(container), sw);
137
138         tree = ctree_new_with_titles(NUM_STAT_COLUMNS, 0, column_titles);
139
140         /* XXX - get 'pos' to set vertical scroll-bar placement. */
141
142         /* The title bars do nothing. */
143         gtk_clist_column_titles_passive(GTK_CLIST(tree));
144
145         /* Auto Resize all columns */
146         for (i = 0; i < NUM_STAT_COLUMNS; i++) {
147                 gtk_clist_set_column_auto_resize(GTK_CLIST(tree), i, TRUE);
148         }
149
150
151         /* Right justify numeric columns */
152         for (i = 1; i <= 5; i++) {
153                 gtk_clist_set_column_justification(GTK_CLIST(tree), i,
154                                 GTK_JUSTIFY_RIGHT);
155         }
156
157         /* Fill in the data. */
158         fill_in_ctree(tree, ps);
159
160         height = GTK_CLIST(tree)->rows * (GTK_CLIST(tree)->row_height + 5);
161         height = MIN(height, MAX_DLG_HEIGHT);
162         gtk_widget_set_usize(tree, DEF_DLG_WIDTH, height);
163
164
165         gtk_container_add(GTK_CONTAINER(sw), tree);
166         ph_stats_free(ps);
167 }
168
169 #define WNAME "Protocol Hierarchy Statistics"
170
171 void
172 proto_hier_stats_cb(GtkWidget *w _U_, gpointer d _U_)
173 {
174         ph_stats_t      *ps;
175         GtkWidget       *dlg, *bt, *vbox, *frame, *bbox;
176
177         /* Get the statistics. */
178         ps = ph_stats_new();
179         if (ps == NULL) {
180                 /* The user gave up before we finished; don't pop up
181                    a statistics window. */
182                 return;
183         }
184
185         dlg = gtk_window_new(GTK_WINDOW_TOPLEVEL);
186         gtk_window_set_title(GTK_WINDOW(dlg), "Ethereal: " WNAME);
187         gtk_signal_connect (GTK_OBJECT (dlg), "realize",
188                 GTK_SIGNAL_FUNC (window_icon_realize_cb), NULL);
189
190         vbox = gtk_vbox_new(FALSE, 5);
191         gtk_container_border_width(GTK_CONTAINER(vbox), 5);
192         gtk_container_add(GTK_CONTAINER(dlg), vbox);
193
194         frame = gtk_frame_new(WNAME);
195         /*gtk_container_add(GTK_CONTAINER(vbox), frame);*/
196         gtk_box_pack_start(GTK_BOX(vbox), frame, TRUE, TRUE, 0);
197
198
199         /* Data section */
200         create_tree(frame, ps);
201
202         /* Button row. We put it in an HButtonBox to
203          * keep it from expanding to the width of the window. */
204         bbox = gtk_hbutton_box_new();
205         gtk_button_box_set_spacing(GTK_BUTTON_BOX(bbox), 5);
206         /*gtk_container_add(GTK_CONTAINER(vbox), bbox);*/
207         gtk_box_pack_start(GTK_BOX(vbox), bbox, FALSE, FALSE, 0);
208
209         /* Close button */
210         bt = gtk_button_new_with_label("Close");
211         gtk_signal_connect_object(GTK_OBJECT(bt), "clicked",
212                         GTK_SIGNAL_FUNC(gtk_widget_destroy),
213                         GTK_OBJECT(dlg));
214         gtk_container_add(GTK_CONTAINER(bbox), bt);
215         GTK_WIDGET_SET_FLAGS(bt, GTK_CAN_DEFAULT);
216         gtk_widget_grab_default(bt);
217         dlg_set_cancel(dlg, bt);
218
219         gtk_widget_show_all(dlg);
220
221 }
222