controls/TextCtrlAutoComplete.py
author Edouard Tisserant <edouard.tisserant@gmail.com>
Tue, 03 Dec 2024 21:26:52 +0100
changeset 4058 5472b88bbc9d
parent 3758 bc71b19b45ff
permissions -rw-r--r--
C runtime: move generated erpc interface in separate "service" directory
428
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
     1
#!/usr/bin/env python
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
     2
# -*- coding: utf-8 -*-
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
     3
1571
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1498
diff changeset
     4
# This file is part of Beremiz, a Integrated Development Environment for
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1498
diff changeset
     5
# programming IEC 61131-3 automates supporting plcopen standard and CanFestival.
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1498
diff changeset
     6
#
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1498
diff changeset
     7
# Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1498
diff changeset
     8
#
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1498
diff changeset
     9
# See COPYING file for copyrights details.
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1498
diff changeset
    10
#
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1498
diff changeset
    11
# This program is free software; you can redistribute it and/or
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1498
diff changeset
    12
# modify it under the terms of the GNU General Public License
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1498
diff changeset
    13
# as published by the Free Software Foundation; either version 2
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1498
diff changeset
    14
# of the License, or (at your option) any later version.
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1498
diff changeset
    15
#
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1498
diff changeset
    16
# This program is distributed in the hope that it will be useful,
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1498
diff changeset
    17
# but WITHOUT ANY WARRANTY; without even the implied warranty of
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1498
diff changeset
    18
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1498
diff changeset
    19
# GNU General Public License for more details.
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1498
diff changeset
    20
#
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1498
diff changeset
    21
# You should have received a copy of the GNU General Public License
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1498
diff changeset
    22
# along with this program; if not, write to the Free Software
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1498
diff changeset
    23
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
428
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
    24
1881
091005ec69c4 fix pylint py3k conversion warning: "(no-absolute-import) import missing `from __future__ import absolute_import`"
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1852
diff changeset
    25
3758
bc71b19b45ff switch to pickle from cPickle
GP Orcullo <kinsamanka@gmail.com>
parents: 3750
diff changeset
    26
import pickle
428
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
    27
import wx
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
    28
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
    29
MAX_ITEM_COUNT = 10
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
    30
MAX_ITEM_SHOWN = 6
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
    31
if wx.Platform == '__WXMSW__':
1180
276a30c68eaa Fixed bugs with TextCtrlAutoComplete
Laurent Bessard
parents: 814
diff changeset
    32
    LISTBOX_BORDER_HEIGHT = 2
276a30c68eaa Fixed bugs with TextCtrlAutoComplete
Laurent Bessard
parents: 814
diff changeset
    33
    LISTBOX_INTERVAL_HEIGHT = 0
428
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
    34
else:
1180
276a30c68eaa Fixed bugs with TextCtrlAutoComplete
Laurent Bessard
parents: 814
diff changeset
    35
    LISTBOX_BORDER_HEIGHT = 4
276a30c68eaa Fixed bugs with TextCtrlAutoComplete
Laurent Bessard
parents: 814
diff changeset
    36
    LISTBOX_INTERVAL_HEIGHT = 6
276a30c68eaa Fixed bugs with TextCtrlAutoComplete
Laurent Bessard
parents: 814
diff changeset
    37
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1735
diff changeset
    38
1180
276a30c68eaa Fixed bugs with TextCtrlAutoComplete
Laurent Bessard
parents: 814
diff changeset
    39
class PopupWithListbox(wx.PopupWindow):
1735
c02818d7e29f clean-up: fix PEP8 W293 blank line contains whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1733
diff changeset
    40
1852
70c1cc354a8f fix pylint warning '(dangerous-default-value) Dangerous default value {} as argument'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1832
diff changeset
    41
    def __init__(self, parent, choices=None):
1180
276a30c68eaa Fixed bugs with TextCtrlAutoComplete
Laurent Bessard
parents: 814
diff changeset
    42
        wx.PopupWindow.__init__(self, parent, wx.BORDER_SIMPLE)
