tuikit/scrollbar.py
author Radek Brich <radek.brich@devl.cz>
Wed, 30 Jan 2013 19:44:01 +0100
changeset 72 6e0656600754
parent 63 2a0e04091898
child 75 2430c643838a
permissions -rw-r--r--
DriverSDL: Fix event dispatching. Add SDL events test.
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
43
369c8ef5070a Rename emitter module to events.
Radek Brich <radek.brich@devl.cz>
parents: 41
diff changeset
     4
from tuikit.events import Event
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     5
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     6
62
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
     7
class Scrollbar(Widget):
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     8
62
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
     9
    """Abstract base class for scrollbars."""
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
    10
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
    11
    def __init__(self):
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
    12
        Widget.__init__(self)
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    13
62
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
    14
        # Scrolling range is 0 .. _scroll_max
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
    15
        self._scroll_max = self._get_length() - 3
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
    16
        # Current position of scrollbar in scrolling range.
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
    17
        self._scroll_pos = 0
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
    18
        # Current position of thumb on scrollbar - used for draw.
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
    19
        self._thumb_pos = 0
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
    20
        # Auxilliary variable, True when user holds mouse on thumb.
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
    21
        self._dragging = False
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
    22
        # Auxilliary variable, 'up' or 'down' depending on last move direction.
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
    23
        self._move = None
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    24
62
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
    25
        #: delay before continuous scrolling when user holds mouse on scrollbar arrow
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
    26
        self.scroll_delay = 0.150
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
    27
        #: interval for continuous scrolling
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
    28
        self.scroll_interval = 0.030
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    29
62
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
    30
        # change event is emitted when user moves scrollbar (even programmatically)
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
    31
        self.add_events('change', Event)
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    32
40
5faa38c10b67 Add ScrollView widget. Update Emitter, rename "on_event" methods to "_handle_event". Update VScrollbar, Layout.
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
    33
    @property
62
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
    34
    def scroll_max(self):
40
5faa38c10b67 Add ScrollView widget. Update Emitter, rename "on_event" methods to "_handle_event". Update VScrollbar, Layout.
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
    35
        """Maximum for scrolling position."""
62
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
    36
        return self._scroll_max
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    37
62
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
    38
    @scroll_max.setter
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
    39
    def scroll_max(self, value):
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
    40
        self._scroll_max = value
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
    41
        self._update_thumb_pos()
40
5faa38c10b67 Add ScrollView widget. Update Emitter, rename "on_event" methods to "_handle_event". Update VScrollbar, Layout.
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
    42
5faa38c10b67 Add ScrollView widget. Update Emitter, rename "on_event" methods to "_handle_event". Update VScrollbar, Layout.
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
    43
    @property
62
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
    44
    def scroll_pos(self):
40
5faa38c10b67 Add ScrollView widget. Update Emitter, rename "on_event" methods to "_handle_event". Update VScrollbar, Layout.
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
    45
        """Scrolling position.
5faa38c10b67 Add ScrollView widget. Update Emitter, rename "on_event" methods to "_handle_event". Update VScrollbar, Layout.
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
    46
5faa38c10b67 Add ScrollView widget. Update Emitter, rename "on_event" methods to "_handle_event". Update VScrollbar, Layout.
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
    47
        Integer number between 0 and 'max'.
5faa38c10b67 Add ScrollView widget. Update Emitter, rename "on_event" methods to "_handle_event". Update VScrollbar, Layout.
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
    48
5faa38c10b67 Add ScrollView widget. Update Emitter, rename "on_event" methods to "_handle_event". Update VScrollbar, Layout.
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
    49
        """
