Commits

Henning Schröder  committed 29d9d97

fixes

  • Participants
  • Parent commits 820aa68

Comments (0)

Files changed (2)

 # -*- coding: utf-8 -*-
+import sys
+
 from console import debug
 from collections import namedtuple
 
-    
-_dispatch_map = {}
+
 
 def dispatch(*types):
+    context = sys._getframe(1).f_locals
+    dispatch_map = context.setdefault("_dispatch_map", {})
     def wrapper(func):
-        def inner(*args, **kwargs):
+        key = (func.__name__,)
+        dispatch_map[key + types] = func
+        def inner(self, *args, **kwargs):
             arg_types = tuple([type(a) for a in args])
-            key = (getattr(func, "im_self", ""), func.__name__, arg_types)
-            real_func =_dispatch_map.get(key)
-            return real_func(*args, **kwargs)
-        key = (getattr(func, "im_self", ""), func.__name__, types)
-        _dispatch_map[key] = func
+            real_func = dispatch_map.get(key + arg_types)
+            return real_func(self, *args, **kwargs)
         return inner
     return wrapper
 
 # -*- coding: utf-8 -*-
 from console import Console, debug
-from tcore import TSize, TPoint, TRect, TEvent, TEventLoop, TObject
+from tcore import TSize, TPoint, TRect, TEvent, TEventLoop, TObject, dispatch
+
+
+class FocusPolicy:
+    NoFocus = 0
+    TabFocus = 1
 
 
 class TKeyEvent(TEvent):
         self.size = size
 
 
+class TFocusEvent(TEvent):
+    pass
+
 
         
 class TPainter(object):
     
     def __init__(self, parent=None):
         super(TWidget, self).__init__(parent)
+        self._focus_policy = FocusPolicy.TabFocus
         self._update_geom(TPoint(0,0), self.sizeHint(), silent=True)
         self._visible = True
+
+    
+    def setFocusPolicy(self, policy):
+        self._focus_policy = policy
+
         
+    def setFocus(self, reason=None):
+        if self._focus_policy == FocusPolicy.TabFocus:
+            return
+        if Application.instance.focusWidget != self:
+            Application.instance.focusWidget = self
+            self.postMessage(TFocusEvent())
+
+        
+    def hasFocus(self):
+        return Application.instance.focusWidget == self
+
 
     def sizeHint(self):
         return TSize(1,1)
         return self.pos().y
     
 
-
+    @dispatch(TSize)
     def resize(self, size):
         (x1, y1, x2, y2) = self._geom
         width, height = size.width, size.height
         self._update_geom(TPoint(x1, y1), TSize(width, height))
 
         
+    @dispatch(int,int)
+    def resize(self, width, height):
+        self.resize(TSize(width, height))
+
+        
     def pos(self):
         (x1, y1, x2, y2) = self._geom
         return TPoint(x1, y1)
 
 
+    @dispatch(TPoint)
     def move(self, pos):
         self._update_geom(pos, self.size())
 
+    
+    @dispatch(int,int)
+    def move(self, x, y):
+        self.move(TPoint(x, y))
+
 
     def geometry(self):
         return TRect(self.pos(), self.size())
 
     
+    @dispatch(TPoint, TSize)
     def setGeometry(self, pos, size):
         self._update_geom(pos, size)
 
+        
+    @dispatch(int,int,int,int)
+    def setGeometry(self, x1, y1, width, height):
+        self._update_geom(TPoint(x1, y1), TSize(width, height))
+        
+        
+    @dispatch(TRect)
+    def setGeometry(self, rect):
+        self._update_geom(rect.topLeft, rect.size)
+
     
     def mapToParent(self, pos):
         parent = self.parent()
             return self.paintEvent(event)
         elif isinstance(event, TKeyEvent):
             return self.keyPressevent(event)
+        elif isinstance(event, TFocusEvent):
+            return self.focusInEvent(event)
 
+        
+    def focusInEvent(self, event):
+        pass
             
+
     def keyPressEvent(self, event):
         pass