1735
c02818d7e29f clean-up: fix PEP8 W293 blank line contains whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1733
diff changeset
    43
1745
f9d32913bad4 clean-up: fix PEP8 E227 missing whitespace around bitwise or shift operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
    44
        self.ListBox = wx.ListBox(self, -1, style=wx.LB_HSCROLL | wx.LB_SINGLE | wx.LB_SORT)
1735
c02818d7e29f clean-up: fix PEP8 W293 blank line contains whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1733
diff changeset
    45
1852
70c1cc354a8f fix pylint warning '(dangerous-default-value) Dangerous default value {} as argument'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1832
diff changeset
    46
        choices = [] if choices is None else choices
428
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
    47
        self.SetChoices(choices)
1735
c02818d7e29f clean-up: fix PEP8 W293 blank line contains whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1733
diff changeset
    48
1180
276a30c68eaa Fixed bugs with TextCtrlAutoComplete
Laurent Bessard
parents: 814
diff changeset
    49
        self.ListBox.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
276a30c68eaa Fixed bugs with TextCtrlAutoComplete
Laurent Bessard
parents: 814
diff changeset
    50
        self.ListBox.Bind(wx.EVT_MOTION, self.OnMotion)
1735
c02818d7e29f clean-up: fix PEP8 W293 blank line contains whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1733
diff changeset
    51
428
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
    52
    def SetChoices(self, choices):
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
    53
        max_text_width = 0
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
    54
        max_text_height = 0
1735
c02818d7e29f clean-up: fix PEP8 W293 blank line contains whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1733
diff changeset
    55
428
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
    56
        self.ListBox.Clear()
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
    57
        for choice in choices:
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
    58
            self.ListBox.Append(choice)
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
    59
            w, h = self.ListBox.GetTextExtent(choice)
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
    60
            max_text_width = max(max_text_width, w)
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
    61
            max_text_height = max(max_text_height, h)
1735
c02818d7e29f clean-up: fix PEP8 W293 blank line contains whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1733
diff changeset
    62
428
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
    63
        itemcount = min(len(choices), MAX_ITEM_SHOWN)
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
    64
        width = self.Parent.GetSize()[0]
1767
c74815729afd clean-up: fix PEP8 E127 continuation line over-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1745
diff changeset
    65
        height = \
c74815729afd clean-up: fix PEP8 E127 continuation line over-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1745
diff changeset
    66
            max_text_height * itemcount + \
c74815729afd clean-up: fix PEP8 E127 continuation line over-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1745
diff changeset
    67
            LISTBOX_INTERVAL_HEIGHT * max(0, itemcount - 1) + \
c74815729afd clean-up: fix PEP8 E127 continuation line over-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1745
diff changeset
    68
            2 * LISTBOX_BORDER_HEIGHT
428
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
    69
        if max_text_width + 10 > width:
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
    70
            height += 15
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
    71
        size = wx.Size(width, height)
1180
276a30c68eaa Fixed bugs with TextCtrlAutoComplete
Laurent Bessard
parents: 814
diff changeset
    72
        if wx.Platform == '__WXMSW__':
276a30c68eaa Fixed bugs with TextCtrlAutoComplete
Laurent Bessard
parents: 814
diff changeset
    73
            size.width -= 2
428
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
    74
        self.ListBox.SetSize(size)
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
    75
        self.SetClientSize(size)
1735
c02818d7e29f clean-up: fix PEP8 W293 blank line contains whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1733
diff changeset
    76
428
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
    77
    def MoveSelection(self, direction):
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
    78
        selected = self.ListBox.GetSelection()
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
    79
        if selected == wx.NOT_FOUND:
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
    80
            if direction >= 0:
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
    81
                selected = 0
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
    82
            else:
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
    83
                selected = self.ListBox.GetCount() - 1
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
    84
        else:
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
    85
            selected = (selected + direction) % (self.ListBox.GetCount() + 1)
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
    86
        if selected == self.ListBox.GetCount():
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
    87
            selected = wx.NOT_FOUND
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
    88
        self.ListBox.SetSelection(selected)
