tuikit/core/window.py
author Radek Brich <radek.brich@devl.cz>
Fri, 28 Mar 2014 19:58:59 +0100
changeset 97 0c2e0c09ba5c
parent 95 05392e369ede
child 105 70c9cfc70cd6
child 109 105b1affc3c2
permissions -rw-r--r--
Add TextField widget, keypress event, cursor.

from tuikit.core.buffer import Buffer
from tuikit.core.signal import Signal
from tuikit.core.container import Container


class Window(Container):

    """Window is rectangular part of screen containing widgets.

    Widgets are drawn into window, events are routed to widgets through window.

    Parent of Window is always WindowManager.

    """

    def __init__(self, buffer=None):
        """New buffer for the window will be created unless given existing
        `buffer` as parameter."""
        Container.__init__(self)
        self._buffer = None
        self.buffer = buffer or Buffer()

    @property
    def buffer(self):
        return self._buffer

    @buffer.setter
    def buffer(self, buffer):
        # replace the buffer
        self._buffer = buffer

    @property
    def size(self):
        return self.buffer.size

    def resize(self, w, h):
        """Resize buffer when window gets resized."""
        Container.resize(self, w, h)
        self.buffer.resize(w, h)
        self.redraw()

    def redraw(self):
        self.buffer.reset_origin()
        Container.draw(self, self.buffer)
        self.buffer.puts('{0.w} {0.h}'.format(self.size), 10, 5)
        self.buffer.frame()

    def draw(self, buffer):
        """Draw this window into `buffer`."""
        self.redraw()
        buffer.draw(self.buffer)


class WindowManager(Container):

    def __init__(self, timer):
        Container.__init__(self)
        self.timer = timer

    def resize(self, w, h):
        self.children[0].resize(w, h)

    def handle_event(self, event_name, *args):
        """Handle input event to managed windows."""
        handler = getattr(self, event_name, None)
        if handler:
            handler(*args)
        else:
            raise Exception('Unknown event: %r %r' % (event_name, args))