author | Radek Brich <radek.brich@devl.cz> |
Sun, 10 Apr 2011 22:54:38 +0200 | |
changeset 5 | ae128c885d0f |
parent 0 | a35731b5e31a |
child 8 | fcaabd817774 |
permissions | -rw-r--r-- |
0 | 1 |
# -*- coding: utf-8 -*- |
2 |
||
3 |
import locale |
|
4 |
||
5 |
from .widget import Widget |
|
6 |
||
7 |
||
8 |
class EditField(Widget): |
|
9 |
def __init__(self, width=10, value=''): |
|
10 |
Widget.__init__(self, width, 1) |
|
11 |
||
12 |
self.code = locale.getpreferredencoding() |
|
13 |
if not type(value) is str: |
|
14 |
value = str(value, self.code) |
|
15 |
||
16 |
self.value = value |
|
17 |
self.maxlen = None # unlimited |
|
18 |
||
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:
0
diff
changeset
|
19 |
self.tw = 0 |
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:
0
diff
changeset
|
20 |
self.cur = 0 # position of cursor on screen |
0 | 21 |
self.pos = 0 # position of cursor in value |
22 |
self.ofs = 0 # position of value beginning on screen |
|
23 |
||
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:
0
diff
changeset
|
24 |
self.connect('resize', self.on_resize) |
0 | 25 |
self.connect('draw', self.on_draw) |
26 |
self.connect('keypress', self.on_keypress) |
|
27 |
||
28 |
||
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:
0
diff
changeset
|
29 |
def on_resize(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:
0
diff
changeset
|
30 |
self.tw = self.width - 2 # real width of text field (minus space for arrows) |
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:
0
diff
changeset
|
31 |
|
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:
0
diff
changeset
|
32 |
|
0 | 33 |
def on_draw(self, screen, x, y): |
34 |
# draw value |
|
35 |
val = self.value + ' ' * self.tw # add spaces to fill rest of field |
|
36 |
val = val[self.ofs : self.ofs + self.tw] # cut value - begin from ofs, limit to tw chars |
|
37 |
screen.puts(x + 1, y, val.encode(self.code)) |
|
38 |
||
39 |
# draw arrows if content overflows |
|
40 |
c = ' ' |
|
41 |
if self.ofs > 0: |
|
42 |
c = '<' |
|
43 |
screen.putch(x, y, c) |
|
44 |
||
45 |
c = ' ' |
|
46 |
if len(self.value[self.ofs:]) > self.tw: |
|
47 |
c = '>' |
|
48 |
screen.putch(x + self.width-1, y, c) |
|
49 |
||
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:
0
diff
changeset
|
50 |
self.cursor = (1 + self.cur, 0) |
0 | 51 |
|
52 |
||
53 |
def on_keypress(self, keyname, char): |
|
54 |
if keyname: |
|
55 |
if keyname == 'left': |
|
56 |
self.move_left() |
|
57 |
||
58 |
if keyname == 'right': |
|
59 |
self.move_right() |
|
60 |
||
61 |
if keyname == 'backspace': |
|
62 |
if self.pos > 0: |
|
63 |
self.move_left() |
|
64 |
self.del_char() |
|
65 |
||
66 |
if keyname == 'delete': |
|
67 |
self.del_char() |
|
68 |
||
69 |
if char: |
|
70 |
self.add_char(char) |
|
71 |
self.move_right() |
|
72 |
||
73 |
self.redraw() |
|
74 |
||
75 |
||
76 |
def move_left(self): |
|
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:
0
diff
changeset
|
77 |
if self.cur > 1 or (self.cur == 1 and self.pos == 1): |
0 | 78 |
# move cursor |
79 |
self.pos -= 1 |
|
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:
0
diff
changeset
|
80 |
self.cur -= 1 |
0 | 81 |
else: |
82 |
# move content in field |
|
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:
0
diff
changeset
|
83 |
if self.pos > self.cur: |
0 | 84 |
self.pos -= 1 |
85 |
self.ofs -= 1 |
|
86 |
||
87 |
||
88 |
def move_right(self): |
|
89 |
if self.pos < len(self.value): |
|
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:
0
diff
changeset
|
90 |
if self.cur < self.tw - 2 \ |
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:
0
diff
changeset
|
91 |
or (self.cur == self.tw - 2 and self.pos == len(self.value)-1): |
0 | 92 |
# move cursor |
93 |
self.pos += 1 |
|
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:
0
diff
changeset
|
94 |
self.cur += 1 |
0 | 95 |
else: |
96 |
# move content in field |
|
97 |
self.pos += 1 |
|
98 |
self.ofs += 1 |
|
99 |
||
100 |
||
101 |
def add_char(self, c): |
|
102 |
self.value = self.value[:self.pos] + c + self.value[self.pos:] |
|
103 |
||
104 |
||
105 |
def del_char(self): |
|
106 |
self.value = self.value[:self.pos] + self.value[self.pos+1:] |
|
107 |