tuikit/core/widget.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:
104
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
     1
from tuikit.core.coords import Point, Size, Rect
89
94f5baef19ac Add Theme, Button.
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
     2
from tuikit.core.theme import default_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
     3
from tuikit.core.signal import Signal
87
ee5ea9671f28 Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     4
94
e50dae408fe9 Add origin to Buffer. Use it to simplify hierarchical drawing.
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
     5
import logging
e50dae408fe9 Add origin to Buffer. Use it to simplify hierarchical drawing.
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
     6
87
ee5ea9671f28 Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     7
ee5ea9671f28 Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     8
class Widget:
ee5ea9671f28 Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     9
ee5ea9671f28 Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    10
    """Base class for all widgets."""
ee5ea9671f28 Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    11
ee5ea9671f28 Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    12
    _num_instances = 0
ee5ea9671f28 Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    13
ee5ea9671f28 Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    14
    def __init__(self):
94
e50dae408fe9 Add origin to Buffer. Use it to simplify hierarchical drawing.
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
    15
        self._num_instances += 1
e50dae408fe9 Add origin to Buffer. Use it to simplify hierarchical drawing.
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
    16
87
ee5ea9671f28 Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    17
        #: Widget name is used for logging etc. Not visible anywhere.
105
70c9cfc70cd6 Update logging.
Radek Brich <radek.brich@devl.cz>
parents: 104
diff changeset
    18
        self.name = '%s#%s' % (self.__class__.__name__, self._num_instances)
87
ee5ea9671f28 Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    19
ee5ea9671f28 Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    20
        #: Parent Widget
ee5ea9671f28 Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    21
        self.parent = None
ee5ea9671f28 Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    22
        #: Window owning this Widget
ee5ea9671f28 Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    23
        self.window = None
89
94f5baef19ac Add Theme, Button.
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
    24
        #: Theme
94f5baef19ac Add Theme, Button.
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
    25
        self.theme = default_theme
119
dd91747504dd Redraw widgets on request. Add scrollbar demo.
Radek Brich <radek.brich@devl.cz>
parents: 118
diff changeset
    26
        #: Timer
dd91747504dd Redraw widgets on request. Add scrollbar demo.
Radek Brich <radek.brich@devl.cz>
parents: 118
diff changeset
    27
        self.timer = None
87
ee5ea9671f28 Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    28
ee5ea9671f28 Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    29
        #: Position inside parent widget. Modified by layout manager.
ee5ea9671f28 Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    30
        self.pos = Point()
ee5ea9671f28 Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    31
        #: Actual size. Modified by layout manager.
93
c1e79acb9fcb Add Layout, FixedLayout.
Radek Brich <radek.brich@devl.cz>
parents: 92
diff changeset
    32
        self._size = Size(10, 10)
90
781774a8d568 Add timer, adjust inheritance of Widget, Container, Window.
Radek Brich <radek.brich@devl.cz>
parents: 89
diff changeset
    33
87
ee5ea9671f28 Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    34
        #: Requested size. Layout manager will use this when placing the widget.
ee5ea9671f28 Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    35
        self.sizereq = Size(1, 1)
ee5ea9671f28 Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    36
        #: Minimal size of widget. Widget will never be sized smaller than this.
ee5ea9671f28 Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    37
        self.sizemin = Size(1, 1)
ee5ea9671f28 Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    38
        #: Maximum size of widget. Widget will never be sized bigger than this.
ee5ea9671f28 Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    39
        #: None means no maximum size (infinite).
ee5ea9671f28 Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    40
        self.sizemax = Size(None, None)
ee5ea9671f28 Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    41
97
0c2e0c09ba5c Add TextField widget, keypress event, cursor.
Radek Brich <radek.brich@devl.cz>
parents: 94
diff changeset
    42
        #: Cursor is position where text input will occur.
104
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
    43
        #: The cursor coordinates are relative to widget.
112
ce2e67e7bbb8 Refactor cursor.
Radek Brich <radek.brich@devl.cz>
parents: 111
diff changeset
    44
        self._cursor = Point()
113
Radek Brich <radek.brich@devl.cz>
parents: 112 107
diff changeset
    45
        #: Cursor is displayed on screen only when the widget is focused.
112
ce2e67e7bbb8 Refactor cursor.
Radek Brich <radek.brich@devl.cz>
parents: 111
diff changeset
    46
        self._cursor_visible = False
97
0c2e0c09ba5c Add TextField widget, keypress event, cursor.
Radek Brich <radek.brich@devl.cz>
parents: 94
diff changeset
    47
