Commits

Henning Schröder committed ca67e30

started TAction, TMenu and TMenuBar

  • Participants
  • Parent commits 2544b2a

Comments (0)

Files changed (2)

-from tgui import TApplication, TWidget, TLabel, TPoint, TSize, TPushButton, TCheckBox, TLineEdit
+from tgui import TApplication, TWidget, TLabel, TPoint, TSize, TPushButton, TCheckBox, TLineEdit, TMenu
 
 
 def test():
 app = TApplication()
 app.desktop().show()
 
-win = TWidget(Pos=(4,4), Size=(30,10), WindowTitle="Foo bar")
+win = TWidget(Pos=(4,4), Size=(50,10), WindowTitle="Foo bar")
 
 c = TCheckBox(win, Text="Is this working?", Pos=(2,3), Size=(10,10))
 
 
 e = TLineEdit(win, Text="edit me..", Pos=(3,4), Size=(18,1))
 
+m = TMenu(win, Pos=(22, 2), Size=(5,5))
+m.addAction("foo")
+m.addAction("bar")
+
 win.show()
 
 app.run()
 
 
 
+
+class TAction(TObject):
+
+    changed = Signal()
+    triggered = Signal()
+
+
+    def __init__(self, parent=None, **kwargs):
+        self._text = ""
+        self._checked = False
+        super(TAction, self).__init__(parent, **kwargs)
+
+        
+    def text(self):
+        return self._text
+    
+    
+    def setText(self, text):
+        self._text = text
+        self.changed.emit()
+
+        
+    Text = Property(text, setText)
+
+    
+    def setChecked(self, flag):
+        self._checked = flag
+        self.changed.emit()
+
+        
+    def isChecked(self):
+        return self._checked
+
+    
+    Checked = Property(isChecked, setChecked)
+
+    
+    def trigger(self):
+        self.triggered.emit()
+
+
 class TWidget(TObject):
 
     
         self._focus_widget = None
         self._palette = TPalette()
         self._windowTitle = ""
+        self._actions = []
         super(TWidget, self).__init__(parent, **kwargs)
         if not parent:
             self.activateWindow()
 
+
+    def actions(self):
+        return self._actions[:]
             
+        
+    @dispatch(str)
+    def addAction(self, title):
+        return self.addAction(TAction(self, Text=title))
+        
+    
+    @dispatch(TAction)
+    def addAction(self, action):
+        self._actions.append(action)
+        self.update()
+        return action
+
+
     def windowTitle(self):
         return self._windowTitle
 
         for child in self.window().children():
             if not isinstance(child, TWidget):
                 continue
-            if child.children():
-                l.extend(child._focusable_widget())
+            if 0: #child.children():
+                pass # XXX: l.extend(child._focusable_widgets())
             elif child.focusPolicy() != TFocusPolicy.NoFocus:
                 l.append(child)
         return l
         else:    
             self.postEvent(THideEvent())
         for child in self.children():
-            child.setVisible(flag)
+            if isinstance(child, TWidget):
+                child.setVisible(flag)
 
             
     def isVisible(self):
 
     def paintEvent(self, event):
         painter = TPainter(self)
-        painter.drawText(self.pos(), self.text())
+        painter.drawText(self.pos(), elide(self.text(), self.width()))
 
 
 
 
 
 class TLineEdit(TWidget):
+    
+    cursorPositionChanged = Signal()
+    textChanged = Signal()
 
 
     def __init__(self, parent, **kwargs):
     
     
     def setText(self, text):
-        self._text = text
-        self.update()        
+        old_text = self._text
+        if old_text != text:
+            self._text = text
+            self.update()
+            self.textChanged.emit(text)
+
     
     Text = Property(text, setText)
         
     
     
     def setCursorPosition(self, pos):
-        self._cursorPosition = pos
-        pos = self.mapToGlobal(self.pos())
-        Console.instance.gotoxy(pos.x + self.cursorPosition() + 1, pos.y)
-        Console.instance.show_cursor()
-        
-        
+        old_pos = self._cursorPosition
+        if pos != old_pos:
+            self._cursorPosition = pos
+            pos = self.mapToGlobal(self.pos())
+            Console.instance.gotoxy(pos.x + self.cursorPosition() + 1, pos.y)
+            Console.instance.show_cursor()
+            self.cursorPositionChanged.emit(old_pos, pos)
+
+
     CursorPosition = Property(cursorPosition, setCursorPosition)
 
     
 
         
     def keyPressEvent(self, event):
