Skip to content

Commit 225317b

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 a35921c commit 225317b

File tree

2 files changed

+19
-30
lines changed

2 files changed

+19
-30
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 & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -898,37 +898,25 @@ def __init__(self, num, fig, *, canvas_class=None):
898898
"required after the deprecation period starting in Matplotlib "
899899
"%(since)s elapses.")
900900
self.canvas = self.get_canvas(fig)
901-
w, h = map(math.ceil, fig.bbox.size)
902-
self.canvas.SetInitialSize(wx.Size(w, h))
903-
self.canvas.SetFocus()
904-
self.sizer = wx.BoxSizer(wx.VERTICAL)
905-
self.sizer.Add(self.canvas, 1, wx.TOP | wx.LEFT | wx.EXPAND)
906-
# By adding toolbar in sizer, we are able to put it at the bottom
907-
# of the frame - so appearance is closer to GTK version
908901

909902
self.figmgr = FigureManagerWx(self.canvas, num, self)
910903

911904
self.toolbar = self._get_toolbar()
912-
913905
if self.figmgr.toolmanager:
914906
backend_tools.add_tools_to_manager(self.figmgr.toolmanager)
915907
if self.toolbar:
916908
backend_tools.add_tools_to_container(self.toolbar)
917-
918909
if self.toolbar is not None:
919-
self.toolbar.Realize()
920-
# On Windows platform, default window size is incorrect, so set
921-
# toolbar width to figure width.
922-
tw, th = self.toolbar.GetSize()
923-
fw, fh = self.canvas.GetSize()
924-
# By adding toolbar in sizer, we are able to put it at the bottom
925-
# of the frame - so appearance is closer to GTK version.
926-
self.toolbar.SetSize(wx.Size(fw, th))
927-
self.sizer.Add(self.toolbar, 0, wx.LEFT | wx.EXPAND)
928-
self.SetSizer(self.sizer)
929-
self.Fit()
910+
self.SetToolBar(self.toolbar)
930911

912+
# On Windows, canvas sizing must occur after toolbar addition;
913+
# otherwise the toolbar further resizes the canvas.
914+
w, h = map(math.ceil, fig.bbox.size)
915+
self.canvas.SetInitialSize(wx.Size(w, h))
931916
self.canvas.SetMinSize((2, 2))
917+
self.canvas.SetFocus()
918+
919+
self.Fit()
932920

933921
self.Bind(wx.EVT_CLOSE, self._on_close)
934922

@@ -966,10 +954,6 @@ def _on_close(self, event):
966954
# Carry on with close event propagation, frame & children destruction
967955
event.Skip()
968956

969-
def GetToolBar(self):
970-
"""Override wxFrame::GetToolBar as we don't have managed toolbar"""
971-
return self.toolbar
972-
973957
def Destroy(self, *args, **kwargs):
974958
try:
975959
self.canvas.mpl_disconnect(self.toolbar._id_drag)
@@ -1049,9 +1033,9 @@ def set_window_title(self, title):
10491033

10501034
def resize(self, width, height):
10511035
# docstring inherited
1052-
self.canvas.SetInitialSize(
1053-
wx.Size(math.ceil(width), math.ceil(height)))
1054-
self.window.GetSizer().Fit(self.window)
1036+
# Directly using SetClientSize doesn't handle the toolbar on Windows.
1037+
self.window.SetSize(self.window.ClientToWindowSize(wx.Size(
1038+
math.ceil(width), math.ceil(height))))
10551039

10561040

10571041
def _load_bitmap(filename):
@@ -1073,8 +1057,8 @@ def _set_frame_icon(frame):
10731057

10741058

10751059
class NavigationToolbar2Wx(NavigationToolbar2, wx.ToolBar):
1076-
def __init__(self, canvas, coordinates=True):
1077-
wx.ToolBar.__init__(self, canvas.GetParent(), -1)
1060+
def __init__(self, canvas, coordinates=True, *, style=wx.TB_BOTTOM):
1061+
wx.ToolBar.__init__(self, canvas.GetParent(), -1, style=style)
10781062

10791063
if 'wxMac' in wx.PlatformInfo:
10801064
self.SetToolBitmapSize((24, 24))
@@ -1202,7 +1186,7 @@ def set_history_buttons(self):
12021186
# tools for matplotlib.backend_managers.ToolManager:
12031187

12041188
class ToolbarWx(ToolContainerBase, wx.ToolBar):
1205-
def __init__(self, toolmanager, parent, style=wx.TB_HORIZONTAL):
1189+
def __init__(self, toolmanager, parent, style=wx.TB_BOTTOM):
12061190
ToolContainerBase.__init__(self, toolmanager)
12071191
wx.ToolBar.__init__(self, parent, -1, style=style)
12081192
self._space = self.AddStretchableSpace()

0 commit comments

Comments
 (0)