Skip to content

Commit 96a1799

Browse files
committed
Use standard toolbar in wx.
The previous approach manually positioned the toolbar to have it at the bottom of the window, but this can in fact be achieved just with style=wx.TB_BOTTOM. Removing manual sizing of the toolbar also fixes a bug previously present on Windows, whereby a `set_size_inches` reducing the size of the canvas would *not* reduce the window width, likely because it was forced to its max value by the toolbar's explicit size.
1 parent ec99c15 commit 96a1799

File tree

2 files changed

+19
-31
lines changed

2 files changed

+19
-31
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
``FigureFrameWx.sizer``
2+
~~~~~~~~~~~~~~~~~~~~~~~
3+
... has been removed. The frame layout is no longer based on a sizer, as the
4+
canvas is now the sole child widget; the toolbar is now a regular toolbar
5+
added using ``SetToolBar``.

lib/matplotlib/backends/backend_wx.py

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -906,37 +906,24 @@ def __init__(self, num, fig):
906906
_set_frame_icon(self)
907907

908908
self.canvas = self.get_canvas(fig)
909-
w, h = map(math.ceil, fig.bbox.size)
910-
self.canvas.SetInitialSize(wx.Size(w, h))
911-
self.canvas.SetFocus()
912-
self.sizer = wx.BoxSizer(wx.VERTICAL)
913-
self.sizer.Add(self.canvas, 1, wx.TOP | wx.LEFT | wx.EXPAND)
914-
# By adding toolbar in sizer, we are able to put it at the bottom
915-
# of the frame - so appearance is closer to GTK version
916-
917909
self.figmgr = FigureManagerWx(self.canvas, num, self)
918910

919911
self.toolbar = self._get_toolbar()
920-
921912
if self.figmgr.toolmanager:
922913
backend_tools.add_tools_to_manager(self.figmgr.toolmanager)
923914
if self.toolbar:
924915
backend_tools.add_tools_to_container(self.toolbar)
925-
926916
if self.toolbar is not None:
927-
self.toolbar.Realize()
928-
# On Windows platform, default window size is incorrect, so set
929-
# toolbar width to figure width.
930-
tw, th = self.toolbar.GetSize()
931-
fw, fh = self.canvas.GetSize()
932-
# By adding toolbar in sizer, we are able to put it at the bottom
933-
# of the frame - so appearance is closer to GTK version.
934-
self.toolbar.SetSize(wx.Size(fw, th))
935-
self.sizer.Add(self.toolbar, 0, wx.LEFT | wx.EXPAND)
936-
self.SetSizer(self.sizer)
937-
self.Fit()
917+
self.SetToolBar(self.toolbar)
938918

919+
# On Windows, canvas sizing must occur after toolbar addition;
920+
# otherwise the toolbar further resizes the canvas.
921+
w, h = map(math.ceil, fig.bbox.size)
922+
self.canvas.SetInitialSize(wx.Size(w, h))
939923
self.canvas.SetMinSize((2, 2))
924+
self.canvas.SetFocus()
925+
926+
self.Fit()
940927

941928
self.Bind(wx.EVT_CLOSE, self._on_close)
942929

@@ -972,10 +959,6 @@ def _on_close(self, event):
972959
# Carry on with close event propagation, frame & children destruction
973960
event.Skip()
974961

975-
def GetToolBar(self):
976-
"""Override wxFrame::GetToolBar as we don't have managed toolbar"""
977-
return self.toolbar
978-
979962
def Destroy(self, *args, **kwargs):
980963
try:
981964
self.canvas.mpl_disconnect(self.toolbar._id_drag)
@@ -1055,9 +1038,9 @@ def set_window_title(self, title):
10551038

10561039
def resize(self, width, height):
10571040
# docstring inherited
1058-
self.canvas.SetInitialSize(
1059-
wx.Size(math.ceil(width), math.ceil(height)))
1060-
self.window.GetSizer().Fit(self.window)
1041+
# Directly using SetClientSize doesn't handle the toolbar on Windows.
1042+
self.window.SetSize(self.window.ClientToWindowSize(wx.Size(
1043+
math.ceil(width), math.ceil(height))))
10611044

10621045

10631046
def _load_bitmap(filename):
@@ -1079,8 +1062,8 @@ def _set_frame_icon(frame):
10791062

10801063

10811064
class NavigationToolbar2Wx(NavigationToolbar2, wx.ToolBar):
1082-
def __init__(self, canvas, coordinates=True):
1083-
wx.ToolBar.__init__(self, canvas.GetParent(), -1)
1065+
def __init__(self, canvas, coordinates=True, *, style=wx.TB_BOTTOM):
1066+
wx.ToolBar.__init__(self, canvas.GetParent(), -1, style=style)
10841067

10851068
if 'wxMac' in wx.PlatformInfo:
10861069
self.SetToolBitmapSize((24, 24))
@@ -1208,7 +1191,7 @@ def set_history_buttons(self):
12081191
# tools for matplotlib.backend_managers.ToolManager:
12091192

12101193
class ToolbarWx(ToolContainerBase, wx.ToolBar):
1211-
def __init__(self, toolmanager, parent, style=wx.TB_HORIZONTAL):
1194+
def __init__(self, toolmanager, parent, style=wx.TB_BOTTOM):
12121195
ToolContainerBase.__init__(self, toolmanager)
12131196
wx.ToolBar.__init__(self, parent, -1, style=style)
12141197
self._space = self.AddStretchableSpace()

0 commit comments

Comments
 (0)