backend_wxcairo.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import wx
  2. from .backend_cairo import cairo, FigureCanvasCairo, RendererCairo
  3. from .backend_wx import (
  4. _BackendWx, _FigureCanvasWxBase, FigureFrameWx,
  5. NavigationToolbar2Wx as NavigationToolbar2WxCairo)
  6. import wx.lib.wxcairo as wxcairo
  7. class FigureFrameWxCairo(FigureFrameWx):
  8. def get_canvas(self, fig):
  9. return FigureCanvasWxCairo(self, -1, fig)
  10. class FigureCanvasWxCairo(_FigureCanvasWxBase, FigureCanvasCairo):
  11. """
  12. The FigureCanvas contains the figure and does event handling.
  13. In the wxPython backend, it is derived from wxPanel, and (usually) lives
  14. inside a frame instantiated by a FigureManagerWx. The parent window
  15. probably implements a wxSizer to control the displayed control size - but
  16. we give a hint as to our preferred minimum size.
  17. """
  18. def __init__(self, parent, id, figure):
  19. # _FigureCanvasWxBase should be fixed to have the same signature as
  20. # every other FigureCanvas and use cooperative inheritance, but in the
  21. # meantime the following will make do.
  22. _FigureCanvasWxBase.__init__(self, parent, id, figure)
  23. FigureCanvasCairo.__init__(self, figure)
  24. self._renderer = RendererCairo(self.figure.dpi)
  25. def draw(self, drawDC=None):
  26. width = int(self.figure.bbox.width)
  27. height = int(self.figure.bbox.height)
  28. surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)
  29. self._renderer.set_ctx_from_surface(surface)
  30. self._renderer.set_width_height(width, height)
  31. self.figure.draw(self._renderer)
  32. self.bitmap = wxcairo.BitmapFromImageSurface(surface)
  33. self._isDrawn = True
  34. self.gui_repaint(drawDC=drawDC, origin='WXCairo')
  35. @_BackendWx.export
  36. class _BackendWxCairo(_BackendWx):
  37. FigureCanvas = FigureCanvasWxCairo
  38. _frame_class = FigureFrameWxCairo