Development: urwid-class_hierarchy.patch

File urwid-class_hierarchy.patch, 7.1 kB (added by anonymous, 2 years ago)
  • urwid/widget.py

    old new  
    2727try: sum # old python? 
    2828except: sum = lambda l: reduce(lambda a,b: a+b, l, 0) 
    2929 
    30 class FlowWidget(object): 
     30class autosuper(type): 
     31        """adding .__super""" 
     32        def __init__(cls, name, bases, dict): 
     33                super(autosuper, cls).__init__(name, bases, dict) 
     34                if hasattr(cls, "_%s__super" % name): 
     35                        raise AttributeError, "Class has same name as one of its super classes" 
     36                setattr(cls, "_%s__super" % name, super(cls)) 
     37 
     38class Widget(object): 
     39 
     40        __metaclass__ = autosuper 
     41        _selectable = False 
     42 
     43        def __init__(self): 
     44                self.__callbacks = {} 
     45 
     46        def register_callback(self, name, callback, user_data=None, pos =-1): 
     47                # XXX check name 
     48                self.__callbacks.setdefault(name, []).insert( 
     49                        pos, (callback, user_data)) 
     50 
     51        def deregister_callback(self, name, callback): 
     52                l = self.__callbacks.get(name, []) 
     53                for nr, (cb, user_data) in enumerate(l): 
     54                        if cb == callback: 
     55                                del l[nr] 
     56                                return 
     57                raise ValueError, "Callback not registered" 
     58 
     59        def _callback(self, name, *args): 
     60                for callback, user_data in self.__callbacks.get(name, []): 
     61                        if user_data is not None: 
     62                                args.append(user_data) 
     63                        if callback(*args): 
     64                                return True 
     65                return False 
     66 
     67        def selectable(self): 
     68                return self._selectable 
     69 
     70class FlowWidget(Widget): 
    3171        """ 
    3272        base class of widgets 
    3373        """ 
    34         def selectable(self): 
    35                 """Return False.  Not selectable by default.""" 
    36                 return False 
    37          
    3874        def rows(self, (maxcol,), focus=False): 
    3975                """ 
    4076                All flow widgets must implement this function. 
     
    4884                raise NotImplementedError() 
    4985 
    5086 
    51 class BoxWidget(object): 
     87class BoxWidget(Widget): 
    5288        """ 
    5389        base class of width and height constrained widgets such as 
    5490        the top level widget attached to the display object 
    5591        """ 
    56         def selectable(self): 
    57                 """Return True.  Selectable by default.""" 
    58                 return True 
    59          
     92        _selectable = True 
     93 
    6094        def render(self, size, focus=False): 
    6195                """ 
    6296                All widgets must implement this function. 
     
    74108                raise ValueError("FixedWidget takes only () for size." \ 
    75109                        "passed: %s" % `size`) 
    76110 
    77 class FixedWidget(object): 
    78         def selectable(self): 
    79                 """Return False.  Not selectable by default.""" 
    80                 return False 
    81  
     111class FixedWidget(Widget): 
    82112        def render(self, size, focus=False): 
    83113                """ 
    84114                All widgets must implement this function. 
     
    101131                top -- number of blank lines above 
    102132                bottom -- number of blank lines below 
    103133                """ 
     134                self.__super.__init__() 
    104135                self.div_char = div_char 
    105136                self.top = top 
    106137                self.bottom = bottom 
     
    133164                """ 
    134165                fill_char -- character to fill area with 
    135166                """ 
     167                self.__super.__init__() 
    136168                self.fill_char = fill_char 
    137169         
    138170        def render(self,(maxcol,maxrow), focus=False ): 
     
    167199                wrap -- wrap mode for text layout 
    168200                layout -- layout object to use, defaults to StandardTextLayout 
    169201                """ 
     202                self.__super.__init__() 
    170203                self._cache_maxcol = None 
    171204                self.set_text(markup) 
    172205                self.set_layout(align, wrap, layout) 
     
    329362                layout -- layout object 
    330363                """ 
    331364                 
    332                 Text.__init__(self,"", align, wrap, layout) 
     365                self.__super.__init__("", align, wrap, layout) 
    333366                assert type(edit_text)==type("") or type(edit_text)==type(u"") 
    334367                self.multiline = multiline 
    335368                self.allow_tab = allow_tab 
     
    588621                """ 
    589622                if default is not None: val = str(default) 
    590623                else: val = "" 
    591                 Edit.__init__(self,caption,val) 
     624                self.__super.__init__(caption,val) 
    592625 
    593626        def keypress(self,(maxcol,),key): 
    594627                """Handle editing keystrokes.  Return others.""" 
     
    649682                user_data -- additional param for on_press callback, 
    650683                             ommited if None for compatibility reasons 
    651684                """ 
     685                self.__super.__init__() 
    652686                self.label = Text("") 
    653687                self.has_mixed = has_mixed 
    654688                self.state = None 
     
    751785                This function will append the new radio button to group. 
    752786                "first True" will set to True if group is empty. 
    753787                """ 
     788                self.__super.__init__() 
    754789 
    755790                if state=="first True": 
    756791                        state = not group 
     
    850885                user_data -- additional param for on_press callback, 
    851886                           ommited if None for compatibility reasons 
    852887                """ 
     888                self.__super.__init__() 
    853889                         
    854890                self.set_label( label ) 
    855891                self.on_press = on_press 
     
    920956                align -- horizontal alignment of cells, see "align" parameter 
    921957                         of Padding widget for available options 
    922958                """ 
     959                self.__super.__init__() 
    923960                self.cells = cells 
    924961                self.cell_width = cell_width 
    925962                self.h_sep = h_sep 
     
    10971134class PaddingError(Exception): 
    10981135        pass 
    10991136 
    1100 class Padding(object): 
     1137class Padding(FlowWidget, BoxWidget): 
    11011138        def __init__(self, w, align, width, min_width=None): 
    11021139                """ 
    11031140                w -- a box, flow or fixed widget to pad on the left and/or right 
     
    11241161                be clipped to fit within the space given.  For example, 
    11251162                if align is 'left' then w may be clipped on the right. 
    11261163                """ 
     1164                self.__super.__init__() 
    11271165 
    11281166                at,aa,wt,wa=decompose_align_width(align, width, PaddingError) 
    11291167                 
     
    12671305                reducing the valign amount when necessary.  If height still  
    12681306                cannot be satisfied it will also be reduced. 
    12691307                """ 
     1308                self.__super.__init__() 
    12701309                vt,va,ht,ha=decompose_valign_height(valign,height,FillerError) 
    12711310                 
    12721311                self.body = body 
     
    14281467                when determining the size and position of top_w.  bottom_w is 
    14291468                always rendered the full size available "below" top_w. 
    14301469                """ 
     1470                self.__super.__init__() 
    14311471 
    14321472                at,aa,wt,wa=decompose_align_width(align, width, OverlayError) 
    14331473                vt,va,ht,ha=decompose_valign_height(valign,height,OverlayError) 
     
    17091749                footer -- a flow widget for below the body (or None) 
    17101750                focus_part -- 'header', 'footer' or 'body' 
    17111751                """ 
     1752                self.__super.__init__() 
    17121753                self.header = header 
    17131754                self.body = body 
    17141755                self.footer = footer 
     
    19271968class PileError(Exception): 
    19281969        pass 
    19291970                 
    1930 class Pile(object): # either FlowWidget or BoxWidget 
     1971class Pile(FlowWidget, BoxWidget): # either FlowWidget or BoxWidget 
    19311972        def __init__(self, widget_list, focus_item=0): 
    19321973                """ 
    19331974                widget_list -- list of widgets 
     
    19461987                If the pile is treated as a box widget there must be at least 
    19471988                one 'weight' tuple in widget_list. 
    19481989                """ 
     1990                self.__super.__init__() 
    19491991                self.widget_list = widget_list 
    19501992                self.item_types = [] 
    19511993                for i in range(len(widget_list)): 
     
    22582300        pass 
    22592301 
    22602302                 
    2261 class Columns(object): # either FlowWidget or BoxWidget 
     2303class Columns(FlowWidget, BoxWidget): # either FlowWidget or BoxWidget 
    22622304        def __init__(self, widget_list, dividechars=0, focus_column=0, 
    22632305                min_width=1, box_columns=None): 
    22642306                """ 
     
    22802322                box widget because in that case all columns are treated as box 
    22812323                widgets. 
    22822324                """ 
     2325                self.__super.__init__() 
    22832326                self.widget_list = widget_list 
    22842327                self.column_types = [] 
    22852328                for i in range(len(widget_list)): 
     
    25842627 
    25852628 
    25862629 
    2587 class BoxAdapter(object): 
     2630class BoxAdapter(FlowWidget): 
    25882631        """ 
    25892632        Adapter for using a box widget where a flow widget would usually go 
    25902633        """ 
     
    25962639                box_widget -- box widget 
    25972640                height -- number of rows for box widget 
    25982641                """ 
     2642                self.__super.__init__() 
    25992643                 
    26002644                self.height = height 
    26012645                self.box_widget = box_widget