comparison frontends/primitivus/custom_widgets.py @ 11:983840425a55

Primitivus: menu is now working /!\ tooltips not managed yet, doesn't manage properly menu when it's displayed on several rows /!\ still need to manage other menus (chat menu)
author Goffi <goffi@goffi.org>
date Wed, 14 Jul 2010 19:36:40 +0800
parents 024b79b61a31
children 7e63429cc929
comparison
equal deleted inserted replaced
10:024b79b61a31 11:983840425a55
340 height = min(list_size,self.max_height) or 1 340 height = min(list_size,self.max_height) or 1
341 return urwid.BoxAdapter(self.genericList, height) 341 return urwid.BoxAdapter(self.genericList, height)
342 342
343 ## MISC ## 343 ## MISC ##
344 344
345 class MenuBox(urwid.WidgetWrap):
346 signals = ['click']
347
348 def __init__(self,parent,items):
349 self.parent = parent
350 self.selected = None
351 content = urwid.SimpleListWalker([ClickableText(text,default_attr='menuitem') for text in items])
352 for wid in content:
353 urwid.connect_signal(wid, 'click', self.onClick)
354
355 self.listBox = urwid.ListBox(content)
356 menubox = urwid.LineBox(urwid.BoxAdapter(self.listBox,len(items)))
357 urwid.WidgetWrap.__init__(self,menubox)
358
359 def getValue(self):
360 return self.selected
361
362 def keypress(self, size, key):
363 if key=='up':
364 if self.listBox.get_focus()[1] == 0:
365 self.parent.keypress(size, key)
366 elif key=='left' or key=='right':
367 self.parent.keypress(size,'up')
368 self.parent.keypress(size,key)
369 return super(MenuBox,self).keypress(size,key)
370
371 def onClick(self, wid):
372 self.selected = wid.getValue()
373 self._emit('click')
374
345 class Menu(urwid.FlowWidget): 375 class Menu(urwid.FlowWidget):
346 376
347 def __init__(self): 377 def __init__(self,loop):
348 super(Menu, self).__init__() 378 super(Menu, self).__init__()
349 self.menu_keys = ['test'] 379 self.loop = loop
350 self.menu = {'test':[('top',None)]} 380 self.menu_keys = []
381 self.menu = {}
351 self.shortcuts = {} #keyboard shortcuts 382 self.shortcuts = {} #keyboard shortcuts
383 self.focus_menu = 0
384 self.save_bottom = None
385
386 def selectable(self):
387 return True
388
389 def __buildOverlay(self,menu_key,columns):
390 """Build the overlay menu which show menuitems
391 @param menu_key: name of the category
392 @colums: column number where the menubox must be displayed"""
393 max_len = 0
394 for item in self.menu[menu_key]:
395 if len(item[0]) > max_len:
396 max_len = len(item[0])
397
398 self.save_bottom = self.loop.widget
399 menu_box = MenuBox(self,[item[0] for item in self.menu[menu_key]])
400 urwid.connect_signal(menu_box, 'click', self.onClick)
401
402 self.loop.widget = urwid.Overlay(urwid.AttrMap(menu_box,'menubar'),self.save_bottom,('fixed left', columns),max_len+2,('fixed top',1),None)
403
404 def keypress(self, size, key):
405 if key == 'right' and self.focus_menu < len(self.menu)-1:
406 self.focus_menu += 1
407 self._invalidate()
408 elif key == 'left' and self.focus_menu > 0:
409 self.focus_menu -= 1
410 self._invalidate()
411 elif key == 'down':
412 if not self.save_bottom:
413 column = sum([len(menu)+4 for menu in self.menu_keys[0:self.focus_menu]],self.focus_menu+1)
414 self.__buildOverlay(self.menu_keys[self.focus_menu],column)
415 elif key == 'up':
416 if self.save_bottom:
417 self.loop.widget = self.save_bottom
418 self.save_bottom = None
419
420 return key
352 421
353 def checkShortcuts(self, key): 422 def checkShortcuts(self, key):
354 for shortcut in self.shortcuts.keys(): 423 for shortcut in self.shortcuts.keys():
355 if key == shortcut: 424 if key == shortcut:
356 category, item, callback = self.shortcuts[shortcuts] 425 category, item, callback = self.shortcuts[shortcut]
357 callback((category, item)) 426 callback((category, item))
358 return key 427 return key
359 428
360 def addMenu(self, category, item, callback, shortcut=None): 429 def addMenu(self, category, item, callback, shortcut=None):
361 """Add a menu item, create the category if new 430 """Add a menu item, create the category if new
363 @param item: menu item (e.g. new/close/about) 432 @param item: menu item (e.g. new/close/about)
364 @callback: method to call when item is selected""" 433 @callback: method to call when item is selected"""
365 if not category in self.menu.keys(): 434 if not category in self.menu.keys():
366 self.menu_keys.append(category) 435 self.menu_keys.append(category)
367 self.menu[category] = [] 436 self.menu[category] = []
368 self.menu[category].append[(item, callback)] 437 self.menu[category].append((item, callback))
369 if shortcut: 438 if shortcut:
370 assert(shortcut not in self.shortcuts.keys()) 439 assert(shortcut not in self.shortcuts.keys())
371 self.shortcuts[shortcut] = (category, item, callback) 440 self.shortcuts[shortcut] = (category, item, callback)
372 441
373 def rows(self,size,focus=False): 442 def rows(self,size,focus=False):
376 def render(self, size, focus=False): 445 def render(self, size, focus=False):
377 return self.display_widget(size, focus).render(size, focus) 446 return self.display_widget(size, focus).render(size, focus)
378 447
379 def display_widget(self, size, focus): 448 def display_widget(self, size, focus):
380 render_txt = [] 449 render_txt = []
450 idx = 0
381 for menu in self.menu_keys: 451 for menu in self.menu_keys:
382 render_txt.append('[ %s ] ' % menu) 452 if focus and idx == self.focus_menu:
453 render_txt.append(('selected_menu', '[ %s ]' % menu))
454 render_txt.append(' ')
455 else:
456 render_txt.append('[ %s ] ' % menu)
457 idx += 1
383 return urwid.AttrMap(urwid.Text(render_txt), 'menubar') 458 return urwid.AttrMap(urwid.Text(render_txt), 'menubar')
459
460 def onClick(self, widget):
461 category = self.menu_keys[self.focus_menu]
462 item = widget.getValue()
463 for menu_item in self.menu[category]:
464 if item == menu_item[0]:
465 callback = menu_item[1]
466 break
467 if callback:
468 self.keypress(None,'up')
469 callback((category, item))
384 470
385 471
386 ## DIALOGS ## 472 ## DIALOGS ##
387 473
388 class GenericDialog(urwid.WidgetWrap): 474 class GenericDialog(urwid.WidgetWrap):