Mercurial > dwindows
changeset 2242:0c0ad67aacb5
GTK3: Fix DW_CLR_DEFAULT support and fix a memory leak.
During testing with the new dwtest code for Rich Edit MLEs on Windows...
I discovered that the CSS color and font overrides in GTK3 were not
being removed, causing DW_CLR_DEFAULT to not function after previous
calls to dw_window_set_color() with other colors. We now save the
CSS providers for the overrides and remove them on the next call.
Previously we just added new CSS providers on each call leaking memory.
author | bsmith@81767d24-ef19-dc11-ae90-00e081727c95 |
---|---|
date | Tue, 19 Jan 2021 13:59:57 +0000 |
parents | 6f28c68642f5 |
children | 8f9ffba67b7c |
files | gtk3/dw.c |
diffstat | 1 files changed, 39 insertions(+), 10 deletions(-) [+] |
line wrap: on
line diff
--- a/gtk3/dw.c Tue Jan 19 00:09:08 2021 +0000 +++ b/gtk3/dw.c Tue Jan 19 13:59:57 2021 +0000 @@ -2708,29 +2708,58 @@ /* Internal functions to convert to GTK3 style CSS */ static void _dw_override_color(GtkWidget *widget, const char *element, GdkRGBA *color) { + gchar *dataname = g_strdup_printf ("_dw_color_%s", element); + GtkCssProvider *provider = g_object_get_data(G_OBJECT(widget), dataname); + GtkStyleContext *scontext = gtk_widget_get_style_context(widget); + + /* If we have an old context from a previous override remove it */ + if(provider) + { + gtk_style_context_remove_provider(scontext, GTK_STYLE_PROVIDER(provider)); + g_object_unref(provider); + provider = NULL; + } + + /* If we have a new color, create a new provider and add it */ if(color) { - GtkCssProvider *provider = gtk_css_provider_new(); gchar *scolor = gdk_rgba_to_string(color); gchar *css = g_strdup_printf ("* { %s: %s; }", element, scolor); + provider = gtk_css_provider_new(); g_free(scolor); gtk_css_provider_load_from_data(provider, css, -1, NULL); g_free(css); - gtk_style_context_add_provider(gtk_widget_get_style_context(widget), GTK_STYLE_PROVIDER(provider), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION); - g_object_unref(provider); + gtk_style_context_add_provider(scontext, GTK_STYLE_PROVIDER(provider), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION); } + g_object_set_data(G_OBJECT(widget), dataname, (gpointer)provider); + g_free(dataname); } static void _dw_override_font(GtkWidget *widget, const char *font) { - GtkCssProvider *provider = gtk_css_provider_new(); - gchar *css = g_strdup_printf ("* { font: %s; }", font); - - gtk_css_provider_load_from_data(provider, css, -1, NULL); - g_free(css); - gtk_style_context_add_provider(gtk_widget_get_style_context(widget), GTK_STYLE_PROVIDER(provider), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION); - g_object_unref(provider); + GtkCssProvider *provider = g_object_get_data(G_OBJECT(widget), "_dw_font"); + GtkStyleContext *scontext = gtk_widget_get_style_context(widget); + + /* If we have an old context from a previous override remove it */ + if(provider) + { + gtk_style_context_remove_provider(scontext, GTK_STYLE_PROVIDER(provider)); + g_object_unref(provider); + provider = NULL; + } + + /* If we have a new font, create a new provider and add it */ + if(font) + { + gchar *css = g_strdup_printf ("* { font: %s; }", font); + + provider = gtk_css_provider_new(); + gtk_css_provider_load_from_data(provider, css, -1, NULL); + g_free(css); + gtk_style_context_add_provider(scontext, GTK_STYLE_PROVIDER(provider), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION); + } + g_object_set_data(G_OBJECT(widget), "_dw_font", (gpointer)provider); } /*