author | Radek Brich <radek.brich@devl.cz> |
Sat, 29 Dec 2012 12:16:06 +0100 | |
changeset 41 | 37b7dfc3eae6 |
parent 40 | 5faa38c10b67 |
child 45 | 43b2279b06e1 |
permissions | -rw-r--r-- |
0 | 1 |
# -*- coding: utf-8 -*- |
2 |
||
3 |
import locale |
|
4 |
||
18
e6c3a5ee91aa
Eliminate relative imports.
Radek Brich <radek.brich@devl.cz>
parents:
10
diff
changeset
|
5 |
from tuikit.widget import Widget |
0 | 6 |
|
7 |
||
8 |
class EditField(Widget): |
|
9 |
def __init__(self, width=10, value=''): |
|
10 |
Widget.__init__(self, width, 1) |
|
11 |
||
34
e3beacd5e536
Update event propagation, keypress event, focusing.
Radek Brich <radek.brich@devl.cz>
parents:
30
diff
changeset
|
12 |
self.allow_focus = True |
e3beacd5e536
Update event propagation, keypress event, focusing.
Radek Brich <radek.brich@devl.cz>
parents:
30
diff
changeset
|
13 |
|
0 | 14 |
self.code = locale.getpreferredencoding() |
10
ec1d47e6fe09
Use isinstance instead of type.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
15 |
if not isinstance(value, str): |
0 | 16 |
value = str(value, self.code) |
17 |
||
18 |
self.value = value |
|
19 |
self.maxlen = None # unlimited |
|
20 |
||
8
fcaabd817774
Update editfield - place cursor to end initially.
Radek Brich <radek.brich@devl.cz>
parents:
5
diff
changeset
|
21 |
self.tw = 0 # real width of text field (minus space for arrows) |
fcaabd817774
Update editfield - place cursor to end initially.
Radek Brich <radek.brich@devl.cz>
parents:
5
diff
changeset
|
22 |
self.pos = len(value) # position of cursor in value |
0 | 23 |
self.ofs = 0 # position of value beginning on screen |
24 |
||
41
37b7dfc3eae6
Update Emitter: All event handlers now have exactly one argument: object inherited from Event class, which carries any data.
Radek Brich <radek.brich@devl.cz>
parents:
40
diff
changeset
|
25 |
def _handle_resize(self, ev): |
37b7dfc3eae6
Update Emitter: All event handlers now have exactly one argument: object inherited from Event class, which carries any data.
Radek Brich <radek.brich@devl.cz>
parents:
40
diff
changeset
|
26 |
super()._handle_resize(ev) |
8
fcaabd817774
Update editfield - place cursor to end initially.
Radek Brich <radek.brich@devl.cz>
parents:
5
diff
changeset
|
27 |
self.tw = self.width - 2 |
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
|
28 |
|
41
37b7dfc3eae6
Update Emitter: All event handlers now have exactly one argument: object inherited from Event class, which carries any data.
Radek Brich <radek.brich@devl.cz>
parents:
40
diff
changeset
|
29 |
def _handle_draw(self, ev): |
37b7dfc3eae6
Update Emitter: All event handlers now have exactly one argument: object inherited from Event class, which carries any data.
Radek Brich <radek.brich@devl.cz>
parents:
40
diff
changeset
|
30 |
super()._handle_draw(ev) |
37b7dfc3eae6
Update Emitter: All event handlers now have exactly one argument: object inherited from Event class, which carries any data.
Radek Brich <radek.brich@devl.cz>
parents:
40
diff
changeset
|
31 |
ev.driver.pushcolor('normal') |
0 | 32 |
# draw value |
33 |
val = self.value + ' ' * self.tw # add spaces to fill rest of field |
|
34 |
val = val[self.ofs : self.ofs + self.tw] # cut value - begin from ofs, limit to tw chars |
|
41
37b7dfc3eae6
Update Emitter: All event handlers now have exactly one argument: object inherited from Event class, which carries any data.
Radek Brich <radek.brich@devl.cz>
parents:
40
diff
changeset
|
35 |
ev.driver.puts(ev.x + 1, ev.y, val.encode(self.code)) |
0 | 36 |
|
37 |
# draw arrows if content overflows |
|
38 |
c = ' ' |
|
39 |
if self.ofs > 0: |
|
40 |
c = '<' |
|
41
37b7dfc3eae6
Update Emitter: All event handlers now have exactly one argument: object inherited from Event class, which carries any data.
Radek Brich <radek.brich@devl.cz>
parents:
40
diff
changeset
|
41 |
ev.driver.putch(ev.x, ev.y, c) |
0 | 42 |
|
43 |
c = ' ' |
|
44 |
if len(self.value[self.ofs:]) > self.tw: |
|
45 |
c = '>' |
|
41
37b7dfc3eae6
Update Emitter: All event handlers now have exactly one argument: object inherited from Event class, which carries any data.
Radek Brich <radek.brich@devl.cz>
parents:
40
diff
changeset
|
46 |
ev.driver.putch(ev.x + self.width-1, ev.y, c) |
0 | 47 |
|
8
fcaabd817774
Update editfield - place cursor to end initially.
Radek Brich <radek.brich@devl.cz>
parents:
5
diff
changeset
|
48 |
self.cursor = (1 + self.pos - self.ofs, 0) |
41
37b7dfc3eae6
Update Emitter: All event handlers now have exactly one argument: object inherited from Event class, which carries any data.
Radek Brich <radek.brich@devl.cz>
parents:
40
diff
changeset
|
49 |
ev.driver.popcolor() |
0 | 50 |
|
41
37b7dfc3eae6
Update Emitter: All event handlers now have exactly one argument: object inherited from Event class, which carries any data.
Radek Brich <radek.brich@devl.cz>
parents:
40
diff
changeset
|
51 |
def _handle_keypress(self, ev): |
37b7dfc3eae6
Update Emitter: All event handlers now have exactly one argument: object inherited from Event class, which carries any data.
Radek Brich <radek.brich@devl.cz>
parents:
40
diff
changeset
|
52 |
super()._handle_keypress(ev) |
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:
8
diff
changeset
|
53 |
handled = False |
41
37b7dfc3eae6
Update Emitter: All event handlers now have exactly one argument: object inherited from Event class, which carries any data.
Radek Brich <radek.brich@devl.cz>
parents:
40
diff
changeset
|
54 |
if ev.keyname: |
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:
8
diff
changeset
|
55 |
handled = True |
41
37b7dfc3eae6
Update Emitter: All event handlers now have exactly one argument: object inherited from Event class, which carries any data.
Radek Brich <radek.brich@devl.cz>
parents:
40
diff
changeset
|
56 |
if ev.keyname == 'left': |
0 | 57 |
self.move_left() |
58 |
||
41
37b7dfc3eae6
Update Emitter: All event handlers now have exactly one argument: object inherited from Event class, which carries any data.
Radek Brich <radek.brich@devl.cz>
parents:
40
diff
changeset
|
59 |
elif ev.keyname == 'right': |
0 | 60 |
self.move_right() |
61 |
||
41
37b7dfc3eae6
Update Emitter: All event handlers now have exactly one argument: object inherited from Event class, which carries any data.
Radek Brich <radek.brich@devl.cz>
parents:
40
diff
changeset
|
62 |
elif ev.keyname == 'backspace': |
0 | 63 |
if self.pos > 0: |
64 |
self.move_left() |
|
65 |
self.del_char() |
|
66 |
||
41
37b7dfc3eae6
Update Emitter: All event handlers now have exactly one argument: object inherited from Event class, which carries any data.
Radek Brich <radek.brich@devl.cz>
parents:
40
diff
changeset
|
67 |
elif ev.keyname == 'delete': |
0 | 68 |
self.del_char() |
69 |
||
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:
8
diff
changeset
|
70 |
else: |
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:
8
diff
changeset
|
71 |
handled = False |
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:
8
diff
changeset
|
72 |
|
41
37b7dfc3eae6
Update Emitter: All event handlers now have exactly one argument: object inherited from Event class, which carries any data.
Radek Brich <radek.brich@devl.cz>
parents:
40
diff
changeset
|
73 |
if ev.char: |
37b7dfc3eae6
Update Emitter: All event handlers now have exactly one argument: object inherited from Event class, which carries any data.
Radek Brich <radek.brich@devl.cz>
parents:
40
diff
changeset
|
74 |
self.add_char(ev.char) |
0 | 75 |
self.move_right() |
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:
8
diff
changeset
|
76 |
handled = True |
0 | 77 |
|
78 |
self.redraw() |
|
79 |
||
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:
8
diff
changeset
|
80 |
return handled |
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:
8
diff
changeset
|
81 |
|
0 | 82 |
def move_left(self): |
8
fcaabd817774
Update editfield - place cursor to end initially.
Radek Brich <radek.brich@devl.cz>
parents:
5
diff
changeset
|
83 |
if self.pos - self.ofs > 1 or (self.ofs == 0 and self.pos == 1): |
0 | 84 |
# move cursor |
85 |
self.pos -= 1 |
|
86 |
else: |
|
87 |
# move content in field |
|
8
fcaabd817774
Update editfield - place cursor to end initially.
Radek Brich <radek.brich@devl.cz>
parents:
5
diff
changeset
|
88 |
if self.ofs > 0: |
0 | 89 |
self.pos -= 1 |
90 |
self.ofs -= 1 |
|
91 |
||
92 |
def move_right(self): |
|
93 |
if self.pos < len(self.value): |
|
8
fcaabd817774
Update editfield - place cursor to end initially.
Radek Brich <radek.brich@devl.cz>
parents:
5
diff
changeset
|
94 |
if self.pos - self.ofs < self.tw - 2 \ |
fcaabd817774
Update editfield - place cursor to end initially.
Radek Brich <radek.brich@devl.cz>
parents:
5
diff
changeset
|
95 |
or (self.pos - self.ofs == self.tw - 2 and self.pos == len(self.value)-1): |
0 | 96 |
# move cursor |
97 |
self.pos += 1 |
|
98 |
else: |
|
99 |
# move content in field |
|
100 |
self.pos += 1 |
|
101 |
self.ofs += 1 |
|
102 |
||
103 |
def add_char(self, c): |
|
104 |
self.value = self.value[:self.pos] + c + self.value[self.pos:] |
|
105 |
||
106 |
def del_char(self): |
|
107 |
self.value = self.value[:self.pos] + self.value[self.pos+1:] |
|
108 |