backend_gtk3cairo.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435
  1. from contextlib import nullcontext
  2. from .backend_cairo import FigureCanvasCairo
  3. from .backend_gtk3 import GLib, Gtk, FigureCanvasGTK3, _BackendGTK3
  4. class FigureCanvasGTK3Cairo(FigureCanvasCairo, FigureCanvasGTK3):
  5. def on_draw_event(self, widget, ctx):
  6. if self._idle_draw_id:
  7. GLib.source_remove(self._idle_draw_id)
  8. self._idle_draw_id = 0
  9. self.draw()
  10. with (self.toolbar._wait_cursor_for_draw_cm() if self.toolbar
  11. else nullcontext()):
  12. allocation = self.get_allocation()
  13. # Render the background before scaling, as the allocated size here is in
  14. # logical pixels.
  15. Gtk.render_background(
  16. self.get_style_context(), ctx,
  17. 0, 0, allocation.width, allocation.height)
  18. scale = self.device_pixel_ratio
  19. # Scale physical drawing to logical size.
  20. ctx.scale(1 / scale, 1 / scale)
  21. self._renderer.set_context(ctx)
  22. # Set renderer to physical size so it renders in full resolution.
  23. self._renderer.width = allocation.width * scale
  24. self._renderer.height = allocation.height * scale
  25. self._renderer.dpi = self.figure.dpi
  26. self.figure.draw(self._renderer)
  27. @_BackendGTK3.export
  28. class _BackendGTK3Cairo(_BackendGTK3):
  29. FigureCanvas = FigureCanvasGTK3Cairo