tuikit/widgets/scrollbar.py
changeset 119 dd91747504dd
parent 118 8c7970520632
equal deleted inserted replaced
118:8c7970520632 119:dd91747504dd
     1 # -*- coding: utf-8 -*-
       
     2 
       
     3 from tuikit.core.widget import Widget
     1 from tuikit.core.widget import Widget
     4 from tuikit.core.signal import Signal
     2 from tuikit.core.signal import Signal
     5 
     3 
     6 
     4 
     7 class Scrollbar(Widget):
     5 class Scrollbar(Widget):
     8 
     6 
     9     """Abstract base class for scrollbars."""
     7     """Abstract base class for scrollbars."""
    10 
     8 
    11     def __init__(self):
     9     def __init__(self, scroll_max):
    12         Widget.__init__(self)
    10         Widget.__init__(self)
    13 
    11 
    14         # Scrolling range is 0 .. _scroll_max
    12         # Scrolling range is 0 .. _scroll_max
    15         self._scroll_max = self._get_length() - 3
    13         self._scroll_max = scroll_max
    16         # Current position of scrollbar in scrolling range.
    14         # Current position of scrollbar in scrolling range.
    17         self._scroll_pos = 0
    15         self._scroll_pos = 0
    18         # Current position of thumb on scrollbar - used for draw.
    16         # Current position of thumb on scrollbar - used for draw.
    19         self._thumb_pos = 0
    17         self._thumb_pos = 0
    20         # Auxilliary variable, True when user holds mouse on thumb.
    18         # Auxilliary variable, True when user holds mouse on thumb.
    64     def move_up(self):
    62     def move_up(self):
    65         """Move scrolling position up/left."""
    63         """Move scrolling position up/left."""
    66         if self._scroll_pos > 0:
    64         if self._scroll_pos > 0:
    67             self.scroll_pos = self._scroll_pos - 1
    65             self.scroll_pos = self._scroll_pos - 1
    68         self._move = 'up'
    66         self._move = 'up'
       
    67         self.redraw()
    69 
    68 
    70     def move_down(self):
    69     def move_down(self):
    71         """Move scrolling position down/right."""
    70         """Move scrolling position down/right."""
    72         if self._scroll_pos < self._scroll_max:
    71         if self._scroll_pos < self._scroll_max:
    73             self.scroll_pos = self._scroll_pos + 1
    72             self.scroll_pos = self._scroll_pos + 1
    74         self._move = 'down'
    73         self._move = 'down'
       
    74         self.redraw()
    75 
    75 
    76     def drag(self, mouse_pos):
    76     def drag(self, mouse_pos):
    77         """Scroll using mouse drag on thumb.
    77         """Scroll using mouse drag on thumb.
    78 
    78 
    79         Args:
    79         Args:
   107     def _continuous_scroll(self):
   107     def _continuous_scroll(self):
   108         if self._move == 'up':
   108         if self._move == 'up':
   109             self.move_up()
   109             self.move_up()
   110         if self._move == 'down':
   110         if self._move == 'down':
   111             self.move_down()
   111             self.move_down()
   112         self.add_timeout(self.scroll_interval, self._continuous_scroll)
   112         self.timer.add_timeout(self.scroll_interval, self._continuous_scroll)
   113 
   113 
   114 
   114 
   115 class VScrollbar(Scrollbar):
   115 class VScrollbar(Scrollbar):
   116     def __init__(self):
   116     def __init__(self):
   117         Scrollbar.__init__(self)
   117         Scrollbar.__init__(self)
   135         if ev.wy == 0 or ev.wy == self.height - 1:
   135         if ev.wy == 0 or ev.wy == self.height - 1:
   136             if ev.wy == 0:
   136             if ev.wy == 0:
   137                 self.move_up()
   137                 self.move_up()
   138             else:
   138             else:
   139                 self.move_down()
   139                 self.move_down()
   140             self.add_timeout(self.scroll_delay, self._continuous_scroll)
   140             self.timer.add_timeout(self.scroll_delay, self._continuous_scroll)
   141             return
   141             return
   142         # thumb bar
   142         # thumb bar
   143         if ev.wy == 1 + self._thumb_pos:
   143         if ev.wy == 1 + self._thumb_pos:
   144             self._dragging = True
   144             self._dragging = True
   145             return
   145             return
   152         if self._dragging:
   152         if self._dragging:
   153             self.drag(ev.wy)
   153             self.drag(ev.wy)
   154             self._dragging = False
   154             self._dragging = False
   155             return
   155             return
   156         if self._move:
   156         if self._move:
   157             self.remove_timeout(self._continuous_scroll)
   157             self.timer.remove_timeout(self._continuous_scroll)
   158             self._move = None
   158             self._move = None
   159             return
   159             return
   160 
   160 
   161     def _get_length(self):
   161     def _get_length(self):
   162         return self.height
   162         return self.height
   163 
   163 
   164 
   164 
   165 class HScrollbar(Scrollbar):
   165 class HScrollbar(Scrollbar):
   166     def __init__(self):
   166     def __init__(self, length=20):
   167         Scrollbar.__init__(self)
   167         Scrollbar.__init__(self, length - 3)
   168         self.sizereq.update(20, 1)
   168         self.sizereq.update(length, 1)
   169 
   169 
   170     def on_draw(self, ev):
   170     def draw(self, buffer):
   171         ug = ev.driver.unigraph
   171         Widget.draw(self, buffer)
   172         ev.driver.pushcolor('normal')
   172         with buffer.attr(self.theme.clr_normal):
   173         ev.driver.putch(ev.x, ev.y, ug.get_char('sb_left'))
   173             buffer.putch(self.theme.sb_left)
   174         for i in range(1, self.width - 1):
   174             for i in range(1, self.width - 1):
   175             ev.driver.putch(ev.x + i, ev.y, ug.get_char('sb_htrack'))
   175                 buffer.putch(self.theme.sb_htrack, i)
   176         ev.driver.putch(ev.x + 1 + self._thumb_pos, ev.y, ug.get_char('sb_thumb'))
   176             buffer.putch(self.theme.sb_thumb, 1 + self._thumb_pos)
   177         ev.driver.putch(ev.x + self.width - 1, ev.y, ug.get_char('sb_right'))
   177             buffer.putch(self.theme.sb_right, self.width - 1)
   178         ev.driver.popcolor()
   178 
   179 
   179     def mousedown_event(self, ev):
   180     def on_mousedown(self, ev):
       
   181         self._dragging = False
   180         self._dragging = False
   182         self._move = None
   181         self._move = None
   183         # arrow buttons
   182         # arrow buttons
   184         if ev.wx == 0 or ev.wx == self.width - 1:
   183         if ev.pos.x == 0 or ev.pos.x == self.width - 1:
   185             if ev.wx == 0:
   184             if ev.pos.x == 0:
   186                 self.move_up()
   185                 self.move_up()
   187             else:
   186             else:
   188                 self.move_down()
   187                 self.move_down()
   189             self.add_timeout(self.scroll_delay, self._continuous_scroll)
   188             self.timer.add_timeout(self.scroll_delay, self._continuous_scroll)
   190             return
   189             return
   191         # thumb bar
   190         # thumb bar
   192         if ev.wx == 1 + self._thumb_pos:
   191         if ev.pos.x == 1 + self._thumb_pos:
   193             self._dragging = True
   192             self._dragging = True
   194             return
   193             return
   195 
   194 
   196     def on_mousemove(self, ev):
   195     def mousemove_event(self, ev):
   197         if self._dragging:
   196         if self._dragging:
   198             self.drag(ev.wx)
   197             self.drag(ev.pos.x)
   199 
   198 
   200     def on_mouseup(self, ev):
   199     def mouseup_event(self, ev):
   201         if self._dragging:
   200         if self._dragging:
   202             self.drag(ev.wx)
   201             self.drag(ev.pos.x)
   203             self._dragging = False
   202             self._dragging = False
   204             return
   203             return
   205         if self._move:
   204         if self._move:
   206             self.remove_timeout(self._continuous_scroll)
   205             self.timer.remove_timeout(self._continuous_scroll)
   207             self._move = None
   206             self._move = None
   208             return
   207             return
   209 
   208 
   210     def _get_length(self):
   209     def _get_length(self):
   211         return self.width
   210         return self.width
   212