tuikit/core/container.py
author Radek Brich <radek.brich@devl.cz>
Sun, 22 Feb 2015 09:53:13 +0100
changeset 119 dd91747504dd
parent 118 8c7970520632
permissions -rw-r--r--
Redraw widgets on request. Add scrollbar demo.
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
119
dd91747504dd Redraw widgets on request. Add scrollbar demo.
Radek Brich <radek.brich@devl.cz>
parents: 118
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
116
165b5d65e1cb Drop AnchorLayout, merge its features into FixedLayout.
Radek Brich <radek.brich@devl.cz>
parents: 115
diff changeset
    26
    def add(self, widget, *args, **kwargs):
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)
119
dd91747504dd Redraw widgets on request. Add scrollbar demo.
Radek Brich <radek.brich@devl.cz>
parents: 118
diff changeset
    32
        widget.timer = self.timer
116
165b5d65e1cb Drop AnchorLayout, merge its features into FixedLayout.
Radek Brich <radek.brich@devl.cz>
parents: 115
diff changeset
    33
        self.layout.add(widget, *args, **kwargs)
119
dd91747504dd Redraw widgets on request. Add scrollbar demo.
Radek Brich <radek.brich@devl.cz>
parents: 118
diff changeset
    34
        if self._focus_widget is None and widget.can_focus():
dd91747504dd Redraw widgets on request. Add scrollbar demo.
Radek Brich <radek.brich@devl.cz>
parents: 118
diff changeset
    35
            self._focus_widget = widget
dd91747504dd Redraw widgets on request. Add scrollbar demo.
Radek Brich <radek.brich@devl.cz>
parents: 118
diff changeset
    36
        widget.redraw()
93
c1e79acb9fcb Add Layout, FixedLayout.
Radek Brich <radek.brich@devl.cz>
parents: 92
diff changeset
    37
c1e79acb9fcb Add Layout, FixedLayout.
Radek Brich <radek.brich@devl.cz>
parents: 92
diff changeset
    38
    def resize(self, w, h):
c1e79acb9fcb Add Layout, FixedLayout.
Radek Brich <radek.brich@devl.cz>
parents: 92
diff changeset
    39
        Widget.resize(self, w, h)
115
b4ff7392003a GridLayout: basic implementation.
Radek Brich <radek.brich@devl.cz>
parents: 114
diff changeset
    40
        self.layout.update(w, h)
89
94f5baef19ac Add Theme, Button.
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
    41
119
dd91747504dd Redraw widgets on request. Add scrollbar demo.
Radek Brich <radek.brich@devl.cz>
parents: 118
diff changeset
    42
    # -------------------------------------------------------------------------
dd91747504dd Redraw widgets on request. Add scrollbar demo.
Radek Brich <radek.brich@devl.cz>
parents: 118
diff changeset
    43
    #   Presentation
dd91747504dd Redraw widgets on request. Add scrollbar demo.
Radek Brich <radek.brich@devl.cz>
parents: 118
diff changeset
    44
dd91747504dd Redraw widgets on request. Add scrollbar demo.
Radek Brich <radek.brich@devl.cz>
parents: 118
diff changeset
    45
    def draw_if_needed(self, buffer):
dd91747504dd Redraw widgets on request. Add scrollbar demo.
Radek Brich <radek.brich@devl.cz>
parents: 118
diff changeset
    46
        """Draw this and child widgets, whichever was requested by redraw."""
dd91747504dd Redraw widgets on request. Add scrollbar demo.
Radek Brich <radek.brich@devl.cz>
parents: 118
diff changeset
    47
        Widget.draw_if_needed(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
    48
        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
    49
            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
    50
                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
    51
                                 child.width, child.height):
119
dd91747504dd Redraw widgets on request. Add scrollbar demo.
Radek Brich <radek.brich@devl.cz>
parents: 118
diff changeset
    52
                    child.draw_if_needed(buffer)
dd91747504dd Redraw widgets on request. Add scrollbar demo.
Radek Brich <radek.brich@devl.cz>
parents: 118
diff changeset
    53
