@@ -1339,12 +1339,39 @@ def __init__(self, toolmanager, parent, style=wx.TB_HORIZONTAL):
1339
1339
self ._label_text = wx .StaticText (self )
1340
1340
self .AddControl (self ._label_text )
1341
1341
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
1343
1354
1344
1355
def add_toolitem (self , name , group , position , image_file , description ,
1345
1356
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
1348
1375
if image_file :
1349
1376
bmp = _load_bitmap (image_file )
1350
1377
kind = wx .ITEM_NORMAL if not toggle else wx .ITEM_CHECK
@@ -1368,18 +1395,8 @@ def handler(event):
1368
1395
control .Bind (wx .EVT_LEFT_DOWN , handler )
1369
1396
1370
1397
self ._toolitems .setdefault (name , [])
1371
- group .insert (position , tool )
1372
1398
self ._toolitems [name ].append ((tool , handler ))
1373
1399
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
-
1383
1400
def toggle_toolitem (self , name , toggled ):
1384
1401
if name not in self ._toolitems :
1385
1402
return
0 commit comments