62
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
    50
        return self._scroll_pos
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    51
62
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
    52
    @scroll_pos.setter
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
    53
    def scroll_pos(self, value):
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
    54
        if self._scroll_pos != value:
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
    55
            self._scroll_pos = value
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
    56
            self._update_thumb_pos()
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
    57
            self.emit('change')
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    58
62
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
    59
    def move_up(self):
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
    60
        """Move scrolling position up/left."""
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
    61
        if self._scroll_pos > 0:
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
    62
            self.scroll_pos = self._scroll_pos - 1
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
    63
        self._move = 'up'
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
    64
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
    65
    def move_down(self):
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
    66
        """Move scrolling position down/right."""
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
    67
        if self._scroll_pos < self._scroll_max:
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
    68
            self.scroll_pos = self._scroll_pos + 1
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
    69
        self._move = 'down'
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
    70
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
    71
    def drag(self, mouse_pos):
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
    72
        """Scroll using mouse drag on thumb.
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
    73
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
    74
        Args:
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
    75
            mouse_pos: new position of mouse, in range 0 .. self._get_length()
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
    76
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
    77
        """
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
    78
        new_pos = int(round((mouse_pos - 1) / (self._get_length() - 3) * self._scroll_max))
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
    79
        if new_pos < 0:
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
    80
            new_pos = 0
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
    81
        if new_pos > self._scroll_max:
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
    82
            new_pos = self._scroll_max
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
    83
        if self._scroll_pos != new_pos:
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
    84
            self.scroll_pos = new_pos
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
    85
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
    86
    def _get_length(self):
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
    87
        """Return length of scrollbar.
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
    88
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
    89
        This will be widget height for vertical scrollbar,
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
    90
        width for horizontal scrollbar.
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
    91
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
    92
        """
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
    93
        raise NotImplementedError()
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
    94
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
    95
    def _update_thumb_pos(self):
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
    96
        """Update value of internal variable _thumb_pos."""
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
    97
        self._thumb_pos = 0
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
    98
        if self._scroll_max and self._scroll_pos <= self._scroll_max:
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
    99
            self._thumb_pos = int(round(self._scroll_pos / self._scroll_max * (self._get_length() - 3)))
40
5faa38c10b67 Add ScrollView widget. Update Emitter, rename "on_event" methods to "_handle_event". Update VScrollbar, Layout.
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   100
        self.redraw()
5faa38c10b67 Add ScrollView widget. Update Emitter, rename "on_event" methods to "_handle_event". Update VScrollbar, Layout.
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   101
62
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   102
    def _continuous_scroll(self):
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   103
        if self._move == 'up':
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   104
            self.move_up()
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   105
        if self._move == 'down':
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   106
            self.move_down()
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   107
        self.add_timeout(self.scroll_interval, self._continuous_scroll)
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   108
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   109
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   110
class VScrollbar(Scrollbar):
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   111
    def __init__(self):
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   112
        Scrollbar.__init__(self)
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   113
        self._default_size.update(1, 20)
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   114
45
43b2279b06e1 Clean up Emitter class, simplify event handling. Fix Container.focusnext() method. Add events test (handler auto-registration, order).
Radek Brich <radek.brich@devl.cz>
parents: 43
diff changeset
   115
    def on_draw(self, ev):
63
2a0e04091898 Rework MenuBar. Add MenuButton. Add mouse event cascading to floaters.
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   116
        ug = ev.driver.unigraph
62
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   117
        ev.driver.pushcolor('normal')
63
2a0e04091898 Rework MenuBar. Add MenuButton. Add mouse event cascading to floaters.
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   118
        ev.driver.putch(ev.x, ev.y, ug.get_char('sb_up'))
62
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   119
        for i in range(1, self.height - 1):
63
2a0e04091898 Rework MenuBar. Add MenuButton. Add mouse event cascading to floaters.
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   120
            ev.driver.putch(ev.x, ev.y + i, ug.get_char('sb_vtrack'))
2a0e04091898 Rework MenuBar. Add MenuButton. Add mouse event cascading to floaters.
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   121
        ev.driver.putch(ev.x, ev.y + 1 + self._thumb_pos, ug.get_char('sb_thumb'))
2a0e04091898 Rework MenuBar. Add MenuButton. Add mouse event cascading to floaters.
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   122
        ev.driver.putch(ev.x, ev.y + self.height - 1, ug.get_char('sb_down'))
62
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   123
        ev.driver.popcolor()
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   124
45
43b2279b06e1 Clean up Emitter class, simplify event handling. Fix Container.focusnext() method. Add events test (handler auto-registration, order).
Radek Brich <radek.brich@devl.cz>
parents: 43
diff changeset
   125
    def on_mousedown(self, ev):
