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