|
1 #!/usr/bin/env python |
|
2 # -*- coding: utf-8 -*- |
|
3 |
|
4 # This file is part of Beremiz, a Integrated Development Environment for |
|
5 # programming IEC 61131-3 automates supporting plcopen standard and CanFestival. |
|
6 # |
|
7 # Copyright (C) 2017: Andrey Skvortsov |
|
8 # |
|
9 # See COPYING file for copyrights details. |
|
10 # |
|
11 # This program is free software; you can redistribute it and/or |
|
12 # modify it under the terms of the GNU General Public License |
|
13 # as published by the Free Software Foundation; either version 2 |
|
14 # of the License, or (at your option) any later version. |
|
15 # |
|
16 # This program is distributed in the hope that it will be useful, |
|
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
19 # GNU General Public License for more details. |
|
20 # |
|
21 # You should have received a copy of the GNU General Public License |
|
22 # along with this program; if not, write to the Free Software |
|
23 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
24 |
|
25 |
|
26 import wx |
|
27 from wx.lib.intctrl import IntCtrl |
|
28 |
|
29 |
|
30 class CustomIntUpdatedEvent(wx.PyCommandEvent): |
|
31 def __init__(self, id, value=0, object=None): |
|
32 wx.PyCommandEvent.__init__(self, CustomIntCtrl.wxEVT_COMMAND_CUSTOM_INT_UPDATED, id) |
|
33 |
|
34 self.__value = value |
|
35 self.SetEventObject(object) |
|
36 |
|
37 def GetValue(self): |
|
38 """Retrieve the value of the control at the time |
|
39 this event was generated.""" |
|
40 return self.__value |
|
41 |
|
42 |
|
43 class CustomIntCtrl(wx.lib.intctrl.IntCtrl): |
|
44 """ |
|
45 This class provides a control that takes and returns long as |
|
46 value, and provides bounds support and optional value limiting. |
|
47 |
|
48 It handles entering negative numbers more user-friendly than |
|
49 original wx.lib.intctrl.IntCtrl. |
|
50 |
|
51 It applies limits as focus is changed to other control and |
|
52 sends event afterwards to signal that editing is done. |
|
53 """ |
|
54 |
|
55 # Used to trap events indicating that the current |
|
56 # integer value of the control has been changed. |
|
57 wxEVT_COMMAND_CUSTOM_INT_UPDATED = wx.NewEventType() |
|
58 EVT_CUSTOM_INT = wx.PyEventBinder(wxEVT_COMMAND_CUSTOM_INT_UPDATED, 1) |
|
59 |
|
60 def __init__(self, *args, **kwargs): |
|
61 wx.lib.intctrl.IntCtrl.__init__(self, *args, **kwargs) |
|
62 self.Bind(wx.EVT_KILL_FOCUS, self.UpdateValue) |
|
63 self.SetLongAllowed(True) |
|
64 self.SetLimited(False) |
|
65 |
|
66 def GetValue(self): |
|
67 """ |
|
68 Returns integer (long) value of the control, |
|
69 but handles entering negative numbers |
|
70 """ |
|
71 s = wx.TextCtrl.GetValue(self) |
|
72 if s == '-': |
|
73 s = '' |
|
74 return self._fromGUI(s) |
|
75 |
|
76 def GetValueStr(self): |
|
77 """Returns string value of TextCtrl""" |
|
78 return wx.TextCtrl.GetValue(self) |
|
79 |
|
80 def UpdateValue(self, event): |
|
81 self.SetLimited(True) |
|
82 self.SetLimited(False) |
|
83 try: |
|
84 self.GetEventHandler().ProcessEvent( |
|
85 CustomIntUpdatedEvent(self.GetId(), self.GetValue(), self)) |
|
86 except ValueError: |
|
87 return |
|
88 event.Skip() |