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