119
dd91747504dd Redraw widgets on request. Add scrollbar demo.
Radek Brich <radek.brich@devl.cz>
parents: 118
diff changeset
    48
        #: True if this widget requests redraw
dd91747504dd Redraw widgets on request. Add scrollbar demo.
Radek Brich <radek.brich@devl.cz>
parents: 118
diff changeset
    49
        self.need_draw = True
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
    50
        #: Hidden widget does not affect layout.
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
        self.hidden = 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
    52
        #: Allow keyboard focus for this 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
    53
        self.allow_focus = 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
    54
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
    55
        self.sig_keypress = Signal(allow_stop=True)
105
70c9cfc70cd6 Update logging.
Radek Brich <radek.brich@devl.cz>
parents: 104
diff changeset
    56
119
dd91747504dd Redraw widgets on request. Add scrollbar demo.
Radek Brich <radek.brich@devl.cz>
parents: 118
diff changeset
    57
    # -------------------------------------------------------------------------
dd91747504dd Redraw widgets on request. Add scrollbar demo.
Radek Brich <radek.brich@devl.cz>
parents: 118
diff changeset
    58
    #   Position and size
87
ee5ea9671f28 Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    59
ee5ea9671f28 Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    60
    @property
ee5ea9671f28 Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    61
    def x(self):
ee5ea9671f28 Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    62
        return self.pos.x
ee5ea9671f28 Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    63
ee5ea9671f28 Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    64
    @property
ee5ea9671f28 Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    65
    def y(self):
ee5ea9671f28 Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    66
        return self.pos.y
ee5ea9671f28 Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    67
114
26c02bd94bd9 Add Widget.posreq. Add OffsetLayout.
Radek Brich <radek.brich@devl.cz>
parents: 113
diff changeset
    68
    def move(self, x, y):
26c02bd94bd9 Add Widget.posreq. Add OffsetLayout.
Radek Brich <radek.brich@devl.cz>
parents: 113
diff changeset
    69
        """Should be called only by layout manager."""
26c02bd94bd9 Add Widget.posreq. Add OffsetLayout.
Radek Brich <radek.brich@devl.cz>
parents: 113
diff changeset
    70
        self.pos.update(x, y)
26c02bd94bd9 Add Widget.posreq. Add OffsetLayout.
Radek Brich <radek.brich@devl.cz>
parents: 113
diff changeset
    71
87
ee5ea9671f28 Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    72
    @property
ee5ea9671f28 Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    73
    def width(self):
ee5ea9671f28 Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    74
        return self.size.w
ee5ea9671f28 Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    75
ee5ea9671f28 Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    76
    @property
ee5ea9671f28 Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    77
    def height(self):
ee5ea9671f28 Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    78
        return self.size.h
ee5ea9671f28 Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    79
93
c1e79acb9fcb Add Layout, FixedLayout.
Radek Brich <radek.brich@devl.cz>
parents: 92
diff changeset
    80
    @property
c1e79acb9fcb Add Layout, FixedLayout.
Radek Brich <radek.brich@devl.cz>
parents: 92
diff changeset
    81
    def size(self):
107
1822c37b2688 Refactoring. Add ImmutablePoint.
Radek Brich <radek.brich@devl.cz>
parents: 106
diff changeset
    82
        return self._size.immutable()
93
c1e79acb9fcb Add Layout, FixedLayout.
Radek Brich <radek.brich@devl.cz>
parents: 92
diff changeset
    83
c1e79acb9fcb Add Layout, FixedLayout.
Radek Brich <radek.brich@devl.cz>
parents: 92
diff changeset
    84
    def resize(self, w, h):
114
26c02bd94bd9 Add Widget.posreq. Add OffsetLayout.
Radek Brich <radek.brich@devl.cz>
parents: 113
diff changeset
    85
        """Should be called only by layout manager."""
93
c1e79acb9fcb Add Layout, FixedLayout.
Radek Brich <radek.brich@devl.cz>
parents: 92
diff changeset
    86
        self._size.update(w, h)
c1e79acb9fcb Add Layout, FixedLayout.
Radek Brich <radek.brich@devl.cz>
parents: 92
diff changeset
    87
106
abcadb7e2ef1 Use Point for mouse events, add them to Container and Widget.
Radek Brich <radek.brich@devl.cz>
parents: 105
diff changeset
    88
    @property
abcadb7e2ef1 Use Point for mouse events, add them to Container and Widget.
Radek Brich <radek.brich@devl.cz>
parents: 105
diff changeset
    89
    def boundaries(self):
