]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/MaskedNumCtrl.py
Test the location of the .zips for writability, since the indexes are
[wxWidgets.git] / wxPython / demo / MaskedNumCtrl.py
1
2 import string
3 import sys
4 import traceback
5
6 import wx
7 import wx.lib.maskededit as me
8 import wx.lib.maskednumctrl as mnum
9 #----------------------------------------------------------------------
10
11 class TestPanel( wx.Panel ):
12 def __init__( self, parent, log ):
13
14 wx.Panel.__init__( self, parent, -1 )
15 self.log = log
16 panel = wx.Panel( self, -1 )
17
18 header = wx.StaticText(panel, -1, """\
19 This shows the various options for MaskedNumCtrl.
20 The controls at the top reconfigure the resulting control at the bottom.
21 """)
22 header.SetForegroundColour( "Blue" )
23
24 intlabel = wx.StaticText( panel, -1, "Integer width:" )
25 self.integerwidth = mnum.MaskedNumCtrl(
26 panel, value=10, integerWidth=2, allowNegative=False
27 )
28
29 fraclabel = wx.StaticText( panel, -1, "Fraction width:" )
30 self.fractionwidth = mnum.MaskedNumCtrl(
31 panel, value=0, integerWidth=2, allowNegative=False
32 )
33
34 groupcharlabel = wx.StaticText( panel,-1, "Grouping char:" )
35 self.groupchar = me.MaskedTextCtrl(
36 panel, -1, value=',', mask='&', excludeChars = '-()',
37 formatcodes='F', emptyInvalid=True, validRequired=True
38 )
39
40 decimalcharlabel = wx.StaticText( panel,-1, "Decimal char:" )
41 self.decimalchar = me.MaskedTextCtrl(
42 panel, -1, value='.', mask='&', excludeChars = '-()',
43 formatcodes='F', emptyInvalid=True, validRequired=True
44 )
45
46 self.set_min = wx.CheckBox( panel, -1, "Set minimum value:" )
47 # Create this MaskedNumCtrl using factory, to show how:
48 self.min = mnum.MaskedNumCtrl( panel, integerWidth=5, fractionWidth=2 )
49 self.min.Enable( False )
50
51 self.set_max = wx.CheckBox( panel, -1, "Set maximum value:" )
52 self.max = mnum.MaskedNumCtrl( panel, integerWidth=5, fractionWidth=2 )
53 self.max.Enable( False )
54
55
56 self.limit_target = wx.CheckBox( panel, -1, "Limit control" )
57 self.allow_none = wx.CheckBox( panel, -1, "Allow empty control" )
58 self.group_digits = wx.CheckBox( panel, -1, "Group digits" )
59 self.group_digits.SetValue( True )
60 self.allow_negative = wx.CheckBox( panel, -1, "Allow negative values" )
61 self.allow_negative.SetValue( True )
62 self.use_parens = wx.CheckBox( panel, -1, "Use parentheses" )
63 self.select_on_entry = wx.CheckBox( panel, -1, "Select on entry" )
64 self.select_on_entry.SetValue( True )
65
66 label = wx.StaticText( panel, -1, "Resulting numeric control:" )
67 font = label.GetFont()
68 font.SetWeight(wx.BOLD)
69 label.SetFont(font)
70
71 self.target_ctl = mnum.MaskedNumCtrl( panel, -1, name="target control" )
72
73 label_numselect = wx.StaticText( panel, -1, """\
74 Programmatically set the above
75 value entry ctrl:""")
76 self.numselect = wx.ComboBox(panel, -1, choices = [ '0', '111', '222.22', '-3', '54321.666666666', '-1353.978',
77 '1234567', '-1234567', '123456789', '-123456789.1',
78 '1234567890.', '-9876543210.9' ])
79
80 grid1 = wx.FlexGridSizer( 0, 4, 0, 0 )
81 grid1.Add( intlabel, 0, wx.ALIGN_LEFT|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)
82 grid1.Add( self.integerwidth, 0, wx.ALIGN_LEFT|wx.ALL, 5 )
83
84 grid1.Add( groupcharlabel, 0, wx.ALIGN_LEFT|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)
85 grid1.Add( self.groupchar, 0, wx.ALIGN_LEFT|wx.ALL, 5 )
86
87 grid1.Add( fraclabel, 0, wx.ALIGN_LEFT|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5 )
88 grid1.Add( self.fractionwidth, 0, wx.ALIGN_LEFT|wx.ALL, 5 )
89
90 grid1.Add( decimalcharlabel, 0, wx.ALIGN_LEFT|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)
91 grid1.Add( self.decimalchar, 0, wx.ALIGN_LEFT|wx.ALL, 5 )
92
93 grid1.Add( self.set_min, 0, wx.ALIGN_LEFT|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5 )
94 grid1.Add( self.min, 0, wx.ALIGN_LEFT|wx.ALL, 5 )
95 grid1.Add( (5,5), 0, wx.ALIGN_LEFT|wx.ALL, 5)
96 grid1.Add( (5,5), 0, wx.ALIGN_LEFT|wx.ALL, 5)
97
98 grid1.Add( self.set_max, 0, wx.ALIGN_LEFT|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5 )
99 grid1.Add( self.max, 0, wx.ALIGN_LEFT|wx.ALL, 5 )
100 grid1.Add( (5,5), 0, wx.ALIGN_LEFT|wx.ALL, 5)
101 grid1.Add( (5,5), 0, wx.ALIGN_LEFT|wx.ALL, 5)
102
103
104 grid1.Add( self.limit_target, 0, wx.ALIGN_LEFT|wx.ALL, 5 )
105 grid1.Add( self.allow_none, 0, wx.ALIGN_LEFT|wx.ALL, 5 )
106 hbox1 = wx.BoxSizer( wx.HORIZONTAL )
107 hbox1.Add( (17,5), 0, wx.ALIGN_LEFT|wx.ALL, 5)
108 hbox1.Add( self.group_digits, 0, wx.ALIGN_LEFT|wx.LEFT, 5 )
109 grid1.Add( hbox1, 0, wx.ALIGN_LEFT|wx.ALL, 5)
110 grid1.Add( (5,5), 0, wx.ALIGN_LEFT|wx.ALL, 5)
111
112 grid1.Add( self.allow_negative, 0, wx.ALIGN_LEFT|wx.ALL, 5 )
113 grid1.Add( self.use_parens, 0, wx.ALIGN_LEFT|wx.ALL, 5 )
114 hbox2 = wx.BoxSizer( wx.HORIZONTAL )
115 hbox2.Add( (17,5), 0, wx.ALIGN_LEFT|wx.ALL, 5)
116 hbox2.Add( self.select_on_entry, 0, wx.ALIGN_LEFT|wx.LEFT, 5 )
117 grid1.Add( hbox2, 0, wx.ALIGN_LEFT|wx.ALL, 5)
118 grid1.Add( (5,5), 0, wx.ALIGN_LEFT|wx.ALL, 5)
119
120
121 grid2 = wx.FlexGridSizer( 0, 2, 0, 0 )
122 grid2.Add( label, 0, wx.ALIGN_LEFT|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5 )
123 grid2.Add( self.target_ctl, 0, wx.ALIGN_LEFT|wx.ALL, 5 )
124 grid2.Add( (5,5), 0, wx.ALIGN_LEFT|wx.ALL, 5)
125 grid2.Add( (5,5), 0, wx.ALIGN_LEFT|wx.ALL, 5)
126 grid2.Add( label_numselect, 0, wx.ALIGN_LEFT|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5 )
127 grid2.Add( self.numselect, 0, wx.ALIGN_LEFT|wx.ALL, 5 )
128 grid2.Add( (5,5), 0, wx.ALIGN_LEFT|wx.ALL, 5)
129 grid2.Add( (5,5), 0, wx.ALIGN_LEFT|wx.ALL, 5)
130 grid2.AddGrowableCol(1)
131
132 self.outer_box = wx.BoxSizer( wx.VERTICAL )
133 self.outer_box.Add(header, 0, wx.ALIGN_LEFT|wx.TOP|wx.LEFT, 20)
134 self.outer_box.Add( grid1, 0, wx.ALIGN_CENTRE|wx.LEFT|wx.BOTTOM|wx.RIGHT, 20 )
135 self.outer_box.Add( grid2, 0, wx.ALIGN_LEFT|wx.ALL, 20 )
136 self.grid2 = grid2
137
138 panel.SetAutoLayout( True )
139 panel.SetSizer( self.outer_box )
140 self.outer_box.Fit( panel )
141 panel.Move( (50,10) )
142 self.panel = panel
143
144 self.Bind(mnum.EVT_MASKEDNUM, self.OnSetIntWidth, self.integerwidth )
145 self.Bind(mnum.EVT_MASKEDNUM, self.OnSetFractionWidth, self.fractionwidth )
146 self.Bind(wx.EVT_TEXT, self.OnSetGroupChar, self.groupchar )
147 self.Bind(wx.EVT_TEXT, self.OnSetDecimalChar, self.decimalchar )
148
149 self.Bind(wx.EVT_CHECKBOX, self.OnSetMin, self.set_min )
150 self.Bind(wx.EVT_CHECKBOX, self.OnSetMax, self.set_max )
151 self.Bind(mnum.EVT_MASKEDNUM, self.SetTargetMinMax, self.min )
152 self.Bind(mnum.EVT_MASKEDNUM, self.SetTargetMinMax, self.max )
153
154 self.Bind(wx.EVT_CHECKBOX, self.SetTargetMinMax, self.limit_target )
155 self.Bind(wx.EVT_CHECKBOX, self.OnSetAllowNone, self.allow_none )
156 self.Bind(wx.EVT_CHECKBOX, self.OnSetGroupDigits, self.group_digits )
157 self.Bind(wx.EVT_CHECKBOX, self.OnSetAllowNegative, self.allow_negative )
158 self.Bind(wx.EVT_CHECKBOX, self.OnSetUseParens, self.use_parens )
159 self.Bind(wx.EVT_CHECKBOX, self.OnSetSelectOnEntry, self.select_on_entry )
160
161 self.Bind(mnum.EVT_MASKEDNUM, self.OnTargetChange, self.target_ctl )
162 self.Bind(wx.EVT_COMBOBOX, self.OnNumberSelect, self.numselect )
163
164
165 def OnSetIntWidth(self, event ):
166 width = self.integerwidth.GetValue()
167
168 if width < 1:
169 self.log.write("integer width must be positive\n")
170 self.integerwidth.SetForegroundColour(wx.RED)
171 else:
172 self.integerwidth.SetForegroundColour(wx.BLACK)
173 self.log.write("setting integer width to %d\n" % width)
174 self.target_ctl.SetParameters( integerWidth = width)
175 # Now resize and fit the dialog as appropriate:
176 self.grid2.SetItemMinSize(self.target_ctl, self.target_ctl.GetSize())
177 self.outer_box.Fit( self.panel )
178 self.outer_box.SetSizeHints( self.panel )
179
180
181 def OnSetFractionWidth(self, event ):
182 width = self.fractionwidth.GetValue()
183 self.log.write("setting fraction width to %d\n" % width)
184 self.target_ctl.SetParameters( fractionWidth = width)
185 # Now resize and fit the dialog as appropriate:
186 self.grid2.SetItemMinSize(self.target_ctl, self.target_ctl.GetSize())
187 self.outer_box.Fit( self.panel )
188 self.outer_box.SetSizeHints( self.panel )
189
190
191 def OnSetGroupChar( self, event ):
192 char = self.groupchar.GetValue()
193 if self.target_ctl.GetDecimalChar() == char:
194 self.log.write("group and decimal chars must be different\n")
195 self.groupchar.SetForegroundColour(wx.RED)
196 else:
197 self.groupchar.SetForegroundColour(wx.BLACK)
198 self.log.write("setting group char to %s\n" % char)
199 self.target_ctl.SetGroupChar( char )
200
201 def OnSetDecimalChar( self, event ):
202 char = self.decimalchar.GetValue()
203 if self.target_ctl.GetGroupChar() == char:
204 self.log.write("group and decimal chars must be different\n")
205 self.decimalchar.SetForegroundColour(wx.RED)
206 else:
207 self.decimalchar.SetForegroundColour(wx.BLACK)
208 self.log.write("setting decimal char to %s\n" % char)
209 self.target_ctl.SetDecimalChar( char )
210
211
212 def OnSetMin( self, event ):
213 self.min.Enable( self.set_min.GetValue() )
214 self.SetTargetMinMax()
215
216 def OnSetMax( self, event ):
217 self.max.Enable( self.set_max.GetValue() )
218 self.SetTargetMinMax()
219
220
221 def SetTargetMinMax( self, event=None ):
222 min = max = None
223 self.target_ctl.SetLimited( self.limit_target.GetValue() )
224
225 if self.set_min.GetValue():
226 min = self.min.GetValue()
227 if self.set_max.GetValue():
228 max = self.max.GetValue()
229
230 cur_min, cur_max = self.target_ctl.GetBounds()
231
232 if min != cur_min and not self.target_ctl.SetMin( min ):
233 if self.target_ctl.GetMax() is None and cur_max > min:
234 self.log.write( "min (%d) won't fit in control -- bound not set\n" % min )
235 else:
236 self.log.write( "min (%d) > current max (%d) -- bound not set\n" % ( min, self.target_ctl.GetMax() ) )
237 self.min.SetParameters( signedForegroundColour=wx.RED, foregroundColour=wx.RED )
238 else:
239 self.min.SetParameters( signedForegroundColour=wx.BLACK, foregroundColour=wx.BLACK )
240 self.min.Refresh()
241
242 if max != cur_max and not self.target_ctl.SetMax( max ):
243 if self.target_ctl.GetMax() is None and cur_min < max:
244 self.log.write( "max (%d) won't fit in control -- bound not set\n" % max )
245 else:
246 self.log.write( "max (%d) < current min (%d) -- bound not set\n" % ( max, self.target_ctl.GetMin() ) )
247 self.max.SetParameters( signedForegroundColour=wx.RED, foregroundColour=wx.RED )
248 else:
249 self.max.SetParameters( signedForegroundColour=wx.BLACK, foregroundColour=wx.BLACK )
250 self.max.Refresh()
251
252 if min != cur_min or max != cur_max:
253 new_min, new_max = self.target_ctl.GetBounds()
254 self.log.write( "current min, max: (%s, %s)\n" % ( str(new_min), str(new_max) ) )
255
256
257 def OnSetAllowNone( self, event ):
258 self.target_ctl.SetAllowNone( self.allow_none.GetValue() )
259
260
261 def OnSetGroupDigits( self, event ):
262 self.target_ctl.SetGroupDigits( self.group_digits.GetValue() )
263 # Now resize and fit the dialog as appropriate:
264 self.grid2.SetItemMinSize(self.target_ctl, self.target_ctl.GetSize())
265 self.outer_box.Fit( self.panel )
266 self.outer_box.SetSizeHints( self.panel )
267
268
269 def OnSetAllowNegative( self, event ):
270 if self.allow_negative.GetValue():
271 self.use_parens.Enable(True)
272 self.target_ctl.SetParameters(allowNegative=True,
273 useParensForNegatives = self.use_parens.GetValue())
274 else:
275 self.target_ctl.SetAllowNegative(False)
276 # Now resize and fit the dialog as appropriate:
277 self.grid2.SetItemMinSize(self.target_ctl, self.target_ctl.GetSize())
278 self.outer_box.Fit( self.panel )
279 self.outer_box.SetSizeHints( self.panel )
280
281
282 def OnSetUseParens( self, event ):
283 self.target_ctl.SetUseParensForNegatives( self.use_parens.GetValue() )
284 # Now resize and fit the dialog as appropriate:
285 self.grid2.SetItemMinSize(self.target_ctl, self.target_ctl.GetSize())
286 self.outer_box.Fit( self.panel )
287 self.outer_box.SetSizeHints( self.panel )
288
289
290 def OnSetSelectOnEntry( self, event ):
291 self.target_ctl.SetSelectOnEntry( self.select_on_entry.GetValue() )
292
293
294 def OnTargetChange( self, event ):
295 ctl = event.GetEventObject()
296 value = ctl.GetValue()
297 ib_str = [ " (out of bounds)", "" ]
298 self.log.write( "value = %s (%s)%s\n" % ( repr(value), repr(type(value)), ib_str[ ctl.IsInBounds(value) ] ) )
299
300
301 def OnNumberSelect( self, event ):
302 value = event.GetString()
303 if value:
304 if value.find('.') != -1:
305 numvalue = float(value)
306 else:
307 numvalue = long(value)
308 else:
309 numvalue = value # try to clear the value again
310
311 try:
312 self.target_ctl.SetValue(numvalue)
313 except:
314 type, value, tb = sys.exc_info()
315 for line in traceback.format_exception_only(type, value):
316 self.log.write(line)
317
318
319 #----------------------------------------------------------------------
320
321 def runTest( frame, nb, log ):
322 win = TestPanel( nb, log )
323 return win
324
325 #----------------------------------------------------------------------
326 overview = mnum.__doc__
327
328 if __name__ == '__main__':
329 import sys,os
330 import run
331 run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])