changeset 111:1cdf4a00b68d

TabsContainer: allow to select a tab when adding it
author souliane <souliane@mailoo.org>
date Sat, 22 Aug 2015 09:26:32 +0200
parents 436076392538
children b3e8edbe0a1e
files urwid_satext/sat_widgets.py
diffstat 1 files changed, 22 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/urwid_satext/sat_widgets.py	Thu Aug 20 18:39:40 2015 +0200
+++ b/urwid_satext/sat_widgets.py	Sat Aug 22 09:26:32 2015 +0200
@@ -1425,23 +1425,28 @@
         if not invisible:
             self._emit('click')
 
-    def _appendButton(self, name):
-        """Append a button to the frame header,
-        and link it to the page change method"""
+    def _appendButton(self, name, selected=False):
+        """Append a button to the frame header, and link it to the page change method.
+        
+        @param name (unicode): button name
+        @param selected (bool): set to True to select this tab
+        """
         button = CustomButton(name, self._buttonClicked, left_border = '', right_border=' | ')
         self._buttons_cont.addWidget(button, button.getSize())
-        if len(self._buttons_cont.widget_list) == 1:
-            #first button: we set the focus and the body
-            self._buttons_cont.focus_position = 0
-            self._buttonClicked(button,True)
+        count = len(self._buttons_cont.widget_list)
+        if selected or count == 1:
+            # first/selected button: we set the focus and the body
+            self.selectTab(count - 1)
 
-    def addTab(self,name,content=None):
+    def addTab(self, name, content=None, selected=False):
         """Add a page to the container
+
         @param name: name of the page (what appear on the tab)
         @param content: content of the page:
             - if None create and empty Listbox
             - if it is a list instance, create a ListBox with the list in a body
             - else it must be a box widget which will be used instead of the ListBox
+        @param selected (bool): set to True to select this tab
         @return: ListBox (content of the page)"""
         if content is None or isinstance(content, list):
             tab = urwid.ListBox(urwid.SimpleListWalker(content or []))
@@ -1449,7 +1454,7 @@
             tab = content
 
         self.tabs.append([name, tab])
-        self._appendButton(name)
+        self._appendButton(name, selected)
         return tab
 
     def addFooter(self, widget):
@@ -1457,6 +1462,14 @@
         @param widget: FlowWidget"""
         self._w.footer = widget
 
+    def selectTab(self, index):
+        """Select a tab.
+
+        @param index (int): index of the tab to select
+        """
+        self._buttons_cont.focus_position = index
+        self._buttonClicked(self._buttons_cont.widget_list[index][1], True)
+
 
 class HighlightColumns(urwid.AttrMap):
     """ Decorated columns which highlight all or some columns """