add gitignore
[chaz/homebank] / src / list_account.c
1 /* HomeBank -- Free, easy, personal accounting for everyone.
2 * Copyright (C) 1995-2014 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 gint dt;
54
55 gtk_tree_model_get(model, iter,
56 LST_DSPACC_DATATYPE, &dt,
57 LST_DSPACC_DATAS, &acc,
58 -1);
59
60 if( dt == DSPACC_TYPE_NORMAL )
61 {
62
63 switch(GPOINTER_TO_INT(user_data))
64 {
65 case 1:
66 g_object_set(renderer, "pixbuf", (acc->flags & AF_ADDED) ? GLOBALS->lst_pixbuf[LST_PIXBUF_ADD] : NULL, NULL);
67 break;
68 case 2:
69 g_object_set(renderer, "pixbuf", (acc->flags & AF_CHANGED) ? GLOBALS->lst_pixbuf[LST_PIXBUF_EDIT] : NULL, NULL);
70 break;
71 }
72 }
73 else
74 g_object_set(renderer, "pixbuf", NULL, NULL);
75 }
76
77 /*
78 ** draw some text from the stored data structure
79 */
80 /*
81 static void
82 acc_type_cell_data_function (GtkTreeViewColumn *col, GtkCellRenderer *renderer, GtkTreeModel *model, GtkTreeIter *iter, gpointer user_data)
83 {
84 Account *acc;
85 gint dt;
86
87 gtk_tree_model_get(model, iter,
88 LST_DSPACC_DATATYPE, &dt,
89 LST_DSPACC_DATAS, &acc,
90 -1);
91
92 if( dt == DSPACC_TYPE_NORMAL && acc->type > 0 )
93 {
94 g_object_set(renderer, "text", _(CYA_ACC_TYPE[acc->type]), NULL);
95 }
96 else
97 g_object_set(renderer, "text", NULL, NULL);
98 }
99 */
100
101 /*
102 ** draw some text from the stored data structure
103 */
104 static void
105 text_cell_data_function (GtkTreeViewColumn *col,
106 GtkCellRenderer *renderer,
107 GtkTreeModel *model,
108 GtkTreeIter *iter,
109 gpointer user_data)
110 {
111 Account *acc;
112 gint dt;
113 gchar *groupname;
114
115 gtk_tree_model_get(model, iter,
116 LST_DSPACC_DATATYPE, &dt,
117 LST_DSPACC_DATAS, &acc,
118 LST_DSPACC_NAME, &groupname,
119 -1);
120
121 if( dt == DSPACC_TYPE_NORMAL )
122 {
123 switch(GPOINTER_TO_INT(user_data))
124 {
125 case 1:
126 g_object_set(renderer, "weight", PANGO_WEIGHT_NORMAL, "text", acc->name, NULL);
127 break;
128 case 2:
129 //g_object_set(renderer, "text", acc->number, NULL);
130 break;
131
132 }
133 }
134 else
135 g_object_set(renderer,
136 "weight", PANGO_WEIGHT_BOLD,
137 "text", groupname,
138 NULL);
139 }
140
141 static void
142 float_cell_data_function (GtkTreeViewColumn *col, GtkCellRenderer *renderer, GtkTreeModel *model, GtkTreeIter *iter, gpointer user_data)
143 {
144 gdouble value;
145 gchar buf[G_ASCII_DTOSTR_BUF_SIZE];
146 Account *acc;
147 gint dt;
148 gint weight;
149 gchar *color;
150
151 gtk_tree_model_get(model, iter,
152 LST_DSPACC_DATATYPE, &dt,
153 LST_DSPACC_DATAS, &acc,
154 GPOINTER_TO_INT(user_data), &value, //LST_DSPACC_(BANK/TODAY/FUTURE)
155 -1);
156
157 if( dt == DSPACC_TYPE_HEADER )
158 {
159 gboolean expanded;
160 GtkTreePath* tp;
161
162 tp = gtk_tree_model_get_path(model, iter);
163 expanded = gtk_tree_view_row_expanded(GTK_TREE_VIEW(gtk_tree_view_column_get_tree_view(col)), tp);
164
165 if(!expanded)
166 {
167 //hb_strfmon(buf, G_ASCII_DTOSTR_BUF_SIZE-1, value, GLOBALS->kcur);
168 mystrfmon(buf, G_ASCII_DTOSTR_BUF_SIZE-1, value, GLOBALS->minor);
169 color = get_normal_color_amount(value);
170 g_object_set(renderer,
171 "weight", PANGO_WEIGHT_NORMAL,
172 "foreground", color,
173 "text", buf,
174 NULL);
175 }
176 else
177 g_object_set(renderer, "text", NULL,
178 NULL);
179
180
181 }
182 else
183 {
184 // prevent errors
185 //kcur = acc != NULL ? acc->kcur : GLOBALS->kcur;
186
187 //original
188 //mystrfmon(buf, G_ASCII_DTOSTR_BUF_SIZE-1, value, GLOBALS->minor);
189
190 //base test
191 /*
192 if(kcur != GLOBALS->kcur)
193 {
194 gdouble newval = amount_base_amount(value, kcur);
195 //g_print("currency=%d :: value='%.2f' newvalue='%.2f'\n", kcur, value, newval);
196 value = newval;
197 mystrfmoncurr(buf, G_ASCII_DTOSTR_BUF_SIZE-1, value, GLOBALS->kcur);
198 }
199 else*/
200
201 //hb_strfmon(buf, G_ASCII_DTOSTR_BUF_SIZE-1, value, kcur);
202 mystrfmon(buf, G_ASCII_DTOSTR_BUF_SIZE-1, value, GLOBALS->minor);
203
204 color = NULL;
205 weight = PANGO_WEIGHT_NORMAL;
206
207 if( dt == DSPACC_TYPE_NORMAL )
208 {
209 color = get_minimum_color_amount(value, acc->minimum);
210 }
211 else
212 {
213 color = get_normal_color_amount(value);
214 weight = PANGO_WEIGHT_BOLD;
215 }
216
217 //g_print("value='%.2f' buf='%s' color='%s'\n", value, buf, color);
218
219 g_object_set(renderer,
220 "weight", weight,
221 "foreground", color,
222 "text", buf,
223 NULL);
224
225 }
226 }
227
228
229 static GtkTreeViewColumn *amount_list_account_column(gchar *name, gint id)
230 {
231 GtkTreeViewColumn *column;
232 GtkCellRenderer *renderer;
233
234 column = gtk_tree_view_column_new();
235 gtk_tree_view_column_set_title(column, name);
236
237 renderer = gtk_cell_renderer_text_new ();
238 g_object_set(renderer, "xalign", 1.0, NULL);
239 gtk_tree_view_column_pack_start(column, renderer, TRUE);
240 gtk_tree_view_column_set_cell_data_func(column, renderer, float_cell_data_function, GINT_TO_POINTER(id), NULL);
241 //gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
242 gtk_tree_view_column_set_resizable(column, TRUE);
243 gtk_tree_view_column_set_alignment (column, 0.5);
244 gtk_tree_view_column_set_spacing( column, 16 );
245 //gtk_tree_view_column_set_sort_column_id (column, LST_DSPACC_BANK);
246
247 return column;
248 }
249
250 static gint
251 list_account_compare_func (GtkTreeModel *model, GtkTreeIter *a, GtkTreeIter *b, gpointer userdata)
252 {
253 gint retval = 0;
254 gint dt1, dt2;
255 Account *entry1, *entry2;
256 //gchar *name1, *name2;
257
258 gtk_tree_model_get(model, a,
259 LST_DSPACC_DATATYPE, &dt1,
260 LST_DSPACC_DATAS, &entry1,
261 //LST_DSPACC_NAME, &name1,
262 -1);
263 gtk_tree_model_get(model, b,
264 LST_DSPACC_DATATYPE, &dt2,
265 LST_DSPACC_DATAS, &entry2,
266 //LST_DSPACC_NAME, &name2,
267 -1);
268
269 if( dt1 == DSPACC_TYPE_NORMAL && dt2 == DSPACC_TYPE_NORMAL )
270 {
271 retval = entry1->pos - entry2->pos;
272 }
273
274 return retval;
275 }
276
277
278 /*
279 *
280 */
281 static
282 gboolean list_account_selectionfunc(
283 GtkTreeSelection *selection, GtkTreeModel *model, GtkTreePath *path, gboolean path_currently_selected, gpointer data)
284 {
285 GtkTreeIter iter;
286
287 if( gtk_tree_path_get_depth( path ) < 2 )
288 return FALSE;
289
290 if(gtk_tree_model_get_iter(model, &iter, path))
291 {
292 gint dt;
293
294 gtk_tree_model_get(model, &iter,
295 LST_DSPACC_DATATYPE, &dt,
296 -1);
297
298 if( dt != DSPACC_TYPE_NORMAL )
299 return FALSE;
300 }
301
302 return TRUE;
303 }
304
305
306 GtkWidget *create_list_account(void)
307 {
308 GtkTreeStore *store;
309 GtkWidget *view;
310 GtkCellRenderer *renderer;
311 GtkTreeViewColumn *column;
312
313 /* create list store */
314 store = gtk_tree_store_new(
315 NUM_LST_DSPACC,
316 G_TYPE_POINTER,
317 G_TYPE_INT, /* datatype */
318 G_TYPE_INT, /* fake: status */
319 G_TYPE_STRING, /* fake: name */
320 G_TYPE_DOUBLE,
321 G_TYPE_DOUBLE,
322 G_TYPE_DOUBLE
323 );
324
325 //treeview
326 view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
327 g_object_unref(store);
328
329 gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (view), PREFS->rules_hint);
330 //gtk_tree_view_set_search_column (GTK_TREE_VIEW (treeview),
331 // COLUMN_DESCRIPTION);
332
333 gtk_tree_selection_set_mode(gtk_tree_view_get_selection(GTK_TREE_VIEW(view)), GTK_SELECTION_SINGLE);
334
335 /* Status */
336 column = gtk_tree_view_column_new();
337 gtk_tree_view_column_set_title(column, _("Status"));
338
339 renderer = gtk_cell_renderer_pixbuf_new ();
340 gtk_cell_renderer_set_fixed_size(renderer, GLOBALS->lst_pixbuf_maxwidth, -1);
341 gtk_tree_view_column_pack_start(column, renderer, TRUE);
342 gtk_tree_view_column_set_cell_data_func(column, renderer, status_cell_data_function, GINT_TO_POINTER(1), NULL);
343
344 renderer = gtk_cell_renderer_pixbuf_new ();
345 gtk_cell_renderer_set_fixed_size(renderer, GLOBALS->lst_pixbuf_maxwidth, -1);
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(2), NULL);
348
349 gtk_tree_view_column_set_alignment (column, 0.5);
350 gtk_tree_view_append_column (GTK_TREE_VIEW(view), column);
351
352 /* Account */
353 column = gtk_tree_view_column_new();
354 gtk_tree_view_column_set_title(column, _("Accounts"));
355
356 renderer = gtk_cell_renderer_text_new ();
357 gtk_tree_view_column_pack_start(column, renderer, TRUE);
358 gtk_tree_view_column_set_cell_data_func(column, renderer, text_cell_data_function, GINT_TO_POINTER(1), NULL);
359 gtk_tree_view_column_set_resizable(column, TRUE);
360 gtk_tree_view_column_set_alignment (column, 0.5);
361
362 gtk_tree_view_append_column (GTK_TREE_VIEW(view), column);
363
364 gtk_tree_view_set_expander_column(GTK_TREE_VIEW (view), column);
365
366
367
368
369
370 /* Bank */
371 column = amount_list_account_column(_("Bank"), LST_DSPACC_BANK);
372 gtk_tree_view_append_column (GTK_TREE_VIEW(view), column);
373
374
375 /* Today */
376 column = amount_list_account_column(_("Today"), LST_DSPACC_TODAY);
377 gtk_tree_view_append_column (GTK_TREE_VIEW(view), column);
378
379
380 /* Future */
381 column = amount_list_account_column(_("Future"), LST_DSPACC_FUTURE);
382 gtk_tree_view_append_column (GTK_TREE_VIEW(view), column);
383
384 /* column 7: empty */
385 column = gtk_tree_view_column_new();
386 gtk_tree_view_append_column (GTK_TREE_VIEW(view), column);
387
388
389 /* disable selection for level 1 of the tree */
390
391 gtk_tree_selection_set_select_function(gtk_tree_view_get_selection(GTK_TREE_VIEW(view)), list_account_selectionfunc, NULL, NULL);
392
393 //sort etc
394 gtk_tree_sortable_set_default_sort_func(GTK_TREE_SORTABLE(store), list_account_compare_func, NULL, NULL);
395 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(store), GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID, GTK_SORT_ASCENDING);
396
397
398 return(view);
399 }
400
This page took 0.05807 seconds and 4 git commands to generate.