add gitignore
[chaz/homebank] / src / rep_time.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 "rep_time.h"
24
25 #include "list_operation.h"
26 #include "gtk-chart.h"
27 #include "gtk-dateentry.h"
28
29 #include "dsp_mainwindow.h"
30 #include "ui-account.h"
31 #include "ui-payee.h"
32 #include "ui-category.h"
33 #include "ui-filter.h"
34
35 /****************************************************************************/
36 /* Debug macros */
37 /****************************************************************************/
38 #define MYDEBUG 0
39
40 #if MYDEBUG
41 #define DB(x) (x);
42 #else
43 #define DB(x);
44 #endif
45
46 /* our global datas */
47 extern struct HomeBank *GLOBALS;
48 extern struct Preferences *PREFS;
49
50
51 /* prototypes */
52 static void ui_reptime_action_viewlist(GtkAction *action, gpointer user_data);
53 static void ui_reptime_action_viewline(GtkAction *action, gpointer user_data);
54 static void ui_reptime_action_detail(GtkAction *action, gpointer user_data);
55 //static void ui_reptime_action_filter(GtkAction *action, gpointer user_data);
56 static void ui_reptime_action_refresh(GtkAction *action, gpointer user_data);
57 static void ui_reptime_action_export(GtkAction *action, gpointer user_data);
58
59 //static void ui_reptime_list_set_cur(GtkTreeView *treeview, guint32 kcur);
60
61
62 static GtkActionEntry entries[] = {
63 { "List" , "hb-view-list" , N_("List") , NULL, N_("View results as list"), G_CALLBACK (ui_reptime_action_viewlist) },
64 { "Line" , "hb-view-line" , N_("Line") , NULL, N_("View results as lines"), G_CALLBACK (ui_reptime_action_viewline) },
65
66 // { "Filter" , "hb-filter" , N_("Filter") , NULL, N_("Edit the filter"), G_CALLBACK (ui_reptime_action_filter) },
67 { "Refresh" , GTK_STOCK_REFRESH , N_("Refresh"), NULL, N_("Refresh results"), G_CALLBACK (ui_reptime_action_refresh) },
68
69 { "Export" , "hb-file-export", N_("Export") , NULL, N_("Export as CSV"), G_CALLBACK (ui_reptime_action_export) },
70 };
71 static guint n_entries = G_N_ELEMENTS (entries);
72
73 static GtkToggleActionEntry toggle_entries[] = {
74 { "Detail", "hb-ope-show", /* name, stock id */
75 N_("Detail"), NULL, /* label, accelerator */
76 N_("Toggle detail"), /* tooltip */
77 G_CALLBACK (ui_reptime_action_detail),
78 FALSE }, /* is_active */
79
80 };
81 static guint n_toggle_entries = G_N_ELEMENTS (toggle_entries);
82
83
84 static const gchar *ui_info =
85 "<ui>"
86 " <toolbar name='ToolBar'>"
87 " <toolitem action='List'/>"
88 " <toolitem action='Line'/>"
89 " <separator/>"
90 " <toolitem action='Detail'/>"
91 " <separator/>"
92 //" <toolitem action='Filter'/>"
93 " <toolitem action='Refresh'/>"
94 " <separator/>"
95 " <toolitem action='Export'/>"
96 " </toolbar>"
97 "</ui>";
98
99
100 static void ui_reptime_date_change(GtkWidget *widget, gpointer user_data);
101 static void ui_reptime_range_change(GtkWidget *widget, gpointer user_data);
102 static void ui_reptime_detail(GtkWidget *widget, gpointer user_data);
103 static void ui_reptime_update(GtkWidget *widget, gpointer user_data);
104 static void ui_reptime_export_csv(GtkWidget *widget, gpointer user_data);
105 static void ui_reptime_compute(GtkWidget *widget, gpointer user_data);
106 static void ui_reptime_sensitive(GtkWidget *widget, gpointer user_data);
107 static void ui_reptime_toggle_detail(GtkWidget *widget, gpointer user_data);
108 static void ui_reptime_toggle_minor(GtkWidget *widget, gpointer user_data);
109 static void ui_reptime_update_daterange(GtkWidget *widget, gpointer user_data);
110 static GtkWidget *ui_list_reptime_create(void);
111
112 static gint ui_list_reptime_compare_func (GtkTreeModel *model, GtkTreeIter *a, GtkTreeIter *b, gpointer userdata);
113
114
115
116 gchar *CYA_TIMESELECT[] = { N_("Account"), N_("Category"), N_("Payee"), NULL };
117
118
119
120 gchar *CYA_VIEWBY[] = { N_("Day"), N_("Week"), N_("Month"), N_("Quarter"), N_("Year"), NULL };
121
122 //extern gchar *CYA_FLT_SELECT[];
123
124 gchar *CYA_ABMONTHS[] =
125 {
126 NULL,
127 N_("Jan"),
128 N_("Feb"),
129 N_("Mar"),
130 N_("Apr"),
131 N_("May"),
132 N_("Jun"),
133 N_("Jul"),
134 N_("Aug"),
135 N_("Sep"),
136 N_("Oct"),
137 N_("Nov"),
138 N_("Dec"),
139 };
140
141 /* action functions -------------------- */
142
143 static void ui_reptime_action_viewlist(GtkAction *action, gpointer user_data)
144 {
145 struct ui_reptime_data *data = user_data;
146
147 gtk_notebook_set_current_page(GTK_NOTEBOOK(data->GR_result), 0);
148 ui_reptime_sensitive(data->window, NULL);
149 }
150
151
152
153
154
155
156
157
158
159
160 static void ui_reptime_action_viewline(GtkAction *action, gpointer user_data)
161 {
162 struct ui_reptime_data *data = user_data;
163
164 gtk_notebook_set_current_page(GTK_NOTEBOOK(data->GR_result), 1);
165 ui_reptime_sensitive(data->window, NULL);
166
167 }
168
169
170 static void ui_reptime_action_detail(GtkAction *action, gpointer user_data)
171 {
172 struct ui_reptime_data *data = user_data;
173
174 ui_reptime_toggle_detail(data->window, NULL);
175 }
176
177 /*
178 static void ui_reptime_action_filter(GtkAction *action, gpointer user_data)
179 {
180 struct ui_reptime_data *data = user_data;
181
182 //debug
183 //create_deffilter_window(data->filter, TRUE);
184
185 if(create_deffilter_window(data->filter, TRUE) != GTK_RESPONSE_REJECT)
186 ui_reptime_compute(data->window, NULL);
187 }
188 */
189
190 static void ui_reptime_action_refresh(GtkAction *action, gpointer user_data)
191 {
192 struct ui_reptime_data *data = user_data;
193
194 ui_reptime_compute(data->window, NULL);
195 }
196
197 static void ui_reptime_action_export(GtkAction *action, gpointer user_data)
198 {
199 struct ui_reptime_data *data = user_data;
200
201 ui_reptime_export_csv(data->window, NULL);
202 }
203
204
205
206 /* ======================== */
207
208
209
210 /*
211 ** ============================================================================
212 */
213
214
215
216
217 /*
218 ** return the month list position correponding to the passed date
219 */
220 static gint DateInMonth(guint32 from, guint32 opedate)
221 {
222 GDate *date1, *date2;
223 gint pos;
224
225 //debug
226 // this return sometimes -1, -2 which is wrong
227
228 date1 = g_date_new_julian(from);
229 date2 = g_date_new_julian(opedate);
230
231 pos = ((g_date_get_year(date2) - g_date_get_year(date1)) * 12) + g_date_get_month(date2) - g_date_get_month(date1);
232
233 //g_print(" from=%d-%d ope=%d-%d => %d\n", g_date_get_month(date1), g_date_get_year(date1), g_date_get_month(date2), g_date_get_year(date2), pos);
234
235 g_date_free(date2);
236 g_date_free(date1);
237
238 return(pos);
239 }
240
241 static gint DateInQuarter(guint32 from, guint32 opedate)
242 {
243 GDate *date1, *date2;
244 gint pos;
245
246 //debug
247 // this return sometimes -1, -2 which is wrong
248
249 date1 = g_date_new_julian(from);
250 date2 = g_date_new_julian(opedate);
251
252 pos = (((g_date_get_year(date2) - g_date_get_year(date1)) * 12) + g_date_get_month(date2) - g_date_get_month(date1))/3;
253
254 DB( g_print(" from=%d-%d ope=%d-%d => %d\n", g_date_get_month(date1), g_date_get_year(date1), g_date_get_month(date2), g_date_get_year(date2), pos) );
255
256 g_date_free(date2);
257 g_date_free(date1);
258
259 return(pos);
260 }
261 /*
262 ** return the year list position correponding to the passed date
263 */
264 static gint DateInYear(guint32 from, guint32 opedate)
265 {
266 GDate *date;
267 gint year_from, year_ope, pos;
268
269 date = g_date_new_julian(from);
270 year_from = g_date_get_year(date);
271 g_date_set_julian(date, opedate);
272 year_ope = g_date_get_year(date);
273 g_date_free(date);
274
275 pos = year_ope - year_from;
276
277 //g_print(" from=%d ope=%d => %d\n", year_from, year_ope, pos);
278
279 return(pos);
280 }
281
282 static void ui_reptime_date_change(GtkWidget *widget, gpointer user_data)
283 {
284 struct ui_reptime_data *data;
285
286 DB( g_print("\n[reptime] date change\n") );
287
288 data = g_object_get_data(G_OBJECT(gtk_widget_get_ancestor(widget, GTK_TYPE_WINDOW)), "inst_data");
289
290 data->filter->mindate = gtk_dateentry_get_date(GTK_DATE_ENTRY(data->PO_mindate));
291 data->filter->maxdate = gtk_dateentry_get_date(GTK_DATE_ENTRY(data->PO_maxdate));
292
293 // set min/max date for both widget
294 gtk_dateentry_set_maxdate(GTK_DATE_ENTRY(data->PO_mindate), data->filter->maxdate);
295 gtk_dateentry_set_mindate(GTK_DATE_ENTRY(data->PO_maxdate), data->filter->mindate);
296
297 g_signal_handler_block(data->CY_range, data->handler_id[HID_REPTIME_RANGE]);
298 gtk_combo_box_set_active(GTK_COMBO_BOX(data->CY_range), FLT_RANGE_OTHER);
299 g_signal_handler_unblock(data->CY_range, data->handler_id[HID_REPTIME_RANGE]);
300
301 ui_reptime_compute(widget, NULL);
302 ui_reptime_update_daterange(widget, NULL);
303
304 }
305
306
307
308 static void ui_reptime_range_change(GtkWidget *widget, gpointer user_data)
309 {
310 struct ui_reptime_data *data;
311 gint range;
312
313 DB( g_print("\n[reptime] range change\n") );
314
315 data = g_object_get_data(G_OBJECT(gtk_widget_get_ancestor(widget, GTK_TYPE_WINDOW)), "inst_data");
316
317 range = gtk_combo_box_get_active(GTK_COMBO_BOX(data->CY_range));
318
319 if(range != FLT_RANGE_OTHER)
320 {
321 filter_preset_daterange_set(data->filter, range);
322
323 g_signal_handler_block(data->PO_mindate, data->handler_id[HID_REPTIME_MINDATE]);
324 g_signal_handler_block(data->PO_maxdate, data->handler_id[HID_REPTIME_MAXDATE]);
325
326 gtk_dateentry_set_date(GTK_DATE_ENTRY(data->PO_mindate), data->filter->mindate);
327 gtk_dateentry_set_date(GTK_DATE_ENTRY(data->PO_maxdate), data->filter->maxdate);
328
329 g_signal_handler_unblock(data->PO_mindate, data->handler_id[HID_REPTIME_MINDATE]);
330 g_signal_handler_unblock(data->PO_maxdate, data->handler_id[HID_REPTIME_MAXDATE]);
331
332 ui_reptime_compute(widget, NULL);
333 ui_reptime_update_daterange(widget, NULL);
334 }
335
336 }
337
338 static void ui_reptime_update_daterange(GtkWidget *widget, gpointer user_data)
339 {
340 struct ui_reptime_data *data;
341 gchar *daterange;
342
343 DB( g_print("\n[reptime] update daterange\n") );
344
345 data = g_object_get_data(G_OBJECT(gtk_widget_get_ancestor(widget, GTK_TYPE_WINDOW)), "inst_data");
346
347 daterange = filter_daterange_text_get(data->filter);
348 gtk_label_set_markup(GTK_LABEL(data->TX_daterange), daterange);
349 g_free(daterange);
350 }
351
352
353 static void ui_reptime_detail(GtkWidget *widget, gpointer user_data)
354 {
355 struct ui_reptime_data *data;
356 guint active = GPOINTER_TO_INT(user_data);
357 guint tmpfor, tmpslice;
358 gboolean showall;
359 guint32 from, to;
360 guint i;
361 GList *list;
362 GtkTreeModel *model;
363 GtkTreeIter iter;
364 guint32 selkey;
365
366 data = g_object_get_data(G_OBJECT(gtk_widget_get_ancestor(widget, GTK_TYPE_WINDOW)), "inst_data");
367
368 DB( g_print("\n[reptime] detail\n") );
369
370 tmpfor = gtk_combo_box_get_active(GTK_COMBO_BOX(data->CY_for));
371 tmpslice = gtk_combo_box_get_active(GTK_COMBO_BOX(data->CY_view));
372 showall = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(data->CM_all));
373 selkey = 0;
374
375 switch(tmpfor)
376 {
377 case FOR_REPTIME_ACCOUNT:
378 selkey = ui_acc_comboboxentry_get_key(GTK_COMBO_BOX(data->PO_acc));
379 break;
380 case FOR_REPTIME_CATEGORY:
381 selkey = ui_cat_comboboxentry_get_key(GTK_COMBO_BOX(data->PO_cat));
382 break;
383 case FOR_REPTIME_PAYEE:
384 selkey = ui_pay_comboboxentry_get_key(GTK_COMBO_BOX(data->PO_pay));
385 break;
386 }
387
388 //DB( g_print(" for=%d, view by=%d :: key=%d\n", tmpfor, tmpslice, selkey) );
389
390 //get our min max date
391 from = data->filter->mindate;
392 to = data->filter->maxdate;
393
394 /* clear and detach our model */
395 model = gtk_tree_view_get_model(GTK_TREE_VIEW(data->LV_detail));
396 gtk_list_store_clear (GTK_LIST_STORE(model));
397
398 if(data->detail)
399 {
400 g_object_ref(model); /* Make sure the model stays with us after the tree view unrefs it */
401 gtk_tree_view_set_model(GTK_TREE_VIEW(data->LV_detail), NULL); /* Detach model from view */
402
403 /* fill in the model */
404 list = g_list_first(GLOBALS->ope_list);
405 while (list != NULL)
406 {
407 Transaction *ope = list->data;
408 Account *acc;
409
410 //DB( g_print(" get %s\n", ope->ope_Word) );
411
412 acc = da_acc_get(ope->kacc);
413 if(acc == NULL) goto next1;
414 if((acc->flags & (AF_CLOSED|AF_NOREPORT))) goto next1;
415
416 //filter here
417 if( !(ope->flags & OF_REMIND) && ope->date >= from && ope->date <= to)
418 {
419 guint32 pos = 0;
420 gboolean include = FALSE;
421
422 switch(tmpfor)
423 {
424 case FOR_REPTIME_ACCOUNT:
425 if( selkey == ope->kacc )
426 include = TRUE;
427 break;
428 case FOR_REPTIME_CATEGORY:
429 {
430 Category *catentry;
431
432 if( ope->flags & OF_SPLIT )
433 {
434 guint nbsplit = da_transaction_splits_count(ope);
435 Split *split;
436
437 for(i=0;i<nbsplit;i++)
438 {
439 split = ope->splits[i];
440 catentry = da_cat_get(split->kcat);
441 if(catentry != NULL) //#1340142
442 {
443 if( selkey == catentry->parent || selkey == catentry->key )
444 include = TRUE;
445
446 }
447 }
448 }
449 else
450 {
451 catentry = da_cat_get(ope->kcat);
452 if(catentry != NULL) //#1340142
453 {
454 if( selkey == catentry->parent || selkey == catentry->key )
455 include = TRUE;
456
457 }
458 }
459 }
460 break;
461 case FOR_REPTIME_PAYEE:
462 if( selkey == ope->kpay )
463 include = TRUE;
464 break;
465 }
466
467 if( include == TRUE || showall == TRUE )
468 {
469
470 switch(tmpslice)
471 {
472 case GROUPBY_REPTIME_DAY:
473 pos = ope->date - from;
474 break;
475
476 case GROUPBY_REPTIME_WEEK:
477 pos = (ope->date - from)/7;
478 break;
479
480 case GROUPBY_REPTIME_MONTH:
481 pos = DateInMonth(from, ope->date);
482 break;
483
484 case GROUPBY_REPTIME_QUARTER:
485 pos = DateInQuarter(from, ope->date);
486 break;
487
488 case GROUPBY_REPTIME_YEAR:
489 pos = DateInYear(from, ope->date);
490 break;
491 }
492
493 DB( g_print("** pos=%d\n", pos) );
494
495 //insert
496 if( pos == active )
497 {
498
499 gtk_list_store_append (GTK_LIST_STORE(model), &iter);
500 gtk_list_store_set (GTK_LIST_STORE(model), &iter,
501 LST_DSPOPE_DATAS, ope,
502 -1);
503 }
504 }
505
506
507 }
508 next1:
509 list = g_list_next(list);
510 }
511
512 /* Re-attach model to view */
513 gtk_tree_view_set_model(GTK_TREE_VIEW(data->LV_detail), model);
514 g_object_unref(model);
515
516 }
517
518 }
519
520
521 static void ui_reptime_update(GtkWidget *widget, gpointer user_data)
522 {
523 struct ui_reptime_data *data;
524 GtkTreeModel *model;
525 gint tmpfor;
526 gchar *title;
527 //gboolean xval;
528
529 DB( g_print("\n[reptime] update\n") );
530
531 data = g_object_get_data(G_OBJECT(gtk_widget_get_ancestor(widget, GTK_TYPE_WINDOW)), "inst_data");
532
533
534 model = gtk_tree_view_get_model(GTK_TREE_VIEW(data->LV_report));
535 //byamount = 0;
536 tmpfor = gtk_combo_box_get_active(GTK_COMBO_BOX(data->CY_for));
537 //tmpslice = gtk_combo_box_get_active(GTK_COMBO_BOX(data->CY_view));
538
539 // ensure not exp & inc for piechart
540 //page = gtk_notebook_get_current_page(GTK_NOTEBOOK(data->GR_result));
541
542 //DB( g_print(" page %d\n\n", page) );
543 //DB( g_print(" tmpslice %d\n\n", tmpslice) );
544
545
546 //column = LST_REPTIME_POS;
547 //DB( g_print(" sort on column %d\n\n", column) );
548 //gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(model), column, GTK_SORT_DESCENDING);
549
550 //gtk_chart_set_datas(GTK_CHART(data->RE_line), model, LST_STAT_AMOUNT, NULL);
551 gtk_chart_show_legend(GTK_CHART(data->RE_line), FALSE, FALSE);
552 gtk_chart_show_xval(GTK_CHART(data->RE_line), TRUE);
553
554 ////TRANSLATORS: example 'Expense by Category'
555 title = g_strdup_printf(_("%s Over Time"), _(CYA_TIMESELECT[tmpfor]) );
556 gtk_chart_set_datas(GTK_CHART(data->RE_line), model, LST_REPTIME_AMOUNT, title);
557 g_free(title);
558
559 }
560
561 static void ui_reptime_export_csv(GtkWidget *widget, gpointer user_data)
562 {
563 struct ui_reptime_data *data;
564 GtkTreeModel *model;
565 GtkTreeIter iter;
566 gboolean valid;
567 gchar *filename = NULL;
568 GIOChannel *io;
569 gchar *outstr, *name;
570 gint tmpfor;
571
572 DB( g_print("\n[reptime] export csv\n") );
573
574 data = g_object_get_data(G_OBJECT(gtk_widget_get_ancestor(widget, GTK_TYPE_WINDOW)), "inst_data");
575
576 tmpfor = gtk_combo_box_get_active(GTK_COMBO_BOX(data->CY_for));
577
578 name = g_strdup_printf("hb-ui_reptime_%s.csv", CYA_TIMESELECT[tmpfor]);
579
580 if( ui_file_chooser_csv(GTK_WINDOW(data->window), GTK_FILE_CHOOSER_ACTION_SAVE, &filename, name) == TRUE )
581 {
582 DB( g_print(" + filename is %s\n", filename) );
583
584 io = g_io_channel_new_file(filename, "w", NULL);
585 if(io != NULL)
586 {
587 // header
588 outstr = g_strdup_printf("%s;%s;\n", _("Time slice"), _("Amount"));
589 g_io_channel_write_chars(io, outstr, -1, NULL, NULL);
590
591 model = gtk_tree_view_get_model(GTK_TREE_VIEW(data->LV_report));
592 valid = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(model), &iter);
593
594 while (valid)
595 {
596 gchar *name;
597 gdouble amount;
598
599 gtk_tree_model_get (model, &iter,
600 //LST_REPTIME_KEY, i,
601 LST_REPTIME_TITLE , &name,
602 LST_REPTIME_AMOUNT , &amount,
603
604 -1);
605
606 outstr = g_strdup_printf("%s;%.2f\n", name, amount);
607 g_io_channel_write_chars(io, outstr, -1, NULL, NULL);
608
609 DB( g_print("%s", outstr) );
610
611 g_free(outstr);
612
613 valid = gtk_tree_model_iter_next(GTK_TREE_MODEL(model), &iter);
614 }
615
616 g_io_channel_unref (io);
617 }
618
619 g_free( filename );
620 }
621
622 g_free(name);
623
624
625 }
626
627
628 static void ui_reptime_for(GtkWidget *widget, gpointer user_data)
629 {
630 struct ui_reptime_data *data;
631 gint page;
632
633 DB( g_print("\n[reptime] for\n") );
634
635 data = g_object_get_data(G_OBJECT(gtk_widget_get_ancestor(widget, GTK_TYPE_WINDOW)), "inst_data");
636
637 page = gtk_combo_box_get_active(GTK_COMBO_BOX(data->CY_for));
638 gtk_notebook_set_current_page(GTK_NOTEBOOK(data->GR_select), page);
639
640 ui_reptime_compute(widget, data);
641 }
642
643
644 static void ui_reptime_compute(GtkWidget *widget, gpointer user_data)
645 {
646 struct ui_reptime_data *data;
647 gint tmpfor, tmpslice;
648 guint32 from, to;
649 gboolean cumul;
650 gboolean showall;
651
652 gdouble cumulation;
653
654 GtkTreeModel *model;
655 GtkTreeIter iter;
656 GList *list;
657 gint id;
658 guint n_result, i;
659 GDate *date1, *date2;
660 gdouble *tmp_amount;
661 guint32 selkey;
662
663 DB( g_print("\n[reptime] compute\n") );
664
665 data = g_object_get_data(G_OBJECT(gtk_widget_get_ancestor(widget, GTK_TYPE_WINDOW)), "inst_data");
666
667 tmpfor = gtk_combo_box_get_active(GTK_COMBO_BOX(data->CY_for));
668 tmpslice = gtk_combo_box_get_active(GTK_COMBO_BOX(data->CY_view));
669 cumul = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(data->CM_cumul));
670 showall = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(data->CM_all));
671 selkey = 0;
672
673 switch(tmpfor)
674 {
675 case FOR_REPTIME_ACCOUNT:
676 selkey = ui_acc_comboboxentry_get_key(GTK_COMBO_BOX(data->PO_acc));
677 break;
678 case FOR_REPTIME_CATEGORY:
679 selkey = ui_cat_comboboxentry_get_key(GTK_COMBO_BOX(data->PO_cat));
680 break;
681 case FOR_REPTIME_PAYEE:
682 selkey = ui_pay_comboboxentry_get_key(GTK_COMBO_BOX(data->PO_pay));
683 break;
684 }
685
686 DB( g_print(" for=%d, view by=%d :: key=%d\n", tmpfor, tmpslice, selkey) );
687
688 /* do nothing if no transaction */
689 if(g_list_length(GLOBALS->ope_list) == 0) return;
690
691 //get our min max date
692 from = data->filter->mindate;
693 to = data->filter->maxdate;
694 if(to < from) return;
695
696 /* count number or results */
697 switch(tmpslice)
698 {
699 case GROUPBY_REPTIME_DAY:
700 n_result = 1 + (to - from);
701 break;
702 case GROUPBY_REPTIME_WEEK:
703 n_result = 1 + ((to - from) / 7);
704 break;
705 case GROUPBY_REPTIME_MONTH:
706 date1 = g_date_new_julian(from);
707 date2 = g_date_new_julian(to);
708 n_result = 1 + ((g_date_get_year(date2) - g_date_get_year(date1)) * 12) + g_date_get_month(date2) - g_date_get_month(date1);
709 g_date_free(date2);
710 g_date_free(date1);
711 break;
712 case GROUPBY_REPTIME_QUARTER:
713 date1 = g_date_new_julian(from);
714 date2 = g_date_new_julian(to);
715 n_result = 1 + (((g_date_get_year(date2) - g_date_get_year(date1)) * 12) + g_date_get_month(date2) - g_date_get_month(date1))/3;
716 g_date_free(date2);
717 g_date_free(date1);
718 break;
719 case GROUPBY_REPTIME_YEAR:
720 date1 = g_date_new_julian(from);
721 date2 = g_date_new_julian(to);
722 n_result = 1 + g_date_get_year(date2) - g_date_get_year(date1);
723 g_date_free(date2);
724 g_date_free(date1);
725 break;
726 default:
727 n_result = 0;
728 }
729
730 DB( g_print(" %s :: n_result=%d\n", CYA_TIMESELECT[tmpfor], n_result) );
731
732 /* allocate some memory */
733
734 tmp_amount = g_malloc0((n_result+2) * sizeof(gdouble));
735
736 if(tmp_amount)
737 {
738 Account *acc;
739
740 /* set currency */
741 /*
742 if( (tmpfor == FOR_REPTIME_ACCOUNT) && (showall == FALSE) )
743 {
744 if(acc = da_acc_get(selkey))
745 {
746 //ui_reptime_list_set_cur(GTK_TREE_VIEW(data->LV_report), acc->kcur);
747 //gtk_chart_set_currency(GTK_CHART(data->RE_line), acc->kcur);
748 }
749 }
750 else
751 {
752 //ui_reptime_list_set_cur(GTK_TREE_VIEW(data->LV_report), GLOBALS->kcur);
753 //gtk_chart_set_currency(GTK_CHART(data->RE_line), GLOBALS->kcur);
754 }*/
755
756 /* compute the results */
757 list = g_list_first(GLOBALS->ope_list);
758 while (list != NULL)
759 {
760 Transaction *ope = list->data;
761
762 //debug
763 DB( g_print("** testing '%s', cat=%d==> %d\n", ope->wording, ope->kcat, filter_test(data->filter, ope)) );
764
765 acc = da_acc_get(ope->kacc);
766 if(acc == NULL) goto next1;
767 if((acc->flags & (AF_CLOSED|AF_NOREPORT))) goto next1;
768
769
770 // add usage of payee or category
771 if( !(ope->flags & OF_REMIND) && ope->date >= from && ope->date <= to)
772 //if( (filter_test(data->filter, ope) == 1) )
773 {
774 gboolean include = FALSE;
775
776 switch(tmpfor)
777 {
778 case FOR_REPTIME_ACCOUNT:
779 if( selkey == ope->kacc )
780 include = TRUE;
781 break;
782 case FOR_REPTIME_CATEGORY:
783 {
784 Category *catentry;
785
786 if( ope->flags & OF_SPLIT )
787 {
788 guint nbsplit = da_transaction_splits_count(ope);
789 Split *split;
790
791 for(i=0;i<nbsplit;i++)
792 {
793 split = ope->splits[i];
794 catentry = da_cat_get(split->kcat);
795 if(catentry != NULL) //#1340142
796 {
797 if( selkey == catentry->parent || selkey == catentry->key )
798 include = TRUE;
799 }
800 }
801 }
802 else
803 {
804 catentry = da_cat_get(ope->kcat);
805 if(catentry != NULL) //#1340142
806 {
807 if( selkey == catentry->parent || selkey == catentry->key )
808 include = TRUE;
809 }
810 }
811
812 }
813 break;
814 case FOR_REPTIME_PAYEE:
815 if( selkey == ope->kpay )
816 include = TRUE;
817 break;
818 }
819
820 if( include == TRUE || showall == TRUE)
821 {
822 guint32 pos = 0;
823 gdouble trn_amount;
824
825 switch(tmpslice)
826 {
827 case GROUPBY_REPTIME_DAY:
828 pos = ope->date - from;
829 break;
830
831 case GROUPBY_REPTIME_WEEK:
832 pos = (ope->date - from)/7;
833 break;
834
835 case GROUPBY_REPTIME_MONTH:
836 pos = DateInMonth(from, ope->date);
837 break;
838
839 case GROUPBY_REPTIME_QUARTER:
840 pos = DateInQuarter(from, ope->date);
841 break;
842
843 case GROUPBY_REPTIME_YEAR:
844 pos = DateInYear(from, ope->date);
845 break;
846 }
847
848 acc = da_acc_get(ope->kacc);
849 trn_amount = 0.0;
850
851 if( tmpfor == FOR_REPTIME_CATEGORY && ope->flags & OF_SPLIT )
852 {
853 guint nbsplit = da_transaction_splits_count(ope);
854 Split *split;
855 Category *catentry;
856
857 for(i=0;i<nbsplit;i++)
858 {
859 split = ope->splits[i];
860 catentry = da_cat_get(split->kcat);
861 if(catentry != NULL) //#1340142
862 {
863 if( selkey == catentry->parent || selkey == catentry->key )
864 trn_amount += split->amount;
865 }
866 }
867 }
868 else
869 trn_amount = ope->amount;
870
871
872 //trn_amount = to_base_amount(trn_amount, acc->kcur);
873
874 DB( g_print("** pos=%d will add %.2f to \n", pos, trn_amount) );
875
876 tmp_amount[pos] += trn_amount;
877
878 }
879 }
880 next1:
881 list = g_list_next(list);
882 }
883
884 /* clear and detach our model */
885 model = gtk_tree_view_get_model(GTK_TREE_VIEW(data->LV_report));
886 gtk_list_store_clear (GTK_LIST_STORE(model));
887 g_object_ref(model); /* Make sure the model stays with us after the tree view unrefs it */
888 gtk_tree_view_set_model(GTK_TREE_VIEW(data->LV_report), NULL); /* Detach model from view */
889
890 cumulation = 0.0;
891
892 /* insert into the treeview */
893 for(i=0, id=0; i<n_result; i++)
894 {
895 gchar *name, *fullcatname;
896 gdouble value;
897 gchar buffer[64];
898 GDate *date;
899
900 name = NULL;
901 fullcatname = NULL;
902
903
904 DB( g_print("try to insert item %d\n", i) );
905
906 /* get the result name */
907 switch(tmpslice)
908 {
909 case GROUPBY_REPTIME_DAY:
910 date = g_date_new_julian (from + i);
911 g_date_strftime (buffer, 63, PREFS->date_format, date);
912 g_date_free(date);
913 name = buffer;
914 break;
915
916 case GROUPBY_REPTIME_WEEK:
917 date = g_date_new_julian(from);
918 g_date_add_days(date, i*7);
919 //g_snprintf(buffer, 63, "%d-%02d", g_date_get_year(date), g_date_get_month(date));
920 g_snprintf(buffer, 63, "%d-%d", g_date_get_year(date), g_date_get_monday_week_of_year(date));
921 g_date_free(date);
922 name = buffer;
923 break;
924
925 case GROUPBY_REPTIME_MONTH:
926 date = g_date_new_julian(from);
927 g_date_add_months(date, i);
928 //g_snprintf(buffer, 63, "%d-%02d", g_date_get_year(date), g_date_get_month(date));
929 g_snprintf(buffer, 63, "%d-%s", g_date_get_year(date), _(CYA_ABMONTHS[g_date_get_month(date)]));
930 g_date_free(date);
931 name = buffer;
932 break;
933
934 case GROUPBY_REPTIME_QUARTER:
935 date = g_date_new_julian(from);
936 g_date_add_months(date, i*3);
937 //g_snprintf(buffer, 63, "%d-%02d", g_date_get_year(date), g_date_get_month(date));
938 g_snprintf(buffer, 63, "%d-%d", g_date_get_year(date), ((g_date_get_month(date)-1)/3)+1);
939 g_date_free(date);
940 name = buffer;
941 break;
942
943 case GROUPBY_REPTIME_YEAR:
944 date = g_date_new_julian(from);
945 g_date_add_years(date, i);
946 g_snprintf(buffer, 63, "%d", g_date_get_year(date));
947 g_date_free(date);
948 name = buffer;
949 break;
950 }
951
952 cumulation += tmp_amount[i];
953 value = cumul == TRUE ? cumulation : tmp_amount[i];
954
955
956 //DB( g_print(" inserting %2d, '%s', %9.2f\n", i, name, value) );
957
958 gtk_list_store_append (GTK_LIST_STORE(model), &iter);
959 gtk_list_store_set (GTK_LIST_STORE(model), &iter,
960 LST_REPTIME_POS, id++,
961 LST_REPTIME_KEY, i,
962 LST_REPTIME_TITLE, name,
963 LST_REPTIME_AMOUNT, value,
964 -1);
965
966 g_free(fullcatname);
967 }
968
969 /* Re-attach model to view */
970 gtk_tree_view_set_model(GTK_TREE_VIEW(data->LV_report), model);
971 g_object_unref(model);
972 }
973
974 /* free our memory */
975 g_free(tmp_amount);
976
977
978 ui_reptime_update(widget, user_data);
979
980 }
981
982
983
984
985
986 /*
987 ** update sensitivity
988 */
989 static void ui_reptime_sensitive(GtkWidget *widget, gpointer user_data)
990 {
991 struct ui_reptime_data *data;
992 gboolean active;
993 gboolean sensitive;
994 gint page;
995
996 DB( g_print("\n[reptime] sensitive\n") );
997
998 data = g_object_get_data(G_OBJECT(gtk_widget_get_ancestor(widget, GTK_TYPE_WINDOW)), "inst_data");
999
1000 active = gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(data->LV_report)), NULL, NULL);
1001
1002 page = gtk_notebook_get_current_page(GTK_NOTEBOOK(data->GR_result));
1003
1004 sensitive = page == 0 ? active : FALSE;
1005 // gtk_widget_set_sensitive(data->TB_buttons[ACTION_REPBUDGET_DETAIL], sensitive);
1006 gtk_action_set_sensitive(gtk_ui_manager_get_action(data->ui, "/ToolBar/Detail"), sensitive);
1007
1008 //view = gtk_combo_box_get_active(GTK_COMBO_BOX(data->CY_view));
1009
1010 }
1011
1012 static void ui_reptime_update_detail(GtkWidget *widget, gpointer user_data)
1013 {
1014 struct ui_reptime_data *data;
1015
1016 data = g_object_get_data(G_OBJECT(gtk_widget_get_ancestor(widget, GTK_TYPE_WINDOW)), "inst_data");
1017
1018 if(data->detail)
1019 {
1020 GtkTreeSelection *treeselection;
1021 GtkTreeModel *model;
1022 GtkTreeIter iter;
1023 guint key;
1024
1025 treeselection = gtk_tree_view_get_selection (GTK_TREE_VIEW(data->LV_report));
1026
1027 if (gtk_tree_selection_get_selected(treeselection, &model, &iter))
1028 {
1029 gtk_tree_model_get(model, &iter, LST_REPTIME_KEY, &key, -1);
1030
1031 DB( g_print(" - active is %d\n", key) );
1032
1033 ui_reptime_detail(GTK_WIDGET(gtk_tree_selection_get_tree_view (treeselection)), GINT_TO_POINTER(key));
1034 }
1035
1036
1037
1038 gtk_widget_show(data->GR_detail);
1039 }
1040 else
1041 gtk_widget_hide(data->GR_detail);
1042 }
1043
1044
1045
1046
1047 static void ui_reptime_toggle_detail(GtkWidget *widget, gpointer user_data)
1048 {
1049 struct ui_reptime_data *data;
1050
1051 data = g_object_get_data(G_OBJECT(gtk_widget_get_ancestor(widget, GTK_TYPE_WINDOW)), "inst_data");
1052
1053 data->detail ^= 1;
1054
1055 DB( printf("(stats) toggledetail to %d\n", data->detail) );
1056
1057 ui_reptime_update_detail(widget, user_data);
1058
1059 }
1060
1061 static void ui_reptime_zoomx_callback(GtkWidget *widget, gpointer user_data)
1062 {
1063 struct ui_reptime_data *data;
1064 gdouble value;
1065
1066 DB( g_print("\n[reptime] zoomx\n") );
1067
1068 data = g_object_get_data(G_OBJECT(gtk_widget_get_ancestor(widget, GTK_TYPE_WINDOW)), "inst_data");
1069
1070 value = gtk_range_get_value(GTK_RANGE(data->RG_zoomx));
1071
1072 DB( g_print(" + scale is %.2f\n", value) );
1073
1074 gtk_chart_set_barw(GTK_CHART(data->RE_line), value);
1075
1076 }
1077
1078
1079
1080 static void ui_reptime_toggle_minor(GtkWidget *widget, gpointer user_data)
1081 {
1082 struct ui_reptime_data *data;
1083
1084 DB( g_print("\n[reptime] toggle\n") );
1085
1086 data = g_object_get_data(G_OBJECT(gtk_widget_get_ancestor(widget, GTK_TYPE_WINDOW)), "inst_data");
1087
1088 GLOBALS->minor = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(data->CM_minor));
1089
1090 //hbfile_update(data->LV_acc, (gpointer)4);
1091 gtk_tree_view_columns_autosize (GTK_TREE_VIEW(data->LV_report));
1092
1093 gtk_chart_show_minor(GTK_CHART(data->RE_line), GLOBALS->minor);
1094
1095 }
1096
1097
1098 static void ui_reptime_toggle_showall(GtkWidget *widget, gpointer user_data)
1099 {
1100 struct ui_reptime_data *data;
1101 gboolean showall;
1102
1103 DB( g_print("\n[reptime] toggle\n") );
1104
1105 data = g_object_get_data(G_OBJECT(gtk_widget_get_ancestor(widget, GTK_TYPE_WINDOW)), "inst_data");
1106
1107 showall = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(data->CM_all));
1108
1109 gtk_widget_set_sensitive(GTK_WIDGET(data->PO_acc), showall^1);
1110 gtk_widget_set_sensitive(GTK_WIDGET(data->PO_cat), showall^1);
1111 gtk_widget_set_sensitive(GTK_WIDGET(data->PO_pay), showall^1);
1112
1113 ui_reptime_compute(widget, data);
1114
1115 }
1116
1117
1118
1119 /*
1120 **
1121 */
1122 static void ui_reptime_setup(struct ui_reptime_data *data, guint32 accnum)
1123 {
1124 DB( g_print("\n[reptime] setup\n") );
1125
1126 data->detail = 0;
1127
1128
1129 data->filter = da_filter_malloc();
1130 filter_default_all_set(data->filter);
1131
1132 /* 3.4 : make int transfer out of stats */
1133 data->filter->option[FILTER_PAYMODE] = 1;
1134 data->filter->paymode[PAYMODE_INTXFER] = FALSE;
1135
1136 filter_preset_daterange_set(data->filter, PREFS->date_range_rep);
1137
1138 g_signal_handler_block(data->PO_mindate, data->handler_id[HID_REPTIME_MINDATE]);
1139 g_signal_handler_block(data->PO_maxdate, data->handler_id[HID_REPTIME_MAXDATE]);
1140
1141 gtk_dateentry_set_date(GTK_DATE_ENTRY(data->PO_mindate), data->filter->mindate);
1142 gtk_dateentry_set_date(GTK_DATE_ENTRY(data->PO_maxdate), data->filter->maxdate);
1143
1144 g_signal_handler_unblock(data->PO_mindate, data->handler_id[HID_REPTIME_MINDATE]);
1145 g_signal_handler_unblock(data->PO_maxdate, data->handler_id[HID_REPTIME_MAXDATE]);
1146
1147
1148 DB( g_print(" populate\n") );
1149 ui_acc_comboboxentry_populate(GTK_COMBO_BOX(data->PO_acc), GLOBALS->h_acc, ACC_LST_INSERT_REPORT);
1150 ui_acc_comboboxentry_set_active(GTK_COMBO_BOX(data->PO_acc), accnum);
1151
1152 ui_pay_comboboxentry_populate(GTK_COMBO_BOX(data->PO_pay), GLOBALS->h_pay);
1153 gtk_combo_box_set_active(GTK_COMBO_BOX(data->PO_pay), 0);
1154
1155 ui_cat_comboboxentry_populate(GTK_COMBO_BOX(data->PO_cat), GLOBALS->h_cat);
1156 gtk_combo_box_set_active(GTK_COMBO_BOX(data->PO_cat), 0);
1157
1158 DB( g_print(" all ok\n") );
1159
1160 }
1161
1162
1163
1164 static void ui_reptime_selection(GtkTreeSelection *treeselection, gpointer user_data)
1165 {
1166 GtkTreeModel *model;
1167 GtkTreeIter iter;
1168 guint key = -1;
1169
1170 DB( g_print("\n[reptime] selection\n") );
1171
1172 if (gtk_tree_selection_get_selected(treeselection, &model, &iter))
1173 {
1174 gtk_tree_model_get(model, &iter, LST_REPTIME_KEY, &key, -1);
1175
1176 }
1177
1178 DB( g_print(" - active is %d\n", key) );
1179
1180 ui_reptime_detail(GTK_WIDGET(gtk_tree_selection_get_tree_view (treeselection)), GINT_TO_POINTER(key));
1181 ui_reptime_sensitive(GTK_WIDGET(gtk_tree_selection_get_tree_view (treeselection)), NULL);
1182 }
1183
1184
1185 /*
1186 **
1187 */
1188 static gboolean ui_reptime_dispose(GtkWidget *widget, GdkEvent *event, gpointer user_data)
1189 {
1190 struct ui_reptime_data *data = user_data;
1191 struct WinGeometry *wg;
1192
1193 DB( g_print("\n[reptime] dispose\n") );
1194
1195 da_filter_free(data->filter);
1196
1197 g_free(data);
1198
1199 //store position and size
1200 wg = &PREFS->tme_wg;
1201 gtk_window_get_position(GTK_WINDOW(widget), &wg->l, &wg->t);
1202 gtk_window_get_size(GTK_WINDOW(widget), &wg->w, &wg->h);
1203
1204 DB( g_print(" window: l=%d, t=%d, w=%d, h=%d\n", wg->l, wg->t, wg->w, wg->h) );
1205
1206
1207
1208 //enable define windows
1209 GLOBALS->define_off--;
1210 ui_mainwindow_update(GLOBALS->mainwindow, GINT_TO_POINTER(UF_SENSITIVE));
1211
1212 return FALSE;
1213 }
1214
1215 // the window creation
1216 GtkWidget *ui_reptime_window_new(guint32 accnum)
1217 {
1218 struct ui_reptime_data *data;
1219 struct WinGeometry *wg;
1220 GtkWidget *window, *mainvbox, *hbox, *vbox, *hbox2, *notebook, *treeview;
1221 GtkWidget *label, *widget, *table, *alignment;
1222 gint row;
1223 GtkUIManager *ui;
1224 GtkActionGroup *actions;
1225 GtkAction *action;
1226 GError *error = NULL;
1227
1228 data = g_malloc0(sizeof(struct ui_reptime_data));
1229 if(!data) return NULL;
1230
1231 DB( g_print("\n[reptime] new\n") );
1232
1233
1234 //disable define windows
1235 GLOBALS->define_off++;
1236 ui_mainwindow_update(GLOBALS->mainwindow, GINT_TO_POINTER(2));
1237
1238 /* create window, etc */
1239 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
1240 data->window = window;
1241
1242 //store our window private data
1243 g_object_set_data(G_OBJECT(window), "inst_data", (gpointer)data);
1244
1245 gtk_window_set_title (GTK_WINDOW (window), _("Trend Time Report"));
1246
1247 //set the window icon
1248 //homebank_window_set_icon_from_file(GTK_WINDOW (window), "report_stats.svg");
1249 gtk_window_set_icon_name(GTK_WINDOW (window), HB_STOCK_REP_TIME);
1250
1251
1252 //window contents
1253 mainvbox = gtk_vbox_new (FALSE, 0);
1254 gtk_container_add (GTK_CONTAINER (window), mainvbox);
1255
1256 hbox = gtk_hbox_new(FALSE, 0);
1257 gtk_box_pack_start (GTK_BOX (mainvbox), hbox, TRUE, TRUE, 0);
1258
1259 //control part
1260 table = gtk_table_new (6, 3, FALSE);
1261 // gtk_alignment_new(xalign, yalign, xscale, yscale)
1262 alignment = gtk_alignment_new(0.0, 0.0, 0.0, 0.0);
1263 gtk_container_add(GTK_CONTAINER(alignment), table);
1264 gtk_box_pack_start (GTK_BOX (hbox), alignment, FALSE, FALSE, 0);
1265
1266 gtk_container_set_border_width (GTK_CONTAINER (table), HB_BOX_SPACING);
1267 gtk_table_set_row_spacings (GTK_TABLE (table), HB_TABROW_SPACING);
1268 gtk_table_set_col_spacings (GTK_TABLE (table), HB_TABCOL_SPACING);
1269
1270 row = 0;
1271 label = make_label(_("Display"), 0.0, 0.5);
1272 gimp_label_set_attributes(GTK_LABEL(label), PANGO_ATTR_WEIGHT, PANGO_WEIGHT_BOLD, -1);
1273 gtk_table_attach_defaults (GTK_TABLE (table), label, 0, 3, row, row+1);
1274
1275 row++;
1276 label = make_label(_("_For:"), 0, 0.5);
1277 gtk_table_attach (GTK_TABLE (table), label, 1, 2, row, row+1, (GtkAttachOptions) (GTK_FILL), (GtkAttachOptions) (0), 0, 0);
1278 widget = make_cycle(label, CYA_TIMESELECT);
1279 data->CY_for = widget;
1280 gtk_table_attach_defaults (GTK_TABLE (table), data->CY_for, 2, 3, row, row+1);
1281
1282 row++;
1283 notebook = gtk_notebook_new();
1284 data->GR_select = notebook;
1285 gtk_widget_show(notebook);
1286 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(notebook), FALSE);
1287 gtk_notebook_set_show_border(GTK_NOTEBOOK(notebook), FALSE);
1288 gtk_table_attach_defaults (GTK_TABLE (table), notebook, 1, 3, row, row+1);
1289
1290 //account
1291 hbox2 = gtk_hbox_new (FALSE, HB_BOX_SPACING);
1292 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), hbox2, NULL);
1293 label = gtk_label_new_with_mnemonic (_("_Account:"));
1294 gtk_box_pack_start (GTK_BOX (hbox2), label, FALSE, FALSE, 0);
1295 widget = ui_acc_comboboxentry_new(label);
1296 data->PO_acc = widget;
1297 gtk_box_pack_start (GTK_BOX (hbox2), widget, TRUE, TRUE, 0);
1298
1299 //category
1300 hbox2 = gtk_hbox_new (FALSE, HB_BOX_SPACING);
1301 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), hbox2, NULL);
1302 label = gtk_label_new_with_mnemonic (_("_Category:"));
1303 gtk_box_pack_start (GTK_BOX (hbox2), label, FALSE, FALSE, 0);
1304 widget = ui_cat_comboboxentry_new(label);
1305 data->PO_cat = widget;
1306 gtk_box_pack_start (GTK_BOX (hbox2), widget, TRUE, TRUE, 0);
1307
1308 //payee
1309 hbox2 = gtk_hbox_new (FALSE, HB_BOX_SPACING);
1310 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), hbox2, NULL);
1311 label = gtk_label_new_with_mnemonic (_("_Payee:"));
1312 gtk_box_pack_start (GTK_BOX (hbox2), label, FALSE, FALSE, 0);
1313 widget = ui_pay_comboboxentry_new(label);
1314 data->PO_pay = widget;
1315 gtk_box_pack_start (GTK_BOX (hbox2), widget, TRUE, TRUE, 0);
1316
1317 row++;
1318 widget = gtk_check_button_new_with_mnemonic (_("Select _all"));
1319 data->CM_all = widget;
1320 gtk_table_attach_defaults (GTK_TABLE (table), widget, 1, 3, row, row+1);
1321
1322 row++;
1323 widget = gtk_check_button_new_with_mnemonic (_("_Cumulate"));
1324 data->CM_cumul = widget;
1325 gtk_table_attach_defaults (GTK_TABLE (table), widget, 1, 3, row, row+1);
1326
1327 row++;
1328 label = make_label(_("_View by:"), 0, 0.5);
1329 gtk_table_attach (GTK_TABLE (table), label, 1, 2, row, row+1, (GtkAttachOptions) (GTK_FILL), (GtkAttachOptions) (0), 0, 0);
1330 widget = make_cycle(label, CYA_VIEWBY);
1331 data->CY_view = widget;
1332 gtk_table_attach_defaults (GTK_TABLE (table), widget, 2, 3, row, row+1);
1333
1334 row++;
1335 widget = gtk_check_button_new_with_mnemonic (_("_Minor currency"));
1336 data->CM_minor = widget;
1337 gtk_table_attach_defaults (GTK_TABLE (table), widget, 1, 3, row, row+1);
1338
1339 row++;
1340 label = make_label(_("_Zoom X:"), 0, 0.5);
1341 gtk_table_attach (GTK_TABLE (table), label, 1, 2, row, row+1, (GtkAttachOptions) (GTK_FILL), (GtkAttachOptions) (0), 0, 0);
1342 widget = make_scale(label);
1343 data->RG_zoomx = widget;
1344 gtk_table_attach_defaults (GTK_TABLE (table), widget, 2, 3, row, row+1);
1345
1346
1347 row++;
1348 widget = gtk_hseparator_new();
1349 gtk_table_attach_defaults (GTK_TABLE (table), widget, 0, 3, row, row+1);
1350
1351 row++;
1352 label = make_label(_("Date filter"), 0.0, 0.5);
1353 gimp_label_set_attributes(GTK_LABEL(label), PANGO_ATTR_WEIGHT, PANGO_WEIGHT_BOLD, -1);
1354 gtk_table_attach_defaults (GTK_TABLE (table), label, 0, 3, row, row+1);
1355
1356 row++;
1357 label = make_label(_("_Range:"), 0, 0.5);
1358 gtk_table_attach (GTK_TABLE (table), label, 1, 2, row, row+1, (GtkAttachOptions) (GTK_FILL), (GtkAttachOptions) (0), 0, 0);
1359 data->CY_range = make_daterange(label, FALSE);
1360 gtk_table_attach_defaults (GTK_TABLE (table), data->CY_range, 2, 3, row, row+1);
1361
1362 row++;
1363 label = make_label(_("_From:"), 0, 0.5);
1364 gtk_table_attach (GTK_TABLE (table), label, 1, 2, row, row+1, (GtkAttachOptions) (GTK_FILL), (GtkAttachOptions) (0), 0, 0);
1365 data->PO_mindate = gtk_dateentry_new();
1366 gtk_table_attach_defaults (GTK_TABLE (table), data->PO_mindate, 2, 3, row, row+1);
1367
1368 row++;
1369 label = make_label(_("_To:"), 0, 0.5);
1370 gtk_table_attach (GTK_TABLE (table), label, 1, 2, row, row+1, (GtkAttachOptions) (GTK_FILL), (GtkAttachOptions) (0), 0, 0);
1371 data->PO_maxdate = gtk_dateentry_new();
1372 gtk_table_attach_defaults (GTK_TABLE (table), data->PO_maxdate, 2, 3, row, row+1);
1373
1374
1375 //part: info + report
1376 vbox = gtk_vbox_new (FALSE, 0);
1377 gtk_box_pack_start (GTK_BOX (hbox), vbox, TRUE, TRUE, 0);
1378
1379 //ui manager
1380 actions = gtk_action_group_new ("Account");
1381
1382 //as we use gettext
1383 gtk_action_group_set_translation_domain(actions, GETTEXT_PACKAGE);
1384
1385 // data to action callbacks is set here (data)
1386 gtk_action_group_add_actions (actions, entries, n_entries, data);
1387
1388 gtk_action_group_add_toggle_actions (actions,
1389 toggle_entries, n_toggle_entries,
1390 data);
1391
1392
1393 /* set which action should have priority in the toolbar */
1394 action = gtk_action_group_get_action(actions, "List");
1395 g_object_set(action, "is_important", TRUE, NULL);
1396
1397 action = gtk_action_group_get_action(actions, "Line");
1398 g_object_set(action, "is_important", TRUE, NULL);
1399
1400 action = gtk_action_group_get_action(actions, "Detail");
1401 g_object_set(action, "is_important", TRUE, NULL);
1402
1403 action = gtk_action_group_get_action(actions, "Refresh");
1404 g_object_set(action, "is_important", TRUE, NULL);
1405
1406
1407 ui = gtk_ui_manager_new ();
1408 gtk_ui_manager_insert_action_group (ui, actions, 0);
1409 gtk_window_add_accel_group (GTK_WINDOW (window), gtk_ui_manager_get_accel_group (ui));
1410
1411 if (!gtk_ui_manager_add_ui_from_string (ui, ui_info, -1, &error))
1412 {
1413 g_message ("building UI failed: %s", error->message);
1414 g_error_free (error);
1415 }
1416
1417 data->ui = ui;
1418 data->actions = actions;
1419
1420 //toolbar
1421 data->TB_bar = gtk_ui_manager_get_widget (ui, "/ToolBar");
1422 gtk_box_pack_start (GTK_BOX (vbox), data->TB_bar, FALSE, FALSE, 0);
1423
1424 //infos
1425 hbox = gtk_hbox_new (FALSE, HB_BOX_SPACING);
1426 gtk_container_set_border_width (GTK_CONTAINER(hbox), HB_BOX_SPACING);
1427 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
1428
1429
1430 widget = make_label(NULL, 0.5, 0.5);
1431 gimp_label_set_attributes (GTK_LABEL (widget), PANGO_ATTR_SCALE, PANGO_SCALE_SMALL, -1);
1432 data->TX_daterange = widget;
1433 gtk_box_pack_start (GTK_BOX (hbox), widget, TRUE, TRUE, 0);
1434
1435
1436
1437 notebook = gtk_notebook_new();
1438 data->GR_result = notebook;
1439 gtk_widget_show(notebook);
1440 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(notebook), FALSE);
1441 gtk_notebook_set_show_border(GTK_NOTEBOOK(notebook), FALSE);
1442
1443 gtk_box_pack_start (GTK_BOX (vbox), notebook, TRUE, TRUE, 0);
1444
1445 //page: list
1446 vbox = gtk_vbox_new (FALSE, 0);
1447 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), vbox, NULL);
1448
1449 widget = gtk_scrolled_window_new (NULL, NULL);
1450 //gtk_scrolled_window_set_placement(GTK_SCROLLED_WINDOW (widget), GTK_CORNER_TOP_RIGHT);
1451 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (widget), GTK_SHADOW_ETCHED_IN);
1452 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (widget), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1453 treeview = ui_list_reptime_create();
1454 data->LV_report = treeview;
1455 gtk_container_add (GTK_CONTAINER(widget), treeview);
1456 gtk_box_pack_start (GTK_BOX (vbox), widget, TRUE, TRUE, 0);
1457
1458 //detail
1459 widget = gtk_scrolled_window_new (NULL, NULL);
1460 data->GR_detail = widget;
1461 //gtk_scrolled_window_set_placement(GTK_SCROLLED_WINDOW (widget), GTK_CORNER_TOP_RIGHT);
1462 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (widget), GTK_SHADOW_ETCHED_IN);
1463 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (widget), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1464 treeview = create_list_transaction(TRN_LIST_TYPE_DETAIL, PREFS->lst_ope_columns);
1465 data->LV_detail = treeview;
1466 gtk_container_add (GTK_CONTAINER(widget), treeview);
1467
1468 gtk_box_pack_start (GTK_BOX (vbox), widget, TRUE, TRUE, 0);
1469
1470
1471 //page: lines
1472 widget = gtk_chart_new(CHART_TYPE_LINE);
1473 data->RE_line = widget;
1474 //gtk_chart_set_minor_prefs(GTK_CHART(widget), PREFS->euro_value, PREFS->minor_cur.suffix_symbol);
1475 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), widget, NULL);
1476
1477 //todo:should move this
1478 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(data->CM_minor), GLOBALS->minor);
1479
1480 gtk_combo_box_set_active(GTK_COMBO_BOX(data->CY_view), 1);
1481
1482 /* attach our minor to treeview */
1483 g_object_set_data(G_OBJECT(gtk_tree_view_get_model(GTK_TREE_VIEW(data->LV_report))), "minor", (gpointer)data->CM_minor);
1484 g_object_set_data(G_OBJECT(gtk_tree_view_get_model(GTK_TREE_VIEW(data->LV_detail))), "minor", (gpointer)data->CM_minor);
1485
1486
1487
1488 /* signal connect */
1489 g_signal_connect (window, "delete-event", G_CALLBACK (ui_reptime_dispose), (gpointer)data);
1490
1491 g_signal_connect (data->CM_cumul, "toggled", G_CALLBACK (ui_reptime_compute), NULL);
1492
1493 g_signal_connect (data->CM_minor, "toggled", G_CALLBACK (ui_reptime_toggle_minor), NULL);
1494
1495 data->handler_id[HID_REPTIME_MINDATE] = g_signal_connect (data->PO_mindate, "changed", G_CALLBACK (ui_reptime_date_change), (gpointer)data);
1496 data->handler_id[HID_REPTIME_MAXDATE] = g_signal_connect (data->PO_maxdate, "changed", G_CALLBACK (ui_reptime_date_change), (gpointer)data);
1497
1498 data->handler_id[HID_REPTIME_RANGE] = g_signal_connect (data->CY_range, "changed", G_CALLBACK (ui_reptime_range_change), NULL);
1499
1500 g_signal_connect (data->CY_for, "changed", G_CALLBACK (ui_reptime_for), (gpointer)data);
1501 data->handler_id[HID_REPTIME_VIEW] = g_signal_connect (data->CY_view, "changed", G_CALLBACK (ui_reptime_compute), (gpointer)data);
1502
1503 //setup, init and show window
1504 ui_reptime_setup(data, accnum);
1505
1506 g_signal_connect (data->CM_all, "toggled", G_CALLBACK (ui_reptime_toggle_showall), NULL);
1507 g_signal_connect (data->PO_acc, "changed", G_CALLBACK (ui_reptime_compute), NULL);
1508 g_signal_connect (data->PO_cat, "changed", G_CALLBACK (ui_reptime_compute), NULL);
1509 g_signal_connect (data->PO_pay, "changed", G_CALLBACK (ui_reptime_compute), NULL);
1510
1511 g_signal_connect (data->RG_zoomx, "value-changed", G_CALLBACK (ui_reptime_zoomx_callback), NULL);
1512
1513
1514 g_signal_connect (gtk_tree_view_get_selection(GTK_TREE_VIEW(data->LV_report)), "changed", G_CALLBACK (ui_reptime_selection), NULL);
1515
1516
1517
1518 /* toolbar */
1519 if(PREFS->toolbar_style == 0)
1520 gtk_toolbar_unset_style(GTK_TOOLBAR(data->TB_bar));
1521 else
1522 gtk_toolbar_set_style(GTK_TOOLBAR(data->TB_bar), PREFS->toolbar_style-1);
1523
1524 //setup, init and show window
1525 wg = &PREFS->tme_wg;
1526 gtk_window_move(GTK_WINDOW(window), wg->l, wg->t);
1527 gtk_window_resize(GTK_WINDOW(window), wg->w, wg->h);
1528
1529 gtk_combo_box_set_active(GTK_COMBO_BOX(data->CY_view), GROUPBY_REPTIME_MONTH);
1530
1531 gtk_widget_show_all (window);
1532
1533 //minor ?
1534 if( PREFS->euro_active )
1535 gtk_widget_show(data->CM_minor);
1536 else
1537 gtk_widget_hide(data->CM_minor);
1538
1539 //gtk_widget_hide(data->GR_detail);
1540
1541
1542
1543 ui_reptime_sensitive(window, NULL);
1544 ui_reptime_update_detail(window, NULL);
1545
1546 DB( g_print("range: %d\n", PREFS->date_range_rep) );
1547
1548 if( PREFS->date_range_rep != 0)
1549 gtk_combo_box_set_active(GTK_COMBO_BOX(data->CY_range), PREFS->date_range_rep);
1550 else
1551 ui_reptime_compute(window, NULL);
1552
1553 return window;
1554 }
1555
1556 /*
1557 ** ============================================================================
1558 */
1559
1560
1561 static void ui_reptime_amount_cell_data_function (GtkTreeViewColumn *col,
1562 GtkCellRenderer *renderer,
1563 GtkTreeModel *model,
1564 GtkTreeIter *iter,
1565 gpointer user_data)
1566 {
1567 gdouble value;
1568 gchar *color;
1569 gchar buf[G_ASCII_DTOSTR_BUF_SIZE];
1570 //guint32 kcur = (guint32)g_object_get_data(G_OBJECT(gtk_tree_view_column_get_tree_view(col)), "kcur_data");
1571
1572 gtk_tree_model_get(model, iter, GPOINTER_TO_INT(user_data), &value, -1);
1573
1574 if( value )
1575 {
1576 mystrfmon(buf, G_ASCII_DTOSTR_BUF_SIZE-1, value, GLOBALS->minor);
1577 //hb_strfmon(buf, G_ASCII_DTOSTR_BUF_SIZE-1, value, kcur);
1578
1579 color = get_normal_color_amount(value);
1580
1581 g_object_set(renderer,
1582 "foreground", color,
1583 "text", buf,
1584 NULL); }
1585 else
1586 {
1587 g_object_set(renderer, "text", "", NULL);
1588 }
1589 }
1590
1591
1592 static GtkTreeViewColumn *amount_list_ui_reptime_column(gchar *name, gint id)
1593 {
1594 GtkTreeViewColumn *column;
1595 GtkCellRenderer *renderer;
1596
1597 column = gtk_tree_view_column_new();
1598 gtk_tree_view_column_set_title(column, name);
1599 renderer = gtk_cell_renderer_text_new ();
1600 g_object_set(renderer, "xalign", 1.0, NULL);
1601 gtk_tree_view_column_pack_start(column, renderer, TRUE);
1602 gtk_tree_view_column_set_cell_data_func(column, renderer, ui_reptime_amount_cell_data_function, GINT_TO_POINTER(id), NULL);
1603 gtk_tree_view_column_set_alignment (column, 0.5);
1604 //gtk_tree_view_column_set_sort_column_id (column, id);
1605 return column;
1606 }
1607
1608 /*
1609 static void ui_reptime_list_set_cur(GtkTreeView *treeview, guint32 kcur)
1610 {
1611 g_object_set_data(G_OBJECT(treeview), "kcur_data", (guint32)kcur);
1612 }*/
1613
1614
1615
1616 /*
1617 ** create our statistic list
1618 */
1619 static GtkWidget *ui_list_reptime_create(void)
1620 {
1621 GtkListStore *store;
1622 GtkWidget *view;
1623 GtkCellRenderer *renderer;
1624 GtkTreeViewColumn *column;
1625
1626 /* create list store */
1627 store = gtk_list_store_new(
1628 NUM_LST_REPTIME,
1629 G_TYPE_INT,
1630 G_TYPE_INT,
1631 G_TYPE_STRING,
1632 G_TYPE_DOUBLE
1633 );
1634
1635 //treeview
1636 view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
1637 g_object_unref(store);
1638
1639 gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (view), PREFS->rules_hint);
1640
1641 /* column: Name */
1642 column = gtk_tree_view_column_new();
1643 gtk_tree_view_column_set_title(column, _("Time slice"));
1644 renderer = gtk_cell_renderer_text_new ();
1645 gtk_tree_view_column_pack_start(column, renderer, TRUE);
1646 //gtk_tree_view_column_set_cell_data_func(column, renderer, ope_result_cell_data_function, NULL, NULL);
1647 gtk_tree_view_column_add_attribute(column, renderer, "text", LST_REPTIME_TITLE);
1648 //gtk_tree_view_column_set_sort_column_id (column, LST_REPTIME_NAME);
1649 gtk_tree_view_column_set_resizable(column, TRUE);
1650 gtk_tree_view_column_set_alignment (column, 0.5);
1651 gtk_tree_view_append_column (GTK_TREE_VIEW(view), column);
1652
1653 /* column: Amount */
1654 column = amount_list_ui_reptime_column(_("Amount"), LST_REPTIME_AMOUNT);
1655 gtk_tree_view_append_column (GTK_TREE_VIEW(view), column);
1656
1657 /* column last: empty */
1658 column = gtk_tree_view_column_new();
1659 gtk_tree_view_append_column (GTK_TREE_VIEW(view), column);
1660
1661 /* sort */
1662 gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(store), LST_REPTIME_POS , ui_list_reptime_compare_func, GINT_TO_POINTER(LST_REPTIME_POS), NULL);
1663 gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(store), LST_REPTIME_AMOUNT, ui_list_reptime_compare_func, GINT_TO_POINTER(LST_REPTIME_AMOUNT), NULL);
1664
1665 return(view);
1666 }
1667
1668 static gint ui_list_reptime_compare_func (GtkTreeModel *model, GtkTreeIter *a, GtkTreeIter *b, gpointer userdata)
1669 {
1670 gint sortcol = GPOINTER_TO_INT(userdata);
1671 gint retval = 0;
1672 gint pos1, pos2;
1673 gdouble val1, val2;
1674
1675 gtk_tree_model_get(model, a,
1676 LST_REPTIME_POS, &pos1,
1677 sortcol, &val1,
1678 -1);
1679 gtk_tree_model_get(model, b,
1680 LST_REPTIME_POS, &pos2,
1681 sortcol, &val2,
1682 -1);
1683 /*
1684 if(pos1 == -1) return(1);
1685 if(pos2 == -1) return(-1);
1686 */
1687
1688 if(sortcol == LST_REPTIME_POS)
1689 retval = pos2 - pos1;
1690 else
1691 retval = (ABS(val1) - ABS(val2)) > 0 ? 1 : -1;
1692
1693 //DB( g_print(" sort %d=%d or %.2f=%.2f :: %d\n", pos1,pos2, val1, val2, ret) );
1694
1695 return retval;
1696 }
1697
This page took 0.14354 seconds and 4 git commands to generate.