tuikit/container.py
author Radek Brich <radek.brich@devl.cz>
Sun, 20 Jan 2013 00:49:19 +0100
changeset 64 03f591f5fe5c
parent 63 2a0e04091898
child 70 db2eab0beb45
permissions -rw-r--r--
Drop Container.offset, add special layout for that - OffsetLayout. Add Widget.bring_up(): moves child to end of children list.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     1
# -*- coding: utf-8 -*-
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     2
18
e6c3a5ee91aa Eliminate relative imports.
Radek Brich <radek.brich@devl.cz>
parents: 16
diff changeset
     3
from tuikit.widget import Widget
40
5faa38c10b67 Add ScrollView widget. Update Emitter, rename "on_event" methods to "_handle_event". Update VScrollbar, Layout.
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
     4
from tuikit.common import Borders, Coords
62
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 46
diff changeset
     5
import logging
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     6
13
19ebde2fd594 Add more generated documentation.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
     7
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     8
class Container(Widget):
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
     9
13
19ebde2fd594 Add more generated documentation.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
    10
    '''Container widget. Base for any widget which can contain other widgets.'''
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
    11
62
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 46
diff changeset
    12
    def __init__(self):
13
19ebde2fd594 Add more generated documentation.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
    13
        '''Create container of requested size.'''
62
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 46
diff changeset
    14
        Widget.__init__(self)
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    15
13
19ebde2fd594 Add more generated documentation.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
    16
        #: List of child widgets.
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    17
        self.children = []
62
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 46
diff changeset
    18
        self._hint_class = {}
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
    19
34
e3beacd5e536 Update event propagation, keypress event, focusing.
Radek Brich <radek.brich@devl.cz>
parents: 32
diff changeset
    20
        self.focuschild = None
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    21
        self.mousechild = None
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    22
34
e3beacd5e536 Update event propagation, keypress event, focusing.
Radek Brich <radek.brich@devl.cz>
parents: 32
diff changeset
    23
        self.allow_focus = True
e3beacd5e536 Update event propagation, keypress event, focusing.
Radek Brich <radek.brich@devl.cz>
parents: 32
diff changeset
    24
13
19ebde2fd594 Add more generated documentation.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
    25
        #: Width of borders (left, top, right, bottom).
19ebde2fd594 Add more generated documentation.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
    26
        #: Child widgets are placed within borders.
16
8791a7da6835 Update VerticalLayout/HorizontalLayout. Add layout demo. Add Size, Borders to common. Update Coords, Rect.
Radek Brich <radek.brich@devl.cz>
parents: 15
diff changeset
    27
        self.borders = Borders()
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
    28
15
c55b4749e562 Add Pager.
Radek Brich <radek.brich@devl.cz>
parents: 13
diff changeset
    29
        self._layout = None
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
    30
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    31
        self.widthrequest = (None, None)
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    32
        self.heightrequest = (None, None)
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    33
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
    34
        self.colorprefix = None
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
    35
38
c6e170452c7f Documentation, fix names of focus methods.
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
    36
        self.trap_focus = False  # if True, tab cycles inside container
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
    37
40
5faa38c10b67 Add ScrollView widget. Update Emitter, rename "on_event" methods to "_handle_event". Update VScrollbar, Layout.
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
    38
    def add(self, widget, **kwargs):
13
19ebde2fd594 Add more generated documentation.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
    39
        '''Add widget into this container.'''
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    40
        self.children.append(widget)
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    41
        widget.parent = self
41
37b7dfc3eae6 Update Emitter: All event handlers now have exactly one argument: object inherited from Event class, which carries any data.
Radek Brich <radek.brich@devl.cz>
parents: 40
diff changeset
    42
        widget.top = self.top
