comparison frontends/primitivus/custom_widgets.py @ 12:7e63429cc929

Primitivus: menu are now managed and fully working - new class MenuRoller for manager different menus - Chat window now create is own menu - some menu items added for general/contact menus
author Goffi <goffi@goffi.org>
date Fri, 16 Jul 2010 20:25:06 +0800
parents 983840425a55
children 8cccbaadb9c5
comparison
equal deleted inserted replaced
11:983840425a55 12:7e63429cc929
372 self.selected = wid.getValue() 372 self.selected = wid.getValue()
373 self._emit('click') 373 self._emit('click')
374 374
375 class Menu(urwid.FlowWidget): 375 class Menu(urwid.FlowWidget):
376 376
377 def __init__(self,loop): 377 def __init__(self,loop, x_orig=0):
378 """Menu widget
379 @param loop: main loop of urwid
380 @param x_orig: absolute start of the abscissa
381 """
378 super(Menu, self).__init__() 382 super(Menu, self).__init__()
379 self.loop = loop 383 self.loop = loop
380 self.menu_keys = [] 384 self.menu_keys = []
381 self.menu = {} 385 self.menu = {}
386 self.x_orig = x_orig
382 self.shortcuts = {} #keyboard shortcuts 387 self.shortcuts = {} #keyboard shortcuts
383 self.focus_menu = 0 388 self.focus_menu = 0
384 self.save_bottom = None 389 self.save_bottom = None
385 390
386 def selectable(self): 391 def selectable(self):
387 return True 392 return True
388 393
394 def setOrigX(self, orig_x):
395 self.x_orig = orig_x
396
389 def __buildOverlay(self,menu_key,columns): 397 def __buildOverlay(self,menu_key,columns):
390 """Build the overlay menu which show menuitems 398 """Build the overlay menu which show menuitems
391 @param menu_key: name of the category 399 @param menu_key: name of the category
392 @colums: column number where the menubox must be displayed""" 400 @colums: column number where the menubox must be displayed"""
393 max_len = 0 401 max_len = 0
406 self.focus_menu += 1 414 self.focus_menu += 1
407 self._invalidate() 415 self._invalidate()
408 elif key == 'left' and self.focus_menu > 0: 416 elif key == 'left' and self.focus_menu > 0:
409 self.focus_menu -= 1 417 self.focus_menu -= 1
410 self._invalidate() 418 self._invalidate()
419 return
411 elif key == 'down': 420 elif key == 'down':
412 if not self.save_bottom: 421 if self.menu_keys and not self.save_bottom:
413 column = sum([len(menu)+4 for menu in self.menu_keys[0:self.focus_menu]],self.focus_menu+1) 422 column = sum([len(menu)+4 for menu in self.menu_keys[0:self.focus_menu]],self.focus_menu+self.x_orig)
414 self.__buildOverlay(self.menu_keys[self.focus_menu],column) 423 self.__buildOverlay(self.menu_keys[self.focus_menu],column)
415 elif key == 'up': 424 elif key == 'up':
416 if self.save_bottom: 425 if self.save_bottom:
417 self.loop.widget = self.save_bottom 426 self.loop.widget = self.save_bottom
418 self.save_bottom = None 427 self.save_bottom = None
466 break 475 break
467 if callback: 476 if callback:
468 self.keypress(None,'up') 477 self.keypress(None,'up')
469 callback((category, item)) 478 callback((category, item))
470 479
480 class MenuRoller(urwid.WidgetWrap):
481
482 def __init__(self,menus_list):
483 """Create a MenuRoller
484 @param menus_list: list of tuple with (name, Menu_instance), name can be None
485 """
486 assert (menus_list)
487 self.selected = 0
488 self.name_list = []
489 self.menus = {}
490
491 self.columns = urwid.Columns([urwid.Text(''),urwid.Text('')])
492 urwid.WidgetWrap.__init__(self, self.columns)
493
494 for menu_tuple in menus_list:
495 name,menu = menu_tuple
496 self.addMenu(name, menu)
497
498 def __showSelected(self):
499 """show menu selected"""
500 name_txt = u'\u21c9 '+self.name_list[self.selected]+u' \u21c7 '
501 current_name = ClickableText(name_txt)
502 name_len = len(name_txt)
503 current_menu = self.menus[self.name_list[self.selected]]
504 current_menu.setOrigX(name_len)
505 self.columns.widget_list[0] = current_name
506 self.columns.column_types[0]=('fixed', name_len)
507 self.columns.widget_list[1] = current_menu
508
509 def keypress(self, size, key):
510 if key=='up':
511 if self.columns.get_focus_column()==0 and self.selected > 0:
512 self.selected -= 1
513 self.__showSelected()
514 elif key=='down':
515 if self.columns.get_focus_column()==0 and self.selected < len(self.name_list)-1:
516 self.selected += 1
517 self.__showSelected()
518
519 return super(MenuRoller, self).keypress(size, key)
520
521 def addMenu(self, name_param, menu):
522 name = name_param or ''
523 if name not in self.name_list:
524 self.name_list.append(name)
525 self.menus[name] = menu
526 if self.name_list[self.selected] == name:
527 self.__showSelected() #if we are on the menu, we update it
528
529 def checkShortcuts(self, key):
530 for menu in self.name_list:
531 key = self.menus[menu].checkShortcuts(key)
532 return key
533
471 534
472 ## DIALOGS ## 535 ## DIALOGS ##
473 536
474 class GenericDialog(urwid.WidgetWrap): 537 class GenericDialog(urwid.WidgetWrap):
475 538