From 91850cc6f638cb9dab8a6788e5b060f46aac29b7 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Mon, 22 Jan 2018 19:13:30 -0800 Subject: [PATCH] Apply gtk3 background. Currently, gtk3 widget backgrounds are not being applied (which only matters if the figure patch is partially transparent). The lines added by this PR are equivalent to the call to `painter.eraseRect(self.rect())` in the qt5agg backend (compare http://doc.qt.io/qt-5/qpainter.html#eraseRect and https://developer.gnome.org/gtk3/stable/GtkStyleContext.html#gtk-render-background). As an example, run import os; os.environ["MPLBACKEND"] = "gtk3cairo" from pylab import * from gi.repository import Gtk rcParams["figure.facecolor"] = (0, 0, 0, 0) css = Gtk.CssProvider() css.load_from_data(b"* {background-color: red;}") gcf().canvas.get_style_context().add_provider( css, Gtk.STYLE_PROVIDER_PRIORITY_USER) gca() show() Without this patch, the red background is not drawn. --- lib/matplotlib/backends/backend_gtk3agg.py | 6 +++++- lib/matplotlib/backends/backend_gtk3cairo.py | 5 ++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/backends/backend_gtk3agg.py b/lib/matplotlib/backends/backend_gtk3agg.py index f54849cf049c..47430c3709d7 100644 --- a/lib/matplotlib/backends/backend_gtk3agg.py +++ b/lib/matplotlib/backends/backend_gtk3agg.py @@ -4,7 +4,7 @@ from . import backend_agg, backend_cairo, backend_gtk3 from ._gtk3_compat import gi from .backend_cairo import cairo -from .backend_gtk3 import _BackendGTK3 +from .backend_gtk3 import Gtk, _BackendGTK3 from matplotlib import transforms # The following combinations are allowed: @@ -38,6 +38,10 @@ def on_draw_event(self, widget, ctx): if not len(self._bbox_queue): self._render_figure(w, h) + Gtk.render_background( + self.get_style_context(), ctx, + allocation.x, allocation.y, + allocation.width, allocation.height) bbox_queue = [transforms.Bbox([[0, 0], [w, h]])] else: bbox_queue = self._bbox_queue diff --git a/lib/matplotlib/backends/backend_gtk3cairo.py b/lib/matplotlib/backends/backend_gtk3cairo.py index fee4ff96c282..dcbf02eb5024 100644 --- a/lib/matplotlib/backends/backend_gtk3cairo.py +++ b/lib/matplotlib/backends/backend_gtk3cairo.py @@ -1,7 +1,7 @@ from . import backend_cairo, backend_gtk3 from ._gtk3_compat import gi from .backend_cairo import cairo -from .backend_gtk3 import _BackendGTK3 +from .backend_gtk3 import Gtk, _BackendGTK3 from matplotlib.backend_bases import cursors @@ -39,6 +39,9 @@ def on_draw_event(self, widget, ctx): # toolbar.set_cursor(cursors.WAIT) self._renderer.set_context(ctx) allocation = self.get_allocation() + Gtk.render_background( + self.get_style_context(), ctx, + allocation.x, allocation.y, allocation.width, allocation.height) self._render_figure(allocation.width, allocation.height) # if toolbar: # toolbar.set_cursor(toolbar._lastCursor)