-        if event.key in (TKey.Key_Tab, TKey.Key_Backtab):
+        if event.key in (TKey.Key_Tab, TKey.Key_Backtab, 
+                         TKey.Key_Return, TKey.Key_Enter):
             pass
         elif event.key or event.text:
             text = self.text()
                     self.setCursorPosition(cpos) # XXX
                 else:
                     self.setCursorPosition(cpos + 1)
-            elif event.key == TKey.Key_Backspace:            
+            elif event.key == TKey.Key_Backspace:     
                 new_text = text[:tpos-1] + text[tpos:]
                 self.setText(new_text)
                 if tpos > 0 and cpos == 0:
 
         super(TLineEdit, self).keyPressEvent(event)
 
+
         
-    
+class TMenu(TWidget):
+
+
+    def __init__(self, parent, **kwargs):
+        self._actions = []
+        self._current = -1
+        super(TMenu, self).__init__(parent, **kwargs)
+        self._menuAction = TAction(self)
+
+        
+    def menuAction(self):
+        return self._menuAction
+    
+    
+    def setMenuAction(self, action):
+        self._menuAction = action
+    
+        
+    MenuAction = Property(menuAction, setMenuAction)
+
+    
+    def title(self):
+        return self.menuAction().title()
+    
+    
+    def setTitle(self, title):
+        self.menuAction().setText(title)
+        #self.update()        
+
+        
+    Title = Property(title, setTitle)
+        
+
+    def sizeHint(self):
+        w = max(len(action.text()) for action in self.actions())
+        h = len(self._actions)
+        return TSize(min(40, w), min(20, h))
+
+    
+    def actionAction(self):
+        self.actions()[self._current]
+        
+    
+    def setActiveAction(self, action):
+        self._current = self.actions().index(action)
+        self.update()
+
+    
+    def keyPressEvent(self, event):
+        if event.key == TKey.Key_Down:
+            self._current += 1
+            if self._current >= len(self.actions()):
+                self._current = 0
+            self.update()
+        elif event.key == TKey.Key_Up:
+            self._current -= 1
+            if self._current < 0:
+                self._current = len(self.actions()) - 1
+            self.update()
+        elif event.key in (TKey.Key_Return, TKey.Key_Enter):
+            self.activeAction().trigger()
+        else:
+            super(TMenu, self).keyPressEvent(event)
+
+        
+    def paintEvent(self, event):
+        pos = self.mapToParent(self.pos())
+        painter = TPainter(self)
+        painter.drawRect(self.geometry())
+        h = self.height() - 2
+        w = self.width() - 2
+        for i, action in enumerate(self.actions()[:h]):
+            if i == self._current:
+                text = ">%s<" % elide(action.text(), w - 2)
+            else:
+                text = " %s " % elide(action.text(), w - 2)
+            painter.drawText(pos.x+2, pos.y+i + 1, text)
+
+
+
+class TMenuBar(TWidget):
+
+
+    def __init__(self, parent, **kwargs):
+        self._menus = []
+        self._current = -1
+        super(TMenuBar, self).__init__(parent, **kwargs)
+
+        
+    def addMenu(self, menu):
+        self._menus.append(menu)
+        
+        
+    def keyPressEvent(self, event):
+        if event.key == TKey.Key_Right:
+            self._current += 1
+            if self._current >= len(self._menus):
+                self._current = 0
+            self.update()
+        elif event.key == TKey.Key_Left:
+            self._current -= 1
+            if self._current < 0:
+                self._current = len(self._menus) - 1
+            self.update()
+        elif event.key in (TKey.Key_Return, TKey.Key_Enter):
+            self._menus[self._current].show()
+        else:
+            super(TMenuBar, self).keyPressEvent(event)
+
+        
+    def paintEvent(self, event):
+        painter = TPainter(self)
+        pos = self.pos()
+        dx = 0
+        for menu in self._menus:
+            title = " %s " % elide(menu.title(), self.width() - dx)
+            painter.drawText(0, dx, title)
+            dx += len(title) + 2
+            
+