@@ -1073,16 +1073,44 @@ def __init__(self, canvas, coordinates=True, *, style=wx.TB_BOTTOM):
1073
1073
self .Bind (wx .EVT_TOOL , getattr (self , callback ),
1074
1074
id = self .wx_ids [text ])
1075
1075
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 )
1080
1093
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
1081
1100
1082
1101
self .Realize ()
1083
1102
1084
1103
NavigationToolbar2 .__init__ (self , canvas )
1085
1104
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
+
1086
1114
@staticmethod
1087
1115
def _icon (name ):
1088
1116
"""
@@ -1179,7 +1207,7 @@ def remove_rubberband(self):
1179
1207
self .canvas .Refresh ()
1180
1208
1181
1209
def set_message (self , s ):
1182
- if self ._coordinates :
1210
+ if self ._label_text :
1183
1211
self ._label_text .SetLabel (s )
1184
1212
1185
1213
def set_history_buttons (self ):
@@ -1201,9 +1229,22 @@ def __init__(self, toolmanager, parent=None, style=wx.TB_BOTTOM):
1201
1229
parent = toolmanager .canvas .GetParent ()
1202
1230
ToolContainerBase .__init__ (self , toolmanager )
1203
1231
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 )
1206
1239
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 ))
1207
1248
self ._toolitems = {}
1208
1249
self ._groups = {} # Mapping of groups to the separator after them.
1209
1250
@@ -1222,8 +1263,9 @@ def add_toolitem(self, name, group, position, image_file, description,
1222
1263
toggle ):
1223
1264
# Find or create the separator that follows this group.
1224
1265
if group not in self ._groups :
1266
+ # On wxGTK we add a StretchableSpace which must also be skipped.
1225
1267
self ._groups [group ] = self .InsertSeparator (
1226
- self ._get_tool_pos ( self . _space ))
1268
+ self .ToolsCount - ( 2 if wx . Platform == "__WXGTK__" else 1 ))
1227
1269
sep = self ._groups [group ]
1228
1270
# List all separators.
1229
1271
seps = [t for t in map (self .GetToolByPos , range (self .ToolsCount ))
0 commit comments