-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEnhancedStatusBar.py
245 lines (195 loc) · 10 KB
/
EnhancedStatusBar.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# --------------------------------------------------------------------------- #
# ENHANCEDSTATUSBAR wxPython IMPLEMENTATION
# Python Code By:
#
# Andrea Gavana, @ 31 May 2005
# Nitro, @ 21 September 2005
# Latest Revision: 21 September 2005, 19.57.20 GMT+2
# Latest Revision before Latest Revision: 21 September 2005, 18.29.35 GMT+2
# Latest Revision before Latest Revision before Latest Revision: 31 May 2005, 23.17 CET
#
#
# TODO List/Caveats
#
# 1. Some Requests/Features To Add?
#
#
# For All Kind Of Problems, Requests Of Enhancements And Bug Reports, Please
# Write To Me At:
#
#
# Or, Obviously, To The wxPython Mailing List!!!
#
#
# End Of Comments
# --------------------------------------------------------------------------- #
""" Description:
EnhancedStatusBar Is A Slight Modification (Actually A Subclassing) Of wx.StatusBar.
It Allows You To Add Almost Any Widget You Like To The wx.StatusBar Of Your Main
Frame Application And Also To Layout Them Properly.
What It Can Do:
1) Almost All The Functionalities Of wx.StatusBar Are Still Present;
2) You Can Add Different Kind Of Widgets Into Every Field Of The EnhancedStatusBar;
3) The AddWidget() Methods Accepts 2 Layout Inputs:
- horizontalalignment: This Specify The Horizontal Alignment For Your Widget,
And Can Be ESB_EXACT_FIT, ESB_ALIGN_CENTER_HORIZONTAL, ESB_ALIGN_LEFT And
ESB_ALIGN_RIGHT;
- varticalalignment: This Specify The Vertical Alignment For Your Widget,
And Can Be ESB_EXACT_FIT, ESB_ALIGN_CENTER_VERTICAL, ESB_ALIGN_BOTTOM And
ESB_ALIGN_LEFT;
EnhancedStatusBar Is Freeware And Distributed Under The wxPython License.
Latest Revision: 21 September 2005, 19.57.20 GMT+2
Latest Revision before Latest Revision: 21 September 2005, 18.29.35 GMT+2
Latest Revision before Latest Revision before Latest Revision: 31 May 2005, 23.17 CET
"""
import wx
# Horizontal Alignment Constants
ESB_ALIGN_CENTER_VERTICAL = 1
ESB_ALIGN_TOP = 2
ESB_ALIGN_BOTTOM = 3
# Vertical Alignment Constants
ESB_ALIGN_CENTER_HORIZONTAL = 11
ESB_ALIGN_LEFT = 12
ESB_ALIGN_RIGHT = 13
# Exact Fit (Either Horizontal Or Vertical Or Both) Constant
ESB_EXACT_FIT = 20
# ---------------------------------------------------------------
# Class EnhancedStatusBar
# ---------------------------------------------------------------
# This Is The Main Class Implementation. See The Demo For Details
# ---------------------------------------------------------------
class EnhancedStatusBarItem(object):
def __init__(self, widget, pos, horizontalalignment=ESB_ALIGN_CENTER_HORIZONTAL, verticalalignment=ESB_ALIGN_CENTER_VERTICAL):
self.__dict__.update( locals() )
class EnhancedStatusBar(wx.StatusBar):
def __init__(self, parent, id=wx.ID_ANY, style=wx.ST_SIZEGRIP,
name=u"EnhancedStatusBar"):
"""Default Class Constructor.
EnhancedStatusBar.__init__(self, parent, id=wx.ID_ANY,
style=wx.ST_SIZEGRIP,
name="EnhancedStatusBar")
"""
wx.StatusBar.__init__(self, parent, id, style, name)
self._items = {}
self._curPos = 0
self._parent = parent
wx.EVT_SIZE(self, self.OnSize)
wx.CallAfter(self.OnSize, None)
def OnSize(self, event):
"""Handles The wx.EVT_SIZE Events For The StatusBar.
Actually, All The Calculations Linked To HorizontalAlignment And
VerticalAlignment Are Done In This Function."""
for pos, item in self._items.items():
widget, horizontalalignment, verticalalignment = item.widget, item.horizontalalignment, item.verticalalignment
rect = self.GetFieldRect(pos)
widgetpos = widget.GetPosition()
widgetsize = widget.GetSize()
rect = self.GetFieldRect(pos)
if horizontalalignment == ESB_EXACT_FIT:
if verticalalignment == ESB_EXACT_FIT:
widget.SetSize((rect.width-2, rect.height-2))
widget.SetPosition((rect.x-1, rect.y-1))
elif verticalalignment == ESB_ALIGN_CENTER_VERTICAL:
if widgetsize[1] < rect.width - 1:
diffs = (rect.height - widgetsize[1])/2
widget.SetSize((rect.width-2, widgetsize[1]))
widget.SetPosition((rect.x-1, rect.y+diffs))
else:
widget.SetSize((rect.width-2, widgetsize[1]))
widget.SetPosition((rect.x-1, rect.y-1))
elif verticalalignment == ESB_ALIGN_TOP:
widget.SetSize((rect.width-2, widgetsize[1]))
widget.SetPosition((rect.x-1, rect.y))
elif verticalalignment == ESB_ALIGN_BOTTOM:
widget.SetSize((rect.width-2, widgetsize[1]))
widget.SetPosition((rect.x-1, rect.height-widgetsize[1]))
elif horizontalalignment == ESB_ALIGN_LEFT:
xpos = rect.x - 1
if verticalalignment == ESB_EXACT_FIT:
widget.SetSize((widgetsize[0], rect.height-2))
widget.SetPosition((xpos, rect.y-1))
elif verticalalignment == ESB_ALIGN_CENTER_VERTICAL:
if widgetsize[1] < rect.height - 1:
diffs = (rect.height - widgetsize[1])/2
widget.SetPosition((xpos, rect.y+diffs))
else:
widget.SetSize((widgetsize[0], rect.height-2))
widget.SetPosition((xpos, rect.y-1))
elif verticalalignment == ESB_ALIGN_TOP:
widget.SetPosition((xpos, rect.y))
elif verticalalignment == ESB_ALIGN_BOTTOM:
widget.SetPosition((xpos, rect.height-widgetsize[1]))
elif horizontalalignment == ESB_ALIGN_RIGHT:
xpos = rect.x + rect.width - widgetsize[0] - 1
if verticalalignment == ESB_EXACT_FIT:
widget.SetSize((widgetsize[0], rect.height-2))
widget.SetPosition((xpos, rect.y-1))
elif verticalalignment == ESB_ALIGN_CENTER_VERTICAL:
if widgetsize[1] < rect.height - 1:
diffs = (rect.height - widgetsize[1])/2
widget.SetPosition((xpos, rect.y+diffs))
else:
widget.SetSize((widgetsize[0], rect.height-2))
widget.SetPosition((xpos, rect.y-1))
elif verticalalignment == ESB_ALIGN_TOP:
widget.SetPosition((xpos, rect.y))
elif verticalalignment == ESB_ALIGN_BOTTOM:
widget.SetPosition((xpos, rect.height-widgetsize[1]))
elif horizontalalignment == ESB_ALIGN_CENTER_HORIZONTAL:
xpos = rect.x + (rect.width - widgetsize[0])/2 - 1
if verticalalignment == ESB_EXACT_FIT:
widget.SetSize((widgetsize[0], rect.height))
widget.SetPosition((xpos, rect.y))
elif verticalalignment == ESB_ALIGN_CENTER_VERTICAL:
if widgetsize[1] < rect.height - 1:
diffs = (rect.height - widgetsize[1])/2
widget.SetPosition((xpos, rect.y+diffs))
else:
widget.SetSize((widgetsize[0], rect.height-1))
widget.SetPosition((xpos, rect.y+1))
elif verticalalignment == ESB_ALIGN_TOP:
widget.SetPosition((xpos, rect.y))
elif verticalalignment == ESB_ALIGN_BOTTOM:
widget.SetPosition((xpos, rect.height-widgetsize[1]))
if event is not None:
event.Skip()
def AddWidget(self, widget, horizontalalignment=ESB_ALIGN_CENTER_HORIZONTAL,
verticalalignment=ESB_ALIGN_CENTER_VERTICAL, pos = -1):
"""Add A Widget To The EnhancedStatusBar.
Parameters:
- horizontalalignment: This Can Be One Of:
a) ESB_EXACT_FIT: The Widget Will Fit Horizontally The StatusBar Field Width;
b) ESB_ALIGN_CENTER_HORIZONTAL: The Widget Will Be Centered Horizontally In
The StatusBar Field;
c) ESB_ALIGN_LEFT: The Widget Will Be Left Aligned In The StatusBar Field;
d) ESB_ALIGN_RIGHT: The Widget Will Be Right Aligned In The StatusBar Field;
- verticalalignment:
a) ESB_EXACT_FIT: The Widget Will Fit Vertically The StatusBar Field Height;
b) ESB_ALIGN_CENTER_VERTICAL: The Widget Will Be Centered Vertically In
The StatusBar Field;
c) ESB_ALIGN_BOTTOM: The Widget Will Be Bottom Aligned In The StatusBar Field;
d) ESB_ALIGN_TOP: The Widget Will Be TOP Aligned In The StatusBar Field;
"""
if pos == -1:
pos = self._curPos
self._curPos += 1
if self.GetFieldsCount() <= pos:
raise u"\nERROR: EnhancedStatusBar has a max of %d items, you tried to set item #%d" % (self.GetFieldsCount(), pos)
if horizontalalignment not in [ESB_ALIGN_CENTER_HORIZONTAL, ESB_EXACT_FIT,
ESB_ALIGN_LEFT, ESB_ALIGN_RIGHT]:
raise u'\nERROR: Parameter "horizontalalignment" Should Be One Of '\
u'"ESB_ALIGN_CENTER_HORIZONTAL", "ESB_ALIGN_LEFT", "ESB_ALIGN_RIGHT"' \
u'"ESB_EXACT_FIT"'
if verticalalignment not in [ESB_ALIGN_CENTER_VERTICAL, ESB_EXACT_FIT,
ESB_ALIGN_TOP, ESB_ALIGN_BOTTOM]:
raise u'\nERROR: Parameter "verticalalignment" Should Be One Of '\
u'"ESB_ALIGN_CENTER_VERTICAL", "ESB_ALIGN_TOP", "ESB_ALIGN_BOTTOM"' \
u'"ESB_EXACT_FIT"'
try:
self.RemoveChild(self._items[pos].widget)
self._items[pos].widget.Destroy()
except KeyError: pass
self._items[pos] = EnhancedStatusBarItem(widget, pos, horizontalalignment, verticalalignment)
wx.CallAfter(self.OnSize, None)