tuikit/scrollview.py
author Radek Brich <radek.brich@devl.cz>
Wed, 26 Dec 2012 01:00:31 +0100
changeset 40 5faa38c10b67
child 41 37b7dfc3eae6
permissions -rw-r--r--
Add ScrollView widget. Update Emitter, rename "on_event" methods to "_handle_event". Update VScrollbar, Layout.

# -*- coding: utf-8 -*-

from tuikit.container import Container
from tuikit.scrollbar import VScrollbar


class ScrollView(Container):
    """Scrolling view

    Shows scrollbars when needed.
    Scrolling area is determined based on child widgets
    size request.

    """

    def __init__(self, width=20, height=20):
        Container.__init__(self, width, height)

        self.vscroll = VScrollbar(height)
        self.vscroll.connect('change', self._on_vscroll_change)
        self.vscroll.allow_layout = False
        self.add(self.vscroll)

    def add(self, widget, **kwargs):
        super().add(widget, **kwargs)
        if widget != self.vscroll:
            widget.connect('sizereq', self._on_child_sizereq)

    def _handle_resize(self):
        super()._handle_resize()
        self.vscroll.x = self.size.w - 1
        self.vscroll.height = self.height
        self._update_vscroll_max()

    def _on_vscroll_change(self, eo):
        self.offset.y = - self.vscroll.pos

    def _on_child_sizereq(self, eo):
        self._update_vscroll_max()

    def _update_vscroll_max(self):
        max_height = 0
        for child in self.children:
            if child == self.vscroll:
                continue
            child_height = child.x + child.sizereq.h
            if child_height > max_height:
                max_height = child_height
        if max_height < self.size.h:
            self.vscroll.hide()
        else:
            self.vscroll.max = max_height - self.size.h
            self.vscroll.show()