Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3f3f07a

Browse files
committed
Fix coordinates text layouting in wx + {windows, macos}.
1 parent c1c60fc commit 3f3f07a

File tree

1 file changed

+50
-8
lines changed

1 file changed

+50
-8
lines changed

lib/matplotlib/backends/backend_wx.py

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,16 +1073,44 @@ def __init__(self, canvas, coordinates=True, *, style=wx.TB_BOTTOM):
10731073
self.Bind(wx.EVT_TOOL, getattr(self, callback),
10741074
id=self.wx_ids[text])
10751075

1076-
self._coordinates = coordinates
1077-
if self._coordinates:
1078-
self.AddStretchableSpace()
1079-
self._label_text = wx.StaticText(self, style=wx.ALIGN_RIGHT)
1076+
if coordinates:
1077+
# Each OS appears to require its own (non-interchangeable) strategy
1078+
# to position the coordinates text box to the right of the toolbar.
1079+
# On Linux (wxGTK), the StaticText is pushed to the right with
1080+
# a StretchableSpace. On Windows and macOS, instead, the size
1081+
# of the StaticText is adjusted to fill all the remaining space
1082+
# beyond the toolbar buttons, via a EVT_SIZE callback. On Windows,
1083+
# wx.ToolBar does not receive all EVT_SIZE, so we connect to the
1084+
# canvas' EVT_SIZE. On macOS, the canvas' EVT_SIZE is not called
1085+
# at init, so we connect to the toolbar's EVT_SIZE.
1086+
if wx.Platform == "__WXGTK__":
1087+
self.AddStretchableSpace()
1088+
self._label_text = wx.StaticText(
1089+
self, label="\n", style=wx.ALIGN_RIGHT)
1090+
else:
1091+
self._label_text = wx.StaticText(
1092+
self, label="\n", style=wx.ALIGN_RIGHT | wx.ST_NO_AUTORESIZE)
10801093
self.AddControl(self._label_text)
1094+
if wx.Platform == "__WXMSW__":
1095+
canvas.Bind(wx.EVT_SIZE, self._on_size)
1096+
elif wx.Platform == "__WXMAC__":
1097+
self.Bind(wx.EVT_SIZE, self._on_size)
1098+
else:
1099+
self._label_text = None
10811100

10821101
self.Realize()
10831102

10841103
NavigationToolbar2.__init__(self, canvas)
10851104

1105+
def _on_size(self, event):
1106+
text = self._label_text
1107+
# During initialization, on Windows, the toolbar size (self.Size) is
1108+
# not properly updated, but event.Size is correct, hence its use here.
1109+
if text is not None:
1110+
self._label_text.SetSize(
1111+
event.Size.Width - text.Position.x, text.Size.Height)
1112+
event.Skip()
1113+
10861114
@staticmethod
10871115
def _icon(name):
10881116
"""
@@ -1179,7 +1207,7 @@ def remove_rubberband(self):
11791207
self.canvas.Refresh()
11801208

11811209
def set_message(self, s):
1182-
if self._coordinates:
1210+
if self._label_text:
11831211
self._label_text.SetLabel(s)
11841212

11851213
def set_history_buttons(self):
@@ -1201,9 +1229,22 @@ def __init__(self, toolmanager, parent=None, style=wx.TB_BOTTOM):
12011229
parent = toolmanager.canvas.GetParent()
12021230
ToolContainerBase.__init__(self, toolmanager)
12031231
wx.ToolBar.__init__(self, parent, -1, style=style)
1204-
self._space = self.AddStretchableSpace()
1205-
self._label_text = wx.StaticText(self, style=wx.ALIGN_RIGHT)
1232+
if wx.Platform == "__WXGTK__":
1233+
self.AddStretchableSpace()
1234+
self._label_text = wx.StaticText(
1235+
self, label="\n", style=wx.ALIGN_RIGHT)
1236+
else:
1237+
self._label_text = wx.StaticText(
1238+
self, label="\n", style=wx.ALIGN_RIGHT | wx.ST_NO_AUTORESIZE)
12061239
self.AddControl(self._label_text)
1240+
if wx.Platform == "__WXMSW__":
1241+
toolmanager.canvas.Bind(
1242+
wx.EVT_SIZE,
1243+
functools.partial(NavigationToolbar2Wx._on_size, self))
1244+
elif wx.Platform == "__WXMAC__":
1245+
self.Bind(
1246+
wx.EVT_SIZE,
1247+
functools.partial(NavigationToolbar2Wx._on_size, self))
12071248
self._toolitems = {}
12081249
self._groups = {} # Mapping of groups to the separator after them.
12091250

@@ -1222,8 +1263,9 @@ def add_toolitem(self, name, group, position, image_file, description,
12221263
toggle):
12231264
# Find or create the separator that follows this group.
12241265
if group not in self._groups:
1266+
# On wxGTK we add a StretchableSpace which must also be skipped.
12251267
self._groups[group] = self.InsertSeparator(
1226-
self._get_tool_pos(self._space))
1268+
self.ToolsCount - (2 if wx.Platform == "__WXGTK__" else 1))
12271269
sep = self._groups[group]
12281270
# List all separators.
12291271
seps = [t for t in map(self.GetToolByPos, range(self.ToolsCount))

0 commit comments

Comments
 (0)