annotate frontends/sortilege_old/editbox.py @ 344:f19771d2e63b

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