tuikit/menubar.py
author Radek Brich <radek.brich@devl.cz>
Tue, 15 Mar 2011 17:27:09 +0100
changeset 2 684cdc352562
parent 1 69318aba22bf
child 7 d4a291b31cbb
permissions -rw-r--r--
Menu, Window and other improvements.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     1
# -*- coding: utf-8 -*-
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     2
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     3
from .widget import Widget
1
69318aba22bf Menu development. New focus. Easier imports from tuikit package.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
     4
import logging
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     5
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     6
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     7
class MenuBar(Widget):
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     8
    def __init__(self, items = []):
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     9
        Widget.__init__(self, 0, 1)
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    10
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    11
        self.bg = 2
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    12
        self.highlight = 3
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    13
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    14
        self.setitems(items)
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    15
        self.selected = None
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    16
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    17
        self.connect('draw', self.on_draw)
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    18
        self.connect('keypress', self.on_keypress)
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    19
        self.connect('mousedown', self.on_mousedown)
1
69318aba22bf Menu development. New focus. Easier imports from tuikit package.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    20
        self.connect('mousemove', self.on_mousemove)
69318aba22bf Menu development. New focus. Easier imports from tuikit package.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    21
        self.connect('unfocus', self.on_unfocus)
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    22
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    23
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    24
    def setitems(self, items):
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    25
        self.items = items
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    26
1
69318aba22bf Menu development. New focus. Easier imports from tuikit package.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    27
        i = 0
69318aba22bf Menu development. New focus. Easier imports from tuikit package.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    28
        for item in self.items:
69318aba22bf Menu development. New focus. Easier imports from tuikit package.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    29
            if isinstance(item[1], Widget):
69318aba22bf Menu development. New focus. Easier imports from tuikit package.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    30
                item[1].x = i
69318aba22bf Menu development. New focus. Easier imports from tuikit package.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    31
                item[1].y = self.y + 1
69318aba22bf Menu development. New focus. Easier imports from tuikit package.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    32
                item[1].allowlayout = False
69318aba22bf Menu development. New focus. Easier imports from tuikit package.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    33
                item[1].hidden = True
69318aba22bf Menu development. New focus. Easier imports from tuikit package.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    34
                item[1].connect('focus', self.on_submenu_focus)
2
684cdc352562 Menu, Window and other improvements.
Radek Brich <radek.brich@devl.cz>
parents: 1
diff changeset
    35
                item[1].menubar = self
1
69318aba22bf Menu development. New focus. Easier imports from tuikit package.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    36
            i += len(item[0]) + 4
69318aba22bf Menu development. New focus. Easier imports from tuikit package.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    37
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    38
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    39
    def on_draw(self, screen, x, y):
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    40
        screen.pushcolor(self.bg)
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    41
        i = 0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    42
        for item in self.items:
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    43
            if self.selected == item:
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    44
                screen.pushcolor(self.highlight, screen.BOLD)
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    45
                screen.puts(x + i, y, '  ' + item[0] + '  ')
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    46
                screen.popcolor()
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    47
            else:
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    48
                screen.puts(x + i, y, '  ' + item[0] + '  ')
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    49
            i += len(item[0]) + 4
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    50
        if i < self.width:
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    51
            screen.puts(x + i, y, ' ' * (self.width - i))
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    52
        screen.popcolor()
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    53
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    54
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    55
    def on_keypress(self, keyname, char):
1
69318aba22bf Menu development. New focus. Easier imports from tuikit package.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    56
        if keyname == 'left':
69318aba22bf Menu development. New focus. Easier imports from tuikit package.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    57
            self.move_selected(-1)
69318aba22bf Menu development. New focus. Easier imports from tuikit package.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    58
        elif keyname == 'right':
69318aba22bf Menu development. New focus. Easier imports from tuikit package.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    59
            self.move_selected(+1)
69318aba22bf Menu development. New focus. Easier imports from tuikit package.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    60
        else:
69318aba22bf Menu development. New focus. Easier imports from tuikit package.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    61
            if self.selected:
69318aba22bf Menu development. New focus. Easier imports from tuikit package.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    62
                if isinstance(self.selected[1], Widget):
69318aba22bf Menu development. New focus. Easier imports from tuikit package.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    63
                    self.selected[1].emit('keypress', keyname, char)
