tuikit/scrollview.py
author Radek Brich <radek.brich@devl.cz>
Wed, 02 Jan 2013 11:48:36 +0100
changeset 44 d77f1ae3786c
parent 41 37b7dfc3eae6
child 45 43b2279b06e1
permissions -rw-r--r--
Add Widget.spot property. TreeView: move spot with cursor node. ScrollView: scroll when spot moves.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
40
5faa38c10b67 Add ScrollView widget. Update Emitter, rename "on_event" methods to "_handle_event". Update VScrollbar, Layout.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     1
# -*- coding: utf-8 -*-
5faa38c10b67 Add ScrollView widget. Update Emitter, rename "on_event" methods to "_handle_event". Update VScrollbar, Layout.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     2
5faa38c10b67 Add ScrollView widget. Update Emitter, rename "on_event" methods to "_handle_event". Update VScrollbar, Layout.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     3
from tuikit.container import Container
5faa38c10b67 Add ScrollView widget. Update Emitter, rename "on_event" methods to "_handle_event". Update VScrollbar, Layout.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     4
from tuikit.scrollbar import VScrollbar
5faa38c10b67 Add ScrollView widget. Update Emitter, rename "on_event" methods to "_handle_event". Update VScrollbar, Layout.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     5
5faa38c10b67 Add ScrollView widget. Update Emitter, rename "on_event" methods to "_handle_event". Update VScrollbar, Layout.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     6
5faa38c10b67 Add ScrollView widget. Update Emitter, rename "on_event" methods to "_handle_event". Update VScrollbar, Layout.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     7
class ScrollView(Container):
5faa38c10b67 Add ScrollView widget. Update Emitter, rename "on_event" methods to "_handle_event". Update VScrollbar, Layout.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     8
    """Scrolling view
5faa38c10b67 Add ScrollView widget. Update Emitter, rename "on_event" methods to "_handle_event". Update VScrollbar, Layout.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     9
5faa38c10b67 Add ScrollView widget. Update Emitter, rename "on_event" methods to "_handle_event". Update VScrollbar, Layout.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    10
    Shows scrollbars when needed.
44
d77f1ae3786c Add Widget.spot property. TreeView: move spot with cursor node. ScrollView: scroll when spot moves.
Radek Brich <radek.brich@devl.cz>
parents: 41
diff changeset
    11
    Scrolling area is determined according to child widget's size request.
40
5faa38c10b67 Add ScrollView widget. Update Emitter, rename "on_event" methods to "_handle_event". Update VScrollbar, Layout.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    12
5faa38c10b67 Add ScrollView widget. Update Emitter, rename "on_event" methods to "_handle_event". Update VScrollbar, Layout.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    13
    """
5faa38c10b67 Add ScrollView widget. Update Emitter, rename "on_event" methods to "_handle_event". Update VScrollbar, Layout.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    14
5faa38c10b67 Add ScrollView widget. Update Emitter, rename "on_event" methods to "_handle_event". Update VScrollbar, Layout.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    15
    def __init__(self, width=20, height=20):
5faa38c10b67 Add ScrollView widget. Update Emitter, rename "on_event" methods to "_handle_event". Update VScrollbar, Layout.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    16
        Container.__init__(self, width, height)
5faa38c10b67 Add ScrollView widget. Update Emitter, rename "on_event" methods to "_handle_event". Update VScrollbar, Layout.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    17
5faa38c10b67 Add ScrollView widget. Update Emitter, rename "on_event" methods to "_handle_event". Update VScrollbar, Layout.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    18
        self.vscroll = VScrollbar(height)
5faa38c10b67 Add ScrollView widget. Update Emitter, rename "on_event" methods to "_handle_event". Update VScrollbar, Layout.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    19
        self.vscroll.connect('change', self._on_vscroll_change)
5faa38c10b67 Add ScrollView widget. Update Emitter, rename "on_event" methods to "_handle_event". Update VScrollbar, Layout.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    20
        self.vscroll.allow_layout = False
5faa38c10b67 Add ScrollView widget. Update Emitter, rename "on_event" methods to "_handle_event". Update VScrollbar, Layout.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    21
        self.add(self.vscroll)
