tuikit/core/container.py
author Radek Brich <radek.brich@devl.cz>
Sun, 15 Feb 2015 12:52:46 +0100
changeset 115 b4ff7392003a
parent 114 26c02bd94bd9
child 116 165b5d65e1cb
permissions -rw-r--r--
GridLayout: basic implementation.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
90
781774a8d568 Add timer, adjust inheritance of Widget, Container, Window.
Radek Brich <radek.brich@devl.cz>
parents: 89
diff changeset
     1
from tuikit.core.widget import Widget
108
11dac45bfba4 Fix cursor.
Radek Brich <radek.brich@devl.cz>
parents: 106
diff changeset
     2
from tuikit.core.coords import Point, Rect
93
c1e79acb9fcb Add Layout, FixedLayout.
Radek Brich <radek.brich@devl.cz>
parents: 92
diff changeset
     3
from tuikit.layouts.fixed import FixedLayout
89
94f5baef19ac Add Theme, Button.
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
     4
94f5baef19ac Add Theme, Button.
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
     5
90
781774a8d568 Add timer, adjust inheritance of Widget, Container, Window.
Radek Brich <radek.brich@devl.cz>
parents: 89
diff changeset
     6
class Container(Widget):
781774a8d568 Add timer, adjust inheritance of Widget, Container, Window.
Radek Brich <radek.brich@devl.cz>
parents: 89
diff changeset
     7
781774a8d568 Add timer, adjust inheritance of Widget, Container, Window.
Radek Brich <radek.brich@devl.cz>
parents: 89
diff changeset
     8
    """Container widget.
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     9
90
781774a8d568 Add timer, adjust inheritance of Widget, Container, Window.
Radek Brich <radek.brich@devl.cz>
parents: 89
diff changeset
    10
    Can contain other widgets to create hierarchical structure.
781774a8d568 Add timer, adjust inheritance of Widget, Container, Window.
Radek Brich <radek.brich@devl.cz>
parents: 89
diff changeset
    11
781774a8d568 Add timer, adjust inheritance of Widget, Container, Window.
Radek Brich <radek.brich@devl.cz>
parents: 89
diff changeset
    12
    """
32
088b92ffb119 Clean up, refactoring. Rename EventSource to Emitter, begin merging emit() method with handle().
Radek Brich <radek.brich@devl.cz>
parents: 23
diff changeset
    13
93
c1e79acb9fcb Add Layout, FixedLayout.
Radek Brich <radek.brich@devl.cz>
parents: 92
diff changeset
    14
    def __init__(self, layout_class=FixedLayout):
90
781774a8d568 Add timer, adjust inheritance of Widget, Container, Window.
Radek Brich <radek.brich@devl.cz>
parents: 89
diff changeset
    15
        Widget.__init__(self)
13
19ebde2fd594 Add more generated documentation.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
    16
        #: List of child widgets.
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
    17
        self._widgets = []
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
    18
        #: Widget with keyboard focus
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
    19
        self.focus_widget = None
113
Radek Brich <radek.brich@devl.cz>
parents: 112 108
diff changeset
    20
        #: Widget on last mouse position
Radek Brich <radek.brich@devl.cz>
parents: 112 108
diff changeset
    21
        self.mouse_widget = None
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
    22
        #: If True, tab cycles inside container
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
    23
        self.trap_focus = False
93
c1e79acb9fcb Add Layout, FixedLayout.
Radek Brich <radek.brich@devl.cz>
parents: 92
diff changeset
    24
        self.layout = layout_class()
9
7175ed629a76 Added ComboBox, HorizontalLayout, TreeNode, TreeModel, TreeView. Widget is now descendant of EventSource. Improved color management (color prefixes).
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
    25
90
781774a8d568 Add timer, adjust inheritance of Widget, Container, Window.
Radek Brich <radek.brich@devl.cz>
parents: 89
diff changeset
    26
    def add(self, widget):
87
ee5ea9671f28 Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
    27
        """Add widget into container."""
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
    28
        self._widgets.append(widget)
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    29
        widget.parent = self
90
781774a8d568 Add timer, adjust inheritance of Widget, Container, Window.
Radek Brich <radek.brich@devl.cz>
parents: 89
diff changeset
    30
        widget.window = self.window
