]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/MaskedNumCtrl.py
1 # 11/20/2003 - Jeff Grimmett (grimmtooth@softhome.net)
3 # o Updated for wx namespace
5 # 11/29/2003 - Jeff Grimmett (grimmtooth@softhome.net)
7 # o wx.lib.maskednumctrl needs hit up with the renamer and new binders
9 # 12/09/2003 - Jeff Grimmett (grimmtooth@softhome.net)
11 # o Issues with lib corrected.
13 # 12/20/2003 - Jeff Grimmett (grimmtooth@softhome.net)
15 # o wxMaskedNumCtrl -> MaskedNumCtrl
23 import wx
.lib
.maskednumctrl
as mnum
24 #----------------------------------------------------------------------
26 class TestPanel( wx
.Panel
):
27 def __init__( self
, parent
, log
):
29 wx
.Panel
.__init
__( self
, parent
, -1 )
31 panel
= wx
.Panel( self
, -1 )
33 header
= wx
.StaticText(panel
, -1, """\
34 This shows the various options for MaskedNumCtrl.
35 The controls at the top reconfigure the resulting control at the bottom.
37 header
.SetForegroundColour( "Blue" )
39 intlabel
= wx
.StaticText( panel
, -1, "Integer width:" )
40 self
.integerwidth
= mnum
.MaskedNumCtrl(
41 panel
, value
=10, integerWidth
=2, allowNegative
=False
44 fraclabel
= wx
.StaticText( panel
, -1, "Fraction width:" )
45 self
.fractionwidth
= mnum
.MaskedNumCtrl(
46 panel
, value
=0, integerWidth
=2, allowNegative
=False
49 groupcharlabel
= wx
.StaticText( panel
,-1, "Grouping char:" )
50 self
.groupchar
= mnum
.MaskedTextCtrl(
51 panel
, -1, value
=',', mask
='&', excludeChars
= '-()',
52 formatcodes
='F', emptyInvalid
=True, validRequired
=True
55 decimalcharlabel
= wx
.StaticText( panel
,-1, "Decimal char:" )
56 self
.decimalchar
= mnum
.MaskedTextCtrl(
57 panel
, -1, value
='.', mask
='&', excludeChars
= '-()',
58 formatcodes
='F', emptyInvalid
=True, validRequired
=True
61 self
.set_min
= wx
.CheckBox( panel
, -1, "Set minimum value:" )
62 # Create this MaskedNumCtrl using factory, to show how:
63 self
.min = mnum
.MaskedNumCtrl( panel
, integerWidth
=5, fractionWidth
=2 )
64 self
.min.Enable( False )
66 self
.set_max
= wx
.CheckBox( panel
, -1, "Set maximum value:" )
67 self
.max = mnum
.MaskedNumCtrl( panel
, integerWidth
=5, fractionWidth
=2 )
68 self
.max.Enable( False )
71 self
.limit_target
= wx
.CheckBox( panel
, -1, "Limit control" )
72 self
.allow_none
= wx
.CheckBox( panel
, -1, "Allow empty control" )
73 self
.group_digits
= wx
.CheckBox( panel
, -1, "Group digits" )
74 self
.group_digits
.SetValue( True )
75 self
.allow_negative
= wx
.CheckBox( panel
, -1, "Allow negative values" )
76 self
.allow_negative
.SetValue( True )
77 self
.use_parens
= wx
.CheckBox( panel
, -1, "Use parentheses" )
78 self
.select_on_entry
= wx
.CheckBox( panel
, -1, "Select on entry" )
79 self
.select_on_entry
.SetValue( True )
81 label
= wx
.StaticText( panel
, -1, "Resulting numeric control:" )
82 font
= label
.GetFont()
83 font
.SetWeight(wx
.BOLD
)
86 self
.target_ctl
= mnum
.MaskedNumCtrl( panel
, -1, name
="target control" )
88 label_numselect
= wx
.StaticText( panel
, -1, """\
89 Programmatically set the above
91 self
.numselect
= wx
.ComboBox(panel
, -1, choices
= [ '0', '111', '222.22', '-3', '54321.666666666', '-1353.978',
92 '1234567', '-1234567', '123456789', '-123456789.1',
93 '1234567890.', '-9876543210.9' ])
95 grid1
= wx
.FlexGridSizer( 0, 4, 0, 0 )
96 grid1
.Add( intlabel
, 0, wx
.ALIGN_LEFT|wx
.ALIGN_CENTER_VERTICAL|wx
.ALL
, 5)
97 grid1
.Add( self
.integerwidth
, 0, wx
.ALIGN_LEFT|wx
.ALL
, 5 )
99 grid1
.Add( groupcharlabel
, 0, wx
.ALIGN_LEFT|wx
.ALIGN_CENTER_VERTICAL|wx
.ALL
, 5)
100 grid1
.Add( self
.groupchar
, 0, wx
.ALIGN_LEFT|wx
.ALL
, 5 )
102 grid1
.Add( fraclabel
, 0, wx
.ALIGN_LEFT|wx
.ALIGN_CENTER_VERTICAL|wx
.ALL
, 5 )
103 grid1
.Add( self
.fractionwidth
, 0, wx
.ALIGN_LEFT|wx
.ALL
, 5 )
105 grid1
.Add( decimalcharlabel
, 0, wx
.ALIGN_LEFT|wx
.ALIGN_CENTER_VERTICAL|wx
.ALL
, 5)
106 grid1
.Add( self
.decimalchar
, 0, wx
.ALIGN_LEFT|wx
.ALL
, 5 )
108 grid1
.Add( self
.set_min
, 0, wx
.ALIGN_LEFT|wx
.ALIGN_CENTER_VERTICAL|wx
.ALL
, 5 )
109 grid1
.Add( self
.min, 0, wx
.ALIGN_LEFT|wx
.ALL
, 5 )
110 grid1
.Add( (5,5), 0, wx
.ALIGN_LEFT|wx
.ALL
, 5)
111 grid1
.Add( (5,5), 0, wx
.ALIGN_LEFT|wx
.ALL
, 5)
113 grid1
.Add( self
.set_max
, 0, wx
.ALIGN_LEFT|wx
.ALIGN_CENTER_VERTICAL|wx
.ALL
, 5 )
114 grid1
.Add( self
.max, 0, wx
.ALIGN_LEFT|wx
.ALL
, 5 )
115 grid1
.Add( (5,5), 0, wx
.ALIGN_LEFT|wx
.ALL
, 5)
116 grid1
.Add( (5,5), 0, wx
.ALIGN_LEFT|wx
.ALL
, 5)
119 grid1
.Add( self
.limit_target
, 0, wx
.ALIGN_LEFT|wx
.ALL
, 5 )
120 grid1
.Add( self
.allow_none
, 0, wx
.ALIGN_LEFT|wx
.ALL
, 5 )
121 hbox1
= wx
.BoxSizer( wx
.HORIZONTAL
)
122 hbox1
.Add( (17,5), 0, wx
.ALIGN_LEFT|wx
.ALL
, 5)
123 hbox1
.Add( self
.group_digits
, 0, wx
.ALIGN_LEFT|wx
.LEFT
, 5 )
124 grid1
.Add( hbox1
, 0, wx
.ALIGN_LEFT|wx
.ALL
, 5)
125 grid1
.Add( (5,5), 0, wx
.ALIGN_LEFT|wx
.ALL
, 5)
127 grid1
.Add( self
.allow_negative
, 0, wx
.ALIGN_LEFT|wx
.ALL
, 5 )
128 grid1
.Add( self
.use_parens
, 0, wx
.ALIGN_LEFT|wx
.ALL
, 5 )
129 hbox2
= wx
.BoxSizer( wx
.HORIZONTAL
)
130 hbox2
.Add( (17,5), 0, wx
.ALIGN_LEFT|wx
.ALL
, 5)
131 hbox2
.Add( self
.select_on_entry
, 0, wx
.ALIGN_LEFT|wx
.LEFT
, 5 )
132 grid1
.Add( hbox2
, 0, wx
.ALIGN_LEFT|wx
.ALL
, 5)
133 grid1
.Add( (5,5), 0, wx
.ALIGN_LEFT|wx
.ALL
, 5)
136 grid2
= wx
.FlexGridSizer( 0, 2, 0, 0 )
137 grid2
.Add( label
, 0, wx
.ALIGN_LEFT|wx
.ALIGN_CENTER_VERTICAL|wx
.ALL
, 5 )
138 grid2
.Add( self
.target_ctl
, 0, wx
.ALIGN_LEFT|wx
.ALL
, 5 )
139 grid2
.Add( (5,5), 0, wx
.ALIGN_LEFT|wx
.ALL
, 5)
140 grid2
.Add( (5,5), 0, wx
.ALIGN_LEFT|wx
.ALL
, 5)
141 grid2
.Add( label_numselect
, 0, wx
.ALIGN_LEFT|wx
.ALIGN_CENTER_VERTICAL|wx
.ALL
, 5 )
142 grid2
.Add( self
.numselect
, 0, wx
.ALIGN_LEFT|wx
.ALL
, 5 )
143 grid2
.Add( (5,5), 0, wx
.ALIGN_LEFT|wx
.ALL
, 5)
144 grid2
.Add( (5,5), 0, wx
.ALIGN_LEFT|wx
.ALL
, 5)
145 grid2
.AddGrowableCol(1)
147 self
.outer_box
= wx
.BoxSizer( wx
.VERTICAL
)
148 self
.outer_box
.Add(header
, 0, wx
.ALIGN_LEFT|wx
.TOP|wx
.LEFT
, 20)
149 self
.outer_box
.Add( grid1
, 0, wx
.ALIGN_CENTRE|wx
.LEFT|wx
.BOTTOM|wx
.RIGHT
, 20 )
150 self
.outer_box
.Add( grid2
, 0, wx
.ALIGN_LEFT|wx
.ALL
, 20 )
153 panel
.SetAutoLayout( True )
154 panel
.SetSizer( self
.outer_box
)
155 self
.outer_box
.Fit( panel
)
156 panel
.Move( (50,10) )
159 self
.Bind(mnum
.EVT_MASKEDNUM
, self
.OnSetIntWidth
, self
.integerwidth
)
160 self
.Bind(mnum
.EVT_MASKEDNUM
, self
.OnSetFractionWidth
, self
.fractionwidth
)
161 self
.Bind(wx
.EVT_TEXT
, self
.OnSetGroupChar
, self
.groupchar
)
162 self
.Bind(wx
.EVT_TEXT
, self
.OnSetDecimalChar
, self
.decimalchar
)
164 self
.Bind(wx
.EVT_CHECKBOX
, self
.OnSetMin
, self
.set_min
)
165 self
.Bind(wx
.EVT_CHECKBOX
, self
.OnSetMax
, self
.set_max
)
166 self
.Bind(mnum
.EVT_MASKEDNUM
, self
.SetTargetMinMax
, self
.min )
167 self
.Bind(mnum
.EVT_MASKEDNUM
, self
.SetTargetMinMax
, self
.max )
169 self
.Bind(wx
.EVT_CHECKBOX
, self
.SetTargetMinMax
, self
.limit_target
)
170 self
.Bind(wx
.EVT_CHECKBOX
, self
.OnSetAllowNone
, self
.allow_none
)
171 self
.Bind(wx
.EVT_CHECKBOX
, self
.OnSetGroupDigits
, self
.group_digits
)
172 self
.Bind(wx
.EVT_CHECKBOX
, self
.OnSetAllowNegative
, self
.allow_negative
)
173 self
.Bind(wx
.EVT_CHECKBOX
, self
.OnSetUseParens
, self
.use_parens
)
174 self
.Bind(wx
.EVT_CHECKBOX
, self
.OnSetSelectOnEntry
, self
.select_on_entry
)
176 self
.Bind(mnum
.EVT_MASKEDNUM
, self
.OnTargetChange
, self
.target_ctl
)
177 self
.Bind(wx
.EVT_COMBOBOX
, self
.OnNumberSelect
, self
.numselect
)
180 def OnSetIntWidth(self
, event
):
181 width
= self
.integerwidth
.GetValue()
184 self
.log
.write("integer width must be positive\n")
185 self
.integerwidth
.SetForegroundColour(wx
.RED
)
187 self
.integerwidth
.SetForegroundColour(wx
.BLACK
)
188 self
.log
.write("setting integer width to %d\n" % width
)
189 self
.target_ctl
.SetParameters( integerWidth
= width
)
190 # Now resize and fit the dialog as appropriate:
191 self
.grid2
.SetItemMinSize(self
.target_ctl
, self
.target_ctl
.GetSize())
192 self
.outer_box
.Fit( self
.panel
)
193 self
.outer_box
.SetSizeHints( self
.panel
)
196 def OnSetFractionWidth(self
, event
):
197 width
= self
.fractionwidth
.GetValue()
198 self
.log
.write("setting fraction width to %d\n" % width
)
199 self
.target_ctl
.SetParameters( fractionWidth
= width
)
200 # Now resize and fit the dialog as appropriate:
201 self
.grid2
.SetItemMinSize(self
.target_ctl
, self
.target_ctl
.GetSize())
202 self
.outer_box
.Fit( self
.panel
)
203 self
.outer_box
.SetSizeHints( self
.panel
)
206 def OnSetGroupChar( self
, event
):
207 char
= self
.groupchar
.GetValue()
208 if self
.target_ctl
.GetDecimalChar() == char
:
209 self
.log
.write("group and decimal chars must be different\n")
210 self
.groupchar
.SetForegroundColour(wx
.RED
)
212 self
.groupchar
.SetForegroundColour(wx
.BLACK
)
213 self
.log
.write("setting group char to %s\n" % char
)
214 self
.target_ctl
.SetGroupChar( char
)
216 def OnSetDecimalChar( self
, event
):
217 char
= self
.decimalchar
.GetValue()
218 if self
.target_ctl
.GetGroupChar() == char
:
219 self
.log
.write("group and decimal chars must be different\n")
220 self
.decimalchar
.SetForegroundColour(wx
.RED
)
222 self
.decimalchar
.SetForegroundColour(wx
.BLACK
)
223 self
.log
.write("setting decimal char to %s\n" % char
)
224 self
.target_ctl
.SetDecimalChar( char
)
227 def OnSetMin( self
, event
):
228 self
.min.Enable( self
.set_min
.GetValue() )
229 self
.SetTargetMinMax()
231 def OnSetMax( self
, event
):
232 self
.max.Enable( self
.set_max
.GetValue() )
233 self
.SetTargetMinMax()
236 def SetTargetMinMax( self
, event
=None ):
238 self
.target_ctl
.SetLimited( self
.limit_target
.GetValue() )
240 if self
.set_min
.GetValue():
241 min = self
.min.GetValue()
242 if self
.set_max
.GetValue():
243 max = self
.max.GetValue()
245 cur_min
, cur_max
= self
.target_ctl
.GetBounds()
247 if min != cur_min
and not self
.target_ctl
.SetMin( min ):
248 if self
.target_ctl
.GetMax() is None and cur_max
> min:
249 self
.log
.write( "min (%d) won't fit in control -- bound not set\n" % min )
251 self
.log
.write( "min (%d) > current max (%d) -- bound not set\n" % ( min, self
.target_ctl
.GetMax() ) )
252 self
.min.SetParameters( signedForegroundColour
=wx
.RED
, foregroundColour
=wx
.RED
)
254 self
.min.SetParameters( signedForegroundColour
=wx
.BLACK
, foregroundColour
=wx
.BLACK
)
257 if max != cur_max
and not self
.target_ctl
.SetMax( max ):
258 if self
.target_ctl
.GetMax() is None and cur_min
< max:
259 self
.log
.write( "max (%d) won't fit in control -- bound not set\n" % max )
261 self
.log
.write( "max (%d) < current min (%d) -- bound not set\n" % ( max, self
.target_ctl
.GetMin() ) )
262 self
.max.SetParameters( signedForegroundColour
=wx
.RED
, foregroundColour
=wx
.RED
)
264 self
.max.SetParameters( signedForegroundColour
=wx
.BLACK
, foregroundColour
=wx
.BLACK
)
267 if min != cur_min
or max != cur_max
:
268 new_min
, new_max
= self
.target_ctl
.GetBounds()
269 self
.log
.write( "current min, max: (%s, %s)\n" % ( str(new_min
), str(new_max
) ) )
272 def OnSetAllowNone( self
, event
):
273 self
.target_ctl
.SetAllowNone( self
.allow_none
.GetValue() )
276 def OnSetGroupDigits( self
, event
):
277 self
.target_ctl
.SetGroupDigits( self
.group_digits
.GetValue() )
278 # Now resize and fit the dialog as appropriate:
279 self
.grid2
.SetItemMinSize(self
.target_ctl
, self
.target_ctl
.GetSize())
280 self
.outer_box
.Fit( self
.panel
)
281 self
.outer_box
.SetSizeHints( self
.panel
)
284 def OnSetAllowNegative( self
, event
):
285 if self
.allow_negative
.GetValue():
286 self
.use_parens
.Enable(True)
287 self
.target_ctl
.SetParameters(allowNegative
=True,
288 useParensForNegatives
= self
.use_parens
.GetValue())
290 self
.target_ctl
.SetAllowNegative(False)
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
)
297 def OnSetUseParens( self
, event
):
298 self
.target_ctl
.SetUseParensForNegatives( self
.use_parens
.GetValue() )
299 # Now resize and fit the dialog as appropriate:
300 self
.grid2
.SetItemMinSize(self
.target_ctl
, self
.target_ctl
.GetSize())
301 self
.outer_box
.Fit( self
.panel
)
302 self
.outer_box
.SetSizeHints( self
.panel
)
305 def OnSetSelectOnEntry( self
, event
):
306 self
.target_ctl
.SetSelectOnEntry( self
.select_on_entry
.GetValue() )
309 def OnTargetChange( self
, event
):
310 ctl
= event
.GetEventObject()
311 value
= ctl
.GetValue()
312 ib_str
= [ " (out of bounds)", "" ]
313 self
.log
.write( "value = %s (%s)%s\n" % ( repr(value
), repr(type(value
)), ib_str
[ ctl
.IsInBounds(value
) ] ) )
316 def OnNumberSelect( self
, event
):
317 value
= event
.GetString()
319 if value
.find('.') != -1:
320 numvalue
= float(value
)
322 numvalue
= long(value
)
324 numvalue
= value
# try to clear the value again
327 self
.target_ctl
.SetValue(numvalue
)
329 type, value
, tb
= sys
.exc_info()
330 for line
in traceback
.format_exception_only(type, value
):
334 #----------------------------------------------------------------------
336 def runTest( frame
, nb
, log
):
337 win
= TestPanel( nb
, log
)
340 #----------------------------------------------------------------------
341 overview
= mnum
.__doc
__
343 if __name__
== '__main__':
346 run
.main(['', os
.path
.basename(sys
.argv
[0])])