1735
c02818d7e29f clean-up: fix PEP8 W293 blank line contains whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1733
diff changeset
    89
428
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
    90
    def GetSelection(self):
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
    91
        return self.ListBox.GetStringSelection()
1735
c02818d7e29f clean-up: fix PEP8 W293 blank line contains whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1733
diff changeset
    92
1180
276a30c68eaa Fixed bugs with TextCtrlAutoComplete
Laurent Bessard
parents: 814
diff changeset
    93
    def OnLeftDown(self, event):
1498
b11045a2f17c fix deprecation warnings about accessing properties m_x, m_y of wxKeyEvent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1180
diff changeset
    94
        selected = self.ListBox.HitTest(wx.Point(event.GetX(), event.GetY()))
1180
276a30c68eaa Fixed bugs with TextCtrlAutoComplete
Laurent Bessard
parents: 814
diff changeset
    95
        parent_size = self.Parent.GetSize()
276a30c68eaa Fixed bugs with TextCtrlAutoComplete
Laurent Bessard
parents: 814
diff changeset
    96
        parent_rect = wx.Rect(0, -parent_size[1], parent_size[0], parent_size[1])
428
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
    97
        if selected != wx.NOT_FOUND:
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
    98
            wx.CallAfter(self.Parent.SetValueFromSelected, self.ListBox.GetString(selected))
3303
0ffb41625592 Preliminary support for WxPython 4.1.0. Needs more testing. Grid selection/focus seems broken, and probably many other bugs hidden in dialogs and editors.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2431
diff changeset
    99
        elif parent_rect.Contains(event.GetX(), event.GetY()):
1498
b11045a2f17c fix deprecation warnings about accessing properties m_x, m_y of wxKeyEvent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1180
diff changeset
   100
            result, x, y = self.Parent.HitTest(wx.Point(event.GetX(), event.GetY() + parent_size[1]))
1180
276a30c68eaa Fixed bugs with TextCtrlAutoComplete
Laurent Bessard
parents: 814
diff changeset
   101
            if result != wx.TE_HT_UNKNOWN:
276a30c68eaa Fixed bugs with TextCtrlAutoComplete
Laurent Bessard
parents: 814
diff changeset
   102
                self.Parent.SetInsertionPoint(self.Parent.XYToPosition(x, y))
276a30c68eaa Fixed bugs with TextCtrlAutoComplete
Laurent Bessard
parents: 814
diff changeset
   103
        else:
276a30c68eaa Fixed bugs with TextCtrlAutoComplete
Laurent Bessard
parents: 814
diff changeset
   104
            wx.CallAfter(self.Parent.DismissListBox)
276a30c68eaa Fixed bugs with TextCtrlAutoComplete
Laurent Bessard
parents: 814
diff changeset
   105
        event.Skip()
1735
c02818d7e29f clean-up: fix PEP8 W293 blank line contains whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1733
diff changeset
   106
1180
276a30c68eaa Fixed bugs with TextCtrlAutoComplete
Laurent Bessard
parents: 814
diff changeset
   107
    def OnMotion(self, event):
276a30c68eaa Fixed bugs with TextCtrlAutoComplete
Laurent Bessard
parents: 814
diff changeset
   108
        self.ListBox.SetSelection(
1498
b11045a2f17c fix deprecation warnings about accessing properties m_x, m_y of wxKeyEvent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1180
diff changeset
   109
            self.ListBox.HitTest(wx.Point(event.GetX(), event.GetY())))
1180
276a30c68eaa Fixed bugs with TextCtrlAutoComplete
Laurent Bessard
parents: 814
diff changeset
   110
        event.Skip()
