20
|
1 |
"
|
|
2 |
COPYRIGHT (c) 1994 by Claus Gittinger
|
59
|
3 |
All Rights Reserved
|
20
|
4 |
|
|
5 |
This software is furnished under a license and may be used
|
|
6 |
only in accordance with the terms of that license and with the
|
|
7 |
inclusion of the above copyright notice. This software may not
|
|
8 |
be provided or otherwise made available to, or used by, any
|
|
9 |
other person. No title to or ownership of the software is
|
|
10 |
hereby transferred.
|
|
11 |
"
|
|
12 |
|
|
13 |
Button subclass:#PopUpList
|
77
|
14 |
instanceVariableNames:'menu menuAction values'
|
59
|
15 |
classVariableNames:''
|
|
16 |
poolDictionaries:''
|
|
17 |
category:'Views-Interactors'
|
20
|
18 |
!
|
|
19 |
|
|
20 |
PopUpList comment:'
|
|
21 |
COPYRIGHT (c) 1994 by Claus Gittinger
|
59
|
22 |
All Rights Reserved
|
20
|
23 |
|
77
|
24 |
$Header: /cvs/stx/stx/libwidg/PopUpList.st,v 1.6 1995-02-06 00:53:02 claus Exp $
|
38
|
25 |
'!
|
|
26 |
|
|
27 |
!PopUpList class methodsFor:'documentation'!
|
|
28 |
|
|
29 |
copyright
|
|
30 |
"
|
|
31 |
COPYRIGHT (c) 1994 by Claus Gittinger
|
59
|
32 |
All Rights Reserved
|
38
|
33 |
|
|
34 |
This software is furnished under a license and may be used
|
|
35 |
only in accordance with the terms of that license and with the
|
|
36 |
inclusion of the above copyright notice. This software may not
|
|
37 |
be provided or otherwise made available to, or used by, any
|
|
38 |
other person. No title to or ownership of the software is
|
|
39 |
hereby transferred.
|
|
40 |
"
|
|
41 |
!
|
|
42 |
|
|
43 |
version
|
|
44 |
"
|
77
|
45 |
$Header: /cvs/stx/stx/libwidg/PopUpList.st,v 1.6 1995-02-06 00:53:02 claus Exp $
|
38
|
46 |
"
|
|
47 |
!
|
20
|
48 |
|
38
|
49 |
documentation
|
|
50 |
"
|
|
51 |
a PopUpList is basically a button with a popup menu.
|
|
52 |
The PopUpLists label is showing the current selection from the
|
|
53 |
list.
|
63
|
54 |
"
|
|
55 |
!
|
38
|
56 |
|
63
|
57 |
examples
|
|
58 |
"
|
38
|
59 |
example use:
|
|
60 |
|
|
61 |
|p|
|
|
62 |
p := PopUpList label:'healthy fruit'.
|
|
63 |
p list:#('apples' 'bananas' 'grape' 'lemon' 'margarithas').
|
|
64 |
p open
|
|
65 |
|
|
66 |
|
|
67 |
with an initial selection:
|
|
68 |
|
|
69 |
|p|
|
|
70 |
p := PopUpList label:'dummy'.
|
|
71 |
p list:#('apples' 'bananas' 'grape' 'lemon' 'margarithas').
|
|
72 |
p selection:'apples'.
|
|
73 |
p open
|
|
74 |
|
|
75 |
|
|
76 |
with separating lines:
|
|
77 |
|
|
78 |
|p|
|
|
79 |
p := PopUpList label:'dummy'.
|
|
80 |
p list:#('apples' 'bananas' 'grape' 'lemon' '-' 'margarithas').
|
|
81 |
p selection:'apples'.
|
|
82 |
p open
|
77
|
83 |
|
|
84 |
|
|
85 |
with an action:
|
|
86 |
|
|
87 |
|p|
|
|
88 |
p := PopUpList label:'dummy'.
|
|
89 |
p list:#('apples' 'bananas' 'grape' 'lemon' '-' 'margarithas').
|
|
90 |
p selection:'apples'.
|
|
91 |
p action:[:what | Transcript showCr:'you selected: ' , what].
|
|
92 |
p open
|
|
93 |
|
|
94 |
|
|
95 |
with values different from the label strings:
|
|
96 |
|
|
97 |
|p|
|
|
98 |
p := PopUpList label:'dummy'.
|
|
99 |
p list:#('apples' 'bananas' 'grape' 'lemon' '-' 'margarithas').
|
|
100 |
p selection:'apples'.
|
|
101 |
p values:#(10 20 30 40 nil 50).
|
|
102 |
p action:[:what | Transcript show:'you selected: '; showCr:what].
|
|
103 |
p open
|
38
|
104 |
"
|
|
105 |
! !
|
20
|
106 |
|
|
107 |
!PopUpList methodsFor:'drawing'!
|
|
108 |
|
|
109 |
drawWith:fgColor and:bgColor
|
|
110 |
|mmH mmV mW mH|
|
|
111 |
|
77
|
112 |
controller pressed ifTrue:[
|
59
|
113 |
super drawWith:enteredFgColor and:enteredBgColor
|
20
|
114 |
] ifFalse:[
|
59
|
115 |
super drawWith:fgColor and:bgColor.
|
20
|
116 |
].
|
77
|
117 |
mmH := device horizontalPixelPerMillimeter rounded.
|
|
118 |
mmV := device verticalPixelPerMillimeter rounded.
|
20
|
119 |
mW := (device horizontalPixelPerMillimeter * 2.5) rounded.
|
|
120 |
mH := (device verticalPixelPerMillimeter * 1.5) rounded.
|
|
121 |
|
|
122 |
self drawEdgesForX:(width - mW - (hSpace*2)) y:(height - mmV // 2)
|
59
|
123 |
width:mW height:mH level:2
|
20
|
124 |
! !
|
|
125 |
|
70
|
126 |
!PopUpList methodsFor:'event handling'!
|
20
|
127 |
|
77
|
128 |
popMenu
|
38
|
129 |
|org mv|
|
20
|
130 |
|
77
|
131 |
menu notNil ifTrue:[
|
|
132 |
self turnOff.
|
|
133 |
menu font:font.
|
|
134 |
|
|
135 |
"
|
|
136 |
adjust the menus width to my current width
|
|
137 |
"
|
|
138 |
mv := menu menuView.
|
|
139 |
mv width:(self width - (2 * menu margin) - (menu borderWidth*2)).
|
|
140 |
mv level:0; borderWidth:0.
|
|
141 |
mv fixSize.
|
|
142 |
"
|
|
143 |
the popupMenu wants Display coordinates in its showAt: method
|
|
144 |
"
|
|
145 |
org := device translatePoint:0@0
|
|
146 |
from:(self id)
|
|
147 |
to:(DisplayRootView new id).
|
|
148 |
|
|
149 |
menu showAt:org "resizing:false"
|
20
|
150 |
].
|
|
151 |
! !
|
|
152 |
|
|
153 |
!PopUpList methodsFor:'initialization'!
|
|
154 |
|
|
155 |
initialize
|
|
156 |
super initialize.
|
77
|
157 |
controller beTriggerOnDown.
|
|
158 |
controller action:[self popMenu].
|
20
|
159 |
self adjust:#left
|
|
160 |
! !
|
|
161 |
|
|
162 |
!PopUpList methodsFor:'accessing'!
|
|
163 |
|
77
|
164 |
model:aModel
|
|
165 |
"set the model - this is forwarded to my menu.
|
|
166 |
The popuplist itself has no model"
|
|
167 |
|
|
168 |
menu model:aModel
|
|
169 |
!
|
|
170 |
|
|
171 |
model
|
|
172 |
"return the model - this is forwarded to my menu.
|
|
173 |
The popuplist itself has no model"
|
|
174 |
|
|
175 |
^ menu model
|
|
176 |
!
|
|
177 |
|
|
178 |
change:aSymbol
|
|
179 |
"set the change symbol - this is forwarded to my menu.
|
|
180 |
The popuplist itself has no model"
|
|
181 |
|
|
182 |
menu change:aSymbol
|
|
183 |
!
|
|
184 |
|
|
185 |
changeSymbol
|
|
186 |
"return the change symbol - this is forwarded to my menu.
|
|
187 |
The popuplist itself has no model"
|
|
188 |
|
|
189 |
^ menu changeSymbol
|
|
190 |
!
|
|
191 |
|
38
|
192 |
action:aOneArgBlock
|
|
193 |
"set the action to be performed on selection changes;
|
|
194 |
the argument, aOneArgBlock will be evaluated with the
|
|
195 |
selection-value as argument"
|
|
196 |
|
|
197 |
menuAction := aOneArgBlock
|
20
|
198 |
!
|
|
199 |
|
|
200 |
list
|
38
|
201 |
"return the list - i.e. the values shown in the pop-up list"
|
|
202 |
|
20
|
203 |
^ menu labels
|
|
204 |
!
|
|
205 |
|
|
206 |
list:aList
|
38
|
207 |
"set the list - i.e. the values shown in the pop-up list"
|
|
208 |
|
20
|
209 |
menu := PopUpMenu
|
59
|
210 |
labels:aList
|
|
211 |
selectors:(Array new:(aList size) withAll:#select:)
|
|
212 |
args:aList
|
|
213 |
receiver:self
|
|
214 |
for:self.
|
38
|
215 |
realized ifTrue:[
|
59
|
216 |
self computeLabelSize
|
38
|
217 |
]
|
|
218 |
|
|
219 |
"
|
|
220 |
|p|
|
|
221 |
p := PopUpList label:'fruit ?'.
|
|
222 |
p list:#('apples' 'bananas' 'grape' 'lemon' 'margarithas').
|
|
223 |
p open
|
|
224 |
"
|
20
|
225 |
!
|
|
226 |
|
77
|
227 |
values:aList
|
|
228 |
"set a value list - these are reported via the action or changeSymbol instead of
|
|
229 |
the labe strings."
|
|
230 |
|
|
231 |
values := aList.
|
|
232 |
menu args:(1 to:aList size).
|
|
233 |
|
|
234 |
"
|
|
235 |
|p|
|
|
236 |
p := PopUpList label:'fruit ?'.
|
|
237 |
p list:#('apples' 'bananas' 'grape' 'lemon' 'margarithas').
|
|
238 |
p values:#(1 2 3 4 'mhmh - good').
|
|
239 |
p open
|
|
240 |
"
|
|
241 |
!
|
|
242 |
|
20
|
243 |
selection:indexOrString
|
38
|
244 |
"set (force) a selection - usually done to set
|
|
245 |
an initial selection"
|
|
246 |
|
20
|
247 |
|index|
|
|
248 |
|
|
249 |
index := menu labels indexOf:indexOrString.
|
|
250 |
index == 0 ifTrue:[
|
59
|
251 |
indexOrString isNumber ifTrue:[
|
|
252 |
index := indexOrString
|
|
253 |
] ifFalse:[
|
|
254 |
^ self
|
|
255 |
]
|
20
|
256 |
].
|
|
257 |
self label:(menu labels at:index)
|
38
|
258 |
|
|
259 |
"
|
|
260 |
|p|
|
|
261 |
p := PopUpList label:'what fruit ?'.
|
|
262 |
p list:#('apples' 'bananas' 'grape' 'lemon' 'margarithas').
|
|
263 |
p selection:'apples'.
|
|
264 |
p open
|
|
265 |
"
|
20
|
266 |
! !
|
|
267 |
|
|
268 |
!PopUpList methodsFor:'private'!
|
|
269 |
|
|
270 |
computeLabelSize
|
|
271 |
"compute the extent needed to hold the label plus the mark"
|
|
272 |
|
|
273 |
|mmH mmV savedLogo longest longestWidth|
|
|
274 |
|
|
275 |
menu isNil ifTrue:[
|
59
|
276 |
super computeLabelSize
|
20
|
277 |
] ifFalse:[
|
59
|
278 |
"hack: simulate logo change to longest menu entry"
|
20
|
279 |
|
59
|
280 |
font := font on:device.
|
|
281 |
longest := logo.
|
|
282 |
longestWidth := font widthOf:logo.
|
|
283 |
menu labels do:[:entry |
|
|
284 |
|this|
|
20
|
285 |
|
59
|
286 |
this := font widthOf:entry printString.
|
|
287 |
this > longestWidth ifTrue:[
|
|
288 |
longest := entry.
|
|
289 |
longestWidth := this
|
|
290 |
].
|
|
291 |
].
|
|
292 |
savedLogo := logo.
|
|
293 |
logo := longest.
|
|
294 |
super computeLabelSize.
|
|
295 |
logo := savedLogo.
|
38
|
296 |
"self halt. "
|
20
|
297 |
].
|
|
298 |
mmH := device horizontalPixelPerMillimeter.
|
|
299 |
mmV := device verticalPixelPerMillimeter.
|
|
300 |
labelWidth := labelWidth + hSpace + (mmH * 2.5) rounded + hSpace.
|
|
301 |
labelHeight := labelHeight max: (mmV * 2) rounded
|
|
302 |
! !
|
|
303 |
|
|
304 |
!PopUpList methodsFor:'user actions'!
|
|
305 |
|
|
306 |
select:anEntry
|
77
|
307 |
|value label|
|
|
308 |
|
38
|
309 |
"/ 'selected:' print. anEntry printNewline.
|
77
|
310 |
values isNil ifTrue:[
|
|
311 |
label := value := anEntry
|
|
312 |
] ifFalse:[
|
|
313 |
label := menu labels at:anEntry.
|
|
314 |
value := values at:anEntry
|
|
315 |
].
|
|
316 |
|
20
|
317 |
menuAction notNil ifTrue:[
|
77
|
318 |
menuAction value:value.
|
20
|
319 |
].
|
|
320 |
self sizeFixed:true.
|
77
|
321 |
self label:label printString.
|
38
|
322 |
(model notNil and:[changeSymbol notNil]) ifTrue:[
|
77
|
323 |
model perform:changeSymbol with:value
|
38
|
324 |
].
|
20
|
325 |
! !
|