backend_wxagg.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import wx
  2. from .backend_agg import FigureCanvasAgg
  3. from .backend_wx import (
  4. _BackendWx, _FigureCanvasWxBase, FigureFrameWx,
  5. NavigationToolbar2Wx as NavigationToolbar2WxAgg)
  6. class FigureFrameWxAgg(FigureFrameWx):
  7. def get_canvas(self, fig):
  8. return FigureCanvasWxAgg(self, -1, fig)
  9. class FigureCanvasWxAgg(FigureCanvasAgg, _FigureCanvasWxBase):
  10. """
  11. The FigureCanvas contains the figure and does event handling.
  12. In the wxPython backend, it is derived from wxPanel, and (usually)
  13. lives inside a frame instantiated by a FigureManagerWx. The parent
  14. window probably implements a wxSizer to control the displayed
  15. control size - but we give a hint as to our preferred minimum
  16. size.
  17. """
  18. def draw(self, drawDC=None):
  19. """
  20. Render the figure using agg.
  21. """
  22. FigureCanvasAgg.draw(self)
  23. self.bitmap = _convert_agg_to_wx_bitmap(self.get_renderer(), None)
  24. self._isDrawn = True
  25. self.gui_repaint(drawDC=drawDC, origin='WXAgg')
  26. def blit(self, bbox=None):
  27. # docstring inherited
  28. if bbox is None:
  29. self.bitmap = _convert_agg_to_wx_bitmap(self.get_renderer(), None)
  30. self.gui_repaint()
  31. return
  32. l, b, w, h = bbox.bounds
  33. r = l + w
  34. t = b + h
  35. x = int(l)
  36. y = int(self.bitmap.GetHeight() - t)
  37. srcBmp = _convert_agg_to_wx_bitmap(self.get_renderer(), None)
  38. srcDC = wx.MemoryDC()
  39. srcDC.SelectObject(srcBmp)
  40. destDC = wx.MemoryDC()
  41. destDC.SelectObject(self.bitmap)
  42. destDC.Blit(x, y, int(w), int(h), srcDC, x, y)
  43. destDC.SelectObject(wx.NullBitmap)
  44. srcDC.SelectObject(wx.NullBitmap)
  45. self.gui_repaint()
  46. def _convert_agg_to_wx_bitmap(agg, bbox):
  47. """
  48. Convert the region of the agg buffer bounded by bbox to a wx.Bitmap. If
  49. bbox is None, the entire buffer is converted.
  50. Note: agg must be a backend_agg.RendererAgg instance.
  51. """
  52. if bbox is None:
  53. # agg => rgba buffer -> bitmap
  54. return wx.Bitmap.FromBufferRGBA(int(agg.width), int(agg.height),
  55. agg.buffer_rgba())
  56. else:
  57. # agg => rgba buffer -> bitmap => clipped bitmap
  58. l, b, width, height = bbox.bounds
  59. r = l + width
  60. t = b + height
  61. srcBmp = wx.Bitmap.FromBufferRGBA(int(agg.width), int(agg.height),
  62. agg.buffer_rgba())
  63. srcDC = wx.MemoryDC()
  64. srcDC.SelectObject(srcBmp)
  65. destBmp = wx.Bitmap(int(width), int(height))
  66. destDC = wx.MemoryDC()
  67. destDC.SelectObject(destBmp)
  68. x = int(l)
  69. y = int(int(agg.height) - t)
  70. destDC.Blit(0, 0, int(width), int(height), srcDC, x, y)
  71. srcDC.SelectObject(wx.NullBitmap)
  72. destDC.SelectObject(wx.NullBitmap)
  73. return destBmp
  74. @_BackendWx.export
  75. class _BackendWxAgg(_BackendWx):
  76. FigureCanvas = FigureCanvasWxAgg
  77. _frame_class = FigureFrameWxAgg