1735
c02818d7e29f clean-up: fix PEP8 W293 blank line contains whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1733
diff changeset
   111
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1735
diff changeset
   112
428
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   113
class TextCtrlAutoComplete(wx.TextCtrl):
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   114
1771
f68a105000be clean-up: fix PEP8 E211 whitespace before '[' or '('
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1767
diff changeset
   115
    def __init__(self, parent, choices=None, dropDownClick=True,
f68a105000be clean-up: fix PEP8 E211 whitespace before '[' or '('
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1767
diff changeset
   116
                 element_path=None, **therest):
428
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   117
        """
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   118
        Constructor works just like wx.TextCtrl except you can pass in a
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   119
        list of choices.  You can also change the choice list at any time
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   120
        by calling setChoices.
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   121
        """
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   122
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   123
        therest['style'] = wx.TE_PROCESS_ENTER | therest.get('style', 0)
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   124
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   125
        wx.TextCtrl.__init__(self, parent, **therest)
1735
c02818d7e29f clean-up: fix PEP8 W293 blank line contains whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1733
diff changeset
   126
1733
dea107dce0c4 clean-up: fix some PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
   127
        # Some variables
428
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   128
        self._dropDownClick = dropDownClick
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   129
        self._lastinsertionpoint = None
1180
276a30c68eaa Fixed bugs with TextCtrlAutoComplete
Laurent Bessard
parents: 814
diff changeset
   130
        self._hasfocus = False
1735
c02818d7e29f clean-up: fix PEP8 W293 blank line contains whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1733
diff changeset
   131
428
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   132
        self._screenheight = wx.SystemSettings.GetMetric(wx.SYS_SCREEN_Y)
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   133
        self.element_path = element_path
1735
c02818d7e29f clean-up: fix PEP8 W293 blank line contains whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1733
diff changeset
   134
428
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   135
        self.listbox = None
1735
c02818d7e29f clean-up: fix PEP8 W293 blank line contains whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1733
diff changeset
   136
428
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   137
        self.SetChoices(choices)
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   138
1782
5b6ad7a7fd9d clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1771
diff changeset
   139
        # gp = self
5b6ad7a7fd9d clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1771
diff changeset
   140
        # while ( gp != None ) :
428
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   141
        #    gp.Bind ( wx.EVT_MOVE , self.onControlChanged, gp )
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   142
        #    gp.Bind ( wx.EVT_SIZE , self.onControlChanged, gp )
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   143
        #    gp = gp.GetParent()
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   144
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   145
        self.Bind(wx.EVT_KILL_FOCUS, self.OnControlChanged)
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   146
        self.Bind(wx.EVT_TEXT_ENTER, self.OnControlChanged)
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   147
        self.Bind(wx.EVT_TEXT, self.OnEnteredText)
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   148
        self.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   149
1733
dea107dce0c4 clean-up: fix some PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
   150
        # If need drop down on left click
428
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   151
        if dropDownClick:
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   152
            self.Bind(wx.EVT_LEFT_DOWN, self.OnClickToggleDown)
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   153
            self.Bind(wx.EVT_LEFT_UP, self.OnClickToggleUp)
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   154
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   155
    def ChangeValue(self, value):
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   156
        wx.TextCtrl.ChangeValue(self, value)
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   157
        self.RefreshListBoxChoices()
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   158
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   159
    def OnEnteredText(self, event):
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   160
        wx.CallAfter(self.RefreshListBoxChoices)
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   161
        event.Skip()
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   162
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   163
    def OnKeyDown(self, event):
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   164
        """ Do some work when the user press on the keys:
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   165
            up and down: move the cursor
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   166
        """
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   167
        keycode = event.GetKeyCode()
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   168
        if keycode in [wx.WXK_DOWN, wx.WXK_UP]:
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   169
            self.PopupListBox()
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   170
            if keycode == wx.WXK_DOWN:
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   171
                self.listbox.MoveSelection(1)
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   172
            else:
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   173
                self.listbox.MoveSelection(-1)
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   174
        elif keycode in [wx.WXK_LEFT, wx.WXK_RIGHT, wx.WXK_RETURN] and self.listbox is not None:
