Mercurial > urwid-satext
comparison urwid_satext/sat_widgets.py @ 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 | 66b65ed9baf2 |
comparison
equal
deleted
inserted
replaced
90:f5992b2a0dbf | 91:b447a9c6f1d3 |
---|---|
390 return: list of ListOption | 390 return: list of ListOption |
391 """ | 391 """ |
392 return [(ListOption(option)) for option in options] | 392 return [(ListOption(option)) for option in options] |
393 | 393 |
394 | 394 |
395 class UnselectableListBox(urwid.ListBox): | |
396 """List box that can be unselectable if all widget are unselectable and visible""" | |
397 | |
398 def __init__(self, body): | |
399 super(UnselectableListBox, self).__init__(body) | |
400 self.__size_cache = None | |
401 | |
402 def selectable(self): | |
403 """Selectable that return False if everything is visible and nothing is selectable""" | |
404 if self.__size_cache is None: | |
405 return self._selectable | |
406 middle, top, bottom = self.calculate_visible(self.__size_cache, self.__focus_cache) | |
407 if top is None or bottom is None: | |
408 return True | |
409 if top[0] or bottom[0]: | |
410 # if not everything is visible, we can select | |
411 return True | |
412 for wid in self.body: | |
413 # if any widget is selectable, we can select | |
414 if wid.selectable(): | |
415 return True | |
416 return False | |
417 | |
418 def render(self, size, focus=False): | |
419 """Call ListBox render, but keep size and focus in cache""" | |
420 self.__size_cache = size | |
421 self.__focus_cache = focus | |
422 return super(UnselectableListBox, self).render(size, focus) | |
423 | |
424 | |
395 class GenericList(urwid.WidgetWrap): | 425 class GenericList(urwid.WidgetWrap): |
396 signals = ['click','change'] | 426 signals = ['click','change'] |
397 | 427 |
398 def __init__(self, options, style=None, align='left', option_type = SelectableText, on_click=None, on_change=None, user_data=None): | 428 def __init__(self, options, style=None, align='left', option_type = SelectableText, on_click=None, on_change=None, user_data=None): |
399 """ | 429 """ |
908 ok_arg = [kwargs['ok_value']] if kwargs.has_key('ok_value') else [] | 938 ok_arg = [kwargs['ok_value']] if kwargs.has_key('ok_value') else [] |
909 buttons = [urwid.Button(_("Ok"), kwargs['ok_cb'], *ok_arg)] | 939 buttons = [urwid.Button(_("Ok"), kwargs['ok_cb'], *ok_arg)] |
910 if buttons: | 940 if buttons: |
911 buttons_flow = urwid.GridFlow(buttons, max([len(button.get_label()) for button in buttons])+4, 1, 1, 'center') | 941 buttons_flow = urwid.GridFlow(buttons, max([len(button.get_label()) for button in buttons])+4, 1, 1, 'center') |
912 body_content = urwid.SimpleListWalker(widgets_lst) | 942 body_content = urwid.SimpleListWalker(widgets_lst) |
913 frame_body = urwid.ListBox(body_content) | 943 frame_body = UnselectableListBox(body_content) |
914 frame = FocusFrame(frame_body, frame_header, buttons_flow if buttons else None, 'footer' if buttons else 'body') | 944 frame = FocusFrame(frame_body, frame_header, buttons_flow if buttons else None, 'footer' if buttons else 'body') |
915 decorated_frame = urwid.LineBox(frame) | 945 decorated_frame = urwid.LineBox(frame) |
916 urwid.WidgetWrap.__init__(self, decorated_frame) | 946 urwid.WidgetWrap.__init__(self, decorated_frame) |
917 | 947 |
918 | 948 |
944 class Alert(GenericDialog): | 974 class Alert(GenericDialog): |
945 """Dialog with just a message and a OK button""" | 975 """Dialog with just a message and a OK button""" |
946 | 976 |
947 def __init__(self, title, message, style=['OK'], **kwargs): | 977 def __init__(self, title, message, style=['OK'], **kwargs): |
948 GenericDialog.__init__(self, [urwid.Text(message, 'center')], title, style, ok_value=None, **kwargs) | 978 GenericDialog.__init__(self, [urwid.Text(message, 'center')], title, style, ok_value=None, **kwargs) |
949 | |
950 | 979 |
951 ## CONTAINERS ## | 980 ## CONTAINERS ## |
952 | 981 |
953 class ColumnsRoller(urwid.Widget): | 982 class ColumnsRoller(urwid.Widget): |
954 _sizing = frozenset(['flow']) | 983 _sizing = frozenset(['flow']) |