andrej@1811: #!/usr/bin/env python
andrej@1811: # -*- coding: utf-8 -*-
andrej@1811: 
andrej@1811: # This file is part of Beremiz, a Integrated Development Environment for
andrej@1811: # programming IEC 61131-3 automates supporting plcopen standard and CanFestival.
andrej@1811: #
andrej@1811: # Copyright (C) 2017: Andrey Skvortsov
andrej@1811: #
andrej@1811: # See COPYING file for copyrights details.
andrej@1811: #
andrej@1811: # This program is free software; you can redistribute it and/or
andrej@1811: # modify it under the terms of the GNU General Public License
andrej@1811: # as published by the Free Software Foundation; either version 2
andrej@1811: # of the License, or (at your option) any later version.
andrej@1811: #
andrej@1811: # This program is distributed in the hope that it will be useful,
andrej@1811: # but WITHOUT ANY WARRANTY; without even the implied warranty of
andrej@1811: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
andrej@1811: # GNU General Public License for more details.
andrej@1811: #
andrej@1811: # You should have received a copy of the GNU General Public License
andrej@1811: # along with this program; if not, write to the Free Software
andrej@1811: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
andrej@1811: 
andrej@1811: 
andrej@1881: from __future__ import absolute_import
andrej@1811: import wx
andrej@1850: import wx.lib.intctrl
andrej@1811: 
andrej@1811: 
andrej@1811: class CustomIntUpdatedEvent(wx.PyCommandEvent):
andrej@1811:     def __init__(self, id, value=0, object=None):
andrej@1811:         wx.PyCommandEvent.__init__(self, CustomIntCtrl.wxEVT_COMMAND_CUSTOM_INT_UPDATED, id)
andrej@1811: 
andrej@1811:         self.__value = value
andrej@1811:         self.SetEventObject(object)
andrej@1811: 
andrej@1811:     def GetValue(self):
andrej@1811:         """Retrieve the value of the control at the time
andrej@1811:         this event was generated."""
andrej@1811:         return self.__value
andrej@1811: 
andrej@1811: 
andrej@1811: class CustomIntCtrl(wx.lib.intctrl.IntCtrl):
andrej@1811:     """
andrej@1811:     This class provides a control that takes and returns long as
andrej@1811:     value, and provides bounds support and optional value limiting.
andrej@1811: 
andrej@1811:     It handles entering negative numbers more user-friendly than
andrej@1811:     original wx.lib.intctrl.IntCtrl.
andrej@1811: 
andrej@1811:     It applies limits as focus is changed to other control and
andrej@1811:     sends event afterwards to signal that editing is done.
andrej@1811:     """
andrej@1811: 
andrej@1811:     # Used to trap events indicating that the current
andrej@1811:     # integer value of the control has been changed.
andrej@1811:     wxEVT_COMMAND_CUSTOM_INT_UPDATED = wx.NewEventType()
andrej@1811:     EVT_CUSTOM_INT = wx.PyEventBinder(wxEVT_COMMAND_CUSTOM_INT_UPDATED, 1)
andrej@1811: 
andrej@1811:     def __init__(self, *args, **kwargs):
andrej@1811:         wx.lib.intctrl.IntCtrl.__init__(self, *args, **kwargs)
andrej@1811:         self.Bind(wx.EVT_KILL_FOCUS, self.UpdateValue)
andrej@1811:         self.SetLongAllowed(True)
andrej@1811:         self.SetLimited(False)
andrej@1811: 
andrej@1811:     def GetValue(self):
andrej@1811:         """
andrej@1811:         Returns integer (long) value of the control,
andrej@1811:         but handles entering negative numbers
andrej@1811:         """
andrej@1811:         s = wx.TextCtrl.GetValue(self)
andrej@1811:         if s == '-':
andrej@1811:             s = ''
andrej@1811:         return self._fromGUI(s)
andrej@1811: 
andrej@1811:     def GetValueStr(self):
andrej@1811:         """Returns string value of TextCtrl"""
andrej@1811:         return wx.TextCtrl.GetValue(self)
andrej@1811: 
andrej@1811:     def UpdateValue(self, event):
andrej@1811:         self.SetLimited(True)
andrej@1811:         self.SetLimited(False)
andrej@1811:         try:
andrej@1811:             self.GetEventHandler().ProcessEvent(
andrej@1811:                 CustomIntUpdatedEvent(self.GetId(), self.GetValue(), self))
andrej@1811:         except ValueError:
andrej@1811:             return
andrej@1811:         event.Skip()