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