7 from button
import Button
8 from basic
import Label
, Image
9 from table
import Table
14 <pre>Select(value=None)</pre>
17 <dt>value<dd>initial value
20 <strong>Example</strong>
22 w = Select(value="goats")
24 w.add("Goats","goats")
27 w.value = 'dogs' #changes the value from goats to dogs
32 # The drop-down arrow button for the selection widget
34 # A button displaying the currently selected item
36 # The first option added to the selector
38 # The PGU table of options
41 def __init__(self
,value
=None,**params
):
42 params
.setdefault('cls','select')
43 Table
.__init
__(self
,**params
)
45 label
= Label(" ",cls
=self
.cls
+".option.label")
46 self
.top_selected
= Button(label
, cls
=self
.cls
+".selected")
47 Table
.add(self
,self
.top_selected
) #,hexpand=1,vexpand=1)#,0,0)
49 self
.top_arrow
= Button(Image(self
.style
.arrow
), cls
=self
.cls
+".arrow")
50 Table
.add(self
,self
.top_arrow
) #,hexpand=1,vexpand=1) #,1,0)
52 self
.options
= Table(cls
=self
.cls
+".options")
53 self
.options
.connect(BLUR
,self
._close
,None)
54 self
.options
.name
= "pulldown-table"
59 def resize(self
,width
=None,height
=None):
61 for w
in self
.options
.widgets
:
62 w
.rect
.w
,w
.rect
.h
= w
.resize()
63 max_w
,max_h
= max(max_w
,w
.rect
.w
),max(max_h
,w
.rect
.h
)
65 #xt,xr,xb,xl = self.top_selected.getspacing()
66 self
.top_selected
.style
.width
= max_w
#+ xl + xr
67 self
.top_selected
.style
.height
= max_h
#+ xt + xb
69 self
.top_arrow
.connect(CLICK
,self
._open
,None)
70 self
.top_selected
.connect(CLICK
,self
._open
,None)
72 w
,h
= Table
.resize(self
,width
,height
)
74 self
.options
.style
.width
= w
75 #HACK: sort of, but not a big one..
80 def _open(self
,value
):
83 opts
.rect
.w
, opts
.rect
.h
= opts
.resize()
87 # while hasattr(c, 'container'):
89 # if (not c.container):
93 # if y + self.rect.h + opts.rect.h <= c.rect.h: #down
94 # dy = self.rect.y + self.rect.h
96 # dy = self.rect.y - self.rect.h
98 opts
.rect
.w
, opts
.rect
.h
= opts
.resize()
100 # TODO - make sure there is enough space to open down
102 yp
= self
.rect
.bottom
-1
104 self
.container
.open(opts
, self
.rect
.x
, yp
)
105 self
.firstOption
.focus()
107 # TODO - this is a hack
108 for opt
in self
.options
.widgets
:
111 def _close(self
,value
):
113 self
.top_selected
.focus()
115 def _setvalue(self
,value
):
116 self
.value
= value
._value
117 if hasattr(self
,'container'):
119 #HACK: improper use of resize()
120 #self.resize() #to recenter the new value, etc.
125 #self.repaint() #this will happen anyways
129 def __setattr__(self
,k
,v
):
132 for w
in self
.values
:
135 _v
= self
.__dict
__.get(k
,NOATTR
)
137 if k
== 'value' and _v
!= NOATTR
and _v
!= v
:
142 mywidget
= Label(" ",cls
=self
.cls
+".option.label")
143 self
.top_selected
.value
= mywidget
145 def add(self
,w
,value
=None):
146 """Add a widget, value item to the Select.
148 <pre>Select.add(widget,value=None)</pre>
151 <dt>widget<dd>Widget or string to represent the item
152 <dt>value<dd>value for this item
155 <strong>Example</strong>
158 w.add("Goat") #adds a Label
159 w.add("Goat","goat") #adds a Label with the value goat
160 w.add(gui.Label("Cuzco"),"goat") #adds a Label with value goat
164 if type(w
) == str: w
= Label(w
,cls
=self
.cls
+".option.label")
167 btn
= Button(w
,cls
=self
.cls
+".option")
168 btn
.connect(CLICK
,self
._setvalue
,w
)
171 self
.options
.add(btn
)
173 if (not self
.firstOption
):
174 self
.firstOption
= btn
176 if value
!= None: w
._value
= value
178 if self
.value
== w
._value
:
179 self
.top_selected
.value
= w
180 self
.values
.append(w
)