5faa38c10b67 Add ScrollView widget. Update Emitter, rename "on_event" methods to "_handle_event". Update VScrollbar, Layout.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    22
5faa38c10b67 Add ScrollView widget. Update Emitter, rename "on_event" methods to "_handle_event". Update VScrollbar, Layout.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    23
    def add(self, widget, **kwargs):
5faa38c10b67 Add ScrollView widget. Update Emitter, rename "on_event" methods to "_handle_event". Update VScrollbar, Layout.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    24
        super().add(widget, **kwargs)
5faa38c10b67 Add ScrollView widget. Update Emitter, rename "on_event" methods to "_handle_event". Update VScrollbar, Layout.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    25
        if widget != self.vscroll:
5faa38c10b67 Add ScrollView widget. Update Emitter, rename "on_event" methods to "_handle_event". Update VScrollbar, Layout.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    26
            widget.connect('sizereq', self._on_child_sizereq)
44
d77f1ae3786c Add Widget.spot property. TreeView: move spot with cursor node. ScrollView: scroll when spot moves.
Radek Brich <radek.brich@devl.cz>
parents: 41
diff changeset
    27
            widget.connect('spotmove', self._on_child_spotmove)
40
5faa38c10b67 Add ScrollView widget. Update Emitter, rename "on_event" methods to "_handle_event". Update VScrollbar, Layout.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    28
41
37b7dfc3eae6 Update Emitter: All event handlers now have exactly one argument: object inherited from Event class, which carries any data.
Radek Brich <radek.brich@devl.cz>
parents: 40
diff changeset
    29
    def _handle_resize(self, ev):
37b7dfc3eae6 Update Emitter: All event handlers now have exactly one argument: object inherited from Event class, which carries any data.
Radek Brich <radek.brich@devl.cz>
parents: 40
diff changeset
    30
        super()._handle_resize(ev)
40
5faa38c10b67 Add ScrollView widget. Update Emitter, rename "on_event" methods to "_handle_event". Update VScrollbar, Layout.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    31
        self.vscroll.x = self.size.w - 1
5faa38c10b67 Add ScrollView widget. Update Emitter, rename "on_event" methods to "_handle_event". Update VScrollbar, Layout.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    32
        self.vscroll.height = self.height
5faa38c10b67 Add ScrollView widget. Update Emitter, rename "on_event" methods to "_handle_event". Update VScrollbar, Layout.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    33
        self._update_vscroll_max()
5faa38c10b67 Add ScrollView widget. Update Emitter, rename "on_event" methods to "_handle_event". Update VScrollbar, Layout.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    34
41
37b7dfc3eae6 Update Emitter: All event handlers now have exactly one argument: object inherited from Event class, which carries any data.
Radek Brich <radek.brich@devl.cz>
parents: 40
diff changeset
    35
    def _on_vscroll_change(self, ev):
40
5faa38c10b67 Add ScrollView widget. Update Emitter, rename "on_event" methods to "_handle_event". Update VScrollbar, Layout.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    36
        self.offset.y = - self.vscroll.pos
5faa38c10b67 Add ScrollView widget. Update Emitter, rename "on_event" methods to "_handle_event". Update VScrollbar, Layout.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    37
41
37b7dfc3eae6 Update Emitter: All event handlers now have exactly one argument: object inherited from Event class, which carries any data.
Radek Brich <radek.brich@devl.cz>
parents: 40
diff changeset
    38
    def _on_child_sizereq(self, ev):
40
5faa38c10b67 Add ScrollView widget. Update Emitter, rename "on_event" methods to "_handle_event". Update VScrollbar, Layout.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    39
        self._update_vscroll_max()
5faa38c10b67 Add ScrollView widget. Update Emitter, rename "on_event" methods to "_handle_event". Update VScrollbar, Layout.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    40
44
d77f1ae3786c Add Widget.spot property. TreeView: move spot with cursor node. ScrollView: scroll when spot moves.
Radek Brich <radek.brich@devl.cz>
parents: 41
diff changeset
    41
    def _on_child_spotmove(self, ev):
