Mercurial > libervia-backend
annotate frontends/sortilege_old/editbox.py @ 762:aed7d99276b8
core (xml_tools), frontends: added a prefix to XMLUI form names in result data, to avoid name conflicts (e.g.: if a form has name "submit", and this name is also used internally by SàT)
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 24 Dec 2013 15:43:22 +0100 |
parents | 84a6e83157c2 |
children |
rev | line source |
---|---|
0 | 1 #!/usr/bin/python |
2 # -*- coding: utf-8 -*- | |
3 | |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
587
diff
changeset
|
4 # sortilege: a SAT frontend |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
587
diff
changeset
|
5 # Copyright (C) 2009, 2010, 2011 Jérôme Poisson (goffi@goffi.org) |
0 | 6 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
587
diff
changeset
|
7 # This program is free software: you can redistribute it and/or modify |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
587
diff
changeset
|
8 # it under the terms of the GNU Affero General Public License as published by |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
587
diff
changeset
|
9 # the Free Software Foundation, either version 3 of the License, or |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
587
diff
changeset
|
10 # (at your option) any later version. |
0 | 11 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
587
diff
changeset
|
12 # This program is distributed in the hope that it will be useful, |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
587
diff
changeset
|
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
587
diff
changeset
|
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
587
diff
changeset
|
15 # GNU Affero General Public License for more details. |
0 | 16 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
587
diff
changeset
|
17 # You should have received a copy of the GNU Affero General Public License |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
587
diff
changeset
|
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
0 | 19 |
20 | |
21 import curses | |
22 from curses import ascii | |
23 from window import Window | |
24 | |
25 def C(k): | |
26 """return the value of Ctrl+key""" | |
27 return ord(ascii.ctrl(k)) | |
28 | |
29 def A(k): | |
30 """return the value of Alt+key""" | |
31 return ord(ascii.alt(k)) | |
32 | |
33 class EditBox(Window): | |
34 """This class manage the edition of text""" | |
35 | |
36 def __init__(self, parent, header, code="utf-8"): | |
37 self.__header=header | |
38 self.__text = unicode() | |
39 self.__curs_pos=0 | |
40 self.__buffer=str() | |
41 self.__replace_mode=False | |
42 self.__parent=parent | |
43 self.__code=code | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
480
diff
changeset
|
44 |
0 | 45 Window.__init__(self, self.__parent, 1, self.__parent.getmaxyx()[1], self.__parent.getmaxyx()[0]-1,0, code=code) |
46 self.update() | |
47 | |
48 def registerEnterCB(self, CB): | |
49 self.__enterCB=CB | |
50 | |
51 def resizeAdapt(self): | |
52 """Adapt window size to self.__parent size. | |
53 Must be called when self.__parent is resized.""" | |
54 self.resize(1, self.__parent.getmaxyx()[1], self.__parent.getmaxyx()[0]-1,0) | |
55 self.update() | |
56 | |
57 def __getTextToPrint(self): | |
58 """return the text printed on the edit line""" | |
59 width = self.rWidth - len(self.__header) -1 | |
60 if self.__curs_pos<width: | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
480
diff
changeset
|
61 begin = 0 |
0 | 62 end = width |
63 else: | |
64 begin = self.__curs_pos-width | |
65 end = self.__curs_pos | |
66 return self.__header+self.__text[begin:end] | |
67 | |
68 def update(self): | |
69 Window.update(self) | |
70 text = self.__getTextToPrint() | |
71 self.addYXStr(0, 0, text, limit=self.rWidth) | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
480
diff
changeset
|
72 |
0 | 73 self.noutrefresh() |
74 | |
75 def __dec_cur(self): | |
76 """move cursor on the left""" | |
77 if self.__curs_pos>0: | |
78 self.__curs_pos = self.__curs_pos - 1 | |
79 | |
80 def __inc_cur(self): | |
81 """move cursor on the right""" | |
82 if self.__curs_pos<len(self.__text): | |
83 self.__curs_pos = self.__curs_pos + 1 | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
480
diff
changeset
|
84 |
0 | 85 def move_cur(self, x): |
86 pos = x+len(self.__header) | |
87 if pos>=self.rWidth: | |
88 pos=self.rWidth-1 | |
89 self.move(0, pos) | |
90 | |
91 def clear_text(self): | |
92 """Clear the text of the edit box""" | |
93 self.__text="" | |
94 self.__curs_pos=0 | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
480
diff
changeset
|
95 |
0 | 96 def replace_cur(self): |
97 """must be called earch time the cursor is moved""" | |
98 self.move_cur(self.__curs_pos) | |
99 self.noutrefresh() | |
100 | |
101 def activate(self, state=True): | |
102 cursor_mode = 1 if state else 0 | |
103 curses.curs_set(cursor_mode) | |
104 Window.activate(self,state) | |
105 | |
106 def handleKey(self, k): | |
107 if ascii.isgraph(k) or ascii.isblank(k): | |
108 pacman = 0 if not self.__replace_mode else 1 | |
109 self.__text = self.__text[:self.__curs_pos] + chr(k) + self.__text[self.__curs_pos + pacman:] | |
110 self.__curs_pos = self.__curs_pos + 1 | |
111 | |
112 elif k==ascii.NL: | |
113 try: | |
114 self.__enterCB(self.__text) | |
115 except NameError: | |
116 pass # TODO: thrown an error here | |
117 self.clear_text() | |
118 | |
119 elif k==curses.KEY_BACKSPACE: | |
120 self.__text = self.__text[:self.__curs_pos-1]+self.__text[self.__curs_pos:] | |
121 self.__dec_cur() | |
122 | |
123 elif k==curses.KEY_DC: | |
124 self.__text = self.__text[:self.__curs_pos]+self.__text[self.__curs_pos+1:] | |
125 | |
126 elif k==curses.KEY_IC: | |
127 self.__replace_mode = not self.__replace_mode | |
128 | |
129 elif k==curses.KEY_LEFT: | |
130 self.__dec_cur() | |
131 | |
132 elif k==curses.KEY_RIGHT: | |
133 self.__inc_cur() | |
134 | |
135 elif k==curses.KEY_HOME or k==C('a'): | |
136 self.__curs_pos=0 | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
480
diff
changeset
|
137 |
0 | 138 elif k==curses.KEY_END or k==C('e'): |
139 self.__curs_pos=len(self.__text) | |
140 | |
141 elif k==C('k'): | |
142 self.__text = self.__text[:self.__curs_pos] | |
143 | |
144 elif k==C('w'): | |
145 before = self.__text[:self.__curs_pos] | |
146 pos = before.rstrip().rfind(" ")+1 | |
147 self.__text = before[:pos] + self.__text[self.__curs_pos:] | |
148 self.__curs_pos = pos | |
149 | |
150 elif k>255: | |
151 self.__buffer="" | |
152 | |
153 else: ## FIXME: dangerous code, must be checked ! (specialy buffer overflow) ## | |
154 #We now manage unicode | |
155 self.__buffer = self.__buffer+chr(k) | |
156 decoded=unicode() | |
157 if len(self.__buffer)>4: | |
158 self.__buffer="" | |
159 return | |
160 try: | |
161 decoded = self.__buffer.decode(self.__code) | |
162 except UnicodeDecodeError, e: | |
163 if e.reason!="unexpected end of data": | |
164 self.__buffer="" | |
165 return | |
166 if len(self.__buffer)==1: ## FIXME: awful ! only for test ! | |
167 self.__buffer="" | |
168 return | |
169 self.__text = self.__text + decoded | |
170 self.__curs_pos = self.__curs_pos + 1 | |
171 self.__buffer="" | |
172 | |
173 self.update() | |
174 |