1 /* HomeBank -- Free, easy, personal accounting for everyone.
2 * Copyright (C) 1995-2017 Maxime DOYEN
4 * This file is part of HomeBank.
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.
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.
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/>.
23 #include "rep_balance.h"
25 #include "list_operation.h"
26 #include "gtk-chart.h"
27 #include "gtk-dateentry.h"
29 #include "ui-account.h"
30 #include "dsp_mainwindow.h"
31 #include "ui-transaction.h"
34 /****************************************************************************/
36 /****************************************************************************/
45 /* our global datas */
46 extern struct HomeBank
*GLOBALS
;
47 extern struct Preferences
*PREFS
;
51 static void repbalance_action_viewlist(GtkAction
*action
, gpointer user_data
);
52 static void repbalance_action_viewline(GtkAction
*action
, gpointer user_data
);
53 static void repbalance_action_detail(GtkAction
*action
, gpointer user_data
);
54 static void repbalance_action_refresh(GtkAction
*action
, gpointer user_data
);
55 static void repbalance_update_daterange(GtkWidget
*widget
, gpointer user_data
);
56 static void repbalance_update_detail(GtkWidget
*widget
, gpointer user_data
);
57 static void repbalance_toggle_detail(GtkWidget
*widget
, gpointer user_data
);
58 static void repbalance_detail(GtkWidget
*widget
, gpointer user_data
);
59 static void repbalance_sensitive(GtkWidget
*widget
, gpointer user_data
);
61 static void repbalance_date_change(GtkWidget
*widget
, gpointer user_data
);
62 static void repbalance_range_change(GtkWidget
*widget
, gpointer user_data
);
63 static void repbalance_update_info(GtkWidget
*widget
, gpointer user_data
);
64 static void repbalance_toggle_minor(GtkWidget
*widget
, gpointer user_data
);
65 static void repbalance_compute(GtkWidget
*widget
, gpointer user_data
);
66 static void repbalance_setup(struct repbalance_data
*data
, guint32 accnum
);
67 static gboolean
repbalance_window_dispose(GtkWidget
*widget
, GdkEvent
*event
, gpointer user_data
);
68 static GtkWidget
*create_list_repbalance(void);
69 static void ui_repbalance_list_set_cur(GtkTreeView
*treeview
, guint32 kcur
);
75 static GtkActionEntry entries
[] = {
76 { "List" , ICONNAME_HB_VIEW_LIST
, N_("List") , NULL
, N_("View results as list"), G_CALLBACK (repbalance_action_viewlist
) },
77 { "Line" , ICONNAME_HB_VIEW_LINE
, N_("Line") , NULL
, N_("View results as lines"), G_CALLBACK (repbalance_action_viewline
) },
78 { "Refresh" , ICONNAME_REFRESH
, N_("Refresh"), NULL
, N_("Refresh results"), G_CALLBACK (repbalance_action_refresh
) },
81 static guint n_entries
= G_N_ELEMENTS (entries
);
83 static GtkToggleActionEntry toggle_entries
[] = {
84 { "Detail", ICONNAME_HB_OPE_SHOW
, /* name, icon-name */
85 N_("Detail"), NULL
, /* label, accelerator */
86 N_("Toggle detail"), /* tooltip */
87 G_CALLBACK (repbalance_action_detail
),
88 FALSE
}, /* is_active */
90 static guint n_toggle_entries
= G_N_ELEMENTS (toggle_entries
);
93 static const gchar
*ui_info
=
95 " <toolbar name='ToolBar'>"
96 " <toolitem action='List'/>"
97 " <toolitem action='Line'/>"
99 " <toolitem action='Detail'/>"
101 " <toolitem action='Refresh'/>"
106 //extern gchar *CYA_FLT_SELECT[];
109 /* action functions -------------------- */
110 static void repbalance_action_viewlist(GtkAction
*action
, gpointer user_data
)
112 struct repbalance_data
*data
= user_data
;
114 gtk_notebook_set_current_page(GTK_NOTEBOOK(data
->GR_result
), 0);
115 repbalance_sensitive(data
->window
, NULL
);
118 static void repbalance_action_viewline(GtkAction
*action
, gpointer user_data
)
120 struct repbalance_data
*data
= user_data
;
122 gtk_notebook_set_current_page(GTK_NOTEBOOK(data
->GR_result
), 1);
123 repbalance_sensitive(data
->window
, NULL
);
126 static void repbalance_action_detail(GtkAction
*action
, gpointer user_data
)
128 struct repbalance_data
*data
= user_data
;
130 repbalance_toggle_detail(data
->window
, NULL
);
133 static void repbalance_action_refresh(GtkAction
*action
, gpointer user_data
)
135 struct repbalance_data
*data
= user_data
;
137 repbalance_compute(data
->window
, NULL
);
142 /* ======================== */
149 static void repbalance_date_change(GtkWidget
*widget
, gpointer user_data
)
151 struct repbalance_data
*data
;
153 DB( g_print("(repbalance) date change\n") );
155 data
= g_object_get_data(G_OBJECT(gtk_widget_get_ancestor(widget
, GTK_TYPE_WINDOW
)), "inst_data");
157 data
->filter
->mindate
= gtk_date_entry_get_date(GTK_DATE_ENTRY(data
->PO_mindate
));
158 data
->filter
->maxdate
= gtk_date_entry_get_date(GTK_DATE_ENTRY(data
->PO_maxdate
));
160 // set min/max date for both widget
161 gtk_date_entry_set_maxdate(GTK_DATE_ENTRY(data
->PO_mindate
), data
->filter
->maxdate
);
162 gtk_date_entry_set_mindate(GTK_DATE_ENTRY(data
->PO_maxdate
), data
->filter
->mindate
);
164 g_signal_handler_block(data
->CY_range
, data
->handler_id
[HID_REPBALANCE_RANGE
]);
165 gtk_combo_box_set_active(GTK_COMBO_BOX(data
->CY_range
), FLT_RANGE_OTHER
);
166 g_signal_handler_unblock(data
->CY_range
, data
->handler_id
[HID_REPBALANCE_RANGE
]);
169 repbalance_compute(widget
, NULL
);
170 repbalance_update_daterange(widget
, NULL
);
175 static void repbalance_update_quickdate(GtkWidget
*widget
, gpointer user_data
)
177 struct repbalance_data
*data
;
179 DB( g_print("(repbalance) update quickdate\n") );
181 data
= g_object_get_data(G_OBJECT(gtk_widget_get_ancestor(widget
, GTK_TYPE_WINDOW
)), "inst_data");
183 g_signal_handler_block(data
->PO_mindate
, data
->handler_id
[HID_REPBALANCE_MINDATE
]);
184 g_signal_handler_block(data
->PO_maxdate
, data
->handler_id
[HID_REPBALANCE_MAXDATE
]);
186 gtk_date_entry_set_date(GTK_DATE_ENTRY(data
->PO_mindate
), data
->filter
->mindate
);
187 gtk_date_entry_set_date(GTK_DATE_ENTRY(data
->PO_maxdate
), data
->filter
->maxdate
);
189 g_signal_handler_unblock(data
->PO_mindate
, data
->handler_id
[HID_REPBALANCE_MINDATE
]);
190 g_signal_handler_unblock(data
->PO_maxdate
, data
->handler_id
[HID_REPBALANCE_MAXDATE
]);
196 static void repbalance_range_change(GtkWidget
*widget
, gpointer user_data
)
198 struct repbalance_data
*data
;
201 DB( g_print("(repbalance) range change\n") );
203 data
= g_object_get_data(G_OBJECT(gtk_widget_get_ancestor(widget
, GTK_TYPE_WINDOW
)), "inst_data");
205 range
= gtk_combo_box_get_active(GTK_COMBO_BOX(data
->CY_range
));
207 if(range
!= FLT_RANGE_OTHER
)
209 filter_preset_daterange_set(data
->filter
, range
, data
->accnum
);
211 repbalance_update_quickdate(widget
, NULL
);
213 repbalance_compute(widget
, NULL
);
214 repbalance_update_daterange(widget
, NULL
);
219 static void repbalance_update_daterange(GtkWidget
*widget
, gpointer user_data
)
221 struct repbalance_data
*data
;
224 DB( g_print("(repbalance) update daterange\n") );
226 data
= g_object_get_data(G_OBJECT(gtk_widget_get_ancestor(widget
, GTK_TYPE_WINDOW
)), "inst_data");
228 daterange
= filter_daterange_text_get(data
->filter
);
229 gtk_label_set_markup(GTK_LABEL(data
->TX_daterange
), daterange
);
234 static void repbalance_selection(GtkTreeSelection
*treeselection
, gpointer user_data
)
240 DB( g_print("(repbalance) selection\n") );
242 if (gtk_tree_selection_get_selected(treeselection
, &model
, &iter
))
244 gtk_tree_model_get(model
, &iter
, LST_OVER_DATE
, &key
, -1);
246 DB( g_print(" - active is %d\n", key
) );
248 repbalance_detail(GTK_WIDGET(gtk_tree_selection_get_tree_view (treeselection
)), GINT_TO_POINTER(key
));
251 repbalance_sensitive(GTK_WIDGET(gtk_tree_selection_get_tree_view (treeselection
)), NULL
);
256 ** update sensitivity
258 static void repbalance_sensitive(GtkWidget
*widget
, gpointer user_data
)
260 struct repbalance_data
*data
;
265 DB( g_print("(repbalance) sensitive\n") );
267 data
= g_object_get_data(G_OBJECT(gtk_widget_get_ancestor(widget
, GTK_TYPE_WINDOW
)), "inst_data");
269 active
= gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(data
->LV_report
)), NULL
, NULL
);
271 page
= gtk_notebook_get_current_page(GTK_NOTEBOOK(data
->GR_result
));
273 sensitive
= page
== 0 ? active
: FALSE
;
274 // gtk_widget_set_sensitive(data->TB_buttons[ACTION_REPBUDGET_DETAIL], sensitive);
275 gtk_action_set_sensitive(gtk_ui_manager_get_action(data
->ui
, "/ToolBar/Detail"), sensitive
);
277 sensitive
= page
== 0 ? FALSE
: TRUE
;
278 gtk_widget_set_sensitive(data
->LB_zoomx
, sensitive
);
279 gtk_widget_set_sensitive(data
->RG_zoomx
, sensitive
);
287 static void repbalance_update_info(GtkWidget
*widget
, gpointer user_data
)
289 struct repbalance_data
*data
;
294 DB( g_print("(repbalance) update info\n") );
296 data
= g_object_get_data(G_OBJECT(gtk_widget_get_ancestor(widget
, GTK_TYPE_WINDOW
)), "inst_data");
298 gboolean selectall
= gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(data
->CM_selectall
));
300 guint32 acckey
= ui_acc_comboboxentry_get_key(GTK_COMBO_BOX(data
->PO_acc
));
302 DB( g_print(" acc key = %d\n", acckey
) );
304 acc
= da_acc_get(acckey
);
308 gboolean minor
= gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(data
->CM_minor
));
310 hb_strfmon(buf
, 127, data
->minimum
, selectall
? GLOBALS
->kcur
: acc
->kcur
, minor
);
312 ////TRANSLATORS: count of transaction in balancedrawn / count of total transaction under abalancedrawn amount threshold
313 info
= g_strdup_printf(_("%d/%d under %s"), data
->nbbalance
, data
->nbope
, buf
);
314 gtk_label_set_text(GTK_LABEL(data
->TX_info
), info
);
321 static void repbalance_detail(GtkWidget
*widget
, gpointer user_data
)
323 struct repbalance_data
*data
;
324 guint active
= GPOINTER_TO_INT(user_data
);
330 data
= g_object_get_data(G_OBJECT(gtk_widget_get_ancestor(widget
, GTK_TYPE_WINDOW
)), "inst_data");
332 DB( g_print("(repbalance) detail\n") );
334 /* clear and detach our model */
335 model
= gtk_tree_view_get_model(GTK_TREE_VIEW(data
->LV_detail
));
336 gtk_list_store_clear (GTK_LIST_STORE(model
));
340 g_object_ref(model
); /* Make sure the model stays with us after the tree view unrefs it */
341 gtk_tree_view_set_model(GTK_TREE_VIEW(data
->LV_detail
), NULL
); /* Detach model from view */
344 gboolean selectall
= gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(data
->CM_selectall
));
346 // get the account key
347 acckey
= ui_acc_comboboxentry_get_key(GTK_COMBO_BOX(data
->PO_acc
));
349 /* fill in the model */
350 list
= g_list_first(data
->ope_list
);
353 Transaction
*ope
= list
->data
;
355 //DB( g_print(" get %s\n", ope->ope_Word) );
358 if( ope
->date
== active
&& (ope
->kacc
== acckey
|| selectall
) )
360 gtk_list_store_append (GTK_LIST_STORE(model
), &iter
);
361 gtk_list_store_set (GTK_LIST_STORE(model
), &iter
,
362 LST_DSPOPE_DATAS
, ope
,
365 list
= g_list_next(list
);
368 /* Re-attach model to view */
369 gtk_tree_view_set_model(GTK_TREE_VIEW(data
->LV_detail
), model
);
370 g_object_unref(model
);
372 gtk_tree_view_columns_autosize( GTK_TREE_VIEW(data
->LV_detail
) );
378 static void repbalance_detail_onRowActivated (GtkTreeView
*treeview
,
380 GtkTreeViewColumn
*col
,
383 struct repbalance_data
*data
;
384 Transaction
*active_txn
;
387 data
= g_object_get_data(G_OBJECT(gtk_widget_get_ancestor(GTK_WIDGET(treeview
), GTK_TYPE_WINDOW
)), "inst_data");
389 DB( g_print ("\n[repbalance] A detail row has been double-clicked!\n") );
391 active_txn
= list_txn_get_active_transaction(GTK_TREE_VIEW(data
->LV_detail
));
394 Transaction
*old_txn
, *new_txn
;
396 old_txn
= da_transaction_clone (active_txn
);
397 new_txn
= active_txn
;
398 result
= deftransaction_external_edit(GTK_WINDOW(data
->window
), old_txn
, new_txn
);
400 if(result
== GTK_RESPONSE_ACCEPT
)
403 GLOBALS
->changes_count
++;
404 repbalance_compute (data
->window
, NULL
);
407 da_transaction_free (old_txn
);
413 static void repbalance_update_detail(GtkWidget
*widget
, gpointer user_data
)
415 struct repbalance_data
*data
;
417 data
= g_object_get_data(G_OBJECT(gtk_widget_get_ancestor(widget
, GTK_TYPE_WINDOW
)), "inst_data");
421 GtkTreeSelection
*treeselection
;
426 treeselection
= gtk_tree_view_get_selection (GTK_TREE_VIEW(data
->LV_report
));
428 if (gtk_tree_selection_get_selected(treeselection
, &model
, &iter
))
430 gtk_tree_model_get(model
, &iter
, LST_OVER_DATE
, &key
, -1);
432 DB( g_print(" - active is %d\n", key
) );
434 repbalance_detail(GTK_WIDGET(gtk_tree_selection_get_tree_view (treeselection
)), GINT_TO_POINTER(key
));
439 gtk_widget_show(data
->GR_detail
);
442 gtk_widget_hide(data
->GR_detail
);
446 static void repbalance_toggle_detail(GtkWidget
*widget
, gpointer user_data
)
448 struct repbalance_data
*data
;
450 data
= g_object_get_data(G_OBJECT(gtk_widget_get_ancestor(widget
, GTK_TYPE_WINDOW
)), "inst_data");
454 DB( g_print("(repbalance) toggledetail to %d\n", (int)data
->detail
) );
456 repbalance_update_detail(widget
, user_data
);
461 static void repbalance_zoomx_callback(GtkWidget
*widget
, gpointer user_data
)
463 struct repbalance_data
*data
;
466 DB( g_print("(statistic) zoomx\n") );
468 data
= g_object_get_data(G_OBJECT(gtk_widget_get_ancestor(widget
, GTK_TYPE_WINDOW
)), "inst_data");
470 value
= gtk_range_get_value(GTK_RANGE(data
->RG_zoomx
));
472 DB( g_print(" + scale is %f\n", value
) );
474 gtk_chart_set_barw(GTK_CHART(data
->RE_line
), value
);
479 static void repbalance_toggle_minor(GtkWidget
*widget
, gpointer user_data
)
481 struct repbalance_data
*data
;
483 DB( g_print("(repbalance) toggle\n") );
485 data
= g_object_get_data(G_OBJECT(gtk_widget_get_ancestor(widget
, GTK_TYPE_WINDOW
)), "inst_data");
487 GLOBALS
->minor
= gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(data
->CM_minor
));
489 repbalance_update_info(widget
,NULL
);
491 gtk_tree_view_columns_autosize (GTK_TREE_VIEW(data
->LV_report
));
493 gtk_chart_show_minor(GTK_CHART(data
->RE_line
), GLOBALS
->minor
);
497 static void repbalance_compute_full_datas(guint32 selkey
, gboolean selectall
, struct repbalance_data
*data
)
501 DB( g_print("(repbalance) compute_full\n") );
503 g_list_free(data
->ope_list
);
504 data
->ope_list
= hbfile_transaction_get_all();
506 if(g_list_length(data
->ope_list
) == 0) return;
508 Transaction
*omin
= g_list_first(data
->ope_list
)->data
;
509 Transaction
*omax
= g_list_last(data
->ope_list
)->data
;
511 // total days in the hbfile
512 data
->n_result
= omax
->date
- omin
->date
;
514 DB( g_print(" - %d days in slice\n", data
->n_result
) );
516 data
->tmp_income
= g_malloc0((data
->n_result
+2) * sizeof(gdouble
));
517 data
->tmp_expense
= g_malloc0((data
->n_result
+2) * sizeof(gdouble
));
519 // to store that initial balance was affected
520 gboolean
*accounts
= g_malloc0((da_acc_get_max_key()+2) * sizeof(gboolean
));
522 if(data
->tmp_income
&& data
->tmp_expense
)
525 /* compute the balance */
526 list
= g_list_first(data
->ope_list
);
531 Transaction
*ope
= list
->data
;
533 if(selkey
== ope
->kacc
|| selectall
== TRUE
)
535 Account
*acc
= da_acc_get(ope
->kacc
);
539 pos
= ope
->date
- omin
->date
;
541 // deal with account initial balance
542 if(accounts
[ope
->kacc
] == 0)
545 trn_amount
= hb_amount_base(acc
->initial
, acc
->kcur
);
547 trn_amount
= acc
->initial
;
550 data
->tmp_expense
[pos
] += trn_amount
;
552 data
->tmp_income
[pos
] += trn_amount
;
554 DB( g_print(" - stored initial %.2f for account %d\n", trn_amount
, ope
->kacc
) );
556 accounts
[ope
->kacc
] = 1;
560 trn_amount
= hb_amount_base(ope
->amount
, acc
->kcur
);
562 trn_amount
= ope
->amount
;
564 //deal with transactions
566 data
->tmp_expense
[pos
] += trn_amount
;
568 data
->tmp_income
[pos
] += trn_amount
;
572 list
= g_list_next(list
);
582 static void repbalance_compute(GtkWidget
*widget
, gpointer user_data
)
584 struct repbalance_data
*data
;
588 gboolean selectall
, eachday
;
591 DB( g_print("(repbalance) compute\n") );
593 data
= g_object_get_data(G_OBJECT(gtk_widget_get_ancestor(widget
, GTK_TYPE_WINDOW
)), "inst_data");
595 selectall
= gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(data
->CM_selectall
));
596 eachday
= gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(data
->CM_eachday
));
598 // get the account key
599 acckey
= ui_acc_comboboxentry_get_key(GTK_COMBO_BOX(data
->PO_acc
));
601 DB( g_print(" acc key = %d\n", acckey
) );
608 // for a single account
611 acc
= da_acc_get(acckey
);
614 data
->minimum
= acc
->minimum
;
615 data
->accnum
= acc
->key
;
616 ui_repbalance_list_set_cur(GTK_TREE_VIEW(data
->LV_report
), acc
->kcur
);
617 gtk_chart_set_currency(GTK_CHART(data
->RE_line
), acc
->kcur
);
623 ui_repbalance_list_set_cur(GTK_TREE_VIEW(data
->LV_report
), GLOBALS
->kcur
);
624 gtk_chart_set_currency(GTK_CHART(data
->RE_line
), GLOBALS
->kcur
);
628 //filter_preset_daterange_set(data->filter, data->filter->range, data->accnum);
629 //repbalance_update_quickdate(widget, NULL);
631 repbalance_compute_full_datas(acckey
, selectall
, data
);
633 /* do nothing if no transaction */
634 if(g_list_length(data
->ope_list
) == 0) return;
637 /* clear and detach our model */
638 model
= gtk_tree_view_get_model(GTK_TREE_VIEW(data
->LV_report
));
639 gtk_list_store_clear (GTK_LIST_STORE(model
));
640 g_object_ref(model
); /* Make sure the model stays with us after the tree view unrefs it */
641 gtk_tree_view_set_model(GTK_TREE_VIEW(data
->LV_report
), NULL
); /* Detach model from view */
643 Transaction
*omin
= g_list_first(data
->ope_list
)->data
;
646 for(i
=0;i
<=data
->n_result
;i
++)
648 gboolean is_balance
= FALSE
;
653 posdate
= omin
->date
+ i
;
655 DB( g_print("omin->date=%d posdate=%d\n", omin
->date
, posdate
) );
658 balance
+= data
->tmp_expense
[i
];
659 balance
+= data
->tmp_income
[i
];
661 if(!eachday
&& data
->tmp_expense
[i
] == 0 && data
->tmp_income
[i
] == 0)
664 if( (posdate
>= data
->filter
->mindate
) && (posdate
<= data
->filter
->maxdate
) )
667 is_balance
= balance
< data
->minimum
? TRUE
: FALSE
;
669 date
= g_date_new_julian (posdate
);
670 g_date_strftime (buf
, 256-1, PREFS
->date_format
, date
);
673 /* column 0: pos (gint) */
674 /* not used: column 1: key (gint) */
675 /* column 2: name (gchar) */
676 /* column x: values (double) */
678 gtk_list_store_append (GTK_LIST_STORE(model
), &iter
);
679 gtk_list_store_set (GTK_LIST_STORE(model
), &iter
,
680 LST_OVER_OVER
, is_balance
,
681 LST_OVER_DATE
, posdate
,
682 LST_OVER_DATESTR
, buf
,
683 LST_OVER_EXPENSE
, data
->tmp_expense
[i
],
684 LST_OVER_INCOME
, data
->tmp_income
[i
],
685 LST_OVER_BALANCE
, balance
,
687 if(is_balance
== TRUE
)
696 g_free(data
->tmp_expense
);
697 g_free(data
->tmp_income
);
699 repbalance_update_info(widget
, NULL
);
701 gtk_chart_show_legend(GTK_CHART(data
->RE_line
), FALSE
, FALSE
);
702 gtk_chart_show_xval(GTK_CHART(data
->RE_line
), TRUE
);
703 gtk_chart_set_overdrawn(GTK_CHART(data
->RE_line
), data
->minimum
);
704 gtk_chart_show_overdrawn(GTK_CHART(data
->RE_line
), !selectall
);
706 gboolean visible
= selectall
? FALSE
: TRUE
;
707 gtk_widget_set_visible (GTK_WIDGET(data
->TX_info
), visible
);
709 /* Re-attach model to view */
710 gtk_tree_view_set_model(GTK_TREE_VIEW(data
->LV_report
), model
);
711 g_object_unref(model
);
713 /* update bar chart */
714 //DB( g_print(" set bar to %d\n\n", LST_STAT_EXPENSE+tmpkind) );
715 gtk_chart_set_datas(GTK_CHART(data
->RE_line
), model
, LST_OVER_BALANCE
, NULL
, NULL
);
716 //gtk_chart_set_line_datas(GTK_CHART(data->RE_line), model, LST_OVER_BALANCE, LST_OVER_DATE);
722 static void repbalance_toggle_selectall(GtkWidget
*widget
, gpointer user_data
)
724 struct repbalance_data
*data
;
727 DB( g_print("(repbalance) toggle selectall\n") );
729 data
= g_object_get_data(G_OBJECT(gtk_widget_get_ancestor(widget
, GTK_TYPE_WINDOW
)), "inst_data");
731 selectall
= gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(data
->CM_selectall
));
733 gtk_widget_set_sensitive(GTK_WIDGET(data
->PO_acc
), selectall
^1);
735 repbalance_compute(widget
, data
);
740 static void repbalance_setup(struct repbalance_data
*data
, guint32 accnum
)
742 DB( g_print("(repbalance) setup\n") );
744 data
->ope_list
= NULL
;
746 data
->filter
= da_filter_malloc();
747 filter_default_all_set(data
->filter
);
749 data
->accnum
= accnum
;
750 filter_preset_daterange_set(data
->filter
, PREFS
->date_range_rep
, data
->accnum
);
752 g_signal_handler_block(data
->PO_mindate
, data
->handler_id
[HID_REPBALANCE_MINDATE
]);
753 g_signal_handler_block(data
->PO_maxdate
, data
->handler_id
[HID_REPBALANCE_MAXDATE
]);
755 gtk_date_entry_set_date(GTK_DATE_ENTRY(data
->PO_mindate
), data
->filter
->mindate
);
756 gtk_date_entry_set_date(GTK_DATE_ENTRY(data
->PO_maxdate
), data
->filter
->maxdate
);
758 g_signal_handler_unblock(data
->PO_mindate
, data
->handler_id
[HID_REPBALANCE_MINDATE
]);
759 g_signal_handler_unblock(data
->PO_maxdate
, data
->handler_id
[HID_REPBALANCE_MAXDATE
]);
761 ui_acc_comboboxentry_populate(GTK_COMBO_BOX(data
->PO_acc
), GLOBALS
->h_acc
, ACC_LST_INSERT_REPORT
);
763 ui_acc_comboboxentry_set_active(GTK_COMBO_BOX(data
->PO_acc
), accnum
);
765 gtk_combo_box_set_active(GTK_COMBO_BOX(data
->PO_acc
), 0);
770 static gboolean
repbalance_window_dispose(GtkWidget
*widget
, GdkEvent
*event
, gpointer user_data
)
772 struct repbalance_data
*data
= user_data
;
773 struct WinGeometry
*wg
;
775 DB( g_print("(repbalance) dispose\n") );
777 g_list_free (data
->ope_list
);
779 da_filter_free(data
->filter
);
783 //store position and size
785 gtk_window_get_position(GTK_WINDOW(widget
), &wg
->l
, &wg
->t
);
786 gtk_window_get_size(GTK_WINDOW(widget
), &wg
->w
, &wg
->h
);
788 DB( g_print(" window: l=%d, t=%d, w=%d, h=%d\n", wg
->l
, wg
->t
, wg
->w
, wg
->h
) );
790 //enable define windows
791 GLOBALS
->define_off
--;
792 ui_mainwindow_update(GLOBALS
->mainwindow
, GINT_TO_POINTER(UF_SENSITIVE
));
798 // the window creation
799 GtkWidget
*repbalance_window_new(gint accnum
)
801 struct repbalance_data
*data
;
802 struct WinGeometry
*wg
;
803 GtkWidget
*window
, *mainvbox
, *hbox
, *vbox
, *notebook
, *treeview
;
804 GtkWidget
*label
, *widget
, *table
;
807 GtkActionGroup
*actions
;
809 GError
*error
= NULL
;
811 data
= g_malloc0(sizeof(struct repbalance_data
));
812 if(!data
) return NULL
;
814 DB( g_print("(repbalance) new\n") );
816 //disable define windows
817 GLOBALS
->define_off
++;
818 ui_mainwindow_update(GLOBALS
->mainwindow
, GINT_TO_POINTER(2));
820 /* create window, etc */
821 window
= gtk_window_new (GTK_WINDOW_TOPLEVEL
);
822 data
->window
= window
;
824 //store our window private data
825 g_object_set_data(G_OBJECT(window
), "inst_data", (gpointer
)data
);
827 gtk_window_set_title (GTK_WINDOW (window
), _("Balance report"));
829 //set the window icon
830 gtk_window_set_icon_name(GTK_WINDOW (window
), ICONNAME_HB_REP_BALANCE
);
833 mainvbox
= gtk_box_new (GTK_ORIENTATION_VERTICAL
, 0);
834 gtk_container_add (GTK_CONTAINER (window
), mainvbox
);
836 hbox
= gtk_box_new(GTK_ORIENTATION_HORIZONTAL
, 0);
837 gtk_box_pack_start (GTK_BOX (mainvbox
), hbox
, TRUE
, TRUE
, 0);
840 table
= gtk_grid_new ();
841 gtk_widget_set_hexpand (GTK_WIDGET(table
), FALSE
);
842 gtk_box_pack_start (GTK_BOX (hbox
), table
, FALSE
, FALSE
, 0);
844 gtk_container_set_border_width (GTK_CONTAINER (table
), SPACING_SMALL
);
845 gtk_grid_set_row_spacing (GTK_GRID (table
), SPACING_SMALL
);
846 gtk_grid_set_column_spacing (GTK_GRID (table
), SPACING_MEDIUM
);
850 label
= make_label_group(_("Display"));
851 gtk_grid_attach (GTK_GRID (table
), label
, 0, row
, 3, 1);
854 label
= make_label_widget(_("A_ccount:"));
855 gtk_grid_attach (GTK_GRID (table
), label
, 1, row
, 1, 1);
856 widget
= ui_acc_comboboxentry_new(label
);
857 data
->PO_acc
= widget
;
858 gtk_grid_attach (GTK_GRID (table
), widget
, 2, row
, 1, 1);
861 widget
= gtk_check_button_new_with_mnemonic (_("Select _all"));
862 data
->CM_selectall
= widget
;
863 gtk_grid_attach (GTK_GRID (table
), widget
, 2, row
, 1, 1);
866 widget
= gtk_check_button_new_with_mnemonic (_("Each _day"));
867 //gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(widget), TRUE);
868 data
->CM_eachday
= widget
;
869 gtk_grid_attach (GTK_GRID (table
), widget
, 2, row
, 1, 1);
872 widget
= gtk_check_button_new_with_mnemonic (_("Euro _minor"));
873 data
->CM_minor
= widget
;
874 gtk_grid_attach (GTK_GRID (table
), widget
, 2, row
, 1, 1);
877 label
= make_label_widget(_("_Zoom X:"));
878 data
->LB_zoomx
= label
;
879 gtk_grid_attach (GTK_GRID (table
), label
, 1, row
, 1, 1);
880 widget
= make_scale(label
);
881 data
->RG_zoomx
= widget
;
882 gtk_grid_attach (GTK_GRID (table
), widget
, 2, row
, 1, 1);
885 widget
= gtk_separator_new(GTK_ORIENTATION_HORIZONTAL
);
886 gtk_grid_attach (GTK_GRID (table
), widget
, 0, row
, 3, 1);
889 label
= make_label_group(_("Date filter"));
890 gtk_grid_attach (GTK_GRID (table
), label
, 0, row
, 3, 1);
893 label
= make_label_widget(_("_Range:"));
894 gtk_grid_attach (GTK_GRID (table
), label
, 1, row
, 1, 1);
895 data
->CY_range
= make_daterange(label
, FALSE
);
896 gtk_grid_attach (GTK_GRID (table
), data
->CY_range
, 2, row
, 1, 1);
899 label
= make_label_widget(_("_From:"));
900 gtk_grid_attach (GTK_GRID (table
), label
, 1, row
, 1, 1);
901 data
->PO_mindate
= gtk_date_entry_new();
902 gtk_grid_attach (GTK_GRID (table
), data
->PO_mindate
, 2, row
, 1, 1);
905 label
= make_label_widget(_("_To:"));
906 gtk_grid_attach (GTK_GRID (table
), label
, 1, row
, 1, 1);
907 data
->PO_maxdate
= gtk_date_entry_new();
908 gtk_grid_attach (GTK_GRID (table
), data
->PO_maxdate
, 2, row
, 1, 1);
911 //part: info + report
912 vbox
= gtk_box_new (GTK_ORIENTATION_VERTICAL
, 0);
913 gtk_box_pack_start (GTK_BOX (hbox
), vbox
, TRUE
, TRUE
, 0);
916 actions
= gtk_action_group_new ("Account");
919 gtk_action_group_set_translation_domain(actions
, GETTEXT_PACKAGE
);
921 // data to action callbacks is set here (data)
922 gtk_action_group_add_actions (actions
, entries
, n_entries
, data
);
924 gtk_action_group_add_toggle_actions (actions
,
925 toggle_entries
, n_toggle_entries
,
929 /* set which action should have priority in the toolbar */
930 action
= gtk_action_group_get_action(actions
, "List");
931 g_object_set(action
, "is_important", TRUE
, NULL
);
933 action
= gtk_action_group_get_action(actions
, "Line");
934 g_object_set(action
, "is_important", TRUE
, NULL
);
936 action
= gtk_action_group_get_action(actions
, "Detail");
937 g_object_set(action
, "is_important", TRUE
, NULL
);
939 action
= gtk_action_group_get_action(actions
, "Refresh");
940 g_object_set(action
, "is_important", TRUE
, NULL
);
944 ui
= gtk_ui_manager_new ();
945 gtk_ui_manager_insert_action_group (ui
, actions
, 0);
946 gtk_window_add_accel_group (GTK_WINDOW (window
), gtk_ui_manager_get_accel_group (ui
));
948 if (!gtk_ui_manager_add_ui_from_string (ui
, ui_info
, -1, &error
))
950 g_message ("building UI failed: %s", error
->message
);
951 g_error_free (error
);
955 data
->actions
= actions
;
958 data
->TB_bar
= gtk_ui_manager_get_widget (ui
, "/ToolBar");
959 gtk_box_pack_start (GTK_BOX (vbox
), data
->TB_bar
, FALSE
, FALSE
, 0);
962 hbox
= gtk_box_new (GTK_ORIENTATION_HORIZONTAL
, SPACING_SMALL
);
963 gtk_container_set_border_width (GTK_CONTAINER(hbox
), SPACING_SMALL
);
964 gtk_box_pack_start (GTK_BOX (vbox
), hbox
, FALSE
, FALSE
, 0);
967 widget
= make_label(NULL
, 0.5, 0.5);
968 gimp_label_set_attributes (GTK_LABEL (widget
), PANGO_ATTR_SCALE
, PANGO_SCALE_SMALL
, -1);
969 data
->TX_daterange
= widget
;
970 gtk_box_pack_start (GTK_BOX (hbox
), widget
, TRUE
, TRUE
, 0);
973 label
= gtk_label_new(NULL
);
974 data
->TX_info
= label
;
975 gtk_box_pack_end (GTK_BOX (hbox
), label
, FALSE
, FALSE
, 0);
977 notebook
= gtk_notebook_new();
978 data
->GR_result
= notebook
;
979 gtk_widget_show(notebook
);
980 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(notebook
), FALSE
);
981 gtk_notebook_set_show_border(GTK_NOTEBOOK(notebook
), FALSE
);
983 gtk_box_pack_start (GTK_BOX (vbox
), notebook
, TRUE
, TRUE
, 0);
986 vbox
= gtk_box_new (GTK_ORIENTATION_VERTICAL
, 0);
987 gtk_notebook_append_page(GTK_NOTEBOOK(notebook
), vbox
, NULL
);
989 widget
= gtk_scrolled_window_new (NULL
, NULL
);
990 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (widget
), GTK_SHADOW_ETCHED_IN
);
991 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (widget
), GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
993 treeview
= create_list_repbalance();
994 data
->LV_report
= treeview
;
995 gtk_container_add (GTK_CONTAINER(widget
), treeview
);
996 //gtk_notebook_append_page(GTK_NOTEBOOK(notebook), widget, NULL);
997 gtk_box_pack_start (GTK_BOX (vbox
), widget
, TRUE
, TRUE
, 0);
1000 widget
= gtk_scrolled_window_new (NULL
, NULL
);
1001 data
->GR_detail
= widget
;
1002 //gtk_scrolled_window_set_placement(GTK_SCROLLED_WINDOW (widget), GTK_CORNER_TOP_RIGHT);
1003 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (widget
), GTK_SHADOW_ETCHED_IN
);
1004 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (widget
), GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
1005 treeview
= create_list_transaction(LIST_TXN_TYPE_DETAIL
, PREFS
->lst_ope_columns
);
1006 data
->LV_detail
= treeview
;
1007 gtk_container_add (GTK_CONTAINER(widget
), treeview
);
1009 gtk_box_pack_start (GTK_BOX (vbox
), widget
, TRUE
, TRUE
, 0);
1013 widget
= gtk_chart_new(CHART_TYPE_LINE
);
1014 data
->RE_line
= widget
;
1015 //gtk_chart_set_minor_prefs(GTK_CHART(widget), PREFS->euro_value, PREFS->minor_cur.suffix_symbol);
1016 gtk_notebook_append_page(GTK_NOTEBOOK(notebook
), widget
, NULL
);
1021 //todo:should move this
1022 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(data
->CM_minor
),GLOBALS
->minor
);
1026 /* attach our minor to treeview */
1027 g_object_set_data(G_OBJECT(gtk_tree_view_get_model(GTK_TREE_VIEW(data
->LV_report
))), "minor", (gpointer
)data
->CM_minor
);
1033 /* signal connect */
1034 g_signal_connect (window
, "delete-event", G_CALLBACK (repbalance_window_dispose
), (gpointer
)data
);
1036 g_signal_connect (data
->CM_minor
, "toggled", G_CALLBACK (repbalance_toggle_minor
), NULL
);
1039 data
->handler_id
[HID_REPBALANCE_MINDATE
] = g_signal_connect (data
->PO_mindate
, "changed", G_CALLBACK (repbalance_date_change
), (gpointer
)data
);
1040 data
->handler_id
[HID_REPBALANCE_MAXDATE
] = g_signal_connect (data
->PO_maxdate
, "changed", G_CALLBACK (repbalance_date_change
), (gpointer
)data
);
1042 data
->handler_id
[HID_REPBALANCE_RANGE
] = g_signal_connect (data
->CY_range
, "changed", G_CALLBACK (repbalance_range_change
), NULL
);
1044 g_signal_connect (gtk_tree_view_get_selection(GTK_TREE_VIEW(data
->LV_report
)), "changed", G_CALLBACK (repbalance_selection
), NULL
);
1046 //setup, init and show window
1047 repbalance_setup(data
, accnum
);
1049 g_signal_connect (data
->CM_selectall
, "toggled", G_CALLBACK (repbalance_toggle_selectall
), NULL
);
1050 g_signal_connect (data
->CM_eachday
, "toggled", G_CALLBACK (repbalance_compute
), NULL
);
1053 //let this here or the setup trigger a compute...
1054 g_signal_connect (data
->PO_acc
, "changed", G_CALLBACK (repbalance_compute
), NULL
);
1056 g_signal_connect (data
->RG_zoomx
, "value-changed", G_CALLBACK (repbalance_zoomx_callback
), NULL
);
1058 g_signal_connect (GTK_TREE_VIEW(data
->LV_detail
), "row-activated", G_CALLBACK (repbalance_detail_onRowActivated
), NULL
);
1062 if(PREFS
->toolbar_style
== 0)
1063 gtk_toolbar_unset_style(GTK_TOOLBAR(data
->TB_bar
));
1065 gtk_toolbar_set_style(GTK_TOOLBAR(data
->TB_bar
), PREFS
->toolbar_style
-1);
1068 //setup, init and show window
1069 wg
= &PREFS
->ove_wg
;
1070 gtk_window_move(GTK_WINDOW(window
), wg
->l
, wg
->t
);
1071 gtk_window_resize(GTK_WINDOW(window
), wg
->w
, wg
->h
);
1075 gtk_widget_show_all (window
);
1078 if( PREFS
->euro_active
)
1079 gtk_widget_show(data
->CM_minor
);
1081 gtk_widget_hide(data
->CM_minor
);
1083 repbalance_sensitive(window
, NULL
);
1084 repbalance_update_detail(window
, NULL
);
1086 gtk_combo_box_set_active(GTK_COMBO_BOX(data
->CY_range
), PREFS
->date_range_rep
);
1093 ** ============================================================================
1097 static void repbalance_date_cell_data_function (GtkTreeViewColumn
*col
, GtkCellRenderer
*renderer
, GtkTreeModel
*model
, GtkTreeIter
*iter
, gpointer user_data
)
1100 gboolean is_balance
;
1104 gtk_tree_model_get(model
, iter
,
1105 LST_OVER_DATESTR
, &datestr
,
1106 LST_OVER_OVER
, &is_balance
,
1110 weight
= PANGO_WEIGHT_NORMAL
;
1112 if(is_balance
==TRUE
)
1114 if(PREFS
->custom_colors
== TRUE
)
1115 color
= PREFS
->color_warn
;
1117 weight
= PANGO_WEIGHT_BOLD
;
1120 g_object_set(renderer
,
1122 "foreground", color
,
1128 static void repbalance_amount_cell_data_function (GtkTreeViewColumn
*col
,
1129 GtkCellRenderer
*renderer
,
1130 GtkTreeModel
*model
,
1135 gchar buf
[G_ASCII_DTOSTR_BUF_SIZE
];
1136 gboolean is_balance
;
1139 guint32 kcur
= GPOINTER_TO_UINT(g_object_get_data(G_OBJECT(gtk_tree_view_column_get_tree_view(col
)), "kcur_data"));
1143 gtk_tree_model_get(model
, iter
,
1144 LST_OVER_OVER
, &is_balance
,
1145 GPOINTER_TO_INT(user_data
), &value
,
1150 g_object_set(renderer
, "text", NULL
, NULL
);
1153 hb_strfmon(buf
, G_ASCII_DTOSTR_BUF_SIZE
-1, value
, kcur
, GLOBALS
->minor
);
1156 weight
= PANGO_WEIGHT_NORMAL
;
1159 if(value
!= 0.0 && PREFS
->custom_colors
== TRUE
)
1160 color
= (value
> 0.0) ? PREFS
->color_inc
: PREFS
->color_exp
;
1162 if(is_balance
==TRUE
)
1164 if(PREFS
->custom_colors
== TRUE
)
1165 color
= PREFS
->color_warn
;
1167 weight
= PANGO_WEIGHT_BOLD
;
1170 g_object_set(renderer
,
1172 "foreground", color
,
1179 static GtkTreeViewColumn
*amount_list_repbalance_column(gchar
*name
, gint id
)
1181 GtkTreeViewColumn
*column
;
1182 GtkCellRenderer
*renderer
;
1184 column
= gtk_tree_view_column_new();
1185 gtk_tree_view_column_set_title(column
, name
);
1186 renderer
= gtk_cell_renderer_text_new ();
1187 g_object_set(renderer
, "xalign", 1.0, NULL
);
1188 gtk_tree_view_column_pack_start(column
, renderer
, TRUE
);
1189 gtk_tree_view_column_set_cell_data_func(column
, renderer
, repbalance_amount_cell_data_function
, GINT_TO_POINTER(id
), NULL
);
1190 gtk_tree_view_column_set_alignment (column
, 0.5);
1191 //gtk_tree_view_column_set_sort_column_id (column, id);
1196 static void ui_repbalance_list_set_cur(GtkTreeView
*treeview
, guint32 kcur
)
1198 g_object_set_data(G_OBJECT(treeview
), "kcur_data", GUINT_TO_POINTER(kcur
));
1203 ** create our statistic list
1205 static GtkWidget
*create_list_repbalance(void)
1207 GtkListStore
*store
;
1209 GtkCellRenderer
*renderer
;
1210 GtkTreeViewColumn
*column
;
1212 /* create list store */
1213 store
= gtk_list_store_new(
1224 view
= gtk_tree_view_new_with_model(GTK_TREE_MODEL(store
));
1225 g_object_unref(store
);
1227 gtk_tree_view_set_grid_lines (GTK_TREE_VIEW (view
), PREFS
->grid_lines
);
1229 /* column debug balance */
1231 column = gtk_tree_view_column_new();
1232 gtk_tree_view_column_set_title(column, "debug balance");
1233 gtk_tree_view_append_column (GTK_TREE_VIEW(view), column);
1234 renderer = gtk_cell_renderer_text_new();
1235 gtk_tree_view_column_pack_start(column, renderer, TRUE);
1236 gtk_tree_view_column_add_attribute(column, renderer, "text", LST_OVER_OVER);
1240 column
= gtk_tree_view_column_new();
1241 gtk_tree_view_column_set_title(column
, _("Date"));
1242 gtk_tree_view_append_column (GTK_TREE_VIEW(view
), column
);
1243 renderer
= gtk_cell_renderer_text_new();
1244 g_object_set(renderer
, "xalign", 1.0, NULL
);
1245 gtk_tree_view_column_pack_start(column
, renderer
, TRUE
);
1246 //gtk_tree_view_column_add_attribute(column, renderer, "text", LST_OVER_DATE);
1247 gtk_tree_view_column_set_alignment (column
, 0.5);
1248 gtk_tree_view_column_set_cell_data_func(column
, renderer
, repbalance_date_cell_data_function
, NULL
, NULL
);
1251 /* column: Expense */
1252 column
= amount_list_repbalance_column(_("Expense"), LST_OVER_EXPENSE
);
1253 gtk_tree_view_append_column (GTK_TREE_VIEW(view
), column
);
1255 /* column: Income */
1256 column
= amount_list_repbalance_column(_("Income"), LST_OVER_INCOME
);
1257 gtk_tree_view_append_column (GTK_TREE_VIEW(view
), column
);
1259 /* column: Balance */
1260 column
= amount_list_repbalance_column(_("Balance"), LST_OVER_BALANCE
);
1261 gtk_tree_view_append_column (GTK_TREE_VIEW(view
), column
);
1263 /* column last: empty */
1264 column
= gtk_tree_view_column_new();
1265 gtk_tree_view_append_column (GTK_TREE_VIEW(view
), column
);