dd91747504dd Redraw widgets on request. Add scrollbar demo.
Radek Brich <radek.brich@devl.cz>
parents: 118
diff changeset
    54
    def redraw(self):
dd91747504dd Redraw widgets on request. Add scrollbar demo.
Radek Brich <radek.brich@devl.cz>
parents: 118
diff changeset
    55
        """Request redraw of all widgets in container."""
dd91747504dd Redraw widgets on request. Add scrollbar demo.
Radek Brich <radek.brich@devl.cz>
parents: 118
diff changeset
    56
        Widget.redraw(self)
dd91747504dd Redraw widgets on request. Add scrollbar demo.
Radek Brich <radek.brich@devl.cz>
parents: 118
diff changeset
    57
        for child in self._widgets:
dd91747504dd Redraw widgets on request. Add scrollbar demo.
Radek Brich <radek.brich@devl.cz>
parents: 118
diff changeset
    58
            child.redraw()
90
781774a8d568 Add timer, adjust inheritance of Widget, Container, Window.
Radek Brich <radek.brich@devl.cz>
parents: 89
diff changeset
    59
781774a8d568 Add timer, adjust inheritance of Widget, Container, Window.
Radek Brich <radek.brich@devl.cz>
parents: 89
diff changeset
    60
    def set_theme(self, theme):
781774a8d568 Add timer, adjust inheritance of Widget, Container, Window.
Radek Brich <radek.brich@devl.cz>
parents: 89
diff changeset
    61
        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
    62
        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
    63
            child.set_theme(theme)
97
0c2e0c09ba5c Add TextField widget, keypress event, cursor.
Radek Brich <radek.brich@devl.cz>
parents: 94
diff changeset
    64
0c2e0c09ba5c Add TextField widget, keypress event, cursor.
Radek Brich <radek.brich@devl.cz>
parents: 94
diff changeset
    65
    @property
0c2e0c09ba5c Add TextField widget, keypress event, cursor.
Radek Brich <radek.brich@devl.cz>
parents: 94
diff changeset
    66
    def cursor(self):
104
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
    67
        """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
    68
        or is set outside of widget boundaries.
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
    69
119
dd91747504dd Redraw widgets on request. Add scrollbar demo.
Radek Brich <radek.brich@devl.cz>
parents: 118
diff changeset
    70
        If this container has child with focus,
dd91747504dd Redraw widgets on request. Add scrollbar demo.
Radek Brich <radek.brich@devl.cz>
parents: 118
diff changeset
    71
        return its cursor position instead.
104
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
    72
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
    73
        """
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
    74
        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
    75
            cursor = self.focus_widget.cursor
108
11dac45bfba4 Fix cursor.
Radek Brich <radek.brich@devl.cz>
parents: 106
diff changeset
    76
            if not cursor:
11dac45bfba4 Fix cursor.
Radek Brich <radek.brich@devl.cz>
parents: 106
diff changeset
    77
                return None
113
Radek Brich <radek.brich@devl.cz>
parents: 112 108
diff changeset
    78
            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
    79
        else:
108
11dac45bfba4 Fix cursor.
Radek Brich <radek.brich@devl.cz>
parents: 106
diff changeset
    80
            cursor = self._cursor.immutable()
119
dd91747504dd Redraw widgets on request. Add scrollbar demo.
Radek Brich <radek.brich@devl.cz>
parents: 118
diff changeset
    81
        if cursor in Rect(0, 0, *self._size):
108
11dac45bfba4 Fix cursor.
Radek Brich <radek.brich@devl.cz>
parents: 106
diff changeset
    82
            return cursor
97
0c2e0c09ba5c Add TextField widget, keypress event, cursor.
Radek Brich <radek.brich@devl.cz>
parents: 94
diff changeset
    83
112
ce2e67e7bbb8 Refactor cursor.
Radek Brich <radek.brich@devl.cz>
parents: 111
diff changeset
    84
    @property
ce2e67e7bbb8 Refactor cursor.
Radek Brich <radek.brich@devl.cz>
parents: 111
diff changeset
    85
    def cursor_visible(self):
