tuikit/scrollbar.py
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--
Update event propagation, keypress event, focusing.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     1
# -*- coding: utf-8 -*-
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     2
18
e6c3a5ee91aa Eliminate relative imports.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
     3
from tuikit.widget import Widget
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     4
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     5
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     6
class VScrollbar(Widget):
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     7
    def __init__(self, height=10):
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     8
        Widget.__init__(self, 1, height)
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     9
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    10
        self.max = height - 3
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    11
        self.pos = 0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    12
        self.thumbpos = 0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    13
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    14
        self.interval = 0.1
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    15
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    16
        self.dragging = False
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    17
        self.move = None
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    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
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    20
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    21
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    22
    def setpos(self, pos):
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    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
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    27
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    28
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    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
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    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
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    36
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    37
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    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
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    40
        self.dragging = False
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    41
        self.move = None
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    42
        # arrow buttons
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    43
        if ev.wy == 0 or ev.wy == self.height - 1:
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    44
            if ev.wy == 0:
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    45
                self.move_up()
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    46
            else:
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    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
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    49
            return
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    50
        # thumb bar
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    51
        if ev.wy == 1 + self.thumbpos:
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    52
            self.dragging = True
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    53
            return
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    54
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    55
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    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
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    58
        if self.dragging:
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    59
            self.drag(ev.wy)
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    60
            self.dragging = False
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    61
            return
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    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
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    64
            self.move = None
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    65
            return
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    66
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    67
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    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
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    70
        if self.dragging:
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    71
            self.drag(ev.wy)
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    72
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    73
34
e3beacd5e536 Update event propagation, keypress event, focusing.
Radek Brich <radek.brich@devl.cz>
parents: 32
diff changeset
    74
    def _timeout(self):
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    75
        if self.move == 'up':
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    76
            self.move_up()
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    77
        if self.move == 'down':
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    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
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    80
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    81
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    82
    def move_up(self):
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    83
        if self.pos > 0:
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    84
            self.setpos(self.pos - 1)
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    85
        self.move = 'up'
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    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
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    88
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    89
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    90
    def move_down(self):
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    91
        if self.pos < self.max:
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    92
            self.setpos(self.pos + 1)
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    93
        self.move = 'down'
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    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
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    96
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    97
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    98
    def drag(self, wy):
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    99
        newpos = int(round((wy - 1) / (self.height - 3) * self.max))
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   100
        if newpos < 0:
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   101
            newpos = 0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   102
        if newpos > self.max:
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   103
            newpos = self.max
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   104
        if self.pos != newpos:
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   105
            self.setpos(newpos)
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   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
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   108