89
94f5baef19ac Add Theme, Button.
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
    31
        widget.set_theme(self.theme)
93
c1e79acb9fcb Add Layout, FixedLayout.
Radek Brich <radek.brich@devl.cz>
parents: 92
diff changeset
    32
        self.layout.add(widget)
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
    33
        if self.focus_widget is None and widget.can_focus():
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
    34
            self.focus_widget = widget
93
c1e79acb9fcb Add Layout, FixedLayout.
Radek Brich <radek.brich@devl.cz>
parents: 92
diff changeset
    35
c1e79acb9fcb Add Layout, FixedLayout.
Radek Brich <radek.brich@devl.cz>
parents: 92
diff changeset
    36
    def resize(self, w, h):
c1e79acb9fcb Add Layout, FixedLayout.
Radek Brich <radek.brich@devl.cz>
parents: 92
diff changeset
    37
        Widget.resize(self, w, h)
115
b4ff7392003a GridLayout: basic implementation.
Radek Brich <radek.brich@devl.cz>
parents: 114
diff changeset
    38
        self.layout.update(w, h)
89
94f5baef19ac Add Theme, Button.
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
    39
94
e50dae408fe9 Add origin to Buffer. Use it to simplify hierarchical drawing.
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
    40
    def draw(self, buffer):
87
ee5ea9671f28 Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
    41
        """Draw child widgets."""
94
e50dae408fe9 Add origin to Buffer. Use it to simplify hierarchical drawing.
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
    42
        Widget.draw(self, buffer)
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
    43
        for child in self._widgets:
94
e50dae408fe9 Add origin to Buffer. Use it to simplify hierarchical drawing.
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
    44
            with buffer.moved_origin(child.x, child.y):
e50dae408fe9 Add origin to Buffer. Use it to simplify hierarchical drawing.
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
    45
                with buffer.clip(buffer.origin.x, buffer.origin.y,
e50dae408fe9 Add origin to Buffer. Use it to simplify hierarchical drawing.
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
    46
                                 child.width, child.height):
e50dae408fe9 Add origin to Buffer. Use it to simplify hierarchical drawing.
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
    47
                    child.draw(buffer)
90
781774a8d568 Add timer, adjust inheritance of Widget, Container, Window.
Radek Brich <radek.brich@devl.cz>
parents: 89
diff changeset
    48
781774a8d568 Add timer, adjust inheritance of Widget, Container, Window.
Radek Brich <radek.brich@devl.cz>
parents: 89
diff changeset
    49
    def set_theme(self, theme):
781774a8d568 Add timer, adjust inheritance of Widget, Container, Window.
Radek Brich <radek.brich@devl.cz>
parents: 89
diff changeset
    50
        Widget.set_theme(self, theme)
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
    51
        for child in self._widgets:
90
781774a8d568 Add timer, adjust inheritance of Widget, Container, Window.
Radek Brich <radek.brich@devl.cz>
parents: 89
diff changeset
    52
            child.set_theme(theme)
97
0c2e0c09ba5c Add TextField widget, keypress event, cursor.
Radek Brich <radek.brich@devl.cz>
parents: 94
diff changeset
    53
0c2e0c09ba5c Add TextField widget, keypress event, cursor.
Radek Brich <radek.brich@devl.cz>
parents: 94
diff changeset
    54
    @property
0c2e0c09ba5c Add TextField widget, keypress event, cursor.
Radek Brich <radek.brich@devl.cz>
parents: 94
diff changeset
    55
    def cursor(self):
