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