]> git.saurik.com Git - wxWidgets.git/blame - wxPython/demo/MaskedNumCtrl.py
Updated build files.
[wxWidgets.git] / wxPython / demo / MaskedNumCtrl.py
CommitLineData
8fa876ca
RD
1
2import string
3import sys
4import traceback
5
6import wx
c878ceea
RD
7from wx.lib import masked
8
8b9a4190
RD
9#----------------------------------------------------------------------
10
8fa876ca 11class TestPanel( wx.Panel ):
8b9a4190
RD
12 def __init__( self, parent, log ):
13
8fa876ca 14 wx.Panel.__init__( self, parent, -1 )
8b9a4190 15 self.log = log
8fa876ca 16 panel = wx.Panel( self, -1 )
8b9a4190 17
8fa876ca 18 header = wx.StaticText(panel, -1, """\
c878ceea 19This shows the various options for masked.NumCtrl.
8b9a4190
RD
20The controls at the top reconfigure the resulting control at the bottom.
21""")
22 header.SetForegroundColour( "Blue" )
23
8fa876ca 24 intlabel = wx.StaticText( panel, -1, "Integer width:" )
c878ceea 25 self.integerwidth = masked.NumCtrl(
8fa876ca
RD
26 panel, value=10, integerWidth=2, allowNegative=False
27 )
28
29 fraclabel = wx.StaticText( panel, -1, "Fraction width:" )
c878ceea
RD
30 self.fractionwidth = masked.NumCtrl(
31 panel, value=0, integerWidth=2, allowNegative=False
8fa876ca
RD
32 )
33
34 groupcharlabel = wx.StaticText( panel,-1, "Grouping char:" )
c878ceea 35 self.groupchar = masked.TextCtrl(
35d8bffe
RD
36 panel, -1, value=',', mask='*', includeChars = ' ', excludeChars = '-()0123456789',
37 formatcodes='F', emptyInvalid=False, validRequired=True
8fa876ca
RD
38 )
39
40 decimalcharlabel = wx.StaticText( panel,-1, "Decimal char:" )
c878ceea 41 self.decimalchar = masked.TextCtrl(
8fa876ca
RD
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:" )
c878ceea
RD
47 # Create this masked.NumCtrl using factory, to show how:
48 self.min = masked.Ctrl( panel, integerWidth=5, fractionWidth=2, controlType=masked.controlTypes.NUMBER )
8b9a4190
RD
49 self.min.Enable( False )
50
8fa876ca 51 self.set_max = wx.CheckBox( panel, -1, "Set maximum value:" )
c878ceea 52 self.max = masked.NumCtrl( panel, integerWidth=5, fractionWidth=2 )
8b9a4190
RD
53 self.max.Enable( False )
54
55
8fa876ca
RD
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" )
8b9a4190 59 self.group_digits.SetValue( True )
8fa876ca 60 self.allow_negative = wx.CheckBox( panel, -1, "Allow negative values" )
8b9a4190 61 self.allow_negative.SetValue( True )
8fa876ca
RD
62 self.use_parens = wx.CheckBox( panel, -1, "Use parentheses" )
63 self.select_on_entry = wx.CheckBox( panel, -1, "Select on entry" )
8b9a4190
RD
64 self.select_on_entry.SetValue( True )
65
8fa876ca 66 label = wx.StaticText( panel, -1, "Resulting numeric control:" )
8b9a4190 67 font = label.GetFont()
8fa876ca 68 font.SetWeight(wx.BOLD)
8b9a4190
RD
69 label.SetFont(font)
70
c878ceea 71 self.target_ctl = masked.NumCtrl( panel, -1, name="target control" )
8b9a4190 72
8fa876ca 73 label_numselect = wx.StaticText( panel, -1, """\
8b9a4190
RD
74Programmatically set the above
75value entry ctrl:""")
8fa876ca 76 self.numselect = wx.ComboBox(panel, -1, choices = [ '0', '111', '222.22', '-3', '54321.666666666', '-1353.978',
8b9a4190
RD
77 '1234567', '-1234567', '123456789', '-123456789.1',
78 '1234567890.', '-9876543210.9' ])
79
8fa876ca
RD
80 grid1 = wx.FlexGridSizer( 0, 4, 0, 0 )
81 grid1.Add( intlabel, 0, wx.ALIGN_LEFT|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)
3c29a62a 82 grid1.Add( self.integerwidth, 0, wx.ALIGN_LEFT|wx.ALL, 5 )
8fa876ca
RD
83
84 grid1.Add( groupcharlabel, 0, wx.ALIGN_LEFT|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)
3c29a62a 85 grid1.Add( self.groupchar, 0, wx.ALIGN_LEFT|wx.ALL, 5 )
8fa876ca
RD
86
87 grid1.Add( fraclabel, 0, wx.ALIGN_LEFT|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5 )
3c29a62a 88 grid1.Add( self.fractionwidth, 0, wx.ALIGN_LEFT|wx.ALL, 5 )
8fa876ca
RD
89
90 grid1.Add( decimalcharlabel, 0, wx.ALIGN_LEFT|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)
3c29a62a 91 grid1.Add( self.decimalchar, 0, wx.ALIGN_LEFT|wx.ALL, 5 )
8fa876ca
RD
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)
8b9a4190
RD
130 grid2.AddGrowableCol(1)
131
8fa876ca
RD
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 )
8b9a4190
RD
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
c878ceea
RD
144 self.Bind(masked.EVT_NUM, self.OnSetIntWidth, self.integerwidth )
145 self.Bind(masked.EVT_NUM, self.OnSetFractionWidth, self.fractionwidth )
8fa876ca
RD
146 self.Bind(wx.EVT_TEXT, self.OnSetGroupChar, self.groupchar )
147 self.Bind(wx.EVT_TEXT, self.OnSetDecimalChar, self.decimalchar )
8b9a4190 148
8fa876ca
RD
149 self.Bind(wx.EVT_CHECKBOX, self.OnSetMin, self.set_min )
150 self.Bind(wx.EVT_CHECKBOX, self.OnSetMax, self.set_max )
c878ceea
RD
151 self.Bind(masked.EVT_NUM, self.SetTargetMinMax, self.min )
152 self.Bind(masked.EVT_NUM, self.SetTargetMinMax, self.max )
8b9a4190 153
8fa876ca
RD
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 )
8b9a4190 160
c878ceea 161 self.Bind(masked.EVT_NUM, self.OnTargetChange, self.target_ctl )
8fa876ca 162 self.Bind(wx.EVT_COMBOBOX, self.OnNumberSelect, self.numselect )
8b9a4190
RD
163
164
165 def OnSetIntWidth(self, event ):
166 width = self.integerwidth.GetValue()
8fa876ca 167
8b9a4190
RD
168 if width < 1:
169 self.log.write("integer width must be positive\n")
8fa876ca 170 self.integerwidth.SetForegroundColour(wx.RED)
8b9a4190 171 else:
8fa876ca 172 self.integerwidth.SetForegroundColour(wx.BLACK)
8b9a4190
RD
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")
8fa876ca 195 self.groupchar.SetForegroundColour(wx.RED)
8b9a4190 196 else:
8fa876ca 197 self.groupchar.SetForegroundColour(wx.BLACK)
8b9a4190
RD
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")
8fa876ca 205 self.decimalchar.SetForegroundColour(wx.RED)
8b9a4190 206 else:
8fa876ca 207 self.decimalchar.SetForegroundColour(wx.BLACK)
8b9a4190
RD
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() ) )
8fa876ca 237 self.min.SetParameters( signedForegroundColour=wx.RED, foregroundColour=wx.RED )
8b9a4190 238 else:
8fa876ca 239 self.min.SetParameters( signedForegroundColour=wx.BLACK, foregroundColour=wx.BLACK )
8b9a4190
RD
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() ) )
8fa876ca 247 self.max.SetParameters( signedForegroundColour=wx.RED, foregroundColour=wx.RED )
8b9a4190 248 else:
8fa876ca 249 self.max.SetParameters( signedForegroundColour=wx.BLACK, foregroundColour=wx.BLACK )
8b9a4190
RD
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
321def runTest( frame, nb, log ):
322 win = TestPanel( nb, log )
323 return win
324
325#----------------------------------------------------------------------
c878ceea 326import wx.lib.masked.numctrl as mnum
f54a36bb
RD
327overview = """<html>
328<PRE><FONT SIZE=-1>
329""" + mnum.__doc__ + """
330</FONT></PRE>"""
8b9a4190
RD
331
332if __name__ == '__main__':
333 import sys,os
334 import run
8eca4fef 335 run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])