def __OnChar(self, event): """ Handler to explicitly look for ':' keyevents, and if found, clear the m_shiftDown field, so it will behave as forward tab. It then calls the base control's _OnChar routine with the modified event instance. """ keycode = event.GetKeyCode() if keycode == ord(':'): event.m_shiftDown = False BaseMaskedTextCtrl._OnChar(self, event) ## handle each keypress
def __init__(self, parent, id = -1, value = '00D:00H:00M', pos = wx.DefaultPosition, size = wx.DefaultSize, style = wx.TE_PROCESS_TAB): # Initialize the base control BaseMaskedTextCtrl.__init__(self, parent, id = id, pos = pos, size = size, style = style, validator = wx.DefaultValidator, name = "HMTimeCtrl", setupEventHandling = False) # ToolTip self.SetToolTipString(_("Days:Hours:Minutes")) # Create the spin button self.__spinButton = wx.SpinButton(parent, -1, wx.DefaultPosition, (-1, self.GetSize()[1]), wx.SP_VERTICAL) # current position self.__posCurrent = 0 # hour field format daysfield = Field(formatcodes='0r<SV', validRegex='\d\d', mask = "##D", validRequired=True) hourfield = Field(formatcodes='0r<SV', validRegex='0\d|1\d|2[0123]', mask = "##H", validRequired=True) minutefield = Field(formatcodes='0r<SV', validRegex='[0-5]\d', mask = "##M", validRequired=True) fields = [daysfield, hourfield, minutefield] params = {} params['mask'] = "##D:##H:##M" params['formatcodes'] = 'F' params['fields'] = fields params['retainFieldValidation'] = True self.SetCtrlParameters(**params) self.SetMaskParameters(validRequired = False) # This makes ':' act like tab (after we fix each ':' key event to remove "shift") self._SetKeyHandler(':', self._OnChangeField) # This makes the up/down keys act like spin button controls: self._SetKeycodeHandler(wx.WXK_UP, self.__OnSpinUp) self._SetKeycodeHandler(wx.WXK_DOWN, self.__OnSpinDown) # Events Table self.__spinButton.Bind(wx.EVT_SPIN_UP, self.__OnSpinUp, self.__spinButton) self.__spinButton.Bind(wx.EVT_SPIN_DOWN, self.__OnSpinDown, self.__spinButton) self.Bind(wx.EVT_SET_FOCUS, self._OnFocus ) ## defeat automatic full selection self.Bind(wx.EVT_KILL_FOCUS, self._OnKillFocus ) ## run internal validator self.Bind(wx.EVT_LEFT_UP, self.__LimitSelection) ## limit selections to single field self.Bind(wx.EVT_LEFT_DCLICK, self._OnDoubleClick ) ## select field under cursor on dclick self.Bind(wx.EVT_KEY_DOWN, self._OnKeyDown ) ## capture control events not normally seen, eg ctrl-tab. self.Bind(wx.EVT_CHAR, self.__OnChar ) ## remove "shift" attribute from colon key event, ## then call BaseMaskedTextCtrl._OnChar with ## the possibly modified event. # Set initial value self.SetValue(value)
def __init__( self, parent, id=-1, value='', pos=wx.DefaultPosition, size=wx.DefaultSize, style=wx.TE_PROCESS_TAB, validator=wx.DefaultValidator, name='IpAddrCtrl', setupEventHandling=True, ## setup event handling by default **kwargs): if not kwargs.has_key('mask'): kwargs['mask'] = mask = "###.###.###.###" if not kwargs.has_key('formatcodes'): kwargs['formatcodes'] = 'F_Sr<>' if not kwargs.has_key('validRegex'): kwargs[ 'validRegex'] = "( \d| \d\d|(1\d\d|2[0-4]\d|25[0-5]))(\.( \d| \d\d|(1\d\d|2[0-4]\d|25[0-5]))){3}" BaseMaskedTextCtrl.__init__(self, parent, id=id, value=value, pos=pos, size=size, style=style, validator=validator, name=name, setupEventHandling=setupEventHandling, **kwargs) # set up individual field parameters as well: field_params = {} field_params[ 'validRegex'] = "( | \d| \d |\d | \d\d|\d\d |\d \d|(1\d\d|2[0-4]\d|25[0-5]))" # require "valid" string; this prevents entry of any value > 255, but allows # intermediate constructions; overall control validation requires well-formatted value. field_params['formatcodes'] = 'V' if field_params: for i in self._field_indices: self.SetFieldParameters(i, **field_params) # This makes '.' act like tab: self._AddNavKey('.', handler=self.OnDot) self._AddNavKey('>', handler=self.OnDot) # for "shift-."
def __init__( self, parent, id=-1, value = '', pos = wx.DefaultPosition, size = wx.DefaultSize, style = wx.TE_PROCESS_TAB, validator = wx.DefaultValidator, name = 'IpAddrCtrl', setupEventHandling = True, ## setup event handling by default **kwargs): if not kwargs.has_key('mask'): kwargs['mask'] = mask = "###.###.###.###" if not kwargs.has_key('formatcodes'): kwargs['formatcodes'] = 'F_Sr<>' if not kwargs.has_key('validRegex'): kwargs['validRegex'] = "( \d| \d\d|(1\d\d|2[0-4]\d|25[0-5]))(\.( \d| \d\d|(1\d\d|2[0-4]\d|25[0-5]))){3}" BaseMaskedTextCtrl.__init__( self, parent, id=id, value = value, pos=pos, size=size, style = style, validator = validator, name = name, setupEventHandling = setupEventHandling, **kwargs) # set up individual field parameters as well: field_params = {} field_params['validRegex'] = "( | \d| \d |\d | \d\d|\d\d |\d \d|(1\d\d|2[0-4]\d|25[0-5]))" # require "valid" string; this prevents entry of any value > 255, but allows # intermediate constructions; overall control validation requires well-formatted value. field_params['formatcodes'] = 'V' if field_params: for i in self._field_indices: self.SetFieldParameters(i, **field_params) # This makes '.' act like tab: self._AddNavKey('.', handler=self.OnDot) self._AddNavKey('>', handler=self.OnDot) # for "shift-."
def SetValue(self, value): """ Takes a string value, validates it for a valid IP address, splits it into an array of 4 fields, justifies it appropriately, and inserts it into the control. Invalid values will raise a ValueError exception. :param string `value`: the IP address in the form '000.000.000.000' """ ## dbg('IpAddrCtrl::SetValue(%s)' % str(value), indent=1) if not isinstance(value, str): ## dbg(indent=0) raise ValueError('%s must be a string' % str(value)) bValid = True # assume True parts = value.split('.') if len(parts) != 4: bValid = False else: for i in range(4): part = parts[i] if not 0 <= len(part) <= 3: bValid = False break elif part.strip(): # non-empty part try: j = int(part) if not 0 <= j <= 255: bValid = False break else: parts[i] = '%3d' % j except Exception: bValid = False break else: # allow empty sections for SetValue (will result in "invalid" value, # but this may be useful for initializing the control: parts[i] = ' ' # convert empty field to 3-char length if not bValid: ## dbg(indent=0) raise ValueError( 'value (%s) must be a string of form n.n.n.n where n is empty or in range 0-255' % str(value)) else: ## dbg('parts:', parts) value = '.'.join(parts) BaseMaskedTextCtrl.SetValue(self, value)
def GetAddress(self): """ Returns the control value, with any spaces removed. """ value = BaseMaskedTextCtrl.GetValue(self) return value.replace(' ', '') # remove spaces from the value
def __init__( self, parent, id=-1, value="", pos=wx.DefaultPosition, size=wx.DefaultSize, style=wx.TE_PROCESS_TAB, validator=wx.DefaultValidator, name="IpAddrCtrl", setupEventHandling=True, **kwargs ): """ Default class constructor. :param Window `parent`: the window parent. Must not be ``None``; :param integer `id`: window identifier. A value of -1 indicates a default value; :param string `value`: value to be shown; :param `pos`: the control position. A value of (-1, -1) indicates a default position, chosen by either the windowing system or wxPython, depending on platform; :type `pos`: tuple or :class:`Point` :param `size`: the control size. A value of (-1, -1) indicates a default size, chosen by either the windowing system or wxPython, depending on platform; :param integer `style`: the window style; :param Validator `validator`: this is mainly provided for data-transfer, as control does its own validation; :param string `name`: the window name; :param boolean `setupEventHandling`: setup event handling by default. """ if "mask" not in kwargs: kwargs["mask"] = mask = "###.###.###.###" if "formatcodes" not in kwargs: kwargs["formatcodes"] = "F_Sr<>" if "validRegex" not in kwargs: kwargs["validRegex"] = "( \d| \d\d|(1\d\d|2[0-4]\d|25[0-5]))(\.( \d| \d\d|(1\d\d|2[0-4]\d|25[0-5]))){3}" BaseMaskedTextCtrl.__init__( self, parent, id=id, value=value, pos=pos, size=size, style=style, validator=validator, name=name, setupEventHandling=setupEventHandling, **kwargs ) # set up individual field parameters as well: field_params = {} field_params["validRegex"] = "( | \d| \d |\d | \d\d|\d\d |\d \d|(1\d\d|2[0-4]\d|25[0-5]))" # require "valid" string; this prevents entry of any value > 255, but allows # intermediate constructions; overall control validation requires well-formatted value. field_params["formatcodes"] = "V" if field_params: for i in self._field_indices: self.SetFieldParameters(i, **field_params) # This makes '.' act like tab: self._AddNavKey(".", handler=self.OnDot) self._AddNavKey(">", handler=self.OnDot) # for "shift-."
def __init__(self, parent, id=-1, value='', pos=wx.DefaultPosition, size=wx.DefaultSize, style=wx.TE_PROCESS_TAB, validator=wx.DefaultValidator, name='IpAddrCtrl', setupEventHandling=True, **kwargs): """ Default class constructor. :param wx.Window `parent`: the window parent. Must not be ``None``; :param integer `id`: window identifier. A value of -1 indicates a default value; :param string `value`: value to be shown; :param `pos`: the control position. A value of (-1, -1) indicates a default position, chosen by either the windowing system or wxPython, depending on platform; :type `pos`: tuple or :class:`wx.Point` :param `size`: the control size. A value of (-1, -1) indicates a default size, chosen by either the windowing system or wxPython, depending on platform; :param integer `style`: the window style; :param wx.Validator `validator`: this is mainly provided for data-transfer, as control does its own validation; :param string `name`: the window name; :param boolean `setupEventHandling`: setup event handling by default. """ if 'mask' not in kwargs: kwargs['mask'] = mask = "###.###.###.###" if 'formatcodes' not in kwargs: kwargs['formatcodes'] = 'F_Sr<>' if 'validRegex' not in kwargs: kwargs[ 'validRegex'] = r"( \d| \d\d|(1\d\d|2[0-4]\d|25[0-5]))(\.( \d| \d\d|(1\d\d|2[0-4]\d|25[0-5]))){3}" BaseMaskedTextCtrl.__init__(self, parent, id=id, value=value, pos=pos, size=size, style=style, validator=validator, name=name, setupEventHandling=setupEventHandling, **kwargs) # set up individual field parameters as well: field_params = {} field_params[ 'validRegex'] = r"( | \d| \d |\d | \d\d|\d\d |\d \d|(1\d\d|2[0-4]\d|25[0-5]))" # require "valid" string; this prevents entry of any value > 255, but allows # intermediate constructions; overall control validation requires well-formatted value. field_params['formatcodes'] = 'V' if field_params: for i in self._field_indices: self.SetFieldParameters(i, **field_params) # This makes '.' act like tab: self._AddNavKey('.', handler=self.OnDot) self._AddNavKey('>', handler=self.OnDot) # for "shift-."