d77f1ae3786c Add Widget.spot property. TreeView: move spot with cursor node. ScrollView: scroll when spot moves.
Radek Brich <radek.brich@devl.cz>
parents: 41
diff changeset
    42
        child = ev.originator
d77f1ae3786c Add Widget.spot property. TreeView: move spot with cursor node. ScrollView: scroll when spot moves.
Radek Brich <radek.brich@devl.cz>
parents: 41
diff changeset
    43
        spot = child.y + child.spot.y
d77f1ae3786c Add Widget.spot property. TreeView: move spot with cursor node. ScrollView: scroll when spot moves.
Radek Brich <radek.brich@devl.cz>
parents: 41
diff changeset
    44
        if spot < self.vscroll.pos:
d77f1ae3786c Add Widget.spot property. TreeView: move spot with cursor node. ScrollView: scroll when spot moves.
Radek Brich <radek.brich@devl.cz>
parents: 41
diff changeset
    45
            self.vscroll.pos = spot
d77f1ae3786c Add Widget.spot property. TreeView: move spot with cursor node. ScrollView: scroll when spot moves.
Radek Brich <radek.brich@devl.cz>
parents: 41
diff changeset
    46
        if spot > (self.size.h - 1) + self.vscroll.pos:
d77f1ae3786c Add Widget.spot property. TreeView: move spot with cursor node. ScrollView: scroll when spot moves.
Radek Brich <radek.brich@devl.cz>
parents: 41
diff changeset
    47
            self.vscroll.pos = spot - (self.size.h - 1)
d77f1ae3786c Add Widget.spot property. TreeView: move spot with cursor node. ScrollView: scroll when spot moves.
Radek Brich <radek.brich@devl.cz>
parents: 41
diff changeset
    48
40
5faa38c10b67 Add ScrollView widget. Update Emitter, rename "on_event" methods to "_handle_event". Update VScrollbar, Layout.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    49
    def _update_vscroll_max(self):
5faa38c10b67 Add ScrollView widget. Update Emitter, rename "on_event" methods to "_handle_event". Update VScrollbar, Layout.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    50
        max_height = 0
5faa38c10b67 Add ScrollView widget. Update Emitter, rename "on_event" methods to "_handle_event". Update VScrollbar, Layout.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    51
        for child in self.children:
5faa38c10b67 Add ScrollView widget. Update Emitter, rename "on_event" methods to "_handle_event". Update VScrollbar, Layout.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    52
            if child == self.vscroll:
5faa38c10b67 Add ScrollView widget. Update Emitter, rename "on_event" methods to "_handle_event". Update VScrollbar, Layout.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    53
                continue
5faa38c10b67 Add ScrollView widget. Update Emitter, rename "on_event" methods to "_handle_event". Update VScrollbar, Layout.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    54
            child_height = child.x + child.sizereq.h
5faa38c10b67 Add ScrollView widget. Update Emitter, rename "on_event" methods to "_handle_event". Update VScrollbar, Layout.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    55
            if child_height > max_height:
5faa38c10b67 Add ScrollView widget. Update Emitter, rename "on_event" methods to "_handle_event". Update VScrollbar, Layout.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    56
                max_height = child_height
5faa38c10b67 Add ScrollView widget. Update Emitter, rename "on_event" methods to "_handle_event". Update VScrollbar, Layout.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    57
        if max_height < self.size.h:
5faa38c10b67 Add ScrollView widget. Update Emitter, rename "on_event" methods to "_handle_event". Update VScrollbar, Layout.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    58
            self.vscroll.hide()
5faa38c10b67 Add ScrollView widget. Update Emitter, rename "on_event" methods to "_handle_event". Update VScrollbar, Layout.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    59
        else:
5faa38c10b67 Add ScrollView widget. Update Emitter, rename "on_event" methods to "_handle_event". Update VScrollbar, Layout.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    60
            self.vscroll.max = max_height - self.size.h
5faa38c10b67 Add ScrollView widget. Update Emitter, rename "on_event" methods to "_handle_event". Update VScrollbar, Layout.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    61
            self.vscroll.show()
5faa38c10b67 Add ScrollView widget. Update Emitter, rename "on_event" methods to "_handle_event". Update VScrollbar, Layout.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    62