104
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
    56
        """Return cursor coordinates or None if cursor is not set
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
    57
        or is set outside of widget boundaries.
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
    58
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
    59
        If this container has child with focus, return its cursor position instead.
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
    60
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
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
        if self.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
    63
            cursor = self.focus_widget.cursor
108
11dac45bfba4 Fix cursor.
Radek Brich <radek.brich@devl.cz>
parents: 106
diff changeset
    64
            if not cursor:
11dac45bfba4 Fix cursor.
Radek Brich <radek.brich@devl.cz>
parents: 106
diff changeset
    65
                return None
113
Radek Brich <radek.brich@devl.cz>
parents: 112 108
diff changeset
    66
            cursor = cursor.moved(*self.focus_widget.pos)
97
0c2e0c09ba5c Add TextField widget, keypress event, cursor.
Radek Brich <radek.brich@devl.cz>
parents: 94
diff changeset
    67
        else:
108
11dac45bfba4 Fix cursor.
Radek Brich <radek.brich@devl.cz>
parents: 106
diff changeset
    68
            cursor = self._cursor.immutable()
11dac45bfba4 Fix cursor.
Radek Brich <radek.brich@devl.cz>
parents: 106
diff changeset
    69
        if cursor in Rect._make((0, 0), self._size):
11dac45bfba4 Fix cursor.
Radek Brich <radek.brich@devl.cz>
parents: 106
diff changeset
    70
            return cursor
97
0c2e0c09ba5c Add TextField widget, keypress event, cursor.
Radek Brich <radek.brich@devl.cz>
parents: 94
diff changeset
    71
112
ce2e67e7bbb8 Refactor cursor.
Radek Brich <radek.brich@devl.cz>
parents: 111
diff changeset
    72
    @property
ce2e67e7bbb8 Refactor cursor.
Radek Brich <radek.brich@devl.cz>
parents: 111
diff changeset
    73
    def cursor_visible(self):
ce2e67e7bbb8 Refactor cursor.
Radek Brich <radek.brich@devl.cz>
parents: 111
diff changeset
    74
        if self.focus_widget:
ce2e67e7bbb8 Refactor cursor.
Radek Brich <radek.brich@devl.cz>
parents: 111
diff changeset
    75
            return self.focus_widget.cursor_visible
ce2e67e7bbb8 Refactor cursor.
Radek Brich <radek.brich@devl.cz>
parents: 111
diff changeset
    76
        else:
ce2e67e7bbb8 Refactor cursor.
Radek Brich <radek.brich@devl.cz>
parents: 111
diff changeset
    77
            return self._cursor_visible
97
0c2e0c09ba5c Add TextField widget, keypress event, cursor.
Radek Brich <radek.brich@devl.cz>
parents: 94
diff changeset
    78
0c2e0c09ba5c Add TextField widget, keypress event, cursor.
Radek Brich <radek.brich@devl.cz>
parents: 94
diff changeset
    79
    ## input events ##
0c2e0c09ba5c Add TextField widget, keypress event, cursor.
Radek Brich <radek.brich@devl.cz>
parents: 94
diff changeset
    80
111
b055add74b18 Refactor events.
Radek Brich <radek.brich@devl.cz>
parents: 109
diff changeset
    81
    def keypress_event(self, ev):
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
    82
        # First, handle the keypress event to focused child 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
    83
        if self.focus_widget is not None:
111
b055add74b18 Refactor events.
Radek Brich <radek.brich@devl.cz>
parents: 109
diff changeset
    84
            if self.focus_widget.keypress_event(ev):
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
    85
                return 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
    86
        # Next, handle default key behaviour by Container
111
b055add74b18 Refactor events.
Radek Brich <radek.brich@devl.cz>
parents: 109
diff changeset
    87
        if ev.keyname == 'tab':
b055add74b18 Refactor events.
Radek Brich <radek.brich@devl.cz>
parents: 109
diff changeset
    88
            return self.focus_next(-1 if 'shift' in ev.mods else 1)
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
    89
        # Finally, handle default keys by 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
    90
        # and send keypress signal
111
b055add74b18 Refactor events.
Radek Brich <radek.brich@devl.cz>
parents: 109
diff changeset
    91
        if Widget.keypress_event(self, ev):
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
    92
            return True
106
abcadb7e2ef1 Use Point for mouse events, add them to Container and Widget.
Radek Brich <radek.brich@devl.cz>
parents: 104
diff changeset
    93
abcadb7e2ef1 Use Point for mouse events, add them to Container and Widget.
Radek Brich <radek.brich@devl.cz>
parents: 104
diff changeset
    94
    def mousedown(self, button, pos):
abcadb7e2ef1 Use Point for mouse events, add them to Container and Widget.
Radek Brich <radek.brich@devl.cz>
parents: 104
diff changeset
    95
        self.mouse_child = None
abcadb7e2ef1 Use Point for mouse events, add them to Container and Widget.
Radek Brich <radek.brich@devl.cz>
parents: 104
diff changeset
    96
        for child in reversed(self.children):
abcadb7e2ef1 Use Point for mouse events, add them to Container and Widget.
Radek Brich <radek.brich@devl.cz>
parents: 104
diff changeset
    97
            if pos in child.boundaries:
abcadb7e2ef1 Use Point for mouse events, add them to Container and Widget.
Radek Brich <radek.brich@devl.cz>
parents: 104
diff changeset
    98
                child.mousedown(button, pos - child.pos)
abcadb7e2ef1 Use Point for mouse events, add them to Container and Widget.
Radek Brich <radek.brich@devl.cz>
parents: 104
diff changeset
    99
                self.mouse_child = child
abcadb7e2ef1 Use Point for mouse events, add them to Container and Widget.
Radek Brich <radek.brich@devl.cz>
parents: 104
diff changeset
   100
abcadb7e2ef1 Use Point for mouse events, add them to Container and Widget.
Radek Brich <radek.brich@devl.cz>
parents: 104
diff changeset
   101
    def mouseup(self, button, pos):
abcadb7e2ef1 Use Point for mouse events, add them to Container and Widget.
Radek Brich <radek.brich@devl.cz>
parents: 104
diff changeset
   102
        if self.mouse_child:
abcadb7e2ef1 Use Point for mouse events, add them to Container and Widget.
Radek Brich <radek.brich@devl.cz>
parents: 104
diff changeset
   103
            self.mouse_child.mouseup(button, pos - self.mouse_child.pos)
abcadb7e2ef1 Use Point for mouse events, add them to Container and Widget.
Radek Brich <radek.brich@devl.cz>
parents: 104
diff changeset
   104
abcadb7e2ef1 Use Point for mouse events, add them to Container and Widget.
Radek Brich <radek.brich@devl.cz>
parents: 104
diff changeset
   105
    def mousemove(self, button, pos, relpos):
abcadb7e2ef1 Use Point for mouse events, add them to Container and Widget.
Radek Brich <radek.brich@devl.cz>
parents: 104
diff changeset
   106
        if self.mouse_child:
abcadb7e2ef1 Use Point for mouse events, add them to Container and Widget.
Radek Brich <radek.brich@devl.cz>
parents: 104
diff changeset
   107
            self.mouse_child.mousemove(button,
abcadb7e2ef1 Use Point for mouse events, add them to Container and Widget.
Radek Brich <radek.brich@devl.cz>
parents: 104
diff changeset
   108
                pos - self.mouse_child.pos, relpos)
abcadb7e2ef1 Use Point for mouse events, add them to Container and Widget.
Radek Brich <radek.brich@devl.cz>
parents: 104
diff changeset
   109
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
   110
    ## focus ##
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
   111
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
   112
    def focus_next(self, step=1):
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
   113
        """Focus next child.
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
   114
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
   115
        Sets focus to next child, if there is one
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
   116
        which can be focused. Cycles from last child
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
   117
        to first when needed. Return value depends on
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
   118
        this cycling:
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
   119
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
   120
         * False means there wasn't any child to focus
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
   121
           before end of list. Focus was either not changed
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
   122
           or first child was focused.
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
   123
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
   124
         * True when focus is set to next child in normal
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
   125
           way or when self.trap_focus is set.
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
   126
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
   127
        Return value is supposed to be returned from keypress
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
   128
        event - in that case, True stops event propagation.
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
   129
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
   130
        """
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
   131
        if self.focus_widget is None:
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
   132
            idx_current = 0
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
   133
        else:
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
   134
            idx_current = self._widgets.index(self.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
   135
        idx_new = idx_current
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
   136
        cycled = False
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
   137
        while 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
   138
            idx_new += step
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
   139
            if idx_new >= len(self._widgets):
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
   140
                idx_new = 0
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
   141
                cycled = 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
   142
            if idx_new < 0:  # for focus_previous
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
   143
                idx_new = len(self._widgets) - 1
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
   144
                cycled = 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
   145
            if idx_current == idx_new:
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
   146
                return False
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
   147
            if self._widgets[idx_new].can_focus():
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
   148
                self.focus_widget = self._widgets[idx_new]
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
   149
                return self.trap_focus or not cycled
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
   150
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
   151
    def focus_previous(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
   152
        """Focus previous child."""
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
   153
        self.focus_next(-1)