backend_wxagg.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import wx
  2. from .backend_agg import FigureCanvasAgg
  3. from .backend_wx import _BackendWx, _FigureCanvasWxBase
  4. from .backend_wx import ( # noqa: F401 # pylint: disable=W0611
  5. NavigationToolbar2Wx as NavigationToolbar2WxAgg)
  6. class FigureCanvasWxAgg(FigureCanvasAgg, _FigureCanvasWxBase):
  7. def draw(self, drawDC=None):
  8. """
  9. Render the figure using agg.
  10. """
  11. FigureCanvasAgg.draw(self)
  12. self.bitmap = _rgba_to_wx_bitmap(self.get_renderer().buffer_rgba())
  13. self._isDrawn = True
  14. self.gui_repaint(drawDC=drawDC)
  15. def blit(self, bbox=None):
  16. # docstring inherited
  17. bitmap = _rgba_to_wx_bitmap(self.get_renderer().buffer_rgba())
  18. if bbox is None:
  19. self.bitmap = bitmap
  20. else:
  21. srcDC = wx.MemoryDC(bitmap)
  22. destDC = wx.MemoryDC(self.bitmap)
  23. x = int(bbox.x0)
  24. y = int(self.bitmap.GetHeight() - bbox.y1)
  25. destDC.Blit(x, y, int(bbox.width), int(bbox.height), srcDC, x, y)
  26. destDC.SelectObject(wx.NullBitmap)
  27. srcDC.SelectObject(wx.NullBitmap)
  28. self.gui_repaint()
  29. def _rgba_to_wx_bitmap(rgba):
  30. """Convert an RGBA buffer to a wx.Bitmap."""
  31. h, w, _ = rgba.shape
  32. return wx.Bitmap.FromBufferRGBA(w, h, rgba)
  33. @_BackendWx.export
  34. class _BackendWxAgg(_BackendWx):
  35. FigureCanvas = FigureCanvasWxAgg