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 |
|
121
|
13 |
'From Smalltalk/X, Version:2.10.5 on 4-may-1995 at 6:16:25 am'!
|
105
|
14 |
|
20
|
15 |
Button subclass:#PopUpList
|
119
|
16 |
instanceVariableNames:'menu menuAction values useIndex listMsg'
|
59
|
17 |
classVariableNames:''
|
|
18 |
poolDictionaries:''
|
|
19 |
category:'Views-Interactors'
|
20
|
20 |
!
|
|
21 |
|
|
22 |
PopUpList comment:'
|
|
23 |
COPYRIGHT (c) 1994 by Claus Gittinger
|
59
|
24 |
All Rights Reserved
|
20
|
25 |
|
121
|
26 |
$Header: /cvs/stx/stx/libwidg/PopUpList.st,v 1.11 1995-05-06 14:17:44 claus Exp $
|
38
|
27 |
'!
|
|
28 |
|
|
29 |
!PopUpList class methodsFor:'documentation'!
|
|
30 |
|
|
31 |
copyright
|
|
32 |
"
|
|
33 |
COPYRIGHT (c) 1994 by Claus Gittinger
|
59
|
34 |
All Rights Reserved
|
38
|
35 |
|
|
36 |
This software is furnished under a license and may be used
|
|
37 |
only in accordance with the terms of that license and with the
|
|
38 |
inclusion of the above copyright notice. This software may not
|
|
39 |
be provided or otherwise made available to, or used by, any
|
|
40 |
other person. No title to or ownership of the software is
|
|
41 |
hereby transferred.
|
|
42 |
"
|
|
43 |
!
|
|
44 |
|
|
45 |
version
|
|
46 |
"
|
121
|
47 |
$Header: /cvs/stx/stx/libwidg/PopUpList.st,v 1.11 1995-05-06 14:17:44 claus Exp $
|
38
|
48 |
"
|
|
49 |
!
|
20
|
50 |
|
38
|
51 |
documentation
|
|
52 |
"
|
|
53 |
a PopUpList is basically a button with a popup menu.
|
|
54 |
The PopUpLists label is showing the current selection from the
|
|
55 |
list.
|
121
|
56 |
When an entry is selected, an actionBlock (if nonNil) is evaluated
|
|
57 |
and (if nonNil), the model is notified via the changeMessage.
|
|
58 |
The default changeMessage used is #selection:, which allows a
|
|
59 |
PopUpList to be used with a SelectionInList as model.
|
|
60 |
(if used with some other model, either use an adaptor, or set the
|
|
61 |
changeMessage to something else ..)
|
63
|
62 |
"
|
|
63 |
!
|
38
|
64 |
|
63
|
65 |
examples
|
|
66 |
"
|
38
|
67 |
example use:
|
|
68 |
|
|
69 |
|p|
|
|
70 |
p := PopUpList label:'healthy fruit'.
|
119
|
71 |
p list:#('apples' 'bananas' 'grape' 'lemon' 'margaritas').
|
38
|
72 |
p open
|
|
73 |
|
|
74 |
|
|
75 |
with an initial selection:
|
|
76 |
|
|
77 |
|p|
|
|
78 |
p := PopUpList label:'dummy'.
|
119
|
79 |
p list:#('apples' 'bananas' 'grape' 'lemon' 'margaritas').
|
38
|
80 |
p selection:'apples'.
|
|
81 |
p open
|
|
82 |
|
|
83 |
|
|
84 |
with separating lines:
|
|
85 |
|
|
86 |
|p|
|
119
|
87 |
p := PopUpList label:'fruit'.
|
|
88 |
p list:#('apples' 'bananas' 'grape' 'lemon' '-' 'margaritas').
|
38
|
89 |
p selection:'apples'.
|
|
90 |
p open
|
77
|
91 |
|
|
92 |
|
|
93 |
with an action:
|
|
94 |
|
|
95 |
|p|
|
|
96 |
p := PopUpList label:'dummy'.
|
119
|
97 |
p list:#('apples' 'bananas' 'grape' 'lemon' '-' 'margaritas').
|
77
|
98 |
p selection:'apples'.
|
|
99 |
p action:[:what | Transcript showCr:'you selected: ' , what].
|
|
100 |
p open
|
|
101 |
|
|
102 |
|
119
|
103 |
sometimes, you may like the index instead of the value:
|
|
104 |
(notice, that the separating line counts, so you have take care ...)
|
|
105 |
|
|
106 |
|p|
|
|
107 |
p := PopUpList label:'dummy'.
|
|
108 |
p list:#('apples' 'bananas' 'grape' 'lemon' '-' 'margaritas').
|
|
109 |
p selection:'apples'.
|
|
110 |
p action:[:what | Transcript show:'you selected: '; showCr:what].
|
|
111 |
p useIndex:true.
|
|
112 |
p open
|
|
113 |
|
|
114 |
|
|
115 |
since the list is actually a popupMenu, you can add double-separators:
|
|
116 |
|
|
117 |
|p|
|
|
118 |
p := PopUpList label:'dummy'.
|
|
119 |
p list:#('apples' 'bananas' 'grape' 'lemon'
|
121
|
120 |
'='
|
|
121 |
'margaritas' 'pina colada'
|
|
122 |
'='
|
|
123 |
'smalltalk' 'c++' 'eiffel').
|
119
|
124 |
p values:#(apples bananas grape lemon
|
121
|
125 |
nil
|
|
126 |
'mhmh - so good' 'makes headache'
|
|
127 |
nil
|
|
128 |
'great' 'another headache' 'no bad').
|
119
|
129 |
p selection:'apples'.
|
|
130 |
p action:[:what | Transcript show:'you selected: '; showCr:what].
|
|
131 |
p open
|
|
132 |
|
|
133 |
|
77
|
134 |
with values different from the label strings:
|
|
135 |
|
|
136 |
|p|
|
|
137 |
p := PopUpList label:'dummy'.
|
119
|
138 |
p list:#('apples' 'bananas' 'grape' 'lemon' '-' 'margaritas').
|
77
|
139 |
p selection:'apples'.
|
|
140 |
p values:#(10 20 30 40 nil 50).
|
|
141 |
p action:[:what | Transcript show:'you selected: '; showCr:what].
|
|
142 |
p open
|
119
|
143 |
|
121
|
144 |
|
119
|
145 |
with a model:
|
|
146 |
|
|
147 |
|p model|
|
|
148 |
|
|
149 |
model := SelectionInList with:#('apples' 'bananas' 'grape' 'lemon' 'margaritas').
|
|
150 |
|
|
151 |
p := PopUpList label:'healthy fruit'.
|
|
152 |
p model:model.
|
|
153 |
p open.
|
121
|
154 |
model selectionIndexHolder inspect
|
|
155 |
|
20
|
156 |
|
121
|
157 |
with a model (using numeric selection values):
|
20
|
158 |
|
121
|
159 |
|p model|
|
|
160 |
|
|
161 |
model := SelectionInList with:#('apples' 'bananas' 'grape' 'lemon' 'margaritas').
|
20
|
162 |
|
121
|
163 |
p := PopUpList label:'healthy fruit'.
|
|
164 |
p model:model; useIndex:true.
|
|
165 |
p open.
|
|
166 |
model selectionIndexHolder inspect
|
20
|
167 |
|
|
168 |
|
121
|
169 |
two PopUpLists on the same model:
|
|
170 |
|
|
171 |
|top panel p model|
|
20
|
172 |
|
121
|
173 |
model := Plug new.
|
|
174 |
model respondTo:#eat: with:[:val | Transcript showCr:'eat: ' , val].
|
|
175 |
model respondTo:#drink: with:[:val | Transcript showCr:'drink: ' , val].
|
|
176 |
model respondTo:#meals with:[#(taco burrito enchilada)].
|
|
177 |
model respondTo:#drinks with:[#(margarita water beer)].
|
20
|
178 |
|
121
|
179 |
top := StandardSystemView new.
|
|
180 |
top extent:(100@100).
|
|
181 |
panel := VerticalPanelView origin:0.0@0.0 corner:1.0@1.0 in:top.
|
|
182 |
panel horizontalLayout:#fitSpace.
|
77
|
183 |
|
121
|
184 |
p := PopUpList label:'meals'.
|
|
185 |
p model:model; listMessage:#meals; change:#eat:.
|
|
186 |
panel add:p.
|
119
|
187 |
|
121
|
188 |
p := PopUpList label:'drinks'.
|
|
189 |
p model:model; listMessage:#drinks; change:#drink:.
|
|
190 |
panel add:p.
|
77
|
191 |
|
121
|
192 |
top open
|
|
193 |
"
|
20
|
194 |
! !
|
|
195 |
|
|
196 |
!PopUpList methodsFor:'initialization'!
|
|
197 |
|
|
198 |
initialize
|
|
199 |
super initialize.
|
77
|
200 |
controller beTriggerOnDown.
|
|
201 |
controller action:[self popMenu].
|
119
|
202 |
self adjust:#left.
|
|
203 |
useIndex := false.
|
121
|
204 |
changeMsg := #selection:.
|
119
|
205 |
listMsg := #list.
|
121
|
206 |
aspectMsg := #selection.
|
|
207 |
onLevel := offLevel.
|
20
|
208 |
! !
|
|
209 |
|
|
210 |
!PopUpList methodsFor:'private'!
|
|
211 |
|
|
212 |
computeLabelSize
|
|
213 |
"compute the extent needed to hold the label plus the mark"
|
|
214 |
|
|
215 |
|mmH mmV savedLogo longest longestWidth|
|
|
216 |
|
|
217 |
menu isNil ifTrue:[
|
59
|
218 |
super computeLabelSize
|
20
|
219 |
] ifFalse:[
|
59
|
220 |
"hack: simulate logo change to longest menu entry"
|
20
|
221 |
|
59
|
222 |
font := font on:device.
|
|
223 |
longest := logo.
|
|
224 |
longestWidth := font widthOf:logo.
|
|
225 |
menu labels do:[:entry |
|
|
226 |
|this|
|
20
|
227 |
|
59
|
228 |
this := font widthOf:entry printString.
|
|
229 |
this > longestWidth ifTrue:[
|
|
230 |
longest := entry.
|
|
231 |
longestWidth := this
|
|
232 |
].
|
|
233 |
].
|
|
234 |
savedLogo := logo.
|
|
235 |
logo := longest.
|
|
236 |
super computeLabelSize.
|
|
237 |
logo := savedLogo.
|
38
|
238 |
"self halt. "
|
20
|
239 |
].
|
|
240 |
mmH := device horizontalPixelPerMillimeter.
|
|
241 |
mmV := device verticalPixelPerMillimeter.
|
|
242 |
labelWidth := labelWidth + hSpace + (mmH * 2.5) rounded + hSpace.
|
|
243 |
labelHeight := labelHeight max: (mmV * 2) rounded
|
121
|
244 |
!
|
|
245 |
|
|
246 |
realize
|
|
247 |
super realize.
|
|
248 |
model notNil ifTrue:[
|
|
249 |
self createMenuFor:(model perform:listMsg).
|
|
250 |
|
|
251 |
].
|
|
252 |
!
|
|
253 |
|
|
254 |
createMenuFor:aList
|
|
255 |
menu := PopUpMenu
|
|
256 |
labels:aList
|
|
257 |
selectors:#select:
|
|
258 |
args:(1 to:aList size)
|
|
259 |
receiver:self
|
|
260 |
for:self.
|
|
261 |
!
|
|
262 |
|
|
263 |
getSelectionFromModel
|
|
264 |
"if I have a model and a listMsg, get my list from it"
|
|
265 |
|
|
266 |
(model notNil
|
|
267 |
and:[aspectMsg notNil]) ifTrue:[
|
|
268 |
self selection:(model perform:aspectMsg).
|
|
269 |
].
|
|
270 |
!
|
|
271 |
|
|
272 |
getListFromModel
|
|
273 |
"if I have a model and a listMsg, get my list from it"
|
|
274 |
|
|
275 |
(model notNil
|
|
276 |
and:[listMsg notNil]) ifTrue:[
|
|
277 |
self list:(model perform:listMsg).
|
|
278 |
].
|
|
279 |
! !
|
|
280 |
|
|
281 |
!PopUpList methodsFor:'event handling'!
|
|
282 |
|
|
283 |
popMenu
|
|
284 |
|org mv|
|
|
285 |
|
|
286 |
menu notNil ifTrue:[
|
|
287 |
self turnOffWithoutRedraw.
|
|
288 |
menu font:font.
|
|
289 |
|
|
290 |
"
|
|
291 |
adjust the menus width to my current width
|
|
292 |
"
|
|
293 |
mv := menu menuView.
|
|
294 |
mv create. "/ stupid: it resizes itself upon first create
|
|
295 |
mv width:(self width - (2 * menu margin) - (menu borderWidth*2)).
|
|
296 |
mv level:0; borderWidth:0.
|
|
297 |
|
|
298 |
"
|
|
299 |
the popupMenu wants Display coordinates in its showAt: method
|
|
300 |
"
|
|
301 |
org := device translatePoint:0@0
|
|
302 |
from:(self id)
|
|
303 |
to:(DisplayRootView new id).
|
|
304 |
|
|
305 |
menu showAt:org "resizing:false"
|
|
306 |
].
|
|
307 |
! !
|
|
308 |
|
|
309 |
!PopUpList methodsFor:'drawing'!
|
|
310 |
|
|
311 |
drawWith:fgColor and:bgColor
|
|
312 |
|mmH mmV mW mH|
|
|
313 |
|
|
314 |
controller pressed ifTrue:[
|
|
315 |
super drawWith:enteredFgColor and:enteredBgColor
|
|
316 |
] ifFalse:[
|
|
317 |
super drawWith:fgColor and:bgColor.
|
|
318 |
].
|
|
319 |
mmH := device horizontalPixelPerMillimeter rounded.
|
|
320 |
mmV := device verticalPixelPerMillimeter rounded.
|
|
321 |
mW := (device horizontalPixelPerMillimeter * 2.5) rounded.
|
|
322 |
mH := (device verticalPixelPerMillimeter * 1.5) rounded.
|
|
323 |
|
|
324 |
self drawEdgesForX:(width - mW - (hSpace*2)) y:(height - mmV // 2)
|
|
325 |
width:mW height:mH level:2
|
|
326 |
! !
|
|
327 |
|
|
328 |
!PopUpList methodsFor:'accessing'!
|
|
329 |
|
|
330 |
useIndex:aBoolean
|
|
331 |
useIndex := aBoolean
|
|
332 |
|
|
333 |
"
|
|
334 |
|p|
|
|
335 |
p := PopUpList label:'fruit ?'.
|
|
336 |
p list:#('apples' 'bananas' 'grape' 'lemon' 'margaritas').
|
|
337 |
p action:[:val | Transcript showCr:'selected: ' , val printString].
|
|
338 |
p open.
|
|
339 |
"
|
|
340 |
"
|
|
341 |
|p|
|
|
342 |
p := PopUpList label:'fruit ?'.
|
|
343 |
p list:#('apples' 'bananas' 'grape' 'lemon' 'margaritas').
|
|
344 |
p action:[:val | Transcript showCr:'selected: ' , val printString].
|
|
345 |
p useIndex:true.
|
|
346 |
p open.
|
|
347 |
"
|
|
348 |
!
|
|
349 |
|
|
350 |
list:aList
|
|
351 |
"set the list - i.e. the values shown in the pop-up list"
|
|
352 |
|
|
353 |
self createMenuFor:aList.
|
|
354 |
realized ifTrue:[
|
|
355 |
self computeLabelSize
|
|
356 |
]
|
|
357 |
|
|
358 |
"
|
|
359 |
|p|
|
|
360 |
p := PopUpList label:'fruit ?'.
|
|
361 |
p list:#('apples' 'bananas' 'grape' 'lemon' 'margaritas').
|
|
362 |
p action:[:val | Transcript showCr:'selected: ' , val printString].
|
|
363 |
p open
|
|
364 |
"
|
|
365 |
!
|
|
366 |
|
|
367 |
action:aOneArgBlock
|
|
368 |
"set the action to be performed on selection changes;
|
|
369 |
the argument, aOneArgBlock will be evaluated with the
|
|
370 |
selection-value as argument"
|
|
371 |
|
|
372 |
menuAction := aOneArgBlock
|
|
373 |
!
|
|
374 |
|
|
375 |
contents
|
|
376 |
^ self label
|
|
377 |
!
|
|
378 |
|
|
379 |
contents:con
|
|
380 |
^ self selection:con
|
|
381 |
|
|
382 |
!
|
|
383 |
|
|
384 |
list
|
|
385 |
"return the list - i.e. the values shown in the pop-up list"
|
|
386 |
|
|
387 |
^ menu labels
|
|
388 |
!
|
|
389 |
|
|
390 |
selection:indexOrString
|
|
391 |
"set (force) a selection - usually done to set
|
|
392 |
an initial selection"
|
|
393 |
|
|
394 |
|index|
|
|
395 |
|
|
396 |
index := menu labels indexOf:indexOrString.
|
|
397 |
index == 0 ifTrue:[^ self].
|
|
398 |
self label:(menu labels at:index)
|
|
399 |
|
|
400 |
"
|
|
401 |
|p|
|
|
402 |
p := PopUpList label:'what fruit ?'.
|
|
403 |
p list:#('apples' 'bananas' 'grape' 'lemon' 'margaritas').
|
|
404 |
p selection:'grape'.
|
|
405 |
p open
|
|
406 |
|
|
407 |
|p|
|
|
408 |
p := PopUpList label:'what fruit ?'.
|
|
409 |
p list:#('apples' 'bananas' 'grape' 'lemon' 'margaritas').
|
|
410 |
p selection:'blabla'.
|
|
411 |
p open
|
|
412 |
"
|
|
413 |
!
|
|
414 |
|
|
415 |
values:aList
|
|
416 |
"set a value list - these are reported via the action or changeSymbol instead of
|
|
417 |
the labe strings."
|
|
418 |
|
|
419 |
values := aList.
|
|
420 |
menu args:(1 to:aList size).
|
|
421 |
|
|
422 |
"
|
|
423 |
|p|
|
|
424 |
p := PopUpList label:'fruit ?'.
|
|
425 |
p list:#('apples' 'bananas' 'grape' 'lemon' 'margaritas').
|
|
426 |
p values:#(1 2 3 4 'mhmh - good').
|
|
427 |
p action:[:val | Transcript showCr:'selected: ' , val printString].
|
|
428 |
p open.
|
|
429 |
"
|
|
430 |
!
|
|
431 |
|
|
432 |
listMessage:aSelector
|
|
433 |
"set the selector by which we ask the model for the list.
|
|
434 |
Default is #list."
|
|
435 |
|
|
436 |
listMsg := aSelector
|
20
|
437 |
! !
|
|
438 |
|
|
439 |
!PopUpList methodsFor:'user actions'!
|
|
440 |
|
|
441 |
select:anEntry
|
119
|
442 |
"this is sent from the popupmenu when an entry was selected"
|
|
443 |
|
77
|
444 |
|value label|
|
|
445 |
|
119
|
446 |
label := menu labels at:anEntry.
|
77
|
447 |
values isNil ifTrue:[
|
121
|
448 |
value := anEntry.
|
|
449 |
useIndex ifFalse:[
|
|
450 |
value := menu labels at:anEntry.
|
|
451 |
]
|
77
|
452 |
] ifFalse:[
|
121
|
453 |
value := values at:anEntry
|
77
|
454 |
].
|
|
455 |
|
20
|
456 |
menuAction notNil ifTrue:[
|
121
|
457 |
menuAction value:value.
|
20
|
458 |
].
|
|
459 |
self sizeFixed:true.
|
77
|
460 |
self label:label printString.
|
119
|
461 |
"
|
121
|
462 |
tell my model - if any
|
119
|
463 |
"
|
95
|
464 |
self sendChangeMessageWith:value
|
20
|
465 |
! !
|
121
|
466 |
|
|
467 |
!PopUpList methodsFor:'change & update'!
|
|
468 |
|
|
469 |
update:something with:aParameter from:changedObject
|
|
470 |
(changedObject == model) ifTrue:[
|
|
471 |
|
|
472 |
something == aspectMsg ifTrue:[
|
|
473 |
self getSelectionFromModel.
|
|
474 |
] ifFalse:[
|
|
475 |
something == listMsg ifTrue:[
|
|
476 |
self getListFromModel.
|
|
477 |
].
|
|
478 |
].
|
|
479 |
^ self
|
|
480 |
].
|
|
481 |
super update:something with:aParameter from:changedObject
|
|
482 |
! !
|
|
483 |
|