119
dd91747504dd Redraw widgets on request. Add scrollbar demo.
Radek Brich <radek.brich@devl.cz>
parents: 118
diff changeset
    90
        return Rect(self.pos, self._size)
106
abcadb7e2ef1 Use Point for mouse events, add them to Container and Widget.
Radek Brich <radek.brich@devl.cz>
parents: 105
diff changeset
    91
119
dd91747504dd Redraw widgets on request. Add scrollbar demo.
Radek Brich <radek.brich@devl.cz>
parents: 118
diff changeset
    92
    # -------------------------------------------------------------------------
dd91747504dd Redraw widgets on request. Add scrollbar demo.
Radek Brich <radek.brich@devl.cz>
parents: 118
diff changeset
    93
    #   Presentation
dd91747504dd Redraw widgets on request. Add scrollbar demo.
Radek Brich <radek.brich@devl.cz>
parents: 118
diff changeset
    94
dd91747504dd Redraw widgets on request. Add scrollbar demo.
Radek Brich <radek.brich@devl.cz>
parents: 118
diff changeset
    95
    def draw_if_needed(self, buffer):
dd91747504dd Redraw widgets on request. Add scrollbar demo.
Radek Brich <radek.brich@devl.cz>
parents: 118
diff changeset
    96
        if self.need_draw:
dd91747504dd Redraw widgets on request. Add scrollbar demo.
Radek Brich <radek.brich@devl.cz>
parents: 118
diff changeset
    97
            self.draw(buffer)
87
ee5ea9671f28 Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    98
94
e50dae408fe9 Add origin to Buffer. Use it to simplify hierarchical drawing.
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
    99
    def draw(self, buffer):
e50dae408fe9 Add origin to Buffer. Use it to simplify hierarchical drawing.
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
   100
        """Draw self into 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
   101
        self.log.debug('Draw into %r at %s (exposed %s)',
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
                       buffer, buffer.origin, self.exposed(buffer))
119
dd91747504dd Redraw widgets on request. Add scrollbar demo.
Radek Brich <radek.brich@devl.cz>
parents: 118
diff changeset
   103
        self.need_draw = False
dd91747504dd Redraw widgets on request. Add scrollbar demo.
Radek Brich <radek.brich@devl.cz>
parents: 118
diff changeset
   104
dd91747504dd Redraw widgets on request. Add scrollbar demo.
Radek Brich <radek.brich@devl.cz>
parents: 118
diff changeset
   105
    def redraw(self):
dd91747504dd Redraw widgets on request. Add scrollbar demo.
Radek Brich <radek.brich@devl.cz>
parents: 118
diff changeset
   106
        """Request redraw."""
dd91747504dd Redraw widgets on request. Add scrollbar demo.
Radek Brich <radek.brich@devl.cz>
parents: 118
diff changeset
   107
        self.need_draw = True
89
94f5baef19ac Add Theme, Button.
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   108
94f5baef19ac Add Theme, Button.
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   109
    def set_theme(self, theme):
94f5baef19ac Add Theme, Button.
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   110
        self.theme = theme
90
781774a8d568 Add timer, adjust inheritance of Widget, Container, Window.
Radek Brich <radek.brich@devl.cz>
parents: 89
diff changeset
   111
94
e50dae408fe9 Add origin to Buffer. Use it to simplify hierarchical drawing.
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
   112
    @staticmethod
e50dae408fe9 Add origin to Buffer. Use it to simplify hierarchical drawing.
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
   113
    def exposed(buffer):
e50dae408fe9 Add origin to Buffer. Use it to simplify hierarchical drawing.
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
   114
        """Exposed part of widget.
e50dae408fe9 Add origin to Buffer. Use it to simplify hierarchical drawing.
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
   115
e50dae408fe9 Add origin to Buffer. Use it to simplify hierarchical drawing.
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
   116
        Only this area needs to be drawn.
e50dae408fe9 Add origin to Buffer. Use it to simplify hierarchical drawing.
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
   117
e50dae408fe9 Add origin to Buffer. Use it to simplify hierarchical drawing.
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
   118
        Returns exposed Rect in widget's local coordinates,
e50dae408fe9 Add origin to Buffer. Use it to simplify hierarchical drawing.
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
   119
        where 0,0 is left top corner of widget to be drawn.
e50dae408fe9 Add origin to Buffer. Use it to simplify hierarchical drawing.
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
   120
