# HG changeset patch # User bsmith@81767d24-ef19-dc11-ae90-00e081727c95 # Date 1611064797 0 # Node ID 0c0ad67aacb54281e9f0e1e49c58cf5f2cee8f51 # Parent 6f28c68642f5216b7f4082ab2c84144511779a05 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. diff -r 6f28c68642f5 -r 0c0ad67aacb5 gtk3/dw.c --- 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); } /*