tuikit/core/container.py
author Radek Brich <radek.brich@devl.cz>
Mon, 01 Sep 2014 08:45:51 +0200
changeset 104 742e504ec053
parent 97 0c2e0c09ba5c
child 106 abcadb7e2ef1
permissions -rw-r--r--
Update TextBox: Replace "spot" with "cursor".

from tuikit.core.widget import Widget
from tuikit.core.coords import Point
from tuikit.layouts.fixed import FixedLayout


class Container(Widget):

    """Container widget.

    Can contain other widgets to create hierarchical structure.

    """

    def __init__(self, layout_class=FixedLayout):
        Widget.__init__(self)
        #: List of child widgets.
        self.children = []
        self.focus_child = None
        self.layout = layout_class()

    def add(self, widget):
        """Add widget into container."""
        self.children.append(widget)
        widget.parent = self
        widget.window = self.window
        widget.set_theme(self.theme)
        self.layout.add(widget)
        if self.focus_child is None:
            self.focus_child = widget

    def resize(self, w, h):
        Widget.resize(self, w, h)
        self.layout.resize()

    def draw(self, buffer):
        """Draw child widgets."""
        Widget.draw(self, buffer)
        for child in self.children:
            with buffer.moved_origin(child.x, child.y):
                with buffer.clip(buffer.origin.x, buffer.origin.y,
                                 child.width, child.height):
                    child.draw(buffer)

    def set_theme(self, theme):
        Widget.set_theme(self, theme)
        for child in self.children:
            child.set_theme(theme)

    @property
    def cursor(self):
        """Return cursor coordinates or None if cursor is not set
        or is set outside of widget boundaries.

        If this container has child with focus, return its cursor position instead.

        """
        if self.focus_child:
            cursor = self.focus_child.cursor
            if cursor is not None:
                return cursor.moved(*self.focus_child.pos)
        else:
            if self._cursor in Rect._make((0, 0), self._size):
                return self._cursor

    ## input events ##

    def keypress(self, keyname, char, mod=0):
        if self.focus_child:
            self.focus_child.keypress(keyname, char, mod)