annotate frontends/sortilege_old/editbox.py @ 792:2136be5a44a8

test: define the constants JIDs and profiles as lists
author souliane <souliane@mailoo.org>
date Thu, 09 Jan 2014 11:18:33 +0100
parents 84a6e83157c2
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
goffi@necton2
parents:
diff changeset
1 #!/usr/bin/python
goffi@necton2
parents:
diff changeset
2 # -*- coding: utf-8 -*-
goffi@necton2
parents:
diff changeset
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
goffi@necton2
parents:
diff changeset
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
goffi@necton2
parents:
diff changeset
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
goffi@necton2
parents:
diff changeset
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
goffi@necton2
parents:
diff changeset
19
goffi@necton2
parents:
diff changeset
20
goffi@necton2
parents:
diff changeset
21 import curses
goffi@necton2
parents:
diff changeset
22 from curses import ascii
goffi@necton2
parents:
diff changeset
23 from window import Window
goffi@necton2
parents:
diff changeset
24
goffi@necton2
parents:
diff changeset
25 def C(k):
goffi@necton2
parents:
diff changeset
26 """return the value of Ctrl+key"""
goffi@necton2
parents:
diff changeset
27 return ord(ascii.ctrl(k))
goffi@necton2
parents:
diff changeset
28
goffi@necton2
parents:
diff changeset
29 def A(k):
goffi@necton2
parents:
diff changeset
30 """return the value of Alt+key"""
goffi@necton2
parents:
diff changeset
31 return ord(ascii.alt(k))
goffi@necton2
parents:
diff changeset
32
goffi@necton2
parents:
diff changeset
33 class EditBox(Window):
goffi@necton2
parents:
diff changeset
34 """This class manage the edition of text"""
goffi@necton2
parents:
diff changeset
35
goffi@necton2
parents:
diff changeset
36 def __init__(self, parent, header, code="utf-8"):
goffi@necton2
parents:
diff changeset
37 self.__header=header
goffi@necton2
parents:
diff changeset
38 self.__text = unicode()
goffi@necton2
parents:
diff changeset
39 self.__curs_pos=0
goffi@necton2
parents:
diff changeset
40 self.__buffer=str()
goffi@necton2
parents:
diff changeset
41 self.__replace_mode=False
goffi@necton2
parents:
diff changeset
42 self.__parent=parent
goffi@necton2
parents:
diff changeset
43 self.__code=code
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 480
diff changeset
44
0
goffi@necton2
parents:
diff changeset
45 Window.__init__(self, self.__parent, 1, self.__parent.getmaxyx()[1], self.__parent.getmaxyx()[0]-1,0, code=code)
goffi@necton2
parents:
diff changeset
46 self.update()
goffi@necton2
parents:
diff changeset
47
goffi@necton2
parents:
diff changeset
48 def registerEnterCB(self, CB):
goffi@necton2
parents:
diff changeset
49 self.__enterCB=CB
goffi@necton2
parents:
diff changeset
50
goffi@necton2
parents:
diff changeset
51 def resizeAdapt(self):
goffi@necton2
parents:
diff changeset
52 """Adapt window size to self.__parent size.
goffi@necton2
parents:
diff changeset
53 Must be called when self.__parent is resized."""
goffi@necton2
parents:
diff changeset
54 self.resize(1, self.__parent.getmaxyx()[1], self.__parent.getmaxyx()[0]-1,0)
goffi@necton2
parents:
diff changeset
55 self.update()
goffi@necton2
parents:
diff changeset
56
goffi@necton2
parents:
diff changeset
57 def __getTextToPrint(self):
goffi@necton2
parents:
diff changeset
58 """return the text printed on the edit line"""
goffi@necton2
parents:
diff changeset
59 width = self.rWidth - len(self.__header) -1
goffi@necton2
parents:
diff changeset
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
goffi@necton2
parents:
diff changeset
62 end = width
goffi@necton2
parents:
diff changeset
63 else:
goffi@necton2
parents:
diff changeset
64 begin = self.__curs_pos-width
goffi@necton2
parents:
diff changeset
65 end = self.__curs_pos
goffi@necton2
parents:
diff changeset
66 return self.__header+self.__text[begin:end]
goffi@necton2
parents:
diff changeset
67
goffi@necton2
parents:
diff changeset
68 def update(self):
goffi@necton2
parents:
diff changeset
69 Window.update(self)
goffi@necton2
parents:
diff changeset
70 text = self.__getTextToPrint()
goffi@necton2
parents:
diff changeset
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
goffi@necton2
parents:
diff changeset
73 self.noutrefresh()
goffi@necton2
parents:
diff changeset
74
goffi@necton2
parents:
diff changeset
75 def __dec_cur(self):
goffi@necton2
parents:
diff changeset
76 """move cursor on the left"""
goffi@necton2
parents:
diff changeset
77 if self.__curs_pos>0:
goffi@necton2
parents:
diff changeset
78 self.__curs_pos = self.__curs_pos - 1
goffi@necton2
parents:
diff changeset
79
goffi@necton2
parents:
diff changeset
80 def __inc_cur(self):
goffi@necton2
parents:
diff changeset
81 """move cursor on the right"""
goffi@necton2
parents:
diff changeset
82 if self.__curs_pos<len(self.__text):
goffi@necton2
parents:
diff changeset
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
goffi@necton2
parents:
diff changeset
85 def move_cur(self, x):
goffi@necton2
parents:
diff changeset
86 pos = x+len(self.__header)
goffi@necton2
parents:
diff changeset
87 if pos>=self.rWidth:
goffi@necton2
parents:
diff changeset
88 pos=self.rWidth-1
goffi@necton2
parents:
diff changeset
89 self.move(0, pos)
goffi@necton2
parents:
diff changeset
90
goffi@necton2
parents:
diff changeset
91 def clear_text(self):
goffi@necton2
parents:
diff changeset
92 """Clear the text of the edit box"""
goffi@necton2
parents:
diff changeset
93 self.__text=""
goffi@necton2
parents:
diff changeset
94 self.__curs_pos=0
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 480
diff changeset
95
0
goffi@necton2
parents:
diff changeset
96 def replace_cur(self):
goffi@necton2
parents:
diff changeset
97 """must be called earch time the cursor is moved"""
goffi@necton2
parents:
diff changeset
98 self.move_cur(self.__curs_pos)
goffi@necton2
parents:
diff changeset
99 self.noutrefresh()
goffi@necton2
parents:
diff changeset
100
goffi@necton2
parents:
diff changeset
101 def activate(self, state=True):
goffi@necton2
parents:
diff changeset
102 cursor_mode = 1 if state else 0
goffi@necton2
parents:
diff changeset
103 curses.curs_set(cursor_mode)
goffi@necton2
parents:
diff changeset
104 Window.activate(self,state)
goffi@necton2
parents:
diff changeset
105
goffi@necton2
parents:
diff changeset
106 def handleKey(self, k):
goffi@necton2
parents:
diff changeset
107 if ascii.isgraph(k) or ascii.isblank(k):
goffi@necton2
parents:
diff changeset
108 pacman = 0 if not self.__replace_mode else 1
goffi@necton2
parents:
diff changeset
109 self.__text = self.__text[:self.__curs_pos] + chr(k) + self.__text[self.__curs_pos + pacman:]
goffi@necton2
parents:
diff changeset
110 self.__curs_pos = self.__curs_pos + 1
goffi@necton2
parents:
diff changeset
111
goffi@necton2
parents:
diff changeset
112 elif k==ascii.NL:
goffi@necton2
parents:
diff changeset
113 try:
goffi@necton2
parents:
diff changeset
114 self.__enterCB(self.__text)
goffi@necton2
parents:
diff changeset
115 except NameError:
goffi@necton2
parents:
diff changeset
116 pass # TODO: thrown an error here
goffi@necton2
parents:
diff changeset
117 self.clear_text()
goffi@necton2
parents:
diff changeset
118
goffi@necton2
parents:
diff changeset
119 elif k==curses.KEY_BACKSPACE:
goffi@necton2
parents:
diff changeset
120 self.__text = self.__text[:self.__curs_pos-1]+self.__text[self.__curs_pos:]
goffi@necton2
parents:
diff changeset
121 self.__dec_cur()
goffi@necton2
parents:
diff changeset
122
goffi@necton2
parents:
diff changeset
123 elif k==curses.KEY_DC:
goffi@necton2
parents:
diff changeset
124 self.__text = self.__text[:self.__curs_pos]+self.__text[self.__curs_pos+1:]
goffi@necton2
parents:
diff changeset
125
goffi@necton2
parents:
diff changeset
126 elif k==curses.KEY_IC:
goffi@necton2
parents:
diff changeset
127 self.__replace_mode = not self.__replace_mode
goffi@necton2
parents:
diff changeset
128
goffi@necton2
parents:
diff changeset
129 elif k==curses.KEY_LEFT:
goffi@necton2
parents:
diff changeset
130 self.__dec_cur()
goffi@necton2
parents:
diff changeset
131
goffi@necton2
parents:
diff changeset
132 elif k==curses.KEY_RIGHT:
goffi@necton2
parents:
diff changeset
133 self.__inc_cur()
goffi@necton2
parents:
diff changeset
134
goffi@necton2
parents:
diff changeset
135 elif k==curses.KEY_HOME or k==C('a'):
goffi@necton2
parents:
diff changeset
136 self.__curs_pos=0
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 480
diff changeset
137
0
goffi@necton2
parents:
diff changeset
138 elif k==curses.KEY_END or k==C('e'):
goffi@necton2
parents:
diff changeset
139 self.__curs_pos=len(self.__text)
goffi@necton2
parents:
diff changeset
140
goffi@necton2
parents:
diff changeset
141 elif k==C('k'):
goffi@necton2
parents:
diff changeset
142 self.__text = self.__text[:self.__curs_pos]
goffi@necton2
parents:
diff changeset
143
goffi@necton2
parents:
diff changeset
144 elif k==C('w'):
goffi@necton2
parents:
diff changeset
145 before = self.__text[:self.__curs_pos]
goffi@necton2
parents:
diff changeset
146 pos = before.rstrip().rfind(" ")+1
goffi@necton2
parents:
diff changeset
147 self.__text = before[:pos] + self.__text[self.__curs_pos:]
goffi@necton2
parents:
diff changeset
148 self.__curs_pos = pos
goffi@necton2
parents:
diff changeset
149
goffi@necton2
parents:
diff changeset
150 elif k>255:
goffi@necton2
parents:
diff changeset
151 self.__buffer=""
goffi@necton2
parents:
diff changeset
152
goffi@necton2
parents:
diff changeset
153 else: ## FIXME: dangerous code, must be checked ! (specialy buffer overflow) ##
goffi@necton2
parents:
diff changeset
154 #We now manage unicode
goffi@necton2
parents:
diff changeset
155 self.__buffer = self.__buffer+chr(k)
goffi@necton2
parents:
diff changeset
156 decoded=unicode()
goffi@necton2
parents:
diff changeset
157 if len(self.__buffer)>4:
goffi@necton2
parents:
diff changeset
158 self.__buffer=""
goffi@necton2
parents:
diff changeset
159 return
goffi@necton2
parents:
diff changeset
160 try:
goffi@necton2
parents:
diff changeset
161 decoded = self.__buffer.decode(self.__code)
goffi@necton2
parents:
diff changeset
162 except UnicodeDecodeError, e:
goffi@necton2
parents:
diff changeset
163 if e.reason!="unexpected end of data":
goffi@necton2
parents:
diff changeset
164 self.__buffer=""
goffi@necton2
parents:
diff changeset
165 return
goffi@necton2
parents:
diff changeset
166 if len(self.__buffer)==1: ## FIXME: awful ! only for test !
goffi@necton2
parents:
diff changeset
167 self.__buffer=""
goffi@necton2
parents:
diff changeset
168 return
goffi@necton2
parents:
diff changeset
169 self.__text = self.__text + decoded
goffi@necton2
parents:
diff changeset
170 self.__curs_pos = self.__curs_pos + 1
goffi@necton2
parents:
diff changeset
171 self.__buffer=""
goffi@necton2
parents:
diff changeset
172
goffi@necton2
parents:
diff changeset
173 self.update()
goffi@necton2
parents:
diff changeset
174