tuikit/core/widget.py
changeset 109 105b1affc3c2
parent 97 0c2e0c09ba5c
child 111 b055add74b18
equal deleted inserted replaced
97:0c2e0c09ba5c 109:105b1affc3c2
     1 from tuikit.core.coords import Point, Size
     1 from tuikit.core.coords import Point, Size
     2 from tuikit.core.theme import default_theme
     2 from tuikit.core.theme import default_theme
       
     3 from tuikit.core.signal import Signal
     3 
     4 
     4 import logging
     5 import logging
     5 
     6 
     6 
     7 
     7 class Widget:
     8 class Widget:
    10 
    11 
    11     _num_instances = 0
    12     _num_instances = 0
    12 
    13 
    13     def __init__(self):
    14     def __init__(self):
    14         self._num_instances += 1
    15         self._num_instances += 1
    15         self._log = logging.getLogger(__name__)
       
    16 
    16 
    17         #: Widget name is used for logging etc. Not visible anywhere.
    17         #: Widget name is used for logging etc. Not visible anywhere.
    18         self.name = '{}{}'.format(
    18         self.name = '{}{}'.format(
    19             self.__class__.__name__,
    19             self.__class__.__name__,
    20             self._num_instances)
    20             self._num_instances)
    42         #: Cursor is position where text input will occur.
    42         #: Cursor is position where text input will occur.
    43         #: It is displayed on screen if widget is active.
    43         #: It is displayed on screen if widget is active.
    44         #: None means no cursor (hidden).
    44         #: None means no cursor (hidden).
    45         self._cursor = None
    45         self._cursor = None
    46 
    46 
       
    47         #: Hidden widget does not affect layout.
       
    48         self.hidden = False
       
    49         #: Allow keyboard focus for this widget.
       
    50         self.allow_focus = False
       
    51 
       
    52         self.sig_keypress = Signal(allow_stop=True)
       
    53 
    47     ## position and size ##
    54     ## position and size ##
    48 
    55 
    49     @property
    56     @property
    50     def x(self):
    57     def x(self):
    51         return self.pos.x
    58         return self.pos.x
    71 
    78 
    72     ## drawing, looks ##
    79     ## drawing, looks ##
    73 
    80 
    74     def draw(self, buffer):
    81     def draw(self, buffer):
    75         """Draw self into buffer."""
    82         """Draw self into buffer."""
    76         self._log.debug('%s draw into %r at %s (exposed %s)',
    83         self.log.debug('Draw into %r at %s (exposed %s)',
    77                         self.name, buffer, buffer.origin, self.exposed(buffer))
    84                        buffer, buffer.origin, self.exposed(buffer))
    78 
    85 
    79     def set_theme(self, theme):
    86     def set_theme(self, theme):
    80         self.theme = theme
    87         self.theme = theme
    81 
    88 
    82     @staticmethod
    89     @staticmethod
    97             return Point(self._cursor)
   104             return Point(self._cursor)
    98 
   105 
    99     ## input events ##
   106     ## input events ##
   100 
   107 
   101     def keypress(self, keyname, char, mod):
   108     def keypress(self, keyname, char, mod):
   102         self._log.debug('%s keypress(%r, %r, %r)',
   109         """Keypress event handler.
   103                         self.name, keyname, char, mod)
   110 
       
   111         Override to accept keyboard input.
       
   112 
       
   113         Returns True if event was consumed.
       
   114 
       
   115         Call this implementation from inherited classes
       
   116         if it does not consume the event.
       
   117 
       
   118         """
       
   119         if self.sig_keypress(keyname, char, mod):
       
   120             return True
       
   121         self.log.debug('Not consumed: keypress(%r, %r, %r)', keyname, char, mod)
   104 
   122 
   105     ## timeouts ##
   123     ## timeouts ##
   106 
   124 
   107     def add_timeout(self, delay, callback, *args):
   125     def add_timeout(self, delay, callback, *args):
   108         """Register `callback` to be called after `delay` seconds."""
   126         """Register `callback` to be called after `delay` seconds."""
   113 
   131 
   114         Removes all timeouts with the `callback` and `args`.
   132         Removes all timeouts with the `callback` and `args`.
   115 
   133 
   116         """
   134         """
   117         self.parent.remove_timeout(self, callback, *args)
   135         self.parent.remove_timeout(self, callback, *args)
       
   136 
       
   137     ## focus ##
       
   138 
       
   139     def can_focus(self):
       
   140         return not self.hidden and self.allow_focus
       
   141 
       
   142     def has_focus(self):
       
   143         if self.parent is None:
       
   144             return True
       
   145         return (self.parent.has_focus()
       
   146             and self.parent.focus_widget == self)
       
   147 
       
   148     ## utilities ##
       
   149 
       
   150     @property
       
   151     def log(self):
       
   152         return logging.getLogger('tuikit.' + self.name)