backend_macosx.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. import matplotlib
  2. from matplotlib import cbook, rcParams
  3. from matplotlib._pylab_helpers import Gcf
  4. from matplotlib.backends import _macosx
  5. from matplotlib.backends.backend_agg import FigureCanvasAgg
  6. from matplotlib.backend_bases import (
  7. _Backend, FigureCanvasBase, FigureManagerBase, NavigationToolbar2,
  8. TimerBase)
  9. from matplotlib.figure import Figure
  10. from matplotlib.widgets import SubplotTool
  11. ########################################################################
  12. #
  13. # The following functions and classes are for pylab and implement
  14. # window/figure managers, etc...
  15. #
  16. ########################################################################
  17. class TimerMac(_macosx.Timer, TimerBase):
  18. """
  19. Subclass of `.TimerBase` that uses CoreFoundation run loops for timer
  20. events.
  21. Attributes
  22. ----------
  23. interval : int
  24. The time between timer events in milliseconds. Default is 1000 ms.
  25. single_shot : bool
  26. Boolean flag indicating whether this timer should operate as single
  27. shot (run once and then stop). Defaults to False.
  28. callbacks : list
  29. Stores list of (func, args) tuples that will be called upon timer
  30. events. This list can be manipulated directly, or the functions
  31. `add_callback` and `remove_callback` can be used.
  32. """
  33. # completely implemented at the C-level (in _macosx.Timer)
  34. class FigureCanvasMac(_macosx.FigureCanvas, FigureCanvasAgg):
  35. """
  36. The canvas the figure renders into. Calls the draw and print fig
  37. methods, creates the renderers, etc...
  38. Events such as button presses, mouse movements, and key presses
  39. are handled in the C code and the base class methods
  40. button_press_event, button_release_event, motion_notify_event,
  41. key_press_event, and key_release_event are called from there.
  42. Attributes
  43. ----------
  44. figure : `matplotlib.figure.Figure`
  45. A high-level Figure instance
  46. """
  47. required_interactive_framework = "macosx"
  48. def __init__(self, figure):
  49. FigureCanvasBase.__init__(self, figure)
  50. width, height = self.get_width_height()
  51. _macosx.FigureCanvas.__init__(self, width, height)
  52. self._device_scale = 1.0
  53. def _set_device_scale(self, value):
  54. if self._device_scale != value:
  55. self.figure.dpi = self.figure.dpi / self._device_scale * value
  56. self._device_scale = value
  57. def _draw(self):
  58. renderer = self.get_renderer(cleared=self.figure.stale)
  59. if self.figure.stale:
  60. self.figure.draw(renderer)
  61. return renderer
  62. def draw(self):
  63. # docstring inherited
  64. self.draw_idle()
  65. self.flush_events()
  66. # draw_idle is provided by _macosx.FigureCanvas
  67. @cbook.deprecated("3.2", alternative="draw_idle()")
  68. def invalidate(self):
  69. return self.draw_idle()
  70. def blit(self, bbox=None):
  71. self.draw_idle()
  72. def resize(self, width, height):
  73. dpi = self.figure.dpi
  74. width /= dpi
  75. height /= dpi
  76. self.figure.set_size_inches(width * self._device_scale,
  77. height * self._device_scale,
  78. forward=False)
  79. FigureCanvasBase.resize_event(self)
  80. self.draw_idle()
  81. def new_timer(self, *args, **kwargs):
  82. # docstring inherited
  83. return TimerMac(*args, **kwargs)
  84. class FigureManagerMac(_macosx.FigureManager, FigureManagerBase):
  85. """
  86. Wrap everything up into a window for the pylab interface
  87. """
  88. def __init__(self, canvas, num):
  89. FigureManagerBase.__init__(self, canvas, num)
  90. title = "Figure %d" % num
  91. _macosx.FigureManager.__init__(self, canvas, title)
  92. if rcParams['toolbar'] == 'toolbar2':
  93. self.toolbar = NavigationToolbar2Mac(canvas)
  94. else:
  95. self.toolbar = None
  96. if self.toolbar is not None:
  97. self.toolbar.update()
  98. if matplotlib.is_interactive():
  99. self.show()
  100. self.canvas.draw_idle()
  101. def close(self):
  102. Gcf.destroy(self.num)
  103. class NavigationToolbar2Mac(_macosx.NavigationToolbar2, NavigationToolbar2):
  104. def __init__(self, canvas):
  105. NavigationToolbar2.__init__(self, canvas)
  106. def _init_toolbar(self):
  107. _macosx.NavigationToolbar2.__init__(
  108. self, str(cbook._get_data_path('images')))
  109. def draw_rubberband(self, event, x0, y0, x1, y1):
  110. self.canvas.set_rubberband(int(x0), int(y0), int(x1), int(y1))
  111. def release(self, event):
  112. self.canvas.remove_rubberband()
  113. def set_cursor(self, cursor):
  114. _macosx.set_cursor(cursor)
  115. def save_figure(self, *args):
  116. filename = _macosx.choose_save_file('Save the figure',
  117. self.canvas.get_default_filename())
  118. if filename is None: # Cancel
  119. return
  120. self.canvas.figure.savefig(filename)
  121. def prepare_configure_subplots(self):
  122. toolfig = Figure(figsize=(6, 3))
  123. canvas = FigureCanvasMac(toolfig)
  124. toolfig.subplots_adjust(top=0.9)
  125. # Need to keep a reference to the tool.
  126. _tool = SubplotTool(self.canvas.figure, toolfig)
  127. return canvas
  128. def set_message(self, message):
  129. _macosx.NavigationToolbar2.set_message(self, message.encode('utf-8'))
  130. ########################################################################
  131. #
  132. # Now just provide the standard names that backend.__init__ is expecting
  133. #
  134. ########################################################################
  135. @_Backend.export
  136. class _BackendMac(_Backend):
  137. FigureCanvas = FigureCanvasMac
  138. FigureManager = FigureManagerMac
  139. @staticmethod
  140. def trigger_manager_draw(manager):
  141. manager.canvas.draw_idle()
  142. @staticmethod
  143. def mainloop():
  144. _macosx.show()