2 * GTK Tap implementation of stats_tree
3 * 2005, Luis E. G. Ontanon
7 * Ethereal - Network traffic analyzer
8 * By Gerald Combs <gerald@ethereal.com>
9 * Copyright 1998 Gerald Combs
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.
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.
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.
33 #include <epan/stats_tree_priv.h>
35 #include "simple_dialog.h"
37 #include <epan/stat_cmd_args.h>
38 #include "stat_menu.h"
39 #include "gui_utils.h"
40 #include "dlg_utils.h"
41 #include "compat_macros.h"
42 #include "tap_dfilter_dlg.h"
43 #include "../tap_dfilter_dlg.h"
45 struct _st_node_pres {
46 #if GTK_MAJOR_VERSION >= 2
53 struct _tree_cfg_pres {
54 tap_dfilter_dlg* stat_dlg;
61 #if GTK_MAJOR_VERSION >= 2
69 /* the columns of the tree pane */
70 enum _stat_tree_columns {
78 /* used for converting numbers */
79 #define NUM_BUF_SIZE 32
81 /* creates the gtk representation for a stat_node
84 static void setup_gtk_node_pr(stat_node* node) {
85 #if GTK_MAJOR_VERSION >= 2
86 GtkTreeIter* parent = NULL;
88 GtkCTreeNode* parent = NULL;
89 static gchar *text[] = {
98 node->pr = g_malloc(sizeof(st_node_pres));
100 #if GTK_MAJOR_VERSION >= 2
101 if (node->st->pr->store) {
102 node->pr->iter = g_malloc0(sizeof(GtkTreeIter));
104 if ( node->parent && node->parent->pr ) {
105 parent = node->parent->pr->iter;
107 gtk_tree_store_append (node->st->pr->store, node->pr->iter, parent);
108 gtk_tree_store_set(node->st->pr->store, node->pr->iter, TITLE_COLUMN, node->name, RATE_COLUMN, "", COUNT_COLUMN, "", -1);
111 if (node->st->pr->ctree) {
112 if ( node->parent && node->parent->pr ) {
113 parent = node->parent->pr->node;
116 text[0] = node->name;
117 node->pr->node = gtk_ctree_insert_node(GTK_CTREE(node->st->pr->ctree),
118 parent, NULL, text, 0, NULL, NULL, NULL, NULL, FALSE, FALSE);
120 /* Force the children of the root node to be expanded. */
121 gtk_ctree_expand(GTK_CTREE(node->st->pr->ctree),
129 static void draw_gtk_node(stat_node* node) {
130 static gchar value[NUM_BUF_SIZE];
131 static gchar rate[NUM_BUF_SIZE];
132 static gchar percent[NUM_BUF_SIZE];
135 stats_tree_get_strs_from_node(node, value, rate, percent);
137 #if GTK_MAJOR_VERSION >= 2
138 if (node->st->pr->store) {
139 gtk_tree_store_set(node->st->pr->store, node->pr->iter,
142 PERCENT_COLUMN, percent,
146 if (node->st->pr->ctree) {
147 gtk_ctree_node_set_text(GTK_CTREE(node->st->pr->ctree),
148 node->pr->node, RATE_COLUMN, rate);
149 gtk_ctree_node_set_text(GTK_CTREE(node->st->pr->ctree),
150 node->pr->node, COUNT_COLUMN, value);
151 gtk_ctree_node_set_text(GTK_CTREE(node->st->pr->ctree),
152 node->pr->node, PERCENT_COLUMN, percent);
156 if (node->children) {
157 for (child = node->children; child; child = child->next )
158 draw_gtk_node(child);
162 static void draw_gtk_tree( void *psp ) {
163 stats_tree *st = psp;
166 for (child = st->root.children; child; child = child->next ) {
167 draw_gtk_node(child);
169 #if GTK_MAJOR_VERSION >= 2
170 gtk_tree_view_expand_row(GTK_TREE_VIEW(st->pr->tree),
171 gtk_tree_model_get_path(GTK_TREE_MODEL(st->pr->store),
179 void protect_thread_critical_region(void);
180 void unprotect_thread_critical_region(void);
182 static void free_gtk_tree(GtkWindow *win _U_, stats_tree *st)
185 protect_thread_critical_region();
186 remove_tap_listener(st);
187 unprotect_thread_critical_region();
189 #if GTK_MAJOR_VERSION >= 2
191 st->root.pr->iter = NULL;
199 /* initializes the stats_tree window */
200 static void init_gtk_tree(const char* optarg) {
201 guint8* abbr = stats_tree_get_abbr(optarg);
202 stats_tree* st = NULL;
203 stats_tree_cfg* cfg = NULL;
204 tree_pres* pr = g_malloc(sizeof(tree_pres));
205 guint8* title = NULL;
206 guint8* window_name = NULL;
207 GString* error_string;
210 GtkWidget *main_vb, *bbox, *bt_close;
211 #if GTK_MAJOR_VERSION >= 2
212 GtkTreeViewColumn* column;
213 GtkCellRenderer* renderer;
215 static char *titles[] = {
225 cfg = stats_tree_get_cfg_by_abbr(abbr);
228 init_strlen = strlen(cfg->pr->stat_dlg->init_string);
230 if (strncmp (optarg, cfg->pr->stat_dlg->init_string, init_strlen) == 0){
231 if (init_strlen == strlen(optarg)) {
232 st = stats_tree_new(cfg,pr,NULL);
234 st = stats_tree_new(cfg,pr,((guint8*)optarg)+init_strlen+1);
238 st = stats_tree_new(cfg,pr,NULL);
241 g_error("no such stats_tree (%s) found in stats_tree registry",abbr);
246 g_error("could not obtain stats_tree abbr from optarg");
249 window_name = g_strdup_printf("%s Stats Tree", cfg->name);
251 st->pr->win = window_new_with_geom(GTK_WINDOW_TOPLEVEL,window_name,window_name);
252 gtk_window_set_default_size(GTK_WINDOW(st->pr->win), 400, 400);
256 title=g_strdup_printf("%s with filter: %s",cfg->name,st->filter);
259 title=g_strdup_printf("%s", cfg->name);
262 gtk_window_set_title(GTK_WINDOW(st->pr->win), title);
265 main_vb = gtk_vbox_new(FALSE, 3);
266 gtk_container_border_width(GTK_CONTAINER(main_vb), 12);
267 gtk_container_add(GTK_CONTAINER(st->pr->win), main_vb);
269 scr_win = scrolled_window_new(NULL, NULL);
271 #if GTK_MAJOR_VERSION >= 2
273 st->pr->store = gtk_tree_store_new (N_COLUMNS, G_TYPE_STRING, G_TYPE_STRING,
274 G_TYPE_STRING, G_TYPE_STRING);
276 st->pr->tree = gtk_tree_view_new_with_model (GTK_TREE_MODEL (st->pr->store));
278 gtk_container_add( GTK_CONTAINER(scr_win), st->pr->tree);
281 renderer = gtk_cell_renderer_text_new ();
282 column = gtk_tree_view_column_new_with_attributes ("Topic / Item", renderer,
283 "text", TITLE_COLUMN,
285 gtk_tree_view_column_set_resizable (column,TRUE);
286 gtk_tree_view_column_set_sizing(column,GTK_TREE_VIEW_COLUMN_AUTOSIZE);
287 gtk_tree_view_append_column (GTK_TREE_VIEW (st->pr->tree), column);
289 renderer = gtk_cell_renderer_text_new ();
290 column = gtk_tree_view_column_new_with_attributes ("Count", renderer,
291 "text", COUNT_COLUMN,
294 gtk_tree_view_column_set_resizable (column,TRUE);
295 gtk_tree_view_column_set_sizing(column,GTK_TREE_VIEW_COLUMN_AUTOSIZE);
296 gtk_tree_view_append_column (GTK_TREE_VIEW (st->pr->tree), column);
298 renderer = gtk_cell_renderer_text_new ();
299 column = gtk_tree_view_column_new_with_attributes ("Rate", renderer,
302 gtk_tree_view_column_set_resizable (column,TRUE);
303 gtk_tree_view_column_set_sizing(column,GTK_TREE_VIEW_COLUMN_AUTOSIZE);
304 gtk_tree_view_append_column (GTK_TREE_VIEW (st->pr->tree), column);
306 renderer = gtk_cell_renderer_text_new ();
307 column = gtk_tree_view_column_new_with_attributes ("Percent", renderer,
308 "text", PERCENT_COLUMN,
310 gtk_tree_view_column_set_resizable(column,TRUE);
311 gtk_tree_view_column_set_sizing(column,GTK_TREE_VIEW_COLUMN_AUTOSIZE);
312 gtk_tree_view_append_column (GTK_TREE_VIEW (st->pr->tree), column);
315 st->pr->ctree = gtk_ctree_new_with_titles (N_COLUMNS, 0, titles);
316 for (i = 0; i < N_COLUMNS; i++) {
318 * XXX - unfortunately, GtkCTree columns can't be
319 * both auto-resizing and resizeable.
321 gtk_clist_set_column_auto_resize(GTK_CLIST(st->pr->ctree), i,
325 gtk_container_add( GTK_CONTAINER(scr_win), st->pr->ctree);
328 gtk_container_add( GTK_CONTAINER(main_vb), scr_win);
330 error_string = register_tap_listener( cfg->tapname,
338 /* error, we failed to attach to the tap. clean up */
339 simple_dialog( ESD_TYPE_ERROR, ESD_BTN_OK, error_string->str );
340 /* destroy_stat_tree_window(st); */
341 g_error("stats_tree for: %s failed to attach to the tap: %s",cfg->name,error_string->str);
342 g_string_free(error_string, TRUE);
346 bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
347 gtk_box_pack_start(GTK_BOX(main_vb), bbox, FALSE, FALSE, 0);
349 bt_close = OBJECT_GET_DATA(bbox, GTK_STOCK_CLOSE);
350 window_set_cancel_button(st->pr->win, bt_close, window_cancel_button_cb);
352 SIGNAL_CONNECT(GTK_WINDOW(st->pr->win), "delete_event", window_delete_event_cb, NULL);
353 SIGNAL_CONNECT(GTK_WINDOW(st->pr->win), "destroy", free_gtk_tree, st);
355 gtk_widget_show_all(st->pr->win);
356 window_present(st->pr->win);
358 #if GTK_MAJOR_VERSION >= 2
359 gtk_tree_view_set_model(GTK_TREE_VIEW(st->pr->tree),GTK_TREE_MODEL(st->pr->store));
364 cf_retap_packets(&cfile);
368 static void register_gtk_stats_tree_tap (gpointer k _U_, gpointer v, gpointer p _U_) {
369 stats_tree_cfg* cfg = v;
372 s = g_strdup_printf("%s,tree",cfg->abbr);
374 register_stat_cmd_arg(s, init_gtk_tree);
376 cfg->pr = g_malloc(sizeof(tree_pres));
378 cfg->pr->stat_dlg = g_malloc(sizeof(tap_dfilter_dlg));
380 cfg->pr->stat_dlg->win_title = g_strdup_printf("%s Stats Tree",cfg->name);
381 cfg->pr->stat_dlg->init_string = g_strdup_printf("%s,tree",cfg->abbr);
382 cfg->pr->stat_dlg->tap_init_cb = init_gtk_tree;
383 cfg->pr->stat_dlg->index = -1;
385 register_stat_menu_item(cfg->name, REGISTER_STAT_GROUP_NONE,
386 gtk_tap_dfilter_dlg_cb, NULL, NULL, cfg->pr->stat_dlg);
389 static void free_tree_presentation(stats_tree* st) {
394 register_tap_listener_stats_tree_stat(void)
397 stats_tree_presentation(register_gtk_stats_tree_tap,
398 setup_gtk_node_pr, NULL,
400 NULL, NULL, free_tree_presentation, NULL, NULL, NULL);