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