295
|
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) 2007: Edouard TISSERANT and Laurent BESSARD
|
|
8 |
#
|
|
9 |
#See COPYING file for copyrights details.
|
|
10 |
#
|
|
11 |
#This library is free software; you can redistribute it and/or
|
|
12 |
#modify it under the terms of the GNU General Public
|
|
13 |
#License as published by the Free Software Foundation; either
|
|
14 |
#version 2.1 of the License, or (at your option) any later version.
|
|
15 |
#
|
|
16 |
#This library 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 GNU
|
|
19 |
#General Public License for more details.
|
|
20 |
#
|
|
21 |
#You should have received a copy of the GNU General Public
|
|
22 |
#License along with this library; if not, write to the Free Software
|
|
23 |
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
24 |
|
|
25 |
import wx
|
268
|
26 |
import cPickle
|
295
|
27 |
|
|
28 |
MAX_ITEM_COUNT = 10
|
|
29 |
MAX_ITEM_SHOWN = 6
|
|
30 |
ITEM_HEIGHT = 25
|
|
31 |
|
|
32 |
class TextCtrlAutoComplete(wx.TextCtrl):
|
|
33 |
|
|
34 |
def __init__ (self, parent, choices=None, dropDownClick=True,
|
|
35 |
element_path=None, **therest):
|
|
36 |
"""
|
268
|
37 |
Constructor works just like wx.TextCtrl except you can pass in a
|
|
38 |
list of choices. You can also change the choice list at any time
|
|
39 |
by calling setChoices.
|
295
|
40 |
"""
|
|
41 |
|
|
42 |
therest['style'] = wx.TE_PROCESS_ENTER | therest.get('style', 0)
|
|
43 |
|
|
44 |
wx.TextCtrl.__init__(self, parent, **therest)
|
268
|
45 |
|
|
46 |
#Some variables
|
|
47 |
self._dropDownClick = dropDownClick
|
|
48 |
self._lastinsertionpoint = 0
|
295
|
49 |
|
|
50 |
self._screenheight = wx.SystemSettings.GetMetric(wx.SYS_SCREEN_Y)
|
268
|
51 |
self.element_path = element_path
|
295
|
52 |
|
268
|
53 |
#widgets
|
295
|
54 |
self.dropdown = wx.PopupWindow(self)
|
268
|
55 |
|
|
56 |
#Control the style
|
295
|
57 |
flags = wx.LB_HSCROLL | wx.LB_SINGLE | wx.LB_SORT
|
|
58 |
|
268
|
59 |
#Create the list and bind the events
|
295
|
60 |
self.dropdownlistbox = wx.ListBox(self.dropdown, style=flags,
|
|
61 |
pos=wx.Point(0, 0))
|
|
62 |
|
|
63 |
self.SetChoices(choices)
|
268
|
64 |
|
|
65 |
#gp = self
|
|
66 |
#while ( gp != None ) :
|
|
67 |
# gp.Bind ( wx.EVT_MOVE , self.onControlChanged, gp )
|
|
68 |
# gp.Bind ( wx.EVT_SIZE , self.onControlChanged, gp )
|
|
69 |
# gp = gp.GetParent()
|
|
70 |
|
295
|
71 |
self.Bind(wx.EVT_KILL_FOCUS, self.onControlChanged, self)
|
|
72 |
self.Bind(wx.EVT_TEXT_ENTER, self.onControlChanged, self)
|
|
73 |
self.Bind(wx.EVT_TEXT, self.onEnteredText, self)
|
|
74 |
self.Bind(wx.EVT_KEY_DOWN, self.onKeyDown, self)
|
268
|
75 |
|
|
76 |
#If need drop down on left click
|
|
77 |
if dropDownClick:
|
295
|
78 |
self.Bind(wx.EVT_LEFT_DOWN , self.onClickToggleDown, self)
|
|
79 |
self.Bind(wx.EVT_LEFT_UP , self.onClickToggleUp, self)
|
|
80 |
|
|
81 |
self.dropdownlistbox.Bind(wx.EVT_LISTBOX, self.onListItemSelected)
|
|
82 |
self.dropdownlistbox.Bind(wx.EVT_LISTBOX_DCLICK, self.onListItemSelected)
|
|
83 |
|
|
84 |
def ChangeValue(self, value):
|
|
85 |
wx.TextCtrl.ChangeValue(self, value)
|
|
86 |
self._refreshListBoxChoices()
|
268
|
87 |
|
|
88 |
def onEnteredText(self, event):
|
295
|
89 |
wx.CallAfter(self._refreshListBoxChoices)
|
|
90 |
event.Skip()
|
|
91 |
|
|
92 |
def onKeyDown(self, event) :
|
268
|
93 |
""" Do some work when the user press on the keys:
|
|
94 |
up and down: move the cursor
|
295
|
95 |
"""
|
268
|
96 |
visible = self.dropdown.IsShown()
|
295
|
97 |
keycode = event.GetKeyCode()
|
|
98 |
if keycode in [wx.WXK_DOWN, wx.WXK_UP]:
|
|
99 |
if not visible:
|
|
100 |
self._showDropDown()
|
|
101 |
elif keycode == wx.WXK_DOWN:
|
|
102 |
self._moveSelection(1)
|
|
103 |
else:
|
|
104 |
self._moveSelection(-1)
|
|
105 |
elif keycode in [wx.WXK_LEFT, wx.WXK_RIGHT, wx.WXK_RETURN] and visible:
|
|
106 |
if self.dropdownlistbox.GetSelection() != wx.NOT_FOUND:
|
268
|
107 |
self._setValueFromSelected()
|
295
|
108 |
else:
|
|
109 |
self._showDropDown(False)
|
|
110 |
event.Skip()
|
|
111 |
elif event.GetKeyCode() == wx.WXK_ESCAPE:
|
|
112 |
self._showDropDown(False)
|
|
113 |
else:
|
268
|
114 |
event.Skip()
|
|
115 |
|
295
|
116 |
def onListItemSelected(self, event):
|
268
|
117 |
self._setValueFromSelected()
|
|
118 |
event.Skip()
|
|
119 |
|
|
120 |
def onClickToggleDown(self, event):
|
|
121 |
self._lastinsertionpoint = self.GetInsertionPoint()
|
295
|
122 |
event.Skip()
|
|
123 |
|
|
124 |
def onClickToggleUp(self, event):
|
|
125 |
if self.GetInsertionPoint() == self._lastinsertionpoint:
|
|
126 |
self._showDropDown(not self.dropdown.IsShown())
|
|
127 |
event.Skip()
|
268
|
128 |
|
|
129 |
def onControlChanged(self, event):
|
|
130 |
res = self.GetValue()
|
|
131 |
config = wx.ConfigBase.Get()
|
|
132 |
listentries = cPickle.loads(str(config.Read(self.element_path, cPickle.dumps([]))))
|
295
|
133 |
if res and res not in listentries:
|
|
134 |
listentries = (listentries + [res])[-MAX_ITEM_COUNT:]
|
|
135 |
config.Write(self.element_path, cPickle.dumps(listentries))
|
268
|
136 |
config.Flush()
|
295
|
137 |
self.SetChoices(listentries)
|
|
138 |
self._showDropDown(False)
|
|
139 |
event.Skip()
|
|
140 |
|
|
141 |
def SetChoices(self, choices):
|
|
142 |
self._choices = choices
|
|
143 |
self._refreshListBoxChoices()
|
|
144 |
|
|
145 |
def GetChoices(self):
|
|
146 |
return self._choices
|
|
147 |
|
|
148 |
#-------------------------------------------------------------------------------
|
|
149 |
# Internal methods
|
|
150 |
#-------------------------------------------------------------------------------
|
|
151 |
|
|
152 |
def _refreshListBoxChoices(self):
|
|
153 |
text = self.GetValue()
|
|
154 |
|
|
155 |
self.dropdownlistbox.Clear()
|
|
156 |
for choice in self._choices:
|
|
157 |
if choice.startswith(text):
|
|
158 |
self.dropdownlistbox.Append(choice)
|
|
159 |
|
|
160 |
itemcount = min(len(self.dropdownlistbox.GetStrings()), MAX_ITEM_SHOWN)
|
|
161 |
self.popupsize = wx.Size(self.GetSize()[0], ITEM_HEIGHT * itemcount + 4)
|
|
162 |
self.dropdownlistbox.SetSize(self.popupsize)
|
|
163 |
self.dropdown.SetClientSize(self.popupsize)
|
|
164 |
|
|
165 |
def _moveSelection(self, direction):
|
|
166 |
selected = self.dropdownlistbox.GetSelection()
|
|
167 |
if selected == wx.NOT_FOUND:
|
|
168 |
if direction >= 0:
|
|
169 |
selected = 0
|
|
170 |
else:
|
|
171 |
selected = self.dropdownlistbox.GetCount() - 1
|
268
|
172 |
else:
|
295
|
173 |
selected = (selected + direction) % (self.dropdownlistbox.GetCount() + 1)
|
|
174 |
if selected == self.dropdownlistbox.GetCount():
|
|
175 |
selected = wx.NOT_FOUND
|
|
176 |
self.dropdownlistbox.SetSelection(selected)
|
|
177 |
|
|
178 |
def _setValueFromSelected(self):
|
|
179 |
"""
|
268
|
180 |
Sets the wx.TextCtrl value from the selected wx.ListCtrl item.
|
|
181 |
Will do nothing if no item is selected in the wx.ListCtrl.
|
295
|
182 |
"""
|
|
183 |
selected = self.dropdownlistbox.GetStringSelection()
|
|
184 |
if selected:
|
|
185 |
self.SetValue(selected)
|
|
186 |
self._showDropDown(False)
|
|
187 |
|
|
188 |
|
|
189 |
def _showDropDown(self, show=True) :
|
|
190 |
"""
|
268
|
191 |
Either display the drop down list (show = True) or hide it (show = False).
|
295
|
192 |
"""
|
268
|
193 |
if show :
|
|
194 |
size = self.dropdown.GetSize()
|
295
|
195 |
width, height = self.GetSizeTuple()
|
|
196 |
x, y = self.ClientToScreenXY(0, height)
|
268
|
197 |
if size.GetWidth() != width :
|
|
198 |
size.SetWidth(width)
|
|
199 |
self.dropdown.SetSize(size)
|
|
200 |
self.dropdownlistbox.SetSize(self.dropdown.GetClientSize())
|
|
201 |
if (y + size.GetHeight()) < self._screenheight :
|
295
|
202 |
self.dropdown.SetPosition(wx.Point(x, y))
|
|
203 |
else:
|
|
204 |
self.dropdown.SetPosition(wx.Point(x, y - height - size.GetHeight()))
|
|
205 |
self.dropdown.Show(show)
|
|
206 |
|