tuikit/core/widget.py
changeset 113 6796adfdc7eb
parent 112 ce2e67e7bbb8
parent 107 1822c37b2688
child 114 26c02bd94bd9
equal deleted inserted replaced
112:ce2e67e7bbb8 113:6796adfdc7eb
     1 from tuikit.core.coords import Point, Size
     1 from tuikit.core.coords import Point, Size, Rect
     2 from tuikit.core.theme import default_theme
     2 from tuikit.core.theme import default_theme
     3 from tuikit.core.signal import Signal
     3 from tuikit.core.signal import Signal
     4 
     4 
     5 import logging
     5 import logging
     6 
     6 
    13 
    13 
    14     def __init__(self):
    14     def __init__(self):
    15         self._num_instances += 1
    15         self._num_instances += 1
    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 = '%s#%s' % (self.__class__.__name__, self._num_instances)
    19             self.__class__.__name__,
       
    20             self._num_instances)
       
    21 
    19 
    22         #: Parent Widget
    20         #: Parent Widget
    23         self.parent = None
    21         self.parent = None
    24         #: Window owning this Widget
    22         #: Window owning this Widget
    25         self.window = None
    23         self.window = None
    38         #: Maximum size of widget. Widget will never be sized bigger than this.
    36         #: Maximum size of widget. Widget will never be sized bigger than this.
    39         #: None means no maximum size (infinite).
    37         #: None means no maximum size (infinite).
    40         self.sizemax = Size(None, None)
    38         self.sizemax = Size(None, None)
    41 
    39 
    42         #: Cursor is position where text input will occur.
    40         #: Cursor is position where text input will occur.
       
    41         #: The cursor coordinates are relative to widget.
    43         self._cursor = Point()
    42         self._cursor = Point()
    44         #: Cursor is displayed on screen only if the widget is focused.
    43         #: Cursor is displayed on screen only when the widget is focused.
    45         self._cursor_visible = False
    44         self._cursor_visible = False
    46 
    45 
    47         #: Hidden widget does not affect layout.
    46         #: Hidden widget does not affect layout.
    48         self.hidden = False
    47         self.hidden = False
    49         #: Allow keyboard focus for this widget.
    48         #: Allow keyboard focus for this widget.
    69     def height(self):
    68     def height(self):
    70         return self.size.h
    69         return self.size.h
    71 
    70 
    72     @property
    71     @property
    73     def size(self):
    72     def size(self):
    74         return self._size.readonly()
    73         return self._size.immutable()
    75 
    74 
    76     def resize(self, w, h):
    75     def resize(self, w, h):
    77         self._size.update(w, h)
    76         self._size.update(w, h)
       
    77 
       
    78     @property
       
    79     def boundaries(self):
       
    80         return Rect._make(self.pos, self._size)
    78 
    81 
    79     ## drawing, looks ##
    82     ## drawing, looks ##
    80 
    83 
    81     def draw(self, buffer):
    84     def draw(self, buffer):
    82         """Draw self into buffer."""
    85         """Draw self into buffer."""
    98         """
   101         """
    99         return buffer.clip_rect.moved(-buffer.origin.x, -buffer.origin.y)
   102         return buffer.clip_rect.moved(-buffer.origin.x, -buffer.origin.y)
   100 
   103 
   101     @property
   104     @property
   102     def cursor(self):
   105     def cursor(self):
   103         return self._cursor
   106         """Return cursor coordinates.
       
   107         
       
   108         Returns None if cursor is set outside of widget boundaries.
       
   109         
       
   110         """
       
   111         if self._cursor in Rect._make((0, 0), self._size):
       
   112             return self._cursor
   104 
   113 
   105     @property
   114     @property
   106     def cursor_visible(self):
   115     def cursor_visible(self):
   107         return self._cursor_visible
   116         return self._cursor_visible
   108 
   117 
   123 
   132 
   124         """
   133         """
   125         if self.sig_keypress(ev):
   134         if self.sig_keypress(ev):
   126             return True
   135             return True
   127         self.log.debug('Not consumed: %s', ev)
   136         self.log.debug('Not consumed: %s', ev)
       
   137 
       
   138     def mousedown(self, button, pos):
       
   139         self._log.debug('mousedown(btn=%r, pos=%r)',
       
   140                         button, pos)
       
   141 
       
   142     def mouseup(self, button, pos):
       
   143         self._log.debug('mouseup(btn=%r, pos=%r)',
       
   144                         button, pos)
       
   145 
       
   146     def mousemove(self, button, pos, relpos):
       
   147         self._log.debug('mousemove(btn=%r, pos=%r, relpos=%r)',
       
   148                         button, pos, relpos)
   128 
   149 
   129     ## timeouts ##
   150     ## timeouts ##
   130 
   151 
   131     def add_timeout(self, delay, callback, *args):
   152     def add_timeout(self, delay, callback, *args):
   132         """Register `callback` to be called after `delay` seconds."""
   153         """Register `callback` to be called after `delay` seconds."""
   153 
   174 
   154     ## utilities ##
   175     ## utilities ##
   155 
   176 
   156     @property
   177     @property
   157     def log(self):
   178     def log(self):
   158         return logging.getLogger('tuikit.' + self.name)
   179         """Logger for widget debugging.
       
   180         
       
   181         Logger name contains full module name, class name and instance number.
       
   182         
       
   183         """
       
   184         return logging.getLogger('%s.%s' % (self.__module__, self.name))