| 56 | |
| 57 | |
| 58 | class FloatBox(wx.TextCtrl): |
| 59 | |
| 60 | def __init__(self, parent, value, id=wx.ID_ANY, style=0, validator=None, **kwargs): |
| 61 | # Workaround for #2591 |
| 62 | if 'wxMac' in wx.PlatformInfo and 'size' not in kwargs: |
| 63 | kwargs['size'] = wx.Size(97, 26) |
| 64 | super().__init__(parent=parent, id=id, style=style, **kwargs) |
| 65 | self.Bind(wx.EVT_TEXT, self.OnText) |
| 66 | self._storedValue = '' |
| 67 | self._validator = validator |
| 68 | self.ChangeValue(valToStr(value)) |
| 69 | |
| 70 | def ChangeValue(self, value): |
| 71 | self._storedValue = value |
| 72 | super().ChangeValue(value) |
| 73 | self.updateColor() |
| 74 | |
| 75 | def ChangeValueFloat(self, value): |
| 76 | self.ChangeValue(valToStr(value)) |
| 77 | |
| 78 | def updateColor(self): |
| 79 | if self.isValid(): |
| 80 | self.SetForegroundColour(wx.SystemSettings.GetColour(wx.SYS_COLOUR_WINDOWTEXT)) |
| 81 | else: |
| 82 | self.SetForegroundColour(wx.RED) |
| 83 | |
| 84 | def isValid(self): |
| 85 | if self._validator is None: |
| 86 | return True |
| 87 | return self._validator.validate(self.GetValue()) |
| 88 | |
| 89 | def getInvalidationReason(self): |
| 90 | if self._validator is None: |
| 91 | return None |
| 92 | return self._validator.getReason(self.GetValue()) |
| 93 | |
| 94 | def OnText(self, event): |
| 95 | currentValue = self.GetValue() |
| 96 | if currentValue == self._storedValue: |
| 97 | event.Skip() |
| 98 | return |
| 99 | if currentValue == '' or re.match(r'^\d*\.?\d*$', currentValue): |
| 100 | self._storedValue = currentValue |
| 101 | self.updateColor() |
| 102 | event.Skip() |
| 103 | else: |
| 104 | self.ChangeValue(self._storedValue) |
| 105 | |
| 106 | def GetValueFloat(self): |
| 107 | return strToFloat(self.GetValue()) |
| 108 | |
| 109 | |
| 110 | class FloatRangeBox(wx.TextCtrl): |
no outgoing calls
no test coverage detected