changeset 95:ca9a77f3b53e

SelectableText is now unselectable if its text is empty, AlwaysSelectable class has the former behaviour.
author Goffi <goffi@goffi.org>
date Mon, 08 Sep 2014 15:42:25 +0200
parents 66b65ed9baf2
children 44fc94b0fe18
files urwid_satext/sat_widgets.py
diffstat 1 files changed, 18 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/urwid_satext/sat_widgets.py	Mon Sep 08 15:42:25 2014 +0200
+++ b/urwid_satext/sat_widgets.py	Mon Sep 08 15:42:25 2014 +0200
@@ -203,28 +203,30 @@
         return urwid.Text(render_text)
 
 
-class SelectableText(urwid.WidgetWrap):
+class AlwaysSelectableText(urwid.WidgetWrap):
     """Text which can be selected with space"""
     signals = ['change']
 
     def __init__(self, text, align='left', header='', focus_attr='default_focus', selected_text=None, selected=False, data=None):
-        """@param text: same as urwid.Text's text parameter
+        """
+        @param text: same as urwid.Text's text parameter
         @param align: same as urwid.Text's align parameter
         @select_attr: attrbute to use when selected
-        @param selected: is the text selected ?"""
+        @param selected: is the text selected ?
+        """
         self.focus_attr = focus_attr
         self.__selected = False
         self.__was_focused = False
         self.header = header
-        self.default_txt = text
+        self.text = text
         urwid.WidgetWrap.__init__(self, urwid.Text("",align=align))
         self.setSelectedText(selected_text)
         self.setState(selected)
 
     def getValue(self):
-        if isinstance(self.default_txt,basestring):
-            return self.default_txt
-        list_attr = self.default_txt if isinstance(self.default_txt, list) else [self.default_txt]
+        if isinstance(self.text,basestring):
+            return self.text
+        list_attr = self.text if isinstance(self.text, list) else [self.text]
         txt = ""
         for attr in list_attr:
             if isinstance(attr,tuple):
@@ -239,7 +241,7 @@
 
     def set_text(self, text):
         """/!\ set_text doesn't change self.selected_txt !"""
-        self.default_txt = text
+        self.text = text
         self.setState(self.__selected,invisible=True)
 
     def setSelectedText(self, text=None):
@@ -251,10 +253,9 @@
         if self.__selected:
             self.setState(self.__selected)
 
-
     def __set_txt(self):
         txt_list = [self.header]
-        txt = self.selected_txt if self.__selected else self.default_txt
+        txt = self.selected_txt if self.__selected else self.text
         if isinstance(txt,list):
             txt_list.extend(txt)
         else:
@@ -318,6 +319,13 @@
         return self._w.render(size, focus)
 
 
+class SelectableText(AlwaysSelectableText):
+    """Like AlwaysSelectableText but not selectable when text is empty"""
+
+    def selectable(self):
+        return bool(self.text)
+
+
 class ClickableText(SelectableText):
     signals = SelectableText.signals + ['click']