diff options
Diffstat (limited to 'urwid/widget.py')
-rw-r--r-- | urwid/widget.py | 78 |
1 files changed, 56 insertions, 22 deletions
diff --git a/urwid/widget.py b/urwid/widget.py index 0cb54d1..d2b7a8b 100644 --- a/urwid/widget.py +++ b/urwid/widget.py @@ -200,7 +200,7 @@ class Widget(object): """ __metaclass__ = WidgetMeta _selectable = False - _sizing = set([]) + _sizing = frozenset([FLOW, BOX, FIXED]) _command_map = command_map # default to the single shared CommandMap def _invalidate(self): @@ -212,29 +212,40 @@ class Widget(object): argument. """ signals.emit_signal(self, name, self, *args) - + def selectable(self): """ Return True if this widget should take focus. Default implementation returns the value of self._selectable. """ return self._selectable - + def sizing(self): """ Return a set including one or more of 'box', 'flow' and 'fixed'. Default implementation returns the value of - self._sizing. + self._sizing, which for the Widget base class includes all + three. + + The sizing modes returned indicate the modes that may be + supported by this widget, but is not sufficient to know + that using that sizing mode will work. Subclasses should + make an effort to remove sizing modes they know will not + work given the state of the widget, but many do not yet + do this. + + If a sizing mode is missing from the set then the widget + should fail when used in that mode. """ return self._sizing def pack(self, size, focus=False): """ - Return a 'packed' (maxcol, maxrow) for this widget. Default + Return a 'packed' (maxcol, maxrow) for this widget. Default implementation (no packing defined) returns size, and calculates maxrow if not given. """ - if size == (): + if not size: if FIXED in self.sizing(): raise NotImplementedError('Fixed widgets must override' ' Widget.pack()') @@ -269,7 +280,7 @@ class Widget(object): words = [] if self.selectable(): words = ["selectable"] + words - if self.sizing(): + if self.sizing() and self.sizing() != frozenset([FLOW, BOX, FIXED]): sizing_modes = list(self.sizing()) sizing_modes.sort() words.append("/".join(sizing_modes)) @@ -277,15 +288,21 @@ class Widget(object): def _repr_attrs(self): return {} - + class FlowWidget(Widget): """ - base class of widgets that determine their rows from the number of + Deprecated. Inherit from Widget and add: + + _sizing = frozenset(['flow']) + + at the top of your class definition instead. + + Base class of widgets that determine their rows from the number of columns available. """ - _sizing = set([FLOW]) - + _sizing = frozenset([FLOW]) + def rows(self, size, focus=False): """ All flow widgets must implement this function. @@ -301,23 +318,30 @@ class FlowWidget(Widget): class BoxWidget(Widget): """ - base class of width and height constrained widgets such as + Deprecated. Inherit from Widget and add: + + _sizing = frozenset(['box']) + _selectable = True + + at the top of your class definition instead. + + Base class of width and height constrained widgets such as the top level widget attached to the display object """ _selectable = True - _sizing = set([BOX]) - + _sizing = frozenset([BOX]) + def render(self, size, focus=False): """ All widgets must implement this function. """ raise NotImplementedError() - + def fixed_size(size): """ raise ValueError if size != (). - + Used by FixedWidgets to test size parameter. """ if size != (): @@ -326,17 +350,23 @@ def fixed_size(size): class FixedWidget(Widget): """ - base class of widgets that know their width and height and + Deprecated. Inherit from Widget and add: + + _sizing = frozenset(['fixed']) + + at the top of your class definition instead. + + Base class of widgets that know their width and height and cannot be resized """ - _sizing = set([FIXED]) - + _sizing = frozenset([FIXED]) + def render(self, size, focus=False): """ All widgets must implement this function. """ raise NotImplementedError() - + def pack(self, size=None, focus=False): """ All fixed widgets must implement this function. @@ -344,10 +374,12 @@ class FixedWidget(Widget): raise NotImplementedError() -class Divider(FlowWidget): +class Divider(Widget): """ Horizontal divider widget """ + _sizing = frozenset([FLOW]) + ignore_focus = True def __init__(self,div_char=u" ",top=0,bottom=0): @@ -446,10 +478,12 @@ class SolidFill(BoxWidget): class TextError(Exception): pass -class Text(FlowWidget): +class Text(Widget): """ a horizontally resizeable text widget """ + _sizing = frozenset([FLOW]) + ignore_focus = True _repr_content_length_max = 140 |