From f3ca0e5af8b0ccb7400accc946e0a32e290b0206 Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Mon, 27 Feb 2017 19:11:02 +0000 Subject: [PATCH] GTK API deprecation: use GtkCssProvider for window background. gdk_window_set_background_rgba is deprecated as of GTK 3.22, because apparently you can't just _say_ any more 'here is what I want my window's background colour to be in places where a widget isn't'. Instead you have to provide a GtkStyleProvider which can be slotted into a wobbly tower of other providers with associated priorities, so that the user can override your choices if they really want to. And the easiest way to constructc a GtkStyleProvider in turn is to write *actual CSS* and get GTK to parse it, so I end up converting my nice numeric RGB values into a complicated text format for another part of _the same process_ to parse back into numbers. Sigh. --- gtk.c | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/gtk.c b/gtk.c index 53f5d22..bf585d6 100644 --- a/gtk.c +++ b/gtk.c @@ -144,6 +144,9 @@ struct frontend { GtkWidget *area; GtkWidget *statusbar; GtkWidget *menubar; +#if GTK_CHECK_VERSION(3,20,0) + GtkCssProvider *css_provider; +#endif guint statusctx; int w, h; midend *me; @@ -290,7 +293,27 @@ static void set_colour(frontend *fe, int colour) static void set_window_background(frontend *fe, int colour) { -#if GTK_CHECK_VERSION(3,0,0) +#if GTK_CHECK_VERSION(3,20,0) + char css_buf[512]; + sprintf(css_buf, ".background { " + "background-color: #%02x%02x%02x; }", + (unsigned)(fe->colours[3*colour + 0] * 255), + (unsigned)(fe->colours[3*colour + 1] * 255), + (unsigned)(fe->colours[3*colour + 2] * 255)); + if (!fe->css_provider) + fe->css_provider = gtk_css_provider_new(); + if (!gtk_css_provider_load_from_data( + GTK_CSS_PROVIDER(fe->css_provider), css_buf, -1, NULL)) + assert(0 && "Couldn't load CSS"); + gtk_style_context_add_provider( + gtk_widget_get_style_context(fe->window), + GTK_STYLE_PROVIDER(fe->css_provider), + GTK_STYLE_PROVIDER_PRIORITY_APPLICATION); + gtk_style_context_add_provider( + gtk_widget_get_style_context(fe->area), + GTK_STYLE_PROVIDER(fe->css_provider), + GTK_STYLE_PROVIDER_PRIORITY_APPLICATION); +#elif GTK_CHECK_VERSION(3,0,0) GdkRGBA rgba; rgba.red = fe->colours[3*colour + 0]; rgba.green = fe->colours[3*colour + 1]; @@ -2355,6 +2378,9 @@ static frontend *new_window(char *arg, int argtype, char **error) extern const int n_xpm_icons; fe = snew(frontend); +#if GTK_CHECK_VERSION(3,20,0) + fe->css_provider = NULL; +#endif fe->timer_active = FALSE; fe->timer_id = -1;