tuikit/core/window.py
author Radek Brich <radek.brich@devl.cz>
Wed, 03 Sep 2014 19:17:04 +0200
changeset 112 ce2e67e7bbb8
parent 111 b055add74b18
child 113 6796adfdc7eb
permissions -rw-r--r--
Refactor 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)
109
105b1affc3c2 Update keypress propagation. Allow focus change by tab key. Add log property to Widget for smart logging.
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
    20
        self.allow_focus = True
105b1affc3c2 Update keypress propagation. Allow focus change by tab key. Add log property to Widget for smart logging.
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
    21
        self.trap_focus = True
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
    22
        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
    23
        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
    24
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
    @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
    26
    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
    27
        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
    28
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
    @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
    30
    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
    31
        # 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
    32
        self._buffer = buffer
93
c1e79acb9fcb Add Layout, FixedLayout.
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
    33
c1e79acb9fcb Add Layout, FixedLayout.
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
    34
    @property
c1e79acb9fcb Add Layout, FixedLayout.
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
    35
    def size(self):
c1e79acb9fcb Add Layout, FixedLayout.
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
    36
        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
    37
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
    38
    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
    39
        """Resize buffer when window gets resized."""
93
c1e79acb9fcb Add Layout, FixedLayout.
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
    40
        Container.resize(self, w, h)
88
90d00354dc70 Add WindowManager, basic event handling.
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
    41
        self.buffer.resize(w, h)
90d00354dc70 Add WindowManager, basic event handling.
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
    42
        self.redraw()
90d00354dc70 Add WindowManager, basic event handling.
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
    43
90d00354dc70 Add WindowManager, basic event handling.
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
    44
    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
    45
        self.buffer.reset_origin()
88
90d00354dc70 Add WindowManager, basic event handling.
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
    46
        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
    47
        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
    48
        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
    49
94
e50dae408fe9 Add origin to Buffer. Use it to simplify hierarchical drawing.
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
    50
    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
    51
        """Draw this window into `buffer`."""
97
0c2e0c09ba5c Add TextField widget, keypress event, cursor.
Radek Brich <radek.brich@devl.cz>
parents: 95
diff changeset
    52
        self.redraw()
94
e50dae408fe9 Add origin to Buffer. Use it to simplify hierarchical drawing.
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
    53
        buffer.draw(self.buffer)
88
90d00354dc70 Add WindowManager, basic event handling.
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
    54
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
class WindowManager(Container):
88
90d00354dc70 Add WindowManager, basic event handling.
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
    57
90
781774a8d568 Add timer, adjust inheritance of Widget, Container, Window.
Radek Brich <radek.brich@devl.cz>
parents: 89
diff changeset
    58
    def __init__(self, timer):
781774a8d568 Add timer, adjust inheritance of Widget, Container, Window.
Radek Brich <radek.brich@devl.cz>
parents: 89
diff changeset
    59
        Container.__init__(self)
781774a8d568 Add timer, adjust inheritance of Widget, Container, Window.
Radek Brich <radek.brich@devl.cz>
parents: 89
diff changeset
    60
        self.timer = timer
88
90d00354dc70 Add WindowManager, basic event handling.
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
    61
109
105b1affc3c2 Update keypress propagation. Allow focus change by tab key. Add log property to Widget for smart logging.
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
    62
    def draw(self, buffer):
105b1affc3c2 Update keypress propagation. Allow focus change by tab key. Add log property to Widget for smart logging.
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
    63
        Container.draw(self, buffer)
105b1affc3c2 Update keypress propagation. Allow focus change by tab key. Add log property to Widget for smart logging.
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
    64
        self.log.debug('%s has focus.', self.get_focused_widget().name)
105b1affc3c2 Update keypress propagation. Allow focus change by tab key. Add log property to Widget for smart logging.
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
    65
88
90d00354dc70 Add WindowManager, basic event handling.
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
    66
    def resize(self, w, h):
109
105b1affc3c2 Update keypress propagation. Allow focus change by tab key. Add log property to Widget for smart logging.
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
    67
        self._widgets[0].resize(w, h)
105b1affc3c2 Update keypress propagation. Allow focus change by tab key. Add log property to Widget for smart logging.
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
    68
111
b055add74b18 Refactor events.
Radek Brich <radek.brich@devl.cz>
parents: 109
diff changeset
    69
    def keypress_event(self, ev):
b055add74b18 Refactor events.
Radek Brich <radek.brich@devl.cz>
parents: 109
diff changeset
    70
        self.log.debug('%s', ev)
b055add74b18 Refactor events.
Radek Brich <radek.brich@devl.cz>
parents: 109
diff changeset
    71
        return Container.keypress_event(self, ev)
88
90d00354dc70 Add WindowManager, basic event handling.
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
    72
111
b055add74b18 Refactor events.
Radek Brich <radek.brich@devl.cz>
parents: 109
diff changeset
    73
    def handle_event(self, event):
88
90d00354dc70 Add WindowManager, basic event handling.
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
    74
        """Handle input event to managed windows."""
111
b055add74b18 Refactor events.
Radek Brich <radek.brich@devl.cz>
parents: 109
diff changeset
    75
        handler = getattr(self, event.name + '_event', None)
88
90d00354dc70 Add WindowManager, basic event handling.
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
    76
        if handler:
111
b055add74b18 Refactor events.
Radek Brich <radek.brich@devl.cz>
parents: 109
diff changeset
    77
            handler(event)
88
90d00354dc70 Add WindowManager, basic event handling.
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
    78
        else:
111
b055add74b18 Refactor events.
Radek Brich <radek.brich@devl.cz>
parents: 109
diff changeset
    79
            raise Exception('Unknown event: %r' % event)
109
105b1affc3c2 Update keypress propagation. Allow focus change by tab key. Add log property to Widget for smart logging.
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
    80
105b1affc3c2 Update keypress propagation. Allow focus change by tab key. Add log property to Widget for smart logging.
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
    81
    def get_focused_widget(self):
105b1affc3c2 Update keypress propagation. Allow focus change by tab key. Add log property to Widget for smart logging.
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
    82
        """Traverse the widget hierarchy to bottom
105b1affc3c2 Update keypress propagation. Allow focus change by tab key. Add log property to Widget for smart logging.
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
    83
        and return actually focused Widget."""
105b1affc3c2 Update keypress propagation. Allow focus change by tab key. Add log property to Widget for smart logging.
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
    84
        node = self
105b1affc3c2 Update keypress propagation. Allow focus change by tab key. Add log property to Widget for smart logging.
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
    85
        while isinstance(node, Container) and node.focus_widget:
105b1affc3c2 Update keypress propagation. Allow focus change by tab key. Add log property to Widget for smart logging.
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
    86
            node = node.focus_widget
105b1affc3c2 Update keypress propagation. Allow focus change by tab key. Add log property to Widget for smart logging.
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
    87
        return node
105b1affc3c2 Update keypress propagation. Allow focus change by tab key. Add log property to Widget for smart logging.
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
    88
112
ce2e67e7bbb8 Refactor cursor.
Radek Brich <radek.brich@devl.cz>
parents: 111
diff changeset
    89
    def get_cursor_if_visible(self):
ce2e67e7bbb8 Refactor cursor.
Radek Brich <radek.brich@devl.cz>
parents: 111
diff changeset
    90
        if self.cursor_visible:
ce2e67e7bbb8 Refactor cursor.
Radek Brich <radek.brich@devl.cz>
parents: 111
diff changeset
    91
            return self.cursor