summaryrefslogtreecommitdiff
path: root/treeviewutils.c
blob: 786cc05d2392315f7099e1c53fe0938d98218cf3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/* MemProf -- memory profiler and leak detector
 * Copyright 2002, Soeren Sandmann (sandmann@daimi.au.dk)
 * Copyright 2003, 2004, Red Hat, Inc.
 *
 * Sysprof -- Sampling, systemwide CPU profiler 
 * Copyright 2004, 2005, Soeren Sandmann
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
/*====*/

#include "treeviewutils.h"

static void on_column_clicked (GtkTreeViewColumn *column,
			       gpointer data);
typedef struct
{
    int		model_column;
    GtkSortType	default_order;
} SortInfo;

static void
set_sort_info (GtkTreeView *view,
	       GtkTreeViewColumn *column,
	       int model_column,
	       GtkSortType default_order)
{
    SortInfo *info;
    
    info = g_new0 (SortInfo, 1);
    info->model_column = model_column;
    info->default_order = default_order;
    
    gtk_tree_view_column_set_clickable (column, TRUE);
    
    g_object_set_data (G_OBJECT (column), "column_info", info);
    g_signal_connect (column, "clicked", G_CALLBACK (on_column_clicked), view);
}

static SortInfo *
get_sort_info (GtkTreeViewColumn *column)
{
    return g_object_get_data (G_OBJECT (column), "column_info");
}

static GtkTreeViewColumn *
find_column_by_model_column (GtkTreeView *view, int model_column)
{
    GList *columns = gtk_tree_view_get_columns (view);
    GList *list;
    GtkTreeViewColumn *result = NULL;
    
    for (list = columns; list != NULL; list = list->next)
    {
	GtkTreeViewColumn *column = list->data;
	SortInfo *info = get_sort_info (column);
	
	if (info->model_column == model_column)
	    result = column;
    }
    
    g_list_free (columns);
    
    return result;
}

void
tree_view_set_sort_column (GtkTreeView *view,
			   int model_column,
			   int sort_type)
{
    GList *columns, *list;
    GtkTreeSortable *sortable;
    GtkTreeViewColumn *column = find_column_by_model_column (view, model_column);
    SortInfo *info = get_sort_info (column);
    
    /* For each column in the view, unset the sort indicator */
    columns = gtk_tree_view_get_columns (view);
    for (list = columns; list != NULL; list = list->next)
    {
	GtkTreeViewColumn *col = GTK_TREE_VIEW_COLUMN (list->data);
	
	gtk_tree_view_column_set_sort_indicator (col, FALSE);
    }
    
    /* Set the sort indicator for this column */
    gtk_tree_view_column_set_sort_indicator (column, TRUE);
    gtk_tree_view_column_set_sort_order (column, sort_type);
    
    /* And sort the column */
    sortable = GTK_TREE_SORTABLE (gtk_tree_view_get_model (view));
    
    gtk_tree_sortable_set_sort_column_id (sortable,
					  info->model_column,
					  sort_type);
}

static void
on_column_clicked (GtkTreeViewColumn *column,
		   gpointer data)
{
    GtkTreeView *view = data;
    GtkSortType sort_type;
    SortInfo *info = get_sort_info (column);
    
    /* Find out what our current sort indicator is. If it is NONE, then
     * select the default for the column, otherwise, select the opposite
     */
    if (!gtk_tree_view_column_get_sort_indicator (column))
    {
	sort_type = info->default_order;
    }
    else
    {
	if (gtk_tree_view_column_get_sort_order (column) == GTK_SORT_ASCENDING)
	    sort_type = GTK_SORT_DESCENDING;
	else
	    sort_type = GTK_SORT_ASCENDING;
    }
    
    tree_view_set_sort_column (view, info->model_column, sort_type);
}

