Advertisement
Guest User

backend_wx.py

a guest
Sep 30th, 2017
462
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 64.71 KB | None | 0 0
  1. """
  2. A wxPython backend for matplotlib, based (very heavily) on
  3. backend_template.py and backend_gtk.py
  4.  
  5. Author: Jeremy O'Donoghue (jeremy@o-donoghue.com)
  6.  
  7. Derived from original copyright work by John Hunter
  8. (jdhunter@ace.bsd.uchicago.edu)
  9.  
  10. Copyright (C) Jeremy O'Donoghue & John Hunter, 2003-4
  11.  
  12. License: This work is licensed under a PSF compatible license. A copy
  13. should be included with this source code.
  14.  
  15. """
  16. from __future__ import (absolute_import, division, print_function,
  17. unicode_literals)
  18.  
  19. from six.moves import xrange
  20.  
  21. import sys
  22. import os
  23. import os.path
  24. import math
  25. import weakref
  26. import warnings
  27.  
  28. import numpy as np
  29.  
  30. import matplotlib
  31. from matplotlib.backend_bases import (
  32. _Backend, FigureCanvasBase, FigureManagerBase, GraphicsContextBase,
  33. NavigationToolbar2, RendererBase, TimerBase, cursors)
  34. from matplotlib.backend_bases import _has_pil
  35.  
  36. from matplotlib._pylab_helpers import Gcf
  37. from matplotlib.cbook import is_writable_file_like, warn_deprecated
  38. from matplotlib.figure import Figure
  39. from matplotlib.path import Path
  40. from matplotlib.transforms import Affine2D
  41. from matplotlib.widgets import SubplotTool
  42. from matplotlib import cbook, rcParams
  43.  
  44. from . import wx_compat as wxc
  45. import wx
  46.  
  47. # Debugging settings here...
  48. # Debug level set here. If the debug level is less than 5, information
  49. # messages (progressively more info for lower value) are printed. In addition,
  50. # traceback is performed, and pdb activated, for all uncaught exceptions in
  51. # this case
  52. _DEBUG = 5
  53. if _DEBUG < 5:
  54. import traceback
  55. import pdb
  56. _DEBUG_lvls = {1: 'Low ', 2: 'Med ', 3: 'High', 4: 'Error'}
  57.  
  58.  
  59. def DEBUG_MSG(string, lvl=3, o=None):
  60. if lvl >= _DEBUG:
  61. cls = o.__class__
  62. # Jeremy, often times the commented line won't print but the
  63. # one below does. I think WX is redefining stderr, damned
  64. # beast
  65. #print >>sys.stderr, "%s- %s in %s" % (_DEBUG_lvls[lvl], string, cls)
  66. print("%s- %s in %s" % (_DEBUG_lvls[lvl], string, cls))
  67.  
  68.  
  69. def debug_on_error(type, value, tb):
  70. """Code due to Thomas Heller - published in Python Cookbook (O'Reilley)"""
  71. traceback.print_exception(type, value, tb)
  72. print()
  73. pdb.pm() # jdh uncomment
  74.  
  75.  
  76. class fake_stderr(object):
  77. """
  78. Wx does strange things with stderr, as it makes the assumption that
  79. there is probably no console. This redirects stderr to the console, since
  80. we know that there is one!
  81. """
  82.  
  83. def write(self, msg):
  84. print("Stderr: %s\n\r" % msg)
  85.  
  86. #if _DEBUG < 5:
  87. #sys.excepthook = debug_on_error
  88. #WxLogger =wx.LogStderr()
  89. #sys.stderr = fake_stderr
  90.  
  91. # the True dots per inch on the screen; should be display dependent
  92. # see
  93. # http://groups.google.com/groups?q=screen+dpi+x11&hl=en&lr=&ie=UTF-8&oe=UTF-8&safe=off&selm=7077.26e81ad5%40swift.cs.tcd.ie&rnum=5
  94. # for some info about screen dpi
  95. PIXELS_PER_INCH = 75
  96.  
  97. # Delay time for idle checks
  98. IDLE_DELAY = 5
  99.  
  100.  
  101. def error_msg_wx(msg, parent=None):
  102. """
  103. Signal an error condition -- in a GUI, popup a error dialog
  104. """
  105. dialog = wx.MessageDialog(parent=parent,
  106. message=msg,
  107. caption='Matplotlib backend_wx error',
  108. style=wx.OK | wx.CENTRE)
  109. dialog.ShowModal()
  110. dialog.Destroy()
  111. return None
  112.  
  113.  
  114. def raise_msg_to_str(msg):
  115. """msg is a return arg from a raise. Join with new lines"""
  116. if not isinstance(msg, six.string_types):
  117. msg = '\n'.join(map(str, msg))
  118. return msg
  119.  
  120.  
  121. class TimerWx(TimerBase):
  122. '''
  123. Subclass of :class:`backend_bases.TimerBase` that uses WxTimer events.
  124.  
  125. Attributes
  126. ----------
  127. interval : int
  128. The time between timer events in milliseconds. Default is 1000 ms.
  129. single_shot : bool
  130. Boolean flag indicating whether this timer should operate as single
  131. shot (run once and then stop). Defaults to False.
  132. callbacks : list
  133. Stores list of (func, args) tuples that will be called upon timer
  134. events. This list can be manipulated directly, or the functions
  135. `add_callback` and `remove_callback` can be used.
  136.  
  137. '''
  138.  
  139. def __init__(self, parent, *args, **kwargs):
  140. TimerBase.__init__(self, *args, **kwargs)
  141.  
  142. # Create a new timer and connect the timer event to our handler.
  143. # For WX, the events have to use a widget for binding.
  144. self.parent = parent
  145. self._timer = wx.Timer(self.parent, wx.NewId())
  146. self.parent.Bind(wx.EVT_TIMER, self._on_timer, self._timer)
  147.  
  148. # Unbinding causes Wx to stop for some reason. Disabling for now.
  149. # def __del__(self):
  150. # TimerBase.__del__(self)
  151. # self.parent.Bind(wx.EVT_TIMER, None, self._timer)
  152.  
  153. def _timer_start(self):
  154. self._timer.Start(self._interval, self._single)
  155.  
  156. def _timer_stop(self):
  157. self._timer.Stop()
  158.  
  159. def _timer_set_interval(self):
  160. self._timer_start()
  161.  
  162. def _timer_set_single_shot(self):
  163. self._timer.Start()
  164.  
  165. def _on_timer(self, *args):
  166. TimerBase._on_timer(self)
  167.  
  168.  
  169. class RendererWx(RendererBase):
  170. """
  171. The renderer handles all the drawing primitives using a graphics
  172. context instance that controls the colors/styles. It acts as the
  173. 'renderer' instance used by many classes in the hierarchy.
  174. """
  175. # In wxPython, drawing is performed on a wxDC instance, which will
  176. # generally be mapped to the client aread of the window displaying
  177. # the plot. Under wxPython, the wxDC instance has a wx.Pen which
  178. # describes the colour and weight of any lines drawn, and a wxBrush
  179. # which describes the fill colour of any closed polygon.
  180.  
  181. fontweights = wxc.fontweights
  182. fontangles = wxc.fontangles
  183.  
  184. # wxPython allows for portable font styles, choosing them appropriately
  185. # for the target platform. Map some standard font names to the portable
  186. # styles
  187. # QUESTION: Is it be wise to agree standard fontnames across all backends?
  188. fontnames = wxc.fontnames
  189.  
  190. def __init__(self, bitmap, dpi):
  191. """
  192. Initialise a wxWindows renderer instance.
  193. """
  194. warn_deprecated('2.0', message="The WX backend is "
  195. "deprecated. It's untested "
  196. "and will be removed in Matplotlib 2.2. "
  197. "Use the WXAgg backend instead. "
  198. "See Matplotlib usage FAQ for more info on backends.",
  199. alternative='WXAgg')
  200. RendererBase.__init__(self)
  201. DEBUG_MSG("__init__()", 1, self)
  202. self.width = bitmap.GetWidth()
  203. self.height = bitmap.GetHeight()
  204. self.bitmap = bitmap
  205. self.fontd = {}
  206. self.dpi = dpi
  207. self.gc = None
  208.  
  209. def flipy(self):
  210. return True
  211.  
  212. def offset_text_height(self):
  213. return True
  214.  
  215. def get_text_width_height_descent(self, s, prop, ismath):
  216. """
  217. get the width and height in display coords of the string s
  218. with FontPropertry prop
  219. """
  220. # return 1, 1
  221. if ismath:
  222. s = self.strip_math(s)
  223.  
  224. if self.gc is None:
  225. gc = self.new_gc()
  226. else:
  227. gc = self.gc
  228. gfx_ctx = gc.gfx_ctx
  229. font = self.get_wx_font(s, prop)
  230. gfx_ctx.SetFont(font, wx.BLACK)
  231. w, h, descent, leading = gfx_ctx.GetFullTextExtent(s)
  232.  
  233. return w, h, descent
  234.  
  235. def get_canvas_width_height(self):
  236. 'return the canvas width and height in display coords'
  237. return self.width, self.height
  238.  
  239. def handle_clip_rectangle(self, gc):
  240. new_bounds = gc.get_clip_rectangle()
  241. if new_bounds is not None:
  242. new_bounds = new_bounds.bounds
  243. gfx_ctx = gc.gfx_ctx
  244. if gfx_ctx._lastcliprect != new_bounds:
  245. gfx_ctx._lastcliprect = new_bounds
  246. if new_bounds is None:
  247. gfx_ctx.ResetClip()
  248. else:
  249. gfx_ctx.Clip(new_bounds[0],
  250. self.height - new_bounds[1] - new_bounds[3],
  251. new_bounds[2], new_bounds[3])
  252.  
  253. @staticmethod
  254. def convert_path(gfx_ctx, path, transform):
  255. wxpath = gfx_ctx.CreatePath()
  256. for points, code in path.iter_segments(transform):
  257. if code == Path.MOVETO:
  258. wxpath.MoveToPoint(*points)
  259. elif code == Path.LINETO:
  260. wxpath.AddLineToPoint(*points)
  261. elif code == Path.CURVE3:
  262. wxpath.AddQuadCurveToPoint(*points)
  263. elif code == Path.CURVE4:
  264. wxpath.AddCurveToPoint(*points)
  265. elif code == Path.CLOSEPOLY:
  266. wxpath.CloseSubpath()
  267. return wxpath
  268.  
  269. def draw_path(self, gc, path, transform, rgbFace=None):
  270. gc.select()
  271. self.handle_clip_rectangle(gc)
  272. gfx_ctx = gc.gfx_ctx
  273. transform = transform + \
  274. Affine2D().scale(1.0, -1.0).translate(0.0, self.height)
  275. wxpath = self.convert_path(gfx_ctx, path, transform)
  276. if rgbFace is not None:
  277. gfx_ctx.SetBrush(wx.Brush(gc.get_wxcolour(rgbFace)))
  278. gfx_ctx.DrawPath(wxpath)
  279. else:
  280. gfx_ctx.StrokePath(wxpath)
  281. gc.unselect()
  282.  
  283. def draw_image(self, gc, x, y, im):
  284. bbox = gc.get_clip_rectangle()
  285. if bbox is not None:
  286. l, b, w, h = bbox.bounds
  287. else:
  288. l = 0
  289. b = 0
  290. w = self.width
  291. h = self.height
  292. rows, cols = im.shape[:2]
  293. bitmap = wxc.BitmapFromBuffer(cols, rows, im.tostring())
  294. gc = self.get_gc()
  295. gc.select()
  296. gc.gfx_ctx.DrawBitmap(bitmap, int(l), int(self.height - b),
  297. int(w), int(-h))
  298. gc.unselect()
  299.  
  300. def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
  301. if ismath:
  302. s = self.strip_math(s)
  303. DEBUG_MSG("draw_text()", 1, self)
  304. gc.select()
  305. self.handle_clip_rectangle(gc)
  306. gfx_ctx = gc.gfx_ctx
  307.  
  308. font = self.get_wx_font(s, prop)
  309. color = gc.get_wxcolour(gc.get_rgb())
  310. gfx_ctx.SetFont(font, color)
  311.  
  312. w, h, d = self.get_text_width_height_descent(s, prop, ismath)
  313. x = int(x)
  314. y = int(y - h)
  315.  
  316. if angle == 0.0:
  317. gfx_ctx.DrawText(s, x, y)
  318. else:
  319. rads = math.radians(angle)
  320. xo = h * math.sin(rads)
  321. yo = h * math.cos(rads)
  322. gfx_ctx.DrawRotatedText(s, x - xo, y - yo, rads)
  323.  
  324. gc.unselect()
  325.  
  326. def new_gc(self):
  327. """
  328. Return an instance of a GraphicsContextWx, and sets the current gc copy
  329. """
  330. DEBUG_MSG('new_gc()', 2, self)
  331. self.gc = GraphicsContextWx(self.bitmap, self)
  332. self.gc.select()
  333. self.gc.unselect()
  334. return self.gc
  335.  
  336. def get_gc(self):
  337. """
  338. Fetch the locally cached gc.
  339. """
  340. # This is a dirty hack to allow anything with access to a renderer to
  341. # access the current graphics context
  342. assert self.gc is not None, "gc must be defined"
  343. return self.gc
  344.  
  345. def get_wx_font(self, s, prop):
  346. """
  347. Return a wx font. Cache instances in a font dictionary for
  348. efficiency
  349. """
  350. DEBUG_MSG("get_wx_font()", 1, self)
  351.  
  352. key = hash(prop)
  353. fontprop = prop
  354. fontname = fontprop.get_name()
  355.  
  356. font = self.fontd.get(key)
  357. if font is not None:
  358. return font
  359.  
  360. # Allow use of platform independent and dependent font names
  361. wxFontname = self.fontnames.get(fontname, wx.ROMAN)
  362. wxFacename = '' # Empty => wxPython chooses based on wx_fontname
  363.  
  364. # Font colour is determined by the active wx.Pen
  365. # TODO: It may be wise to cache font information
  366. size = self.points_to_pixels(fontprop.get_size_in_points())
  367.  
  368. font = wx.Font(int(size + 0.5), # Size
  369. wxFontname, # 'Generic' name
  370. self.fontangles[fontprop.get_style()], # Angle
  371. self.fontweights[fontprop.get_weight()], # Weight
  372. False, # Underline
  373. wxFacename) # Platform font name
  374.  
  375. # cache the font and gc and return it
  376. self.fontd[key] = font
  377.  
  378. return font
  379.  
  380. def points_to_pixels(self, points):
  381. """
  382. convert point measures to pixes using dpi and the pixels per
  383. inch of the display
  384. """
  385. return points * (PIXELS_PER_INCH / 72.0 * self.dpi / 72.0)
  386.  
  387.  
  388. class GraphicsContextWx(GraphicsContextBase):
  389. """
  390. The graphics context provides the color, line styles, etc...
  391.  
  392. This class stores a reference to a wxMemoryDC, and a
  393. wxGraphicsContext that draws to it. Creating a wxGraphicsContext
  394. seems to be fairly heavy, so these objects are cached based on the
  395. bitmap object that is passed in.
  396.  
  397. The base GraphicsContext stores colors as a RGB tuple on the unit
  398. interval, e.g., (0.5, 0.0, 1.0). wxPython uses an int interval, but
  399. since wxPython colour management is rather simple, I have not chosen
  400. to implement a separate colour manager class.
  401. """
  402. _capd = {'butt': wx.CAP_BUTT,
  403. 'projecting': wx.CAP_PROJECTING,
  404. 'round': wx.CAP_ROUND}
  405.  
  406. _joind = {'bevel': wx.JOIN_BEVEL,
  407. 'miter': wx.JOIN_MITER,
  408. 'round': wx.JOIN_ROUND}
  409.  
  410. _cache = weakref.WeakKeyDictionary()
  411.  
  412. def __init__(self, bitmap, renderer):
  413. GraphicsContextBase.__init__(self)
  414. #assert self.Ok(), "wxMemoryDC not OK to use"
  415. DEBUG_MSG("__init__()", 1, self)
  416. DEBUG_MSG("__init__() 2: %s" % bitmap, 1, self)
  417.  
  418. dc, gfx_ctx = self._cache.get(bitmap, (None, None))
  419. if dc is None:
  420. dc = wx.MemoryDC()
  421. dc.SelectObject(bitmap)
  422. gfx_ctx = wx.GraphicsContext.Create(dc)
  423. gfx_ctx._lastcliprect = None
  424. self._cache[bitmap] = dc, gfx_ctx
  425.  
  426. self.bitmap = bitmap
  427. self.dc = dc
  428. self.gfx_ctx = gfx_ctx
  429. self._pen = wx.Pen('BLACK', 1, wx.SOLID)
  430. gfx_ctx.SetPen(self._pen)
  431. self._style = wx.SOLID
  432. self.renderer = renderer
  433.  
  434. def select(self):
  435. """
  436. Select the current bitmap into this wxDC instance
  437. """
  438.  
  439. if sys.platform == 'win32':
  440. self.dc.SelectObject(self.bitmap)
  441. self.IsSelected = True
  442.  
  443. def unselect(self):
  444. """
  445. Select a Null bitmasp into this wxDC instance
  446. """
  447. if sys.platform == 'win32':
  448. self.dc.SelectObject(wx.NullBitmap)
  449. self.IsSelected = False
  450.  
  451. def set_foreground(self, fg, isRGBA=None):
  452. """
  453. Set the foreground color. fg can be a matlab format string, a
  454. html hex color string, an rgb unit tuple, or a float between 0
  455. and 1. In the latter case, grayscale is used.
  456. """
  457. # Implementation note: wxPython has a separate concept of pen and
  458. # brush - the brush fills any outline trace left by the pen.
  459. # Here we set both to the same colour - if a figure is not to be
  460. # filled, the renderer will set the brush to be transparent
  461. # Same goes for text foreground...
  462. DEBUG_MSG("set_foreground()", 1, self)
  463. self.select()
  464. GraphicsContextBase.set_foreground(self, fg, isRGBA)
  465.  
  466. self._pen.SetColour(self.get_wxcolour(self.get_rgb()))
  467. self.gfx_ctx.SetPen(self._pen)
  468. self.unselect()
  469.  
  470. def set_linewidth(self, w):
  471. """
  472. Set the line width.
  473. """
  474. w = float(w)
  475. DEBUG_MSG("set_linewidth()", 1, self)
  476. self.select()
  477. if w > 0 and w < 1:
  478. w = 1
  479. GraphicsContextBase.set_linewidth(self, w)
  480. lw = int(self.renderer.points_to_pixels(self._linewidth))
  481. if lw == 0:
  482. lw = 1
  483. self._pen.SetWidth(lw)
  484. self.gfx_ctx.SetPen(self._pen)
  485. self.unselect()
  486.  
  487. def set_capstyle(self, cs):
  488. """
  489. Set the capstyle as a string in ('butt', 'round', 'projecting')
  490. """
  491. DEBUG_MSG("set_capstyle()", 1, self)
  492. self.select()
  493. GraphicsContextBase.set_capstyle(self, cs)
  494. self._pen.SetCap(GraphicsContextWx._capd[self._capstyle])
  495. self.gfx_ctx.SetPen(self._pen)
  496. self.unselect()
  497.  
  498. def set_joinstyle(self, js):
  499. """
  500. Set the join style to be one of ('miter', 'round', 'bevel')
  501. """
  502. DEBUG_MSG("set_joinstyle()", 1, self)
  503. self.select()
  504. GraphicsContextBase.set_joinstyle(self, js)
  505. self._pen.SetJoin(GraphicsContextWx._joind[self._joinstyle])
  506. self.gfx_ctx.SetPen(self._pen)
  507. self.unselect()
  508.  
  509. @cbook.deprecated("2.1")
  510. def set_linestyle(self, ls):
  511. """
  512. Set the line style to be one of
  513. """
  514. DEBUG_MSG("set_linestyle()", 1, self)
  515. self.select()
  516. GraphicsContextBase.set_linestyle(self, ls)
  517. try:
  518. self._style = wxc.dashd_wx[ls]
  519. except KeyError:
  520. self._style = wx.LONG_DASH # Style not used elsewhere...
  521.  
  522. # On MS Windows platform, only line width of 1 allowed for dash lines
  523. if wx.Platform == '__WXMSW__':
  524. self.set_linewidth(1)
  525.  
  526. self._pen.SetStyle(self._style)
  527. self.gfx_ctx.SetPen(self._pen)
  528. self.unselect()
  529.  
  530. def get_wxcolour(self, color):
  531. """return a wx.Colour from RGB format"""
  532. DEBUG_MSG("get_wx_color()", 1, self)
  533. if len(color) == 3:
  534. r, g, b = color
  535. r *= 255
  536. g *= 255
  537. b *= 255
  538. return wx.Colour(red=int(r), green=int(g), blue=int(b))
  539. else:
  540. r, g, b, a = color
  541. r *= 255
  542. g *= 255
  543. b *= 255
  544. a *= 255
  545. return wx.Colour(
  546. red=int(r),
  547. green=int(g),
  548. blue=int(b),
  549. alpha=int(a))
  550.  
  551.  
  552. class FigureCanvasWx(FigureCanvasBase, wx.Panel):
  553. """
  554. The FigureCanvas contains the figure and does event handling.
  555.  
  556. In the wxPython backend, it is derived from wxPanel, and (usually) lives
  557. inside a frame instantiated by a FigureManagerWx. The parent window
  558. probably implements a wx.Sizer to control the displayed control size - but
  559. we give a hint as to our preferred minimum size.
  560. """
  561.  
  562. keyvald = {
  563. wx.WXK_CONTROL: 'control',
  564. wx.WXK_SHIFT: 'shift',
  565. wx.WXK_ALT: 'alt',
  566. wx.WXK_LEFT: 'left',
  567. wx.WXK_UP: 'up',
  568. wx.WXK_RIGHT: 'right',
  569. wx.WXK_DOWN: 'down',
  570. wx.WXK_ESCAPE: 'escape',
  571. wx.WXK_F1: 'f1',
  572. wx.WXK_F2: 'f2',
  573. wx.WXK_F3: 'f3',
  574. wx.WXK_F4: 'f4',
  575. wx.WXK_F5: 'f5',
  576. wx.WXK_F6: 'f6',
  577. wx.WXK_F7: 'f7',
  578. wx.WXK_F8: 'f8',
  579. wx.WXK_F9: 'f9',
  580. wx.WXK_F10: 'f10',
  581. wx.WXK_F11: 'f11',
  582. wx.WXK_F12: 'f12',
  583. wx.WXK_SCROLL: 'scroll_lock',
  584. wx.WXK_PAUSE: 'break',
  585. wx.WXK_BACK: 'backspace',
  586. wx.WXK_RETURN: 'enter',
  587. wx.WXK_INSERT: 'insert',
  588. wx.WXK_DELETE: 'delete',
  589. wx.WXK_HOME: 'home',
  590. wx.WXK_END: 'end',
  591. wx.WXK_PAGEUP: 'pageup',
  592. wx.WXK_PAGEDOWN: 'pagedown',
  593. wx.WXK_NUMPAD0: '0',
  594. wx.WXK_NUMPAD1: '1',
  595. wx.WXK_NUMPAD2: '2',
  596. wx.WXK_NUMPAD3: '3',
  597. wx.WXK_NUMPAD4: '4',
  598. wx.WXK_NUMPAD5: '5',
  599. wx.WXK_NUMPAD6: '6',
  600. wx.WXK_NUMPAD7: '7',
  601. wx.WXK_NUMPAD8: '8',
  602. wx.WXK_NUMPAD9: '9',
  603. wx.WXK_NUMPAD_ADD: '+',
  604. wx.WXK_NUMPAD_SUBTRACT: '-',
  605. wx.WXK_NUMPAD_MULTIPLY: '*',
  606. wx.WXK_NUMPAD_DIVIDE: '/',
  607. wx.WXK_NUMPAD_DECIMAL: 'dec',
  608. wx.WXK_NUMPAD_ENTER: 'enter',
  609. wx.WXK_NUMPAD_UP: 'up',
  610. wx.WXK_NUMPAD_RIGHT: 'right',
  611. wx.WXK_NUMPAD_DOWN: 'down',
  612. wx.WXK_NUMPAD_LEFT: 'left',
  613. wx.WXK_NUMPAD_PAGEUP: 'pageup',
  614. wx.WXK_NUMPAD_PAGEDOWN: 'pagedown',
  615. wx.WXK_NUMPAD_HOME: 'home',
  616. wx.WXK_NUMPAD_END: 'end',
  617. wx.WXK_NUMPAD_INSERT: 'insert',
  618. wx.WXK_NUMPAD_DELETE: 'delete',
  619. }
  620.  
  621. def __init__(self, parent, id, figure):
  622. """
  623. Initialise a FigureWx instance.
  624.  
  625. - Initialise the FigureCanvasBase and wxPanel parents.
  626. - Set event handlers for:
  627. EVT_SIZE (Resize event)
  628. EVT_PAINT (Paint event)
  629. """
  630.  
  631. FigureCanvasBase.__init__(self, figure)
  632. # Set preferred window size hint - helps the sizer (if one is
  633. # connected)
  634. l, b, w, h = figure.bbox.bounds
  635. w = int(math.ceil(w))
  636. h = int(math.ceil(h))
  637.  
  638. wx.Panel.__init__(self, parent, id, size=wx.Size(w, h))
  639.  
  640. def do_nothing(*args, **kwargs):
  641. warnings.warn(
  642. "could not find a setinitialsize function for backend_wx; "
  643. "please report your wxpython version=%s "
  644. "to the matplotlib developers list" %
  645. wxc.backend_version)
  646. pass
  647.  
  648. # try to find the set size func across wx versions
  649. try:
  650. getattr(self, 'SetInitialSize')
  651. except AttributeError:
  652. self.SetInitialSize = getattr(self, 'SetBestFittingSize',
  653. do_nothing)
  654.  
  655. if not hasattr(self, 'IsShownOnScreen'):
  656. self.IsShownOnScreen = getattr(self, 'IsVisible',
  657. lambda *args: True)
  658.  
  659. # Create the drawing bitmap
  660. self.bitmap = wxc.EmptyBitmap(w, h)
  661. DEBUG_MSG("__init__() - bitmap w:%d h:%d" % (w, h), 2, self)
  662. # TODO: Add support for 'point' inspection and plot navigation.
  663. self._isDrawn = False
  664.  
  665. self.Bind(wx.EVT_SIZE, self._onSize)
  666. self.Bind(wx.EVT_PAINT, self._onPaint)
  667. self.Bind(wx.EVT_KEY_DOWN, self._onKeyDown)
  668. self.Bind(wx.EVT_KEY_UP, self._onKeyUp)
  669. self.Bind(wx.EVT_RIGHT_DOWN, self._onRightButtonDown)
  670. self.Bind(wx.EVT_RIGHT_DCLICK, self._onRightButtonDClick)
  671. self.Bind(wx.EVT_RIGHT_UP, self._onRightButtonUp)
  672. self.Bind(wx.EVT_MOUSEWHEEL, self._onMouseWheel)
  673. self.Bind(wx.EVT_LEFT_DOWN, self._onLeftButtonDown)
  674. self.Bind(wx.EVT_LEFT_DCLICK, self._onLeftButtonDClick)
  675. self.Bind(wx.EVT_LEFT_UP, self._onLeftButtonUp)
  676. self.Bind(wx.EVT_MOTION, self._onMotion)
  677. self.Bind(wx.EVT_LEAVE_WINDOW, self._onLeave)
  678. self.Bind(wx.EVT_ENTER_WINDOW, self._onEnter)
  679. # Add middle button events
  680. self.Bind(wx.EVT_MIDDLE_DOWN, self._onMiddleButtonDown)
  681. self.Bind(wx.EVT_MIDDLE_DCLICK, self._onMiddleButtonDClick)
  682. self.Bind(wx.EVT_MIDDLE_UP, self._onMiddleButtonUp)
  683.  
  684. self.Bind(wx.EVT_MOUSE_CAPTURE_CHANGED, self._onCaptureLost)
  685. self.Bind(wx.EVT_MOUSE_CAPTURE_LOST, self._onCaptureLost)
  686.  
  687. if wx.VERSION_STRING < "2.9":
  688. # only needed in 2.8 to reduce flicker
  689. self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM)
  690. self.Bind(wx.EVT_ERASE_BACKGROUND, self._onEraseBackground)
  691. else:
  692. # this does the same in 2.9+
  693. self.SetBackgroundStyle(wx.BG_STYLE_PAINT)
  694.  
  695. self.macros = {} # dict from wx id to seq of macros
  696.  
  697. def Destroy(self, *args, **kwargs):
  698. wx.Panel.Destroy(self, *args, **kwargs)
  699.  
  700. def Copy_to_Clipboard(self, event=None):
  701. "copy bitmap of canvas to system clipboard"
  702. bmp_obj = wx.BitmapDataObject()
  703. bmp_obj.SetBitmap(self.bitmap)
  704.  
  705. if not wx.TheClipboard.IsOpened():
  706. open_success = wx.TheClipboard.Open()
  707. if open_success:
  708. wx.TheClipboard.SetData(bmp_obj)
  709. wx.TheClipboard.Close()
  710. wx.TheClipboard.Flush()
  711.  
  712. def draw_idle(self):
  713. """
  714. Delay rendering until the GUI is idle.
  715. """
  716. DEBUG_MSG("draw_idle()", 1, self)
  717. self._isDrawn = False # Force redraw
  718.  
  719. # Triggering a paint event is all that is needed to defer drawing
  720. # until later. The platform will send the event when it thinks it is
  721. # a good time (usually as soon as there are no other events pending).
  722. self.Refresh(eraseBackground=False)
  723.  
  724. def draw(self, drawDC=None):
  725. """
  726. Render the figure using RendererWx instance renderer, or using a
  727. previously defined renderer if none is specified.
  728. """
  729. DEBUG_MSG("draw()", 1, self)
  730. self.renderer = RendererWx(self.bitmap, self.figure.dpi)
  731. self.figure.draw(self.renderer)
  732. self._isDrawn = True
  733. self.gui_repaint(drawDC=drawDC)
  734.  
  735. def new_timer(self, *args, **kwargs):
  736. """
  737. Creates a new backend-specific subclass of
  738. :class:`backend_bases.Timer`. This is useful for getting periodic
  739. events through the backend's native event loop. Implemented only
  740. for backends with GUIs.
  741.  
  742. Other Parameters
  743. ----------------
  744. interval : scalar
  745. Timer interval in milliseconds
  746. callbacks : list
  747. Sequence of (func, args, kwargs) where ``func(*args, **kwargs)``
  748. will be executed by the timer every *interval*.
  749.  
  750. """
  751. return TimerWx(self, *args, **kwargs)
  752.  
  753. def flush_events(self):
  754. wx.Yield()
  755.  
  756. def start_event_loop(self, timeout=0):
  757. """
  758. Start an event loop. This is used to start a blocking event
  759. loop so that interactive functions, such as ginput and
  760. waitforbuttonpress, can wait for events. This should not be
  761. confused with the main GUI event loop, which is always running
  762. and has nothing to do with this.
  763.  
  764. This call blocks until a callback function triggers
  765. stop_event_loop() or *timeout* is reached. If *timeout* is
  766. <=0, never timeout.
  767.  
  768. Raises RuntimeError if event loop is already running.
  769. """
  770. if hasattr(self, '_event_loop'):
  771. raise RuntimeError("Event loop already running")
  772. id = wx.NewId()
  773. timer = wx.Timer(self, id=id)
  774. if timeout > 0:
  775. timer.Start(timeout * 1000, oneShot=True)
  776. self.Bind(wx.EVT_TIMER, self.stop_event_loop, id=id)
  777.  
  778. # Event loop handler for start/stop event loop
  779. self._event_loop = wxc.EventLoop()
  780. self._event_loop.Run()
  781. timer.Stop()
  782.  
  783. def stop_event_loop(self, event=None):
  784. """
  785. Stop an event loop. This is used to stop a blocking event
  786. loop so that interactive functions, such as ginput and
  787. waitforbuttonpress, can wait for events.
  788.  
  789. """
  790. if hasattr(self, '_event_loop'):
  791. if self._event_loop.IsRunning():
  792. self._event_loop.Exit()
  793. del self._event_loop
  794.  
  795. def _get_imagesave_wildcards(self):
  796. 'return the wildcard string for the filesave dialog'
  797. default_filetype = self.get_default_filetype()
  798. filetypes = self.get_supported_filetypes_grouped()
  799. sorted_filetypes = sorted(filetypes.items())
  800. wildcards = []
  801. extensions = []
  802. filter_index = 0
  803. for i, (name, exts) in enumerate(sorted_filetypes):
  804. ext_list = ';'.join(['*.%s' % ext for ext in exts])
  805. extensions.append(exts[0])
  806. wildcard = '%s (%s)|%s' % (name, ext_list, ext_list)
  807. if default_filetype in exts:
  808. filter_index = i
  809. wildcards.append(wildcard)
  810. wildcards = '|'.join(wildcards)
  811. return wildcards, extensions, filter_index
  812.  
  813. def gui_repaint(self, drawDC=None, origin='WX'):
  814. """
  815. Performs update of the displayed image on the GUI canvas, using the
  816. supplied wx.PaintDC device context.
  817.  
  818. The 'WXAgg' backend sets origin accordingly.
  819. """
  820. DEBUG_MSG("gui_repaint()", 1, self)
  821. if self.IsShownOnScreen():
  822. if not drawDC:
  823. # not called from OnPaint use a ClientDC
  824. drawDC = wx.ClientDC(self)
  825.  
  826. # following is for 'WX' backend on Windows
  827. # the bitmap can not be in use by another DC,
  828. # see GraphicsContextWx._cache
  829. if wx.Platform == '__WXMSW__' and origin == 'WX':
  830. img = self.bitmap.ConvertToImage()
  831. bmp = img.ConvertToBitmap()
  832. drawDC.DrawBitmap(bmp, 0, 0)
  833. else:
  834. drawDC.DrawBitmap(self.bitmap, 0, 0)
  835.  
  836. filetypes = FigureCanvasBase.filetypes.copy()
  837. filetypes['bmp'] = 'Windows bitmap'
  838. filetypes['jpeg'] = 'JPEG'
  839. filetypes['jpg'] = 'JPEG'
  840. filetypes['pcx'] = 'PCX'
  841. filetypes['png'] = 'Portable Network Graphics'
  842. filetypes['tif'] = 'Tagged Image Format File'
  843. filetypes['tiff'] = 'Tagged Image Format File'
  844. filetypes['xpm'] = 'X pixmap'
  845.  
  846. def print_figure(self, filename, *args, **kwargs):
  847. # Use pure Agg renderer to draw
  848. FigureCanvasBase.print_figure(self, filename, *args, **kwargs)
  849. # Restore the current view; this is needed because the
  850. # artist contains methods rely on particular attributes
  851. # of the rendered figure for determining things like
  852. # bounding boxes.
  853. if self._isDrawn:
  854. self.draw()
  855.  
  856. def print_bmp(self, filename, *args, **kwargs):
  857. return self._print_image(filename, wx.BITMAP_TYPE_BMP, *args, **kwargs)
  858.  
  859. if not _has_pil:
  860. def print_jpeg(self, filename, *args, **kwargs):
  861. return self._print_image(filename, wx.BITMAP_TYPE_JPEG,
  862. *args, **kwargs)
  863. print_jpg = print_jpeg
  864.  
  865. def print_pcx(self, filename, *args, **kwargs):
  866. return self._print_image(filename, wx.BITMAP_TYPE_PCX, *args, **kwargs)
  867.  
  868. def print_png(self, filename, *args, **kwargs):
  869. return self._print_image(filename, wx.BITMAP_TYPE_PNG, *args, **kwargs)
  870.  
  871. if not _has_pil:
  872. def print_tiff(self, filename, *args, **kwargs):
  873. return self._print_image(filename, wx.BITMAP_TYPE_TIF,
  874. *args, **kwargs)
  875. print_tif = print_tiff
  876.  
  877. def print_xpm(self, filename, *args, **kwargs):
  878. return self._print_image(filename, wx.BITMAP_TYPE_XPM, *args, **kwargs)
  879.  
  880. def _print_image(self, filename, filetype, *args, **kwargs):
  881. origBitmap = self.bitmap
  882.  
  883. l, b, width, height = self.figure.bbox.bounds
  884. width = int(math.ceil(width))
  885. height = int(math.ceil(height))
  886.  
  887. self.bitmap = wxc.EmptyBitmap(width, height)
  888.  
  889. renderer = RendererWx(self.bitmap, self.figure.dpi)
  890.  
  891. gc = renderer.new_gc()
  892.  
  893. self.figure.draw(renderer)
  894.  
  895. # image is the object that we call SaveFile on.
  896. image = self.bitmap
  897. # set the JPEG quality appropriately. Unfortunately, it is only
  898. # possible to set the quality on a wx.Image object. So if we
  899. # are saving a JPEG, convert the wx.Bitmap to a wx.Image,
  900. # and set the quality.
  901. if filetype == wx.BITMAP_TYPE_JPEG:
  902. jpeg_quality = kwargs.get('quality',
  903. rcParams['savefig.jpeg_quality'])
  904. image = self.bitmap.ConvertToImage()
  905. image.SetOption(wx.IMAGE_OPTION_QUALITY, str(jpeg_quality))
  906.  
  907. # Now that we have rendered into the bitmap, save it
  908. # to the appropriate file type and clean up
  909. if isinstance(filename, six.string_types):
  910. if not image.SaveFile(filename, filetype):
  911. DEBUG_MSG('print_figure() file save error', 4, self)
  912. raise RuntimeError(
  913. 'Could not save figure to %s\n' %
  914. (filename))
  915. elif is_writable_file_like(filename):
  916. if not isinstance(image, wx.Image):
  917. image = image.ConvertToImage()
  918. if not image.SaveStream(filename, filetype):
  919. DEBUG_MSG('print_figure() file save error', 4, self)
  920. raise RuntimeError(
  921. 'Could not save figure to %s\n' %
  922. (filename))
  923.  
  924. # Restore everything to normal
  925. self.bitmap = origBitmap
  926.  
  927. # Note: draw is required here since bits of state about the
  928. # last renderer are strewn about the artist draw methods. Do
  929. # not remove the draw without first verifying that these have
  930. # been cleaned up. The artist contains() methods will fail
  931. # otherwise.
  932. if self._isDrawn:
  933. self.draw()
  934. self.Refresh()
  935.  
  936. def _onPaint(self, evt):
  937. """
  938. Called when wxPaintEvt is generated
  939. """
  940.  
  941. DEBUG_MSG("_onPaint()", 1, self)
  942. drawDC = wx.PaintDC(self)
  943. if not self._isDrawn:
  944. self.draw(drawDC=drawDC)
  945. else:
  946. self.gui_repaint(drawDC=drawDC)
  947. evt.Skip()
  948.  
  949. def _onEraseBackground(self, evt):
  950. """
  951. Called when window is redrawn; since we are blitting the entire
  952. image, we can leave this blank to suppress flicker.
  953. """
  954. pass
  955.  
  956. def _onSize(self, evt):
  957. """
  958. Called when wxEventSize is generated.
  959.  
  960. In this application we attempt to resize to fit the window, so it
  961. is better to take the performance hit and redraw the whole window.
  962. """
  963.  
  964. DEBUG_MSG("_onSize()", 2, self)
  965. # Create a new, correctly sized bitmap
  966. self._width, self._height = self.GetClientSize()
  967. self.bitmap = wxc.EmptyBitmap(self._width, self._height)
  968.  
  969. self._isDrawn = False
  970.  
  971. if self._width <= 1 or self._height <= 1:
  972. return # Empty figure
  973.  
  974. dpival = self.figure.dpi
  975. winch = self._width / dpival
  976. hinch = self._height / dpival
  977. self.figure.set_size_inches(winch, hinch, forward=False)
  978.  
  979. # Rendering will happen on the associated paint event
  980. # so no need to do anything here except to make sure
  981. # the whole background is repainted.
  982. self.Refresh(eraseBackground=False)
  983. FigureCanvasBase.resize_event(self)
  984.  
  985. def _get_key(self, evt):
  986.  
  987. keyval = evt.KeyCode
  988. if keyval in self.keyvald:
  989. key = self.keyvald[keyval]
  990. elif keyval < 256:
  991. key = chr(keyval)
  992. # wx always returns an uppercase, so make it lowercase if the shift
  993. # key is not depressed (NOTE: this will not handle Caps Lock)
  994. if not evt.ShiftDown():
  995. key = key.lower()
  996. else:
  997. key = None
  998.  
  999. for meth, prefix in (
  1000. [evt.AltDown, 'alt'],
  1001. [evt.ControlDown, 'ctrl'], ):
  1002. if meth():
  1003. key = '{0}+{1}'.format(prefix, key)
  1004.  
  1005. return key
  1006.  
  1007. def _onKeyDown(self, evt):
  1008. """Capture key press."""
  1009. key = self._get_key(evt)
  1010. evt.Skip()
  1011. FigureCanvasBase.key_press_event(self, key, guiEvent=evt)
  1012.  
  1013. def _onKeyUp(self, evt):
  1014. """Release key."""
  1015. key = self._get_key(evt)
  1016. # print 'release key', key
  1017. evt.Skip()
  1018. FigureCanvasBase.key_release_event(self, key, guiEvent=evt)
  1019.  
  1020. def _set_capture(self, capture=True):
  1021. """control wx mouse capture """
  1022. if self.HasCapture():
  1023. self.ReleaseMouse()
  1024. if capture:
  1025. self.CaptureMouse()
  1026.  
  1027. def _onCaptureLost(self, evt):
  1028. """Capture changed or lost"""
  1029. self._set_capture(False)
  1030.  
  1031. def _onRightButtonDown(self, evt):
  1032. """Start measuring on an axis."""
  1033. x = evt.GetX()
  1034. y = self.figure.bbox.height - evt.GetY()
  1035. evt.Skip()
  1036. self._set_capture(True)
  1037. FigureCanvasBase.button_press_event(self, x, y, 3, guiEvent=evt)
  1038.  
  1039. def _onRightButtonDClick(self, evt):
  1040. """Start measuring on an axis."""
  1041. x = evt.GetX()
  1042. y = self.figure.bbox.height - evt.GetY()
  1043. evt.Skip()
  1044. self._set_capture(True)
  1045. FigureCanvasBase.button_press_event(self, x, y, 3,
  1046. dblclick=True, guiEvent=evt)
  1047.  
  1048. def _onRightButtonUp(self, evt):
  1049. """End measuring on an axis."""
  1050. x = evt.GetX()
  1051. y = self.figure.bbox.height - evt.GetY()
  1052. evt.Skip()
  1053. self._set_capture(False)
  1054. FigureCanvasBase.button_release_event(self, x, y, 3, guiEvent=evt)
  1055.  
  1056. def _onLeftButtonDown(self, evt):
  1057. """Start measuring on an axis."""
  1058. x = evt.GetX()
  1059. y = self.figure.bbox.height - evt.GetY()
  1060. evt.Skip()
  1061. self._set_capture(True)
  1062. FigureCanvasBase.button_press_event(self, x, y, 1, guiEvent=evt)
  1063.  
  1064. def _onLeftButtonDClick(self, evt):
  1065. """Start measuring on an axis."""
  1066. x = evt.GetX()
  1067. y = self.figure.bbox.height - evt.GetY()
  1068. evt.Skip()
  1069. self._set_capture(True)
  1070. FigureCanvasBase.button_press_event(self, x, y, 1,
  1071. dblclick=True, guiEvent=evt)
  1072.  
  1073. def _onLeftButtonUp(self, evt):
  1074. """End measuring on an axis."""
  1075. x = evt.GetX()
  1076. y = self.figure.bbox.height - evt.GetY()
  1077. # print 'release button', 1
  1078. evt.Skip()
  1079. self._set_capture(False)
  1080. FigureCanvasBase.button_release_event(self, x, y, 1, guiEvent=evt)
  1081.  
  1082. # Add middle button events
  1083. def _onMiddleButtonDown(self, evt):
  1084. """Start measuring on an axis."""
  1085. x = evt.GetX()
  1086. y = self.figure.bbox.height - evt.GetY()
  1087. evt.Skip()
  1088. self._set_capture(True)
  1089. FigureCanvasBase.button_press_event(self, x, y, 2, guiEvent=evt)
  1090.  
  1091. def _onMiddleButtonDClick(self, evt):
  1092. """Start measuring on an axis."""
  1093. x = evt.GetX()
  1094. y = self.figure.bbox.height - evt.GetY()
  1095. evt.Skip()
  1096. self._set_capture(True)
  1097. FigureCanvasBase.button_press_event(self, x, y, 2,
  1098. dblclick=True, guiEvent=evt)
  1099.  
  1100. def _onMiddleButtonUp(self, evt):
  1101. """End measuring on an axis."""
  1102. x = evt.GetX()
  1103. y = self.figure.bbox.height - evt.GetY()
  1104. # print 'release button', 1
  1105. evt.Skip()
  1106. self._set_capture(False)
  1107. FigureCanvasBase.button_release_event(self, x, y, 2, guiEvent=evt)
  1108.  
  1109. def _onMouseWheel(self, evt):
  1110. """Translate mouse wheel events into matplotlib events"""
  1111.  
  1112. # Determine mouse location
  1113. x = evt.GetX()
  1114. y = self.figure.bbox.height - evt.GetY()
  1115.  
  1116. # Convert delta/rotation/rate into a floating point step size
  1117. delta = evt.GetWheelDelta()
  1118. rotation = evt.GetWheelRotation()
  1119. rate = evt.GetLinesPerAction()
  1120. # print "delta,rotation,rate",delta,rotation,rate
  1121. step = rate * float(rotation) / delta
  1122.  
  1123. # Done handling event
  1124. evt.Skip()
  1125.  
  1126. # Mac is giving two events for every wheel event
  1127. # Need to skip every second one
  1128. if wx.Platform == '__WXMAC__':
  1129. if not hasattr(self, '_skipwheelevent'):
  1130. self._skipwheelevent = True
  1131. elif self._skipwheelevent:
  1132. self._skipwheelevent = False
  1133. return # Return without processing event
  1134. else:
  1135. self._skipwheelevent = True
  1136.  
  1137. # Convert to mpl event
  1138. FigureCanvasBase.scroll_event(self, x, y, step, guiEvent=evt)
  1139.  
  1140. def _onMotion(self, evt):
  1141. """Start measuring on an axis."""
  1142.  
  1143. x = evt.GetX()
  1144. y = self.figure.bbox.height - evt.GetY()
  1145. evt.Skip()
  1146. FigureCanvasBase.motion_notify_event(self, x, y, guiEvent=evt)
  1147.  
  1148. def _onLeave(self, evt):
  1149. """Mouse has left the window."""
  1150.  
  1151. evt.Skip()
  1152. FigureCanvasBase.leave_notify_event(self, guiEvent=evt)
  1153.  
  1154. def _onEnter(self, evt):
  1155. """Mouse has entered the window."""
  1156. FigureCanvasBase.enter_notify_event(self, guiEvent=evt)
  1157.  
  1158.  
  1159. ########################################################################
  1160. #
  1161. # The following functions and classes are for pylab compatibility
  1162. # mode (matplotlib.pylab) and implement figure managers, etc...
  1163. #
  1164. ########################################################################
  1165.  
  1166.  
  1167. class FigureFrameWx(wx.Frame):
  1168. def __init__(self, num, fig):
  1169. # On non-Windows platform, explicitly set the position - fix
  1170. # positioning bug on some Linux platforms
  1171. if wx.Platform == '__WXMSW__':
  1172. pos = wx.DefaultPosition
  1173. else:
  1174. pos = wx.Point(20, 20)
  1175. l, b, w, h = fig.bbox.bounds
  1176. wx.Frame.__init__(self, parent=None, id=-1, pos=pos,
  1177. title="Figure %d" % num)
  1178. # Frame will be sized later by the Fit method
  1179. DEBUG_MSG("__init__()", 1, self)
  1180. self.num = num
  1181.  
  1182. statbar = StatusBarWx(self)
  1183. self.SetStatusBar(statbar)
  1184. self.canvas = self.get_canvas(fig)
  1185. self.canvas.SetInitialSize(wx.Size(fig.bbox.width, fig.bbox.height))
  1186. self.canvas.SetFocus()
  1187. self.sizer = wx.BoxSizer(wx.VERTICAL)
  1188. self.sizer.Add(self.canvas, 1, wx.TOP | wx.LEFT | wx.EXPAND)
  1189. # By adding toolbar in sizer, we are able to put it at the bottom
  1190. # of the frame - so appearance is closer to GTK version
  1191.  
  1192. self.toolbar = self._get_toolbar(statbar)
  1193.  
  1194. if self.toolbar is not None:
  1195. self.toolbar.Realize()
  1196. # On Windows platform, default window size is incorrect, so set
  1197. # toolbar width to figure width.
  1198. if wxc.is_phoenix:
  1199. tw, th = self.toolbar.GetSize()
  1200. fw, fh = self.canvas.GetSize()
  1201. else:
  1202. tw, th = self.toolbar.GetSizeTuple()
  1203. fw, fh = self.canvas.GetSizeTuple()
  1204. # By adding toolbar in sizer, we are able to put it at the bottom
  1205. # of the frame - so appearance is closer to GTK version.
  1206. self.toolbar.SetSize(wx.Size(fw, th))
  1207. self.sizer.Add(self.toolbar, 0, wx.LEFT | wx.EXPAND)
  1208. self.SetSizer(self.sizer)
  1209. self.Fit()
  1210.  
  1211. self.canvas.SetMinSize((2, 2))
  1212.  
  1213. # give the window a matplotlib icon rather than the stock one.
  1214. # This is not currently working on Linux and is untested elsewhere.
  1215. # icon_path = os.path.join(matplotlib.rcParams['datapath'],
  1216. # 'images', 'matplotlib.png')
  1217. #icon = wx.IconFromBitmap(wx.Bitmap(icon_path))
  1218. # for xpm type icons try:
  1219. #icon = wx.Icon(icon_path, wx.BITMAP_TYPE_XPM)
  1220. # self.SetIcon(icon)
  1221.  
  1222. self.figmgr = FigureManagerWx(self.canvas, num, self)
  1223.  
  1224. self.Bind(wx.EVT_CLOSE, self._onClose)
  1225.  
  1226. def _get_toolbar(self, statbar):
  1227. if rcParams['toolbar'] == 'toolbar2':
  1228. toolbar = NavigationToolbar2Wx(self.canvas)
  1229. toolbar.set_status_bar(statbar)
  1230. else:
  1231. toolbar = None
  1232. return toolbar
  1233.  
  1234. def get_canvas(self, fig):
  1235. return FigureCanvasWx(self, -1, fig)
  1236.  
  1237. def get_figure_manager(self):
  1238. DEBUG_MSG("get_figure_manager()", 1, self)
  1239. return self.figmgr
  1240.  
  1241. def _onClose(self, evt):
  1242. DEBUG_MSG("onClose()", 1, self)
  1243. self.canvas.close_event()
  1244. self.canvas.stop_event_loop()
  1245. Gcf.destroy(self.num)
  1246. # self.Destroy()
  1247.  
  1248. def GetToolBar(self):
  1249. """Override wxFrame::GetToolBar as we don't have managed toolbar"""
  1250. return self.toolbar
  1251.  
  1252. def Destroy(self, *args, **kwargs):
  1253. try:
  1254. self.canvas.mpl_disconnect(self.toolbar._idDrag)
  1255. # Rationale for line above: see issue 2941338.
  1256. except AttributeError:
  1257. pass # classic toolbar lacks the attribute
  1258. if not self.IsBeingDeleted():
  1259. wx.Frame.Destroy(self, *args, **kwargs)
  1260. if self.toolbar is not None:
  1261. self.toolbar.Destroy()
  1262. wxapp = wx.GetApp()
  1263. if wxapp:
  1264. wxapp.Yield()
  1265. return True
  1266.  
  1267.  
  1268. class FigureManagerWx(FigureManagerBase):
  1269. """
  1270. This class contains the FigureCanvas and GUI frame
  1271.  
  1272. It is instantiated by GcfWx whenever a new figure is created. GcfWx is
  1273. responsible for managing multiple instances of FigureManagerWx.
  1274.  
  1275. Attributes
  1276. ----------
  1277. canvas : `FigureCanvas`
  1278. a FigureCanvasWx(wx.Panel) instance
  1279. window : wxFrame
  1280. a wxFrame instance - wxpython.org/Phoenix/docs/html/Frame.html
  1281.  
  1282. """
  1283.  
  1284. def __init__(self, canvas, num, frame):
  1285. DEBUG_MSG("__init__()", 1, self)
  1286. FigureManagerBase.__init__(self, canvas, num)
  1287. self.frame = frame
  1288. self.window = frame
  1289.  
  1290. self.tb = frame.GetToolBar()
  1291. self.toolbar = self.tb # consistent with other backends
  1292.  
  1293. def notify_axes_change(fig):
  1294. 'this will be called whenever the current axes is changed'
  1295. if self.tb is not None:
  1296. self.tb.update()
  1297. self.canvas.figure.add_axobserver(notify_axes_change)
  1298.  
  1299. def show(self):
  1300. self.frame.Show()
  1301. self.canvas.draw()
  1302.  
  1303. def destroy(self, *args):
  1304. DEBUG_MSG("destroy()", 1, self)
  1305. self.frame.Destroy()
  1306. wxapp = wx.GetApp()
  1307. if wxapp:
  1308. wxapp.Yield()
  1309.  
  1310. def get_window_title(self):
  1311. return self.window.GetTitle()
  1312.  
  1313. def set_window_title(self, title):
  1314. self.window.SetTitle(title)
  1315.  
  1316. def resize(self, width, height):
  1317. 'Set the canvas size in pixels'
  1318. self.canvas.SetInitialSize(wx.Size(width, height))
  1319. self.window.GetSizer().Fit(self.window)
  1320.  
  1321. # Identifiers for toolbar controls - images_wx contains bitmaps for the images
  1322. # used in the controls. wxWindows does not provide any stock images, so I've
  1323. # 'stolen' those from GTK2, and transformed them into the appropriate format.
  1324. #import images_wx
  1325.  
  1326. _NTB_AXISMENU = wx.NewId()
  1327. _NTB_AXISMENU_BUTTON = wx.NewId()
  1328. _NTB_X_PAN_LEFT = wx.NewId()
  1329. _NTB_X_PAN_RIGHT = wx.NewId()
  1330. _NTB_X_ZOOMIN = wx.NewId()
  1331. _NTB_X_ZOOMOUT = wx.NewId()
  1332. _NTB_Y_PAN_UP = wx.NewId()
  1333. _NTB_Y_PAN_DOWN = wx.NewId()
  1334. _NTB_Y_ZOOMIN = wx.NewId()
  1335. _NTB_Y_ZOOMOUT = wx.NewId()
  1336. #_NTB_SUBPLOT =wx.NewId()
  1337. _NTB_SAVE = wx.NewId()
  1338. _NTB_CLOSE = wx.NewId()
  1339.  
  1340.  
  1341. def _load_bitmap(filename):
  1342. """
  1343. Load a bitmap file from the backends/images subdirectory in which the
  1344. matplotlib library is installed. The filename parameter should not
  1345. contain any path information as this is determined automatically.
  1346.  
  1347. Returns a wx.Bitmap object
  1348. """
  1349.  
  1350. basedir = os.path.join(rcParams['datapath'], 'images')
  1351.  
  1352. bmpFilename = os.path.normpath(os.path.join(basedir, filename))
  1353. if not os.path.exists(bmpFilename):
  1354. raise IOError('Could not find bitmap file "%s"; dying' % bmpFilename)
  1355.  
  1356. bmp = wx.Bitmap(bmpFilename)
  1357. return bmp
  1358.  
  1359.  
  1360. class MenuButtonWx(wx.Button):
  1361. """
  1362. wxPython does not permit a menu to be incorporated directly into a toolbar.
  1363. This class simulates the effect by associating a pop-up menu with a button
  1364. in the toolbar, and managing this as though it were a menu.
  1365. """
  1366.  
  1367. def __init__(self, parent):
  1368.  
  1369. wx.Button.__init__(self, parent, _NTB_AXISMENU_BUTTON, "Axes: ",
  1370. style=wx.BU_EXACTFIT)
  1371. self._toolbar = parent
  1372. self._menu = wx.Menu()
  1373. self._axisId = []
  1374. # First two menu items never change...
  1375. self._allId = wx.NewId()
  1376. self._invertId = wx.NewId()
  1377. self._menu.Append(self._allId, "All", "Select all axes", False)
  1378. self._menu.Append(self._invertId, "Invert", "Invert axes selected",
  1379. False)
  1380. self._menu.AppendSeparator()
  1381.  
  1382. self.Bind(wx.EVT_BUTTON, self._onMenuButton, id=_NTB_AXISMENU_BUTTON)
  1383. self.Bind(wx.EVT_MENU, self._handleSelectAllAxes, id=self._allId)
  1384. self.Bind(wx.EVT_MENU, self._handleInvertAxesSelected,
  1385. id=self._invertId)
  1386.  
  1387. def Destroy(self):
  1388. self._menu.Destroy()
  1389. self.Destroy()
  1390.  
  1391. def _onMenuButton(self, evt):
  1392. """Handle menu button pressed."""
  1393. if wxc.is_phoenix:
  1394. x, y = self.GetPosition()
  1395. w, h = self.GetSize()
  1396. else:
  1397. x, y = self.GetPositionTuple()
  1398. w, h = self.GetSizeTuple()
  1399. self.PopupMenuXY(self._menu, x, y + h - 4)
  1400. # When menu returned, indicate selection in button
  1401. evt.Skip()
  1402.  
  1403. def _handleSelectAllAxes(self, evt):
  1404. """Called when the 'select all axes' menu item is selected."""
  1405. if len(self._axisId) == 0:
  1406. return
  1407. for i in range(len(self._axisId)):
  1408. self._menu.Check(self._axisId[i], True)
  1409. self._toolbar.set_active(self.getActiveAxes())
  1410. evt.Skip()
  1411.  
  1412. def _handleInvertAxesSelected(self, evt):
  1413. """Called when the invert all menu item is selected"""
  1414. if len(self._axisId) == 0:
  1415. return
  1416. for i in range(len(self._axisId)):
  1417. if self._menu.IsChecked(self._axisId[i]):
  1418. self._menu.Check(self._axisId[i], False)
  1419. else:
  1420. self._menu.Check(self._axisId[i], True)
  1421. self._toolbar.set_active(self.getActiveAxes())
  1422. evt.Skip()
  1423.  
  1424. def _onMenuItemSelected(self, evt):
  1425. """Called whenever one of the specific axis menu items is selected"""
  1426. current = self._menu.IsChecked(evt.GetId())
  1427. if current:
  1428. new = False
  1429. else:
  1430. new = True
  1431. self._menu.Check(evt.GetId(), new)
  1432. # Lines above would be deleted based on svn tracker ID 2841525;
  1433. # not clear whether this matters or not.
  1434. self._toolbar.set_active(self.getActiveAxes())
  1435. evt.Skip()
  1436.  
  1437. def updateAxes(self, maxAxis):
  1438. """Ensures that there are entries for max_axis axes in the menu
  1439. (selected by default)."""
  1440. if maxAxis > len(self._axisId):
  1441. for i in range(len(self._axisId) + 1, maxAxis + 1, 1):
  1442. menuId = wx.NewId()
  1443. self._axisId.append(menuId)
  1444. self._menu.Append(menuId, "Axis %d" % i,
  1445. "Select axis %d" % i,
  1446. True)
  1447. self._menu.Check(menuId, True)
  1448. self.Bind(wx.EVT_MENU, self._onMenuItemSelected, id=menuId)
  1449. elif maxAxis < len(self._axisId):
  1450. for menuId in self._axisId[maxAxis:]:
  1451. self._menu.Delete(menuId)
  1452. self._axisId = self._axisId[:maxAxis]
  1453. self._toolbar.set_active(list(xrange(maxAxis)))
  1454.  
  1455. def getActiveAxes(self):
  1456. """Return a list of the selected axes."""
  1457. active = []
  1458. for i in range(len(self._axisId)):
  1459. if self._menu.IsChecked(self._axisId[i]):
  1460. active.append(i)
  1461. return active
  1462.  
  1463. def updateButtonText(self, lst):
  1464. """Update the list of selected axes in the menu button"""
  1465. axis_txt = ''
  1466. for e in lst:
  1467. axis_txt += '%d,' % (e + 1)
  1468. # remove trailing ',' and add to button string
  1469. self.SetLabel("Axes: %s" % axis_txt[:-1])
  1470.  
  1471.  
  1472. cursord = {
  1473. cursors.MOVE: wx.CURSOR_HAND,
  1474. cursors.HAND: wx.CURSOR_HAND,
  1475. cursors.POINTER: wx.CURSOR_ARROW,
  1476. cursors.SELECT_REGION: wx.CURSOR_CROSS,
  1477. cursors.WAIT: wx.CURSOR_WAIT,
  1478. }
  1479.  
  1480.  
  1481. class SubplotToolWX(wx.Frame):
  1482. def __init__(self, targetfig):
  1483. wx.Frame.__init__(self, None, -1, "Configure subplots")
  1484.  
  1485. toolfig = Figure((6, 3))
  1486. canvas = FigureCanvasWx(self, -1, toolfig)
  1487.  
  1488. # Create a figure manager to manage things
  1489. figmgr = FigureManager(canvas, 1, self)
  1490.  
  1491. # Now put all into a sizer
  1492. sizer = wx.BoxSizer(wx.VERTICAL)
  1493. # This way of adding to sizer allows resizing
  1494. sizer.Add(canvas, 1, wx.LEFT | wx.TOP | wx.GROW)
  1495. self.SetSizer(sizer)
  1496. self.Fit()
  1497. tool = SubplotTool(targetfig, toolfig)
  1498.  
  1499.  
  1500. class NavigationToolbar2Wx(NavigationToolbar2, wx.ToolBar):
  1501. def __init__(self, canvas):
  1502. wx.ToolBar.__init__(self, canvas.GetParent(), -1)
  1503. NavigationToolbar2.__init__(self, canvas)
  1504. self.canvas = canvas
  1505. self._idle = True
  1506. self.statbar = None
  1507. self.prevZoomRect = None
  1508. # for now, use alternate zoom-rectangle drawing on all
  1509. # Macs. N.B. In future versions of wx it may be possible to
  1510. # detect Retina displays with window.GetContentScaleFactor()
  1511. # and/or dc.GetContentScaleFactor()
  1512. self.retinaFix = 'wxMac' in wx.PlatformInfo
  1513.  
  1514. def get_canvas(self, frame, fig):
  1515. return FigureCanvasWx(frame, -1, fig)
  1516.  
  1517. def _init_toolbar(self):
  1518. DEBUG_MSG("_init_toolbar", 1, self)
  1519.  
  1520. self._parent = self.canvas.GetParent()
  1521.  
  1522. self.wx_ids = {}
  1523. for text, tooltip_text, image_file, callback in self.toolitems:
  1524. if text is None:
  1525. self.AddSeparator()
  1526. continue
  1527. self.wx_ids[text] = wx.NewId()
  1528. wxc._AddTool(self, self.wx_ids, text,
  1529. _load_bitmap(image_file + '.png'),
  1530. tooltip_text)
  1531.  
  1532. self.Bind(wx.EVT_TOOL, getattr(self, callback),
  1533. id=self.wx_ids[text])
  1534.  
  1535. self.Realize()
  1536.  
  1537. def zoom(self, *args):
  1538. self.ToggleTool(self.wx_ids['Pan'], False)
  1539. NavigationToolbar2.zoom(self, *args)
  1540.  
  1541. def pan(self, *args):
  1542. self.ToggleTool(self.wx_ids['Zoom'], False)
  1543. NavigationToolbar2.pan(self, *args)
  1544.  
  1545. def configure_subplots(self, evt):
  1546. frame = wx.Frame(None, -1, "Configure subplots")
  1547.  
  1548. toolfig = Figure((6, 3))
  1549. canvas = self.get_canvas(frame, toolfig)
  1550.  
  1551. # Create a figure manager to manage things
  1552. figmgr = FigureManager(canvas, 1, frame)
  1553.  
  1554. # Now put all into a sizer
  1555. sizer = wx.BoxSizer(wx.VERTICAL)
  1556. # This way of adding to sizer allows resizing
  1557. sizer.Add(canvas, 1, wx.LEFT | wx.TOP | wx.GROW)
  1558. frame.SetSizer(sizer)
  1559. frame.Fit()
  1560. tool = SubplotTool(self.canvas.figure, toolfig)
  1561. frame.Show()
  1562.  
  1563. def save_figure(self, *args):
  1564. # Fetch the required filename and file type.
  1565. filetypes, exts, filter_index = self.canvas._get_imagesave_wildcards()
  1566. default_file = self.canvas.get_default_filename()
  1567. dlg = wx.FileDialog(self._parent, "Save to file", "", default_file,
  1568. filetypes,
  1569. wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT)
  1570. dlg.SetFilterIndex(filter_index)
  1571. if dlg.ShowModal() == wx.ID_OK:
  1572. dirname = dlg.GetDirectory()
  1573. filename = dlg.GetFilename()
  1574. DEBUG_MSG(
  1575. 'Save file dir:%s name:%s' %
  1576. (dirname, filename), 3, self)
  1577. format = exts[dlg.GetFilterIndex()]
  1578. basename, ext = os.path.splitext(filename)
  1579. if ext.startswith('.'):
  1580. ext = ext[1:]
  1581. if ext in ('svg', 'pdf', 'ps', 'eps', 'png') and format != ext:
  1582. # looks like they forgot to set the image type drop
  1583. # down, going with the extension.
  1584. warnings.warn(
  1585. 'extension %s did not match the selected '
  1586. 'image type %s; going with %s' %
  1587. (ext, format, ext), stacklevel=0)
  1588. format = ext
  1589. try:
  1590. self.canvas.figure.savefig(
  1591. os.path.join(dirname, filename), format=format)
  1592. except Exception as e:
  1593. error_msg_wx(str(e))
  1594.  
  1595. def set_cursor(self, cursor):
  1596. cursor = wxc.Cursor(cursord[cursor])
  1597. self.canvas.SetCursor(cursor)
  1598. self.canvas.Update()
  1599.  
  1600. def release(self, event):
  1601. try:
  1602. del self.lastrect
  1603. except AttributeError:
  1604. pass
  1605.  
  1606. @cbook.deprecated("2.1", alternative="canvas.draw_idle")
  1607. def dynamic_update(self):
  1608. d = self._idle
  1609. self._idle = False
  1610. if d:
  1611. self.canvas.draw()
  1612. self._idle = True
  1613.  
  1614. def press(self, event):
  1615. if self._active == 'ZOOM':
  1616. if not self.retinaFix:
  1617. self.wxoverlay = wx.Overlay()
  1618. else:
  1619. if event.inaxes is not None:
  1620. self.savedRetinaImage = self.canvas.copy_from_bbox(
  1621. event.inaxes.bbox)
  1622. self.zoomStartX = event.xdata
  1623. self.zoomStartY = event.ydata
  1624. self.zoomAxes = event.inaxes
  1625.  
  1626. def release(self, event):
  1627. if self._active == 'ZOOM':
  1628. # When the mouse is released we reset the overlay and it
  1629. # restores the former content to the window.
  1630. if not self.retinaFix:
  1631. self.wxoverlay.Reset()
  1632. del self.wxoverlay
  1633. else:
  1634. del self.savedRetinaImage
  1635. if self.prevZoomRect:
  1636. self.prevZoomRect.pop(0).remove()
  1637. self.prevZoomRect = None
  1638. if self.zoomAxes:
  1639. self.zoomAxes = None
  1640.  
  1641. def draw_rubberband(self, event, x0, y0, x1, y1):
  1642. if self.retinaFix: # On Macs, use the following code
  1643. # wx.DCOverlay does not work properly on Retina displays.
  1644. rubberBandColor = '#C0C0FF'
  1645. if self.prevZoomRect:
  1646. self.prevZoomRect.pop(0).remove()
  1647. self.canvas.restore_region(self.savedRetinaImage)
  1648. X0, X1 = self.zoomStartX, event.xdata
  1649. Y0, Y1 = self.zoomStartY, event.ydata
  1650. lineX = (X0, X0, X1, X1, X0)
  1651. lineY = (Y0, Y1, Y1, Y0, Y0)
  1652. self.prevZoomRect = self.zoomAxes.plot(
  1653. lineX, lineY, '-', color=rubberBandColor)
  1654. self.zoomAxes.draw_artist(self.prevZoomRect[0])
  1655. self.canvas.blit(self.zoomAxes.bbox)
  1656. return
  1657.  
  1658. # Use an Overlay to draw a rubberband-like bounding box.
  1659.  
  1660. dc = wx.ClientDC(self.canvas)
  1661. odc = wx.DCOverlay(self.wxoverlay, dc)
  1662. odc.Clear()
  1663.  
  1664. # Mac's DC is already the same as a GCDC, and it causes
  1665. # problems with the overlay if we try to use an actual
  1666. # wx.GCDC so don't try it.
  1667. if 'wxMac' not in wx.PlatformInfo:
  1668. dc = wx.GCDC(dc)
  1669.  
  1670. height = self.canvas.figure.bbox.height
  1671. y1 = height - y1
  1672. y0 = height - y0
  1673.  
  1674. if y1 < y0:
  1675. y0, y1 = y1, y0
  1676. if x1 < y0:
  1677. x0, x1 = x1, x0
  1678.  
  1679. w = x1 - x0
  1680. h = y1 - y0
  1681. rect = wx.Rect(x0, y0, w, h)
  1682.  
  1683. rubberBandColor = '#C0C0FF' # or load from config?
  1684.  
  1685. # Set a pen for the border
  1686. color = wxc.NamedColour(rubberBandColor)
  1687. dc.SetPen(wx.Pen(color, 1))
  1688.  
  1689. # use the same color, plus alpha for the brush
  1690. r, g, b, a = color.Get(True)
  1691. color.Set(r, g, b, 0x60)
  1692. dc.SetBrush(wx.Brush(color))
  1693. if wxc.is_phoenix:
  1694. dc.DrawRectangle(rect)
  1695. else:
  1696. dc.DrawRectangleRect(rect)
  1697.  
  1698. def set_status_bar(self, statbar):
  1699. self.statbar = statbar
  1700.  
  1701. def set_message(self, s):
  1702. if self.statbar is not None:
  1703. self.statbar.set_function(s)
  1704.  
  1705. def set_history_buttons(self):
  1706. can_backward = (self._views._pos > 0)
  1707. can_forward = (self._views._pos < len(self._views._elements) - 1)
  1708. self.EnableTool(self.wx_ids['Back'], can_backward)
  1709. self.EnableTool(self.wx_ids['Forward'], can_forward)
  1710.  
  1711.  
  1712. class StatusBarWx(wx.StatusBar):
  1713. """
  1714. A status bar is added to _FigureFrame to allow measurements and the
  1715. previously selected scroll function to be displayed as a user
  1716. convenience.
  1717. """
  1718.  
  1719. def __init__(self, parent):
  1720. wx.StatusBar.__init__(self, parent, -1)
  1721. self.SetFieldsCount(2)
  1722. self.SetStatusText("None", 1)
  1723. #self.SetStatusText("Measurement: None", 2)
  1724. # self.Reposition()
  1725.  
  1726. def set_function(self, string):
  1727. self.SetStatusText("%s" % string, 1)
  1728.  
  1729. # def set_measurement(self, string):
  1730. # self.SetStatusText("Measurement: %s" % string, 2)
  1731.  
  1732.  
  1733. #< Additions for printing support: Matt Newville
  1734.  
  1735. class PrintoutWx(wx.Printout):
  1736. """
  1737. Simple wrapper around wx Printout class -- all the real work
  1738. here is scaling the matplotlib canvas bitmap to the current
  1739. printer's definition.
  1740. """
  1741.  
  1742. def __init__(self, canvas, width=5.5, margin=0.5, title='matplotlib'):
  1743. wx.Printout.__init__(self, title=title)
  1744. self.canvas = canvas
  1745. # width, in inches of output figure (approximate)
  1746. self.width = width
  1747. self.margin = margin
  1748.  
  1749. def HasPage(self, page):
  1750. # current only supports 1 page print
  1751. return page == 1
  1752.  
  1753. def GetPageInfo(self):
  1754. return (1, 1, 1, 1)
  1755.  
  1756. def OnPrintPage(self, page):
  1757. self.canvas.draw()
  1758.  
  1759. dc = self.GetDC()
  1760. (ppw, pph) = self.GetPPIPrinter() # printer's pixels per in
  1761. (pgw, pgh) = self.GetPageSizePixels() # page size in pixels
  1762. (dcw, dch) = dc.GetSize()
  1763. if wxc.is_phoenix:
  1764. (grw, grh) = self.canvas.GetSize()
  1765. else:
  1766. (grw, grh) = self.canvas.GetSizeTuple()
  1767.  
  1768. # save current figure dpi resolution and bg color,
  1769. # so that we can temporarily set them to the dpi of
  1770. # the printer, and the bg color to white
  1771. bgcolor = self.canvas.figure.get_facecolor()
  1772. fig_dpi = self.canvas.figure.dpi
  1773.  
  1774. # draw the bitmap, scaled appropriately
  1775. vscale = float(ppw) / fig_dpi
  1776.  
  1777. # set figure resolution,bg color for printer
  1778. self.canvas.figure.dpi = ppw
  1779. self.canvas.figure.set_facecolor('#FFFFFF')
  1780.  
  1781. renderer = RendererWx(self.canvas.bitmap, self.canvas.figure.dpi)
  1782. self.canvas.figure.draw(renderer)
  1783. self.canvas.bitmap.SetWidth(
  1784. int(self.canvas.bitmap.GetWidth() * vscale))
  1785. self.canvas.bitmap.SetHeight(
  1786. int(self.canvas.bitmap.GetHeight() * vscale))
  1787. self.canvas.draw()
  1788.  
  1789. # page may need additional scaling on preview
  1790. page_scale = 1.0
  1791. if self.IsPreview():
  1792. page_scale = float(dcw) / pgw
  1793.  
  1794. # get margin in pixels = (margin in in) * (pixels/in)
  1795. top_margin = int(self.margin * pph * page_scale)
  1796. left_margin = int(self.margin * ppw * page_scale)
  1797. class PrintoutWx():
  1798. # set scale so that width of output is self.width inches
  1799. # (assuming grw is size of graph in inches....)
  1800. user_scale = (self.width * fig_dpi * page_scale) / float(grw)
  1801.  
  1802. dc.SetDeviceOrigin(left_margin, top_margin)
  1803. dc.SetUserScale(user_scale, user_scale)
  1804.  
  1805. self.title = title
  1806. try:
  1807. dc.DrawBitmap(self.canvas.bitmap, 0, 0)
  1808. except:
  1809. try:
  1810. dc.DrawBitmap(self.canvas.bitmap, (0, 0))
  1811. except:
  1812. pass
  1813.  
  1814. # restore original figure resolution
  1815. self.canvas.figure.set_facecolor(bgcolor)
  1816. self.canvas.figure.dpi = fig_dpi
  1817. self.canvas.draw()
  1818. return True
  1819. #>
  1820.  
  1821. ########################################################################
  1822. #
  1823. # Now just provide the standard names that backend.__init__ is expecting
  1824. #
  1825. ########################################################################
  1826.  
  1827. Toolbar = NavigationToolbar2Wx
  1828.  
  1829.  
  1830. @_Backend.export
  1831. class _BackendWx(_Backend):
  1832. FigureCanvas = FigureCanvasWx
  1833. FigureManager = FigureManagerWx
  1834. _frame_class = FigureFrameWx
  1835.  
  1836. @staticmethod
  1837. def trigger_manager_draw(manager):
  1838. manager.canvas.draw_idle()
  1839.  
  1840. @classmethod
  1841. def new_figure_manager(cls, num, *args, **kwargs):
  1842. # Create a wx.App instance if it has not been created sofar.
  1843. wxapp = wx.GetApp()
  1844. if wxapp is None:
  1845. wxapp = wx.App(False)
  1846. wxapp.SetExitOnFrameDelete(True)
  1847. # Retain a reference to the app object so that it does not get
  1848. # garbage collected.
  1849. _BackendWx._theWxApp = wxapp
  1850. return super(_BackendWx, cls).new_figure_manager(num, *args, **kwargs)
  1851.  
  1852. @classmethod
  1853. def new_figure_manager_given_figure(cls, num, figure):
  1854. frame = cls._frame_class(num, figure)
  1855. figmgr = frame.get_figure_manager()
  1856. if matplotlib.is_interactive():
  1857. figmgr.frame.Show()
  1858. figure.canvas.draw_idle()
  1859. return figmgr
  1860.  
  1861. @staticmethod
  1862. def mainloop():
  1863. if not wx.App.IsMainLoopRunning():
  1864. wxapp = wx.GetApp()
  1865. if wxapp is not None:
  1866. wxapp.MainLoop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement