# HG changeset patch # User Goffi # Date 1281352153 -28800 # Node ID f582fddd0cc03c5da94d7eb0d94b4bb4d4523a07 # Parent ae50b53ff868a267f00e5e019f5e4da8f8783e11 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space diff -r ae50b53ff868 -r f582fddd0cc0 frontends/primitivus/custom_widgets.py --- a/frontends/primitivus/custom_widgets.py Fri Aug 06 12:18:50 2010 +0800 +++ b/frontends/primitivus/custom_widgets.py Mon Aug 09 19:09:13 2010 +0800 @@ -692,6 +692,126 @@ ## CONTAINERS ## +class ColumnsRoller(urwid.FlowWidget): + + def __init__(self, widget_list = None, focus_column=0): + self.widget_list = widget_list or [] + self.focus_column = focus_column + self.__start = 0 + self.__next = False + + def addWidget(self, widget, width): + self.widget_list.append((width,widget)) + if len(self.widget_list) == 1: + self.set_focus(0) + + def selectable(self): + try: + return self.widget_list[self.focus_column][1].selectable() + except IndexError: + return False + + def keypress(self, size, key): + if key=='left': + if self.focus_column>0: + self.focus_column-=1 + self._invalidate() + return + if key=='right': + if self.focus_columnlen(self.widget_list)-1: + idx = len(self.widget_list)-1 + self.focus_column = idx + + def rows(self,size,focus=False): + return 1 + + def __calculate_limits(self, size): + (maxcol,) = size + _prev = _next = False + start_wid = 0 + end_wid = len(self.widget_list)-1 + + total_wid = sum([w[0] for w in self.widget_list]) + while total_wid > maxcol: + if self.focus_column == end_wid: + if not _prev: + total_wid+=1 + _prev = True + total_wid-=self.widget_list[start_wid][0] + start_wid+=1 + else: + if not _next: + total_wid+=1 + _next = True + total_wid-=self.widget_list[end_wid][0] + end_wid-=1 + + cols_left = maxcol - total_wid + + return _prev,_next,start_wid,end_wid,cols_left + + + def mouse_event(self, size, event, button, x, y, focus): + (maxcol,)=size + + if urwid.is_mouse_press(event) and button == 1: + _prev,_next,start_wid,end_wid,cols_left = self.__calculate_limits(size) + if x==0 and _prev: + self.keypress(size,'left') + return True + if x==maxcol-1 and _next: + self.keypress(size,'right') + return True + + current_pos = 1 if _prev else 0 + idx = 0 + while current_pos