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