From 6266ca2f12292836223a2cf74c9de7d97e8a258e Mon Sep 17 00:00:00 2001 From: Christopher Lam Date: Fri, 29 Nov 2019 19:48:39 +0800 Subject: [PATCH] g_free() output of get_negative_color because gdk_rgba_to_string() returns a newly-allocated string * get_negative_color is gchar* instead of const gchar* * move to dialog-utils.c * rename get_negative_color() to get_negative_color_str() in window-main-summarybar.c * add g_free to gnc_tree_model_account_dispose () * modify code to g_free () after use --- gnucash/gnome-utils/dialog-utils.c | 11 +++++ gnucash/gnome-utils/dialog-utils.h | 2 + gnucash/gnome-utils/gnc-tree-model-account.c | 34 ++++++++------ gnucash/gnome-utils/window-main-summarybar.c | 6 +-- gnucash/gnome/gnc-budget-view.c | 49 +++++++++++--------- 5 files changed, 65 insertions(+), 37 deletions(-) diff --git a/gnucash/gnome-utils/dialog-utils.c b/gnucash/gnome-utils/dialog-utils.c index 3ff96017b0..ca66223feb 100644 --- a/gnucash/gnome-utils/dialog-utils.c +++ b/gnucash/gnome-utils/dialog-utils.c @@ -894,3 +894,14 @@ gnc_cost_policy_select_new (void) return cost_policy_widget; } +gchar* +get_negative_color (void) +{ + GdkRGBA color; + GtkWidget *label = gtk_label_new ("Color"); + GtkStyleContext *context = gtk_widget_get_style_context (GTK_WIDGET(label)); + gtk_style_context_add_class (context, "negative-numbers"); + gtk_style_context_get_color (context, GTK_STATE_FLAG_NORMAL, &color); + + return gdk_rgba_to_string (&color); +} diff --git a/gnucash/gnome-utils/dialog-utils.h b/gnucash/gnome-utils/dialog-utils.h index c30571d188..93857ef71c 100644 --- a/gnucash/gnome-utils/dialog-utils.h +++ b/gnucash/gnome-utils/dialog-utils.h @@ -161,4 +161,6 @@ gboolean gnc_new_book_option_display (GtkWidget *parent); GtkWidget * gnc_cost_policy_select_new (void); +gchar* get_negative_color (void); + #endif /* DIALOG_UTILS_H */ diff --git a/gnucash/gnome-utils/gnc-tree-model-account.c b/gnucash/gnome-utils/gnc-tree-model-account.c index 79de8f4201..90e2dc9838 100644 --- a/gnucash/gnome-utils/gnc-tree-model-account.c +++ b/gnucash/gnome-utils/gnc-tree-model-account.c @@ -32,6 +32,7 @@ #include "gnc-tree-model-account.h" #include "gnc-component-manager.h" #include "Account.h" +#include "dialog-utils.h" #include "gnc-accounting-period.h" #include "gnc-commodity.h" #include "gnc-prefs.h" @@ -96,7 +97,7 @@ typedef struct GncTreeModelAccountPrivate QofBook *book; Account *root; gint event_handler_id; - const gchar *negative_color; + gchar *negative_color; GHashTable *account_values_hash; @@ -109,17 +110,7 @@ typedef struct GncTreeModelAccountPrivate /************************************************************/ /* Account Tree Model - Misc Functions */ /************************************************************/ -static gchar* -get_negative_color (void) -{ - GdkRGBA color; - GtkWidget *label = gtk_label_new ("Color"); - GtkStyleContext *context = gtk_widget_get_style_context (GTK_WIDGET(label)); - gtk_style_context_add_class (context, "negative-numbers"); - gtk_style_context_get_color (context, GTK_STATE_FLAG_NORMAL, &color); - return gdk_rgba_to_string (&color); -} /** Tell the GncTreeModelAccount code to update the color that it will * use for negative numbers. This function will iterate over all @@ -144,7 +135,14 @@ gnc_tree_model_account_update_color (gpointer gsettings, gchar *key, gpointer us g_free, g_free); use_red = gnc_prefs_get_bool (GNC_PREFS_GROUP_GENERAL, GNC_PREF_NEGATIVE_IN_RED); - priv->negative_color = use_red ? get_negative_color () : NULL; + + if (priv->negative_color) + g_free (priv->negative_color); + + if (use_red) + priv->negative_color = get_negative_color (); + else + priv->negative_color = NULL; } /************************************************************/ @@ -190,7 +188,14 @@ gnc_tree_model_account_init (GncTreeModelAccount *model) priv = GNC_TREE_MODEL_ACCOUNT_GET_PRIVATE(model); priv->book = NULL; priv->root = NULL; - priv->negative_color = red ? get_negative_color () : NULL; + + if (priv->negative_color) + g_free (priv->negative_color); + + if (red) + priv->negative_color = get_negative_color (); + else + priv->negative_color = NULL; // create the account values cache hash priv->account_values_hash = g_hash_table_new_full (g_str_hash, g_str_equal, @@ -244,6 +249,9 @@ gnc_tree_model_account_dispose (GObject *object) priv->event_handler_id = 0; } + if (priv->negative_color) + g_free (priv->negative_color); + // destroy the cached acount values g_hash_table_destroy (priv->account_values_hash); diff --git a/gnucash/gnome-utils/window-main-summarybar.c b/gnucash/gnome-utils/window-main-summarybar.c index 9262bded32..14b992835e 100644 --- a/gnucash/gnome-utils/window-main-summarybar.c +++ b/gnucash/gnome-utils/window-main-summarybar.c @@ -443,7 +443,7 @@ gnc_main_window_summary_refresh (GNCMainSummary * summary) } static gchar* -get_negative_color (void) +get_negative_color_str (void) { GdkRGBA color; GdkRGBA *rgba; @@ -467,7 +467,7 @@ summarybar_update_color (gpointer gsettings, gchar *key, gpointer user_data) { GNCMainSummary *summary = user_data; - summary->negative_color = get_negative_color(); + summary->negative_color = get_negative_color_str(); summary->show_negative_color = gnc_prefs_get_bool (GNC_PREFS_GROUP_GENERAL, GNC_PREF_NEGATIVE_IN_RED); gnc_main_window_summary_refresh (summary); @@ -640,7 +640,7 @@ gnc_main_window_summary_new (void) retval->totals_combo = gtk_combo_box_new_with_model (GTK_TREE_MODEL (retval->datamodel)); g_object_unref (retval->datamodel); - retval->negative_color = get_negative_color(); + retval->negative_color = get_negative_color_str(); retval->show_negative_color = gnc_prefs_get_bool (GNC_PREFS_GROUP_GENERAL, GNC_PREF_NEGATIVE_IN_RED); gnc_prefs_register_cb (GNC_PREFS_GROUP_GENERAL, GNC_PREF_NEGATIVE_IN_RED, summarybar_update_color, retval); diff --git a/gnucash/gnome/gnc-budget-view.c b/gnucash/gnome/gnc-budget-view.c index 02ace74b9b..780a1f062a 100644 --- a/gnucash/gnome/gnc-budget-view.c +++ b/gnucash/gnome/gnc-budget-view.c @@ -911,18 +911,6 @@ gbv_get_accumulated_budget_amount(GncBudget* budget, Account* account, guint per return info.total; } -static gchar* -get_negative_color (void) -{ - GdkRGBA color; - GtkWidget *label = gtk_label_new ("Color"); - GtkStyleContext *context = gtk_widget_get_style_context (GTK_WIDGET(label)); - gtk_style_context_add_class (context, "negative-numbers"); - gtk_style_context_get_color (context, GTK_STATE_FLAG_NORMAL, &color); - gtk_widget_destroy(label); - - return gdk_rgba_to_string(&color); -} /** \brief Calculates and displays budget amount for a period in a defined account. @@ -991,11 +979,16 @@ budget_col_source(Account *account, GtkTreeViewColumn *col, xaccSPrintAmount(amtbuff, numeric, gnc_account_print_info(account, FALSE)); - g_object_set(cell, "foreground", - red && gnc_numeric_negative_p(numeric) - ? get_negative_color() - : NULL, - NULL); + + if (red && gnc_numeric_negative_p(numeric)) + { + gchar *color = get_negative_color (); + g_object_set(cell, "foreground", color, NULL); + g_free (color); + } + else + g_object_set(cell, "foreground", NULL, NULL); + } } return g_strdup(amtbuff); @@ -1078,8 +1071,16 @@ budget_total_col_source(Account *account, GtkTreeViewColumn *col, total = bgv_get_total_for_account(account, budget, NULL); xaccSPrintAmount(amtbuff, total, gnc_account_print_info(account, TRUE)); - g_object_set(cell, "foreground", - red && gnc_numeric_negative_p(total) ? get_negative_color () : NULL, NULL); + + if (red && gnc_numeric_negative_p(total)) + { + gchar *color = get_negative_color (); + g_object_set(cell, "foreground", color, NULL); + g_free (color); + } + else + g_object_set(cell, "foreground", NULL, NULL); + return g_strdup(amtbuff); } @@ -1218,8 +1219,14 @@ totals_col_source(GtkTreeViewColumn *col, GtkCellRenderer *cell, xaccSPrintAmount(amtbuff, total, gnc_commodity_print_info(total_currency, period_num < 0 ? TRUE : FALSE)); - g_object_set(cell, "foreground", - red && gnc_numeric_negative_p(total) ? get_negative_color () : NULL, NULL); + if (red && gnc_numeric_negative_p(total)) + { + gchar *color = get_negative_color (); + g_object_set(cell, "foreground", color, NULL); + g_free (color); + } + else + g_object_set(cell, "foreground", NULL, NULL); g_object_set(G_OBJECT(cell), "text", amtbuff, "xalign", 1.0, NULL); }