tuikit/core/container.py
changeset 113 6796adfdc7eb
parent 112 ce2e67e7bbb8
parent 108 11dac45bfba4
child 114 26c02bd94bd9
--- a/tuikit/core/container.py	Wed Sep 03 19:17:04 2014 +0200
+++ b/tuikit/core/container.py	Wed Sep 03 21:56:20 2014 +0200
@@ -1,5 +1,5 @@
 from tuikit.core.widget import Widget
-from tuikit.core.coords import Point
+from tuikit.core.coords import Point, Rect
 from tuikit.layouts.fixed import FixedLayout
 
 
@@ -17,6 +17,8 @@
         self._widgets = []
         #: Widget with keyboard focus
         self.focus_widget = None
+        #: Widget on last mouse position
+        self.mouse_widget = None
         #: If True, tab cycles inside container
         self.trap_focus = False
         self.layout = layout_class()
@@ -51,11 +53,21 @@
 
     @property
     def cursor(self):
+        """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 self.focus_widget:
             cursor = self.focus_widget.cursor
-            return cursor.moved(*self.focus_widget.pos)
+            if not cursor:
+                return None
+            cursor = cursor.moved(*self.focus_widget.pos)
         else:
-            return self._cursor
+            cursor = self._cursor.immutable()
+        if cursor in Rect._make((0, 0), self._size):
+            return cursor
 
     @property
     def cursor_visible(self):
@@ -79,6 +91,22 @@
         if Widget.keypress_event(self, ev):
             return True
 
+    def mousedown(self, button, pos):
+        self.mouse_child = None
+        for child in reversed(self.children):
+            if pos in child.boundaries:
+                child.mousedown(button, pos - child.pos)
+                self.mouse_child = child
+
+    def mouseup(self, button, pos):
+        if self.mouse_child:
+            self.mouse_child.mouseup(button, pos - self.mouse_child.pos)
+
+    def mousemove(self, button, pos, relpos):
+        if self.mouse_child:
+            self.mouse_child.mousemove(button,
+                pos - self.mouse_child.pos, relpos)
+
     ## focus ##
 
     def focus_next(self, step=1):