tuikit/widgets/scrollbar.py
author Radek Brich <radek.brich@devl.cz>
Sat, 21 Feb 2015 12:01:57 +0100
changeset 118 8c7970520632
parent 76 tuikit/scrollbar.py@fa5301e58eca
child 119 dd91747504dd
permissions -rw-r--r--
Add mouse events, event demo.
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
118
8c7970520632 Add mouse events, event demo.
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
     3
from tuikit.core.widget import Widget
8c7970520632 Add mouse events, event demo.
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
     4
from tuikit.core.signal import Signal
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)
118
8c7970520632 Add mouse events, event demo.
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
    31
        self.sig_changed = Signal()
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):
76
fa5301e58eca Update demo_input, demo_editor. Update ScrollView: show/hide scrollbars as needed on child size requests.
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
    40
        if value < 0:
fa5301e58eca Update demo_input, demo_editor. Update ScrollView: show/hide scrollbars as needed on child size requests.
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
    41
            value = 0
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
    42
        self._scroll_max = value
75
2430c643838a Clean up hints - add methods update_hint, hint_value to Widget. Split ScrollView into OffsetView and Scrolling components. Reimplement ScrollWindow using Scrolling component.
Radek Brich <radek.brich@devl.cz>
parents: 63
diff changeset
    43
        if self._scroll_pos > value:
2430c643838a Clean up hints - add methods update_hint, hint_value to Widget. Split ScrollView into OffsetView and Scrolling components. Reimplement ScrollWindow using Scrolling component.
Radek Brich <radek.brich@devl.cz>
parents: 63
diff changeset
    44
            self._scroll_pos = value
118
8c7970520632 Add mouse events, event demo.
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
    45
            self.sig_changed()
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
    46
        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
    47
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
    @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
    49
    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
    50
        """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
    51
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
    52
        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
    53
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
    54
        """
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
    55
        return self._scroll_pos
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    56
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
    57
    @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
    58
    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
    59
        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
    60
            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
    61
            self._update_thumb_pos()
118
8c7970520632 Add mouse events, event demo.
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
    62
            self.sig_changed()
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    63
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
    64
    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
    65
        """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
    66
        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
    67
            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
    68
        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
    69
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
    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
    71
        """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
    72
        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
    73
            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
    74
        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
    75
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
    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
    77
        """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
    78
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
        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
    80
            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
    81
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
        """
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
        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
    84
        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
    85
            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
    86
        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
    87
            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
    88
        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
    89
            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
    90
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
    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
    92
        """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
    93
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
        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
    95
        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
    96
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
        """
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
        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
    99
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
   100
    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
   101
        """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
   102
        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
   103
        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
   104
            self._thumb_pos = int(round(self._scroll_pos / self._scroll_max * (self._get_length() - 3)))
118
8c7970520632 Add mouse events, event demo.
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   105
        #self.redraw()
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
   106
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
   107
    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
   108
        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
   109
            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
   110
        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
   111
            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
   112
        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
   113
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
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
   115
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
   116
    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
   117
        Scrollbar.__init__(self)
118
8c7970520632 Add mouse events, event demo.
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   118
        self.sizereq.update(1, 20)
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
118
8c7970520632 Add mouse events, event demo.
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   120
    def draw(self, buffer):
8c7970520632 Add mouse events, event demo.
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   121
        Widget.draw(self, buffer)
63
2a0e04091898 Rework MenuBar. Add MenuButton. Add mouse event cascading to floaters.
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   122
        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
   123
        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
   124
        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
   125
        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
   126
            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
   127
        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
   128
        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
   129
        ev.driver.popcolor()
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   130
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
   131
    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
   132
        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
   133
        self._move = None
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   134
        # arrow buttons
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   135
        if ev.wy == 0 or ev.wy == self.height - 1:
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   136
            if ev.wy == 0:
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   137
                self.move_up()
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   138
            else:
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   139
                self.move_down()
59
729fcdfe6b57 Cleanup timeouts.
Radek Brich <radek.brich@devl.cz>
parents: 45
diff changeset
   140
            self.add_timeout(self.scroll_delay, self._continuous_scroll)
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   141
            return
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   142
        # 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
   143
        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
   144
            self._dragging = True
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   145
            return
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   146
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
   147
    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
   148
        if self._dragging:
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   149
            self.drag(ev.wy)
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   150
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
   151
    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
   152
        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
   153
            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
   154
            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
   155
            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
   156
        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
   157
            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
   158
            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
   159
            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
   160
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
    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
   162
        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
   163
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   164
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
   165
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
   166
    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
   167
        Scrollbar.__init__(self)
118
8c7970520632 Add mouse events, event demo.
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   168
        self.sizereq.update(20, 1)
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
   169
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
   170
    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
   171
        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
   172
        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
   173
        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
   174
        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
   175
            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
   176
        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
   177
        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
   178
        ev.driver.popcolor()
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   179
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
   180
    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
   181
        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
   182
        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
   183
        # 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
   184
        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
   185
            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
   186
                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
   187
            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
   188
                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
   189
            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
   190
            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
   191
        # 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
   192
        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
   193
            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
   194
            return
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   195
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
   196
    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
   197
        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
   198
            self.drag(ev.wx)
59
729fcdfe6b57 Cleanup timeouts.
Radek Brich <radek.brich@devl.cz>
parents: 45
diff changeset
   199
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
   200
    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
   201
        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
   202
            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
   203
            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
   204
            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
   205
        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
   206
            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
   207
            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
   208
            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
   209
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
   210
    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
   211
        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
   212