1180
276a30c68eaa Fixed bugs with TextCtrlAutoComplete
Laurent Bessard
parents: 814
diff changeset
   175
            selected = self.listbox.GetSelection()
276a30c68eaa Fixed bugs with TextCtrlAutoComplete
Laurent Bessard
parents: 814
diff changeset
   176
            if selected != "":
276a30c68eaa Fixed bugs with TextCtrlAutoComplete
Laurent Bessard
parents: 814
diff changeset
   177
                self.SetValueFromSelected(selected)
276a30c68eaa Fixed bugs with TextCtrlAutoComplete
Laurent Bessard
parents: 814
diff changeset
   178
            else:
276a30c68eaa Fixed bugs with TextCtrlAutoComplete
Laurent Bessard
parents: 814
diff changeset
   179
                event.Skip()
428
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   180
        elif event.GetKeyCode() == wx.WXK_ESCAPE:
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   181
            self.DismissListBox()
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   182
        else:
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   183
            event.Skip()
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   184
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   185
    def OnClickToggleDown(self, event):
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   186
        self._lastinsertionpoint = self.GetInsertionPoint()
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   187
        event.Skip()
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   188
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   189
    def OnClickToggleUp(self, event):
1180
276a30c68eaa Fixed bugs with TextCtrlAutoComplete
Laurent Bessard
parents: 814
diff changeset
   190
        if not self._hasfocus:
276a30c68eaa Fixed bugs with TextCtrlAutoComplete
Laurent Bessard
parents: 814
diff changeset
   191
            self._hasfocus = True
276a30c68eaa Fixed bugs with TextCtrlAutoComplete
Laurent Bessard
parents: 814
diff changeset
   192
        elif self.GetInsertionPoint() == self._lastinsertionpoint:
428
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   193
            wx.CallAfter(self.PopupListBox)
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   194
        self._lastinsertionpoint = None
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   195
        event.Skip()
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   196
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   197
    def OnControlChanged(self, event):
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   198
        res = self.GetValue()
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   199
        config = wx.ConfigBase.Get()
3758
bc71b19b45ff switch to pickle from cPickle
GP Orcullo <kinsamanka@gmail.com>
parents: 3750
diff changeset
   200
        listentries = pickle.loads(config.Read(self.element_path,
bc71b19b45ff switch to pickle from cPickle
GP Orcullo <kinsamanka@gmail.com>
parents: 3750
diff changeset
   201
                                               pickle.dumps([], 0).decode()
bc71b19b45ff switch to pickle from cPickle
GP Orcullo <kinsamanka@gmail.com>
parents: 3750
diff changeset
   202
                                              ).encode())
428
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   203
        if res and res not in listentries:
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   204
            listentries = (listentries + [res])[-MAX_ITEM_COUNT:]
3758
bc71b19b45ff switch to pickle from cPickle
GP Orcullo <kinsamanka@gmail.com>
parents: 3750
diff changeset
   205
            config.Write(self.element_path, pickle.dumps(listentries, 0))
428
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   206
            config.Flush()
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   207
            self.SetChoices(listentries)
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   208
        self.DismissListBox()
1180
276a30c68eaa Fixed bugs with TextCtrlAutoComplete
Laurent Bessard
parents: 814
diff changeset
   209
        self._hasfocus = False
428
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   210
        event.Skip()
1735
c02818d7e29f clean-up: fix PEP8 W293 blank line contains whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1733
diff changeset
   211
428
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   212
    def SetChoices(self, choices):
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   213
        self._choices = choices
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   214
        self.RefreshListBoxChoices()
1735
c02818d7e29f clean-up: fix PEP8 W293 blank line contains whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1733
diff changeset
   215
