author | laurent |
Thu, 03 Sep 2009 11:28:46 +0200 | |
changeset 387 | 2cc48e356902 |
parent 326 | 386566f263f3 |
child 426 | 3f285782ac9b |
permissions | -rw-r--r-- |
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 |
|
326
386566f263f3
Bug opening Auto complete frame when not expected fixed
lbessard
parents:
295
diff
changeset
|
48 |
self._lastinsertionpoint = None |
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()) |
|
326
386566f263f3
Bug opening Auto complete frame when not expected fixed
lbessard
parents:
295
diff
changeset
|
127 |
self._lastinsertionpoint = None |
295 | 128 |
event.Skip() |
268 | 129 |
|
130 |
def onControlChanged(self, event): |
|
131 |
res = self.GetValue() |
|
132 |
config = wx.ConfigBase.Get() |
|
133 |
listentries = cPickle.loads(str(config.Read(self.element_path, cPickle.dumps([])))) |
|
295 | 134 |
if res and res not in listentries: |
135 |
listentries = (listentries + [res])[-MAX_ITEM_COUNT:] |
|
136 |
config.Write(self.element_path, cPickle.dumps(listentries)) |
|
268 | 137 |
config.Flush() |
295 | 138 |
self.SetChoices(listentries) |
139 |
self._showDropDown(False) |
|
140 |
event.Skip() |
|
141 |
||
142 |
def SetChoices(self, choices): |
|
143 |
self._choices = choices |
|
144 |
self._refreshListBoxChoices() |
|
145 |
||
146 |
def GetChoices(self): |
|
147 |
return self._choices |
|
148 |
||
149 |
#------------------------------------------------------------------------------- |
|
150 |
# Internal methods |
|
151 |
#------------------------------------------------------------------------------- |
|
152 |
||
153 |
def _refreshListBoxChoices(self): |
|
154 |
text = self.GetValue() |
|
155 |
||
156 |
self.dropdownlistbox.Clear() |
|
157 |
for choice in self._choices: |
|
158 |
if choice.startswith(text): |
|
159 |
self.dropdownlistbox.Append(choice) |
|
160 |
||
161 |
itemcount = min(len(self.dropdownlistbox.GetStrings()), MAX_ITEM_SHOWN) |
|
162 |
self.popupsize = wx.Size(self.GetSize()[0], ITEM_HEIGHT * itemcount + 4) |
|
163 |
self.dropdownlistbox.SetSize(self.popupsize) |
|
164 |
self.dropdown.SetClientSize(self.popupsize) |
|
165 |
||
166 |
def _moveSelection(self, direction): |
|
167 |
selected = self.dropdownlistbox.GetSelection() |
|
168 |
if selected == wx.NOT_FOUND: |
|
169 |
if direction >= 0: |
|
170 |
selected = 0 |
|
171 |
else: |
|
172 |
selected = self.dropdownlistbox.GetCount() - 1 |
|
268 | 173 |
else: |
295 | 174 |
selected = (selected + direction) % (self.dropdownlistbox.GetCount() + 1) |
175 |
if selected == self.dropdownlistbox.GetCount(): |
|
176 |
selected = wx.NOT_FOUND |
|
177 |
self.dropdownlistbox.SetSelection(selected) |
|
178 |
||
179 |
def _setValueFromSelected(self): |
|
180 |
""" |
|
268 | 181 |
Sets the wx.TextCtrl value from the selected wx.ListCtrl item. |
182 |
Will do nothing if no item is selected in the wx.ListCtrl. |
|
295 | 183 |
""" |
184 |
selected = self.dropdownlistbox.GetStringSelection() |
|
185 |
if selected: |
|
186 |
self.SetValue(selected) |
|
187 |
self._showDropDown(False) |
|
188 |
||
189 |
||
190 |
def _showDropDown(self, show=True) : |
|
191 |
""" |
|
268 | 192 |
Either display the drop down list (show = True) or hide it (show = False). |
295 | 193 |
""" |
268 | 194 |
if show : |
195 |
size = self.dropdown.GetSize() |
|
295 | 196 |
width, height = self.GetSizeTuple() |
197 |
x, y = self.ClientToScreenXY(0, height) |
|
268 | 198 |
if size.GetWidth() != width : |
199 |
size.SetWidth(width) |
|
200 |
self.dropdown.SetSize(size) |
|
201 |
self.dropdownlistbox.SetSize(self.dropdown.GetClientSize()) |
|
202 |
if (y + size.GetHeight()) < self._screenheight : |
|
295 | 203 |
self.dropdown.SetPosition(wx.Point(x, y)) |
204 |
else: |
|
205 |
self.dropdown.SetPosition(wx.Point(x, y - height - size.GetHeight())) |
|
206 |
self.dropdown.Show(show) |
|
207 |