29#include <glib/gi18n.h>
33#include "gnc-component-manager.h"
37#include "gnc-locale-utils.h"
39#include "window-main-summarybar.h"
40#include "dialog-utils.h"
52 GtkLabel *bar_labels[3];
53 GtkListStore *datamodel;
58 gboolean show_negative_color;
59 gchar *negative_color;
62#define WINDOW_SUMMARYBAR_CM_CLASS "summary-bar"
64#define GNC_PREFS_GROUP "window.pages.account-tree.summary"
65#define GNC_PREF_GRAND_TOTAL "grand-total"
66#define GNC_PREF_NON_CURRENCY "non-currency"
81 gnc_commodity * currency;
90#define TOTAL_CURR_TOTAL 1
91#define TOTAL_NON_CURR_TOTAL 2
92#define TOTAL_GRAND_TOTAL 3
98 gnc_commodity *default_currency;
100 gboolean non_currency;
110gnc_ui_get_currency_accumulator(GList **list, gnc_commodity * currency, gint total_mode)
115 for (current = g_list_first(*list); current; current = g_list_next(current))
117 found = current->data;
119 && (found->total_mode == total_mode))
126 found->currency = currency;
127 found->assets = gnc_numeric_zero ();
128 found->profits = gnc_numeric_zero ();
129 found->total_mode = total_mode;
130 *list = g_list_append (*list, found);
139gnc_ui_accounts_recurse (
Account *parent, GList **currency_list,
142 gnc_numeric start_amount;
143 gnc_numeric start_amount_default_currency;
144 gnc_numeric end_amount;
145 gnc_numeric end_amount_default_currency;
147 gnc_commodity * account_currency;
151 GList *children, *node;
153 if (parent == NULL)
return;
156 for (node = children; node; node = g_list_next(node))
159 QofBook *book = gnc_account_get_book (account);
161 gnc_commodity *to_curr = options.default_currency;
162 gboolean non_currency = FALSE;
167 if (options.grand_total)
168 grand_total_accum = gnc_ui_get_currency_accumulator(currency_list,
175 non_curr_accum = gnc_ui_get_currency_accumulator(currency_list,
177 TOTAL_NON_CURR_TOTAL);
180 if (!non_currency || options.non_currency)
182 currency_accum = gnc_ui_get_currency_accumulator(currency_list,
187 switch (account_type)
199 end_amount_default_currency =
206 if (!non_currency || options.non_currency)
208 currency_accum->assets =
216 non_curr_accum->assets =
222 if (options.grand_total)
224 grand_total_accum->assets =
225 gnc_numeric_add (grand_total_accum->assets, end_amount_default_currency,
230 gnc_ui_accounts_recurse(account, currency_list, options);
235 start_amount_default_currency =
242 end_amount_default_currency =
249 if (!non_currency || options.non_currency)
251 currency_accum->profits =
255 currency_accum->profits =
263 non_curr_accum->profits =
264 gnc_numeric_add (non_curr_accum->profits, start_amount_default_currency,
267 non_curr_accum->profits =
273 if (options.grand_total)
275 grand_total_accum->profits =
277 start_amount_default_currency,
280 grand_total_accum->profits =
282 end_amount_default_currency,
287 gnc_ui_accounts_recurse(account, currency_list, options);
303 g_list_free(children);
311 if (mnemonic == NULL)
314 switch (currency_accum->total_mode)
316 case TOTAL_CURR_TOTAL:
317 label_str = g_strdup_printf(
_(
"%s, Total:"), mnemonic );
319 case TOTAL_NON_CURR_TOTAL:
320 label_str = g_strdup_printf(
_(
"%s, Non Currency Commodities Total:"), mnemonic );
322 case TOTAL_GRAND_TOTAL:
323 label_str = g_strdup_printf(
_(
"%s, Grand Total:"), mnemonic );
327 label_str = g_strdup_printf(
_(
"%s:"), mnemonic );
336 COLUMN_MNEMONIC_TYPE,
340 COLUMN_PROFITS_VALUE,
370 GList *currency_list;
375 root = gnc_get_current_root_account ();
377 if (options.default_currency == NULL)
382 options.grand_total =
384 options.non_currency =
386 options.start_date = gnc_accounting_period_fiscal_start();
387 options.end_date = gnc_accounting_period_fiscal_end();
389 currency_list = NULL;
392 if (options.grand_total)
394 gnc_ui_get_currency_accumulator (¤cy_list, options.default_currency,
398 gnc_ui_get_currency_accumulator (¤cy_list, options.default_currency,
401 gnc_ui_accounts_recurse(root, ¤cy_list, options);
405 char asset_amount_string[256], profit_amount_string[256];
407 gtk_list_store_clear(summary->datamodel);
408 for (current = g_list_first(currency_list); current; current = g_list_next(current))
410 gchar *total_mode_label;
411 gchar *bidi_total, *bidi_asset_amount, *bidi_profit_amount;
413 currency_accum = current->data;
416 currency_accum->assets,
417 gnc_commodity_print_info(currency_accum->currency, TRUE));
420 currency_accum->profits,
421 gnc_commodity_print_info(currency_accum->currency, TRUE));
423 gtk_list_store_append(summary->datamodel, &iter);
424 total_mode_label = get_total_mode_label (currency_accum);
430 gchar *row_key = g_strdup_printf
431 (
"%d:%s", currency_accum->total_mode,
433 gtk_list_store_set(summary->datamodel, &iter,
435 COLUMN_MNEMONIC_TYPE, bidi_total,
436 COLUMN_ASSETS,
_(
"Net Assets:"),
437 COLUMN_ASSETS_VALUE, bidi_asset_amount,
439 COLUMN_PROFITS,
_(
"Profits:"),
440 COLUMN_PROFITS_VALUE, bidi_profit_amount,
444 g_free(total_mode_label);
446 g_free(bidi_asset_amount);
447 g_free(bidi_profit_amount);
452 summary_update_bar (summary);
455 g_list_free_full (currency_list, g_free);
459get_negative_color_str (
void)
463 GtkWidget *label = gtk_label_new (
"Color");
464 GtkStyleContext *context = gtk_widget_get_style_context (GTK_WIDGET(label));
465 g_object_ref_sink (label);
466 gtk_style_context_add_class (context,
"gnc-class-negative-numbers");
467 gtk_style_context_get_color (context, GTK_STATE_FLAG_NORMAL, &color);
468 color_str = g_strdup_printf (
"#%02X%02X%02X",
469 (
int)(0.5 + CLAMP (color.red, 0., 1.) * 255.),
470 (
int)(0.5 + CLAMP (color.green, 0., 1.) * 255.),
471 (
int)(0.5 + CLAMP (color.blue, 0., 1.) * 255.));
472 g_object_unref (label);
477summarybar_update_color (gpointer gsettings, gchar *key, gpointer user_data)
481 g_free(summary->negative_color);
482 summary->negative_color = get_negative_color_str();
483 summary->show_negative_color =
gnc_prefs_get_bool (GNC_PREFS_GROUP_GENERAL, GNC_PREF_NEGATIVE_IN_RED);
485 gnc_main_window_summary_refresh (summary);
489gnc_main_window_summary_destroy_cb(
GNCMainSummary *summary, gpointer data)
492 gnc_unregister_gui_component(summary->component_id);
495 summarybar_update_color, summary);
499 gtk_widget_destroy (summary->menu);
500 g_object_unref (summary->menu);
502 g_object_unref (summary->datamodel);
504 g_free (summary->active_key);
505 g_free (summary->negative_color);
510summarybar_refresh_handler(GHashTable * changes, gpointer user_data)
513 gnc_main_window_summary_refresh(summary);
517prefs_changed_cb (gpointer prefs, gchar *pref, gpointer user_data)
520 gnc_main_window_summary_refresh(summary);
525make_markup (GtkTreeModel *model, GtkTreeIter *iter,
527 gint label_col, gint value_col, gint neg_col)
529 gchar *label, *value;
531 gtk_tree_model_get (model, iter, label_col, &label, value_col, &value, neg_col, &neg,
533 gchar* markup = summary->show_negative_color && neg
534 ? g_markup_printf_escaped (
"%s <span foreground='%s'>%s</span>",
535 label, summary->negative_color, value)
536 : g_markup_printf_escaped (
"%s %s", label, value);
545fill_row_labels (
const GNCMainSummary *summary, GtkTreeModel *model,
546 GtkTreeIter *iter, GtkLabel *labels[3])
549 gtk_tree_model_get (model, iter, COLUMN_MNEMONIC_TYPE, &type, -1);
550 gtk_label_set_text (labels[0], type);
553 gchar* markup = make_markup (model, iter, summary,
554 COLUMN_ASSETS, COLUMN_ASSETS_VALUE, COLUMN_ASSETS_NEG);
555 gtk_label_set_markup (labels[1], markup);
558 markup = make_markup (model, iter, summary,
559 COLUMN_PROFITS, COLUMN_PROFITS_VALUE, COLUMN_PROFITS_NEG);
560 gtk_label_set_markup (labels[2], markup);
567summary_find_row_by_key (GtkTreeModel *model,
const gchar *key,
570 if (!key || !gtk_tree_model_get_iter_first (model, iter))
575 gchar *row_key = NULL;
576 gtk_tree_model_get (model, iter, COLUMN_KEY, &row_key, -1);
577 gboolean match = row_key && g_strcmp0 (row_key, key) == 0;
582 while (gtk_tree_model_iter_next (model, iter));
594 GtkTreeModel *model = GTK_TREE_MODEL (summary->datamodel);
597 if (!summary_find_row_by_key (model, summary->active_key, &iter))
599 g_free (summary->active_key);
600 summary->active_key = NULL;
602 if (!gtk_tree_model_get_iter_first (model, &iter))
604 for (
int i = 0; i < 3; i++)
605 gtk_label_set_text (summary->bar_labels[i],
"");
608 gtk_tree_model_get (model, &iter, COLUMN_KEY, &summary->active_key, -1);
611 fill_row_labels (summary, model, &iter, summary->bar_labels);
617summary_menu_item_activate_cb (GtkMenuItem *item, gpointer user_data)
620 const gchar *key = g_object_get_data (G_OBJECT (item),
"summary-key");
625 g_free (summary->active_key);
626 summary->active_key = g_strdup (key);
627 summary_update_bar (summary);
631summary_menu_remove_child_cb (GtkWidget *child, gpointer user_data)
633 gtk_widget_destroy (child);
641 GtkTreeModel *model = GTK_TREE_MODEL (summary->datamodel);
642 GtkWidget *active_item = NULL;
644 gtk_container_foreach (GTK_CONTAINER (summary->menu),
645 summary_menu_remove_child_cb, NULL);
648 if (!gtk_tree_model_get_iter_first (model, &iter))
653 GtkWidget *item = gtk_menu_item_new ();
654 GtkWidget *box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 5);
659 gtk_box_set_homogeneous (GTK_BOX (box), TRUE);
661 for (
int i = 0; i < 3; i++)
663 labels[i] = GTK_LABEL (gtk_label_new (NULL));
664 gtk_label_set_xalign (labels[i], 0.0);
665 gtk_box_pack_start (GTK_BOX (box), GTK_WIDGET (labels[i]),
668 fill_row_labels (summary, model, &iter, labels);
670 gtk_container_add (GTK_CONTAINER (item), box);
671 gtk_tree_model_get (model, &iter, COLUMN_KEY, &key, -1);
672 if (g_strcmp0 (key, summary->active_key) == 0)
675 g_object_set_data_full (G_OBJECT (item),
"summary-key", key, g_free);
677 g_signal_connect (item,
"activate",
678 G_CALLBACK (summary_menu_item_activate_cb), summary);
679 gtk_menu_shell_append (GTK_MENU_SHELL (summary->menu), item);
681 while (gtk_tree_model_iter_next (model, &iter));
687summary_popup_menu (
GNCMainSummary *summary,
const GdkEvent *trigger)
689 GtkWidget *active_item = summary_menu_populate (summary);
692 gtk_widget_set_size_request (summary->menu,
693 gtk_widget_get_allocated_width (summary->button),
695 gtk_widget_show_all (summary->menu);
700 gtk_menu_popup_at_widget (GTK_MENU (summary->menu), summary->button,
701 GDK_GRAVITY_NORTH_WEST,
702 GDK_GRAVITY_SOUTH_WEST,
706 gtk_menu_shell_select_item (GTK_MENU_SHELL (summary->menu), active_item);
712summary_button_press_cb (GtkWidget *widget, GdkEventButton *event,
715 if (event->type != GDK_BUTTON_PRESS || event->button != GDK_BUTTON_PRIMARY)
718 summary_popup_menu (user_data, (
const GdkEvent *) event);
724summary_button_clicked_cb (GtkButton *button, gpointer user_data)
728 if (!gtk_widget_get_visible (summary->menu))
729 summary_popup_menu (summary, NULL);
732typedef gboolean (*SummaryStepFn) (GtkTreeModel *, GtkTreeIter *);
735summary_iter_last (GtkTreeModel *model, GtkTreeIter *iter)
737 int n = gtk_tree_model_iter_n_children (model, NULL);
738 return n > 0 && gtk_tree_model_iter_nth_child (model, iter, NULL, n - 1);
742summary_button_key_press_cb (GtkWidget *widget, GdkEventKey *event, gpointer user_data)
745 GtkTreeModel *model = GTK_TREE_MODEL (summary->datamodel);
748 if (event->state & gtk_accelerator_get_default_mod_mask ())
751 switch (event->keyval)
755 fn = gtk_tree_model_iter_previous;
758 case GDK_KEY_KP_Down:
759 fn = gtk_tree_model_iter_next;
762 case GDK_KEY_KP_Home:
763 fn = gtk_tree_model_get_iter_first;
767 fn = summary_iter_last;
774 if (!summary_find_row_by_key (model, summary->active_key, &iter) || !fn (model, &iter))
777 g_free (summary->active_key);
778 gtk_tree_model_get (model, &iter, COLUMN_KEY, &summary->active_key, -1);
779 summary_update_bar (summary);
784gnc_main_window_summary_new (
void)
789 retval->datamodel = gtk_list_store_new (N_COLUMNS,
799 retval->hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 5);
800 gtk_box_set_homogeneous (GTK_BOX (retval->hbox), FALSE);
803 gtk_widget_set_name (GTK_WIDGET(retval->hbox),
"gnc-id-account-summary-bar");
805 retval->negative_color = get_negative_color_str();
806 retval->show_negative_color =
gnc_prefs_get_bool (GNC_PREFS_GROUP_GENERAL, GNC_PREF_NEGATIVE_IN_RED);
808 summarybar_update_color, retval);
810 retval->component_id = gnc_register_gui_component (WINDOW_SUMMARYBAR_CM_CLASS,
811 summarybar_refresh_handler,
813 gnc_gui_component_watch_entity_type (retval->component_id,
820 | GNC_EVENT_ITEM_REMOVED
821 | GNC_EVENT_ITEM_CHANGED);
824 retval->button = gtk_button_new ();
825 GtkWidget *inner = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 5);
826 gtk_box_set_homogeneous (GTK_BOX (inner), TRUE);
828 for (i = 0; i < 3; i++)
830 retval->bar_labels[i] = GTK_LABEL (gtk_label_new (NULL));
831 gtk_label_set_xalign (retval->bar_labels[i], 0.5);
832 gtk_box_pack_start (GTK_BOX (inner), GTK_WIDGET (retval->bar_labels[i]),
836 GtkWidget *outer = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 5);
837 GtkWidget *arrow = gtk_image_new_from_icon_name (
"pan-up-symbolic",
838 GTK_ICON_SIZE_BUTTON);
839 gtk_box_pack_start (GTK_BOX (outer), inner, TRUE, TRUE, 0);
840 gtk_box_pack_end (GTK_BOX (outer), arrow, FALSE, FALSE, 0);
841 gtk_container_add (GTK_CONTAINER (retval->button), outer);
844 retval->menu = g_object_ref_sink (gtk_menu_new ());
845 g_signal_connect (retval->button,
"button-press-event",
846 G_CALLBACK (summary_button_press_cb), retval);
847 g_signal_connect (retval->button,
"clicked",
848 G_CALLBACK (summary_button_clicked_cb), retval);
849 g_signal_connect (retval->button,
"key-press-event",
850 G_CALLBACK (summary_button_key_press_cb), retval);
851 gtk_container_set_border_width (GTK_CONTAINER (retval->hbox), 2);
852 gtk_box_pack_start (GTK_BOX(retval->hbox), retval->button, TRUE, TRUE, 5);
853 gtk_widget_show_all (retval->button);
854 gtk_widget_show (retval->hbox);
856 g_signal_connect_swapped (G_OBJECT (retval->hbox),
"destroy",
857 G_CALLBACK (gnc_main_window_summary_destroy_cb),
860 gnc_main_window_summary_refresh(retval);
863 prefs_changed_cb, retval);
Account handling public routines.
General utilities for dealing with accounting periods.
Additional event handling code.
Generic api to store and retrieve preferences.
utility functions for the GnuCash UI
GNCAccountType
The account types are used to determine how the transaction data in the account is displayed.
GNCAccountType xaccAccountGetType(const Account *acc)
Returns the account's account type.
gnc_numeric xaccAccountGetBalanceAsOfDate(Account *acc, time64 date)
Get the balance of the account at the end of the day before the date specified.
GList * gnc_account_get_children(const Account *account)
This routine returns a GList of all children accounts of the specified account.
gnc_commodity * xaccAccountGetCommodity(const Account *acc)
Get the account's commodity.
@ ACCT_TYPE_INCOME
Income accounts are used to denote income.
@ ACCT_TYPE_MUTUAL
Mutual Fund accounts will typically be shown in registers which show three columns: price,...
@ ACCT_TYPE_TRADING
Account used to record multiple commodity transactions.
@ ACCT_TYPE_EXPENSE
Expense accounts are used to denote expenses.
@ ACCT_TYPE_BANK
The bank account type denotes a savings or checking account held at a bank.
@ ACCT_TYPE_PAYABLE
A/P account type.
@ ACCT_TYPE_ASSET
asset (and liability) accounts indicate generic, generalized accounts that are none of the above.
@ ACCT_TYPE_CURRENCY
The currency account type indicates that the account is a currency trading account.
@ ACCT_TYPE_RECEIVABLE
A/R account type.
@ ACCT_TYPE_CASH
The cash account type is used to denote a shoe-box or pillowcase stuffed with * cash.
@ ACCT_TYPE_LIABILITY
liability (and asset) accounts indicate generic, generalized accounts that are none of the above.
@ ACCT_TYPE_EQUITY
Equity account is used to balance the balance sheet.
@ ACCT_TYPE_CREDIT
The Credit card account is used to denote credit (e.g.
@ ACCT_TYPE_STOCK
Stock accounts will typically be shown in registers which show three columns: price,...
gboolean gnc_commodity_equiv(const gnc_commodity *a, const gnc_commodity *b)
This routine returns TRUE if the two commodities are equivalent.
int gnc_commodity_get_fraction(const gnc_commodity *cm)
Retrieve the fraction for the specified commodity.
const char * gnc_commodity_get_nice_symbol(const gnc_commodity *cm)
Retrieve a symbol for the specified commodity, suitable for display to the user.
const char * gnc_commodity_get_unique_name(const gnc_commodity *cm)
Retrieve the 'unique' name for the specified commodity.
gboolean gnc_commodity_is_currency(const gnc_commodity *cm)
Checks to see if the specified commodity is an ISO 4217 recognized currency or a legacy currency.
gint64 time64
Most systems that are currently maintained, including Microsoft Windows, BSD-derived Unixes and Linux...
#define GNC_EVENT_ITEM_ADDED
These events are used when a split is added to an account.
int xaccSPrintAmount(char *bufp, gnc_numeric val, GNCPrintAmountInfo info)
Make a string representation of a gnc_numeric.
char * gnc_wrap_text_with_bidi_ltr_isolate(const char *text)
This function helps with GTK's use of 'Unicode Bidirectional Text Algorithm'.
gnc_commodity * gnc_default_currency(void)
Return the default currency set by the user.
gnc_numeric gnc_numeric_sub(gnc_numeric a, gnc_numeric b, gint64 denom, gint how)
Return a-b.
gboolean gnc_numeric_negative_p(gnc_numeric a)
Returns 1 if a < 0, otherwise returns 0.
gnc_numeric gnc_numeric_add(gnc_numeric a, gnc_numeric b, gint64 denom, gint how)
Return a+b.
@ GNC_HOW_RND_ROUND_HALF_UP
Round to the nearest integer, rounding away from zero when there are two equidistant nearest integers...
void gnc_prefs_remove_cb_by_func(const gchar *group, const gchar *pref_name, gpointer func, gpointer user_data)
Remove a function that was registered for a callback when the given preference changed.
gulong gnc_prefs_register_cb(const char *group, const gchar *pref_name, gpointer func, gpointer user_data)
Register a callback that gets triggered when the given preference changes.
void gnc_prefs_remove_cb_by_id(const gchar *group, guint id)
Remove a function that was registered for a callback when a specific preference in the settings group...
gboolean gnc_prefs_get_bool(const gchar *group, const gchar *pref_name)
Get a boolean value from the preferences backend.
gnc_numeric gnc_pricedb_convert_balance_nearest_price_t64(GNCPriceDB *pdb, gnc_numeric balance, const gnc_commodity *balance_currency, const gnc_commodity *new_currency, time64 t)
Convert a balance from one currency to another using the price nearest to the given time.
GNCPriceDB * gnc_pricedb_get_db(QofBook *book)
Return the pricedb associated with the book.
An accumulator for a given currency.