We always HAVE_CONFIG_H so don't bother checking whether we have it or not.
[metze/wireshark/wip.git] / ui / 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24  */
25
26 #include "config.h"
27
28 #include <gtk/gtk.h>
29
30 #include "ui/simple_dialog.h"
31 #include "../file.h"
32
33 #include "ui/gtk/gui_stat_util.h"
34
35 /* init a main window for stats, set title and display used filter in window */
36
37 void
38 init_main_stat_window(GtkWidget *window, GtkWidget *mainbox, const char *title, const char *filter)
39 {
40         GtkWidget *main_label;
41         GtkWidget *filter_label;
42         char      *filter_string;
43
44         gtk_window_set_title(GTK_WINDOW(window), title);
45
46         gtk_container_add(GTK_CONTAINER(window), mainbox);
47         gtk_container_set_border_width(GTK_CONTAINER(mainbox), 10);
48         gtk_widget_show(mainbox);
49
50         main_label = gtk_label_new(title);
51         gtk_box_pack_start(GTK_BOX(mainbox), main_label, FALSE, FALSE, 0);
52         gtk_widget_show(main_label);
53
54         filter_string = g_strdup_printf("Filter: %s", filter ? filter : "");
55         filter_label  = gtk_label_new(filter_string);
56         g_free(filter_string);
57         gtk_label_set_line_wrap(GTK_LABEL(filter_label), TRUE);
58         gtk_box_pack_start(GTK_BOX(mainbox), filter_label, FALSE, FALSE, 0);
59         gtk_widget_show(filter_label);
60 }
61
62 /* create a table, using a scrollable GtkTreeView */
63
64 GtkTreeView *
65 create_stat_table(GtkWidget *scrolled_window, GtkWidget *vbox, int columns, const stat_column *headers)
66 {
67         GtkTreeView       *table;
68         GtkListStore      *store;
69         GtkWidget         *tree;
70         GtkTreeViewColumn *column;
71         GtkTreeSelection  *sel;
72         GtkCellRenderer   *renderer;
73         GType             *types;
74         int                i;
75
76         if (columns <= 0)
77                 return NULL;
78
79         types = g_malloc(columns *sizeof(GType));
80         for (i = 0; i < columns; i++)
81                 types[i] = headers[i].type;
82
83         store = gtk_list_store_newv (columns, types);
84         g_free(types);
85
86         /* create table */
87         tree  = gtk_tree_view_new_with_model (GTK_TREE_MODEL (store));
88         table = GTK_TREE_VIEW(tree);
89         g_object_unref (G_OBJECT (store));
90
91         for (i = 0; i < columns; i++) {
92                 renderer = gtk_cell_renderer_text_new ();
93                 if (headers[i].align == RIGHT) {
94                         /* right align */
95                         g_object_set(G_OBJECT(renderer), "xalign", 1.0, NULL);
96                 }
97                 g_object_set(renderer, "ypad", 0, NULL);
98                 column = gtk_tree_view_column_new_with_attributes (headers[i].title, renderer, "text",
99                                         i, NULL);
100                 gtk_tree_view_column_set_resizable(column, TRUE);
101                 gtk_tree_view_append_column (table, column);
102         }
103         gtk_container_add(GTK_CONTAINER(scrolled_window), GTK_WIDGET (table));
104         gtk_box_pack_start(GTK_BOX(vbox), scrolled_window, TRUE, TRUE, 0);
105
106         /* configure TreeView */
107         gtk_tree_view_set_rules_hint(table, FALSE);
108         gtk_tree_view_set_headers_clickable(table, FALSE);
109
110         sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(table));
111         gtk_tree_selection_set_mode(sel, GTK_SELECTION_SINGLE);
112
113         gtk_widget_show(scrolled_window);
114
115         return table;
116 }