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.
This commit is contained in:
Simon Tatham
2017-02-27 19:11:02 +00:00
parent 7cae89fb4b
commit f3ca0e5af8

28
gtk.c
View File

@ -144,6 +144,9 @@ struct frontend {
GtkWidget *area; GtkWidget *area;
GtkWidget *statusbar; GtkWidget *statusbar;
GtkWidget *menubar; GtkWidget *menubar;
#if GTK_CHECK_VERSION(3,20,0)
GtkCssProvider *css_provider;
#endif
guint statusctx; guint statusctx;
int w, h; int w, h;
midend *me; midend *me;
@ -290,7 +293,27 @@ static void set_colour(frontend *fe, int colour)
static void set_window_background(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; GdkRGBA rgba;
rgba.red = fe->colours[3*colour + 0]; rgba.red = fe->colours[3*colour + 0];
rgba.green = fe->colours[3*colour + 1]; 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; extern const int n_xpm_icons;
fe = snew(frontend); fe = snew(frontend);
#if GTK_CHECK_VERSION(3,20,0)
fe->css_provider = NULL;
#endif
fe->timer_active = FALSE; fe->timer_active = FALSE;
fe->timer_id = -1; fe->timer_id = -1;