changeset 91:b447a9c6f1d3

added UnselectableListBox which is a ListBox which can be unselectable when everything is not selectable and visible. Its is used in dialogs
author Goffi <goffi@goffi.org>
date Mon, 08 Sep 2014 15:42:25 +0200
parents f5992b2a0dbf
children fdd0543677d4
files urwid_satext/sat_widgets.py
diffstat 1 files changed, 31 insertions(+), 2 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
@@ -392,6 +392,36 @@
         return [(ListOption(option)) for option in options]
 
 
+class UnselectableListBox(urwid.ListBox):
+    """List box that can be unselectable if all widget are unselectable and visible"""
+
+    def __init__(self, body):
+        super(UnselectableListBox, self).__init__(body)
+        self.__size_cache = None
+
+    def selectable(self):
+        """Selectable that return False if everything is visible and nothing is selectable"""
+        if self.__size_cache is None:
+            return self._selectable
+        middle, top, bottom = self.calculate_visible(self.__size_cache, self.__focus_cache)
+        if top is None or bottom is None:
+            return True
+        if top[0] or bottom[0]:
+            # if not everything is visible, we can select
+            return True
+        for wid in self.body:
+            # if any widget is selectable, we can select
+            if wid.selectable():
+                return True
+        return False
+
+    def render(self, size, focus=False):
+        """Call ListBox render, but keep size and focus in cache"""
+        self.__size_cache = size
+        self.__focus_cache = focus
+        return super(UnselectableListBox, self).render(size, focus)
+
+
 class GenericList(urwid.WidgetWrap):
     signals = ['click','change']
 
@@ -910,7 +940,7 @@
         if buttons:
             buttons_flow = urwid.GridFlow(buttons, max([len(button.get_label()) for button in buttons])+4, 1, 1, 'center')
         body_content = urwid.SimpleListWalker(widgets_lst)
-        frame_body = urwid.ListBox(body_content)
+        frame_body = UnselectableListBox(body_content)
         frame = FocusFrame(frame_body, frame_header, buttons_flow if buttons else None, 'footer' if buttons else 'body')
         decorated_frame = urwid.LineBox(frame)
         urwid.WidgetWrap.__init__(self, decorated_frame)
@@ -947,7 +977,6 @@
     def __init__(self, title, message, style=['OK'], **kwargs):
         GenericDialog.__init__(self, [urwid.Text(message, 'center')], title, style, ok_value=None, **kwargs)
 
-
 ## CONTAINERS ##
 
 class ColumnsRoller(urwid.Widget):