Simplify the ciode a bit.
[obnox/wireshark/wip.git] / gtk / gui_stat_util.c
1 /* gui_stat_util.c
2  * gui functions used by stats
3  * Copyright 2003 Lars Roland
4  *
5  * $Id$
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
9  * Copyright 1998 Gerald Combs
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 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <stdio.h>
31
32 #ifdef HAVE_SYS_TYPES_H
33 # include <sys/types.h>
34 #endif
35 #include <string.h>
36
37 #include <gtk/gtk.h>
38
39 #include "../simple_dialog.h"
40 #include "../file.h"
41
42 #include "gtk/gui_stat_util.h"
43
44
45 /* insert a string into a GTK_TABLE at column x and row y*/
46 #if 0
47 /* Statistic table */
48 typedef struct _gtk_table {
49         GtkWidget *widget;  /**< the table widget */
50         int height;         /**< the height */
51         int width;          /**< the width */
52 }gtk_table;
53
54 void
55 add_table_entry(gtk_table *tab, const char *str, int x, int y)
56 {
57         GtkWidget *tmp;
58
59         if(y>=tab->height){
60                 tab->height=y+1;
61                 gtk_table_resize(GTK_TABLE(tab->widget), tab->height, tab->width);
62         }
63         if(x>=tab->width){
64                 tab->width=x+1;
65                 gtk_table_resize(GTK_TABLE(tab->widget), tab->height, tab->width);
66         }
67
68         tmp=gtk_label_new(str);
69         gtk_table_attach_defaults(GTK_TABLE(tab->widget), tmp, x, x+1, y, y+1);
70         gtk_label_set_justify(GTK_LABEL(tmp), GTK_JUSTIFY_LEFT);
71         gtk_widget_show(tmp);
72 }
73 #endif
74
75 /* init a main window for stats, set title and display used filter in window */
76
77 void
78 init_main_stat_window(GtkWidget *window, GtkWidget *mainbox, const char *title, const char *filter)
79 {
80         GtkWidget *main_label;
81         GtkWidget *filter_label;
82         char *filter_string;
83
84
85         gtk_window_set_title(GTK_WINDOW(window), title);
86
87         gtk_container_add(GTK_CONTAINER(window), mainbox);
88         gtk_container_set_border_width(GTK_CONTAINER(mainbox), 10);
89         gtk_widget_show(mainbox);
90
91         main_label=gtk_label_new(title);
92         gtk_box_pack_start(GTK_BOX(mainbox), main_label, FALSE, FALSE, 0);
93         gtk_widget_show(main_label);
94
95         filter_string = g_strdup_printf("Filter: %s", filter ? filter : "");
96         filter_label=gtk_label_new(filter_string);
97         g_free(filter_string);
98         gtk_label_set_line_wrap(GTK_LABEL(filter_label), TRUE);
99         gtk_box_pack_start(GTK_BOX(mainbox), filter_label, FALSE, FALSE, 0);
100         gtk_widget_show(filter_label);
101
102 }
103
104 /* create a table, using a scrollable GtkTreeView */
105
106 GtkTreeView *
107 create_stat_table(GtkWidget *scrolled_window, GtkWidget *vbox, int columns, const stat_column *headers)
108 {
109         GtkTreeView *table;
110         GtkListStore *store;
111         GtkWidget *tree;
112         GtkTreeViewColumn *column;
113         GtkTreeSelection  *sel;
114         GtkCellRenderer *renderer;
115         GType *types;
116         int i;
117
118         if (columns <= 0)
119                 return NULL;
120
121         types = g_malloc(columns *sizeof(GType));
122         for (i = 0; i < columns; i++)
123                 types[i] = headers[i].type;
124
125         store = gtk_list_store_newv (columns, types);
126         g_free(types);
127
128         /* create table */
129         tree = gtk_tree_view_new_with_model (GTK_TREE_MODEL (store));
130         table = GTK_TREE_VIEW(tree);
131         g_object_unref (G_OBJECT (store));
132
133         for (i = 0; i < columns; i++) {
134                 renderer = gtk_cell_renderer_text_new ();
135                 if (headers[i].align == RIGHT) {
136                         /* right align */
137                         g_object_set(G_OBJECT(renderer), "xalign", 1.0, NULL);
138                 }
139                 g_object_set(renderer, "ypad", 0, NULL);
140                 column = gtk_tree_view_column_new_with_attributes (headers[i].title, renderer, "text",
141                                         i, NULL);
142                 gtk_tree_view_column_set_resizable(column, TRUE);
143                 gtk_tree_view_append_column (table, column);
144         }
145         gtk_container_add(GTK_CONTAINER(scrolled_window), GTK_WIDGET (table));
146         gtk_box_pack_start(GTK_BOX(vbox), scrolled_window, TRUE, TRUE, 0);
147
148         /* configure TreeView */
149         gtk_tree_view_set_rules_hint(table, FALSE);
150         gtk_tree_view_set_headers_clickable(table, FALSE);
151
152         sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(table));
153         gtk_tree_selection_set_mode(sel, GTK_SELECTION_SINGLE);
154
155         gtk_widget_show(scrolled_window);
156
157         return table;
158 }
159