]> Dogcows Code - chaz/homebank/blob - src/list_account.c
Merge branch 'upstream'
[chaz/homebank] / src / list_account.c
1 /* HomeBank -- Free, easy, personal accounting for everyone.
2 * Copyright (C) 1995-2017 Maxime DOYEN
3 *
4 * This file is part of HomeBank.
5 *
6 * HomeBank is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * HomeBank is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20
21 #include "homebank.h"
22
23 #include "list_account.h"
24
25 /****************************************************************************/
26 /* Debug macros */
27 /****************************************************************************/
28 #define MYDEBUG 0
29
30 #if MYDEBUG
31 #define DB(x) (x);
32 #else
33 #define DB(x);
34 #endif
35
36 /* our global datas */
37 extern struct HomeBank *GLOBALS;
38 extern struct Preferences *PREFS;
39
40 extern gchar *CYA_ACC_TYPE[]; //in ui_account.c
41
42 /*
43 ** draw some icons according to the stored data structure
44 */
45 static void
46 status_cell_data_function (GtkTreeViewColumn *col,
47 GtkCellRenderer *renderer,
48 GtkTreeModel *model,
49 GtkTreeIter *iter,
50 gpointer user_data)
51 {
52 Account *acc;
53 gchar *iconname = NULL;
54 gint dt;
55
56 gtk_tree_model_get(model, iter,
57 LST_DSPACC_DATATYPE, &dt,
58 LST_DSPACC_DATAS, &acc,
59 -1);
60
61 if( dt == DSPACC_TYPE_NORMAL )
62 {
63
64 switch(GPOINTER_TO_INT(user_data))
65 {
66 case 1:
67 iconname = (acc->flags & AF_ADDED) ? ICONNAME_NEW : NULL;
68 break;
69 case 2:
70 iconname = (acc->flags & AF_CHANGED) ? ICONNAME_HB_OPE_EDIT : NULL;
71 break;
72 }
73 }
74
75 g_object_set(renderer, "icon-name", iconname, NULL);
76 }
77
78 /*
79 ** draw some text from the stored data structure
80 */
81 /*
82 static void
83 acc_type_cell_data_function (GtkTreeViewColumn *col, GtkCellRenderer *renderer, GtkTreeModel *model, GtkTreeIter *iter, gpointer user_data)
84 {
85 Account *acc;
86 gint dt;
87
88 gtk_tree_model_get(model, iter,
89 LST_DSPACC_DATATYPE, &dt,
90 LST_DSPACC_DATAS, &acc,
91 -1);
92
93 if( dt == DSPACC_TYPE_NORMAL && acc->type > 0 )
94 {
95 g_object_set(renderer, "text", _(CYA_ACC_TYPE[acc->type]), NULL);
96 }
97 else
98 g_object_set(renderer, "text", NULL, NULL);
99 }
100 */
101
102 /*
103 ** draw some text from the stored data structure
104 */
105 static void
106 text_cell_data_function (GtkTreeViewColumn *col,
107 GtkCellRenderer *renderer,
108 GtkTreeModel *model,
109 GtkTreeIter *iter,
110 gpointer user_data)
111 {
112 Account *acc;
113 gint dt;
114 gchar *groupname;
115
116 gtk_tree_model_get(model, iter,
117 LST_DSPACC_DATATYPE, &dt,
118 LST_DSPACC_DATAS, &acc,
119 LST_DSPACC_NAME, &groupname,
120 -1);
121
122 if( dt == DSPACC_TYPE_NORMAL )
123 {
124 switch(GPOINTER_TO_INT(user_data))
125 {
126 case 1:
127 g_object_set(renderer, "weight", PANGO_WEIGHT_NORMAL, "text", acc->name, NULL);
128 break;
129 case 2:
130 //g_object_set(renderer, "text", acc->number, NULL);
131 break;
132
133 }
134 }
135 else
136 g_object_set(renderer,
137 "weight", PANGO_WEIGHT_BOLD,
138 "text", groupname,
139 NULL);
140 }
141
142
143 static void
144 float_cell_data_function (GtkTreeViewColumn *col, GtkCellRenderer *renderer, GtkTreeModel *model, GtkTreeIter *iter, gpointer user_data)
145 {
146 gdouble value;
147 gchar buf[G_ASCII_DTOSTR_BUF_SIZE];
148 Account *acc;
149 gint dt;
150 gint weight;
151 gchar *color;
152
153 gtk_tree_model_get(model, iter,
154 LST_DSPACC_DATATYPE, &dt,
155 LST_DSPACC_DATAS, &acc,
156 GPOINTER_TO_INT(user_data), &value, //LST_DSPACC_(BANK/TODAY/FUTURE)
157 -1);
158
159 guint32 kcur = (acc != NULL) ? acc->kcur : GLOBALS->kcur;
160
161 if( dt == DSPACC_TYPE_HEADER )
162 {
163 gboolean expanded;
164 GtkTreePath* tp;
165
166 tp = gtk_tree_model_get_path(model, iter);
167 expanded = gtk_tree_view_row_expanded(GTK_TREE_VIEW(gtk_tree_view_column_get_tree_view(col)), tp);
168
169 if(!expanded)
170 {
171 hb_strfmon(buf, G_ASCII_DTOSTR_BUF_SIZE-1, value, kcur, GLOBALS->minor);
172 color = get_normal_color_amount(value);
173 g_object_set(renderer,
174 "weight", PANGO_WEIGHT_NORMAL,
175 "foreground", color,
176 "text", buf,
177 NULL);
178 }
179 else
180 g_object_set(renderer, "text", NULL,
181 NULL);
182
183 }
184 else
185 {
186 hb_strfmon(buf, G_ASCII_DTOSTR_BUF_SIZE-1, value, kcur, GLOBALS->minor);
187
188 color = NULL;
189 weight = PANGO_WEIGHT_NORMAL;
190
191 if( dt == DSPACC_TYPE_NORMAL )
192 {
193 color = get_minimum_color_amount(value, acc->minimum);
194 }
195 else
196 {
197 color = get_normal_color_amount(value);
198 weight = PANGO_WEIGHT_BOLD;
199 }
200
201 //g_print("value='%.2f' buf='%s' color='%s'\n", value, buf, color);
202
203 g_object_set(renderer,
204 "weight", weight,
205 "foreground", color,
206 "text", buf,
207 NULL);
208
209 }
210 }
211
212
213 static GtkTreeViewColumn *amount_list_account_column(gchar *name, gint id)
214 {
215 GtkTreeViewColumn *column;
216 GtkCellRenderer *renderer;
217
218 column = gtk_tree_view_column_new();
219 gtk_tree_view_column_set_title(column, name);
220
221 renderer = gtk_cell_renderer_text_new ();
222 g_object_set(renderer, "xalign", 1.0, NULL);
223 gtk_tree_view_column_pack_start(column, renderer, TRUE);
224 gtk_tree_view_column_set_cell_data_func(column, renderer, float_cell_data_function, GINT_TO_POINTER(id), NULL);
225 gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
226 gtk_tree_view_column_set_alignment (column, 0.5);
227 gtk_tree_view_column_set_spacing( column, 16 );
228 //gtk_tree_view_column_set_sort_column_id (column, LST_DSPACC_BANK);
229
230 return column;
231 }
232
233 static gint
234 list_account_compare_func (GtkTreeModel *model, GtkTreeIter *a, GtkTreeIter *b, gpointer userdata)
235 {
236 gint retval = 0;
237 gint dt1, dt2;
238 Account *entry1, *entry2;
239 gchar *name1, *name2;
240
241 gtk_tree_model_get(model, a,
242 LST_DSPACC_DATATYPE, &dt1,
243 LST_DSPACC_DATAS, &entry1,
244 LST_DSPACC_NAME, &name1,
245 -1);
246 gtk_tree_model_get(model, b,
247 LST_DSPACC_DATATYPE, &dt2,
248 LST_DSPACC_DATAS, &entry2,
249 LST_DSPACC_NAME, &name2,
250 -1);
251
252 if( dt1 == DSPACC_TYPE_NORMAL && dt2 == DSPACC_TYPE_NORMAL )
253 {
254 retval = entry1->pos - entry2->pos;
255 }
256 else
257 if( dt1 == DSPACC_TYPE_HEADER && dt2 == DSPACC_TYPE_HEADER )
258 {
259 retval = hb_string_utf8_compare(name1, name2);
260 }
261
262 g_free(name2);
263 g_free(name1);
264
265 return retval;
266 }
267
268
269 /*
270 *
271 */
272 static
273 gboolean list_account_selectionfunc(
274 GtkTreeSelection *selection, GtkTreeModel *model, GtkTreePath *path, gboolean path_currently_selected, gpointer data)
275 {
276 GtkTreeIter iter;
277
278 if( gtk_tree_path_get_depth( path ) < 2 )
279 return FALSE;
280
281 if(gtk_tree_model_get_iter(model, &iter, path))
282 {
283 gint dt;
284
285 gtk_tree_model_get(model, &iter,
286 LST_DSPACC_DATATYPE, &dt,
287 -1);
288
289 if( dt != DSPACC_TYPE_NORMAL )
290 return FALSE;
291 }
292
293 return TRUE;
294 }
295
296
297 static void list_account_destroy(GtkTreeView *treeview, gpointer user_data)
298 {
299 GtkTreeViewColumn *column;
300
301 //todo: unsafe to use direct column index
302 column = gtk_tree_view_get_column(treeview, LST_DSPACC_NAME);
303 if( column )
304 {
305 PREFS->pnl_acc_col_acc_width = gtk_tree_view_column_get_width(column);
306 }
307 }
308
309
310 GtkWidget *create_list_account(void)
311 {
312 GtkTreeStore *store;
313 GtkWidget *view;
314 GtkCellRenderer *renderer;
315 GtkTreeViewColumn *column;
316
317 /* create list store */
318 store = gtk_tree_store_new(
319 NUM_LST_DSPACC,
320 G_TYPE_INT, /* fake: status */
321 G_TYPE_STRING, /* fake: name */
322 G_TYPE_DOUBLE,
323 G_TYPE_DOUBLE,
324 G_TYPE_DOUBLE,
325 G_TYPE_POINTER, /* datas*/
326 G_TYPE_INT /* datatype */
327 );
328
329 //treeview
330 view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
331 g_object_unref(store);
332
333 gtk_tree_view_set_grid_lines (GTK_TREE_VIEW (view), PREFS->grid_lines);
334 //gtk_tree_view_set_search_column (GTK_TREE_VIEW (treeview),
335 // COLUMN_DESCRIPTION);
336
337 //gtk_tree_view_set_enable_tree_lines(GTK_TREE_VIEW (view), TRUE);
338
339 gtk_tree_selection_set_mode(gtk_tree_view_get_selection(GTK_TREE_VIEW(view)), GTK_SELECTION_SINGLE);
340
341 /* Status */
342 column = gtk_tree_view_column_new();
343 //gtk_tree_view_column_set_title(column, _("Status"));
344
345 renderer = gtk_cell_renderer_pixbuf_new ();
346 gtk_tree_view_column_pack_start(column, renderer, TRUE);
347 gtk_tree_view_column_set_cell_data_func(column, renderer, status_cell_data_function, GINT_TO_POINTER(1), NULL);
348
349 renderer = gtk_cell_renderer_pixbuf_new ();
350 gtk_tree_view_column_pack_start(column, renderer, TRUE);
351 gtk_tree_view_column_set_cell_data_func(column, renderer, status_cell_data_function, GINT_TO_POINTER(2), NULL);
352
353 gtk_tree_view_column_set_alignment (column, 0.5);
354 gtk_tree_view_append_column (GTK_TREE_VIEW(view), column);
355
356 /* Account */
357 renderer = gtk_cell_renderer_text_new ();
358 g_object_set(renderer,
359 "ellipsize", PANGO_ELLIPSIZE_END,
360 "ellipsize-set", TRUE,
361 NULL);
362
363 column = gtk_tree_view_column_new();
364 gtk_tree_view_column_set_title(column, _("Accounts"));
365 gtk_tree_view_column_pack_start(column, renderer, TRUE);
366 gtk_tree_view_column_set_cell_data_func(column, renderer, text_cell_data_function, GINT_TO_POINTER(LST_DSPACC_NAME), NULL);
367 gtk_tree_view_column_set_alignment (column, 0.5);
368 gtk_tree_view_column_set_min_width(column, HB_MINWIDTH_LIST/2);
369 gtk_tree_view_column_set_resizable(column, TRUE);
370 gtk_tree_view_append_column (GTK_TREE_VIEW(view), column);
371
372 gtk_tree_view_column_set_fixed_width(column, PREFS->pnl_acc_col_acc_width);
373 gtk_tree_view_set_expander_column(GTK_TREE_VIEW (view), column);
374
375 /* Bank */
376 column = amount_list_account_column(_("Bank"), LST_DSPACC_BANK);
377 gtk_tree_view_append_column (GTK_TREE_VIEW(view), column);
378
379 /* Today */
380 column = amount_list_account_column(_("Today"), LST_DSPACC_TODAY);
381 gtk_tree_view_append_column (GTK_TREE_VIEW(view), column);
382
383 /* Future */
384 column = amount_list_account_column(_("Future"), LST_DSPACC_FUTURE);
385 gtk_tree_view_append_column (GTK_TREE_VIEW(view), column);
386
387 /* column 7: empty */
388 column = gtk_tree_view_column_new();
389 gtk_tree_view_append_column (GTK_TREE_VIEW(view), column);
390
391
392 /* disable selection for level 1 of the tree */
393
394 gtk_tree_selection_set_select_function(gtk_tree_view_get_selection(GTK_TREE_VIEW(view)), list_account_selectionfunc, NULL, NULL);
395
396 //sort etc
397 gtk_tree_sortable_set_default_sort_func(GTK_TREE_SORTABLE(store), list_account_compare_func, NULL, NULL);
398 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(store), GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID, GTK_SORT_ASCENDING);
399
400 g_signal_connect (view, "destroy", G_CALLBACK (list_account_destroy), NULL);
401
402 return(view);
403 }
404
This page took 0.057022 seconds and 4 git commands to generate.