comparison frontends/primitivus/custom_widgets.py @ 6:d586d06a9d8f

primitivus group chat & misc primitivus: new widget: SurroundedText (text with a character repeated around it) primitivus: new decorator LabelLine (like lineBox, but with a label on the top line) wix & primitivus & quick_app: group chat method move to quick_chat wix: when new message, window is not raised anymore, but RequestUserAttention is called instead
author Goffi <goffi@goffi.org>
date Thu, 08 Jul 2010 14:12:18 +0800
parents 592cd64933dd
children 94868f58850b
comparison
equal deleted inserted replaced
5:592cd64933dd 6:d586d06a9d8f
18 You should have received a copy of the GNU General Public License 18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. 19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 """ 20 """
21 21
22 import urwid 22 import urwid
23 from urwid.escape import utf8decode
23 24
24 class Password(urwid.Edit): 25 class Password(urwid.Edit):
25 26
26 def __init__(self, *args, **kwargs): 27 def __init__(self, *args, **kwargs):
27 self.hidden_char=kwargs['hidden_char'] if kwargs.has_key('hidden_char') else '*' 28 self.hidden_char=kwargs['hidden_char'] if kwargs.has_key('hidden_char') else '*'
57 elif key == 'enter': 58 elif key == 'enter':
58 self._emit('click') 59 self._emit('click')
59 return super(AdvancedEdit, self).keypress(size, key) 60 return super(AdvancedEdit, self).keypress(size, key)
60 61
61 62
63 class SurroundedText(urwid.FlowWidget):
64
65 def __init__(self,text,car=utf8decode('─')):
66 self.text=text
67 self.car=car
68
69 def rows(self,size,focus=False):
70 return self.display_widget(size, focus).rows(size, focus)
71
72 def render(self, size, focus=False):
73 return self.display_widget(size, focus).render(size, focus)
74
75 def display_widget(self, size, focus):
76 (maxcol,) = size
77 middle = (maxcol-len(self.text))/2
78 render_text = middle * self.car + self.text + (maxcol - len(self.text) - middle) * self.car
79 return urwid.Text(render_text)
80
81
82
62 class SelectableText(urwid.FlowWidget): 83 class SelectableText(urwid.FlowWidget):
84 """Text which can be selected with space"""
63 signals = ['change'] 85 signals = ['change']
64 86
65 def __init__(self, text, align='left'): 87 def __init__(self, text, align='left'):
66 self.text=unicode(text) 88 self.text=unicode(text)
67 self.align = align 89 self.align = align
253 def rows(self, size, focus=False): 275 def rows(self, size, focus=False):
254 return self.displayWidget(size, focus).rows(size, focus) 276 return self.displayWidget(size, focus).rows(size, focus)
255 277
256 def displayWidget(self, size, focus): 278 def displayWidget(self, size, focus):
257 list_size = sum([wid.rows(size, focus) for wid in self.genericList.content]) 279 list_size = sum([wid.rows(size, focus) for wid in self.genericList.content])
258 height = min(list_size,self.max_height) 280 height = min(list_size,self.max_height) or 1
259 return urwid.BoxAdapter(self.genericList, height) 281 return urwid.BoxAdapter(self.genericList, height)
282
283 ## DIALOGS ##
260 284
261 class GenericDialog(urwid.WidgetWrap): 285 class GenericDialog(urwid.WidgetWrap):
262 286
263 def __init__(self, widgets_lst, title, style=[], **kwargs): 287 def __init__(self, widgets_lst, title, style=[], **kwargs):
264 frame_header = urwid.AttrMap(urwid.Text(title,'center'),'title') 288 frame_header = urwid.AttrMap(urwid.Text(title,'center'),'title')
284 308
285 309
286 310
287 class InputDialog(GenericDialog): 311 class InputDialog(GenericDialog):
288 312
289 def __init__(self, title, instrucions, style=['OK/CANCEL'], **kwargs): 313 def __init__(self, title, instrucions, style=['OK/CANCEL'], default_txt = '', **kwargs):
290 instr_wid = urwid.Text(instrucions+':') 314 instr_wid = urwid.Text(instrucions+':')
291 edit_box = urwid.Edit() 315 edit_box = urwid.Edit(edit_text=default_txt)
292 GenericDialog.__init__(self, [instr_wid,edit_box], title, style, ok_value=edit_box, **kwargs) 316 GenericDialog.__init__(self, [instr_wid,edit_box], title, style, ok_value=edit_box, **kwargs)
293 317
294 class ConfirmDialog(GenericDialog): 318 class ConfirmDialog(GenericDialog):
295 319
296 def __init__(self, title, style=['YES/NO'], **kwargs): 320 def __init__(self, title, style=['YES/NO'], **kwargs):
298 322
299 class Alert(GenericDialog): 323 class Alert(GenericDialog):
300 324
301 def __init__(self, title, message, style=['OK'], **kwargs): 325 def __init__(self, title, message, style=['OK'], **kwargs):
302 GenericDialog.__init__(self, [urwid.Text(message, 'center')], title, style, ok_value=None, **kwargs) 326 GenericDialog.__init__(self, [urwid.Text(message, 'center')], title, style, ok_value=None, **kwargs)
327
328 ## CONTAINERS ##
303 329
304 class FocusFrame(urwid.Frame): 330 class FocusFrame(urwid.Frame):
305 """Frame which manage "tab" key""" 331 """Frame which manage "tab" key"""
306 332
307 def keypress(self, size, key): 333 def keypress(self, size, key):
314 widget = getattr(self,'_'+focus_name) 340 widget = getattr(self,'_'+focus_name)
315 if widget!=None and widget.selectable(): 341 if widget!=None and widget.selectable():
316 self.set_focus(focus_name) 342 self.set_focus(focus_name)
317 343
318 return urwid.Frame.keypress(self, size, key) 344 return urwid.Frame.keypress(self, size, key)
345
346 ## DECORATORS ##
347 class LabelLine(urwid.LineBox):
348
349 def __init__(self, original_widget, label_widget):
350 urwid.LineBox.__init__(self, original_widget)
351 top_columns = self._w.widget_list[0]
352 top_columns.widget_list[1] = label_widget
353