tuikit/core/container.py
changeset 119 dd91747504dd
parent 118 8c7970520632
--- a/tuikit/core/container.py	Sat Feb 21 12:01:57 2015 +0100
+++ b/tuikit/core/container.py	Sun Feb 22 09:53:13 2015 +0100
@@ -16,7 +16,7 @@
         #: List of child widgets.
         self._widgets = []
         #: Widget with keyboard focus
-        self.focus_widget = None
+        self._focus_widget = None
         #: Widget on last mouse position
         self.mouse_widget = None
         #: If True, tab cycles inside container
@@ -29,22 +29,33 @@
         widget.parent = self
         widget.window = self.window
         widget.set_theme(self.theme)
+        widget.timer = self.timer
         self.layout.add(widget, *args, **kwargs)
-        if self.focus_widget is None and widget.can_focus():
-            self.focus_widget = widget
+        if self._focus_widget is None and widget.can_focus():
+            self._focus_widget = widget
+        widget.redraw()
 
     def resize(self, w, h):
         Widget.resize(self, w, h)
         self.layout.update(w, h)
 
-    def draw(self, buffer):
-        """Draw child widgets."""
-        Widget.draw(self, buffer)
+    # -------------------------------------------------------------------------
+    #   Presentation
+
+    def draw_if_needed(self, buffer):
+        """Draw this and child widgets, whichever was requested by redraw."""
+        Widget.draw_if_needed(self, buffer)
         for child in self._widgets:
             with buffer.moved_origin(child.x, child.y):
                 with buffer.clip(buffer.origin.x, buffer.origin.y,
                                  child.width, child.height):
-                    child.draw(buffer)
+                    child.draw_if_needed(buffer)
+
+    def redraw(self):
+        """Request redraw of all widgets in container."""
+        Widget.redraw(self)
+        for child in self._widgets:
+            child.redraw()
 
     def set_theme(self, theme):
         Widget.set_theme(self, theme)
@@ -56,7 +67,8 @@
         """Return cursor coordinates or None if cursor is not set
         or is set outside of widget boundaries.
 
-        If this container has child with focus, return its cursor position instead.
+        If this container has child with focus,
+        return its cursor position instead.
 
         """
         if self.focus_widget:
@@ -66,7 +78,7 @@
             cursor = cursor.moved(*self.focus_widget.pos)
         else:
             cursor = self._cursor.immutable()
-        if cursor in Rect._make((0, 0), self._size):
+        if cursor in Rect(0, 0, *self._size):
             return cursor
 
     @property
@@ -76,7 +88,8 @@
         else:
             return self._cursor_visible
 
-    ## input events ##
+    # -------------------------------------------------------------------------
+    #   Events
 
     def keypress_event(self, ev):
         # First, handle the keypress event to focused child widget
@@ -106,7 +119,8 @@
         if self.mouse_widget:
             self.mouse_widget.mousemove_event(ev.rebase(self.mouse_widget.pos))
 
-    ## focus ##
+    # -------------------------------------------------------------------------
+    #   Focus
 
     def focus_next(self, step=1):
         """Focus next child.
@@ -150,3 +164,15 @@
     def focus_previous(self):
         """Focus previous child."""
         self.focus_next(-1)
+
+    @property
+    def focus_widget(self):
+        return self._focus_widget
+
+    @focus_widget.setter
+    def focus_widget(self, widget):
+        orig_focus_widget = self._focus_widget
+        self._focus_widget = widget
+        widget.redraw()
+        if orig_focus_widget:
+            orig_focus_widget.redraw()