/
gnc-tree-model-account-types.c
517 lines (424 loc) · 15.4 KB
/
gnc-tree-model-account-types.c
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
/*
* gnc-tree-model-account-types.c -- GtkTreeModel implementation
* to display account types in a GtkTreeView.
*
* Copyright (C) 2003 Jan Arne Petersen <jpetersen@uni-bonn.de>
* Copyright (C) 2005, 2006 Chris Shoemaker <c.shoemaker@cox.net>
* Copyright (C) 2006 Eskil Bylund <eskil.bylund@gmail.com>
*
* 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, contact:
*
* Free Software Foundation Voice: +1-617-542-5942
* 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652
* Boston, MA 02110-1301, USA gnu@gnu.org
*/
#include "config.h"
#include <gtk/gtk.h>
#include "qof.h"
#include "gnc-tree-model-account-types.h"
#include "Account.h"
static QofLogModule log_module = GNC_MOD_GUI;
static GtkTreeModel *account_types_tree_model = NULL;
#define TYPE_MASK "type-mask"
/* Functions for the type system */
static void
gnc_tree_model_account_types_class_init (GncTreeModelAccountTypesClass *klass);
static void
gnc_tree_model_account_types_init (GncTreeModelAccountTypes * model);
static void
gnc_tree_model_account_types_finalize (GObject * object);
/* Functions implementing GtkTreeModel */
static void
gnc_tree_model_account_types_tree_model_init (GtkTreeModelIface * iface);
typedef struct GncTreeModelAccountTypesPrivate
{
guint32 selected;
} GncTreeModelAccountTypesPrivate;
#define GNC_TREE_MODEL_ACCOUNT_TYPES_GET_PRIVATE(o) \
(G_TYPE_INSTANCE_GET_PRIVATE ((o), GNC_TYPE_TREE_MODEL_ACCOUNT_TYPES, \
GncTreeModelAccountTypesPrivate))
static GObjectClass *parent_class = NULL;
GType
gnc_tree_model_account_types_get_type (void)
{
static GType gnc_tree_model_account_types_type = 0;
if (gnc_tree_model_account_types_type == 0)
{
static const GTypeInfo our_info =
{
sizeof (GncTreeModelAccountTypesClass),
NULL,
NULL,
(GClassInitFunc) gnc_tree_model_account_types_class_init,
NULL,
NULL,
sizeof (GncTreeModelAccountTypes),
0,
(GInstanceInitFunc) gnc_tree_model_account_types_init
};
static const GInterfaceInfo tree_model_info =
{
(GInterfaceInitFunc) gnc_tree_model_account_types_tree_model_init,
NULL,
NULL
};
gnc_tree_model_account_types_type =
g_type_register_static (G_TYPE_OBJECT,
"GncTreeModelAccountTypes",
&our_info, 0);
g_type_add_interface_static (gnc_tree_model_account_types_type,
GTK_TYPE_TREE_MODEL, &tree_model_info);
}
return gnc_tree_model_account_types_type;
}
static void
gnc_tree_model_account_types_class_init (GncTreeModelAccountTypesClass * klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
parent_class = g_type_class_peek_parent (klass);
object_class->finalize = gnc_tree_model_account_types_finalize;
g_type_class_add_private(klass, sizeof(GncTreeModelAccountTypesPrivate));
}
static void
gnc_tree_model_account_types_init (GncTreeModelAccountTypes * model)
{
while (model->stamp == 0)
{
model->stamp = g_random_int ();
}
}
static void
gnc_tree_model_account_types_finalize (GObject * object)
{
GncTreeModelAccountTypes *model;
GncTreeModelAccountTypesPrivate *priv;
g_return_if_fail (object != NULL);
g_return_if_fail (GNC_IS_TREE_MODEL_ACCOUNT_TYPES (object));
model = GNC_TREE_MODEL_ACCOUNT_TYPES (object);
priv = GNC_TREE_MODEL_ACCOUNT_TYPES_GET_PRIVATE (model);
G_OBJECT_CLASS (parent_class)->finalize (object);
}
GtkTreeModel *
gnc_tree_model_account_types_new (guint32 selected)
{
GncTreeModelAccountTypes *model;
GncTreeModelAccountTypesPrivate *priv;
model = g_object_new (GNC_TYPE_TREE_MODEL_ACCOUNT_TYPES, NULL);
priv = GNC_TREE_MODEL_ACCOUNT_TYPES_GET_PRIVATE(model);
priv->selected = selected;
return GTK_TREE_MODEL (model);
}
static GtkTreeModel *
gnc_tree_model_account_types_master(void)
{
if (!account_types_tree_model)
account_types_tree_model = gnc_tree_model_account_types_new(0);
return account_types_tree_model;
}
static gboolean
gnc_tree_model_account_types_is_valid (GtkTreeModel *model,
GtkTreeIter *iter, gpointer data)
{
GNCAccountType type;
GObject *f_model = G_OBJECT (data);
guint32 valid_types = GPOINTER_TO_UINT (g_object_get_data (
f_model, TYPE_MASK));
gtk_tree_model_get (model, iter,
GNC_TREE_MODEL_ACCOUNT_TYPES_COL_TYPE, &type, -1);
return (valid_types & (1 << type)) ? TRUE : FALSE;
}
GtkTreeModel *
gnc_tree_model_account_types_filter_using_mask (guint32 types)
{
GtkTreeModel *f_model;
f_model = gtk_tree_model_filter_new (gnc_tree_model_account_types_master (),
NULL);
g_object_set_data (G_OBJECT (f_model), TYPE_MASK, GUINT_TO_POINTER (types));
gtk_tree_model_filter_set_visible_func (
GTK_TREE_MODEL_FILTER (f_model), gnc_tree_model_account_types_is_valid,
f_model, NULL);
return f_model;
}
void
gnc_tree_model_account_types_set_mask (GtkTreeModel *f_model,
guint32 types)
{
g_return_if_fail (f_model);
g_object_set_data (G_OBJECT (f_model), TYPE_MASK, GUINT_TO_POINTER (types));
gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (f_model));
}
guint32
gnc_tree_model_account_types_get_mask (GtkTreeModel *f_model)
{
g_return_val_if_fail (f_model, 0);
return GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (f_model), TYPE_MASK));
}
guint32
gnc_tree_model_account_types_get_selection (GtkTreeSelection *sel)
{
GtkTreeModel *f_model, *model;
GtkTreePath *path;
GtkTreeView *view;
GList *list, *node;
guint32 bits = 0;
g_return_val_if_fail(GTK_IS_TREE_SELECTION(sel), 0);
view = gtk_tree_selection_get_tree_view(sel);
g_return_val_if_fail (view, 0);
/* circumvent a bug in gtk+ not always filling f_model */
f_model = NULL;
list = gtk_tree_selection_get_selected_rows(sel, &f_model);
if (!f_model)
f_model = gtk_tree_view_get_model(view);
model = gtk_tree_model_filter_get_model(GTK_TREE_MODEL_FILTER(f_model));
if (model != account_types_tree_model)
PERR("TreeSelection's TreeModel is not the account-types Model");
else
{
for (node = list; node; node = node->next)
{
path = gtk_tree_model_filter_convert_path_to_child_path(
GTK_TREE_MODEL_FILTER(f_model), (GtkTreePath*)node->data);
if (!path || gtk_tree_path_get_depth(path) != 1)
{
PERR("Invalid Account-types TreePath.");
continue;
}
bits |= (1 << gtk_tree_path_get_indices(path)[0]);
}
}
g_list_foreach (list, (GFunc)gtk_tree_path_free, NULL);
g_list_free (list);
return bits;
}
GNCAccountType
gnc_tree_model_account_types_get_selection_single(GtkTreeSelection *sel)
{
gint i;
guint32 selected = gnc_tree_model_account_types_get_selection(sel);
for (i = 0; i < NUM_ACCOUNT_TYPES; i++)
if (selected & (1 << i))
return i;
return ACCT_TYPE_NONE;
}
void
gnc_tree_model_account_types_set_selection (GtkTreeSelection *sel,
guint32 selected)
{
GtkTreePath *path, *f_path;
GtkTreeModelFilter *f_model;
gint i;
GtkTreeView *view;
g_return_if_fail(GTK_IS_TREE_SELECTION(sel));
view = gtk_tree_selection_get_tree_view(sel);
g_return_if_fail (view);
f_model = GTK_TREE_MODEL_FILTER(gtk_tree_view_get_model(view));
g_return_if_fail(gtk_tree_model_filter_get_model(f_model) ==
account_types_tree_model);
gtk_tree_selection_unselect_all(sel);
path = gtk_tree_path_new_first();
for (i = 0; i < NUM_ACCOUNT_TYPES; i++)
{
if (selected & (1 << i))
{
f_path = gtk_tree_model_filter_convert_child_path_to_path(
f_model, path);
gtk_tree_selection_select_path(sel, f_path);
gtk_tree_view_scroll_to_cell(view, f_path, NULL, FALSE, 0.0, 0.0);
}
gtk_tree_path_next(path);
}
gtk_tree_path_free(path);
}
/* Static functions implementing GtkTreeModel */
static GtkTreeModelFlags
gnc_tree_model_account_types_get_flags (GtkTreeModel * tree_model)
{
return GTK_TREE_MODEL_ITERS_PERSIST | GTK_TREE_MODEL_LIST_ONLY;
}
static int
gnc_tree_model_account_types_get_n_columns (GtkTreeModel * tree_model)
{
return GNC_TREE_MODEL_ACCOUNT_TYPES_NUM_COLUMNS;
}
static GType
gnc_tree_model_account_types_get_column_type (GtkTreeModel * tree_model,
int index)
{
g_return_val_if_fail(GNC_IS_TREE_MODEL_ACCOUNT_TYPES (tree_model),
G_TYPE_INVALID);
g_return_val_if_fail((index < GNC_TREE_MODEL_ACCOUNT_TYPES_NUM_COLUMNS)
&& (index >= 0), G_TYPE_INVALID);
switch (index)
{
case GNC_TREE_MODEL_ACCOUNT_TYPES_COL_TYPE:
return G_TYPE_INT;
case GNC_TREE_MODEL_ACCOUNT_TYPES_COL_NAME:
return G_TYPE_STRING;
case GNC_TREE_MODEL_ACCOUNT_TYPES_COL_SELECTED:
return G_TYPE_BOOLEAN;
default:
g_assert_not_reached ();
return G_TYPE_INVALID;
}
}
static gboolean
gnc_tree_model_account_types_get_iter (GtkTreeModel * tree_model,
GtkTreeIter * iter, GtkTreePath * path)
{
GncTreeModelAccountTypes *model = GNC_TREE_MODEL_ACCOUNT_TYPES(tree_model);
gint i;
g_return_val_if_fail (GNC_IS_TREE_MODEL_ACCOUNT_TYPES (model), FALSE);
g_return_val_if_fail (gtk_tree_path_get_depth (path) > 0, FALSE);
i = gtk_tree_path_get_indices (path)[0];
if (i > ACCT_TYPE_NONE && i < NUM_ACCOUNT_TYPES)
{
iter->stamp = model->stamp;
iter->user_data = GINT_TO_POINTER (i);
return TRUE;
}
iter->stamp = 0;
return FALSE;
}
static GtkTreePath *
gnc_tree_model_account_types_get_path (GtkTreeModel * tree_model,
GtkTreeIter * iter)
{
GncTreeModelAccountTypes *model = GNC_TREE_MODEL_ACCOUNT_TYPES(tree_model);
GtkTreePath *path;
g_return_val_if_fail (GNC_IS_TREE_MODEL_ACCOUNT_TYPES (model), NULL);
g_return_val_if_fail (iter != NULL, NULL);
g_return_val_if_fail (iter->stamp == model->stamp, NULL);
path = gtk_tree_path_new ();
gtk_tree_path_append_index (path, GPOINTER_TO_INT (iter->user_data));
return path;
}
static void
gnc_tree_model_account_types_get_value (GtkTreeModel * tree_model,
GtkTreeIter * iter, int column,
GValue * value)
{
GncTreeModelAccountTypes *model = GNC_TREE_MODEL_ACCOUNT_TYPES(tree_model);
GncTreeModelAccountTypesPrivate *priv;
g_return_if_fail (GNC_IS_TREE_MODEL_ACCOUNT_TYPES (model));
g_return_if_fail (iter != NULL);
g_return_if_fail (iter->stamp == model->stamp);
priv = GNC_TREE_MODEL_ACCOUNT_TYPES_GET_PRIVATE(model);
switch (column)
{
case GNC_TREE_MODEL_ACCOUNT_TYPES_COL_TYPE:
g_value_init (value, G_TYPE_INT);
g_value_set_int (value, GPOINTER_TO_INT (iter->user_data));
break;
case GNC_TREE_MODEL_ACCOUNT_TYPES_COL_NAME:
g_value_init (value, G_TYPE_STRING);
g_value_set_string (value, xaccAccountGetTypeStr (
GPOINTER_TO_INT (iter->user_data)));
break;
case GNC_TREE_MODEL_ACCOUNT_TYPES_COL_SELECTED:
g_value_init (value, G_TYPE_BOOLEAN);
g_value_set_boolean (value, priv->selected &
(1 << GPOINTER_TO_INT (iter->user_data)));
break;
default:
g_assert_not_reached ();
}
}
static gboolean
gnc_tree_model_account_types_iter_next (GtkTreeModel * tree_model,
GtkTreeIter * iter)
{
GncTreeModelAccountTypes *model = GNC_TREE_MODEL_ACCOUNT_TYPES(tree_model);
g_return_val_if_fail (GNC_IS_TREE_MODEL_ACCOUNT_TYPES (model), FALSE);
g_return_val_if_fail (iter != NULL, FALSE);
g_return_val_if_fail (iter->stamp == model->stamp, FALSE);
if (GPOINTER_TO_INT (iter->user_data) < NUM_ACCOUNT_TYPES - 1)
{
iter->user_data = GINT_TO_POINTER(
GPOINTER_TO_INT(iter->user_data) + 1);
return TRUE;
}
iter->stamp = 0;
return FALSE;
}
static gboolean
gnc_tree_model_account_types_iter_children (GtkTreeModel * tree_model,
GtkTreeIter * iter,
GtkTreeIter * parent)
{
g_return_val_if_fail (GNC_IS_TREE_MODEL_ACCOUNT_TYPES(tree_model), FALSE);
if (parent != NULL)
return FALSE;
iter->stamp = GNC_TREE_MODEL_ACCOUNT_TYPES (tree_model)->stamp;
iter->user_data = GINT_TO_POINTER (0);
return TRUE;
}
static gboolean
gnc_tree_model_account_types_iter_has_child (GtkTreeModel * tree_model,
GtkTreeIter * iter)
{
return FALSE;
}
static int
gnc_tree_model_account_types_iter_n_children (GtkTreeModel * tree_model,
GtkTreeIter * iter)
{
g_return_val_if_fail (GNC_IS_TREE_MODEL_ACCOUNT_TYPES (tree_model), -1);
if (iter == NULL)
return NUM_ACCOUNT_TYPES;
g_return_val_if_fail (
GNC_TREE_MODEL_ACCOUNT_TYPES (tree_model)->stamp == iter->stamp, -1);
return 0;
}
static gboolean
gnc_tree_model_account_types_iter_nth_child (GtkTreeModel * tree_model,
GtkTreeIter * iter,
GtkTreeIter * parent, int n)
{
GncTreeModelAccountTypes *model;
g_return_val_if_fail (GNC_IS_TREE_MODEL_ACCOUNT_TYPES (tree_model), FALSE);
if (parent != NULL)
return FALSE;
model = GNC_TREE_MODEL_ACCOUNT_TYPES (tree_model);
if (n > ACCT_TYPE_NONE && n < NUM_ACCOUNT_TYPES)
{
iter->stamp = model->stamp;
iter->user_data = GINT_TO_POINTER (n);
return TRUE;
}
iter->stamp = 0;
return FALSE;
}
static gboolean
gnc_tree_model_account_types_iter_parent (GtkTreeModel * tree_model,
GtkTreeIter * iter,
GtkTreeIter * child)
{
return FALSE;
}
static void
gnc_tree_model_account_types_tree_model_init (GtkTreeModelIface * iface)
{
iface->get_flags = gnc_tree_model_account_types_get_flags;
iface->get_n_columns = gnc_tree_model_account_types_get_n_columns;
iface->get_column_type = gnc_tree_model_account_types_get_column_type;
iface->get_iter = gnc_tree_model_account_types_get_iter;
iface->get_path = gnc_tree_model_account_types_get_path;
iface->get_value = gnc_tree_model_account_types_get_value;
iface->iter_next = gnc_tree_model_account_types_iter_next;
iface->iter_children = gnc_tree_model_account_types_iter_children;
iface->iter_has_child = gnc_tree_model_account_types_iter_has_child;
iface->iter_n_children = gnc_tree_model_account_types_iter_n_children;
iface->iter_nth_child = gnc_tree_model_account_types_iter_nth_child;
iface->iter_parent = gnc_tree_model_account_types_iter_parent;
}