]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/MaskedEditControls.py
Added SF Patch#
[wxWidgets.git] / wxPython / demo / MaskedEditControls.py
1 # 11/23/2003 - Jeff Grimmett (grimmtooth@softhome.net)
2 #
3 # o Updated for wx namespace
4 #
5 # 11/26/2003 - Jeff Grimmett (grimmtooth@softhome.net)
6 #
7 # o the three libraries below all have not been hit by the
8 # wx renamer.
9 #
10 # 12/09/2003 - Jeff Grimmett (grimmtooth@softhome.net)
11 #
12 # o A few changes to correct my own mistakes earlier :-).
13 #
14 # 12/20/2003 - Jeff Grimmett (grimmtooth@softhome.net)
15 #
16 # o wxMaskedTextCtrl -> MaskedTextCtrl
17 # o wxMaskedComboBox -> MaskedComboBox
18 # o wxIpAddrCtrl -> IpAddrCtrl
19 # o wxMaskedNumCtrl -> MaskedNumCtrl
20 # o wxTimeCtrl -> TimeCtrl
21 # o wxScrolledPanel -> ScrolledPanel
22 #
23
24 import string
25 import sys
26 import traceback
27
28 import wx
29 import wx.lib.maskededit as med
30 import wx.lib.maskedctrl as mctl
31 import wx.lib.scrolledpanel as scroll
32
33
34 class demoMixin:
35 """
36 Centralized routines common to demo pages, to remove repetition.
37 """
38 def labelGeneralTable(self, sizer):
39 description = wx.StaticText( self, -1, "Description", )
40 mask = wx.StaticText( self, -1, "Mask Value" )
41 formatcode = wx.StaticText( self, -1, "Format" )
42 regex = wx.StaticText( self, -1, "Regexp Validator(opt.)" )
43 ctrl = wx.StaticText( self, -1, "MaskedTextCtrl" )
44
45 description.SetFont( wx.Font(9, wx.SWISS, wx.NORMAL, wx.BOLD))
46 mask.SetFont( wx.Font(9, wx.SWISS, wx.NORMAL, wx.BOLD))
47 formatcode.SetFont( wx.Font(9, wx.SWISS, wx.NORMAL, wx.BOLD) )
48 regex.SetFont( wx.Font(9, wx.SWISS, wx.NORMAL, wx.BOLD))
49 ctrl.SetFont( wx.Font(9, wx.SWISS, wx.NORMAL, wx.BOLD))
50
51 sizer.Add(description)
52 sizer.Add(mask)
53 sizer.Add(formatcode)
54 sizer.Add(regex)
55 sizer.Add(ctrl)
56
57
58 def layoutGeneralTable(self, controls, sizer):
59 for control in controls:
60 sizer.Add( wx.StaticText( self, -1, control[0]) )
61 sizer.Add( wx.StaticText( self, -1, control[1]) )
62 sizer.Add( wx.StaticText( self, -1, control[3]) )
63 sizer.Add( wx.StaticText( self, -1, control[4]) )
64
65 if control in controls:
66 newControl = med.MaskedTextCtrl( self, -1, "",
67 mask = control[1],
68 excludeChars = control[2],
69 formatcodes = control[3],
70 includeChars = "",
71 validRegex = control[4],
72 validRange = control[5],
73 choices = control[6],
74 choiceRequired = True,
75 defaultValue = control[7],
76 demo = True,
77 name = control[0])
78 self.editList.append(newControl)
79 sizer.Add(newControl)
80
81
82 def changeControlParams(self, event, parameter, checked_value, notchecked_value):
83 if event.IsChecked(): value = checked_value
84 else: value = notchecked_value
85
86 kwargs = {parameter: value}
87
88 for control in self.editList:
89 control.SetCtrlParameters(**kwargs)
90 control.Refresh()
91
92 self.Refresh()
93
94
95
96 #----------------------------------------------------------------------------
97 class demoPage1(scroll.ScrolledPanel, demoMixin):
98 def __init__(self, parent, log):
99 scroll.ScrolledPanel.__init__(self, parent, -1)
100 self.sizer = wx.BoxSizer( wx.VERTICAL )
101 self.editList = []
102
103 label = wx.StaticText( self, -1, """\
104 Here are some basic MaskedTextCtrls to give you an idea of what you can do
105 with this control. Note that all controls have been auto-sized by including 'F' in
106 the format codes.
107
108 Try entering nonsensical or partial values in validated fields to see what happens.
109 Note that the State and Last Name fields are list-limited (valid last names are:
110 Smith, Jones, Williams). Signs on numbers can be toggled with the minus key.
111 """)
112 label.SetForegroundColour( "Blue" )
113 header = wx.BoxSizer( wx.HORIZONTAL )
114 header.Add( label, 0, flag=wx.ALIGN_LEFT|wx.ALL, border = 5 )
115
116 highlight = wx.CheckBox( self, -1, "Highlight Empty" )
117 disallow = wx.CheckBox( self, -1, "Disallow Empty" )
118 showFill = wx.CheckBox( self, -1, "change fillChar" )
119
120 vbox = wx.BoxSizer( wx.VERTICAL )
121 vbox.Add( highlight, 0, wx.ALIGN_LEFT|wx.ALL, 5 )
122 vbox.Add( disallow, 0, wx.ALIGN_LEFT|wx.ALL, 5 )
123 vbox.Add( showFill, 0, wx.ALIGN_LEFT|wx.ALL, 5 )
124 header.Add((15, 0))
125 header.Add(vbox, 0, flag=wx.ALIGN_LEFT|wx.ALL, border=5 )
126
127 self.Bind(wx.EVT_CHECKBOX, self.onHighlightEmpty, id=highlight.GetId())
128 self.Bind(wx.EVT_CHECKBOX, self.onDisallowEmpty, id=disallow.GetId())
129 self.Bind(wx.EVT_CHECKBOX, self.onShowFill, id=showFill.GetId())
130
131 grid = wx.FlexGridSizer( 0, 5, vgap=10, hgap=10 )
132 self.labelGeneralTable(grid)
133
134 # The following list is of the controls for the demo. Feel free to play around with
135 # the options!
136 controls = [
137 #description mask excl format regexp range,list,initial
138 ("Phone No", "(###) ###-#### x:###", "", 'F^-', "^\(\d{3}\) \d{3}-\d{4}", '','',''),
139 ("Social Sec#", "###-##-####", "", 'F', "\d{3}-\d{2}-\d{4}", '','',''),
140 ("Full Name", "C{14}", "", 'F_', '^[A-Z][a-zA-Z]+ [A-Z][a-zA-Z]+', '','',''),
141 ("Last Name Only", "C{14}", "", 'F {list}', '^[A-Z][a-zA-Z]+', '',('Smith','Jones','Williams'),''),
142 ("Zip plus 4", "#{5}-#{4}", "", 'F', "\d{5}-(\s{4}|\d{4})", '','',''),
143 ("Customer No", "\CAA-###", "", 'F!', "C[A-Z]{2}-\d{3}", '','',''),
144 ("Invoice Total", "#{9}.##", "", 'F-_,', "", '','',''),
145 ("Integer", "#{9}", "", 'F-_', "", '','',''),
146 ]
147
148 self.layoutGeneralTable(controls, grid)
149 self.sizer.Add( header, 0, flag=wx.ALIGN_LEFT|wx.ALL, border=5 )
150 self.sizer.Add( grid, 0, flag= wx.ALIGN_LEFT|wx.LEFT, border=5 )
151 self.SetSizer(self.sizer)
152 self.SetupScrolling()
153 self.SetAutoLayout(1)
154
155
156 def onDisallowEmpty( self, event ):
157 """ Set emptyInvalid parameter on/off """
158 self.changeControlParams( event, "emptyInvalid", True, False )
159
160 def onHighlightEmpty( self, event ):
161 """ Highlight empty values"""
162 self.changeControlParams( event, "emptyBackgroundColour", "Blue", "White" )
163
164 def onShowFill( self, event ):
165 """ Set fillChar parameter to '?' or ' ' """
166 self.changeControlParams( event, "fillChar", '?', ' ' )
167
168
169 class demoPage2(scroll.ScrolledPanel, demoMixin):
170 def __init__( self, parent, log ):
171 self.log = log
172 scroll.ScrolledPanel.__init__( self, parent, -1 )
173 self.sizer = wx.BoxSizer( wx.VERTICAL )
174
175 label = wx.StaticText( self, -1, """\
176 All these controls have been created by passing a single parameter, the autoformat code,
177 and use the factory class wxMaskedCtrl with its default controlType.
178 The maskededit module contains an internal dictionary of types and formats (autoformats).
179 Many of these already do complicated validation; To see some examples, try
180 29 Feb 2002 vs. 2004 for the date formats, or email address validation.
181 """)
182
183 label.SetForegroundColour( "Blue" )
184 self.sizer.Add( label, 0, wx.ALIGN_LEFT|wx.ALL, 5 )
185
186 description = wx.StaticText( self, -1, "Description")
187 autofmt = wx.StaticText( self, -1, "AutoFormat Code")
188 ctrl = wx.StaticText( self, -1, "MaskedCtrl")
189
190 description.SetFont( wx.Font( 9, wx.SWISS, wx.NORMAL, wx.BOLD ) )
191 autofmt.SetFont( wx.Font( 9, wx.SWISS, wx.NORMAL, wx.BOLD ) )
192 ctrl.SetFont( wx.Font( 9, wx.SWISS, wx.NORMAL, wx.BOLD ) )
193
194 grid = wx.FlexGridSizer( 0, 3, vgap=10, hgap=5 )
195 grid.Add( description, 0, wx.ALIGN_LEFT )
196 grid.Add( autofmt, 0, wx.ALIGN_LEFT )
197 grid.Add( ctrl, 0, wx.ALIGN_LEFT )
198
199 for autoformat, desc in med.autoformats:
200 grid.Add( wx.StaticText( self, -1, desc), 0, wx.ALIGN_LEFT )
201 grid.Add( wx.StaticText( self, -1, autoformat), 0, wx.ALIGN_LEFT )
202 grid.Add( mctl.wxMaskedCtrl( self, -1, "",
203 autoformat = autoformat,
204 demo = True,
205 name = autoformat),
206 0, wx.ALIGN_LEFT )
207
208 self.sizer.Add( grid, 0, wx.ALIGN_LEFT|wx.ALL, border=5 )
209 self.SetSizer( self.sizer )
210 self.SetAutoLayout( 1 )
211 self.SetupScrolling()
212
213
214 class demoPage3(scroll.ScrolledPanel, demoMixin):
215 def __init__(self, parent, log):
216 self.log = log
217 scroll.ScrolledPanel.__init__(self, parent, -1)
218 self.sizer = wx.BoxSizer( wx.VERTICAL )
219 self.editList = []
220
221 label = wx.StaticText( self, -1, """\
222 Here MaskedTextCtrls that have default values. The states
223 control has a list of valid values, and the unsigned integer
224 has a legal range specified.
225 """)
226 label.SetForegroundColour( "Blue" )
227 requireValid = wx.CheckBox( self, -1, "Require Valid Value" )
228 self.Bind(wx.EVT_CHECKBOX, self.onRequireValid, id=requireValid.GetId())
229
230 header = wx.BoxSizer( wx.HORIZONTAL )
231 header.Add( label, 0, flag=wx.ALIGN_LEFT|wx.ALL, border = 5)
232 header.Add((75, 0))
233 header.Add( requireValid, 0, flag=wx.ALIGN_LEFT|wx.ALL, border=10 )
234
235 grid = wx.FlexGridSizer( 0, 5, vgap=10, hgap=10 )
236 self.labelGeneralTable( grid )
237
238 controls = [
239 #description mask excl format regexp range,list,initial
240 ("U.S. State (2 char)", "AA", "", 'F!_', "[A-Z]{2}", '',med.states, med.states[0]),
241 ("Integer (signed)", "#{6}", "", 'F-_', "", '','', ' 0 '),
242 ("Integer (unsigned)\n(1-399)","######", "", 'F_', "", (1,399),'', '1 '),
243 ("Float (signed)", "#{6}.#{9}", "", 'F-_R', "", '','', '000000.000000000'),
244 ("Date (MDY) + Time", "##/##/#### ##:##:## AM", 'BCDEFGHIJKLMNOQRSTUVWXYZ','DF!',"", '','', wx.DateTime_Now().Format("%m/%d/%Y %I:%M:%S %p")),
245 ]
246 self.layoutGeneralTable( controls, grid )
247
248 self.sizer.Add( header, 0, flag=wx.ALIGN_LEFT|wx.ALL, border=5 )
249 self.sizer.Add( grid, 0, flag=wx.ALIGN_LEFT|wx.ALL, border=5 )
250
251 self.SetSizer( self.sizer )
252 self.SetAutoLayout( 1 )
253 self.SetupScrolling()
254
255 def onRequireValid( self, event ):
256 """ Set validRequired parameter on/off """
257 self.changeControlParams( event, "validRequired", True, False )
258
259
260 class demoPage4(scroll.ScrolledPanel, demoMixin):
261 def __init__( self, parent, log ):
262 self.log = log
263 scroll.ScrolledPanel.__init__( self, parent, -1 )
264 self.sizer = wx.BoxSizer( wx.VERTICAL )
265
266 label = wx.StaticText( self, -1, """\
267 These controls have field-specific choice lists and allow autocompletion.
268
269 Down arrow or Page Down in an uncompleted field with an auto-completable field will attempt
270 to auto-complete a field if it has a choice list.
271 Page Down and Shift-Down arrow will also auto-complete, or cycle through the complete list.
272 Page Up and Shift-Up arrow will similarly cycle backwards through the list.
273 """)
274
275 label.SetForegroundColour( "Blue" )
276 self.sizer.Add( label, 0, wx.ALIGN_LEFT|wx.ALL, 5 )
277
278 description = wx.StaticText( self, -1, "Description" )
279 autofmt = wx.StaticText( self, -1, "AutoFormat Code" )
280 fields = wx.StaticText( self, -1, "Field Objects" )
281 ctrl = wx.StaticText( self, -1, "MaskedTextCtrl" )
282
283 description.SetFont( wx.Font( 9, wx.SWISS, wx.NORMAL, wx.BOLD ) )
284 autofmt.SetFont( wx.Font( 9, wx.SWISS, wx.NORMAL, wx.BOLD ) )
285 fields.SetFont( wx.Font( 9, wx.SWISS, wx.NORMAL, wx.BOLD ) )
286 ctrl.SetFont( wx.Font( 9, wx.SWISS, wx.NORMAL, wx.BOLD ) )
287
288 grid = wx.FlexGridSizer( 0, 4, vgap=10, hgap=10 )
289 grid.Add( description, 0, wx.ALIGN_LEFT )
290 grid.Add( autofmt, 0, wx.ALIGN_LEFT )
291 grid.Add( fields, 0, wx.ALIGN_LEFT )
292 grid.Add( ctrl, 0, wx.ALIGN_LEFT )
293
294 autoformat = "USPHONEFULLEXT"
295 fieldsDict = {0: med.Field(choices=["617","781","508","978","413"], choiceRequired=True)}
296 fieldsLabel = """\
297 {0: Field(choices=[
298 "617","781",
299 "508","978","413"],
300 choiceRequired=True)}"""
301 grid.Add( wx.StaticText( self, -1, "Restricted Area Code"), 0, wx.ALIGN_LEFT )
302 grid.Add( wx.StaticText( self, -1, autoformat), 0, wx.ALIGN_LEFT )
303 grid.Add( wx.StaticText( self, -1, fieldsLabel), 0, wx.ALIGN_LEFT )
304 grid.Add( med.MaskedTextCtrl( self, -1, "",
305 autoformat = autoformat,
306 fields = fieldsDict,
307 demo = True,
308 name = autoformat),
309 0, wx.ALIGN_LEFT )
310
311 autoformat = "EXPDATEMMYY"
312 fieldsDict = {1: med.Field(choices=["03", "04", "05"], choiceRequired=True)}
313 fieldsLabel = """\
314 {1: Field(choices=[
315 "03", "04", "05"],
316 choiceRequired=True)}"""
317 exp = med.MaskedTextCtrl( self, -1, "",
318 autoformat = autoformat,
319 fields = fieldsDict,
320 demo = True,
321 name = autoformat)
322
323 grid.Add( wx.StaticText( self, -1, "Restricted Expiration"), 0, wx.ALIGN_LEFT )
324 grid.Add( wx.StaticText( self, -1, autoformat), 0, wx.ALIGN_LEFT )
325 grid.Add( wx.StaticText( self, -1, fieldsLabel), 0, wx.ALIGN_LEFT )
326 grid.Add( exp, 0, wx.ALIGN_LEFT )
327
328 fieldsDict = {0: med.Field(choices=["02134","02155"], choiceRequired=True),
329 1: med.Field(choices=["1234", "5678"], choiceRequired=False)}
330 fieldsLabel = """\
331 {0: Field(choices=["02134","02155"],
332 choiceRequired=True),
333 1: Field(choices=["1234", "5678"],
334 choiceRequired=False)}"""
335 autoformat = "USZIPPLUS4"
336 zip = med.MaskedTextCtrl( self, -1, "",
337 autoformat = autoformat,
338 fields = fieldsDict,
339 demo = True,
340 name = autoformat)
341
342 grid.Add( wx.StaticText( self, -1, "Restricted Zip + 4"), 0, wx.ALIGN_LEFT )
343 grid.Add( wx.StaticText( self, -1, autoformat), 0, wx.ALIGN_LEFT )
344 grid.Add( wx.StaticText( self, -1, fieldsLabel), 0, wx.ALIGN_LEFT )
345 grid.Add( zip, 0, wx.ALIGN_LEFT )
346
347 self.sizer.Add( grid, 0, wx.ALIGN_LEFT|wx.ALL, border=5 )
348 self.SetSizer( self.sizer )
349 self.SetAutoLayout(1)
350 self.SetupScrolling()
351
352
353 class demoPage5(scroll.ScrolledPanel, demoMixin):
354 def __init__( self, parent, log ):
355 self.log = log
356 scroll.ScrolledPanel.__init__( self, parent, -1 )
357 self.sizer = wx.BoxSizer( wx.VERTICAL )
358
359
360 labelMaskedCombos = wx.StaticText( self, -1, """\
361 These are some examples of MaskedComboBox:""")
362 labelMaskedCombos.SetForegroundColour( "Blue" )
363
364
365 label_statecode = wx.StaticText( self, -1, """\
366 A state selector; only
367 "legal" values can be
368 entered:""")
369 statecode = med.MaskedComboBox( self, -1, med.states[0],
370 choices = med.states,
371 autoformat="USSTATE")
372
373 label_statename = wx.StaticText( self, -1, """\
374 A state name selector,
375 with auto-select:""")
376
377 # Create this one using factory function:
378 statename = mctl.wxMaskedCtrl( self, -1, med.state_names[0],
379 controlType = mctl.controlTypes.MASKEDCOMBO,
380 choices = med.state_names,
381 autoformat="USSTATENAME",
382 autoSelect=True)
383 statename.SetCtrlParameters(formatcodes = 'F!V_')
384
385
386 numerators = [ str(i) for i in range(1, 4) ]
387 denominators = [ string.ljust(str(i), 2) for i in [2,3,4,5,8,16,32,64] ]
388 fieldsDict = {0: med.Field(choices=numerators, choiceRequired=False),
389 1: med.Field(choices=denominators, choiceRequired=True)}
390 choices = []
391 for n in numerators:
392 for d in denominators:
393 if n != d:
394 choices.append( '%s/%s' % (n,d) )
395
396
397 label_fraction = wx.StaticText( self, -1, """\
398 A masked ComboBox for fraction selection.
399 Choices for each side of the fraction can
400 be selected with PageUp/Down:""")
401
402 fraction = mctl.wxMaskedCtrl( self, -1, "",
403 controlType = mctl.MASKEDCOMBO,
404 choices = choices,
405 choiceRequired = True,
406 mask = "#/##",
407 formatcodes = "F_",
408 validRegex = "^\d\/\d\d?",
409 fields = fieldsDict )
410
411
412 label_code = wx.StaticText( self, -1, """\
413 A masked ComboBox to validate
414 text from a list of numeric codes:""")
415
416 choices = ["91", "136", "305", "4579"]
417 code = med.MaskedComboBox( self, -1, choices[0],
418 choices = choices,
419 choiceRequired = True,
420 formatcodes = "F_r",
421 mask = "####")
422
423 label_selector = wx.StaticText( self, -1, """\
424 Programmatically set
425 choice sets:""")
426 self.list_selector = wx.ComboBox(self, -1, '', choices = ['list1', 'list2', 'list3'])
427 self.dynamicbox = mctl.wxMaskedCtrl( self, -1, ' ',
428 controlType = mctl.controlTypes.MASKEDCOMBO,
429 mask = 'XXXX',
430 formatcodes = 'F_',
431 # these are to give dropdown some initial height,
432 # as base control apparently only sets that size
433 # during initial construction <sigh>:
434 choices = ['', '1', '2', '3', '4', '5'] )
435
436 self.dynamicbox.Clear() # get rid of initial choices used to size the dropdown
437
438
439 labelIpAddrs = wx.StaticText( self, -1, """\
440 Here are some examples of IpAddrCtrl, a control derived from MaskedTextCtrl:""")
441 labelIpAddrs.SetForegroundColour( "Blue" )
442
443
444 label_ipaddr1 = wx.StaticText( self, -1, "An empty control:")
445 ipaddr1 = med.IpAddrCtrl( self, -1, style = wx.TE_PROCESS_TAB )
446
447
448 label_ipaddr2 = wx.StaticText( self, -1, "A restricted mask:")
449 ipaddr2 = med.IpAddrCtrl( self, -1, mask=" 10. 1.109.###" )
450
451
452 label_ipaddr3 = wx.StaticText( self, -1, """\
453 A control with restricted legal values:
454 10. (1|2) . (129..255) . (0..255)""")
455 ipaddr3 = mctl.wxMaskedCtrl( self, -1,
456 controlType = mctl.controlTypes.IPADDR,
457 mask=" 10. #.###.###")
458 ipaddr3.SetFieldParameters(0, validRegex="1|2",validRequired=False ) # requires entry to match or not allowed
459
460 # This allows any value in penultimate field, but colors anything outside of the range invalid:
461 ipaddr3.SetFieldParameters(1, validRange=(129,255), validRequired=False )
462
463
464
465 labelNumerics = wx.StaticText( self, -1, """\
466 Here are some useful configurations of a MaskedTextCtrl for integer and floating point input that still treat
467 the control as a text control. (For a true numeric control, check out the MaskedNumCtrl class!)""")
468 labelNumerics.SetForegroundColour( "Blue" )
469
470 label_intctrl1 = wx.StaticText( self, -1, """\
471 An integer entry control with
472 shifting insert enabled:""")
473 self.intctrl1 = med.MaskedTextCtrl(self, -1, name='intctrl', mask="#{9}", formatcodes = '_-,F>')
474 label_intctrl2 = wx.StaticText( self, -1, """\
475 Right-insert integer entry:""")
476 self.intctrl2 = med.MaskedTextCtrl(self, -1, name='intctrl', mask="#{9}", formatcodes = '_-,Fr')
477
478 label_floatctrl = wx.StaticText( self, -1, """\
479 A floating point entry control
480 with right-insert for ordinal:""")
481 self.floatctrl = med.MaskedTextCtrl(self, -1, name='floatctrl', mask="#{9}.#{2}", formatcodes="F,_-R", useParensForNegatives=False)
482 self.floatctrl.SetFieldParameters(0, formatcodes='r<', validRequired=True) # right-insert, require explicit cursor movement to change fields
483 self.floatctrl.SetFieldParameters(1, defaultValue='00') # don't allow blank fraction
484
485 label_numselect = wx.StaticText( self, -1, """\
486 <= Programmatically set the value
487 of the float entry ctrl:""")
488 numselect = wx.ComboBox(self, -1, choices = [ '', '111', '222.22', '-3', '54321.666666666', '-1353.978',
489 '1234567', '-1234567', '123456789', '-123456789.1',
490 '1234567890.', '-1234567890.1' ])
491
492 parens_check = wx.CheckBox(self, -1, "Use () to indicate negatives in above controls")
493
494
495
496 gridCombos = wx.FlexGridSizer( 0, 4, vgap=10, hgap = 10 )
497 gridCombos.Add( label_statecode, 0, wx.ALIGN_LEFT )
498 gridCombos.Add( statecode, 0, wx.ALIGN_LEFT )
499 gridCombos.Add( label_fraction, 0, wx.ALIGN_LEFT )
500 gridCombos.Add( fraction, 0, wx.ALIGN_LEFT )
501 gridCombos.Add( label_statename, 0, wx.ALIGN_LEFT )
502 gridCombos.Add( statename, 0, wx.ALIGN_LEFT )
503 gridCombos.Add( label_code, 0, wx.ALIGN_LEFT )
504 gridCombos.Add( code, 0, wx.ALIGN_LEFT )
505 gridCombos.Add( label_selector, 0, wx.ALIGN_LEFT)
506 hbox = wx.BoxSizer( wx.HORIZONTAL )
507 hbox.Add( self.list_selector, 0, wx.ALIGN_LEFT )
508 hbox.Add(wx.StaticText(self, -1, ' => '), 0, wx.ALIGN_LEFT)
509 hbox.Add( self.dynamicbox, 0, wx.ALIGN_LEFT )
510 gridCombos.Add( hbox, 0, wx.ALIGN_LEFT )
511
512 gridIpAddrs = wx.FlexGridSizer( 0, 4, vgap=10, hgap = 15 )
513 gridIpAddrs.Add( label_ipaddr1, 0, wx.ALIGN_LEFT )
514 gridIpAddrs.Add( ipaddr1, 0, wx.ALIGN_LEFT )
515 gridIpAddrs.Add( label_ipaddr2, 0, wx.ALIGN_LEFT )
516 gridIpAddrs.Add( ipaddr2, 0, wx.ALIGN_LEFT )
517 gridIpAddrs.Add( label_ipaddr3, 0, wx.ALIGN_LEFT )
518 gridIpAddrs.Add( ipaddr3, 0, wx.ALIGN_LEFT )
519
520 gridNumerics = wx.FlexGridSizer( 0, 4, vgap=10, hgap = 10 )
521 gridNumerics.Add( label_intctrl1, 0, wx.ALIGN_LEFT )
522 gridNumerics.Add( self.intctrl1, 0, wx.ALIGN_LEFT )
523 gridNumerics.Add( label_intctrl2, 0, wx.ALIGN_RIGHT )
524 gridNumerics.Add( self.intctrl2, 0, wx.ALIGN_LEFT )
525 gridNumerics.Add( label_floatctrl, 0, wx.ALIGN_LEFT )
526 gridNumerics.Add( self.floatctrl, 0, wx.ALIGN_LEFT )
527 gridNumerics.Add( label_numselect, 0, wx.ALIGN_RIGHT )
528 gridNumerics.Add( numselect, 0, wx.ALIGN_LEFT )
529
530 self.sizer.Add( labelMaskedCombos, 0, wx.ALIGN_LEFT|wx.ALL, 5 )
531 self.sizer.Add( gridCombos, 0, wx.ALIGN_LEFT|wx.ALL, border=5 )
532 self.sizer.Add( wx.StaticLine(self, -1), 0, wx.EXPAND|wx.TOP|wx.BOTTOM, border=8 )
533 self.sizer.Add( labelIpAddrs, 0, wx.ALIGN_LEFT|wx.ALL, 5 )
534 self.sizer.Add( gridIpAddrs, 0, wx.ALIGN_LEFT|wx.ALL, border=5 )
535 self.sizer.Add( wx.StaticLine(self, -1), 0, wx.EXPAND|wx.TOP|wx.BOTTOM, border=8 )
536 self.sizer.Add( labelNumerics, 0, wx.ALIGN_LEFT|wx.ALL, 5 )
537 self.sizer.Add( gridNumerics, 0, wx.ALIGN_LEFT|wx.ALL, border=5 )
538 self.sizer.Add( parens_check, 0, wx.ALIGN_LEFT|wx.ALL, 5 )
539
540 self.SetSizer( self.sizer )
541 self.SetAutoLayout(1)
542 self.SetupScrolling()
543
544 self.Bind(wx.EVT_COMBOBOX, self.OnComboSelection, id=fraction.GetId())
545 self.Bind(wx.EVT_COMBOBOX, self.OnComboSelection, id=code.GetId())
546 self.Bind(wx.EVT_COMBOBOX, self.OnComboSelection, id=statecode.GetId())
547 self.Bind(wx.EVT_COMBOBOX, self.OnComboSelection, id=statename.GetId())
548 self.Bind(wx.EVT_TEXT, self.OnTextChange, id=code.GetId())
549 self.Bind(wx.EVT_TEXT, self.OnTextChange, id=statecode.GetId())
550 self.Bind(wx.EVT_TEXT, self.OnTextChange, id=statename.GetId())
551 self.Bind(wx.EVT_COMBOBOX, self.OnListSelection, id=self.list_selector.GetId())
552
553 self.Bind(wx.EVT_TEXT, self.OnTextChange, id=self.intctrl1.GetId())
554 self.Bind(wx.EVT_TEXT, self.OnTextChange, id=self.intctrl2.GetId())
555 self.Bind(wx.EVT_TEXT, self.OnTextChange, id=self.floatctrl.GetId())
556 self.Bind(wx.EVT_COMBOBOX, self.OnNumberSelect, id=numselect.GetId())
557 self.Bind(wx.EVT_CHECKBOX, self.OnParensCheck, id=parens_check.GetId())
558
559 self.Bind(wx.EVT_TEXT, self.OnIpAddrChange, id=ipaddr1.GetId())
560 self.Bind(wx.EVT_TEXT, self.OnIpAddrChange, id=ipaddr2.GetId())
561 self.Bind(wx.EVT_TEXT, self.OnIpAddrChange, id=ipaddr3.GetId())
562
563
564
565
566 def OnComboSelection( self, event ):
567 ctl = self.FindWindowById( event.GetId() )
568 if not ctl.IsValid():
569 self.log.write('current value not a valid choice')
570 self.log.write('new value = %s' % ctl.GetValue())
571
572 def OnTextChange( self, event ):
573 ctl = self.FindWindowById( event.GetId() )
574 if ctl.IsValid():
575 self.log.write('new value = %s\n' % ctl.GetValue() )
576
577 def OnNumberSelect( self, event ):
578 value = event.GetString()
579 # Format choice to fit into format for #{9}.#{2}, with sign position reserved:
580 # (ordinal + fraction == 11 + decimal point + sign == 13)
581 if value:
582 floattext = "%13.2f" % float(value)
583 else:
584 floattext = value # clear the value again
585 try:
586 self.floatctrl.SetValue(floattext)
587 except:
588 type, value, tb = sys.exc_info()
589 for line in traceback.format_exception_only(type, value):
590 self.log.write(line)
591
592 def OnParensCheck( self, event ):
593 self.intctrl1.SetCtrlParameters(useParensForNegatives=event.IsChecked())
594 self.intctrl2.SetCtrlParameters(useParensForNegatives=event.IsChecked())
595 self.floatctrl.SetCtrlParameters(useParensForNegatives=event.IsChecked())
596
597 def OnIpAddrChange( self, event ):
598 ipaddr = self.FindWindowById( event.GetId() )
599 if ipaddr.IsValid():
600 self.log.write('new addr = %s\n' % ipaddr.GetAddress() )
601
602 def OnListSelection( self, event ):
603 list = self.list_selector.GetStringSelection()
604 formatcodes = 'F_'
605 if list == 'list1':
606 choices = ['abc', 'defg', 'hi']
607 mask = 'aaaa'
608 elif list == 'list2':
609 choices = ['1', '2', '34', '567']
610 formatcodes += 'r'
611 mask = '###'
612 else:
613 choices = med.states
614 mask = 'AA'
615 formatcodes += '!'
616 self.dynamicbox.SetCtrlParameters( mask = mask,
617 choices = choices,
618 choiceRequired=True,
619 autoSelect=True,
620 formatcodes=formatcodes)
621 self.dynamicbox.SetValue(choices[0])
622
623 # ---------------------------------------------------------------------
624 class TestMaskedTextCtrls(wx.Notebook):
625 def __init__(self, parent, id, log):
626 wx.Notebook.__init__(self, parent, id)
627 self.log = log
628
629 win = demoPage1(self, log)
630 self.AddPage(win, "General examples")
631
632 win = demoPage2(self, log)
633 self.AddPage(win, 'Auto-formatted controls')
634
635 win = demoPage3(self, log)
636 self.AddPage(win, "Using default values")
637
638 win = demoPage4(self, log)
639 self.AddPage(win, 'Using auto-complete fields')
640
641 win = demoPage5(self, log)
642 self.AddPage(win, 'Other masked controls')
643
644
645 #----------------------------------------------------------------------------
646
647 def runTest(frame, nb, log):
648 testWin = TestMaskedTextCtrls(nb, -1, log)
649 return testWin
650
651 def RunStandalone():
652 app = wx.PySimpleApp()
653 frame = wx.Frame(None, -1, "Test MaskedTextCtrl", size=(640, 480))
654 win = TestMaskedTextCtrls(frame, -1, sys.stdout)
655 frame.Show(True)
656 app.MainLoop()
657 #----------------------------------------------------------------------------
658
659 overview = """<html>
660 <PRE><FONT SIZE=-1>
661 """ + med.__doc__ + """
662 </FONT></PRE>
663 """
664
665 if __name__ == "__main__":
666 import sys,os
667 import run
668 run.main(['', os.path.basename(sys.argv[0])])