diff frontends/primitivus/custom_widgets.py @ 12:7e63429cc929

Primitivus: menu are now managed and fully working - new class MenuRoller for manager different menus - Chat window now create is own menu - some menu items added for general/contact menus
author Goffi <goffi@goffi.org>
date Fri, 16 Jul 2010 20:25:06 +0800
parents 983840425a55
children 8cccbaadb9c5
line wrap: on
line diff
--- a/frontends/primitivus/custom_widgets.py	Wed Jul 14 19:36:40 2010 +0800
+++ b/frontends/primitivus/custom_widgets.py	Fri Jul 16 20:25:06 2010 +0800
@@ -374,18 +374,26 @@
 
 class Menu(urwid.FlowWidget):
 
-    def __init__(self,loop):
+    def __init__(self,loop, x_orig=0):
+        """Menu widget
+        @param loop: main loop of urwid
+        @param x_orig: absolute start of the abscissa
+        """
         super(Menu, self).__init__()
         self.loop = loop
         self.menu_keys = []
         self.menu = {}
+        self.x_orig = x_orig
         self.shortcuts = {} #keyboard shortcuts
         self.focus_menu = 0
         self.save_bottom = None
 
     def selectable(self):
         return True
-    
+   
+    def setOrigX(self, orig_x):
+        self.x_orig = orig_x
+
     def __buildOverlay(self,menu_key,columns):
         """Build the overlay menu which show menuitems
         @param menu_key: name of the category
@@ -408,9 +416,10 @@
         elif key == 'left' and self.focus_menu > 0:
             self.focus_menu -= 1
             self._invalidate()
+            return
         elif key == 'down':
-            if not self.save_bottom:
-                column = sum([len(menu)+4 for menu in self.menu_keys[0:self.focus_menu]],self.focus_menu+1)
+            if self.menu_keys and not self.save_bottom:
+                column = sum([len(menu)+4 for menu in self.menu_keys[0:self.focus_menu]],self.focus_menu+self.x_orig)
                 self.__buildOverlay(self.menu_keys[self.focus_menu],column)
         elif key == 'up':
             if  self.save_bottom:
@@ -468,6 +477,60 @@
             self.keypress(None,'up')
             callback((category, item))
 
+class MenuRoller(urwid.WidgetWrap):
+
+    def __init__(self,menus_list):
+        """Create a MenuRoller
+        @param menus_list: list of tuple with (name, Menu_instance), name can be None
+        """
+        assert (menus_list)
+        self.selected = 0
+        self.name_list = []
+        self.menus = {}
+             
+        self.columns = urwid.Columns([urwid.Text(''),urwid.Text('')]) 
+        urwid.WidgetWrap.__init__(self, self.columns)
+        
+        for menu_tuple in menus_list:
+            name,menu = menu_tuple
+            self.addMenu(name, menu)
+
+    def __showSelected(self):
+        """show menu selected"""
+        name_txt = u'\u21c9 '+self.name_list[self.selected]+u' \u21c7 '
+        current_name = ClickableText(name_txt) 
+        name_len = len(name_txt)
+        current_menu = self.menus[self.name_list[self.selected]]
+        current_menu.setOrigX(name_len)
+        self.columns.widget_list[0] = current_name
+        self.columns.column_types[0]=('fixed', name_len)
+        self.columns.widget_list[1] = current_menu
+
+    def keypress(self, size, key):
+        if key=='up':
+            if self.columns.get_focus_column()==0 and self.selected > 0:
+                self.selected -= 1
+                self.__showSelected()
+        elif key=='down':
+            if self.columns.get_focus_column()==0 and self.selected < len(self.name_list)-1:
+                self.selected += 1
+                self.__showSelected()
+
+        return super(MenuRoller, self).keypress(size, key)
+
+    def addMenu(self, name_param, menu):
+        name = name_param or ''
+        if name not in self.name_list:
+            self.name_list.append(name)
+        self.menus[name] = menu
+        if self.name_list[self.selected] == name:
+            self.__showSelected() #if we are on the menu, we update it
+
+    def checkShortcuts(self, key):
+        for menu in self.name_list:
+            key = self.menus[menu].checkShortcuts(key)
+        return key
+        
 
 ## DIALOGS ##