69318aba22bf Menu development. New focus. Easier imports from tuikit package.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    64
69318aba22bf Menu development. New focus. Easier imports from tuikit package.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    65
69318aba22bf Menu development. New focus. Easier imports from tuikit package.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    66
    def move_selected(self, offset):
69318aba22bf Menu development. New focus. Easier imports from tuikit package.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    67
        if self.selected:
69318aba22bf Menu development. New focus. Easier imports from tuikit package.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    68
            i = self.items.index(self.selected)
69318aba22bf Menu development. New focus. Easier imports from tuikit package.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    69
            item = self.items[(i + offset) % len(self.items)]
69318aba22bf Menu development. New focus. Easier imports from tuikit package.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    70
            self.unselect()
69318aba22bf Menu development. New focus. Easier imports from tuikit package.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    71
            self.select(item)
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    72
        self.redraw()
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    73
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    74
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    75
    def on_mousedown(self, ev):
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    76
        i = 0
1
69318aba22bf Menu development. New focus. Easier imports from tuikit package.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    77
        self.unselect()
69318aba22bf Menu development. New focus. Easier imports from tuikit package.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    78
        for item in self.items:
69318aba22bf Menu development. New focus. Easier imports from tuikit package.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    79
            w = len(item[0]) + 4
69318aba22bf Menu development. New focus. Easier imports from tuikit package.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    80
            if ev.wx >= i and ev.wx < i + w:
69318aba22bf Menu development. New focus. Easier imports from tuikit package.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    81
                self.select(item)
69318aba22bf Menu development. New focus. Easier imports from tuikit package.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    82
            i += w
69318aba22bf Menu development. New focus. Easier imports from tuikit package.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    83
69318aba22bf Menu development. New focus. Easier imports from tuikit package.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    84
69318aba22bf Menu development. New focus. Easier imports from tuikit package.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    85
    def on_mousemove(self, ev):
69318aba22bf Menu development. New focus. Easier imports from tuikit package.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    86
        self.on_mousedown(ev)
69318aba22bf Menu development. New focus. Easier imports from tuikit package.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    87
69318aba22bf Menu development. New focus. Easier imports from tuikit package.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    88
69318aba22bf Menu development. New focus. Easier imports from tuikit package.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    89
    def on_unfocus(self, newfocus):
69318aba22bf Menu development. New focus. Easier imports from tuikit package.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    90
        #logging.getLogger('tuikit').debug('unfocus')
69318aba22bf Menu development. New focus. Easier imports from tuikit package.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    91
        if self.selected and newfocus == self.selected[1]:
69318aba22bf Menu development. New focus. Easier imports from tuikit package.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    92
            return
69318aba22bf Menu development. New focus. Easier imports from tuikit package.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    93
        self.unselect()
69318aba22bf Menu development. New focus. Easier imports from tuikit package.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    94
69318aba22bf Menu development. New focus. Easier imports from tuikit package.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    95
69318aba22bf Menu development. New focus. Easier imports from tuikit package.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    96
    def on_submenu_focus(self):
69318aba22bf Menu development. New focus. Easier imports from tuikit package.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    97
        self.setfocus()
69318aba22bf Menu development. New focus. Easier imports from tuikit package.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    98
69318aba22bf Menu development. New focus. Easier imports from tuikit package.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    99
69318aba22bf Menu development. New focus. Easier imports from tuikit package.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   100
    def select(self, item):
69318aba22bf Menu development. New focus. Easier imports from tuikit package.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   101
        self.selected = item
69318aba22bf Menu development. New focus. Easier imports from tuikit package.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   102
        if isinstance(item[1], Widget):
69318aba22bf Menu development. New focus. Easier imports from tuikit package.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   103
            item[1].hidden = False
69318aba22bf Menu development. New focus. Easier imports from tuikit package.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   104
            item[1].redraw()
69318aba22bf Menu development. New focus. Easier imports from tuikit package.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   105
69318aba22bf Menu development. New focus. Easier imports from tuikit package.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   106
69318aba22bf Menu development. New focus. Easier imports from tuikit package.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   107
    def unselect(self):
0
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   108
        if self.selected:
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   109
            if isinstance(self.selected[1], Widget):
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   110
                self.selected[1].hidden = True
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   111
            self.selected = None
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   112