ce2e67e7bbb8 Refactor cursor.
Radek Brich <radek.brich@devl.cz>
parents: 111
diff changeset
    86
        if self.focus_widget:
ce2e67e7bbb8 Refactor cursor.
Radek Brich <radek.brich@devl.cz>
parents: 111
diff changeset
    87
            return self.focus_widget.cursor_visible
ce2e67e7bbb8 Refactor cursor.
Radek Brich <radek.brich@devl.cz>
parents: 111
diff changeset
    88
        else:
ce2e67e7bbb8 Refactor cursor.
Radek Brich <radek.brich@devl.cz>
parents: 111
diff changeset
    89
            return self._cursor_visible
97
0c2e0c09ba5c Add TextField widget, keypress event, cursor.
Radek Brich <radek.brich@devl.cz>
parents: 94
diff changeset
    90
119
dd91747504dd Redraw widgets on request. Add scrollbar demo.
Radek Brich <radek.brich@devl.cz>
parents: 118
diff changeset
    91
    # -------------------------------------------------------------------------
dd91747504dd Redraw widgets on request. Add scrollbar demo.
Radek Brich <radek.brich@devl.cz>
parents: 118
diff changeset
    92
    #   Events
97
0c2e0c09ba5c Add TextField widget, keypress event, cursor.
Radek Brich <radek.brich@devl.cz>
parents: 94
diff changeset
    93
111
b055add74b18 Refactor events.
Radek Brich <radek.brich@devl.cz>
parents: 109
diff changeset
    94
    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
    95
        # 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
    96
        if self.focus_widget is not None:
111
b055add74b18 Refactor events.
Radek Brich <radek.brich@devl.cz>
parents: 109
diff changeset
    97
            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
    98
                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
    99
        # Next, handle default key behaviour by Container
111
b055add74b18 Refactor events.
Radek Brich <radek.brich@devl.cz>
parents: 109
diff changeset
   100
        if ev.keyname == 'tab':
b055add74b18 Refactor events.
Radek Brich <radek.brich@devl.cz>
parents: 109
diff changeset
   101
            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
   102
        # 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
   103
        # and send keypress signal
111
b055add74b18 Refactor events.
Radek Brich <radek.brich@devl.cz>
parents: 109
diff changeset
   104
        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
   105
            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
   106
118
8c7970520632 Add mouse events, event demo.
Radek Brich <radek.brich@devl.cz>
parents: 116
diff changeset
   107
    def mousedown_event(self, ev):
8c7970520632 Add mouse events, event demo.
Radek Brich <radek.brich@devl.cz>
parents: 116
diff changeset
   108
        self.mouse_widget = None
8c7970520632 Add mouse events, event demo.
Radek Brich <radek.brich@devl.cz>
parents: 116
diff changeset
   109
        for child in reversed(self._widgets):
8c7970520632 Add mouse events, event demo.
Radek Brich <radek.brich@devl.cz>
parents: 116
diff changeset
   110
            if ev.pos in child.boundaries:
8c7970520632 Add mouse events, event demo.
Radek Brich <radek.brich@devl.cz>
parents: 116
diff changeset
   111
                child.mousedown_event(ev.rebase(child.pos))
8c7970520632 Add mouse events, event demo.
Radek Brich <radek.brich@devl.cz>
parents: 116
diff changeset
   112
                self.mouse_widget = child
106
abcadb7e2ef1 Use Point for mouse events, add them to Container and Widget.
Radek Brich <radek.brich@devl.cz>
parents: 104
diff changeset
   113
118
8c7970520632 Add mouse events, event demo.
Radek Brich <radek.brich@devl.cz>
parents: 116
diff changeset
   114
    def mouseup_event(self, ev):
8c7970520632 Add mouse events, event demo.
Radek Brich <radek.brich@devl.cz>
parents: 116
diff changeset
   115
        if self.mouse_widget:
8c7970520632 Add mouse events, event demo.
Radek Brich <radek.brich@devl.cz>
parents: 116
diff changeset
   116
            self.mouse_widget.mouseup_event(ev.rebase(self.mouse_widget.pos))
