tuikit/scrollbar.py
changeset 34 e3beacd5e536
parent 32 088b92ffb119
child 40 5faa38c10b67
equal deleted inserted replaced
33:45f1b6d590bd 34:e3beacd5e536
    14         self.interval = 0.1
    14         self.interval = 0.1
    15 
    15 
    16         self.dragging = False
    16         self.dragging = False
    17         self.move = None
    17         self.move = None
    18 
    18 
    19         self.connect('draw', self.on_draw)
       
    20         self.connect('mousedown', self.on_mousedown)
       
    21         self.connect('mouseup', self.on_mouseup)
       
    22         self.connect('mousemove', self.on_mousemove)
       
    23 
       
    24         self.add_events('change')
    19         self.add_events('change')
    25 
    20 
    26 
    21 
    27     def setpos(self, pos):
    22     def setpos(self, pos):
    28         self.pos = pos
    23         self.pos = pos
    30         if self.max and self.pos <= self.max:
    25         if self.max and self.pos <= self.max:
    31             self.thumbpos = int(round(self.pos / self.max * (self.height - 3)))
    26             self.thumbpos = int(round(self.pos / self.max * (self.height - 3)))
    32 
    27 
    33 
    28 
    34     def on_draw(self, screen, x, y):
    29     def on_draw(self, screen, x, y):
       
    30         super().on_draw(screen, x, y)
    35         screen.putch(x, y, screen.unigraph.UP_ARROW)
    31         screen.putch(x, y, screen.unigraph.UP_ARROW)
    36         for i in range(y + 1, y + self.height - 1):
    32         for i in range(y + 1, y + self.height - 1):
    37             screen.putch(x, i, screen.unigraph.LIGHT_SHADE)
    33             screen.putch(x, i, screen.unigraph.LIGHT_SHADE)
    38         screen.putch(x, y + 1 + self.thumbpos, screen.unigraph.BLOCK)
    34         screen.putch(x, y + 1 + self.thumbpos, screen.unigraph.BLOCK)
    39         screen.putch(x, y + self.height - 1, screen.unigraph.DOWN_ARROW)
    35         screen.putch(x, y + self.height - 1, screen.unigraph.DOWN_ARROW)
    40 
    36 
    41 
    37 
    42     def on_mousedown(self, ev):
    38     def on_mousedown(self, ev):
       
    39         super().on_mousedown(ev)
    43         self.dragging = False
    40         self.dragging = False
    44         self.move = None
    41         self.move = None
    45         # arrow buttons
    42         # arrow buttons
    46         if ev.wy == 0 or ev.wy == self.height - 1:
    43         if ev.wy == 0 or ev.wy == self.height - 1:
    47             if ev.wy == 0:
    44             if ev.wy == 0:
    48                 self.move_up()
    45                 self.move_up()
    49             else:
    46             else:
    50                 self.move_down()
    47                 self.move_down()
    51             self.top.add_timeout(self.interval * 2, self.on_timeout)
    48             self.top.add_timeout(self.interval * 2, self._timeout)
    52             return
    49             return
    53         # thumb bar
    50         # thumb bar
    54         if ev.wy == 1 + self.thumbpos:
    51         if ev.wy == 1 + self.thumbpos:
    55             self.dragging = True
    52             self.dragging = True
    56             return
    53             return
    57 
    54 
    58 
    55 
    59     def on_mouseup(self, ev):
    56     def on_mouseup(self, ev):
       
    57         super().on_mouseup(ev)
    60         if self.dragging:
    58         if self.dragging:
    61             self.drag(ev.wy)
    59             self.drag(ev.wy)
    62             self.dragging = False
    60             self.dragging = False
    63             return
    61             return
    64         if self.move:
    62         if self.move:
    65             self.top.remove_timeout(self.on_timeout)
    63             self.top.remove_timeout(self._timeout)
    66             self.move = None
    64             self.move = None
    67             return
    65             return
    68 
    66 
    69 
    67 
    70     def on_mousemove(self, ev):
    68     def on_mousemove(self, ev):
       
    69         super().on_mousemove(ev)
    71         if self.dragging:
    70         if self.dragging:
    72             self.drag(ev.wy)
    71             self.drag(ev.wy)
    73 
    72 
    74 
    73 
    75     def on_timeout(self):
    74     def _timeout(self):
    76         if self.move == 'up':
    75         if self.move == 'up':
    77             self.move_up()
    76             self.move_up()
    78         if self.move == 'down':
    77         if self.move == 'down':
    79             self.move_down()
    78             self.move_down()
    80         self.top.add_timeout(self.interval, self.on_timeout)
    79         self.top.add_timeout(self.interval, self._timeout)
    81 
    80 
    82 
    81 
    83     def move_up(self):
    82     def move_up(self):
    84         if self.pos > 0:
    83         if self.pos > 0:
    85             self.setpos(self.pos - 1)
    84             self.setpos(self.pos - 1)
    86         self.move = 'up'
    85         self.move = 'up'
    87         self.redraw()
    86         self.redraw()
    88         self.handle('change')
    87         self.emit('change')
    89 
    88 
    90 
    89 
    91     def move_down(self):
    90     def move_down(self):
    92         if self.pos < self.max:
    91         if self.pos < self.max:
    93             self.setpos(self.pos + 1)
    92             self.setpos(self.pos + 1)
    94         self.move = 'down'
    93         self.move = 'down'
    95         self.redraw()
    94         self.redraw()
    96         self.handle('change')
    95         self.emit('change')
    97 
    96 
    98 
    97 
    99     def drag(self, wy):
    98     def drag(self, wy):
   100         newpos = int(round((wy - 1) / (self.height - 3) * self.max))
    99         newpos = int(round((wy - 1) / (self.height - 3) * self.max))
   101         if newpos < 0:
   100         if newpos < 0:
   103         if newpos > self.max:
   102         if newpos > self.max:
   104             newpos = self.max
   103             newpos = self.max
   105         if self.pos != newpos:
   104         if self.pos != newpos:
   106             self.setpos(newpos)
   105             self.setpos(newpos)
   107             self.redraw()
   106             self.redraw()
   108             self.handle('change')
   107             self.emit('change')
   109 
   108