backend_gtk3cairo.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. try:
  2. from contextlib import nullcontext
  3. except ImportError:
  4. from contextlib import ExitStack as nullcontext # Py 3.6.
  5. from . import backend_cairo, backend_gtk3
  6. from .backend_gtk3 import Gtk, _BackendGTK3
  7. from matplotlib.backend_bases import cursors
  8. class RendererGTK3Cairo(backend_cairo.RendererCairo):
  9. def set_context(self, ctx):
  10. self.gc.ctx = backend_cairo._to_context(ctx)
  11. class FigureCanvasGTK3Cairo(backend_gtk3.FigureCanvasGTK3,
  12. backend_cairo.FigureCanvasCairo):
  13. def __init__(self, figure):
  14. super().__init__(figure)
  15. self._renderer = RendererGTK3Cairo(self.figure.dpi)
  16. def on_draw_event(self, widget, ctx):
  17. """GtkDrawable draw event."""
  18. with (self.toolbar._wait_cursor_for_draw_cm() if self.toolbar
  19. else nullcontext()):
  20. self._renderer.set_context(ctx)
  21. allocation = self.get_allocation()
  22. Gtk.render_background(
  23. self.get_style_context(), ctx,
  24. allocation.x, allocation.y,
  25. allocation.width, allocation.height)
  26. self._renderer.set_width_height(
  27. allocation.width, allocation.height)
  28. self.figure.draw(self._renderer)
  29. @_BackendGTK3.export
  30. class _BackendGTK3Cairo(_BackendGTK3):
  31. FigureCanvas = FigureCanvasGTK3Cairo