affichage fenêtre au lieu de plein écran.
[minwii.git] / src / pgu / gui / menus.py
1 """
2 """
3 from const import *
4 import table
5 import basic, button
6
7 class _Menu_Options(table.Table):
8 def __init__(self,menu,**params):
9 table.Table.__init__(self,**params)
10
11 self.menu = menu
12
13 def event(self,e):
14 handled = False
15 arect = self.get_abs_rect()
16
17 if e.type == MOUSEMOTION:
18 abspos = e.pos[0]+arect.x,e.pos[1]+arect.y
19 for w in self.menu.container.widgets:
20 if not w is self.menu:
21 mrect = w.get_abs_rect()
22 if mrect.collidepoint(abspos):
23 self.menu._close(None)
24 w._open(None)
25 handled = True
26
27 if not handled: table.Table.event(self,e)
28
29 class _Menu(button.Button):
30 def __init__(self,parent,widget=None,**params): #TODO widget= could conflict with module widget
31 params.setdefault('cls','menu')
32 button.Button.__init__(self,widget,**params)
33
34 self.parent = parent
35
36 self._cls = self.cls
37 self.options = _Menu_Options(self, cls=self.cls+".options")
38
39 self.connect(CLICK,self._open,None)
40
41 self.pos = 0
42
43 def _open(self,value):
44 self.parent.value = self
45 self.pcls = 'down'
46
47 self.repaint()
48 self.container.open(self.options,self.rect.x,self.rect.bottom)
49 self.options.connect(BLUR,self._close,None)
50 self.options.focus()
51 self.repaint()
52
53 def _pass(self,value):
54 pass
55
56 def _close(self,value):
57 self.pcls = ''
58 self.parent.value = None
59 self.repaint()
60 self.options.close()
61
62 def _value(self,value):
63 self._close(None)
64 if value['fnc'] != None:
65 value['fnc'](value['value'])
66
67 def event(self,e):
68 button.Button.event(self,e)
69
70 if self.parent.value == self:
71 self.pcls = 'down'
72
73 def add(self,w,fnc=None,value=None):
74 w.style.align = -1
75 b = button.Button(w,cls=self.cls+".option")
76 b.connect(CLICK,self._value,{'fnc':fnc,'value':value})
77
78 self.options.tr()
79 self.options.add(b)
80
81 return b
82
83 class Menus(table.Table):
84 """A drop down menu bar.
85
86 <pre>Menus(data)</pre>
87
88 <dl>
89 <dt>data<dd>Menu data, a list of (path,fnc,value), see example below
90 </dl>
91
92 <strong>Example</strong>
93 <code>
94 data = [
95 ('File/Save',fnc_save,None),
96 ('File/New',fnc_new,None),
97 ('Edit/Copy',fnc_copy,None),
98 ('Edit/Cut',fnc_cut,None),
99 ('Help/About',fnc_help,help_about_content),
100 ('Help/Reference',fnc_help,help_reference_content),
101 ]
102 w = Menus(data)
103 """
104
105 def __init__(self,data,menu_cls='menu',**params):
106 params.setdefault('cls','menus')
107 table.Table.__init__(self,**params)
108
109 self.value = None
110
111 n,m,mt = 0,None,None
112 for path,cmd,value in data:
113 parts = path.split("/")
114 if parts[0] != mt:
115 mt = parts[0]
116 m = _Menu(self,basic.Label(mt,cls=menu_cls+".label"),cls=menu_cls)
117 self.add(m,n,0)
118 n += 1
119 m.add(basic.Label(parts[1],cls=m.cls+".option.label"),cmd,value)