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.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
86
0978fb755d31 Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     1
from tuikit.core.buffer import Buffer
0978fb755d31 Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     2
from tuikit.core.signal import Signal
87
ee5ea9671f28 Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents: 86
diff changeset
     3
from tuikit.core.container import Container
86
0978fb755d31 Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     4
0978fb755d31 Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     5
87
ee5ea9671f28 Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents: 86
diff changeset
     6
class Window(Container):
86
0978fb755d31 Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     7
0978fb755d31 Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     8
    """Window is rectangular part of screen containing widgets.
0978fb755d31 Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     9
0978fb755d31 Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    10
    Widgets are drawn into window, events are routed to widgets through window.
0978fb755d31 Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    11
97
0c2e0c09ba5c Add TextField widget, keypress event, cursor.
Radek Brich <radek.brich@devl.cz>
parents: 95
diff changeset
    12
    Parent of Window is always WindowManager.
0c2e0c09ba5c Add TextField widget, keypress event, cursor.
Radek Brich <radek.brich@devl.cz>
parents: 95
diff changeset
    13
86
0978fb755d31 Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    14
    """
0978fb755d31 Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    15
0978fb755d31 Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    16
    def __init__(self, buffer=None):
0978fb755d31 Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    17
        """New buffer for the window will be created unless given existing
0978fb755d31 Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    18
        `buffer` as parameter."""
87
ee5ea9671f28 Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents: 86
diff changeset
    19
        Container.__init__(self)
86
0978fb755d31 Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    20
        self._buffer = None
0978fb755d31 Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    21
        self.buffer = buffer or Buffer()
0978fb755d31 Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    22
0978fb755d31 Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    23
    @property
0978fb755d31 Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    24
    def buffer(self):
0978fb755d31 Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    25
        return self._buffer
0978fb755d31 Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    26
0978fb755d31 Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    27
    @buffer.setter
0978fb755d31 Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    28
    def buffer(self, buffer):
0978fb755d31 Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    29
        # replace the buffer
0978fb755d31 Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    30
        self._buffer = buffer
93
c1e79acb9fcb Add Layout, FixedLayout.
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
    31
c1e79acb9fcb Add Layout, FixedLayout.
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
    32
    @property
c1e79acb9fcb Add Layout, FixedLayout.
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
    33
    def size(self):
c1e79acb9fcb Add Layout, FixedLayout.
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
    34
        return self.buffer.size
86
0978fb755d31 Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    35
0978fb755d31 Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    36
    def resize(self, w, h):
90
781774a8d568 Add timer, adjust inheritance of Widget, Container, Window.
Radek Brich <radek.brich@devl.cz>
parents: 89
diff changeset
    37
        """Resize buffer when window gets resized."""
93
c1e79acb9fcb Add Layout, FixedLayout.
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
    38
        Container.resize(self, w, h)
88
90d00354dc70 Add WindowManager, basic event handling.
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
    39
        self.buffer.resize(w, h)
90d00354dc70 Add WindowManager, basic event handling.
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
    40
        self.redraw()
90d00354dc70 Add WindowManager, basic event handling.
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
    41
90d00354dc70 Add WindowManager, basic event handling.
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
    42
    def redraw(self):
94
e50dae408fe9 Add origin to Buffer. Use it to simplify hierarchical drawing.
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
    43
        self.buffer.reset_origin()
88
90d00354dc70 Add WindowManager, basic event handling.
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
    44
        Container.draw(self, self.buffer)
95
05392e369ede Refactoring: Swap drawing operations parameters. Coords are now last and have default values.
Radek Brich <radek.brich@devl.cz>
parents: 94
diff changeset
    45
        self.buffer.puts('{0.w} {0.h}'.format(self.size), 10, 5)
88
90d00354dc70 Add WindowManager, basic event handling.
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
    46
        self.buffer.frame()
86
0978fb755d31 Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    47
94
e50dae408fe9 Add origin to Buffer. Use it to simplify hierarchical drawing.
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
    48
    def draw(self, buffer):
e50dae408fe9 Add origin to Buffer. Use it to simplify hierarchical drawing.
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
    49
        """Draw this window into `buffer`."""
97
0c2e0c09ba5c Add TextField widget, keypress event, cursor.
Radek Brich <radek.brich@devl.cz>
parents: 95
diff changeset
    50
        self.redraw()
94
e50dae408fe9 Add origin to Buffer. Use it to simplify hierarchical drawing.
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
    51
        buffer.draw(self.buffer)
88
90d00354dc70 Add WindowManager, basic event handling.
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
    52
90d00354dc70 Add WindowManager, basic event handling.
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
    53
90
781774a8d568 Add timer, adjust inheritance of Widget, Container, Window.
Radek Brich <radek.brich@devl.cz>
parents: 89
diff changeset
    54
class WindowManager(Container):
88
90d00354dc70 Add WindowManager, basic event handling.
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
    55
90
781774a8d568 Add timer, adjust inheritance of Widget, Container, Window.
Radek Brich <radek.brich@devl.cz>
parents: 89
diff changeset
    56
    def __init__(self, timer):
781774a8d568 Add timer, adjust inheritance of Widget, Container, Window.
Radek Brich <radek.brich@devl.cz>
parents: 89
diff changeset
    57
        Container.__init__(self)
781774a8d568 Add timer, adjust inheritance of Widget, Container, Window.
Radek Brich <radek.brich@devl.cz>
parents: 89
diff changeset
    58
        self.timer = timer
88
90d00354dc70 Add WindowManager, basic event handling.
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
    59
90d00354dc70 Add WindowManager, basic event handling.
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
    60
    def resize(self, w, h):
90
781774a8d568 Add timer, adjust inheritance of Widget, Container, Window.
Radek Brich <radek.brich@devl.cz>
parents: 89
diff changeset
    61
        self.children[0].resize(w, h)
88
90d00354dc70 Add WindowManager, basic event handling.
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
    62
90d00354dc70 Add WindowManager, basic event handling.
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
    63
    def handle_event(self, event_name, *args):
90d00354dc70 Add WindowManager, basic event handling.
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
    64
        """Handle input event to managed windows."""
90d00354dc70 Add WindowManager, basic event handling.
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
    65
        handler = getattr(self, event_name, None)
90d00354dc70 Add WindowManager, basic event handling.
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
    66
        if handler:
90d00354dc70 Add WindowManager, basic event handling.
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
    67
            handler(*args)
90d00354dc70 Add WindowManager, basic event handling.
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
    68
        else:
90d00354dc70 Add WindowManager, basic event handling.
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
    69
            raise Exception('Unknown event: %r %r' % (event_name, args))