62
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 46
diff changeset
    43
        widget.hints.update({k:v() for k,v in self._hint_class.items()})
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 46
diff changeset
    44
        for key in kwargs:
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 46
diff changeset
    45
            try:
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 46
diff changeset
    46
                widget.hints[key].update(kwargs[key])
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 46
diff changeset
    47
            except AttributeError:
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 46
diff changeset
    48
                widget.hints[key] = widget.hints[key].__class__(kwargs[key])
38
c6e170452c7f Documentation, fix names of focus methods.
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
    49
        if self.focuschild is None and widget.can_focus():
63
2a0e04091898 Rework MenuBar. Add MenuButton. Add mouse event cascading to floaters.
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
    50
            widget.set_focus()
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    51
62
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 46
diff changeset
    52
    def add_floater(self, widget, **kwargs):
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 46
diff changeset
    53
        widget.floater = True
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 46
diff changeset
    54
        widget._size.update(widget._sizereq)
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 46
diff changeset
    55
        self.add(widget, **kwargs)
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 46
diff changeset
    56
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 46
diff changeset
    57
    def register_hints(self, *hints):
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 46
diff changeset
    58
        for hint_name, hint_class in zip(hints[::2], hints[1::2]):
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 46
diff changeset
    59
            self._hint_class[hint_name] = hint_class
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 46
diff changeset
    60
64
03f591f5fe5c Drop Container.offset, add special layout for that - OffsetLayout.
Radek Brich <radek.brich@devl.cz>
parents: 63
diff changeset
    61
    def bring_up_child(self, child):
03f591f5fe5c Drop Container.offset, add special layout for that - OffsetLayout.
Radek Brich <radek.brich@devl.cz>
parents: 63
diff changeset
    62
        if child in self.children:
03f591f5fe5c Drop Container.offset, add special layout for that - OffsetLayout.
Radek Brich <radek.brich@devl.cz>
parents: 63
diff changeset
    63
            self.children.remove(child)
03f591f5fe5c Drop Container.offset, add special layout for that - OffsetLayout.
Radek Brich <radek.brich@devl.cz>
parents: 63
diff changeset
    64
            self.children.append(child)
03f591f5fe5c Drop Container.offset, add special layout for that - OffsetLayout.
Radek Brich <radek.brich@devl.cz>
parents: 63
diff changeset
    65
42
0224ce40792f Make Container.layout a property.
Radek Brich <radek.brich@devl.cz>
parents: 41
diff changeset
    66
    @property
0224ce40792f Make Container.layout a property.
Radek Brich <radek.brich@devl.cz>
parents: 41
diff changeset
    67
    def layout(self):
0224ce40792f Make Container.layout a property.
Radek Brich <radek.brich@devl.cz>
parents: 41
diff changeset
    68
        return self._layout
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    69
42
0224ce40792f Make Container.layout a property.
Radek Brich <radek.brich@devl.cz>
parents: 41
diff changeset
    70
    @layout.setter
0224ce40792f Make Container.layout a property.
Radek Brich <radek.brich@devl.cz>
parents: 41
diff changeset
    71
    def layout(self, value):
0224ce40792f Make Container.layout a property.
Radek Brich <radek.brich@devl.cz>
parents: 41
diff changeset
    72
        """Layout manager for placing child widgets."""
0224ce40792f Make Container.layout a property.
Radek Brich <radek.brich@devl.cz>
parents: 41
diff changeset
    73
        self._layout = value
0224ce40792f Make Container.layout a property.
Radek Brich <radek.brich@devl.cz>
parents: 41
diff changeset
    74
        self._layout.container = self
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    75
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    76
62
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 46
diff changeset
    77
    def move_child(self, child, x, y):
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 46
diff changeset
    78
        pass
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 46
diff changeset
    79
41
37b7dfc3eae6 Update Emitter: All event handlers now have exactly one argument: object inherited from Event class, which carries any data.
Radek Brich <radek.brich@devl.cz>
parents: 40
diff changeset
    80
    def _set_top(self, value):
