comparison frontends/primitivus/custom_widgets.py @ 10:024b79b61a31

Primitivus: misc fixes + menubar first draft - Menu bar: first draft of class - Password widget fixed - change some str to unicode, notably for JID
author Goffi <goffi@goffi.org>
date Tue, 13 Jul 2010 02:24:59 +0800
parents 6650747dfdcb
children 983840425a55
comparison
equal deleted inserted replaced
9:6650747dfdcb 10:024b79b61a31
21 21
22 import urwid 22 import urwid
23 from urwid.escape import utf8decode 23 from urwid.escape import utf8decode
24 24
25 class Password(urwid.Edit): 25 class Password(urwid.Edit):
26 toto=0
26 """Edit box which doesn't show what is entered (show '*' or other char instead)""" 27 """Edit box which doesn't show what is entered (show '*' or other char instead)"""
27 28
28 def __init__(self, *args, **kwargs): 29 def __init__(self, *args, **kwargs):
29 """Same args than Edit.__init__ with an additional keyword arg 'hidden_char' 30 """Same args than Edit.__init__ with an additional keyword arg 'hidden_char'
30 @param hidden_char: char to show instead of what is actually entered: default '*' 31 @param hidden_char: char to show instead of what is actually entered: default '*'
38 hidden_txt = len(text)*'*' 39 hidden_txt = len(text)*'*'
39 super(Password, self).set_edit_text(hidden_txt) 40 super(Password, self).set_edit_text(hidden_txt)
40 41
41 def get_edit_text(self): 42 def get_edit_text(self):
42 return self.__real_text 43 return self.__real_text
44
45 def insert_text(self, text):
46 self._edit_text = self.__real_text
47 super(Password,self).insert_text(text)
48
49 def render(self, size, focus=False):
50 Password.toto+=1
51 if Password.toto==30:
52 import os,pdb
53 os.system('reset')
54 pdb.set_trace()
55 return super(Password, self).render(size, focus)
43 56
44 class AdvancedEdit(urwid.Edit): 57 class AdvancedEdit(urwid.Edit):
45 """Edit box with some custom improvments 58 """Edit box with some custom improvments
46 new chars: 59 new chars:
47 - C-a: like 'home' 60 - C-a: like 'home'
325 def displayWidget(self, size, focus): 338 def displayWidget(self, size, focus):
326 list_size = sum([wid.rows(size, focus) for wid in self.genericList.content]) 339 list_size = sum([wid.rows(size, focus) for wid in self.genericList.content])
327 height = min(list_size,self.max_height) or 1 340 height = min(list_size,self.max_height) or 1
328 return urwid.BoxAdapter(self.genericList, height) 341 return urwid.BoxAdapter(self.genericList, height)
329 342
343 ## MISC ##
344
345 class Menu(urwid.FlowWidget):
346
347 def __init__(self):
348 super(Menu, self).__init__()
349 self.menu_keys = ['test']
350 self.menu = {'test':[('top',None)]}
351 self.shortcuts = {} #keyboard shortcuts
352
353 def checkShortcuts(self, key):
354 for shortcut in self.shortcuts.keys():
355 if key == shortcut:
356 category, item, callback = self.shortcuts[shortcuts]
357 callback((category, item))
358 return key
359
360 def addMenu(self, category, item, callback, shortcut=None):
361 """Add a menu item, create the category if new
362 @param category: category of the menu (e.g. File/Edit)
363 @param item: menu item (e.g. new/close/about)
364 @callback: method to call when item is selected"""
365 if not category in self.menu.keys():
366 self.menu_keys.append(category)
367 self.menu[category] = []
368 self.menu[category].append[(item, callback)]
369 if shortcut:
370 assert(shortcut not in self.shortcuts.keys())
371 self.shortcuts[shortcut] = (category, item, callback)
372
373 def rows(self,size,focus=False):
374 return self.display_widget(size, focus).rows(size, focus)
375
376 def render(self, size, focus=False):
377 return self.display_widget(size, focus).render(size, focus)
378
379 def display_widget(self, size, focus):
380 render_txt = []
381 for menu in self.menu_keys:
382 render_txt.append('[ %s ] ' % menu)
383 return urwid.AttrMap(urwid.Text(render_txt), 'menubar')
384
385
330 ## DIALOGS ## 386 ## DIALOGS ##
331 387
332 class GenericDialog(urwid.WidgetWrap): 388 class GenericDialog(urwid.WidgetWrap):
333 389
334 def __init__(self, widgets_lst, title, style=[], **kwargs): 390 def __init__(self, widgets_lst, title, style=[], **kwargs):