]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/MaskedNumCtrl.py
Rename the grid selection mode flags
[wxWidgets.git] / wxPython / demo / MaskedNumCtrl.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 # 12/09/2003 - Jeff Grimmett (grimmtooth@softhome.net)
10 #
11 # o Issues with lib corrected.
12 #
13 # 12/20/2003 - Jeff Grimmett (grimmtooth@softhome.net)
14 #
15 # o wxMaskedNumCtrl -> MaskedNumCtrl
16 #
17
18 import string
19 import sys
20 import traceback
21
22 import wx
23 import wx.lib.maskednumctrl as mnum
24 #----------------------------------------------------------------------
25
26 class TestPanel( wx.Panel ):
27 def __init__( self, parent, log ):
28
29 wx.Panel.__init__( self, parent, -1 )
30 self.log = log
31 panel = wx.Panel( self, -1 )
32
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.
36 """)
37 header.SetForegroundColour( "Blue" )
38
39 intlabel = wx.StaticText( panel, -1, "Integer width:" )
40 self.integerwidth = mnum.MaskedNumCtrl(
41 panel, value=10, integerWidth=2, allowNegative=False
42 )
43
44 fraclabel = wx.StaticText( panel, -1, "Fraction width:" )
45 self.fractionwidth = mnum.MaskedNumCtrl(
46 panel, value=0, integerWidth=2, allowNegative=False
47 )
48
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
53 )
54
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
59 )
60
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 )
65
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 )
69
70
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 )
80
81 label = wx.StaticText( panel, -1, "Resulting numeric control:" )
82 font = label.GetFont()
83 font.SetWeight(wx.BOLD)
84 label.SetFont(font)
85
86 self.target_ctl = mnum.MaskedNumCtrl( panel, -1, name="target control" )
87
88 label_numselect = wx.StaticText( panel, -1, """\
89 Programmatically set the above
90 value entry ctrl:""")
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' ])
94
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 )
98
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 )
101
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 )
104
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 )
107
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)
112
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)
117
118
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)
126
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)
134
135
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)
146
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 )
151 self.grid2 = grid2
152
153 panel.SetAutoLayout( True )
154 panel.SetSizer( self.outer_box )
155 self.outer_box.Fit( panel )
156 panel.Move( (50,10) )
157 self.panel = panel
158
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 )
163
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 )
168
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 )
175
176 self.Bind(mnum.EVT_MASKEDNUM, self.OnTargetChange, self.target_ctl )
177 self.Bind(wx.EVT_COMBOBOX, self.OnNumberSelect, self.numselect )
178
179
180 def OnSetIntWidth(self, event ):
181 width = self.integerwidth.GetValue()
182
183 if width < 1:
184 self.log.write("integer width must be positive\n")
185 self.integerwidth.SetForegroundColour(wx.RED)
186 else:
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 )
194
195
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 )
204
205
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)
211 else:
212 self.groupchar.SetForegroundColour(wx.BLACK)
213 self.log.write("setting group char to %s\n" % char)
214 self.target_ctl.SetGroupChar( char )
215
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)
221 else:
222 self.decimalchar.SetForegroundColour(wx.BLACK)
223 self.log.write("setting decimal char to %s\n" % char)
224 self.target_ctl.SetDecimalChar( char )
225
226
227 def OnSetMin( self, event ):
228 self.min.Enable( self.set_min.GetValue() )
229 self.SetTargetMinMax()
230
231 def OnSetMax( self, event ):
232 self.max.Enable( self.set_max.GetValue() )
233 self.SetTargetMinMax()
234
235
236 def SetTargetMinMax( self, event=None ):
237 min = max = None
238 self.target_ctl.SetLimited( self.limit_target.GetValue() )
239
240 if self.set_min.GetValue():
241 min = self.min.GetValue()
242 if self.set_max.GetValue():
243 max = self.max.GetValue()
244
245 cur_min, cur_max = self.target_ctl.GetBounds()
246
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 )
250 else:
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 )
253 else:
254 self.min.SetParameters( signedForegroundColour=wx.BLACK, foregroundColour=wx.BLACK )
255 self.min.Refresh()
256
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 )
260 else:
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 )
263 else:
264 self.max.SetParameters( signedForegroundColour=wx.BLACK, foregroundColour=wx.BLACK )
265 self.max.Refresh()
266
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) ) )
270
271
272 def OnSetAllowNone( self, event ):
273 self.target_ctl.SetAllowNone( self.allow_none.GetValue() )
274
275
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 )
282
283
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())
289 else:
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 )
295
296
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 )
303
304
305 def OnSetSelectOnEntry( self, event ):
306 self.target_ctl.SetSelectOnEntry( self.select_on_entry.GetValue() )
307
308
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) ] ) )
314
315
316 def OnNumberSelect( self, event ):
317 value = event.GetString()
318 if value:
319 if value.find('.') != -1:
320 numvalue = float(value)
321 else:
322 numvalue = long(value)
323 else:
324 numvalue = value # try to clear the value again
325
326 try:
327 self.target_ctl.SetValue(numvalue)
328 except:
329 type, value, tb = sys.exc_info()
330 for line in traceback.format_exception_only(type, value):
331 self.log.write(line)
332
333
334 #----------------------------------------------------------------------
335
336 def runTest( frame, nb, log ):
337 win = TestPanel( nb, log )
338 return win
339
340 #----------------------------------------------------------------------
341 overview = mnum.__doc__
342
343 if __name__ == '__main__':
344 import sys,os
345 import run
346 run.main(['', os.path.basename(sys.argv[0])])