37b7dfc3eae6 Update Emitter: All event handlers now have exactly one argument: object inherited from Event class, which carries any data.
Radek Brich <radek.brich@devl.cz>
parents: 40
diff changeset
    81
        self._top = value
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    82
        for child in self.children:
41
37b7dfc3eae6 Update Emitter: All event handlers now have exactly one argument: object inherited from Event class, which carries any data.
Radek Brich <radek.brich@devl.cz>
parents: 40
diff changeset
    83
            child.top = value
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    84
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    85
63
2a0e04091898 Rework MenuBar. Add MenuButton. Add mouse event cascading to floaters.
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
    86
    def focus_next(self, step=1):
38
c6e170452c7f Documentation, fix names of focus methods.
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
    87
        """Focus next child.
c6e170452c7f Documentation, fix names of focus methods.
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
    88
c6e170452c7f Documentation, fix names of focus methods.
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
    89
        Sets focus to next child, if there is one
c6e170452c7f Documentation, fix names of focus methods.
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
    90
        which can be focused. Cycles from last child
c6e170452c7f Documentation, fix names of focus methods.
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
    91
        to first when needed. Return value depends on
c6e170452c7f Documentation, fix names of focus methods.
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
    92
        this cycling:
c6e170452c7f Documentation, fix names of focus methods.
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
    93
c6e170452c7f Documentation, fix names of focus methods.
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
    94
         * False means there wasn't any child to focus
45
43b2279b06e1 Clean up Emitter class, simplify event handling. Fix Container.focusnext() method. Add events test (handler auto-registration, order).
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
    95
           before end of list. Focus was either not changed
43b2279b06e1 Clean up Emitter class, simplify event handling. Fix Container.focusnext() method. Add events test (handler auto-registration, order).
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
    96
           or first child was focused.
38
c6e170452c7f Documentation, fix names of focus methods.
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
    97
c6e170452c7f Documentation, fix names of focus methods.
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
    98
         * True when focus is set to next child in normal
c6e170452c7f Documentation, fix names of focus methods.
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
    99
           way or when self.trap_focus is set.
c6e170452c7f Documentation, fix names of focus methods.
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   100
c6e170452c7f Documentation, fix names of focus methods.
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   101
        Return value is supposed to be returned from keypress
c6e170452c7f Documentation, fix names of focus methods.
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   102
        event - in that case, True stops event propagation.
c6e170452c7f Documentation, fix names of focus methods.
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   103
c6e170452c7f Documentation, fix names of focus methods.
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   104
        """
64
03f591f5fe5c Drop Container.offset, add special layout for that - OffsetLayout.
Radek Brich <radek.brich@devl.cz>
parents: 63
diff changeset
   105
        if self.focuschild is None:
03f591f5fe5c Drop Container.offset, add special layout for that - OffsetLayout.
Radek Brich <radek.brich@devl.cz>
parents: 63
diff changeset
   106
            idx_current = 0
03f591f5fe5c Drop Container.offset, add special layout for that - OffsetLayout.
Radek Brich <radek.brich@devl.cz>
parents: 63
diff changeset
   107
        else:
03f591f5fe5c Drop Container.offset, add special layout for that - OffsetLayout.
Radek Brich <radek.brich@devl.cz>
parents: 63
diff changeset
   108
            idx_current = self.children.index(self.focuschild)
45
43b2279b06e1 Clean up Emitter class, simplify event handling. Fix Container.focusnext() method. Add events test (handler auto-registration, order).
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
   109
        idx_new = idx_current
34
e3beacd5e536 Update event propagation, keypress event, focusing.
Radek Brich <radek.brich@devl.cz>
parents: 32
diff changeset
   110
        cycled = False
45
43b2279b06e1 Clean up Emitter class, simplify event handling. Fix Container.focusnext() method. Add events test (handler auto-registration, order).
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
   111
        while True:
63
2a0e04091898 Rework MenuBar. Add MenuButton. Add mouse event cascading to floaters.
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   112
            idx_new += step