106
abcadb7e2ef1 Use Point for mouse events, add them to Container and Widget.
Radek Brich <radek.brich@devl.cz>
parents: 104
diff changeset
   117
118
8c7970520632 Add mouse events, event demo.
Radek Brich <radek.brich@devl.cz>
parents: 116
diff changeset
   118
    def mousemove_event(self, ev):
8c7970520632 Add mouse events, event demo.
Radek Brich <radek.brich@devl.cz>
parents: 116
diff changeset
   119
        if self.mouse_widget:
8c7970520632 Add mouse events, event demo.
Radek Brich <radek.brich@devl.cz>
parents: 116
diff changeset
   120
            self.mouse_widget.mousemove_event(ev.rebase(self.mouse_widget.pos))
106
abcadb7e2ef1 Use Point for mouse events, add them to Container and Widget.
Radek Brich <radek.brich@devl.cz>
parents: 104
diff changeset
   121
119
dd91747504dd Redraw widgets on request. Add scrollbar demo.
Radek Brich <radek.brich@devl.cz>
parents: 118
diff changeset
   122
    # -------------------------------------------------------------------------
dd91747504dd Redraw widgets on request. Add scrollbar demo.
Radek Brich <radek.brich@devl.cz>
parents: 118
diff changeset
   123
    #   Focus
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
   124
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
    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
   126
        """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
   127
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
        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
   129
        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
   130
        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
   131
        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
   132
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
         * 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
   134
           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
   135
           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
   136
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
         * 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
   138
           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
   139
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
        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
   141
        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
   142
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
        """
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
        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
   145
            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
   146
        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
   147
            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
   148
        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
   149
        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
   150
        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
   151
            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
   152
            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
   153
                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
   154
                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
   155
            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
   156
                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
   157
                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
   158
            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
   159
                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
   160
            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
   161
                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
   162
                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
   163
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
   164
    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
   165
        """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
   166
        self.focus_next(-1)
119
dd91747504dd Redraw widgets on request. Add scrollbar demo.
Radek Brich <radek.brich@devl.cz>
parents: 118
diff changeset
   167
dd91747504dd Redraw widgets on request. Add scrollbar demo.
Radek Brich <radek.brich@devl.cz>
parents: 118
diff changeset
   168
    @property
dd91747504dd Redraw widgets on request. Add scrollbar demo.
Radek Brich <radek.brich@devl.cz>
parents: 118
diff changeset
   169
    def focus_widget(self):
dd91747504dd Redraw widgets on request. Add scrollbar demo.
Radek Brich <radek.brich@devl.cz>
parents: 118
diff changeset
   170
        return self._focus_widget
dd91747504dd Redraw widgets on request. Add scrollbar demo.
Radek Brich <radek.brich@devl.cz>
parents: 118
diff changeset
   171
dd91747504dd Redraw widgets on request. Add scrollbar demo.
Radek Brich <radek.brich@devl.cz>
parents: 118
diff changeset
   172
    @focus_widget.setter
dd91747504dd Redraw widgets on request. Add scrollbar demo.
Radek Brich <radek.brich@devl.cz>
parents: 118
diff changeset
   173
    def focus_widget(self, widget):
dd91747504dd Redraw widgets on request. Add scrollbar demo.
Radek Brich <radek.brich@devl.cz>
parents: 118
diff changeset
   174
        orig_focus_widget = self._focus_widget
dd91747504dd Redraw widgets on request. Add scrollbar demo.
Radek Brich <radek.brich@devl.cz>
parents: 118
diff changeset
   175
        self._focus_widget = widget
dd91747504dd Redraw widgets on request. Add scrollbar demo.
Radek Brich <radek.brich@devl.cz>
parents: 118
diff changeset
   176
        widget.redraw()
dd91747504dd Redraw widgets on request. Add scrollbar demo.
Radek Brich <radek.brich@devl.cz>
parents: 118
diff changeset
   177
        if orig_focus_widget:
dd91747504dd Redraw widgets on request. Add scrollbar demo.
Radek Brich <radek.brich@devl.cz>
parents: 118
diff changeset
   178
            orig_focus_widget.redraw()