Commit before breaking everything
[matches/honours.git] / research / transmission_spectroscopy / simulator / pgu-0.18 / build / lib / pgu / gui / menus.py
1 """
2 """
3 from .const import *
4 from . import table
5 from . 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 _valuefunc(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._valuefunc,{'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     Example:
87         data = [
88             ('File/Save', fnc_save, None),
89             ('File/New', fnc_new, None),
90             ('Edit/Copy', fnc_copy, None),
91             ('Edit/Cut', fnc_cut, None),
92             ('Help/About', fnc_help, help_about_content),
93             ('Help/Reference', fnc_help, help_reference_content),
94             ]
95         w = Menus(data)
96
97     """
98     
99     def __init__(self,data,menu_cls='menu',**params):
100         params.setdefault('cls','menus')
101         table.Table.__init__(self,**params)
102         
103         self.value = None
104         
105         n,m,mt = 0,None,None
106         for path,cmd,value in data:
107             parts = path.split("/")
108             if parts[0] != mt:
109                 mt = parts[0]
110                 m = _Menu(self,basic.Label(mt,cls=menu_cls+".label"),cls=menu_cls)
111                 self.add(m,n,0)
112                 n += 1
113             print ("add", parts[1], cmd, value)
114             m.add(basic.Label(parts[1],cls=m.cls+".option.label"),cmd,value)
115

UCC git Repository :: git.ucc.asn.au