34
e3beacd5e536 Update event propagation, keypress event, focusing.
Radek Brich <radek.brich@devl.cz>
parents: 32
diff changeset
   113
            if idx_new >= len(self.children):
e3beacd5e536 Update event propagation, keypress event, focusing.
Radek Brich <radek.brich@devl.cz>
parents: 32
diff changeset
   114
                idx_new = 0
e3beacd5e536 Update event propagation, keypress event, focusing.
Radek Brich <radek.brich@devl.cz>
parents: 32
diff changeset
   115
                cycled = True
63
2a0e04091898 Rework MenuBar. Add MenuButton. Add mouse event cascading to floaters.
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   116
            if idx_new < 0: # for focus_previous
2a0e04091898 Rework MenuBar. Add MenuButton. Add mouse event cascading to floaters.
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   117
                idx_new = len(self.children) - 1
2a0e04091898 Rework MenuBar. Add MenuButton. Add mouse event cascading to floaters.
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   118
                cycled = True
45
43b2279b06e1 Clean up Emitter class, simplify event handling. Fix Container.focusnext() method. Add events test (handler auto-registration, order).
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
   119
            if idx_current == idx_new:
43b2279b06e1 Clean up Emitter class, simplify event handling. Fix Container.focusnext() method. Add events test (handler auto-registration, order).
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
   120
                return False
38
c6e170452c7f Documentation, fix names of focus methods.
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   121
            if self.children[idx_new].can_focus():
c6e170452c7f Documentation, fix names of focus methods.
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   122
                self.children[idx_new].set_focus()
c6e170452c7f Documentation, fix names of focus methods.
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   123
                return self.trap_focus or not cycled
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
   124
63
2a0e04091898 Rework MenuBar. Add MenuButton. Add mouse event cascading to floaters.
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   125
    def focus_previous(self):
2a0e04091898 Rework MenuBar. Add MenuButton. Add mouse event cascading to floaters.
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   126
        """Focus previous child."""
2a0e04091898 Rework MenuBar. Add MenuButton. Add mouse event cascading to floaters.
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   127
        self.focus_next(-1)
2a0e04091898 Rework MenuBar. Add MenuButton. Add mouse event cascading to floaters.
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   128
2a0e04091898 Rework MenuBar. Add MenuButton. Add mouse event cascading to floaters.
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   129
    def on_focus(self, ev):
2a0e04091898 Rework MenuBar. Add MenuButton. Add mouse event cascading to floaters.
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   130
        if self.focuschild:
2a0e04091898 Rework MenuBar. Add MenuButton. Add mouse event cascading to floaters.
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   131
            self.focuschild.emit('focus', ev)
2a0e04091898 Rework MenuBar. Add MenuButton. Add mouse event cascading to floaters.
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   132
2a0e04091898 Rework MenuBar. Add MenuButton. Add mouse event cascading to floaters.
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   133
    def on_unfocus(self, ev):
2a0e04091898 Rework MenuBar. Add MenuButton. Add mouse event cascading to floaters.
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   134
        if self.focuschild:
2a0e04091898 Rework MenuBar. Add MenuButton. Add mouse event cascading to floaters.
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   135
            self.focuschild.emit('unfocus', ev)
2a0e04091898 Rework MenuBar. Add MenuButton. Add mouse event cascading to floaters.
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   136
34
e3beacd5e536 Update event propagation, keypress event, focusing.
Radek Brich <radek.brich@devl.cz>
parents: 32
diff changeset
   137
    def draw(self, driver, x, y):
36
caf927c3f10b Update docs.
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   138
        """Draw the container and its children.
caf927c3f10b Update docs.
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   139
caf927c3f10b Update docs.
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   140
        This method should not be overriden by subclasses,
caf927c3f10b Update docs.
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   141
        use on_draw instead.
caf927c3f10b Update docs.
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   142
caf927c3f10b Update docs.
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   143
        """
2
684cdc352562 Menu, Window and other improvements.
Radek Brich <radek.brich@devl.cz>
parents: 1
diff changeset
   144
        if self.hidden:
