comparison gtk3/dw.c @ 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 3e9c5bff0a57
children 8f9ffba67b7c
comparison
equal deleted inserted replaced
2241:6f28c68642f5 2242:0c0ad67aacb5
2706 } 2706 }
2707 2707
2708 /* Internal functions to convert to GTK3 style CSS */ 2708 /* Internal functions to convert to GTK3 style CSS */
2709 static void _dw_override_color(GtkWidget *widget, const char *element, GdkRGBA *color) 2709 static void _dw_override_color(GtkWidget *widget, const char *element, GdkRGBA *color)
2710 { 2710 {
2711 gchar *dataname = g_strdup_printf ("_dw_color_%s", element);
2712 GtkCssProvider *provider = g_object_get_data(G_OBJECT(widget), dataname);
2713 GtkStyleContext *scontext = gtk_widget_get_style_context(widget);
2714
2715 /* If we have an old context from a previous override remove it */
2716 if(provider)
2717 {
2718 gtk_style_context_remove_provider(scontext, GTK_STYLE_PROVIDER(provider));
2719 g_object_unref(provider);
2720 provider = NULL;
2721 }
2722
2723 /* If we have a new color, create a new provider and add it */
2711 if(color) 2724 if(color)
2712 { 2725 {
2713 GtkCssProvider *provider = gtk_css_provider_new();
2714 gchar *scolor = gdk_rgba_to_string(color); 2726 gchar *scolor = gdk_rgba_to_string(color);
2715 gchar *css = g_strdup_printf ("* { %s: %s; }", element, scolor); 2727 gchar *css = g_strdup_printf ("* { %s: %s; }", element, scolor);
2716 2728
2729 provider = gtk_css_provider_new();
2717 g_free(scolor); 2730 g_free(scolor);
2718 gtk_css_provider_load_from_data(provider, css, -1, NULL); 2731 gtk_css_provider_load_from_data(provider, css, -1, NULL);
2719 g_free(css); 2732 g_free(css);
2720 gtk_style_context_add_provider(gtk_widget_get_style_context(widget), GTK_STYLE_PROVIDER(provider), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION); 2733 gtk_style_context_add_provider(scontext, GTK_STYLE_PROVIDER(provider), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
2734 }
2735 g_object_set_data(G_OBJECT(widget), dataname, (gpointer)provider);
2736 g_free(dataname);
2737 }
2738
2739 static void _dw_override_font(GtkWidget *widget, const char *font)
2740 {
2741 GtkCssProvider *provider = g_object_get_data(G_OBJECT(widget), "_dw_font");
2742 GtkStyleContext *scontext = gtk_widget_get_style_context(widget);
2743
2744 /* If we have an old context from a previous override remove it */
2745 if(provider)
2746 {
2747 gtk_style_context_remove_provider(scontext, GTK_STYLE_PROVIDER(provider));
2721 g_object_unref(provider); 2748 g_object_unref(provider);
2722 } 2749 provider = NULL;
2723 } 2750 }
2724 2751
2725 static void _dw_override_font(GtkWidget *widget, const char *font) 2752 /* If we have a new font, create a new provider and add it */
2726 { 2753 if(font)
2727 GtkCssProvider *provider = gtk_css_provider_new(); 2754 {
2728 gchar *css = g_strdup_printf ("* { font: %s; }", font); 2755 gchar *css = g_strdup_printf ("* { font: %s; }", font);
2729 2756
2730 gtk_css_provider_load_from_data(provider, css, -1, NULL); 2757 provider = gtk_css_provider_new();
2731 g_free(css); 2758 gtk_css_provider_load_from_data(provider, css, -1, NULL);
2732 gtk_style_context_add_provider(gtk_widget_get_style_context(widget), GTK_STYLE_PROVIDER(provider), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION); 2759 g_free(css);
2733 g_object_unref(provider); 2760 gtk_style_context_add_provider(scontext, GTK_STYLE_PROVIDER(provider), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
2761 }
2762 g_object_set_data(G_OBJECT(widget), "_dw_font", (gpointer)provider);
2734 } 2763 }
2735 2764
2736 /* 2765 /*
2737 * Sets the font used by a specified window (widget) handle. 2766 * Sets the font used by a specified window (widget) handle.
2738 * Parameters: 2767 * Parameters: