po: Run make update-po to update the .po files
[memprof.git] / src / treeviewutils.c
blobc2a1a9d11517051ff02094b4b1f24e29e9812e44
1 /* -*- mode: C; c-file-style: "linux" -*- */
3 /* MemProf -- memory profiler and leak detector
4 * Copyright 2002, Soeren Sandmann (sandmann@daimi.au.dk)
5 * Copyright 2003, Red Hat, Inc.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 /*====*/
23 #include "treeviewutils.h"
25 void
26 column_set_sort_id (GtkTreeViewColumn *column,
27 int column_id)
29 g_object_set_data (G_OBJECT (column),
30 "mi-saved-sort-column", GINT_TO_POINTER (column_id));
31 gtk_tree_view_column_set_sort_column_id (column, column_id);
34 void
35 tree_view_unset_sort_ids (GtkTreeView *tree_view)
37 GList *columns = gtk_tree_view_get_columns (tree_view);
38 GList *l;
40 for (l = columns; l; l = l->next) {
41 gtk_tree_view_column_set_sort_column_id (l->data, -1);
44 g_list_free (columns);
47 void
48 tree_view_set_sort_ids (GtkTreeView *tree_view)
50 GList *columns = gtk_tree_view_get_columns (tree_view);
51 GList *l;
53 for (l = columns; l; l = l->next) {
54 int column_id = GPOINTER_TO_INT (g_object_get_data (l->data, "mi-saved-sort-column"));
55 gtk_tree_view_column_set_sort_column_id (l->data, column_id);
58 g_list_free (columns);
61 int
62 list_iter_get_index (GtkTreeModel *model,
63 GtkTreeIter *iter)
65 GtkTreePath *path = gtk_tree_model_get_path (model,iter);
66 int result;
68 g_assert (path);
69 g_assert (gtk_tree_path_get_depth (path) == 1);
70 result = gtk_tree_path_get_indices (path)[0];
71 gtk_tree_path_free (path);
73 return result;
77 GtkTreeViewColumn *
78 add_plain_text_column (GtkTreeView *view, const gchar *title, gint model_column)
80 GtkCellRenderer *renderer;
81 GtkTreeViewColumn *column;
83 renderer = gtk_cell_renderer_text_new ();
84 g_object_set (renderer, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
85 column = gtk_tree_view_column_new_with_attributes (title, renderer,
86 "text", model_column,
87 NULL);
88 gtk_tree_view_column_set_resizable (column, TRUE);
89 gtk_tree_view_append_column (view, column);
90 column_set_sort_id (column, model_column);
92 return column;
95 static void
96 pointer_to_text (GtkTreeViewColumn *tree_column,
97 GtkCellRenderer *cell, GtkTreeModel *tree_model,
98 GtkTreeIter *iter, gpointer data)
100 gpointer p;
101 gchar *text;
102 int column = GPOINTER_TO_INT (data);
104 gtk_tree_model_get (tree_model, iter, column, &p, -1);
105 text = g_strdup_printf ("%p", p);
106 g_object_set (cell, "text", text, NULL);
107 g_free (text);
110 typedef struct {
111 int column;
112 char *format;
113 } ColumnInfo;
115 static void
116 double_to_text (GtkTreeViewColumn *tree_column,
117 GtkCellRenderer *cell, GtkTreeModel *tree_model,
118 GtkTreeIter *iter, gpointer data)
120 gdouble d;
121 gchar *text;
122 ColumnInfo *info = data;
124 gtk_tree_model_get (tree_model, iter, info->column, &d, -1);
126 text = g_strdup_printf (info->format, d);
128 g_object_set (cell, "text", text, NULL);
129 g_free (text);
132 static void
133 free_column_info (void *data)
135 ColumnInfo *info = data;
136 g_free (info->format);
137 g_free (info);
140 GtkTreeViewColumn *
141 add_double_format_column (GtkTreeView *view, const gchar *title, gint model_column, const char *format)
143 GtkCellRenderer *renderer;
144 GtkTreeViewColumn *column;
145 ColumnInfo *column_info = g_new (ColumnInfo, 1);
147 renderer = gtk_cell_renderer_text_new ();
149 column = gtk_tree_view_column_new ();
150 gtk_tree_view_column_set_title (column, title);
151 gtk_tree_view_column_pack_start (column, renderer, TRUE);
152 gtk_tree_view_column_set_resizable (column, FALSE);
154 column_info->column = model_column;
155 column_info->format = g_strdup (format);
157 gtk_tree_view_column_set_cell_data_func (column, renderer,
158 double_to_text, column_info, free_column_info);
160 gtk_tree_view_append_column (view, column);
161 column_set_sort_id (column, model_column);
163 return column;
166 GtkTreeViewColumn *
167 add_pointer_column (GtkTreeView *view, const gchar *title, gint model_column)
169 GtkCellRenderer *renderer;
170 GtkTreeViewColumn *column;
172 renderer = gtk_cell_renderer_text_new ();
173 g_object_set (renderer, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
175 column = gtk_tree_view_column_new ();
176 if (title)
177 gtk_tree_view_column_set_title (column, title);
178 gtk_tree_view_column_pack_start (column, renderer, TRUE);
179 gtk_tree_view_column_set_resizable (column, TRUE);
180 gtk_tree_view_column_set_cell_data_func (column, renderer,
181 pointer_to_text, GINT_TO_POINTER (model_column), NULL);
183 gtk_tree_view_append_column (view, column);
184 column_set_sort_id (column, model_column);
186 return column;