7 from .button import Button
8 from .basic import Label, Image
9 from .table import Table
12 """A combo dropdown box widget.
15 w = Select(value="goats")
17 w.add("Goats","goats")
19 w.value = 'dogs' #changes the value from goats to dogs
23 # The drop-down arrow button for the selection widget
25 # A button displaying the currently selected item
27 # The first option added to the selector
29 # The PGU table of options
33 def __init__(self,value=None,**params):
34 params.setdefault('cls','select')
35 Table.__init__(self,**params)
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)
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)
44 self.options = Table(cls=self.cls+".options")
45 self.options.connect(BLUR,self._close,None)
46 self.options.name = "pulldown-table"
51 def resize(self,width=None,height=None):
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)
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
61 self.top_arrow.connect(CLICK,self._open,None)
62 self.top_selected.connect(CLICK,self._open,None)
64 w,h = Table.resize(self,width,height)
66 self.options.style.width = w
67 #HACK: sort of, but not a big one..
72 def _open(self,value):
75 opts.rect.w, opts.rect.h = opts.resize()
79 # while hasattr(c, 'container'):
81 # if (not c.container):
85 # if y + self.rect.h + opts.rect.h <= c.rect.h: #down
86 # dy = self.rect.y + self.rect.h
88 # dy = self.rect.y - self.rect.h
90 opts.rect.w, opts.rect.h = opts.resize()
92 # TODO - make sure there is enough space to open down
94 yp = self.rect.bottom-1
96 self.container.open(opts, self.rect.x, yp)
97 self.firstOption.focus()
99 # TODO - this is a hack
100 for opt in self.options.widgets:
103 def _close(self,value):
105 self.top_selected.focus()
107 def _setvalue(self,value):
108 self.value = value._value
111 #HACK: improper use of resize()
112 #self.resize() #to recenter the new value, etc.
117 #self.repaint() #this will happen anyways
125 def value(self, val):
127 for w in self.values:
136 mywidget = Label(" ",cls=self.cls+".option.label")
137 self.top_selected.value = mywidget
140 def add(self,w,value=None):
141 """Add a widget and associated value to the dropdown box."""
143 if type(w) == str: w = Label(w,cls=self.cls+".option.label")
146 btn = Button(w,cls=self.cls+".option")
147 btn.connect(CLICK,self._setvalue,w)
150 self.options.add(btn)
152 if (not self.firstOption):
153 self.firstOption = btn
155 if value != None: w._value = value
157 if self.value == w._value:
158 self.top_selected.value = w
159 self.values.append(w)