comparison urwid_satext/sat_widgets.py @ 75:8ff563825080

FocusFrame is now based on Pile instead of Frame, header, body and footer attributes can be used to change part at any time
author Goffi <goffi@goffi.org>
date Fri, 21 Mar 2014 15:20:59 +0100
parents 0afff3c54b6e
children 6c2a1b349416
comparison
equal deleted inserted replaced
74:0afff3c54b6e 75:8ff563825080
1043 render.append((urwid.SolidCanvas(" "*cols_left, size[0], 1),False,False,cols_left)) 1043 render.append((urwid.SolidCanvas(" "*cols_left, size[0], 1),False,False,cols_left))
1044 1044
1045 return urwid.CanvasJoin(render) 1045 return urwid.CanvasJoin(render)
1046 1046
1047 1047
1048 class FocusFrame(urwid.Frame): 1048 class FocusFrame(urwid.Pile):
1049 """Frame which manage 'tab' key""" 1049 """Frame-like which manage 'tab' key"""
1050 _sizing = frozenset(['box'])
1051
1052 def __init__(self, body, header=None, footer=None, focus_part='body'):
1053 self._header = self._footer = None
1054 super(FocusFrame, self).__init__([body])
1055 self.header = header
1056 self._body = body
1057 self.footer = footer
1058 self.focus_position = focus_part
1059
1060 def _focus_part_recalc(self):
1061 self._FOCUS_PARTS=[]
1062 if self._header is not None:
1063 self._FOCUS_PARTS.append('header')
1064 self._FOCUS_PARTS.append('body')
1065 if self._footer is not None:
1066 self._FOCUS_PARTS.append('footer')
1067 assert(len(self._FOCUS_PARTS) == len(self.contents))
1068
1069 @property
1070 def header(self):
1071 return self._header
1072
1073 @header.setter
1074 def header(self, widget):
1075 content = (widget, ('pack', None))
1076 if widget is None:
1077 if self._header is not None:
1078 del self.contents[0]
1079 elif self._header is None:
1080 self.contents.insert(0, content)
1081 else:
1082 self.contents[0] = content
1083 self._header = widget
1084 self._focus_part_recalc()
1085
1086 @property
1087 def body(self):
1088 return self._body
1089
1090 @body.setter
1091 def body(self, widget):
1092 assert widget is not None
1093 idx = self._FOCUS_PARTS.index('body')
1094 self.contents[idx] = (widget, ("weight", 1))
1095
1096 @property
1097 def footer(self):
1098 return self._footer
1099
1100 @footer.setter
1101 def footer(self, widget):
1102 content = (widget, ('pack', None))
1103 if widget is None:
1104 if self._footer is not None:
1105 del self.contents[-1]
1106 elif self._footer is None:
1107 self.contents.append(content)
1108 else:
1109 self.contents[-1] = content
1110 self._footer = widget
1111 self._focus_part_recalc()
1112
1113 @property
1114 def focus_position_named(self):
1115 return self._FOCUS_PARTS[self.int_focus_position]
1116
1117 @property
1118 def focus_position(self):
1119 return super(FocusFrame, self).focus_position
1120
1121 @focus_position.setter
1122 def focus_position(self, position):
1123 if isinstance(position, basestring):
1124 try:
1125 position = self._FOCUS_PARTS.index(position)
1126 except IndexError:
1127 raise IndexError("This FocusFrame has no %s" % position)
1128 urwid.Pile.focus_position.__set__(self, position)
1050 1129
1051 def keypress(self, size, key): 1130 def keypress(self, size, key):
1052 ret = urwid.Frame.keypress(self, size, key) 1131 ret = super(FocusFrame, self).keypress(size, key)
1053 if not ret: 1132 if not ret:
1054 return 1133 return
1055 1134
1056 if key == 'tab': 1135 if key == 'tab':
1057 focus_list = ('header','body','footer') 1136 try:
1058 focus_idx = focus_list.index(self.focus_part) 1137 self.focus_position -= 1
1059 for i in range(2): 1138 except IndexError:
1060 focus_idx = (focus_idx + 1) % len(focus_list) 1139 self.focus_position = 2
1061 focus_name = focus_list[focus_idx]
1062 widget = getattr(self,'_'+focus_name)
1063 if widget!=None and widget.selectable():
1064 self.set_focus(focus_name)
1065 1140
1066 return ret 1141 return ret
1067 1142
1068 1143
1069 class TabsContainer(urwid.WidgetWrap): 1144 class TabsContainer(urwid.WidgetWrap):