comparison urwid_satext/sat_widgets.py @ 95:ca9a77f3b53e

SelectableText is now unselectable if its text is empty, AlwaysSelectable class has the former behaviour.
author Goffi <goffi@goffi.org>
date Mon, 08 Sep 2014 15:42:25 +0200
parents 66b65ed9baf2
children 44fc94b0fe18
comparison
equal deleted inserted replaced
94:66b65ed9baf2 95:ca9a77f3b53e
201 middle = (maxcol-len(self.text))/2 201 middle = (maxcol-len(self.text))/2
202 render_text = middle * self.car + self.text + (maxcol - len(self.text) - middle) * self.car 202 render_text = middle * self.car + self.text + (maxcol - len(self.text) - middle) * self.car
203 return urwid.Text(render_text) 203 return urwid.Text(render_text)
204 204
205 205
206 class SelectableText(urwid.WidgetWrap): 206 class AlwaysSelectableText(urwid.WidgetWrap):
207 """Text which can be selected with space""" 207 """Text which can be selected with space"""
208 signals = ['change'] 208 signals = ['change']
209 209
210 def __init__(self, text, align='left', header='', focus_attr='default_focus', selected_text=None, selected=False, data=None): 210 def __init__(self, text, align='left', header='', focus_attr='default_focus', selected_text=None, selected=False, data=None):
211 """@param text: same as urwid.Text's text parameter 211 """
212 @param text: same as urwid.Text's text parameter
212 @param align: same as urwid.Text's align parameter 213 @param align: same as urwid.Text's align parameter
213 @select_attr: attrbute to use when selected 214 @select_attr: attrbute to use when selected
214 @param selected: is the text selected ?""" 215 @param selected: is the text selected ?
216 """
215 self.focus_attr = focus_attr 217 self.focus_attr = focus_attr
216 self.__selected = False 218 self.__selected = False
217 self.__was_focused = False 219 self.__was_focused = False
218 self.header = header 220 self.header = header
219 self.default_txt = text 221 self.text = text
220 urwid.WidgetWrap.__init__(self, urwid.Text("",align=align)) 222 urwid.WidgetWrap.__init__(self, urwid.Text("",align=align))
221 self.setSelectedText(selected_text) 223 self.setSelectedText(selected_text)
222 self.setState(selected) 224 self.setState(selected)
223 225
224 def getValue(self): 226 def getValue(self):
225 if isinstance(self.default_txt,basestring): 227 if isinstance(self.text,basestring):
226 return self.default_txt 228 return self.text
227 list_attr = self.default_txt if isinstance(self.default_txt, list) else [self.default_txt] 229 list_attr = self.text if isinstance(self.text, list) else [self.text]
228 txt = "" 230 txt = ""
229 for attr in list_attr: 231 for attr in list_attr:
230 if isinstance(attr,tuple): 232 if isinstance(attr,tuple):
231 txt+=attr[1] 233 txt+=attr[1]
232 else: 234 else:
237 """for compatibility with urwid.Text""" 239 """for compatibility with urwid.Text"""
238 return self.getValue() 240 return self.getValue()
239 241
240 def set_text(self, text): 242 def set_text(self, text):
241 """/!\ set_text doesn't change self.selected_txt !""" 243 """/!\ set_text doesn't change self.selected_txt !"""
242 self.default_txt = text 244 self.text = text
243 self.setState(self.__selected,invisible=True) 245 self.setState(self.__selected,invisible=True)
244 246
245 def setSelectedText(self, text=None): 247 def setSelectedText(self, text=None):
246 """Text to display when selected 248 """Text to display when selected
247 @text: text as in urwid.Text or None for default value""" 249 @text: text as in urwid.Text or None for default value"""
249 text = ('selected',self.getValue()) 251 text = ('selected',self.getValue())
250 self.selected_txt = text 252 self.selected_txt = text
251 if self.__selected: 253 if self.__selected:
252 self.setState(self.__selected) 254 self.setState(self.__selected)
253 255
254
255 def __set_txt(self): 256 def __set_txt(self):
256 txt_list = [self.header] 257 txt_list = [self.header]
257 txt = self.selected_txt if self.__selected else self.default_txt 258 txt = self.selected_txt if self.__selected else self.text
258 if isinstance(txt,list): 259 if isinstance(txt,list):
259 txt_list.extend(txt) 260 txt_list.extend(txt)
260 else: 261 else:
261 txt_list.append(txt) 262 txt_list.append(txt)
262 self._w.base_widget.set_text(txt_list) 263 self._w.base_widget.set_text(txt_list)
314 attr+="_focus" 315 attr+="_focus"
315 attr_list[idx] = (attr,attr_len) 316 attr_list[idx] = (attr,attr_len)
316 self._w.base_widget._invalidate() 317 self._w.base_widget._invalidate()
317 self.__was_focused = True #bloody ugly hack :) 318 self.__was_focused = True #bloody ugly hack :)
318 return self._w.render(size, focus) 319 return self._w.render(size, focus)
320
321
322 class SelectableText(AlwaysSelectableText):
323 """Like AlwaysSelectableText but not selectable when text is empty"""
324
325 def selectable(self):
326 return bool(self.text)
319 327
320 328
321 class ClickableText(SelectableText): 329 class ClickableText(SelectableText):
322 signals = SelectableText.signals + ['click'] 330 signals = SelectableText.signals + ['click']
323 331