Henning Schröder avatar Henning Schröder committed cb9f23f

focus handling

Comments (0)

Files changed (4)

 
         
         # Frame the interface area at fixed VT100 size
-        self.screen = self.window.subwin(25, 80, 0, 0)
+        self.width = 80
+        self.height= 25
+        self.screen = self.window.subwin(self.height, self.width, 0, 0)
         #self.screen.box()
         #self.screen.hline(2, 1, curses.ACS_HLINE, 77)
         self.screen.refresh()
         return "".join(l)
 
         
-    
+    def xreadkeys(self):
+        keys = self.readkeys()
+        xkeys = keymap.get(keys, None)
+        if xkeys:
+            return True, xkeys
+        return False, keys
+        
+
     def refresh(self):
         self.screen.refresh()
     
         if self.queue:
             obj, event = self.queue.pop()
             debug("processing event", event, "for", obj)
-            obj.event(event)
+            if obj:
+                obj.event(event)
             return True
         return False
 
         self._event_filters = set()
         if parent:
             parent._children.add(self)
+
             
+    def setParent(self, new_parent):
+        parent = self.parent()
+        if parent:
+            parent._children.remove(self)
+        self._parent = new_parent
+        if new_parent:
+            new_parent._children.add(self)
+
             
     def children(self):
         return list(self._children)
     def event(self, event):
         for receiver_object in self._event_filters:
             if receiver_object.eventFilter(self, event):
-                return True
-        return False
-            
+                return
+
     
     def eventFilter(self, obj, event):
         pass
 app = TApplication()
 app.desktop().show()
 win = TWidget()
-win.move(TPoint(10, 10))
+win.move(TPoint(5, 5))
 win.resize(TSize(60, 15))
 l = TLabel(win)
 l.setText("Hello, world")
 b.move(TPoint(4,4))
 win.show()
 
-while app.event_loop.processEvent():
-    pass
-app.desktop().console.getchar(wait=True)
-app.desktop().console.close()
+app.run()
+##while app.event_loop.processEvent():
+##    pass
+##app.desktop().console.getchar(wait=True)
+#app.desktop().console.close()
 from tcore import TSize, TPoint, TRect, TEvent, TEventLoop, TObject, dispatch
 
 
-class FocusPolicy:
+class TFocusPolicy:
     NoFocus = 0
     TabFocus = 1
 
     
 class TEventTypes:
+    KeyPress = 6
+    #KeyRelease = 7
     FocusIn = 8
-    FocousOut = 9
+    FocusOut = 9
     Paint = 12
     Resize = 14
     Show = 17
     User = 1000
 
 
+class TKey:
+    pass
+
+
 
 class TKeyEvent(TEvent):
-    pass
+
+
+    def __init__(self, key, modifiers=None):
+        super(TKeyEvent, self).__init__(TEventTypes.KeyPress)
+        self.key = key
+        self.modifiers = modifiers
+
 
 
 class TPaintEvent(TEvent):
 
+    
     def __init__(self, rect=None):
-        super(TPaintEvent, self).__init__()
+        super(TPaintEvent, self).__init__(TEventTypes.Paint)
         self.rect = rect
 
+        
 
 class TCloseEvent(TEvent):
-    pass
+    
+    
+    def __init__(self):
+        super(TCloseEvent, self).__init__(TEventTypes.Close)
 
 
+        
 class THideEvent(TEvent):
-    pass
+
+    
+    def __init__(self):
+        super(THideEvent, self).__init__(TEventTypes.Hide)
+
 
 
 class TShowEvent(TEvent):
-    pass
+
+    
+    def __init__(self):
+        super(TShowEvent, self).__init__(TEventTypes.Show)
+
 
 
 class TResizeEvent(TEvent):
     
 
 
+class TInputHook(object):
+
+
+    def __init__(self):
+        self.xreadkeys = Console.instance.xreadkeys
+    
+
+    def poll(self):
+        extended, keys = self.xreadkeys()
+        if keys:
+            Console.instance.text(1,1, "poll %r    " % keys)
+            return TKeyEvent(keys)
+
+
 
 class TApplication(TObject):
 
     
     def __init__(self):
         super(TApplication, self).__init__()
+        self._focus_widget = None
+        self._top_level_widgets = set()
         self.event_loop = TEventLoop.instance()
+        TApplication.instance = self
         self._desktop = TDesktopWidget()
-        self._focus_widget = self
-        TApplication.instance = self
+        self.input_hook = TInputHook()
 
         #self.focusChanged = Signal()
-        
+
 
     @classmethod
     def desktop(cls):
 
     @classmethod
     def setActiveWindow(cls, window):
-        self.setFocusWidget(window.focusNextChild())
+        cls.setFocusWidget(window.focusNextChild())
 
     
     @classmethod
-    def allWidget(cls):
-        found = [self]
+    def allWidgets(cls):
+        found = list(cls.topLevelWidgets())
         for obj in found:
             for child in obj.children():
                 if isinstance(child, TWidget) and child not in found:
                     found.append(child)
