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

Skip to content

Commit 0e884b3

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

File tree

1 file changed

+48
-8
lines changed

1 file changed

+48
-8
lines changed

lib/matplotlib/backends/backend_wx.py

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,16 +1073,42 @@ 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+
text.SetSize(event.Size.Width - text.Position.x, text.Size.Height)
1110+
event.Skip()
1111+
10861112
@staticmethod
10871113
def _icon(name):
10881114
"""
@@ -1179,7 +1205,7 @@ def remove_rubberband(self):
11791205
self.canvas.Refresh()
11801206

11811207
def set_message(self, s):
1182-
if self._coordinates:
1208+
if self._label_text:
11831209
self._label_text.SetLabel(s)
11841210

11851211
def set_history_buttons(self):
@@ -1201,9 +1227,22 @@ def __init__(self, toolmanager, parent=None, style=wx.TB_BOTTOM):
12011227
parent = toolmanager.canvas.GetParent()
12021228
ToolContainerBase.__init__(self, toolmanager)
12031229
wx.ToolBar.__init__(self, parent, -1, style=style)
1204-
self._space = self.AddStretchableSpace()
1205-
self._label_text = wx.StaticText(self, style=wx.ALIGN_RIGHT)
1230+
if wx.Platform == "__WXGTK__":
1231+
self.AddStretchableSpace()
1232+
self._label_text = wx.StaticText(
1233+
self, label="\n", style=wx.ALIGN_RIGHT)
1234+
else:
1235+
self._label_text = wx.StaticText(
1236+
self, label="\n", style=wx.ALIGN_RIGHT | wx.ST_NO_AUTORESIZE)
12061237
self.AddControl(self._label_text)
1238+
if wx.Platform == "__WXMSW__":
1239+
toolmanager.canvas.Bind(
1240+
wx.EVT_SIZE,
1241+
functools.partial(NavigationToolbar2Wx._on_size, self))
1242+
elif wx.Platform == "__WXMAC__":
1243+
self.Bind(
1244+
wx.EVT_SIZE,
1245+
functools.partial(NavigationToolbar2Wx._on_size, self))
12071246
self._toolitems = {}
12081247
self._groups = {} # Mapping of groups to the separator after them.
12091248

@@ -1222,8 +1261,9 @@ def add_toolitem(self, name, group, position, image_file, description,
12221261
toggle):
12231262
# Find or create the separator that follows this group.
12241263
if group not in self._groups:
1264+
# On wxGTK we add a StretchableSpace which must also be skipped.
12251265
self._groups[group] = self.InsertSeparator(
1226-
self._get_tool_pos(self._space))
1266+
self.ToolsCount - (2 if wx.Platform == "__WXGTK__" else 1))
12271267
sep = self._groups[group]
12281268
# List all separators.
12291269
seps = [t for t in map(self.GetToolByPos, range(self.ToolsCount))

0 commit comments

Comments
 (0)