author | Radek Brich <radek.brich@devl.cz> |
Fri, 07 Oct 2011 11:07:59 +0200 | |
changeset 18 | e6c3a5ee91aa |
parent 17 | 5be7cc43402e |
child 20 | 472a753664f9 |
permissions | -rw-r--r-- |
0 | 1 |
# -*- coding: utf-8 -*- |
2 |
||
3 |
import curses |
|
4 |
import curses.ascii |
|
5 |
import locale |
|
6 |
import logging |
|
7 |
||
18
e6c3a5ee91aa
Eliminate relative imports.
Radek Brich <radek.brich@devl.cz>
parents:
17
diff
changeset
|
8 |
from tuikit.common import Rect |
0 | 9 |
|
10 |
||
11 |
class MouseEvent: |
|
12 |
def __init__(self, x=0, y=0): |
|
13 |
self.x = x # global coordinates |
|
14 |
self.y = y |
|
15 |
self.wx = x # local widget coordinates |
|
16 |
self.wy = y |
|
17 |
self.px = 0 # parent coordinates |
|
18 |
self.py = 0 |
|
19 |
self.button = 0 |
|
20 |
||
21 |
||
22 |
def childevent(self, child): |
|
23 |
ev = MouseEvent(self.x, self.y) |
|
24 |
# original local coordinates are new parent coordinates |
|
25 |
ev.px = self.wx |
|
26 |
ev.py = self.wy |
|
27 |
# update local coordinates |
|
28 |
ev.wx = self.wx - child.x |
|
29 |
ev.wy = self.wy - child.y |
|
30 |
||
31 |
return ev |
|
32 |
||
33 |
||
34 |
class BackendCurses: |
|
35 |
xterm_codes = ( |
|
36 |
(0x09, 'tab' ), |
|
37 |
(0x0a, 'enter' ), |
|
38 |
(0x7f, 'backspace' ), |
|
4
d197ca00496f
Add escape sequences for linux console.
Radek Brich <radek.brich@devl.cz>
parents:
3
diff
changeset
|
39 |
(0x1b, 'escape' ), |
0 | 40 |
(0x1b,0x4f,0x50, 'f1' ), |
41 |
(0x1b,0x4f,0x51, 'f2' ), |
|
42 |
(0x1b,0x4f,0x52, 'f3' ), |
|
43 |
(0x1b,0x4f,0x53, 'f4' ), |
|
44 |
(0x1b,0x5b,0x31,0x35,0x7e, 'f5' ), |
|
45 |
(0x1b,0x5b,0x31,0x37,0x7e, 'f6' ), |
|
46 |
(0x1b,0x5b,0x31,0x38,0x7e, 'f7' ), |
|
47 |
(0x1b,0x5b,0x31,0x39,0x7e, 'f8' ), |
|
4
d197ca00496f
Add escape sequences for linux console.
Radek Brich <radek.brich@devl.cz>
parents:
3
diff
changeset
|
48 |
(0x1b,0x5b,0x31,0x7e, 'home' ), # linux |
0 | 49 |
(0x1b,0x5b,0x32,0x30,0x7e, 'f9' ), |
50 |
(0x1b,0x5b,0x32,0x31,0x7e, 'f10' ), |
|
51 |
(0x1b,0x5b,0x32,0x33,0x7e, 'f11' ), |
|
52 |
(0x1b,0x5b,0x32,0x34,0x7e, 'f12' ), |
|
53 |
(0x1b,0x5b,0x32,0x7e, 'insert' ), |
|
54 |
(0x1b,0x5b,0x33,0x7e, 'delete' ), |
|
4
d197ca00496f
Add escape sequences for linux console.
Radek Brich <radek.brich@devl.cz>
parents:
3
diff
changeset
|
55 |
(0x1b,0x5b,0x34,0x7e, 'end' ), # linux |
0 | 56 |
(0x1b,0x5b,0x35,0x7e, 'pageup' ), |
57 |
(0x1b,0x5b,0x36,0x7e, 'pagedown' ), |
|
4
d197ca00496f
Add escape sequences for linux console.
Radek Brich <radek.brich@devl.cz>
parents:
3
diff
changeset
|
58 |
(0x1b,0x5b,0x41, 'up' ), |
d197ca00496f
Add escape sequences for linux console.
Radek Brich <radek.brich@devl.cz>
parents:
3
diff
changeset
|
59 |
(0x1b,0x5b,0x42, 'down' ), |
d197ca00496f
Add escape sequences for linux console.
Radek Brich <radek.brich@devl.cz>
parents:
3
diff
changeset
|
60 |
(0x1b,0x5b,0x43, 'right' ), |
d197ca00496f
Add escape sequences for linux console.
Radek Brich <radek.brich@devl.cz>
parents:
3
diff
changeset
|
61 |
(0x1b,0x5b,0x44, 'left' ), |
0 | 62 |
(0x1b,0x5b,0x46, 'end' ), |
63 |
(0x1b,0x5b,0x48, 'home' ), |
|
4
d197ca00496f
Add escape sequences for linux console.
Radek Brich <radek.brich@devl.cz>
parents:
3
diff
changeset
|
64 |
(0x1b,0x5b,0x4d, 'mouse' ), |
d197ca00496f
Add escape sequences for linux console.
Radek Brich <radek.brich@devl.cz>
parents:
3
diff
changeset
|
65 |
(0x1b,0x5b,0x5b,0x41, 'f1' ), # linux |
d197ca00496f
Add escape sequences for linux console.
Radek Brich <radek.brich@devl.cz>
parents:
3
diff
changeset
|
66 |
(0x1b,0x5b,0x5b,0x42, 'f2' ), # linux |
d197ca00496f
Add escape sequences for linux console.
Radek Brich <radek.brich@devl.cz>
parents:
3
diff
changeset
|
67 |
(0x1b,0x5b,0x5b,0x43, 'f3' ), # linux |
d197ca00496f
Add escape sequences for linux console.
Radek Brich <radek.brich@devl.cz>
parents:
3
diff
changeset
|
68 |
(0x1b,0x5b,0x5b,0x44, 'f4' ), # linux |
d197ca00496f
Add escape sequences for linux console.
Radek Brich <radek.brich@devl.cz>
parents:
3
diff
changeset
|
69 |
(0x1b,0x5b,0x5b,0x45, 'f5' ), # linux |
0 | 70 |
) |
71 |
||
7
d4a291b31cbb
New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents:
5
diff
changeset
|
72 |
color_names = { |
d4a291b31cbb
New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents:
5
diff
changeset
|
73 |
'black' : curses.COLOR_BLACK, |
d4a291b31cbb
New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents:
5
diff
changeset
|
74 |
'blue' : curses.COLOR_BLUE, |
d4a291b31cbb
New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents:
5
diff
changeset
|
75 |
'cyan' : curses.COLOR_CYAN, |
d4a291b31cbb
New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents:
5
diff
changeset
|
76 |
'green' : curses.COLOR_GREEN, |
d4a291b31cbb
New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents:
5
diff
changeset
|
77 |
'magenta' : curses.COLOR_MAGENTA, |
d4a291b31cbb
New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents:
5
diff
changeset
|
78 |
'red' : curses.COLOR_RED, |
d4a291b31cbb
New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents:
5
diff
changeset
|
79 |
'white' : curses.COLOR_WHITE, |
d4a291b31cbb
New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents:
5
diff
changeset
|
80 |
'yellow' : curses.COLOR_YELLOW, |
d4a291b31cbb
New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents:
5
diff
changeset
|
81 |
} |
d4a291b31cbb
New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents:
5
diff
changeset
|
82 |
|
0 | 83 |
def __init__(self, screen): |
84 |
self.screen = screen |
|
85 |
self.height, self.width = screen.getmaxyx() |
|
86 |
||
5
ae128c885d0f
New GridLayout. Change cursor behavior (hide on unfocus event). Change resize event to propagate through containers. Change container clipping - allowlayout=false children are clipped without borders. More Widget doc.
Radek Brich <radek.brich@devl.cz>
parents:
4
diff
changeset
|
87 |
self.cursor = None |
0 | 88 |
self.clipstack = [] |
7
d4a291b31cbb
New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents:
5
diff
changeset
|
89 |
|
d4a291b31cbb
New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents:
5
diff
changeset
|
90 |
self.colors = {} # maps names to curses attributes |
d4a291b31cbb
New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents:
5
diff
changeset
|
91 |
self.colorpairs = {} # maps tuple (fg,bg) to curses color_pair |
d4a291b31cbb
New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents:
5
diff
changeset
|
92 |
self.colorstack = [] # pushcolor/popcolor puts or gets attributes from this |
9
7175ed629a76
Added ComboBox, HorizontalLayout, TreeNode, TreeModel, TreeView. Widget is now descendant of EventSource. Improved color management (color prefixes).
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
93 |
self.colorprefix = [] # stack of color prefixes |
7
d4a291b31cbb
New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents:
5
diff
changeset
|
94 |
|
0 | 95 |
self.inputqueue = [] |
96 |
self.mbtnstack = [] |
|
97 |
||
98 |
self.log = logging.getLogger('tuikit') |
|
99 |
||
100 |
# initialize curses |
|
101 |
curses.curs_set(False) |
|
102 |
curses.mousemask(curses.ALL_MOUSE_EVENTS | curses.REPORT_MOUSE_POSITION) |
|
103 |
curses.mouseinterval(0) # do not wait to detect clicks, we use only press/release |
|
104 |
||
105 |
screen.immedok(0) |
|
106 |
screen.keypad(0) |
|
107 |
||
108 |
# http://en.wikipedia.org/wiki/List_of_Unicode_characters#Geometric_shapes |
|
109 |
self.UP_ARROW = '▲' #curses.ACS_UARROW |
|
110 |
self.DOWN_ARROW = '▼' #curses.ACS_DARROW |
|
111 |
||
112 |
# http://en.wikipedia.org/wiki/Box-drawing_characters |
|
113 |
self.LIGHT_SHADE = '░' #curses.ACS_BOARD |
|
114 |
self.MEDIUM_SHADE = '▒' |
|
115 |
self.DARK_SHADE = '▓' |
|
116 |
self.BLOCK = '█' |
|
117 |
||
118 |
self.COLUMN = '▁▂▃▄▅▆▇█' |
|
119 |
self.CORNER_ROUND = '╭╮╰╯' |
|
120 |
self.CORNER = '┌┐└┘' |
|
121 |
self.LINE = '─━│┃┄┅┆┇┈┉┊┋' |
|
122 |
||
123 |
self.HLINE = '─' # curses.ACS_HLINE |
|
124 |
self.VLINE = '│' # curses.ACS_VLINE |
|
125 |
self.ULCORNER = '┌' # curses.ACS_ULCORNER |
|
126 |
self.URCORNER = '┐' # curses.ACS_URCORNER |
|
127 |
self.LLCORNER = '└' # curses.ACS_LLCORNER |
|
128 |
self.LRCORNER = '┘' # curses.ACS_LRCORNER |
|
129 |
self.LTEE = '├' |
|
130 |
self.RTEE = '┤' |
|
131 |
||
132 |
||
133 |
## clip operations ## |
|
134 |
||
135 |
def pushclip(self, x, y, w, h): |
|
136 |
newclip = Rect(x, y, w, h) |
|
137 |
if len(self.clipstack): |
|
138 |
oldclip = self.clipstack[-1] |
|
139 |
newclip = self.intersect(oldclip, newclip) |
|
140 |
self.clipstack.append(newclip) |
|
141 |
||
142 |
||
143 |
def popclip(self): |
|
144 |
self.clipstack.pop() |
|
145 |
||
146 |
||
147 |
def testclip(self, x, y): |
|
148 |
# no clip rectangle on stack => passed |
|
149 |
if not len(self.clipstack): |
|
150 |
return True |
|
151 |
# test against top clip rect from stack |
|
152 |
clip = self.clipstack[-1] |
|
153 |
if x < clip.x or y < clip.y \ |
|
154 |
or x >= clip.x + clip.w or y >= clip.y + clip.h: |
|
155 |
return False |
|
156 |
# passed |
|
157 |
return True |
|
158 |
||
159 |
||
160 |
def intersect(self, r1, r2): |
|
161 |
x1 = max(r1.x, r2.x) |
|
162 |
y1 = max(r1.y, r2.y) |
|
163 |
x2 = min(r1.x + r1.w, r2.x + r2.w) |
|
164 |
y2 = min(r1.y + r1.h, r2.y + r2.h) |
|
165 |
if x1 >= x2 or y1 >= y2: |
|
166 |
return Rect() |
|
167 |
return Rect(x1, y1, x2-x1, y2-y1) |
|
168 |
||
169 |
||
170 |
def union(self, r1, r2): |
|
171 |
x = min(r1.x, r2.x) |
|
172 |
y = min(r1.y, r2.y) |
|
173 |
w = max(r1.x + r1.w, r2.x + r2.w) - x |
|
174 |
h = max(r1.y + r1.h, r2.y + r2.h) - y |
|
175 |
return Rect(x, y, w, h) |
|
176 |
||
177 |
||
9
7175ed629a76
Added ComboBox, HorizontalLayout, TreeNode, TreeModel, TreeView. Widget is now descendant of EventSource. Improved color management (color prefixes).
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
178 |
## colors, attributes ## |
0 | 179 |
|
7
d4a291b31cbb
New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents:
5
diff
changeset
|
180 |
def _parsecolor(self, name): |
d4a291b31cbb
New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents:
5
diff
changeset
|
181 |
name = name.lower().strip() |
d4a291b31cbb
New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents:
5
diff
changeset
|
182 |
return self.color_names[name] |
d4a291b31cbb
New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents:
5
diff
changeset
|
183 |
|
d4a291b31cbb
New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents:
5
diff
changeset
|
184 |
|
d4a291b31cbb
New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents:
5
diff
changeset
|
185 |
def _getcolorpair(self, fg, bg): |
d4a291b31cbb
New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents:
5
diff
changeset
|
186 |
pair = (fg, bg) |
d4a291b31cbb
New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents:
5
diff
changeset
|
187 |
if pair in self.colorpairs: |
d4a291b31cbb
New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents:
5
diff
changeset
|
188 |
return self.colorpairs[pair] |
d4a291b31cbb
New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents:
5
diff
changeset
|
189 |
num = len(self.colorpairs) + 1 |
d4a291b31cbb
New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents:
5
diff
changeset
|
190 |
curses.init_pair(num, fg, bg) |
d4a291b31cbb
New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents:
5
diff
changeset
|
191 |
self.colorpairs[pair] = num |
d4a291b31cbb
New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents:
5
diff
changeset
|
192 |
return num |
d4a291b31cbb
New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents:
5
diff
changeset
|
193 |
|
d4a291b31cbb
New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents:
5
diff
changeset
|
194 |
|
d4a291b31cbb
New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents:
5
diff
changeset
|
195 |
def _parseattrs(self, attrs): |
d4a291b31cbb
New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents:
5
diff
changeset
|
196 |
res = 0 |
d4a291b31cbb
New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents:
5
diff
changeset
|
197 |
for a in attrs: |
d4a291b31cbb
New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents:
5
diff
changeset
|
198 |
a = a.lower().strip() |
d4a291b31cbb
New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents:
5
diff
changeset
|
199 |
trans = { |
d4a291b31cbb
New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents:
5
diff
changeset
|
200 |
'blink' : curses.A_BLINK, |
d4a291b31cbb
New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents:
5
diff
changeset
|
201 |
'bold' : curses.A_BOLD, |
d4a291b31cbb
New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents:
5
diff
changeset
|
202 |
'dim' : curses.A_DIM, |
d4a291b31cbb
New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents:
5
diff
changeset
|
203 |
'standout' : curses.A_STANDOUT, |
d4a291b31cbb
New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents:
5
diff
changeset
|
204 |
'underline' : curses.A_UNDERLINE, |
d4a291b31cbb
New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents:
5
diff
changeset
|
205 |
} |
d4a291b31cbb
New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents:
5
diff
changeset
|
206 |
res = res | trans[a] |
d4a291b31cbb
New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents:
5
diff
changeset
|
207 |
return res |
d4a291b31cbb
New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents:
5
diff
changeset
|
208 |
|
d4a291b31cbb
New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents:
5
diff
changeset
|
209 |
|
d4a291b31cbb
New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents:
5
diff
changeset
|
210 |
def setcolor(self, name, desc): |
d4a291b31cbb
New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents:
5
diff
changeset
|
211 |
parts = desc.split(',') |
d4a291b31cbb
New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents:
5
diff
changeset
|
212 |
fg, bg = parts[0].split(' on ') |
d4a291b31cbb
New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents:
5
diff
changeset
|
213 |
attrs = parts[1:] |
d4a291b31cbb
New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents:
5
diff
changeset
|
214 |
fg = self._parsecolor(fg) |
d4a291b31cbb
New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents:
5
diff
changeset
|
215 |
bg = self._parsecolor(bg) |
d4a291b31cbb
New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents:
5
diff
changeset
|
216 |
col = self._getcolorpair(fg, bg) |
d4a291b31cbb
New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents:
5
diff
changeset
|
217 |
attr = self._parseattrs(attrs) |
d4a291b31cbb
New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents:
5
diff
changeset
|
218 |
self.colors[name] = curses.color_pair(col) | attr |
d4a291b31cbb
New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents:
5
diff
changeset
|
219 |
|
d4a291b31cbb
New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents:
5
diff
changeset
|
220 |
|
d4a291b31cbb
New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents:
5
diff
changeset
|
221 |
def pushcolor(self, name): |
9
7175ed629a76
Added ComboBox, HorizontalLayout, TreeNode, TreeModel, TreeView. Widget is now descendant of EventSource. Improved color management (color prefixes).
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
222 |
# add prefix if available |
7175ed629a76
Added ComboBox, HorizontalLayout, TreeNode, TreeModel, TreeView. Widget is now descendant of EventSource. Improved color management (color prefixes).
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
223 |
if len(self.colorprefix): |
7175ed629a76
Added ComboBox, HorizontalLayout, TreeNode, TreeModel, TreeView. Widget is now descendant of EventSource. Improved color management (color prefixes).
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
224 |
prefixname = self.colorprefix[-1] + name |
7175ed629a76
Added ComboBox, HorizontalLayout, TreeNode, TreeModel, TreeView. Widget is now descendant of EventSource. Improved color management (color prefixes).
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
225 |
if prefixname in self.colors: |
7175ed629a76
Added ComboBox, HorizontalLayout, TreeNode, TreeModel, TreeView. Widget is now descendant of EventSource. Improved color management (color prefixes).
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
226 |
name = prefixname |
7
d4a291b31cbb
New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents:
5
diff
changeset
|
227 |
attr = self.colors[name] |
d4a291b31cbb
New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents:
5
diff
changeset
|
228 |
self.screen.attrset(attr) |
d4a291b31cbb
New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents:
5
diff
changeset
|
229 |
self.colorstack.append(attr) |
0 | 230 |
|
231 |
||
232 |
def popcolor(self): |
|
233 |
self.colorstack.pop() |
|
234 |
if len(self.colorstack): |
|
7
d4a291b31cbb
New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents:
5
diff
changeset
|
235 |
attr = self.colorstack[-1] |
0 | 236 |
else: |
7
d4a291b31cbb
New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents:
5
diff
changeset
|
237 |
attr = 0 |
d4a291b31cbb
New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents:
5
diff
changeset
|
238 |
self.screen.attrset(attr) |
0 | 239 |
|
240 |
||
9
7175ed629a76
Added ComboBox, HorizontalLayout, TreeNode, TreeModel, TreeView. Widget is now descendant of EventSource. Improved color management (color prefixes).
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
241 |
def pushcolorprefix(self, name): |
7175ed629a76
Added ComboBox, HorizontalLayout, TreeNode, TreeModel, TreeView. Widget is now descendant of EventSource. Improved color management (color prefixes).
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
242 |
self.colorprefix.append(name) |
7175ed629a76
Added ComboBox, HorizontalLayout, TreeNode, TreeModel, TreeView. Widget is now descendant of EventSource. Improved color management (color prefixes).
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
243 |
|
7175ed629a76
Added ComboBox, HorizontalLayout, TreeNode, TreeModel, TreeView. Widget is now descendant of EventSource. Improved color management (color prefixes).
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
244 |
|
7175ed629a76
Added ComboBox, HorizontalLayout, TreeNode, TreeModel, TreeView. Widget is now descendant of EventSource. Improved color management (color prefixes).
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
245 |
def popcolorprefix(self): |
7175ed629a76
Added ComboBox, HorizontalLayout, TreeNode, TreeModel, TreeView. Widget is now descendant of EventSource. Improved color management (color prefixes).
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
246 |
self.colorprefix.pop() |
7175ed629a76
Added ComboBox, HorizontalLayout, TreeNode, TreeModel, TreeView. Widget is now descendant of EventSource. Improved color management (color prefixes).
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
247 |
|
7175ed629a76
Added ComboBox, HorizontalLayout, TreeNode, TreeModel, TreeView. Widget is now descendant of EventSource. Improved color management (color prefixes).
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
248 |
|
0 | 249 |
## drawing ## |
250 |
||
251 |
def putch(self, x, y, c): |
|
252 |
if not self.testclip(x, y): |
|
253 |
return |
|
254 |
try: |
|
10
ec1d47e6fe09
Use isinstance instead of type.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
255 |
if isinstance(c, str) and len(c) == 1: |
0 | 256 |
self.screen.addstr(y, x, c) |
257 |
else: |
|
258 |
self.screen.addch(y, x, c) |
|
259 |
except curses.error: |
|
260 |
pass |
|
261 |
||
262 |
||
263 |
def puts(self, x, y, s): |
|
264 |
for c in s: |
|
265 |
self.putch(x, y, c) |
|
266 |
x += 1 |
|
267 |
||
268 |
||
269 |
def hline(self, x, y, w, c=' '): |
|
10
ec1d47e6fe09
Use isinstance instead of type.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
270 |
if isinstance(c, str): |
0 | 271 |
s = c*w |
272 |
else: |
|
273 |
s = [c]*w |
|
274 |
self.puts(x, y, s) |
|
275 |
||
276 |
||
277 |
def vline(self, x, y, h, c=' '): |
|
278 |
for i in range(h): |
|
279 |
self.putch(x, y+i, c) |
|
280 |
||
281 |
||
282 |
def frame(self, x, y, w, h): |
|
283 |
self.putch(x, y, self.ULCORNER) |
|
284 |
self.putch(x+w-1, y, self.URCORNER) |
|
285 |
self.putch(x, y+h-1, self.LLCORNER) |
|
286 |
self.putch(x+w-1, y+h-1, self.LRCORNER) |
|
287 |
self.hline(x+1, y, w-2, self.HLINE) |
|
288 |
self.hline(x+1, y+h-1, w-2, self.HLINE) |
|
289 |
self.vline(x, y+1, h-2, self.VLINE) |
|
290 |
self.vline(x+w-1, y+1, h-2, self.VLINE) |
|
291 |
||
292 |
||
293 |
def fill(self, x, y, w, h, c=' '): |
|
294 |
for i in range(h): |
|
295 |
self.hline(x, y + i, w, c) |
|
296 |
||
297 |
||
298 |
def erase(self): |
|
299 |
self.screen.erase() |
|
300 |
||
301 |
||
302 |
def commit(self): |
|
303 |
if self.cursor: |
|
304 |
self.screen.move(*self.cursor) |
|
305 |
curses.curs_set(True) |
|
306 |
else: |
|
307 |
curses.curs_set(False) |
|
308 |
self.screen.refresh() |
|
309 |
||
310 |
||
311 |
## cursor ## |
|
312 |
||
313 |
def showcursor(self, x, y): |
|
314 |
if not self.testclip(x, y): |
|
315 |
return |
|
316 |
self.cursor = (y, x) |
|
7
d4a291b31cbb
New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents:
5
diff
changeset
|
317 |
|
d4a291b31cbb
New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents:
5
diff
changeset
|
318 |
|
5
ae128c885d0f
New GridLayout. Change cursor behavior (hide on unfocus event). Change resize event to propagate through containers. Change container clipping - allowlayout=false children are clipped without borders. More Widget doc.
Radek Brich <radek.brich@devl.cz>
parents:
4
diff
changeset
|
319 |
def hidecursor(self): |
ae128c885d0f
New GridLayout. Change cursor behavior (hide on unfocus event). Change resize event to propagate through containers. Change container clipping - allowlayout=false children are clipped without borders. More Widget doc.
Radek Brich <radek.brich@devl.cz>
parents:
4
diff
changeset
|
320 |
curses.curs_set(False) |
ae128c885d0f
New GridLayout. Change cursor behavior (hide on unfocus event). Change resize event to propagate through containers. Change container clipping - allowlayout=false children are clipped without borders. More Widget doc.
Radek Brich <radek.brich@devl.cz>
parents:
4
diff
changeset
|
321 |
self.cursor = None |
0 | 322 |
|
323 |
||
324 |
## input ## |
|
325 |
||
3
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
326 |
def inputqueue_fill(self, timeout=None): |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
327 |
if timeout is None: |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
328 |
# wait indefinitely |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
329 |
c = self.screen.getch() |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
330 |
self.inputqueue.insert(0, c) |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
331 |
|
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
332 |
elif timeout > 0: |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
333 |
# wait |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
334 |
curses.halfdelay(timeout) |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
335 |
c = self.screen.getch() |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
336 |
curses.cbreak() |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
337 |
if c == -1: |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
338 |
return |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
339 |
self.inputqueue.insert(0, c) |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
340 |
|
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
341 |
# timeout = 0 -> no wait |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
342 |
|
0 | 343 |
self.screen.nodelay(1) |
344 |
||
345 |
while True: |
|
346 |
c = self.screen.getch() |
|
347 |
if c == -1: |
|
348 |
break |
|
349 |
self.inputqueue.insert(0, c) |
|
350 |
||
351 |
self.screen.nodelay(0) |
|
352 |
||
353 |
||
3
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
354 |
def inputqueue_top(self, num=0): |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
355 |
return self.inputqueue[-1-num] |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
356 |
|
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
357 |
|
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
358 |
def inputqueue_get(self): |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
359 |
c = None |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
360 |
try: |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
361 |
c = self.inputqueue.pop() |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
362 |
except IndexError: |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
363 |
pass |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
364 |
return c |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
365 |
|
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
366 |
|
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
367 |
def inputqueue_get_wait(self): |
0 | 368 |
c = None |
369 |
while c is None: |
|
370 |
try: |
|
371 |
c = self.inputqueue.pop() |
|
372 |
except IndexError: |
|
373 |
curses.napms(25) |
|
3
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
374 |
self.inputqueue_fill(0) |
0 | 375 |
return c |
376 |
||
377 |
||
3
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
378 |
def inputqueue_unget(self, c): |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
379 |
self.inputqueue.append(c) |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
380 |
|
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
381 |
|
0 | 382 |
def process_input(self, timeout=None): |
3
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
383 |
# empty queue -> fill |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
384 |
if len(self.inputqueue) == 0: |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
385 |
self.inputqueue_fill(timeout) |
0 | 386 |
|
3
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
387 |
res = [] |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
388 |
while len(self.inputqueue): |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
389 |
c = self.inputqueue_get() |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
390 |
|
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
391 |
if c == curses.KEY_MOUSE: |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
392 |
res += self.process_mouse() |
17
5be7cc43402e
Handle curses resize event.
Radek Brich <radek.brich@devl.cz>
parents:
10
diff
changeset
|
393 |
|
5be7cc43402e
Handle curses resize event.
Radek Brich <radek.brich@devl.cz>
parents:
10
diff
changeset
|
394 |
elif c == curses.KEY_RESIZE: |
5be7cc43402e
Handle curses resize event.
Radek Brich <radek.brich@devl.cz>
parents:
10
diff
changeset
|
395 |
self.height, self.width = self.screen.getmaxyx() |
5be7cc43402e
Handle curses resize event.
Radek Brich <radek.brich@devl.cz>
parents:
10
diff
changeset
|
396 |
res.append(('resize',)) |
0 | 397 |
|
3
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
398 |
elif curses.ascii.isctrl(c): |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
399 |
self.inputqueue_unget(c) |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
400 |
res += self.process_control_chars() |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
401 |
|
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
402 |
elif c >= 192 and c <= 255: |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
403 |
self.inputqueue_unget(c) |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
404 |
res += self.process_utf8_chars() |
0 | 405 |
|
3
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
406 |
elif curses.ascii.isprint(c): |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
407 |
res += [('keypress', None, str(chr(c)))] |
0 | 408 |
|
3
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
409 |
else: |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
410 |
#self.top.keypress(None, unicode(chr(c))) |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
411 |
self.inputqueue_unget(c) |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
412 |
res += self.process_control_chars() |
0 | 413 |
|
3
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
414 |
return res |
0 | 415 |
|
416 |
||
417 |
def process_mouse(self): |
|
4
d197ca00496f
Add escape sequences for linux console.
Radek Brich <radek.brich@devl.cz>
parents:
3
diff
changeset
|
418 |
try: |
d197ca00496f
Add escape sequences for linux console.
Radek Brich <radek.brich@devl.cz>
parents:
3
diff
changeset
|
419 |
id, x, y, z, bstate = curses.getmouse() |
d197ca00496f
Add escape sequences for linux console.
Radek Brich <radek.brich@devl.cz>
parents:
3
diff
changeset
|
420 |
except curses.error: |
d197ca00496f
Add escape sequences for linux console.
Radek Brich <radek.brich@devl.cz>
parents:
3
diff
changeset
|
421 |
return [] |
d197ca00496f
Add escape sequences for linux console.
Radek Brich <radek.brich@devl.cz>
parents:
3
diff
changeset
|
422 |
|
0 | 423 |
ev = MouseEvent(x, y) |
424 |
||
425 |
out = [] |
|
426 |
||
427 |
if bstate & curses.REPORT_MOUSE_POSITION: |
|
428 |
out += [('mousemove', ev)] |
|
429 |
||
430 |
if bstate & curses.BUTTON1_PRESSED: |
|
431 |
ev.button = 1 |
|
432 |
out += [('mousedown', ev)] |
|
433 |
||
434 |
if bstate & curses.BUTTON3_PRESSED: |
|
435 |
ev.button = 3 |
|
436 |
out += [('mousedown', ev)] |
|
437 |
||
438 |
if bstate & curses.BUTTON1_RELEASED: |
|
439 |
ev.button = 1 |
|
440 |
out += [('mouseup', ev)] |
|
441 |
||
442 |
if bstate & curses.BUTTON3_RELEASED: |
|
443 |
ev.button = 3 |
|
444 |
out += [('mouseup', ev)] |
|
445 |
||
446 |
return out |
|
447 |
||
448 |
||
449 |
def process_utf8_chars(self): |
|
450 |
#FIXME read exact number of chars as defined by utf-8 |
|
451 |
utf = '' |
|
452 |
while len(utf) <= 6: |
|
3
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
453 |
c = self.inputqueue_get_wait() |
0 | 454 |
utf += chr(c) |
455 |
try: |
|
456 |
uni = str(utf, 'utf-8') |
|
457 |
return [('keypress', None, uni)] |
|
458 |
except UnicodeDecodeError: |
|
459 |
continue |
|
460 |
raise Exception('Invalid UTF-8 sequence: %r' % utf) |
|
461 |
||
462 |
||
463 |
def process_control_chars(self): |
|
3
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
464 |
codes = self.xterm_codes |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
465 |
matchingcodes = [] |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
466 |
match = None |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
467 |
consumed = [] |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
468 |
|
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
469 |
# consume next char, filter out matching codes |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
470 |
c = self.inputqueue_get_wait() |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
471 |
consumed.append(c) |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
472 |
|
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
473 |
while True: |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
474 |
self.log.debug('c=%s len=%s', c, len(codes)) |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
475 |
for code in codes: |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
476 |
if c == code[len(consumed)-1]: |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
477 |
if len(code) - 1 == len(consumed): |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
478 |
match = code |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
479 |
else: |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
480 |
matchingcodes += [code] |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
481 |
|
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
482 |
self.log.debug('matching=%s', len(matchingcodes)) |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
483 |
|
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
484 |
# match found, or no matching code found -> stop |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
485 |
if len(matchingcodes) == 0: |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
486 |
break |
0 | 487 |
|
3
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
488 |
# match found and some sequencies still match -> continue |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
489 |
if len(matchingcodes) > 0: |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
490 |
if len(self.inputqueue) == 0: |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
491 |
self.inputqueue_fill(1) |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
492 |
|
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
493 |
c = self.inputqueue_get() |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
494 |
if c: |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
495 |
consumed.append(c) |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
496 |
codes = matchingcodes |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
497 |
matchingcodes = [] |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
498 |
else: |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
499 |
break |
0 | 500 |
|
3
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
501 |
keyname = None |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
502 |
if match: |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
503 |
# compare match to consumed, return unused chars |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
504 |
l = len(match) - 1 |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
505 |
while len(consumed) > l: |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
506 |
self.inputqueue_unget(consumed[-1]) |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
507 |
del consumed[-1] |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
508 |
keyname = match[-1] |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
509 |
|
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
510 |
if match is None: |
0 | 511 |
self.log.debug('Unknown control sequence: %s', |
3
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
512 |
','.join(['0x%x'%x for x in consumed])) |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
513 |
return [('keypress', 'Unknown', None)] |
0 | 514 |
|
515 |
if keyname == 'mouse': |
|
516 |
return self.process_xterm_mouse() |
|
517 |
||
518 |
return [('keypress', keyname, None)] |
|
519 |
||
520 |
||
521 |
def process_xterm_mouse(self): |
|
3
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
522 |
t = self.inputqueue_get_wait() |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
523 |
x = self.inputqueue_get_wait() - 0x21 |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
524 |
y = self.inputqueue_get_wait() - 0x21 |
0 | 525 |
|
526 |
ev = MouseEvent(x, y) |
|
527 |
out = [] |
|
528 |
||
529 |
if t in (0x20, 0x21, 0x22): # button press |
|
530 |
btn = t - 0x1f |
|
531 |
ev.button = btn |
|
532 |
if not btn in self.mbtnstack: |
|
533 |
self.mbtnstack.append(btn) |
|
534 |
out += [('mousedown', ev)] |
|
535 |
else: |
|
536 |
out += [('mousemove', ev)] |
|
537 |
||
538 |
elif t == 0x23: # button release |
|
539 |
ev.button = self.mbtnstack.pop() |
|
540 |
out += [('mouseup', ev)] |
|
541 |
||
542 |
elif t in (0x60, 0x61): # wheel up, down |
|
543 |
ev.button = 4 + t - 0x60 |
|
544 |
out += [('mousewheel', ev)] |
|
545 |
||
546 |
else: |
|
547 |
raise Exception('Unknown mouse event: %x' % t) |
|
548 |
||
549 |
return out |