34
e3beacd5e536 Update event propagation, keypress event, focusing.
Radek Brich <radek.brich@devl.cz>
parents: 32
diff changeset
   145
            return True
5
ae128c885d0f New GridLayout. Change cursor behavior (hide on unfocus event). Change resize event to propagate through containers. Change container clipping - allowlayout=false children are clipped without borders. More Widget doc.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   146
23
4e72fd2a0e14 Rename BackendCurses to DriverCurses. Add DriverDummy - dummy driver for debugging purposes. Move clipping stack from driver to common.ClipStack class.
Radek Brich <radek.brich@devl.cz>
parents: 18
diff changeset
   147
        driver.clipstack.push(x, y, self.width, self.height)
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
   148
        if self.colorprefix:
23
4e72fd2a0e14 Rename BackendCurses to DriverCurses. Add DriverDummy - dummy driver for debugging purposes. Move clipping stack from driver to common.ClipStack class.
Radek Brich <radek.brich@devl.cz>
parents: 18
diff changeset
   149
            driver.pushcolorprefix(self.colorprefix)
5
ae128c885d0f New GridLayout. Change cursor behavior (hide on unfocus event). Change resize event to propagate through containers. Change container clipping - allowlayout=false children are clipped without borders. More Widget doc.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   150
46
2b43a7f38c34 Minor updates. Replace super() with direct class reference. Add demo_colors.
Radek Brich <radek.brich@devl.cz>
parents: 45
diff changeset
   151
        Widget.draw(self, driver, x, y)
5
ae128c885d0f New GridLayout. Change cursor behavior (hide on unfocus event). Change resize event to propagate through containers. Change container clipping - allowlayout=false children are clipped without borders. More Widget doc.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
   152
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   153
        l, t, r, b = self.borders
23
4e72fd2a0e14 Rename BackendCurses to DriverCurses. Add DriverDummy - dummy driver for debugging purposes. Move clipping stack from driver to common.ClipStack class.
Radek Brich <radek.brich@devl.cz>
parents: 18
diff changeset
   154
        driver.clipstack.push(x+l, y+t, self.width-l-r, self.height-t-b)
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   155
62
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 46
diff changeset
   156
        for child in [ch for ch in self.children if not ch.floater]:
40
5faa38c10b67 Add ScrollView widget. Update Emitter, rename "on_event" methods to "_handle_event". Update VScrollbar, Layout.
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
   157
            child.draw(driver,
64
03f591f5fe5c Drop Container.offset, add special layout for that - OffsetLayout.
Radek Brich <radek.brich@devl.cz>
parents: 63
diff changeset
   158
                x + child.x,
03f591f5fe5c Drop Container.offset, add special layout for that - OffsetLayout.
Radek Brich <radek.brich@devl.cz>
parents: 63
diff changeset
   159
                y + child.y)
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   160
23
4e72fd2a0e14 Rename BackendCurses to DriverCurses. Add DriverDummy - dummy driver for debugging purposes. Move clipping stack from driver to common.ClipStack class.
Radek Brich <radek.brich@devl.cz>
parents: 18
diff changeset
   161
        driver.clipstack.pop()
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   162
62
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 46
diff changeset
   163
        # draw floaters - no clipping, no colorprefix
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 46
diff changeset
   164
        if self.parent is None:
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 46
diff changeset
   165
            self.draw_floaters(driver, x, y)
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 46
diff changeset
   166
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
   167
        if self.colorprefix:
23
4e72fd2a0e14 Rename BackendCurses to DriverCurses. Add DriverDummy - dummy driver for debugging purposes. Move clipping stack from driver to common.ClipStack class.
Radek Brich <radek.brich@devl.cz>
parents: 18
diff changeset
   168
            driver.popcolorprefix()
