10000 Merge pull request #17363 from anntzer/wxtb · matplotlib/matplotlib@2240f34 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2240f34

Browse files
authored
Merge pull request #17363 from anntzer/wxtb
Fix toolbar separators in wx+toolmanager.
2 parents 5c13fb4 + 5fe972e commit 2240f34

File tree

1 file changed

+30
-13
lines changed

1 file changed

+30
-13
lines changed

lib/matplotlib/backends/backend_wx.py

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1339,12 +1339,39 @@ def __init__(self, toolmanager, parent, style=wx.TB_HORIZONTAL):
13391339
self._label_text = wx.StaticText(self)
13401340
self.AddControl(self._label_text)
13411341
self._toolitems = {}
1342-
self._groups = {}
1342+
self._groups = {} # Mapping of groups to the separator after them.
1343+
1344+
def _get_tool_pos(self, tool):
1345+
"""
1346+
Find the position (index) of a wx.ToolBarToolBase in a ToolBar.
1347+
1348+
``ToolBar.GetToolPos`` is not useful because wx assigns the same Id to
1349+
all Separators and StretchableSpaces.
1350+
"""
1351+
pos, = [pos for pos in range(self.ToolsCount)
1352+
if self.GetToolByPos(pos) == tool]
1353+
return pos
13431354

13441355
def add_toolitem(self, name, group, position, image_file, description,
13451356
toggle):
1346-
before, group = self._add_to_group(group, name, position)
1347-
idx = self.GetToolPos(before.Id)
1357+
# Find or create the separator that follows this group.
1358+
if group not in self._groups:
1359+
self._groups[group] = self.InsertSeparator(
1360+
self._get_tool_pos(self._space))
1361+
sep = self._groups[group]
1362+
# List all separators.
1363+
seps = [t for t in map(self.GetToolByPos, range(self.ToolsCount))
1364+
if t.IsSeparator() and not t.IsStretchableSpace()]
1365+
# Find where to insert the tool.
1366+
if position >= 0:
1367+
# Find the start of the group by looking for the separator
1368+
# preceding this one; then move forward from it.
1369+
start = (0 if sep == seps[0]
1370+
else self._get_tool_pos(seps[seps.index(sep) - 1]) + 1)
1371+
else:
1372+
# Move backwards from this separator.
1373+
start = self._get_tool_pos(sep) + 1
1374+
idx = start + position
13481375
if image_file:
13491376
bmp = _load_bitmap(image_file)
13501377
kind = wx.ITEM_NORMAL if not toggle else wx.ITEM_CHECK
@@ -1368,18 +1395,8 @@ def handler(event):
13681395
control.Bind(wx.EVT_LEFT_DOWN, handler)
13691396

13701397
self._toolitems.setdefault(name, [])
1371-
group.insert(position, tool)
13721398
self._toolitems[name].append((tool, handler))
13731399

1374-
def _add_to_group(self, group, name, position):
1375-
gr = self._groups.get(group, [])
1376-
if not gr:
1377-
sep = self.InsertSeparator(self.GetToolPos(self._space.Id))
1378-
gr.append(sep)
1379-
before = gr[position]
1380-
self._groups[group] = gr
1381-
return before, gr
1382-
13831400
def toggle_toolitem(self, name, toggled):
13841401
if name not in self._toolitems:
13851402
return

0 commit comments

Comments
 (0)
0