e50dae408fe9 Add origin to Buffer. Use it to simplify hierarchical drawing.
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
   121
        """
e50dae408fe9 Add origin to Buffer. Use it to simplify hierarchical drawing.
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
   122
        return buffer.clip_rect.moved(-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
   123
97
0c2e0c09ba5c Add TextField widget, keypress event, cursor.
Radek Brich <radek.brich@devl.cz>
parents: 94
diff changeset
   124
    @property
0c2e0c09ba5c Add TextField widget, keypress event, cursor.
Radek Brich <radek.brich@devl.cz>
parents: 94
diff changeset
   125
    def cursor(self):
113
Radek Brich <radek.brich@devl.cz>
parents: 112 107
diff changeset
   126
        """Return cursor coordinates.
Radek Brich <radek.brich@devl.cz>
parents: 112 107
diff changeset
   127
        
Radek Brich <radek.brich@devl.cz>
parents: 112 107
diff changeset
   128
        Returns None if cursor is set outside of widget boundaries.
Radek Brich <radek.brich@devl.cz>
parents: 112 107
diff changeset
   129
        
Radek Brich <radek.brich@devl.cz>
parents: 112 107
diff changeset
   130
        """
119
dd91747504dd Redraw widgets on request. Add scrollbar demo.
Radek Brich <radek.brich@devl.cz>
parents: 118
diff changeset
   131
        if self._cursor in Rect(0, 0, *self._size):
104
742e504ec053 Update TextBox: Replace "spot" with "cursor".
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   132
            return self._cursor
97
0c2e0c09ba5c Add TextField widget, keypress event, cursor.
Radek Brich <radek.brich@devl.cz>
parents: 94
diff changeset
   133
112
ce2e67e7bbb8 Refactor cursor.
Radek Brich <radek.brich@devl.cz>
parents: 111
diff changeset
   134
    @property
ce2e67e7bbb8 Refactor cursor.
Radek Brich <radek.brich@devl.cz>
parents: 111
diff changeset
   135
    def cursor_visible(self):
ce2e67e7bbb8 Refactor cursor.
Radek Brich <radek.brich@devl.cz>
parents: 111
diff changeset
   136
        return self._cursor_visible
97
0c2e0c09ba5c Add TextField widget, keypress event, cursor.
Radek Brich <radek.brich@devl.cz>
parents: 94
diff changeset
   137
119
dd91747504dd Redraw widgets on request. Add scrollbar demo.
Radek Brich <radek.brich@devl.cz>
parents: 118
diff changeset
   138
    # -------------------------------------------------------------------------
dd91747504dd Redraw widgets on request. Add scrollbar demo.
Radek Brich <radek.brich@devl.cz>
parents: 118
diff changeset
   139
    #   Events
97
0c2e0c09ba5c Add TextField widget, keypress event, cursor.
Radek Brich <radek.brich@devl.cz>
parents: 94
diff changeset
   140
111
b055add74b18 Refactor events.
Radek Brich <radek.brich@devl.cz>
parents: 109
diff changeset
   141
    def resize_event(self, ev):
b055add74b18 Refactor events.
Radek Brich <radek.brich@devl.cz>
parents: 109
diff changeset
   142
        self.resize(ev.w, ev.h)
b055add74b18 Refactor events.
Radek Brich <radek.brich@devl.cz>
parents: 109
diff changeset
   143
b055add74b18 Refactor events.
Radek Brich <radek.brich@devl.cz>
parents: 109
diff changeset
   144
    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
   145
        """Keypress event handler.
97
0c2e0c09ba5c Add TextField widget, keypress event, cursor.
Radek Brich <radek.brich@devl.cz>
parents: 94
diff changeset
   146
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
   147
        Override to accept keyboard input.
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
118
8c7970520632 Add mouse events, event demo.
Radek Brich <radek.brich@devl.cz>
parents: 117
diff changeset
   149
        Return True if event was consumed.
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
   150
118
8c7970520632 Add mouse events, event demo.
Radek Brich <radek.brich@devl.cz>
parents: 117
diff changeset
   151
        Call parent implementation from inherited classes
8c7970520632 Add mouse events, event demo.
Radek Brich <radek.brich@devl.cz>
parents: 117
diff changeset
   152
        when not consuming the event.