4e72fd2a0e14 Rename BackendCurses to DriverCurses. Add DriverDummy - dummy driver for debugging purposes. Move clipping stack from driver to common.ClipStack class.
Radek Brich <radek.brich@devl.cz>
parents: 18
diff changeset
   169
        driver.clipstack.pop()
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   170
62
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 46
diff changeset
   171
    def draw_floaters(self, driver, x, y):
63
2a0e04091898 Rework MenuBar. Add MenuButton. Add mouse event cascading to floaters.
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   172
        #logging.getLogger('tuikit').info('draw_floaters %s %r %r', self, x ,y )
2a0e04091898 Rework MenuBar. Add MenuButton. Add mouse event cascading to floaters.
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   173
        # draw our floaters
62
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 46
diff changeset
   174
        for child in [ch for ch in self.children if ch.floater]:
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 46
diff changeset
   175
            child.draw(driver,
64
03f591f5fe5c Drop Container.offset, add special layout for that - OffsetLayout.
Radek Brich <radek.brich@devl.cz>
parents: 63
diff changeset
   176
                x + child.x,
03f591f5fe5c Drop Container.offset, add special layout for that - OffsetLayout.
Radek Brich <radek.brich@devl.cz>
parents: 63
diff changeset
   177
                y + child.y)
62
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 46
diff changeset
   178
        # delve into child containers, draw their floaters
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 46
diff changeset
   179
        for child in [ch for ch in self.children if not ch.floater and isinstance(ch, Container)]:
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 46
diff changeset
   180
            child.draw_floaters(driver,
64
03f591f5fe5c Drop Container.offset, add special layout for that - OffsetLayout.
Radek Brich <radek.brich@devl.cz>
parents: 63
diff changeset
   181
                x + child.x,
03f591f5fe5c Drop Container.offset, add special layout for that - OffsetLayout.
Radek Brich <radek.brich@devl.cz>
parents: 63
diff changeset
   182
                y + child.y)
62
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 46
diff changeset
   183
45
43b2279b06e1 Clean up Emitter class, simplify event handling. Fix Container.focusnext() method. Add events test (handler auto-registration, order).
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
   184
    def on_resize(self, ev):
42
0224ce40792f Make Container.layout a property.
Radek Brich <radek.brich@devl.cz>
parents: 41
diff changeset
   185
        for child in self.children:
0224ce40792f Make Container.layout a property.
Radek Brich <radek.brich@devl.cz>
parents: 41
diff changeset
   186
            child.emit('resize')
0224ce40792f Make Container.layout a property.
Radek Brich <radek.brich@devl.cz>
parents: 41
diff changeset
   187
45
43b2279b06e1 Clean up Emitter class, simplify event handling. Fix Container.focusnext() method. Add events test (handler auto-registration, order).
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
   188
    def on_keypress(self, ev):
63
2a0e04091898 Rework MenuBar. Add MenuButton. Add mouse event cascading to floaters.
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   189
        if self.focuschild is not None and not self.focuschild.hidden:
2a0e04091898 Rework MenuBar. Add MenuButton. Add mouse event cascading to floaters.
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   190
            if self.focuschild.emit('keypress', ev):
42
0224ce40792f Make Container.layout a property.
Radek Brich <radek.brich@devl.cz>
parents: 41
diff changeset
   191
                return True
0224ce40792f Make Container.layout a property.
Radek Brich <radek.brich@devl.cz>
parents: 41
diff changeset
   192
        if ev.keyname == 'tab':
0224ce40792f Make Container.layout a property.
Radek Brich <radek.brich@devl.cz>
parents: 41
diff changeset
   193
            return self.focus_next()
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   194
45
43b2279b06e1 Clean up Emitter class, simplify event handling. Fix Container.focusnext() method. Add events test (handler auto-registration, order).
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
   195
    def on_mousedown(self, ev):
