tuikit/container.py
changeset 42 0224ce40792f
parent 41 37b7dfc3eae6
child 45 43b2279b06e1
equal deleted inserted replaced
41:37b7dfc3eae6 42:0224ce40792f
     1 # -*- coding: utf-8 -*-
     1 # -*- coding: utf-8 -*-
     2 
     2 
     3 from tuikit.widget import Widget
     3 from tuikit.widget import Widget
     4 from tuikit.common import Borders, Coords
     4 from tuikit.common import Borders, Coords
     5 
       
     6 import logging
       
     7 
     5 
     8 
     6 
     9 class Container(Widget):
     7 class Container(Widget):
    10 
     8 
    11     '''Container widget. Base for any widget which can contain other widgets.'''
     9     '''Container widget. Base for any widget which can contain other widgets.'''
    46         widget.top = self.top
    44         widget.top = self.top
    47         widget.hints.update(kwargs)
    45         widget.hints.update(kwargs)
    48         if self.focuschild is None and widget.can_focus():
    46         if self.focuschild is None and widget.can_focus():
    49             self.focuschild = widget
    47             self.focuschild = widget
    50 
    48 
       
    49     @property
       
    50     def layout(self):
       
    51         return self._layout
    51 
    52 
    52     def layout(self, layout):
    53     @layout.setter
    53         '''Set layout manager for placing child widgets.'''
    54     def layout(self, value):
    54         self._layout = layout
    55         """Layout manager for placing child widgets."""
    55         layout.container = self
    56         self._layout = value
       
    57         self._layout.container = self
    56 
    58 
    57 
    59 
    58     def _set_top(self, value):
    60     def _set_top(self, value):
    59         self._top = value
    61         self._top = value
    60         for child in self.children:
    62         for child in self.children:
    89             if self.children[idx_new].can_focus():
    91             if self.children[idx_new].can_focus():
    90                 self.children[idx_new].set_focus()
    92                 self.children[idx_new].set_focus()
    91                 return self.trap_focus or not cycled
    93                 return self.trap_focus or not cycled
    92             idx_new += 1
    94             idx_new += 1
    93 
    95 
    94     def _handle_keypress(self, ev):
       
    95         super()._handle_keypress(ev)
       
    96         if self.focuschild is not None:
       
    97             handled = self.focuschild.emit('keypress', ev)
       
    98             if handled:
       
    99                 return True
       
   100         if ev.keyname == 'tab':
       
   101             return self.focus_next()
       
   102 
       
   103     def _handle_resize(self, ev):
       
   104         super()._handle_resize(ev)
       
   105         for child in self.children:
       
   106             child.emit('resize')
       
   107 
       
   108 
       
   109     def draw(self, driver, x, y):
    96     def draw(self, driver, x, y):
   110         """Draw the container and its children.
    97         """Draw the container and its children.
   111 
    98 
   112         This method should not be overriden by subclasses,
    99         This method should not be overriden by subclasses,
   113         use on_draw instead.
   100         use on_draw instead.
   137 
   124 
   138         if self.colorprefix:
   125         if self.colorprefix:
   139             driver.popcolorprefix()
   126             driver.popcolorprefix()
   140         driver.clipstack.pop()
   127         driver.clipstack.pop()
   141 
   128 
       
   129     def _handle_resize(self, ev):
       
   130         super()._handle_resize(ev)
       
   131         for child in self.children:
       
   132             child.emit('resize')
       
   133 
       
   134     def _handle_keypress(self, ev):
       
   135         super()._handle_keypress(ev)
       
   136         if self.focuschild is not None:
       
   137             handled = self.focuschild.emit('keypress', ev)
       
   138             if handled:
       
   139                 return True
       
   140         if ev.keyname == 'tab':
       
   141             return self.focus_next()
   142 
   142 
   143     def _handle_mousedown(self, ev):
   143     def _handle_mousedown(self, ev):
   144         super()._handle_mousedown(ev)
   144         super()._handle_mousedown(ev)
   145         handled = False
   145         handled = False
   146         for child in reversed(self.children):
   146         for child in reversed(self.children):