controls/CustomIntCtrl.py
author Andrey Skvortsov <andrej.skvortzov@gmail.com>
Fri, 15 Sep 2017 20:01:21 +0300
changeset 1811 4e3c78a84c64
child 1850 614396cbffbf
permissions -rw-r--r--
add custom TextCtrl allowed to enter long integer with bounds checking

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