63
2a0e04091898 Rework MenuBar. Add MenuButton. Add mouse event cascading to floaters.
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   196
        self.cascade_mouse_event(ev)
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   197
45
43b2279b06e1 Clean up Emitter class, simplify event handling. Fix Container.focusnext() method. Add events test (handler auto-registration, order).
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
   198
    def on_mouseup(self, ev):
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   199
        if self.mousechild:
62
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 46
diff changeset
   200
            child_ev = ev.make_child_event(self, self.mousechild)
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 46
diff changeset
   201
            self.mousechild.emit('mouseup', child_ev)
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   202
            self.mousechild = None
34
e3beacd5e536 Update event propagation, keypress event, focusing.
Radek Brich <radek.brich@devl.cz>
parents: 32
diff changeset
   203
            return True
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   204
45
43b2279b06e1 Clean up Emitter class, simplify event handling. Fix Container.focusnext() method. Add events test (handler auto-registration, order).
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
   205
    def on_mousemove(self, ev):
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   206
        if self.mousechild:
62
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 46
diff changeset
   207
            child_ev = ev.make_child_event(self, self.mousechild)
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 46
diff changeset
   208
            self.mousechild.emit('mousemove', child_ev)
34
e3beacd5e536 Update event propagation, keypress event, focusing.
Radek Brich <radek.brich@devl.cz>
parents: 32
diff changeset
   209
            return True
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   210
45
43b2279b06e1 Clean up Emitter class, simplify event handling. Fix Container.focusnext() method. Add events test (handler auto-registration, order).
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
   211
    def on_mousewheel(self, ev):
63
2a0e04091898 Rework MenuBar. Add MenuButton. Add mouse event cascading to floaters.
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   212
        self.cascade_mouse_event(ev)
2a0e04091898 Rework MenuBar. Add MenuButton. Add mouse event cascading to floaters.
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   213
2a0e04091898 Rework MenuBar. Add MenuButton. Add mouse event cascading to floaters.
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   214
    def cascade_mouse_event(self, ev):
2a0e04091898 Rework MenuBar. Add MenuButton. Add mouse event cascading to floaters.
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   215
        """Resend mouse event to child under cursor.
2a0e04091898 Rework MenuBar. Add MenuButton. Add mouse event cascading to floaters.
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   216
2a0e04091898 Rework MenuBar. Add MenuButton. Add mouse event cascading to floaters.
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   217
        Args:
2a0e04091898 Rework MenuBar. Add MenuButton. Add mouse event cascading to floaters.
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   218
            ev: Original mouse event. Event type and cursor
2a0e04091898 Rework MenuBar. Add MenuButton. Add mouse event cascading to floaters.
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   219
                position is extracted from this.
2a0e04091898 Rework MenuBar. Add MenuButton. Add mouse event cascading to floaters.
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   220
2a0e04091898 Rework MenuBar. Add MenuButton. Add mouse event cascading to floaters.
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   221
        Returns:
2a0e04091898 Rework MenuBar. Add MenuButton. Add mouse event cascading to floaters.
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   222
            Boolean value. True when event was handled by a child.
2a0e04091898 Rework MenuBar. Add MenuButton. Add mouse event cascading to floaters.
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   223
2a0e04091898 Rework MenuBar. Add MenuButton. Add mouse event cascading to floaters.
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   224
        """
2a0e04091898 Rework MenuBar. Add MenuButton. Add mouse event cascading to floaters.
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   225
        if self.parent is None:
2a0e04091898 Rework MenuBar. Add MenuButton. Add mouse event cascading to floaters.
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   226
            if self.floaters_mouse_event(ev):
2a0e04091898 Rework MenuBar. Add MenuButton. Add mouse event cascading to floaters.
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   227
                return True
2a0e04091898 Rework MenuBar. Add MenuButton. Add mouse event cascading to floaters.
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   228
        for child in reversed([ch for ch in self.children if not ch.floater]):
64
03f591f5fe5c Drop Container.offset, add special layout for that - OffsetLayout.
Radek Brich <radek.brich@devl.cz>
parents: 63
diff changeset
   229
            if child.enclose(ev.wx, ev.wy):
