Commit before breaking everything
[matches/honours.git] / research / transmission_spectroscopy / simulator / pgu-0.18 / build / lib / pgu / gui / select.py
1 """
2 """
3
4 import traceback
5
6 from .const import *
7 from .button import Button
8 from .basic import Label, Image
9 from .table import Table
10
11 class Select(Table):
12     """A combo dropdown box widget.
13     
14     Example:
15         w = Select(value="goats")
16         w.add("Cats","cats")
17         w.add("Goats","goats")
18         w.add("Dogs","Dogs")
19         w.value = 'dogs' #changes the value from goats to dogs
20     
21     """
22
23     # The drop-down arrow button for the selection widget
24     top_arrow = None
25     # A button displaying the currently selected item
26     top_selection = None
27     # The first option added to the selector
28     firstOption = None
29     # The PGU table of options
30     options = None
31     _value = None
32
33     def __init__(self,value=None,**params):
34         params.setdefault('cls','select')
35         Table.__init__(self,**params)
36         
37         label = Label(" ",cls=self.cls+".option.label")
38         self.top_selected = Button(label, cls=self.cls+".selected")
39         Table.add(self,self.top_selected) #,hexpand=1,vexpand=1)#,0,0)
40         
41         self.top_arrow = Button(Image(self.style.arrow), cls=self.cls+".arrow")
42         Table.add(self,self.top_arrow) #,hexpand=1,vexpand=1) #,1,0)
43         
44         self.options = Table(cls=self.cls+".options")
45         self.options.connect(BLUR,self._close,None)
46         self.options.name = "pulldown-table"
47         
48         self.values = []
49         self.value = value
50
51     def resize(self,width=None,height=None):
52         max_w,max_h = 0,0
53         for w in self.options.widgets:
54             w.rect.w,w.rect.h = w.resize()
55             max_w,max_h = max(max_w,w.rect.w),max(max_h,w.rect.h)
56         
57         #xt,xr,xb,xl = self.top_selected.getspacing()
58         self.top_selected.style.width = max_w #+ xl + xr
59         self.top_selected.style.height = max_h #+ xt + xb
60         
61         self.top_arrow.connect(CLICK,self._open,None)
62         self.top_selected.connect(CLICK,self._open,None)
63         
64         w,h = Table.resize(self,width,height)
65         
66         self.options.style.width = w
67         #HACK: sort of, but not a big one..
68         self.options.resize()
69         
70         return w,h
71         
72     def _open(self,value):
73         opts = self.options
74         
75         opts.rect.w, opts.rect.h = opts.resize()
76         
77 #        y = self.rect.y
78 #        c = self.container
79 #        while hasattr(c, 'container'):
80 #            y += c.rect.y
81 #            if (not c.container): 
82 #                break
83 #            c = c.container
84             
85 #        if y + self.rect.h + opts.rect.h <= c.rect.h: #down
86 #            dy = self.rect.y + self.rect.h
87 #        else: #up
88 #            dy = self.rect.y - self.rect.h
89
90         opts.rect.w, opts.rect.h = opts.resize()
91
92         # TODO - make sure there is enough space to open down
93         # ...
94         yp = self.rect.bottom-1
95
96         self.container.open(opts, self.rect.x, yp)
97         self.firstOption.focus()
98
99         # TODO - this is a hack
100         for opt in self.options.widgets:
101             opt.repaint()
102
103     def _close(self,value):
104         self.options.close()
105         self.top_selected.focus()
106     
107     def _setvalue(self,value):
108         self.value = value._value
109         if self.container:
110             #self.chsize()
111             #HACK: improper use of resize()
112             #self.resize() #to recenter the new value, etc.
113             pass
114         #    #self._resize()
115         
116         self._close(None)
117         #self.repaint() #this will happen anyways
118         
119     
120     @property
121     def value(self):
122         return self._value
123
124     @value.setter
125     def value(self, val):
126         mywidget = None
127         for w in self.values:
128             if w._value == val:
129                 mywidget = w
130         oldval = self._value
131         self._value = val
132         if (oldval != val):
133             self.send(CHANGE)
134             self.repaint()
135         if not mywidget:
136             mywidget = Label(" ",cls=self.cls+".option.label")
137         self.top_selected.value = mywidget
138         
139     
140     def add(self,w,value=None):
141         """Add a widget and associated value to the dropdown box."""
142         
143         if type(w) == str: w = Label(w,cls=self.cls+".option.label")
144         
145         w.style.align = -1
146         btn = Button(w,cls=self.cls+".option")
147         btn.connect(CLICK,self._setvalue,w)
148         
149         self.options.tr()
150         self.options.add(btn)
151         
152         if (not self.firstOption):
153             self.firstOption = btn
154         
155         if value != None: w._value = value
156         else: w._value = w
157         if self.value == w._value:
158             self.top_selected.value = w
159         self.values.append(w)
160

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