GtkTreeViewColumn *
add_plain_text_column (GtkTreeView *view, const gchar *title, gint model_column)
{
    GtkCellRenderer *renderer;
    GtkTreeViewColumn *column;
    
    renderer = gtk_cell_renderer_text_new ();
    g_object_set (renderer, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
    column = gtk_tree_view_column_new_with_attributes (title, renderer,
						       "text", model_column,
						       NULL);
    gtk_tree_view_column_set_resizable (column, TRUE);
    gtk_tree_view_append_column (view, column);
    
    set_sort_info (view, column, model_column, GTK_SORT_ASCENDING);
    
    return column;
}

static void
pointer_to_text (GtkTreeViewColumn *tree_column,
		 GtkCellRenderer *cell, GtkTreeModel *tree_model,
		 GtkTreeIter *iter, gpointer data)
{
    gpointer p;
    gchar *text;
    int column = GPOINTER_TO_INT (data);
    
    gtk_tree_model_get (tree_model, iter, column, &p, -1);
    text = g_strdup_printf ("%p", p);
    g_object_set (cell, "text", text, NULL);
    g_free (text);
}

typedef struct {
    int column;
    char *format;
} ColumnInfo;

static void
double_to_text (GtkTreeViewColumn *tree_column,
		GtkCellRenderer *cell, GtkTreeModel *tree_model,
		GtkTreeIter *iter, gpointer data)
{
    gdouble d;
    gchar *text;
    ColumnInfo *info = data;
    
    gtk_tree_model_get (tree_model, iter, info->column, &d, -1);
    
    text = g_strdup_printf (info->format, d);
    
    g_object_set (cell, "text", text, NULL);
    g_free (text);
}

static void
free_column_info (void *data)
{
    ColumnInfo *info = data;
    g_free (info->format);
    g_free (info);
}

GtkTreeViewColumn *
add_double_format_column (GtkTreeView *view,
			  const gchar *title,
			  gint         model_column,
			  const char  *format)
{
    GtkCellRenderer *renderer;
    GtkTreeViewColumn *column;
    ColumnInfo *column_info = g_new (ColumnInfo, 1);
    
    renderer = gtk_cell_renderer_text_new ();
    g_object_set (renderer, "xalign", 1.0, NULL);
    
    column = gtk_tree_view_column_new ();
    gtk_tree_view_column_set_title  (column, title);
    gtk_tree_view_column_pack_start (column, renderer, TRUE);
    gtk_tree_view_column_set_resizable (column, FALSE);
    
    column_info->column = model_column;
    column_info->format = g_strdup (format);
    
    gtk_tree_view_column_set_cell_data_func (
	column, renderer, double_to_text, column_info, free_column_info);
    
    gtk_tree_view_append_column (view, column);
    
    set_sort_info (view, column, model_column, GTK_SORT_DESCENDING);
    
    return column;
}

GtkTreeViewColumn *
add_pointer_column (GtkTreeView *view, const gchar *title, gint model_column)
{
    GtkCellRenderer *renderer;
    GtkTreeViewColumn *column;
    
    renderer = gtk_cell_renderer_text_new ();
    
    column = gtk_tree_view_column_new ();
    if (title)
	gtk_tree_view_column_set_title  (column, title);
    gtk_tree_view_column_pack_start (column, renderer, TRUE);
    gtk_tree_view_column_set_resizable (column, TRUE);
    gtk_tree_view_column_set_cell_data_func (
	column, renderer, pointer_to_text, GINT_TO_POINTER (model_column), NULL);
    
    gtk_tree_view_append_column (view, column);
    
    return column;
}

void
tree_view_set_model_with_default_sort (GtkTreeView  *view,
				       GtkTreeModel *model,
				       int           model_column,
				       GtkSortType   default_sort)
{
    gboolean	     was_sorted = FALSE;
    int		     old_column;
    GtkSortType	     old_type;
    GtkTreeSortable *old_model;
    GtkAdjustment   *adjustment;
    
    old_model = GTK_TREE_SORTABLE (gtk_tree_view_get_model (view));
    
    if (old_model)
    {
	was_sorted = gtk_tree_sortable_get_sort_column_id (
	    GTK_TREE_SORTABLE (old_model), &old_column, &old_type);
    }
    
    gtk_tree_view_set_model (view, model);
    
    if (was_sorted)
	tree_view_set_sort_column (view, old_column, old_type);
    else
	tree_view_set_sort_column (view, model_column, default_sort);
    
    /* Workaround for GTK+ crack, see bug 405625 */
    adjustment = gtk_tree_view_get_vadjustment (view);
    if (adjustment)
	gtk_adjustment_set_value (adjustment, 0);
}

static void
process_iter (GtkTreeView      *view,
	      GtkTreeIter      *iter,
	      VisibleCallback   callback,
	      gpointer          data)
{
    GtkTreeModel *model = gtk_tree_view_get_model (view);
    GtkTreePath *path;
    GtkTreeIter child;
    
    path = gtk_tree_model_get_path (model, iter);
    
    callback (view, path, iter, data);
    
    if (gtk_tree_view_row_expanded (view, path))
    {
	if (gtk_tree_model_iter_children (model, &child, iter))
	{
	    do
	    {
		process_iter (view, &child, callback, data);
	    }
	    while (gtk_tree_model_iter_next (model, &child));
	}
    }
    
    gtk_tree_path_free (path);
}

void
tree_view_foreach_visible (GtkTreeView *view,
			   VisibleCallback callback,
			   gpointer data)
{
    GtkTreeModel *model = gtk_tree_view_get_model (view);
    GtkTreeIter iter;
    
    if (model && gtk_tree_model_get_iter_first (model, &iter))
	process_iter (view, &iter, callback, data);
}