1 # -*- coding: utf-8 -*- |
1 # -*- coding: utf-8 -*- |
2 |
2 |
3 from .widget import Widget |
3 from .widget import Widget |
|
4 import logging |
4 |
5 |
5 |
6 |
6 class MenuBar(Widget): |
7 class MenuBar(Widget): |
7 def __init__(self, items = []): |
8 def __init__(self, items = []): |
8 Widget.__init__(self, 0, 1) |
9 Widget.__init__(self, 0, 1) |
14 self.selected = None |
15 self.selected = None |
15 |
16 |
16 self.connect('draw', self.on_draw) |
17 self.connect('draw', self.on_draw) |
17 self.connect('keypress', self.on_keypress) |
18 self.connect('keypress', self.on_keypress) |
18 self.connect('mousedown', self.on_mousedown) |
19 self.connect('mousedown', self.on_mousedown) |
|
20 self.connect('mousemove', self.on_mousemove) |
|
21 self.connect('unfocus', self.on_unfocus) |
19 |
22 |
20 |
23 |
21 def setitems(self, items): |
24 def setitems(self, items): |
22 self.items = items |
25 self.items = items |
|
26 |
|
27 i = 0 |
|
28 for item in self.items: |
|
29 if isinstance(item[1], Widget): |
|
30 item[1].x = i |
|
31 item[1].y = self.y + 1 |
|
32 item[1].allowlayout = False |
|
33 item[1].hidden = True |
|
34 item[1].connect('focus', self.on_submenu_focus) |
|
35 i += len(item[0]) + 4 |
23 |
36 |
24 |
37 |
25 def on_draw(self, screen, x, y): |
38 def on_draw(self, screen, x, y): |
26 screen.pushcolor(self.bg) |
39 screen.pushcolor(self.bg) |
27 i = 0 |
40 i = 0 |
37 screen.puts(x + i, y, ' ' * (self.width - i)) |
50 screen.puts(x + i, y, ' ' * (self.width - i)) |
38 screen.popcolor() |
51 screen.popcolor() |
39 |
52 |
40 |
53 |
41 def on_keypress(self, keyname, char): |
54 def on_keypress(self, keyname, char): |
|
55 if keyname == 'left': |
|
56 self.move_selected(-1) |
|
57 elif keyname == 'right': |
|
58 self.move_selected(+1) |
|
59 else: |
|
60 if self.selected: |
|
61 if isinstance(self.selected[1], Widget): |
|
62 self.selected[1].emit('keypress', keyname, char) |
|
63 |
|
64 |
|
65 def move_selected(self, offset): |
|
66 if self.selected: |
|
67 i = self.items.index(self.selected) |
|
68 item = self.items[(i + offset) % len(self.items)] |
|
69 self.unselect() |
|
70 self.select(item) |
42 self.redraw() |
71 self.redraw() |
43 |
72 |
44 |
73 |
45 def on_mousedown(self, ev): |
74 def on_mousedown(self, ev): |
46 i = 0 |
75 i = 0 |
|
76 self.unselect() |
|
77 for item in self.items: |
|
78 w = len(item[0]) + 4 |
|
79 if ev.wx >= i and ev.wx < i + w: |
|
80 self.select(item) |
|
81 i += w |
|
82 |
|
83 |
|
84 def on_mousemove(self, ev): |
|
85 self.on_mousedown(ev) |
|
86 |
|
87 |
|
88 def on_unfocus(self, newfocus): |
|
89 #logging.getLogger('tuikit').debug('unfocus') |
|
90 if self.selected and newfocus == self.selected[1]: |
|
91 return |
|
92 self.unselect() |
|
93 |
|
94 |
|
95 def on_submenu_focus(self): |
|
96 self.setfocus() |
|
97 |
|
98 |
|
99 def select(self, item): |
|
100 self.selected = item |
|
101 if isinstance(item[1], Widget): |
|
102 item[1].hidden = False |
|
103 item[1].redraw() |
|
104 |
|
105 |
|
106 def unselect(self): |
47 if self.selected: |
107 if self.selected: |
48 if isinstance(self.selected[1], Widget): |
108 if isinstance(self.selected[1], Widget): |
49 self.selected[1].hidden = True |
109 self.selected[1].hidden = True |
50 self.selected = None |
110 self.selected = None |
51 for item in self.items: |
|
52 w = len(item[0]) + 4 |
|
53 if ev.wx >= i and ev.wx < i + w: |
|
54 self.selected = item |
|
55 if isinstance(item[1], Widget): |
|
56 item[1].x = i |
|
57 item[1].y = self.y + 1 |
|
58 item[1].hidden = False |
|
59 item[1].redraw() |
|
60 i += w |
|
61 |
111 |