428
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   216
    def GetChoices(self):
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   217
        return self._choices
1735
c02818d7e29f clean-up: fix PEP8 W293 blank line contains whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1733
diff changeset
   218
428
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   219
    def SetValueFromSelected(self, selected):
1180
276a30c68eaa Fixed bugs with TextCtrlAutoComplete
Laurent Bessard
parents: 814
diff changeset
   220
        """
276a30c68eaa Fixed bugs with TextCtrlAutoComplete
Laurent Bessard
parents: 814
diff changeset
   221
        Sets the wx.TextCtrl value from the selected wx.ListCtrl item.
276a30c68eaa Fixed bugs with TextCtrlAutoComplete
Laurent Bessard
parents: 814
diff changeset
   222
        Will do nothing if no item is selected in the wx.ListCtrl.
276a30c68eaa Fixed bugs with TextCtrlAutoComplete
Laurent Bessard
parents: 814
diff changeset
   223
        """
276a30c68eaa Fixed bugs with TextCtrlAutoComplete
Laurent Bessard
parents: 814
diff changeset
   224
        if selected != "":
428
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   225
            self.SetValue(selected)
1180
276a30c68eaa Fixed bugs with TextCtrlAutoComplete
Laurent Bessard
parents: 814
diff changeset
   226
        self.DismissListBox()
1735
c02818d7e29f clean-up: fix PEP8 W293 blank line contains whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1733
diff changeset
   227
428
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   228
    def RefreshListBoxChoices(self):
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   229
        if self.listbox is not None:
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   230
            text = self.GetValue()
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   231
            choices = [choice for choice in self._choices if choice.startswith(text)]
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   232
            self.listbox.SetChoices(choices)
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   233
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   234
    def PopupListBox(self):
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   235
        if self.listbox is None:
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   236
            self.listbox = PopupWithListbox(self)
1735
c02818d7e29f clean-up: fix PEP8 W293 blank line contains whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1733
diff changeset
   237
428
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   238
            # Show the popup right below or above the button
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   239
            # depending on available screen space...
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   240
            pos = self.ClientToScreen((0, 0))
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   241
            sz = self.GetSize()
1180
276a30c68eaa Fixed bugs with TextCtrlAutoComplete
Laurent Bessard
parents: 814
diff changeset
   242
            if wx.Platform == '__WXMSW__':
276a30c68eaa Fixed bugs with TextCtrlAutoComplete
Laurent Bessard
parents: 814
diff changeset
   243
                pos.x -= 2
276a30c68eaa Fixed bugs with TextCtrlAutoComplete
Laurent Bessard
parents: 814
diff changeset
   244
                pos.y -= 2
428
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   245
            self.listbox.Position(pos, (0, sz[1]))
1735
c02818d7e29f clean-up: fix PEP8 W293 blank line contains whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1733
diff changeset
   246
428
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   247
            self.RefreshListBoxChoices()
1735
c02818d7e29f clean-up: fix PEP8 W293 blank line contains whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1733
diff changeset
   248
1180
276a30c68eaa Fixed bugs with TextCtrlAutoComplete
Laurent Bessard
parents: 814
diff changeset
   249
            self.listbox.Show()
428
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   250
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   251
    def DismissListBox(self):
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   252
        if self.listbox is not None:
1180
276a30c68eaa Fixed bugs with TextCtrlAutoComplete
Laurent Bessard
parents: 814
diff changeset
   253
            if self.listbox.ListBox.HasCapture():
276a30c68eaa Fixed bugs with TextCtrlAutoComplete
Laurent Bessard
parents: 814
diff changeset
   254
                self.listbox.ListBox.ReleaseMouse()
276a30c68eaa Fixed bugs with TextCtrlAutoComplete
Laurent Bessard
parents: 814
diff changeset
   255
            self.listbox.Destroy()
428
ea09f33ce717 Update internationalization for new functionalities.
laurent
parents: 426
diff changeset
   256
            self.listbox = None