-        return found[1:]
+        return found
 
     
     @classmethod
+    def _registerTopLevelWidget(cls, widget):
+        if not cls.focusWidget():
+            cls.setFocusWidget(widget)
+        cls.instance._top_level_widgets.add(widget)
+
+        
+    @classmethod
+    def _unregisterTopLevelWidget(cls, widget):
+        cls.instance._top_level_widgets.remove(widget)
+
+
+    @classmethod
+    def topLevelWidgets(cls):
+        return list(cls.instance._top_level_widgets)
+
+    
+    
+    @classmethod
     def focusWidget(cls):
         return cls.instance._focus_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))
+            if old_focus_widget:
+                cls.instance.postEvent(old_focus_widget, TFocusEvent(TEventTypes.FocusOut))
+            cls.instance.postEvent(widget, TFocusEvent(TEventTypes.FocusIn))
 
 
     def postEvent(self, receiver_object, event):
     
         
     def run(self):
-        self._desktop.show()
+        #self._desktop.show()
+        win = self.activeWindow().focusNextChild()
         while True:
-            self.processEvent()
+            if self.processEvent():
+                Console.instance.refresh()
+            event = self.input_hook.poll()
+            if event:
+                self.postEvent(self.focusWidget(), event)
 
             
-            
 
 
 class TWidget(TObject):
     
     def __init__(self, parent=None):
         super(TWidget, self).__init__(parent)
-        self._focus_policy = FocusPolicy.TabFocus
+        self._focus_policy = TFocusPolicy.TabFocus
         self._update_geom(TPoint(0,0), self.sizeHint(), silent=True)
         self._visible = True
+        if not parent:
+            TApplication._registerTopLevelWidget(self)
 
 
+    def _find_focusNextChild(self):
+        focus_widget = TApplication.focusWidget()
+        found = False
+        for child in self.children():
+            if not isinstance(child, TWidget):
+                continue
+            if child == focus_widget:
+                found = True
+                continue
+            if found or not focus_widget:
+                return child
+    
+            
     def focusNextChild(self):
-        return self
-    
-    
-    def focusPreviousChild(self):
-        return self
+        widget = self._find_focusNextChild()
+        if widget:
+            widget.setFocus()
 
     
+    def _find_focusPreviousChild(self):
+        focus_widget = TApplication.focusWidget()
+        previous = None
+        for child in self.children():
+            if child == focus_widget:
+                return previous
+            if isinstance(child, TWidget):
+                if not focus_widget:
+                    return child
+                previous = child
+
+
+    def focusPreviousChild(self):
+        widget = self._find_focusPreviousChild()
+        if widget:
+            widet.setFocus()
+
+
     def setFocusPolicy(self, policy):
         self._focus_policy = policy
 
         
+    def focusPolicy(self):
+        return self,_focus_policy
+
+        
     def setFocus(self, reason=None):
-        if self._focus_policy == FocusPolicy.TabFocus:
+        if self._focus_policy == TFocusPolicy.NoFocus:
             return
         TApplication.setFocusWidget(self)
 
             parent = obj.parent()
         return obj
 
+
+    def __del__(self):
+        TApplication._unregisterTopLevelWidget(self)
     
+
     def close(self):
         if self.parent() == None:
             self.postEvent(THideEvent())
 
     
     def event(self, event):
-        if super(TWidget, self).event(event):
-            return True
+        super(TWidget, self).event(event)
+        if event.accepted or event.ignored:
+            return
         if isinstance(event, TPaintEvent):
-            return self.paintEvent(event)
+            self.paintEvent(event)
         elif isinstance(event, TKeyEvent):
-            return self.keyPressevent(event)
+            self.keyPressEvent(event)
+        elif isinstance(event, TShowEvent):
+            self.showEvent(event)
+        elif isinstance(event, THideEvent):
+            self.hideEvent(event)
+        elif isinstance(event, TCloseEvent):
+            self.closeEvent(event)
         elif isinstance(event, TFocusEvent):
             if event.gotFocus():
-                return self.focusInEvent(event)
+                self.focusInEvent(event)
             else:
-                return self.focusOutEvent(event)
+                self.focusOutEvent(event)
 
+                
+    def showEvent(self, event):
+        if not self.parent():
+            TApplication.setActiveWindow(self)
         
+
     def focusInEvent(self, event):
         pass
             
 
 
     def keyPressEvent(self, event):
-        pass
+        if event.key == Key_Tab:
+            widget = self.parent().focusNextChild()
+            widget.setFocus()
+            event.accept()
 
 
     def paintEvent(self, event):
 
 
     def __init__(self):
+        self.console = Console()
         super(TDesktopWidget, self).__init__()
-        self._update_geom(TPoint(1, 1), TSize(78, 23))
-        self.console = Console()
+        self._update_geom(TPoint(0, 0), self.sizeHint())
+
+        
+    def sizeHint(self):
+        return TSize(self.console.width, self.console.height)
 
         
     def paintEvent(self, event):
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.