GnuCash 7574033+
Loading...
Searching...
No Matches
window-main-summarybar.c
1/********************************************************************
2 * window-main-summarybar.c -- summary of financial info *
3 * Copyright (C) 1998,1999 Jeremy Collins *
4 * Copyright (C) 1998,1999,2000 Linas Vepstas *
5 * Copyright (C) 2001 Bill Gribble *
6 * Copyright (C) 2005 Joshua Sled <jsled@asynchronous.org> *
7 * *
8 * This program is free software; you can redistribute it and/or *
9 * modify it under the terms of the GNU General Public License as *
10 * published by the Free Software Foundation; either version 2 of *
11 * the License, or (at your option) any later version. *
12 * *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU General Public License*
19 * along with this program; if not, contact: *
20 * *
21 * Free Software Foundation Voice: +1-617-542-5942 *
22 * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
23 * Boston, MA 02110-1301, USA gnu@gnu.org *
24 ********************************************************************/
25
26#include <config.h>
27
28#include <gtk/gtk.h>
29#include <glib/gi18n.h>
30
31#include "Account.h"
33#include "gnc-component-manager.h"
34#include "gnc-euro.h"
35#include "gnc-event.h"
36#include "gnc-prefs.h"
37#include "gnc-locale-utils.h"
38#include "gnc-ui-util.h"
39#include "window-main-summarybar.h"
40#include "dialog-utils.h"
41
42/* The summary bar is a button showing the currently selected row of the
43 * datamodel (mnemonic, net assets, profits). Clicking it pops up a
44 * GtkMenu listing every row, anchored so that the menu's bottom edge
45 * sits on the top edge of the bar (the bar lives at the bottom of the
46 * main window, so there is never room below it), with the current row
47 * pre-selected. */
48typedef struct
49{
50 GtkWidget *hbox;
51 GtkWidget *button;
52 GtkLabel *bar_labels[3];
53 GtkListStore *datamodel;
54 GtkWidget *menu;
55 gchar *active_key; /* COLUMN_KEY of the row shown in the bar */
56 int component_id;
57 int cnxn_id;
58 gboolean show_negative_color;
59 gchar *negative_color;
61
62#define WINDOW_SUMMARYBAR_CM_CLASS "summary-bar"
63
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"
67
79typedef struct
80{
81 gnc_commodity * currency;
82 gnc_numeric assets;
83 gnc_numeric profits;
84 gint total_mode;
86
87
88/* defines for total_mode in GNCCurrencyAcc and GNCCurrencyItem */
89#define TOTAL_SINGLE 0
90#define TOTAL_CURR_TOTAL 1
91#define TOTAL_NON_CURR_TOTAL 2
92#define TOTAL_GRAND_TOTAL 3
93
94
96typedef struct
97{
98 gnc_commodity *default_currency;
99 gboolean grand_total;
100 gboolean non_currency;
101 time64 start_date;
102 time64 end_date;
104
109static GNCCurrencyAcc *
110gnc_ui_get_currency_accumulator(GList **list, gnc_commodity * currency, gint total_mode)
111{
112 GList *current;
113 GNCCurrencyAcc *found;
114
115 for (current = g_list_first(*list); current; current = g_list_next(current))
116 {
117 found = current->data;
118 if ((gnc_commodity_equiv(currency, found->currency))
119 && (found->total_mode == total_mode))
120 {
121 return found;
122 }
123 }
124
125 found = g_new0 (GNCCurrencyAcc, 1);
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);
131
132 return found;
133}
134
138static void
139gnc_ui_accounts_recurse (Account *parent, GList **currency_list,
140 GNCSummarybarOptions options)
141{
142 gnc_numeric start_amount;
143 gnc_numeric start_amount_default_currency;
144 gnc_numeric end_amount;
145 gnc_numeric end_amount_default_currency;
146 GNCAccountType account_type;
147 gnc_commodity * account_currency;
148 GNCCurrencyAcc *currency_accum = NULL;
149 GNCCurrencyAcc *grand_total_accum = NULL;
150 GNCCurrencyAcc *non_curr_accum = NULL;
151 GList *children, *node;
152
153 if (parent == NULL) return;
154
155 children = gnc_account_get_children(parent);
156 for (node = children; node; node = g_list_next(node))
157 {
158 Account *account = node->data;
159 QofBook *book = gnc_account_get_book (account);
160 GNCPriceDB *pricedb = gnc_pricedb_get_db (book);
161 gnc_commodity *to_curr = options.default_currency;
162 gboolean non_currency = FALSE;
163
164 account_type = xaccAccountGetType(account);
165 account_currency = xaccAccountGetCommodity(account);
166
167 if (options.grand_total)
168 grand_total_accum = gnc_ui_get_currency_accumulator(currency_list,
169 to_curr,
170 TOTAL_GRAND_TOTAL);
171
172 if (!gnc_commodity_is_currency(account_currency))
173 {
174 non_currency = TRUE;
175 non_curr_accum = gnc_ui_get_currency_accumulator(currency_list,
176 to_curr,
177 TOTAL_NON_CURR_TOTAL);
178 }
179
180 if (!non_currency || options.non_currency)
181 {
182 currency_accum = gnc_ui_get_currency_accumulator(currency_list,
183 account_currency,
184 TOTAL_SINGLE);
185 }
186
187 switch (account_type)
188 {
189 case ACCT_TYPE_BANK:
190 case ACCT_TYPE_CASH:
191 case ACCT_TYPE_ASSET:
192 case ACCT_TYPE_STOCK:
193 case ACCT_TYPE_MUTUAL:
194 case ACCT_TYPE_CREDIT:
198 end_amount = xaccAccountGetBalanceAsOfDate(account, options.end_date);
199 end_amount_default_currency =
201 end_amount,
202 account_currency,
203 to_curr,
204 options.end_date);
205
206 if (!non_currency || options.non_currency)
207 {
208 currency_accum->assets =
209 gnc_numeric_add (currency_accum->assets, end_amount,
210 gnc_commodity_get_fraction (account_currency),
212 }
213
214 if (non_currency)
215 {
216 non_curr_accum->assets =
217 gnc_numeric_add (non_curr_accum->assets, end_amount_default_currency,
220 }
221
222 if (options.grand_total)
223 {
224 grand_total_accum->assets =
225 gnc_numeric_add (grand_total_accum->assets, end_amount_default_currency,
228 }
229
230 gnc_ui_accounts_recurse(account, currency_list, options);
231 break;
232 case ACCT_TYPE_INCOME:
234 start_amount = xaccAccountGetBalanceAsOfDate(account, options.start_date);
235 start_amount_default_currency =
237 start_amount,
238 account_currency,
239 to_curr,
240 options.start_date);
241 end_amount = xaccAccountGetBalanceAsOfDate(account, options.end_date);
242 end_amount_default_currency =
244 end_amount,
245 account_currency,
246 to_curr,
247 options.end_date);
248
249 if (!non_currency || options.non_currency)
250 {
251 currency_accum->profits =
252 gnc_numeric_add (currency_accum->profits, start_amount,
253 gnc_commodity_get_fraction (account_currency),
255 currency_accum->profits =
256 gnc_numeric_sub (currency_accum->profits, end_amount,
257 gnc_commodity_get_fraction (account_currency),
259 }
260
261 if (non_currency)
262 {
263 non_curr_accum->profits =
264 gnc_numeric_add (non_curr_accum->profits, start_amount_default_currency,
267 non_curr_accum->profits =
268 gnc_numeric_sub (non_curr_accum->profits, end_amount_default_currency,
271 }
272
273 if (options.grand_total)
274 {
275 grand_total_accum->profits =
276 gnc_numeric_add (grand_total_accum->profits,
277 start_amount_default_currency,
280 grand_total_accum->profits =
281 gnc_numeric_sub (grand_total_accum->profits,
282 end_amount_default_currency,
285 }
286
287 gnc_ui_accounts_recurse(account, currency_list, options);
288 break;
289 case ACCT_TYPE_EQUITY:
290 /* no-op, see comments at top about summing assets */
291 break;
297 break;
299 default:
300 break;
301 }
302 }
303 g_list_free(children);
304}
305
306static char*
307get_total_mode_label (GNCCurrencyAcc *currency_accum)
308{
309 const char *mnemonic = gnc_commodity_get_nice_symbol (currency_accum->currency);
310 char *label_str;
311 if (mnemonic == NULL)
312 mnemonic = "";
313 // i.e., "$, grand total," [profits: $12,345.67, assets: $23,456.78]
314 switch (currency_accum->total_mode)
315 {
316 case TOTAL_CURR_TOTAL:
317 label_str = g_strdup_printf( _("%s, Total:"), mnemonic );
318 break;
319 case TOTAL_NON_CURR_TOTAL:
320 label_str = g_strdup_printf( _("%s, Non Currency Commodities Total:"), mnemonic );
321 break;
322 case TOTAL_GRAND_TOTAL:
323 label_str = g_strdup_printf( _("%s, Grand Total:"), mnemonic );
324 break;
325 case TOTAL_SINGLE:
326 default:
327 label_str = g_strdup_printf( _("%s:"), mnemonic );
328 break;
329 }
330 return label_str;
331}
332
333enum
334{
335 COLUMN_KEY, /* stable row id: "<total_mode>:<commodity unique name>" */
336 COLUMN_MNEMONIC_TYPE,
337 COLUMN_ASSETS,
338 COLUMN_ASSETS_VALUE,
339 COLUMN_PROFITS,
340 COLUMN_PROFITS_VALUE,
341 COLUMN_ASSETS_NEG,
342 COLUMN_PROFITS_NEG,
343 N_COLUMNS
344};
345
346static void summary_update_bar (GNCMainSummary *summary);
347
348/* The gnc_main_window_summary_refresh() subroutine redraws summary
349 * information. The statusbar includes two fields, titled 'profits'
350 * and 'assets'. The total assets equal the sum of all of the
351 * non-equity, non-income accounts. In theory, assets also equals the
352 * grand total value of the equity accounts, but that assumes that
353 * folks are using the equity account type correctly (which is not
354 * likely). Thus we show the sum of assets, rather than the sum of
355 * equities.
356 *
357 * The EURO gets special treatment. There can be one line with
358 * EUR amounts and a EUR (total) line which sums up all EURO
359 * member currencies.
360 *
361 * There can be a 'grand total', too, which sums up all accounts
362 * converted to one common currency and a total of all non
363 * currency commodities (e.g. stock, funds). */
364
365static void
366gnc_main_window_summary_refresh (GNCMainSummary * summary)
367{
368 Account *root;
369 GNCCurrencyAcc *currency_accum;
370 GList *currency_list;
371 GList *current;
372 GNCSummarybarOptions options;
373
374
375 root = gnc_get_current_root_account ();
376 options.default_currency = gnc_default_currency ();
377 if (options.default_currency == NULL)
378 {
379 options.default_currency = xaccAccountGetCommodity(root);
380 }
381
382 options.grand_total =
383 gnc_prefs_get_bool(GNC_PREFS_GROUP, GNC_PREF_GRAND_TOTAL);
384 options.non_currency =
385 gnc_prefs_get_bool(GNC_PREFS_GROUP, GNC_PREF_NON_CURRENCY);
386 options.start_date = gnc_accounting_period_fiscal_start();
387 options.end_date = gnc_accounting_period_fiscal_end();
388
389 currency_list = NULL;
390
391 /* grand total should be first in the list */
392 if (options.grand_total)
393 {
394 gnc_ui_get_currency_accumulator (&currency_list, options.default_currency,
395 TOTAL_GRAND_TOTAL);
396 }
397 /* Make sure there's at least one accumulator in the list. */
398 gnc_ui_get_currency_accumulator (&currency_list, options.default_currency,
399 TOTAL_SINGLE);
400
401 gnc_ui_accounts_recurse(root, &currency_list, options);
402
403 {
404 GtkTreeIter iter;
405 char asset_amount_string[256], profit_amount_string[256];
406
407 gtk_list_store_clear(summary->datamodel);
408 for (current = g_list_first(currency_list); current; current = g_list_next(current))
409 {
410 gchar *total_mode_label;
411 gchar *bidi_total, *bidi_asset_amount, *bidi_profit_amount;
412
413 currency_accum = current->data;
414
415 xaccSPrintAmount(asset_amount_string,
416 currency_accum->assets,
417 gnc_commodity_print_info(currency_accum->currency, TRUE));
418
419 xaccSPrintAmount(profit_amount_string,
420 currency_accum->profits,
421 gnc_commodity_print_info(currency_accum->currency, TRUE));
422
423 gtk_list_store_append(summary->datamodel, &iter);
424 total_mode_label = get_total_mode_label (currency_accum);
425 bidi_total = gnc_wrap_text_with_bidi_ltr_isolate(total_mode_label);
426 bidi_asset_amount = gnc_wrap_text_with_bidi_ltr_isolate(asset_amount_string);
427 bidi_profit_amount = gnc_wrap_text_with_bidi_ltr_isolate(profit_amount_string);
428 /* Not the displayed label: two different commodities can
429 * share a symbol (e.g. AUD and USD both show "$"). */
430 gchar *row_key = g_strdup_printf
431 ("%d:%s", currency_accum->total_mode,
432 gnc_commodity_get_unique_name (currency_accum->currency));
433 gtk_list_store_set(summary->datamodel, &iter,
434 COLUMN_KEY, row_key,
435 COLUMN_MNEMONIC_TYPE, bidi_total,
436 COLUMN_ASSETS, _("Net Assets:"),
437 COLUMN_ASSETS_VALUE, bidi_asset_amount,
438 COLUMN_ASSETS_NEG, gnc_numeric_negative_p(currency_accum->assets),
439 COLUMN_PROFITS, _("Profits:"),
440 COLUMN_PROFITS_VALUE, bidi_profit_amount,
441 COLUMN_PROFITS_NEG, gnc_numeric_negative_p(currency_accum->profits),
442 -1);
443 g_free(row_key);
444 g_free(total_mode_label);
445 g_free(bidi_total);
446 g_free(bidi_asset_amount);
447 g_free(bidi_profit_amount);
448 }
449
450 /* The selected row is kept across refreshes by key (falling
451 * back to the first row in summary_update_bar if it is gone). */
452 summary_update_bar (summary);
453 }
454
455 g_list_free_full (currency_list, g_free);
456}
457
458static gchar*
459get_negative_color_str (void)
460{
461 GdkRGBA color;
462 gchar *color_str;
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);
473 return color_str;
474}
475
476static void
477summarybar_update_color (gpointer gsettings, gchar *key, gpointer user_data)
478{
479 GNCMainSummary *summary = user_data;
480
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);
484
485 gnc_main_window_summary_refresh (summary);
486}
487
488static void
489gnc_main_window_summary_destroy_cb(GNCMainSummary *summary, gpointer data)
490{
491 gnc_prefs_remove_cb_by_id (GNC_PREFS_GROUP, summary->cnxn_id);
492 gnc_unregister_gui_component(summary->component_id);
493
494 gnc_prefs_remove_cb_by_func(GNC_PREFS_GROUP_GENERAL, GNC_PREF_NEGATIVE_IN_RED,
495 summarybar_update_color, summary);
496
497 if (summary->menu)
498 {
499 gtk_widget_destroy (summary->menu);
500 g_object_unref (summary->menu);
501 }
502 g_object_unref (summary->datamodel);
503
504 g_free (summary->active_key);
505 g_free (summary->negative_color);
506 g_free (summary);
507}
508
509static void
510summarybar_refresh_handler(GHashTable * changes, gpointer user_data)
511{
512 GNCMainSummary * summary = user_data;
513 gnc_main_window_summary_refresh(summary);
514}
515
516static void
517prefs_changed_cb (gpointer prefs, gchar *pref, gpointer user_data)
518{
519 GNCMainSummary * summary = user_data;
520 gnc_main_window_summary_refresh(summary);
521}
522
523
524static gchar *
525make_markup (GtkTreeModel *model, GtkTreeIter *iter,
526 const GNCMainSummary *summary,
527 gint label_col, gint value_col, gint neg_col)
528{
529 gchar *label, *value;
530 gboolean neg;
531 gtk_tree_model_get (model, iter, label_col, &label, value_col, &value, neg_col, &neg,
532 -1);
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);
537 g_free (label);
538 g_free (value);
539 return markup;
540}
541
542/* Fill three labels (mnemonic/type, net assets, profits) from one row of
543 * the datamodel. Used for both the bar and the popup menu items. */
544static void
545fill_row_labels (const GNCMainSummary *summary, GtkTreeModel *model,
546 GtkTreeIter *iter, GtkLabel *labels[3])
547{
548 gchar *type;
549 gtk_tree_model_get (model, iter, COLUMN_MNEMONIC_TYPE, &type, -1);
550 gtk_label_set_text (labels[0], type);
551 g_free (type);
552
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);
556 g_free (markup);
557
558 markup = make_markup (model, iter, summary,
559 COLUMN_PROFITS, COLUMN_PROFITS_VALUE, COLUMN_PROFITS_NEG);
560 gtk_label_set_markup (labels[2], markup);
561 g_free (markup);
562}
563
564/* Find the row whose COLUMN_KEY equals key. Returns TRUE and sets iter
565 * if found. */
566static gboolean
567summary_find_row_by_key (GtkTreeModel *model, const gchar *key,
568 GtkTreeIter *iter)
569{
570 if (!key || !gtk_tree_model_get_iter_first (model, iter))
571 return FALSE;
572
573 do
574 {
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;
578 g_free (row_key);
579 if (match)
580 return TRUE;
581 }
582 while (gtk_tree_model_iter_next (model, iter));
583
584 return FALSE;
585}
586
587/* Show the active row of the datamodel in the bar itself. The row is
588 * identified by key, so it survives rows being added, removed or
589 * reordered. If the remembered row has vanished (or none was chosen
590 * yet), fall back to the first row and remember that instead. */
591static void
592summary_update_bar (GNCMainSummary *summary)
593{
594 GtkTreeModel *model = GTK_TREE_MODEL (summary->datamodel);
595 GtkTreeIter iter;
596
597 if (!summary_find_row_by_key (model, summary->active_key, &iter))
598 {
599 g_free (summary->active_key);
600 summary->active_key = NULL;
601
602 if (!gtk_tree_model_get_iter_first (model, &iter))
603 {
604 for (int i = 0; i < 3; i++)
605 gtk_label_set_text (summary->bar_labels[i], "");
606 return;
607 }
608 gtk_tree_model_get (model, &iter, COLUMN_KEY, &summary->active_key, -1);
609 }
610
611 fill_row_labels (summary, model, &iter, summary->bar_labels);
612}
613
614
615/* A row of the popup menu was chosen: make it the row shown in the bar. */
616static void
617summary_menu_item_activate_cb (GtkMenuItem *item, gpointer user_data)
618{
619 GNCMainSummary *summary = user_data;
620 const gchar *key = g_object_get_data (G_OBJECT (item), "summary-key");
621
622 if (!key)
623 return;
624
625 g_free (summary->active_key);
626 summary->active_key = g_strdup (key);
627 summary_update_bar (summary);
628}
629
630static void
631summary_menu_remove_child_cb (GtkWidget *child, gpointer user_data)
632{
633 gtk_widget_destroy (child);
634}
635
636/* Rebuild the menu from the datamodel. Returns the menu item for the
637 * active row (or NULL). */
638static GtkWidget *
639summary_menu_populate (GNCMainSummary *summary)
640{
641 GtkTreeModel *model = GTK_TREE_MODEL (summary->datamodel);
642 GtkWidget *active_item = NULL;
643
644 gtk_container_foreach (GTK_CONTAINER (summary->menu),
645 summary_menu_remove_child_cb, NULL);
646
647 GtkTreeIter iter;
648 if (!gtk_tree_model_get_iter_first (model, &iter))
649 return NULL;
650
651 do
652 {
653 GtkWidget *item = gtk_menu_item_new ();
654 GtkWidget *box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 5);
655 GtkLabel *labels[3];
656 gchar *key = NULL;
657
658 /* Homogeneous, like the bar, so the three columns line up. */
659 gtk_box_set_homogeneous (GTK_BOX (box), TRUE);
660
661 for (int i = 0; i < 3; i++)
662 {
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]),
666 TRUE, TRUE, 0);
667 }
668 fill_row_labels (summary, model, &iter, labels);
669
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)
673 active_item = item;
674 /* The item owns the key string and frees it when destroyed. */
675 g_object_set_data_full (G_OBJECT (item), "summary-key", key, g_free);
676
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);
680 }
681 while (gtk_tree_model_iter_next (model, &iter));
682
683 return active_item;
684}
685
686static void
687summary_popup_menu (GNCMainSummary *summary, const GdkEvent *trigger)
688{
689 GtkWidget *active_item = summary_menu_populate (summary);
690
691 /* Make the popup as wide as the bar. */
692 gtk_widget_set_size_request (summary->menu,
693 gtk_widget_get_allocated_width (summary->button),
694 -1);
695 gtk_widget_show_all (summary->menu);
696
697 /* Menu's bottom-left corner on the bar's top-left corner: the list
698 * grows upward from the bar. GDK flips it downward only if there is
699 * no room above. */
700 gtk_menu_popup_at_widget (GTK_MENU (summary->menu), summary->button,
701 GDK_GRAVITY_NORTH_WEST,
702 GDK_GRAVITY_SOUTH_WEST,
703 trigger);
704
705 if (active_item)
706 gtk_menu_shell_select_item (GTK_MENU_SHELL (summary->menu), active_item);
707}
708
709/* Mouse: open on press of the primary button. Returning TRUE stops the
710 * button's own handling, so no "clicked" follows. */
711static gboolean
712summary_button_press_cb (GtkWidget *widget, GdkEventButton *event,
713 gpointer user_data)
714{
715 if (event->type != GDK_BUTTON_PRESS || event->button != GDK_BUTTON_PRIMARY)
716 return FALSE;
717
718 summary_popup_menu (user_data, (const GdkEvent *) event);
719 return TRUE;
720}
721
722/* Keyboard: Space/Enter on the focused bar emits "clicked". */
723static void
724summary_button_clicked_cb (GtkButton *button, gpointer user_data)
725{
726 GNCMainSummary *summary = user_data;
727
728 if (!gtk_widget_get_visible (summary->menu))
729 summary_popup_menu (summary, NULL);
730}
731
732typedef gboolean (*SummaryStepFn) (GtkTreeModel *, GtkTreeIter *);
733
734static gboolean
735summary_iter_last (GtkTreeModel *model, GtkTreeIter *iter)
736{
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);
739}
740
741static gboolean
742summary_button_key_press_cb (GtkWidget *widget, GdkEventKey *event, gpointer user_data)
743{
744 GNCMainSummary *summary = user_data;
745 GtkTreeModel *model = GTK_TREE_MODEL (summary->datamodel);
746 SummaryStepFn fn;
747
748 if (event->state & gtk_accelerator_get_default_mod_mask ())
749 return FALSE;
750
751 switch (event->keyval)
752 {
753 case GDK_KEY_Up:
754 case GDK_KEY_KP_Up:
755 fn = gtk_tree_model_iter_previous;
756 break;
757 case GDK_KEY_Down:
758 case GDK_KEY_KP_Down:
759 fn = gtk_tree_model_iter_next;
760 break;
761 case GDK_KEY_Home:
762 case GDK_KEY_KP_Home:
763 fn = gtk_tree_model_get_iter_first;
764 break;
765 case GDK_KEY_End:
766 case GDK_KEY_KP_End:
767 fn = summary_iter_last;
768 break;
769 default:
770 return FALSE;
771 }
772
773 GtkTreeIter iter;
774 if (!summary_find_row_by_key (model, summary->active_key, &iter) || !fn (model, &iter))
775 return TRUE;
776
777 g_free (summary->active_key);
778 gtk_tree_model_get (model, &iter, COLUMN_KEY, &summary->active_key, -1);
779 summary_update_bar (summary);
780 return TRUE;
781}
782
783GtkWidget *
784gnc_main_window_summary_new (void)
785{
786 GNCMainSummary * retval = g_new0(GNCMainSummary, 1);
787 int i;
788
789 retval->datamodel = gtk_list_store_new (N_COLUMNS,
790 G_TYPE_STRING,
791 G_TYPE_STRING,
792 G_TYPE_STRING,
793 G_TYPE_STRING,
794 G_TYPE_STRING,
795 G_TYPE_STRING,
796 G_TYPE_BOOLEAN,
797 G_TYPE_BOOLEAN);
798
799 retval->hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 5);
800 gtk_box_set_homogeneous (GTK_BOX (retval->hbox), FALSE);
801
802 // Set the name for this widget so it can be easily manipulated with css
803 gtk_widget_set_name (GTK_WIDGET(retval->hbox), "gnc-id-account-summary-bar");
804
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);
807 gnc_prefs_register_cb (GNC_PREFS_GROUP_GENERAL, GNC_PREF_NEGATIVE_IN_RED,
808 summarybar_update_color, retval);
809
810 retval->component_id = gnc_register_gui_component (WINDOW_SUMMARYBAR_CM_CLASS,
811 summarybar_refresh_handler,
812 NULL, retval);
813 gnc_gui_component_watch_entity_type (retval->component_id,
814 GNC_ID_ACCOUNT,
815 QOF_EVENT_DESTROY
816 | QOF_EVENT_CREATE
817 | QOF_EVENT_ADD
818 | QOF_EVENT_MODIFY
820 | GNC_EVENT_ITEM_REMOVED
821 | GNC_EVENT_ITEM_CHANGED);
822
823 /* The bar: a button holding the three centred labels and an arrow. */
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);
827
828 for (i = 0; i < 3; i++)
829 {
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]),
833 TRUE, TRUE, 0);
834 }
835
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);
842
843 /* The popup list; contents are rebuilt each time it is opened. */
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);
855
856 g_signal_connect_swapped (G_OBJECT (retval->hbox), "destroy",
857 G_CALLBACK (gnc_main_window_summary_destroy_cb),
858 retval);
859
860 gnc_main_window_summary_refresh(retval);
861
862 retval->cnxn_id = gnc_prefs_register_cb (GNC_PREFS_GROUP, NULL,
863 prefs_changed_cb, retval);
864
865 return retval->hbox;
866}
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.
Definition Account.h:103
GNCAccountType xaccAccountGetType(const Account *acc)
Returns the account's account type.
Definition Account.cpp:3248
gnc_numeric xaccAccountGetBalanceAsOfDate(Account *acc, time64 date)
Get the balance of the account at the end of the day before the date specified.
Definition Account.cpp:3514
GList * gnc_account_get_children(const Account *account)
This routine returns a GList of all children accounts of the specified account.
Definition Account.cpp:2941
gnc_commodity * xaccAccountGetCommodity(const Account *acc)
Get the account's commodity.
Definition Account.cpp:3389
@ ACCT_TYPE_INCOME
Income accounts are used to denote income.
Definition Account.h:140
@ ACCT_TYPE_MUTUAL
Mutual Fund accounts will typically be shown in registers which show three columns: price,...
Definition Account.h:125
@ ACCT_TYPE_TRADING
Account used to record multiple commodity transactions.
Definition Account.h:155
@ ACCT_TYPE_EXPENSE
Expense accounts are used to denote expenses.
Definition Account.h:143
@ ACCT_TYPE_BANK
The bank account type denotes a savings or checking account held at a bank.
Definition Account.h:107
@ ACCT_TYPE_PAYABLE
A/P account type.
Definition Account.h:151
@ ACCT_TYPE_ASSET
asset (and liability) accounts indicate generic, generalized accounts that are none of the above.
Definition Account.h:116
@ ACCT_TYPE_CURRENCY
The currency account type indicates that the account is a currency trading account.
Definition Account.h:129
@ ACCT_TYPE_RECEIVABLE
A/R account type.
Definition Account.h:149
@ ACCT_TYPE_CASH
The cash account type is used to denote a shoe-box or pillowcase stuffed with * cash.
Definition Account.h:110
@ ACCT_TYPE_LIABILITY
liability (and asset) accounts indicate generic, generalized accounts that are none of the above.
Definition Account.h:119
@ ACCT_TYPE_EQUITY
Equity account is used to balance the balance sheet.
Definition Account.h:146
@ ACCT_TYPE_CREDIT
The Credit card account is used to denote credit (e.g.
Definition Account.h:113
@ ACCT_TYPE_STOCK
Stock accounts will typically be shown in registers which show three columns: price,...
Definition Account.h:122
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...
Definition gnc-date.h:87
#define GNC_EVENT_ITEM_ADDED
These events are used when a split is added to an account.
Definition gnc-event.h:45
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.
STRUCTS.
An accumulator for a given currency.
options for summarybar
QofBook reference.
Definition qofbook-p.hpp:47