109
105b1affc3c2 Update keypress propagation. Allow focus change by tab key. Add log property to Widget for smart logging.
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   153
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
        """
111
b055add74b18 Refactor events.
Radek Brich <radek.brich@devl.cz>
parents: 109
diff changeset
   155
        if self.sig_keypress(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
   156
            return True
111
b055add74b18 Refactor events.
Radek Brich <radek.brich@devl.cz>
parents: 109
diff changeset
   157
        self.log.debug('Not consumed: %s', ev)
97
0c2e0c09ba5c Add TextField widget, keypress event, cursor.
Radek Brich <radek.brich@devl.cz>
parents: 94
diff changeset
   158
118
8c7970520632 Add mouse events, event demo.
Radek Brich <radek.brich@devl.cz>
parents: 117
diff changeset
   159
    def mousedown_event(self, ev):
8c7970520632 Add mouse events, event demo.
Radek Brich <radek.brich@devl.cz>
parents: 117
diff changeset
   160
        self.log.debug('Not consumed: %s', ev)
106
abcadb7e2ef1 Use Point for mouse events, add them to Container and Widget.
Radek Brich <radek.brich@devl.cz>
parents: 105
diff changeset
   161
118
8c7970520632 Add mouse events, event demo.
Radek Brich <radek.brich@devl.cz>
parents: 117
diff changeset
   162
    def mouseup_event(self, ev):
8c7970520632 Add mouse events, event demo.
Radek Brich <radek.brich@devl.cz>
parents: 117
diff changeset
   163
        self.log.debug('Not consumed: %s', ev)
106
abcadb7e2ef1 Use Point for mouse events, add them to Container and Widget.
Radek Brich <radek.brich@devl.cz>
parents: 105
diff changeset
   164
118
8c7970520632 Add mouse events, event demo.
Radek Brich <radek.brich@devl.cz>
parents: 117
diff changeset
   165
    def mousemove_event(self, ev):
8c7970520632 Add mouse events, event demo.
Radek Brich <radek.brich@devl.cz>
parents: 117
diff changeset
   166
        self.log.debug('Not consumed: %s', ev)
106
abcadb7e2ef1 Use Point for mouse events, add them to Container and Widget.
Radek Brich <radek.brich@devl.cz>
parents: 105
diff changeset
   167
119
dd91747504dd Redraw widgets on request. Add scrollbar demo.
Radek Brich <radek.brich@devl.cz>
parents: 118
diff changeset
   168
    # -------------------------------------------------------------------------
dd91747504dd Redraw widgets on request. Add scrollbar demo.
Radek Brich <radek.brich@devl.cz>
parents: 118
diff changeset
   169
    #   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
   170
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
   171
    def can_focus(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
   172
        return not self.hidden and self.allow_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
   173
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
   174
    def has_focus(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
   175
        if self.parent 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
   176
            return True
119
dd91747504dd Redraw widgets on request. Add scrollbar demo.
Radek Brich <radek.brich@devl.cz>
parents: 118
diff changeset
   177
        return (self.parent.has_focus() and
dd91747504dd Redraw widgets on request. Add scrollbar demo.
Radek Brich <radek.brich@devl.cz>
parents: 118
diff changeset
   178
                self.parent.focus_widget == self)
109
105b1affc3c2 Update keypress propagation. Allow focus change by tab key. Add log property to Widget for smart logging.
Radek Brich <radek.brich@devl.cz>
parents: 97
diff changeset
   179
119
dd91747504dd Redraw widgets on request. Add scrollbar demo.
Radek Brich <radek.brich@devl.cz>
parents: 118
diff changeset
   180
    # -------------------------------------------------------------------------
dd91747504dd Redraw widgets on request. Add scrollbar demo.
Radek Brich <radek.brich@devl.cz>
parents: 118
diff changeset
   181
    #   Utilities
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
   182
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
   183
    @property
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
   184
    def log(self):
113
Radek Brich <radek.brich@devl.cz>
parents: 112 107
diff changeset
   185
        """Logger for widget debugging.
Radek Brich <radek.brich@devl.cz>
parents: 112 107
diff changeset
   186
        
Radek Brich <radek.brich@devl.cz>
parents: 112 107
diff changeset
   187
        Logger name contains full module name, class name and instance number.
Radek Brich <radek.brich@devl.cz>
parents: 112 107
diff changeset
   188
        
Radek Brich <radek.brich@devl.cz>
parents: 112 107
diff changeset
   189
        """
Radek Brich <radek.brich@devl.cz>
parents: 112 107
diff changeset
   190
        return logging.getLogger('%s.%s' % (self.__module__, self.name))