62
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 46
diff changeset
   230
                child_ev = ev.make_child_event(self, child)
63
2a0e04091898 Rework MenuBar. Add MenuButton. Add mouse event cascading to floaters.
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   231
                child.emit(ev.event_name, child_ev)
2a0e04091898 Rework MenuBar. Add MenuButton. Add mouse event cascading to floaters.
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   232
                if ev.event_name == 'mousedown':
2a0e04091898 Rework MenuBar. Add MenuButton. Add mouse event cascading to floaters.
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   233
                    self.mousechild = child
2a0e04091898 Rework MenuBar. Add MenuButton. Add mouse event cascading to floaters.
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   234
                return True
2a0e04091898 Rework MenuBar. Add MenuButton. Add mouse event cascading to floaters.
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   235
        return False
34
e3beacd5e536 Update event propagation, keypress event, focusing.
Radek Brich <radek.brich@devl.cz>
parents: 32
diff changeset
   236
63
2a0e04091898 Rework MenuBar. Add MenuButton. Add mouse event cascading to floaters.
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   237
    def floaters_mouse_event(self, ev):
2a0e04091898 Rework MenuBar. Add MenuButton. Add mouse event cascading to floaters.
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   238
        """Resend mouse event to our floaters and any floaters in child containers."""
2a0e04091898 Rework MenuBar. Add MenuButton. Add mouse event cascading to floaters.
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   239
        # delve into child containers, test their floaters
2a0e04091898 Rework MenuBar. Add MenuButton. Add mouse event cascading to floaters.
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   240
        for child in reversed([ch for ch in self.children \
2a0e04091898 Rework MenuBar. Add MenuButton. Add mouse event cascading to floaters.
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   241
                               if not ch.floater and isinstance(ch, Container)]):
2a0e04091898 Rework MenuBar. Add MenuButton. Add mouse event cascading to floaters.
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   242
            child_ev = ev.make_child_event(self, child)
2a0e04091898 Rework MenuBar. Add MenuButton. Add mouse event cascading to floaters.
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   243
            if child.floaters_mouse_event(child_ev):
2a0e04091898 Rework MenuBar. Add MenuButton. Add mouse event cascading to floaters.
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   244
                if ev.event_name == 'mousedown':
2a0e04091898 Rework MenuBar. Add MenuButton. Add mouse event cascading to floaters.
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   245
                    self.mousechild = child
2a0e04091898 Rework MenuBar. Add MenuButton. Add mouse event cascading to floaters.
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   246
                return True
2a0e04091898 Rework MenuBar. Add MenuButton. Add mouse event cascading to floaters.
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   247
        # test our floaters
2a0e04091898 Rework MenuBar. Add MenuButton. Add mouse event cascading to floaters.
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   248
        for child in reversed([ch for ch in self.children if ch.floater]):
64
03f591f5fe5c Drop Container.offset, add special layout for that - OffsetLayout.
Radek Brich <radek.brich@devl.cz>
parents: 63
diff changeset
   249
            if child.enclose(ev.wx, ev.wy):
63
2a0e04091898 Rework MenuBar. Add MenuButton. Add mouse event cascading to floaters.
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   250
                child_ev = ev.make_child_event(self, child)
2a0e04091898 Rework MenuBar. Add MenuButton. Add mouse event cascading to floaters.
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   251
                child.emit(ev.event_name, child_ev)
2a0e04091898 Rework MenuBar. Add MenuButton. Add mouse event cascading to floaters.
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   252
                if ev.event_name == 'mousedown':
2a0e04091898 Rework MenuBar. Add MenuButton. Add mouse event cascading to floaters.
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   253
                    self.mousechild = child
2a0e04091898 Rework MenuBar. Add MenuButton. Add mouse event cascading to floaters.
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   254
                return True
2a0e04091898 Rework MenuBar. Add MenuButton. Add mouse event cascading to floaters.
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   255