tuikit/scrollview.py
changeset 44 d77f1ae3786c
parent 41 37b7dfc3eae6
child 45 43b2279b06e1
equal deleted inserted replaced
43:369c8ef5070a 44:d77f1ae3786c
     6 
     6 
     7 class ScrollView(Container):
     7 class ScrollView(Container):
     8     """Scrolling view
     8     """Scrolling view
     9 
     9 
    10     Shows scrollbars when needed.
    10     Shows scrollbars when needed.
    11     Scrolling area is determined based on child widgets
    11     Scrolling area is determined according to child widget's size request.
    12     size request.
       
    13 
    12 
    14     """
    13     """
    15 
    14 
    16     def __init__(self, width=20, height=20):
    15     def __init__(self, width=20, height=20):
    17         Container.__init__(self, width, height)
    16         Container.__init__(self, width, height)
    23 
    22 
    24     def add(self, widget, **kwargs):
    23     def add(self, widget, **kwargs):
    25         super().add(widget, **kwargs)
    24         super().add(widget, **kwargs)
    26         if widget != self.vscroll:
    25         if widget != self.vscroll:
    27             widget.connect('sizereq', self._on_child_sizereq)
    26             widget.connect('sizereq', self._on_child_sizereq)
       
    27             widget.connect('spotmove', self._on_child_spotmove)
    28 
    28 
    29     def _handle_resize(self, ev):
    29     def _handle_resize(self, ev):
    30         super()._handle_resize(ev)
    30         super()._handle_resize(ev)
    31         self.vscroll.x = self.size.w - 1
    31         self.vscroll.x = self.size.w - 1
    32         self.vscroll.height = self.height
    32         self.vscroll.height = self.height
    35     def _on_vscroll_change(self, ev):
    35     def _on_vscroll_change(self, ev):
    36         self.offset.y = - self.vscroll.pos
    36         self.offset.y = - self.vscroll.pos
    37 
    37 
    38     def _on_child_sizereq(self, ev):
    38     def _on_child_sizereq(self, ev):
    39         self._update_vscroll_max()
    39         self._update_vscroll_max()
       
    40 
       
    41     def _on_child_spotmove(self, ev):
       
    42         child = ev.originator
       
    43         spot = child.y + child.spot.y
       
    44         if spot < self.vscroll.pos:
       
    45             self.vscroll.pos = spot
       
    46         if spot > (self.size.h - 1) + self.vscroll.pos:
       
    47             self.vscroll.pos = spot - (self.size.h - 1)
    40 
    48 
    41     def _update_vscroll_max(self):
    49     def _update_vscroll_max(self):
    42         max_height = 0
    50         max_height = 0
    43         for child in self.children:
    51         for child in self.children:
    44             if child == self.vscroll:
    52             if child == self.vscroll: