author | Radek Brich <radek.brich@devl.cz> |
Sun, 16 Dec 2012 20:49:54 +0100 | |
changeset 34 | e3beacd5e536 |
parent 32 | 088b92ffb119 |
child 40 | 5faa38c10b67 |
permissions | -rw-r--r-- |
0 | 1 |
# -*- coding: utf-8 -*- |
2 |
||
18
e6c3a5ee91aa
Eliminate relative imports.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
3 |
from tuikit.widget import Widget |
0 | 4 |
|
5 |
||
6 |
class VScrollbar(Widget): |
|
7 |
def __init__(self, height=10): |
|
8 |
Widget.__init__(self, 1, height) |
|
9 |
||
10 |
self.max = height - 3 |
|
11 |
self.pos = 0 |
|
12 |
self.thumbpos = 0 |
|
13 |
||
14 |
self.interval = 0.1 |
|
15 |
||
16 |
self.dragging = False |
|
17 |
self.move = None |
|
18 |
||
32
088b92ffb119
Clean up, refactoring. Rename EventSource to Emitter, begin merging emit() method with handle().
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
19 |
self.add_events('change') |
0 | 20 |
|
21 |
||
22 |
def setpos(self, pos): |
|
23 |
self.pos = pos |
|
3
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
24 |
self.thumbpos = 0 |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
25 |
if self.max and self.pos <= self.max: |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
26 |
self.thumbpos = int(round(self.pos / self.max * (self.height - 3))) |
0 | 27 |
|
28 |
||
29 |
def on_draw(self, screen, x, y): |
|
34
e3beacd5e536
Update event propagation, keypress event, focusing.
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
30 |
super().on_draw(screen, x, y) |
24
b248ef500557
Add DriverPygame (incomplete). Move unicode graphics constants to UnicodeGraphics class. Move shared parts of drivers to Driver base class.
Radek Brich <radek.brich@devl.cz>
parents:
18
diff
changeset
|
31 |
screen.putch(x, y, screen.unigraph.UP_ARROW) |
0 | 32 |
for i in range(y + 1, y + self.height - 1): |
24
b248ef500557
Add DriverPygame (incomplete). Move unicode graphics constants to UnicodeGraphics class. Move shared parts of drivers to Driver base class.
Radek Brich <radek.brich@devl.cz>
parents:
18
diff
changeset
|
33 |
screen.putch(x, i, screen.unigraph.LIGHT_SHADE) |
b248ef500557
Add DriverPygame (incomplete). Move unicode graphics constants to UnicodeGraphics class. Move shared parts of drivers to Driver base class.
Radek Brich <radek.brich@devl.cz>
parents:
18
diff
changeset
|
34 |
screen.putch(x, y + 1 + self.thumbpos, screen.unigraph.BLOCK) |
b248ef500557
Add DriverPygame (incomplete). Move unicode graphics constants to UnicodeGraphics class. Move shared parts of drivers to Driver base class.
Radek Brich <radek.brich@devl.cz>
parents:
18
diff
changeset
|
35 |
screen.putch(x, y + self.height - 1, screen.unigraph.DOWN_ARROW) |
0 | 36 |
|
37 |
||
38 |
def on_mousedown(self, ev): |
|
34
e3beacd5e536
Update event propagation, keypress event, focusing.
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
39 |
super().on_mousedown(ev) |
0 | 40 |
self.dragging = False |
41 |
self.move = None |
|
42 |
# arrow buttons |
|
43 |
if ev.wy == 0 or ev.wy == self.height - 1: |
|
44 |
if ev.wy == 0: |
|
45 |
self.move_up() |
|
46 |
else: |
|
47 |
self.move_down() |
|
34
e3beacd5e536
Update event propagation, keypress event, focusing.
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
48 |
self.top.add_timeout(self.interval * 2, self._timeout) |
0 | 49 |
return |
50 |
# thumb bar |
|
51 |
if ev.wy == 1 + self.thumbpos: |
|
52 |
self.dragging = True |
|
53 |
return |
|
54 |
||
55 |
||
56 |
def on_mouseup(self, ev): |
|
34
e3beacd5e536
Update event propagation, keypress event, focusing.
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
57 |
super().on_mouseup(ev) |
0 | 58 |
if self.dragging: |
59 |
self.drag(ev.wy) |
|
60 |
self.dragging = False |
|
61 |
return |
|
62 |
if self.move: |
|
34
e3beacd5e536
Update event propagation, keypress event, focusing.
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
63 |
self.top.remove_timeout(self._timeout) |
0 | 64 |
self.move = None |
65 |
return |
|
66 |
||
67 |
||
68 |
def on_mousemove(self, ev): |
|
34
e3beacd5e536
Update event propagation, keypress event, focusing.
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
69 |
super().on_mousemove(ev) |
0 | 70 |
if self.dragging: |
71 |
self.drag(ev.wy) |
|
72 |
||
73 |
||
34
e3beacd5e536
Update event propagation, keypress event, focusing.
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
74 |
def _timeout(self): |
0 | 75 |
if self.move == 'up': |
76 |
self.move_up() |
|
77 |
if self.move == 'down': |
|
78 |
self.move_down() |
|
34
e3beacd5e536
Update event propagation, keypress event, focusing.
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
79 |
self.top.add_timeout(self.interval, self._timeout) |
0 | 80 |
|
81 |
||
82 |
def move_up(self): |
|
83 |
if self.pos > 0: |
|
84 |
self.setpos(self.pos - 1) |
|
85 |
self.move = 'up' |
|
86 |
self.redraw() |
|
34
e3beacd5e536
Update event propagation, keypress event, focusing.
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
87 |
self.emit('change') |
0 | 88 |
|
89 |
||
90 |
def move_down(self): |
|
91 |
if self.pos < self.max: |
|
92 |
self.setpos(self.pos + 1) |
|
93 |
self.move = 'down' |
|
94 |
self.redraw() |
|
34
e3beacd5e536
Update event propagation, keypress event, focusing.
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
95 |
self.emit('change') |
0 | 96 |
|
97 |
||
98 |
def drag(self, wy): |
|
99 |
newpos = int(round((wy - 1) / (self.height - 3) * self.max)) |
|
100 |
if newpos < 0: |
|
101 |
newpos = 0 |
|
102 |
if newpos > self.max: |
|
103 |
newpos = self.max |
|
104 |
if self.pos != newpos: |
|
105 |
self.setpos(newpos) |
|
106 |
self.redraw() |
|
34
e3beacd5e536
Update event propagation, keypress event, focusing.
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
107 |
self.emit('change') |
0 | 108 |