backend_gtk3cairo.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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 import cbook
  8. from matplotlib.backend_bases import cursors
  9. class RendererGTK3Cairo(backend_cairo.RendererCairo):
  10. def set_context(self, ctx):
  11. self.gc.ctx = backend_cairo._to_context(ctx)
  12. class FigureCanvasGTK3Cairo(backend_gtk3.FigureCanvasGTK3,
  13. backend_cairo.FigureCanvasCairo):
  14. def _renderer_init(self):
  15. """Use cairo renderer."""
  16. self._renderer = RendererGTK3Cairo(self.figure.dpi)
  17. def _render_figure(self, width, height):
  18. self._renderer.set_width_height(width, height)
  19. self.figure.draw(self._renderer)
  20. def on_draw_event(self, widget, ctx):
  21. """GtkDrawable draw event."""
  22. with (self.toolbar._wait_cursor_for_draw_cm() if self.toolbar
  23. else nullcontext()):
  24. self._renderer.set_context(ctx)
  25. allocation = self.get_allocation()
  26. Gtk.render_background(
  27. self.get_style_context(), ctx,
  28. allocation.x, allocation.y,
  29. allocation.width, allocation.height)
  30. self._render_figure(allocation.width, allocation.height)
  31. @cbook.deprecated("3.1", alternative="backend_gtk3.FigureManagerGTK3")
  32. class FigureManagerGTK3Cairo(backend_gtk3.FigureManagerGTK3):
  33. pass
  34. @_BackendGTK3.export
  35. class _BackendGTK3Cairo(_BackendGTK3):
  36. FigureCanvas = FigureCanvasGTK3Cairo