Commits

Henning Schröder committed 5f91e5d

focus events

  • Participants
  • Parent commits 29d9d97

Comments (0)

Files changed (2)

         dispatch_map[key + types] = func
         def inner(self, *args, **kwargs):
             arg_types = tuple([type(a) for a in args])
-            real_func = dispatch_map.get(key + arg_types)
+            real_func = dispatch_map.get(key + arg_types, None)
+            if real_func is None:
+                raise TypeError("Not method %s with signature %s found" % (func.__name__, str(arg_types)))
             return real_func(self, *args, **kwargs)
         return inner
     return wrapper
 class TEvent(object):
 
     
-    def __init__(self):
+    def __init__(self, kind):
+        self.kind = kind
         self.accepted = False
         self.ignored = False
 
-
     
     def accept(self):
         self.accepted = True
     NoFocus = 0
     TabFocus = 1
 
+    
+class TEventTypes:
+    FocusIn = 8
+    FocousOut = 9
+    Paint = 12
+    Resize = 14
+    Show = 17
+    Hide = 18
+    Close = 19
+    User = 1000
+
+
 
 class TKeyEvent(TEvent):
     pass
 
 
 class TResizeEvent(TEvent):
+
     
     def __init__(self, size):
-        super(TResizeEvent, self).__init__()
+        super(TResizeEvent, self).__init__(TEventTypes.Resize)
         self.size = size
 
 
+
 class TFocusEvent(TEvent):
-    pass
+
+
+    def __init__(self, kind, reason=None):
+        super(TFocusEvent, self).__init__(kind)
+        self.reason = reason
+
+
+    def gotFocus(self):
+        return self.kind == TEventTypes.FocusIn
+
+
+    def lostFocus(self):
+        return self.kind == TEventTypes.FocusOut
+
 
 
         
         if obj:
             self.stack.append(obj)
         self.obj = obj
-        self.con = Console.instance
+        self.engine = Console.instance
 
 
     def begin(self, obj):
         x2 = x1 + width - 1
         y2 = y1 + height - 1
         # topleft
-        self.con.text(x1, y1, "+")
+        self.engine.text(x1, y1, "+")
         # topright
-        self.con.text(x2, y1, "+")
+        self.engine.text(x2, y1, "+")
         # top line
-        self.con.text(x1 + 1, y1, "-" * (width-2))
+        self.engine.text(x1 + 1, y1, "-" * (width-2))
         # bottomleft
-        self.con.text(x1, y2, "+")
+        self.engine.text(x1, y2, "+")
         # bottomright
-        self.con.text(x2, y2, "+")
+        self.engine.text(x2, y2, "+")
         # bottom line
-        self.con.text(x1+1, y2, "-" * (width-2))
+        self.engine.text(x1+1, y2, "-" * (width-2))
         # side lines
         for y in range(y1+1, y2):
-            self.con.text(x1, y, "|") # left
-            self.con.text(x2, y, "|") # right
+            self.engine.text(x1, y, "|") # left
+            self.engine.text(x2, y, "|") # right
 
 
     
         x2, y2 = end_pos.x, end_pos.y
         if y1 == y1:
             width = x2 - x1
-            self.con.text(x1, y1, "-" * width)
+            self.engine.text(x1, y1, "-" * width)
         elif x1 == x2:
             for y in range(y1, y2+1):
-                self.con.text(x1, y, "|")
+                self.engine.text(x1, y, "|")
         else:
             raise NotImplementedError("Only horizontal and vertial lines supported right now")
 
         
     def drawText(self, pos, text):
         gpos = self.obj.mapToGlobal(pos)
-        self.con.text(gpos.x, gpos.y, text)
+        self.engine.text(gpos.x, gpos.y, text)
         
 
 
         x2 = x1 + width - 1
         y2 = y1 + height - 1
         for y in range(y1, y2):
-            self.con.text(x1, y, char*width)
+            self.engine.text(x1, y, char*width)
     
 
 
         super(TApplication, self).__init__()
         self.event_loop = TEventLoop.instance()
         self._desktop = TDesktopWidget()
+        self._focus_widget = self
         TApplication.instance = self
 
+        #self.focusChanged = Signal()
         
-    def desktop(self):
-        return self._desktop
+
+    @classmethod
+    def desktop(cls):
+        return cls.instance._desktop
+
+    
+    @classmethod
+    def activeWindow(cls):
+        return cls.focusWidget().window()
+
+
+    @classmethod
+    def setActiveWindow(cls, window):
+        self.setFocusWidget(window.focusNextChild())
+
+    
+    @classmethod
+    def allWidget(cls):
+        found = [self]
+        for obj in found:
+            for child in obj.children():
+                if isinstance(child, TWidget) and child not in found:
+                    found.append(child)
+        return found[1:]
+
+    
+    @classmethod
+    def focusWidget(cls):
+        return cls.instance._focus_widget
+    
+    
+    @classmethod
+    def setFocusWidget(cls, widget):
+        old_focus_widget = cls.instance._focus_widget
+        if widget != old_focus_widget:
+            cls.instance._focus_widget = widget
+            self.postMessage(old_focus_widget, TFocusEvent(TEventTypes.FocusOut))
+            self.postMessage(widget, TFocusEvent(TEventTypes.FocusIn))
 
 
     def postEvent(self, receiver_object, event):
         self.event_loop.postEvent(receiver_object, event)
-    
+
     
     def sendEvent(self, receiver_object, event):
         self.event_loop.sendEvent(receiver_object, event)
         self._update_geom(TPoint(0,0), self.sizeHint(), silent=True)
         self._visible = True
 
+
+    def focusNextChild(self):
+        return self
+    
+    
+    def focusPreviousChild(self):
+        return self
+
     
     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())
+        TApplication.setFocusWidget(self)
 
         
     def hasFocus(self):
-        return Application.instance.focusWidget == self
+        return TApplication.focusWidget() == self
 
 
+    def clearFocus(self):
+        if self.hasFocus():
+            self.postEvent(TFocusEvent())
+
+
+    
     def sizeHint(self):
         return TSize(1,1)
 
         
     def window(self):
-        parent = self
-        parent_parent = parent.parent()
-        while parent_parent:
-            parent = parent_parent
-            parent_parent = parent.parent()
-        return parent
+        obj = self
+        parent = obj.parent()
+        while parent:
+            obj = parent
+            parent = obj.parent()
+        return obj
 
     
     def close(self):
-        pass
-    
-        
+        if self.parent() == None:
+            self.postEvent(THideEvent())
+            self.postEvent(TCloseEvent())
+            self.clearFocus()
+            return True
+        else:
+            return False
+
+ 
+       
     def show(self):
         self.setVisible(True)
         
         elif isinstance(event, TKeyEvent):
             return self.keyPressevent(event)
         elif isinstance(event, TFocusEvent):
-            return self.focusInEvent(event)
+            if event.gotFocus():
+                return self.focusInEvent(event)
+            else:
+                return self.focusOutEvent(event)
 
         
     def focusInEvent(self, event):
         pass
             
+    
+    def focusOutEvent(self, event):
+        pass
+
 
     def keyPressEvent(self, event):
         pass