|
1 | 1 | """
|
2 |
| - A wxPython backend for matplotlib, based (very heavily) on |
3 |
| - backend_template.py and backend_gtk.py |
| 2 | +A wxPython backend for matplotlib. |
4 | 3 |
|
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. |
| 4 | +Originally contributed by Jeremy O'Donoghue (jeremy@o-donoghue.com) and John |
| 5 | +Hunter (jdhunter@ace.bsd.uchicago.edu). |
14 | 6 |
|
| 7 | +Copyright (C) Jeremy O'Donoghue & John Hunter, 2003-4. |
15 | 8 | """
|
16 | 9 |
|
17 | 10 | import six
|
18 | 11 |
|
19 |
| -import sys |
20 |
| -import os |
21 | 12 | import os.path
|
22 | 13 | import math
|
23 |
| -import weakref |
| 14 | +import sys |
24 | 15 | import warnings
|
| 16 | +import weakref |
25 | 17 |
|
26 | 18 | import matplotlib
|
27 | 19 | from matplotlib.backend_bases import (
|
|
37 | 29 | from matplotlib.widgets import SubplotTool
|
38 | 30 | from matplotlib import cbook, rcParams, backend_tools
|
39 | 31 |
|
40 |
| -from . import wx_compat as wxc |
41 | 32 | import wx
|
42 | 33 |
|
43 | 34 | # Debugging settings here...
|
@@ -171,14 +162,45 @@ class RendererWx(RendererBase):
|
171 | 162 | # describes the colour and weight of any lines drawn, and a wxBrush
|
172 | 163 | # which describes the fill colour of any closed polygon.
|
173 | 164 |
|
174 |
| - fontweights = wxc.fontweights |
175 |
| - fontangles = wxc.fontangles |
| 165 | + # Font styles, families and weight. |
| 166 | + fontweights = { |
| 167 | + 100: wx.FONTWEIGHT_LIGHT, |
| 168 | + 200: wx.FONTWEIGHT_LIGHT, |
| 169 | + 300: wx.FONTWEIGHT_LIGHT, |
| 170 | + 400: wx.FONTWEIGHT_NORMAL, |
| 171 | + 500: wx.FONTWEIGHT_NORMAL, |
| 172 | + 600: wx.FONTWEIGHT_NORMAL, |
| 173 | + 700: wx.FONTWEIGHT_BOLD, |
| 174 | + 800: wx.FONTWEIGHT_BOLD, |
| 175 | + 900: wx.FONTWEIGHT_BOLD, |
| 176 | + 'ultralight': wx.FONTWEIGHT_LIGHT, |
| 177 | + 'light': wx.FONTWEIGHT_LIGHT, |
| 178 | + 'normal': wx.FONTWEIGHT_NORMAL, |
| 179 | + 'medium': wx.FONTWEIGHT_NORMAL, |
| 180 | + 'semibold': wx.FONTWEIGHT_NORMAL, |
| 181 | + 'bold': wx.FONTWEIGHT_BOLD, |
| 182 | + 'heavy': wx.FONTWEIGHT_BOLD, |
| 183 | + 'ultrabold': wx.FONTWEIGHT_BOLD, |
| 184 | + 'black': wx.FONTWEIGHT_BOLD, |
| 185 | + } |
| 186 | + fontangles = { |
| 187 | + 'italic': wx.FONTSTYLE_ITALIC, |
| 188 | + 'normal': wx.FONTSTYLE_NORMAL, |
| 189 | + 'oblique': wx.FONTSTYLE_SLANT, |
| 190 | + } |
176 | 191 |
|
177 |
| - # wxPython allows for portable font styles, choosing them appropriately |
178 |
| - # for the target platform. Map some standard font names to the portable |
179 |
| - # styles |
| 192 | + # wxPython allows for portable font styles, choosing them appropriately for |
| 193 | + # the target platform. Map some standard font names to the portable styles. |
180 | 194 | # QUESTION: Is it be wise to agree standard fontnames across all backends?
|
181 |
| - fontnames = wxc.fontnames |
| 195 | + fontnames = { |
| 196 | + 'Sans': wx.FONTFAMILY_SWISS, |
| 197 | + 'Roman': wx.FONTFAMILY_ROMAN, |
| 198 | + 'Script': wx.FONTFAMILY_SCRIPT, |
| 199 | + 'Decorative': wx.FONTFAMILY_DECORATIVE, |
| 200 | + 'Modern': wx.FONTFAMILY_MODERN, |
| 201 | + 'Courier': wx.FONTFAMILY_MODERN, |
| 202 | + 'courier': wx.FONTFAMILY_MODERN, |
| 203 | + } |
182 | 204 |
|
183 | 205 | def __init__(self, bitmap, dpi):
|
184 | 206 | """
|
@@ -283,7 +305,7 @@ def draw_image(self, gc, x, y, im):
|
283 | 305 | w = self.width
|
284 | 306 | h = self.height
|
285 | 307 | rows, cols = im.shape[:2]
|
286 |
| - bitmap = wxc.BitmapFromBuffer(cols, rows, im.tostring()) |
| 308 | + bitmap = wx.Bitmap.FromBufferRGBA(cols, rows, im.tostring()) |
287 | 309 | gc = self.get_gc()
|
288 | 310 | gc.select()
|
289 | 311 | gc.gfx_ctx.DrawBitmap(bitmap, int(l), int(self.height - b),
|
@@ -610,7 +632,7 @@ def __init__(self, parent, id, figure):
|
610 | 632 | wx.Panel.__init__(self, parent, id, size=wx.Size(w, h))
|
611 | 633 |
|
612 | 634 | # Create the drawing bitmap
|
613 |
| - self.bitmap = wxc.EmptyBitmap(w, h) |
| 635 | + self.bitmap = wx.Bitmap(w, h) |
614 | 636 | DEBUG_MSG("__init__() - bitmap w:%d h:%d" % (w, h), 2, self)
|
615 | 637 | # TODO: Add support for 'point' inspection and plot navigation.
|
616 | 638 | self._isDrawn = False
|
@@ -715,7 +737,7 @@ def start_event_loop(self, timeout=0):
|
715 | 737 | self.Bind(wx.EVT_TIMER, self.stop_event_loop, id=id)
|
716 | 738 |
|
717 | 739 | # Event loop handler for start/stop event loop
|
718 |
| - self._event_loop = wxc.EventLoop() |
| 740 | + self._event_loop = wx.GUIEventLoop() |
719 | 741 | self._event_loop.Run()
|
720 | 742 | timer.Stop()
|
721 | 743 |
|
@@ -829,7 +851,7 @@ def _onSize(self, evt):
|
829 | 851 | return
|
830 | 852 | self._width, self._height = size
|
831 | 853 | # Create a new, correctly sized bitmap
|
832 |
| - self.bitmap = wxc.EmptyBitmap(self._width, self._height) |
| 854 | + self.bitmap = wx.Bitmap(self._width, self._height) |
833 | 855 |
|
834 | 856 | self._isDrawn = False
|
835 | 857 |
|
@@ -1067,7 +1089,7 @@ def _print_image(self, filename, filetype, *args, **kwargs):
|
1067 | 1089 | width = math.ceil(width)
|
1068 | 1090 | height = math.ceil(height)
|
1069 | 1091 |
|
1070 |
| - self.bitmap = wxc.EmptyBitmap(width, height) |
| 1092 | + self.bitmap = wx.Bitmap(width, height) |
1071 | 1093 |
|
1072 | 1094 | renderer = RendererWx(self.bitmap, self.figure.dpi)
|
1073 | 1095 |
|
@@ -1156,12 +1178,8 @@ def __init__(self, num, fig):
|
1156 | 1178 | self.toolbar.Realize()
|
1157 | 1179 | # On Windows platform, default window size is incorrect, so set
|
1158 | 1180 | # toolbar width to figure width.
|
1159 |
| - if wxc.is_phoenix: |
1160 |
| - tw, th = self.toolbar.GetSize() |
1161 |
| - fw, fh = self.canvas.GetSize() |
1162 |
| - else: |
1163 |
| - tw, th = self.toolbar.GetSizeTuple() |
1164 |
| - fw, fh = self.canvas.GetSizeTuple() |
| 1181 | + tw, th = self.toolbar.GetSize() |
| 1182 | + fw, fh = self.canvas.GetSize() |
1165 | 1183 | # By adding toolbar in sizer, we are able to put it at the bottom
|
1166 | 1184 | # of the frame - so appearance is closer to GTK version.
|
1167 | 1185 | self.toolbar.SetSize(wx.Size(fw, th))
|
@@ -1352,12 +1370,8 @@ def Destroy(self):
|
1352 | 1370 |
|
1353 | 1371 | def _onMenuButton(self, evt):
|
1354 | 1372 | """Handle menu button pressed."""
|
1355 |
| - if wxc.is_phoenix: |
1356 |
| - x, y = self.GetPosition() |
1357 |
| - w, h = self.GetSize() |
1358 |
| - else: |
1359 |
| - x, y = self.GetPositionTuple() |
1360 |
| - w, h = self.GetSizeTuple() |
| 1373 | + x, y = self.GetPosition() |
| 1374 | + w, h = self.GetSize() |
1361 | 1375 | self.PopupMenuXY(self._menu, x, y + h - 4)
|
1362 | 1376 | # When menu returned, indicate selection in button
|
1363 | 1377 | evt.Skip()
|
@@ -1484,11 +1498,15 @@ def _init_toolbar(self):
|
1484 | 1498 | if text is None:
|
1485 | 1499 | self.AddSeparator()
|
1486 | 1500 | continue
|
1487 |
| - self.wx_ids[text] = wx.NewId() |
1488 |
| - wxc._AddTool(self, self.wx_ids, text, |
1489 |
| - _load_bitmap(image_file + '.png'), |
1490 |
| - tooltip_text) |
1491 |
| - |
| 1501 | + self.wx_ids[text] = ( |
| 1502 | + self.AddTool( |
| 1503 | + -1, |
| 1504 | + bitmap=_load_bitmap(image_file + ".png"), |
| 1505 | + bmpDisabled=wx.NullBitmap, |
| 1506 | + label=text, shortHelp=text, longHelp=tooltip_text, |
| 1507 | + kind=(wx.ITEM_CHECK if text in ["Pan", "Zoom"] |
| 1508 | + else wx.ITEM_NORMAL)) |
| 1509 | + .Id) |
1492 | 1510 | self.Bind(wx.EVT_TOOL, getattr(self, callback),
|
1493 | 1511 | id=self.wx_ids[text])
|
1494 | 1512 |
|
@@ -1553,7 +1571,7 @@ def save_figure(self, *args):
|
1553 | 1571 | error_msg_wx(str(e))
|
1554 | 1572 |
|
1555 | 1573 | def set_cursor(self, cursor):
|
1556 |
| - cursor = wxc.Cursor(cursord[cursor]) |
| 1574 | + cursor = wx.Cursor(cursord[cursor]) |
1557 | 1575 | self.canvas.SetCursor(cursor)
|
1558 | 1576 | self.canvas.Update()
|
1559 | 1577 |
|
@@ -1629,17 +1647,14 @@ def draw_rubberband(self, event, x0, y0, x1, y1):
|
1629 | 1647 | rubberBandColor = '#C0C0FF' # or load from config?
|
1630 | 1648 |
|
1631 | 1649 | # Set a pen for the border
|
1632 |
| - color = wxc.NamedColour(rubberBandColor) |
| 1650 | + color = wx.Colour(rubberBandColor) |
1633 | 1651 | dc.SetPen(wx.Pen(color, 1))
|
1634 | 1652 |
|
1635 | 1653 | # use the same color, plus alpha for the brush
|
1636 | 1654 | r, g, b, a = color.Get(True)
|
1637 | 1655 | color.Set(r, g, b, 0x60)
|
1638 | 1656 | dc.SetBrush(wx.Brush(color))
|
1639 |
| - if wxc.is_phoenix: |
1640 |
| - dc.DrawRectangle(rect) |
1641 |
| - else: |
1642 |
| - dc.DrawRectangleRect(rect) |
| 1657 | + dc.DrawRectangle(rect) |
1643 | 1658 |
|
1644 | 1659 | def set_status_bar(self, statbar):
|
1645 | 1660 | self.statbar = statbar
|
@@ -1726,7 +1741,7 @@ def trigger(self, *args):
|
1726 | 1741 |
|
1727 | 1742 | class SetCursorWx(backend_tools.SetCursorBase):
|
1728 | 1743 | def set_cursor(self, cursor):
|
1729 |
| - cursor = wxc.Cursor(cursord[cursor]) |
| 1744 | + cursor = wx.Cursor(cursord[cursor]) |
1730 | 1745 | self.canvas.SetCursor(cursor)
|
1731 | 1746 | self.canvas.Update()
|
1732 | 1747 |
|
@@ -1764,17 +1779,14 @@ def draw_rubberband(self, x0, y0, x1, y1):
|
1764 | 1779 | rubberBandColor = '#C0C0FF' # or load from config?
|
1765 | 1780 |
|
1766 | 1781 | # Set a pen for the border
|
1767 |
| - color = wxc.NamedColour(rubberBandColor) |
| 1782 | + color = wx.Colour(rubberBandColor) |
1768 | 1783 | dc.SetPen(wx.Pen(color, 1))
|
1769 | 1784 |
|
1770 | 1785 | # use the same color, plus alpha for the brush
|
1771 | 1786 | r, g, b, a = color.Get(True)
|
1772 | 1787 | color.Set(r, g, b, 0x60)
|
1773 | 1788 | dc.SetBrush(wx.Brush(color))
|
1774 |
| - if wxc.is_phoenix: |
1775 |
| - dc.DrawRectangle(rect) |
1776 |
| - else: |
1777 |
| - dc.DrawRectangleRect(rect) |
| 1789 | + dc.DrawRectangle(rect) |
1778 | 1790 |
|
1779 | 1791 | def remove_rubberband(self):
|
1780 | 1792 | if self.wxoverlay is None:
|
@@ -1805,10 +1817,7 @@ def draw_rubberband(self, x0, y0, x1, y1):
|
1805 | 1817 | dc.SetPen(wx.Pen(wx.BLACK, 1, wx.SOLID))
|
1806 | 1818 | dc.SetBrush(wx.TRANSPARENT_BRUSH)
|
1807 | 1819 | self._rect = (x0, self.canvas._height-y0, x1-x0, -y1+y0)
|
1808 |
| - if wxc.is_phoenix: |
1809 |
| - dc.DrawRectangle(self._rect) |
1810 |
| - else: |
1811 |
| - dc.DrawRectangleRect(self._rect) |
| 1820 | + dc.DrawRectangle(self._rect) |
1812 | 1821 |
|
1813 | 1822 | def remove_rubberband(self, dc=None):
|
1814 | 1823 | if not self._rect:
|
@@ -1856,13 +1865,10 @@ def OnPrintPage(self, page):
|
1856 | 1865 | self.canvas.draw()
|
1857 | 1866 |
|
1858 | 1867 | dc = self.GetDC()
|
1859 |
| - (ppw, pph) = self.GetPPIPrinter() # printer's pixels per in |
1860 |
| - (pgw, pgh) = self.GetPageSizePixels() # page size in pixels |
1861 |
| - (dcw, dch) = dc.GetSize() |
1862 |
| - if wxc.is_phoenix: |
1863 |
| - (grw, grh) = self.canvas.GetSize() |
1864 |
| - else: |
1865 |
| - (grw, grh) = self.canvas.GetSizeTuple() |
| 1868 | + ppw, pph = self.GetPPIPrinter() # printer's pixels per in |
| 1869 | + pgw, pgh = self.GetPageSizePixels() # page size in pixels |
| 1870 | + dcw, dch = dc.GetSize() |
| 1871 | + grw, grh = self.canvas.GetSize() |
1866 | 1872 |
|
1867 | 1873 | # save current figure dpi resolution and bg color,
|
1868 | 1874 | # so that we can temporarily set them to the dpi of
|
|
0 commit comments