62
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   126
        self._dragging = False
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   127
        self._move = None
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   128
        # arrow buttons
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   129
        if ev.wy == 0 or ev.wy == self.height - 1:
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   130
            if ev.wy == 0:
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   131
                self.move_up()
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   132
            else:
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   133
                self.move_down()
59
729fcdfe6b57 Cleanup timeouts.
Radek Brich <radek.brich@devl.cz>
parents: 45
diff changeset
   134
            self.add_timeout(self.scroll_delay, self._continuous_scroll)
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   135
            return
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   136
        # thumb bar
62
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   137
        if ev.wy == 1 + self._thumb_pos:
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   138
            self._dragging = True
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   139
            return
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   140
45
43b2279b06e1 Clean up Emitter class, simplify event handling. Fix Container.focusnext() method. Add events test (handler auto-registration, order).
Radek Brich <radek.brich@devl.cz>
parents: 43
diff changeset
   141
    def on_mousemove(self, ev):
62
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   142
        if self._dragging:
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   143
            self.drag(ev.wy)
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   144
62
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   145
    def on_mouseup(self, ev):
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   146
        if self._dragging:
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   147
            self.drag(ev.wy)
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   148
            self._dragging = False
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   149
            return
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   150
        if self._move:
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   151
            self.remove_timeout(self._continuous_scroll)
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   152
            self._move = None
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   153
            return
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   154
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   155
    def _get_length(self):
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   156
        return self.height
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   157
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   158
62
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   159
class HScrollbar(Scrollbar):
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   160
    def __init__(self):
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   161
        Scrollbar.__init__(self)
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   162
        self._default_size.update(20, 1)
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   163
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   164
    def on_draw(self, ev):
63
2a0e04091898 Rework MenuBar. Add MenuButton. Add mouse event cascading to floaters.
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   165
        ug = ev.driver.unigraph
62
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   166
        ev.driver.pushcolor('normal')
63
2a0e04091898 Rework MenuBar. Add MenuButton. Add mouse event cascading to floaters.
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   167
        ev.driver.putch(ev.x, ev.y, ug.get_char('sb_left'))
62
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   168
        for i in range(1, self.width - 1):
63
2a0e04091898 Rework MenuBar. Add MenuButton. Add mouse event cascading to floaters.
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   169
            ev.driver.putch(ev.x + i, ev.y, ug.get_char('sb_htrack'))
2a0e04091898 Rework MenuBar. Add MenuButton. Add mouse event cascading to floaters.
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   170
        ev.driver.putch(ev.x + 1 + self._thumb_pos, ev.y, ug.get_char('sb_thumb'))
2a0e04091898 Rework MenuBar. Add MenuButton. Add mouse event cascading to floaters.
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   171
        ev.driver.putch(ev.x + self.width - 1, ev.y, ug.get_char('sb_right'))
62
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   172
        ev.driver.popcolor()
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   173
62
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   174
    def on_mousedown(self, ev):
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   175
        self._dragging = False
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   176
        self._move = None
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   177
        # arrow buttons
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   178
        if ev.wx == 0 or ev.wx == self.width - 1:
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   179
            if ev.wx == 0:
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   180
                self.move_up()
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   181
            else:
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   182
                self.move_down()
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   183
            self.add_timeout(self.scroll_delay, self._continuous_scroll)
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   184
            return
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   185
        # thumb bar
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   186
        if ev.wx == 1 + self._thumb_pos:
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   187
            self._dragging = True
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   188
            return
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   189
62
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   190
    def on_mousemove(self, ev):
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   191
        if self._dragging:
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   192
            self.drag(ev.wx)
59
729fcdfe6b57 Cleanup timeouts.
Radek Brich <radek.brich@devl.cz>
parents: 45
diff changeset
   193
62
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   194
    def on_mouseup(self, ev):
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   195
        if self._dragging:
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   196
            self.drag(ev.wx)
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   197
            self._dragging = False
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   198
            return
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   199
        if self._move:
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   200
            self.remove_timeout(self._continuous_scroll)
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   201
            self._move = None
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   202
            return
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   203
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   204
    def _get_length(self):
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   205
        return self.width
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   206