tuikit/menubar.py
author Radek Brich <radek.brich@devl.cz>
Sat, 29 Dec 2012 12:16:06 +0100
changeset 41 37b7dfc3eae6
parent 40 5faa38c10b67
child 45 43b2279b06e1
permissions -rw-r--r--
Update Emitter: All event handlers now have exactly one argument: object inherited from Event class, which carries any data.

# -*- coding: utf-8 -*-

from tuikit.widget import Widget


class MenuBar(Widget):
    def __init__(self, items = []):
        Widget.__init__(self, 0, 1)

        self.allow_focus = True

        self.bg = 'menu'
        self.highlight = 'menu-active'

        self.setitems(items)
        self.selected = None

    def setitems(self, items):
        self.items = items

        i = 0
        for item in self.items:
            if isinstance(item[1], Widget):
                item[1].x = i
                item[1].y = self.y + 1
                item[1].allow_layout = False
                item[1].hidden = True
                item[1].connect('focus', self.on_submenu_focus)
                item[1].menubar = self
            i += len(item[0]) + 4

    def _handle_draw(self, ev):
        super()._handle_draw(ev)
        ev.driver.pushcolor(self.bg)
        i = 0
        for item in self.items:
            if self.selected == item:
                ev.driver.pushcolor(self.highlight)
                ev.driver.puts(ev.x + i, ev.y, '  ' + item[0] + '  ')
                ev.driver.popcolor()
            else:
                ev.driver.puts(ev.x + i, ev.y, '  ' + item[0] + '  ')
            i += len(item[0]) + 4
        if i < self.width:
            ev.driver.puts(ev.x + i, ev.y, ' ' * (self.width - i))
        ev.driver.popcolor()


    def _handle_keypress(self, ev):
        super()._handle_draw(ev)
        if ev.keyname == 'left':
            self.move_selected(-1)
        elif ev.keyname == 'right':
            self.move_selected(+1)
        else:
            if self.selected:
                if isinstance(self.selected[1], Widget):
                    self.selected[1].emit('keypress', ev.keyname, ev.char)


    def move_selected(self, offset):
        if self.selected:
            i = self.items.index(self.selected)
            item = self.items[(i + offset) % len(self.items)]
            self.unselect()
            self.select(item)
        self.redraw()

    def _handle_mousedown(self, ev):
        super()._handle_mousedown(ev)
        self._select_xy(ev.wx, ev.wy)

    def _handle_mousemove(self, ev):
        super()._handle_mousemove(ev)
        self._select_xy(ev.wx, ev.wy)

    def _select_xy(self, wx, wy):
        i = 0
        self.unselect()
        for item in self.items:
            w = len(item[0]) + 4
            if wx >= i and wx < i + w:
                self.select(item)
            i += w

    def _handle_unfocus(self, ev):
        super()._handle_unfocus(ev)
        if self.selected and ev.new == self.selected[1]:
            return
        self.unselect()

    def on_submenu_focus(self, ev):
        self.set_focus()

    def select(self, item):
        self.selected = item
        if isinstance(item[1], Widget):
            item[1].hidden = False
            item[1].redraw()

    def unselect(self):
        if self.selected:
            if isinstance(self.selected[1], Widget):
                self.selected[1].hidden = True
            self.selected = None