@@ -1073,16 +1073,42 @@ 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
+ text .SetSize (event .Size .Width - text .Position .x , text .Size .Height )
1110
+ event .Skip ()
1111
+
1086
1112
@staticmethod
1087
1113
def _icon (name ):
1088
1114
"""
@@ -1179,7 +1205,7 @@ def remove_rubberband(self):
1179
1205
self .canvas .Refresh ()
1180
1206
1181
1207
def set_message (self , s ):
1182
- if self ._coordinates :
1208
+ if self ._label_text :
1183
1209
self ._label_text .SetLabel (s )
1184
1210
1185
1211
def set_history_buttons (self ):
@@ -1201,9 +1227,22 @@ def __init__(self, toolmanager, parent=None, style=wx.TB_BOTTOM):
1201
1227
parent = toolmanager .canvas .GetParent ()
1202
1228
ToolContainerBase .__init__ (self , toolmanager )
1203
1229
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 )
1206
1237
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 ))
1207
1246
self ._toolitems = {}
1208
1247
self ._groups = {} # Mapping of groups to the separator after them.
1209
1248
@@ -1222,8 +1261,9 @@ def add_toolitem(self, name, group, position, image_file, description,
1222
1261
toggle ):
1223
1262
# Find or create the separator that follows this group.
1224
1263
if group not in self ._groups :
1264
+ # On wxGTK we add a StretchableSpace which must also be skipped.
1225
1265
self ._groups [group ] = self .InsertSeparator (
1226
- self ._get_tool_pos ( self . _space ))
1266
+ self .ToolsCount - ( 2 if wx . Platform == "__WXGTK__" else 1 ))
1227
1267
sep = self ._groups [group ]
1228
1268
# List all separators.
1229
1269
seps = [t for t in map (self .GetToolByPos , range (self .ToolsCount ))
0 commit comments