Package Gnumed :: Package timelinelib :: Package wxgui :: Package components :: Module numctrl
[frames] | no frames]

Source Code for Module Gnumed.timelinelib.wxgui.components.numctrl

 1  # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018  Rickard Lindberg, Roger Lindberg 
 2  # 
 3  # This file is part of Timeline. 
 4  # 
 5  # Timeline is free software: you can redistribute it and/or modify 
 6  # it under the terms of the GNU General Public License as published by 
 7  # the Free Software Foundation, either version 3 of the License, or 
 8  # (at your option) any later version. 
 9  # 
10  # Timeline is distributed in the hope that it will be useful, 
11  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
12  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
13  # GNU General Public License for more details. 
14  # 
15  # You should have received a copy of the GNU General Public License 
16  # along with Timeline.  If not, see <http://www.gnu.org/licenses/>. 
17   
18   
19  import wx 
20   
21   
22 -class NumCtrl(wx.Panel):
23
24 - def __init__(self, parent, size=(-1, -1)):
25 wx.Panel.__init__(self, parent) 26 self._CreateGui(size)
27
28 - def _CreateGui(self, size):
29 sizer = wx.BoxSizer(wx.HORIZONTAL) 30 self.text = wx.TextCtrl(self, size=size) 31 self.spin = wx.SpinButton(self, style=wx.SP_VERTICAL, size=(-1, self.text.GetSize()[1] + 5)) 32 sizer.Add(self.text, proportion=1, flag=wx.ALIGN_CENTER_VERTICAL) 33 sizer.Add(self.spin, proportion=0, flag=wx.ALIGN_CENTER_VERTICAL) 34 self.Bind(wx.EVT_CHAR, self._OnKeyPress, self.text) 35 self.Bind(wx.EVT_SPIN_UP, self._OnSpinUp, self.spin) 36 self.Bind(wx.EVT_SPIN_DOWN, self._OnSpinDown, self.spin) 37 self.SetSizerAndFit(sizer)
38
39 - def SetValue(self, value):
40 self.text.SetValue(value)
41
42 - def GetValue(self):
43 return self.text.GetValue()
44
45 - def _OnKeyPress(self, event):
46 if self.ValidKeyCode(event.GetKeyCode()): 47 event.Skip()
48
49 - def _OnSpinUp(self, event):
50 self._Spin(1)
51
52 - def _OnSpinDown(self, event):
53 self._Spin(-1)
54
55 - def _Spin(self, value):
56 self.SetValue(str(int(self.GetValue()) + value))
57
58 - def ValidKeyCode(self, keycode):
59 """Allow digits, and a minus sign in the first position.""" 60 if IsAscci(keycode): 61 if IsMinus(keycode): 62 if self.MinusIsOk(): 63 return True 64 else: 65 return False 66 elif IsNotAlpha(keycode): 67 return True 68 else: 69 return False 70 return True
71
72 - def MinusIsOk(self):
73 return '-' not in self.GetValue() and self.GetInsertionPoint() == 0
74 75
76 -def IsNotAlpha(keycode):
77 return not chr(keycode).isalpha()
78 79
80 -def IsMinus(keycode):
81 return chr(keycode) == '-'
82 83
84 -def IsAscci(keycode):
85 return keycode < 255
86