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