author | Claus Gittinger <cg@exept.de> |
Tue, 30 Apr 1996 17:36:22 +0200 | |
changeset 607 | 67462978fb20 |
parent 593 | 86dd024ed773 |
child 609 | 734f451e9ad0 |
permissions | -rw-r--r-- |
0 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1989 by Claus Gittinger |
59 | 3 |
All Rights Reserved |
0 | 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:#Toggle |
|
377 | 14 |
instanceVariableNames:'showLamp lampColor lampWidth lampHeight' |
15 |
classVariableNames:'DefaultShowLamp DefaultLampColor' |
|
16 |
poolDictionaries:'' |
|
17 |
category:'Views-Interactors' |
|
0 | 18 |
! |
19 |
||
22 | 20 |
!Toggle class methodsFor:'documentation'! |
21 |
||
38 | 22 |
copyright |
23 |
" |
|
24 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
59 | 25 |
All Rights Reserved |
38 | 26 |
|
27 |
This software is furnished under a license and may be used |
|
28 |
only in accordance with the terms of that license and with the |
|
29 |
inclusion of the above copyright notice. This software may not |
|
30 |
be provided or otherwise made available to, or used by, any |
|
31 |
other person. No title to or ownership of the software is |
|
32 |
hereby transferred. |
|
33 |
" |
|
34 |
! |
|
35 |
||
22 | 36 |
documentation |
37 |
" |
|
128 | 38 |
this button changes state whenever clicked upon and stays down (pressed) |
39 |
until clicked again. All the main action is in Button and the controller |
|
40 |
(ToggleController). |
|
38 | 41 |
|
42 |
The toggle may optionally display a little kind-of-lamp (or LED), which |
|
43 |
is turned on when the toggle is pressed. (i.e. as in the Interviews toolkit). |
|
44 |
||
45 |
whenever the Toggle changes its change, it will evaluate one of |
|
46 |
pressAction or releaseAction. |
|
47 |
||
48 |
For ST-80 compatibility, if the model is nonNil, this one gets a new |
|
128 | 49 |
value and is sent a changed message. Also, the toggle will follow changes |
50 |
in the model and update its display as appropriate. |
|
38 | 51 |
If nonNil, the model is supposed to be a ValueHolder holding true or false. |
52 |
||
53 |
||
585 | 54 |
[instance variables:] |
55 |
showLamp <Boolean> true if a lamp should be displayed |
|
56 |
lampColor <Color> color of the lamp |
|
57 |
lampWidth <Integer> width of the lamp in pixel |
|
58 |
lampHeight <Integer> height of the lamp in pixel |
|
119 | 59 |
|
585 | 60 |
[styleSheet values:] |
61 |
toggleShowLamp <Boolean> if true, a lamp is shown; default:false. |
|
62 |
toggleLampColor <Color> lamps color; default:yellow. |
|
63 |
||
64 |
[author:] |
|
65 |
Claus Gittinger |
|
63 | 66 |
" |
67 |
! |
|
38 | 68 |
|
63 | 69 |
examples |
70 |
" |
|
38 | 71 |
Examples: |
128 | 72 |
Try all, to see what is possible. |
377 | 73 |
(notice, that these examples are meant to show what can be done; |
74 |
usually, all style-related stuff is preinitialized - you should not |
|
75 |
normally play around with onLevel, offLevel, showLamp: etc) |
|
38 | 76 |
|
128 | 77 |
|
38 | 78 |
simple: |
593 | 79 |
[exBegin] |
377 | 80 |
|v t| |
38 | 81 |
|
377 | 82 |
v := StandardSystemView new extent:200@200. |
83 |
t := Toggle label:'press here' in:v. |
|
84 |
t origin:10 @ 10. |
|
85 |
t action:[:value | Transcript show:'toggle state: '; showCr:value.]. |
|
86 |
v open |
|
593 | 87 |
[exEnd] |
119 | 88 |
|
128 | 89 |
|
119 | 90 |
separate press/release actions: |
593 | 91 |
[exBegin] |
377 | 92 |
|v t| |
119 | 93 |
|
377 | 94 |
v := StandardSystemView new extent:200@200. |
95 |
t := Toggle label:'press here' in:v. |
|
96 |
t origin:10 @ 10. |
|
97 |
t pressAction:[Transcript showCr:'toggle pressed'.]. |
|
98 |
t releaseAction:[Transcript showCr:'toggle released'.]. |
|
99 |
v open |
|
593 | 100 |
[exEnd] |
38 | 101 |
|
128 | 102 |
|
38 | 103 |
changing logo: |
593 | 104 |
[exBegin] |
377 | 105 |
|v t| |
106 |
||
107 |
v := StandardSystemView new extent:200@200. |
|
108 |
t := Toggle label:'eat me' in:v. |
|
109 |
t origin:10 @ 10. |
|
110 |
t pressAction:[Transcript showCr:'smaller'. t label:'drink me']. |
|
111 |
t releaseAction:[Transcript showCr:'larger'. t label:'eat me']. |
|
112 |
v open |
|
593 | 113 |
[exEnd] |
377 | 114 |
|
115 |
||
378 | 116 |
changing logo: |
593 | 117 |
[exBegin] |
378 | 118 |
|v t| |
119 |
||
120 |
v := StandardSystemView new extent:200@200. |
|
121 |
t := Toggle in:v. |
|
122 |
t logo:(Form width:16 height:14 fromArray:#( |
|
123 |
2r00000000 2r00000000 |
|
124 |
2r00000000 2r00000000 |
|
125 |
2r00000000 2r00000000 |
|
126 |
2r00000111 2r11100000 |
|
127 |
2r00001100 2r00000000 |
|
128 |
2r00011000 2r00000000 |
|
129 |
2r00011000 2r00000000 |
|
130 |
2r00011000 2r00000000 |
|
131 |
2r00011000 2r00000000 |
|
132 |
2r00001100 2r00000000 |
|
133 |
2r00000111 2r11100000 |
|
134 |
2r00000000 2r00000000 |
|
135 |
2r00000000 2r00000000 |
|
136 |
2r00000000 2r00000000)). |
|
137 |
t origin:10 @ 10. |
|
138 |
v open |
|
593 | 139 |
[exEnd] |
378 | 140 |
|
141 |
||
377 | 142 |
uhh - changing logos and getting bg-colors from the image: |
593 | 143 |
[exBegin] |
377 | 144 |
|v t i| |
145 |
||
146 |
v := StandardSystemView new extent:200@200. |
|
147 |
t := Toggle in:v. |
|
148 |
t origin:10 @ 10. |
|
149 |
t showLamp:false. |
|
150 |
t activeLogo:(i := Image fromFile:'red-ball.gif'). |
|
151 |
t passiveLogo:(Image fromFile:'green-ball.gif'). |
|
152 |
t backgroundColor:(i at:0@0). |
|
153 |
t enteredBackgroundColor:(t backgroundColor). |
|
154 |
t activeBackgroundColor:(t backgroundColor). |
|
155 |
v open |
|
593 | 156 |
[exEnd] |
38 | 157 |
|
128 | 158 |
|
38 | 159 |
changing logo and freezing size (looks better): |
593 | 160 |
[exBegin] |
377 | 161 |
|v t| |
38 | 162 |
|
377 | 163 |
v := StandardSystemView new extent:200@200. |
164 |
'create with large logo; freeze; change to small logo'. |
|
165 |
t := Toggle label:'drink me' in:v. |
|
166 |
t sizeFixed:true. |
|
167 |
t label:'eat me'. |
|
38 | 168 |
|
377 | 169 |
t origin:10 @ 10. |
170 |
t pressAction:[Transcript showCr:'smaller'. t label:'drink me']. |
|
171 |
t releaseAction:[Transcript showCr:'larger'. t label:'eat me']. |
|
172 |
v open |
|
593 | 173 |
[exEnd] |
38 | 174 |
|
128 | 175 |
|
119 | 176 |
adding a lamp (in some view styles, this is the default anyway): |
593 | 177 |
[exBegin] |
377 | 178 |
|v t| |
38 | 179 |
|
377 | 180 |
v := StandardSystemView new extent:200@200. |
181 |
t := Toggle label:'off' in:v. |
|
182 |
t showLamp:true. |
|
183 |
t origin:10 @ 10. |
|
184 |
t pressAction:[Transcript showCr:'on'. t label:'on']. |
|
185 |
t releaseAction:[Transcript showCr:'off'. t label:'off']. |
|
186 |
v open |
|
593 | 187 |
[exEnd] |
38 | 188 |
|
128 | 189 |
|
119 | 190 |
changing lamps color: |
593 | 191 |
[exBegin] |
377 | 192 |
|v t| |
38 | 193 |
|
377 | 194 |
v := StandardSystemView new extent:200@200. |
195 |
t := Toggle label:'off' in:v. |
|
196 |
t showLamp:true. |
|
197 |
t lampColor:Color red. |
|
198 |
t origin:10 @ 10. |
|
199 |
t pressAction:[Transcript showCr:'on'. t label:'on'.]. |
|
200 |
t releaseAction:[Transcript showCr:'off'. t label:'off'.]. |
|
201 |
v open |
|
593 | 202 |
[exEnd] |
119 | 203 |
|
128 | 204 |
|
119 | 205 |
changing lamps color & size: |
593 | 206 |
[exBegin] |
377 | 207 |
|v t| |
119 | 208 |
|
377 | 209 |
v := StandardSystemView new extent:200@200. |
210 |
t := Toggle label:'off' in:v. |
|
211 |
t showLamp:true. |
|
212 |
t lampColor:Color red. |
|
213 |
t origin:10 @ 10. |
|
214 |
t pressAction:[Transcript showCr:'on'. t label:'on'.]. |
|
215 |
t releaseAction:[Transcript showCr:'off'. t label:'off'.]. |
|
216 |
v open |
|
593 | 217 |
[exEnd] |
119 | 218 |
|
128 | 219 |
|
119 | 220 |
lamp only - no '3D going-in' (this is the default with IRIS style) |
593 | 221 |
[exBegin] |
377 | 222 |
|v t| |
119 | 223 |
|
377 | 224 |
v := StandardSystemView new extent:200@200. |
225 |
t := Toggle label:'off' in:v. |
|
226 |
t showLamp:true. |
|
227 |
t onLevel:(t offLevel). |
|
228 |
t origin:10 @ 10. |
|
229 |
t pressAction:[Transcript showCr:'on'. t label:'on']. |
|
230 |
t releaseAction:[Transcript showCr:'off'. t label:'off']. |
|
231 |
v open |
|
593 | 232 |
[exEnd] |
38 | 233 |
|
128 | 234 |
|
38 | 235 |
lamp and freezing size of the label (looks better): |
593 | 236 |
[exBegin] |
377 | 237 |
|v t| |
38 | 238 |
|
377 | 239 |
v := StandardSystemView new extent:200@200. |
240 |
t := Toggle label:'off' in:v. |
|
241 |
t showLamp:true. |
|
242 |
t sizeFixed:true. |
|
243 |
t origin:10 @ 10. |
|
244 |
t pressAction:[Transcript showCr:'on'. t label:'on']. |
|
245 |
t releaseAction:[Transcript showCr:'off'. t label:'off']. |
|
246 |
v open |
|
593 | 247 |
[exEnd] |
38 | 248 |
|
128 | 249 |
|
38 | 250 |
another variation: |
593 | 251 |
[exBegin] |
377 | 252 |
|v t| |
38 | 253 |
|
377 | 254 |
v := StandardSystemView new extent:200@200. |
255 |
t := Toggle label:'off' in:v. |
|
256 |
t showLamp:true. |
|
257 |
t sizeFixed:true. |
|
258 |
t onLevel:(t offLevel). |
|
259 |
t origin:10 @ 10. |
|
260 |
t pressAction:[Transcript showCr:'on'. t label:'on']. |
|
261 |
t releaseAction:[Transcript showCr:'off'. t label:'off']. |
|
262 |
v open |
|
593 | 263 |
[exEnd] |
38 | 264 |
|
128 | 265 |
|
38 | 266 |
and another one: |
593 | 267 |
[exBegin] |
377 | 268 |
|v t| |
38 | 269 |
|
377 | 270 |
v := StandardSystemView new extent:200@200. |
271 |
t := Toggle label:'off' in:v. |
|
272 |
t showLamp:true. |
|
273 |
t sizeFixed:true. |
|
38 | 274 |
|
377 | 275 |
t showLamp:false. |
276 |
t offLevel:3. |
|
277 |
t onLevel:3. |
|
278 |
t origin:10 @ 10. |
|
279 |
t pressAction:[Transcript showCr:'on'. t showLamp:true. t label:'on']. |
|
280 |
t releaseAction:[Transcript showCr:'off'. t showLamp:false. t label:'off']. |
|
281 |
v open |
|
593 | 282 |
[exEnd] |
38 | 283 |
|
128 | 284 |
|
38 | 285 |
another font: |
593 | 286 |
[exBegin] |
377 | 287 |
|v t| |
38 | 288 |
|
377 | 289 |
v := StandardSystemView new extent:200@200. |
290 |
t := Toggle label:'off' in:v. |
|
291 |
t font:(Font family:'times' face:'bold' style:'roman' size:24). |
|
292 |
t label:'hello'. |
|
293 |
t origin:10 @ 10. |
|
294 |
t pressAction:[Transcript showCr:'on'.]. |
|
295 |
t releaseAction:[Transcript showCr:'off'.]. |
|
296 |
v open |
|
593 | 297 |
[exEnd] |
38 | 298 |
|
128 | 299 |
|
38 | 300 |
another font (no, I dont know what it means :-): |
593 | 301 |
[exBegin] |
377 | 302 |
|v t| |
38 | 303 |
|
377 | 304 |
v := StandardSystemView new extent:200@200. |
305 |
t := Toggle label:'off' in:v. |
|
306 |
t font:(Font family:'k14' face:nil style:nil size:nil). |
|
307 |
t label:(TwoByteString with:(Character value:16r3021)). |
|
38 | 308 |
|
377 | 309 |
t origin:10 @ 10. |
310 |
t pressAction:[Transcript showCr:'on'.]. |
|
311 |
t releaseAction:[Transcript showCr:'off'.]. |
|
312 |
v open |
|
593 | 313 |
[exEnd] |
119 | 314 |
|
128 | 315 |
|
316 |
using a model (look at 'value' in the inspector): |
|
593 | 317 |
[exBegin] |
377 | 318 |
|m v t| |
119 | 319 |
|
377 | 320 |
m := ValueHolder newBoolean. |
321 |
m inspect. |
|
119 | 322 |
|
377 | 323 |
v := StandardSystemView new extent:200@200. |
324 |
t := Toggle label:'press here to change value' in:v. |
|
325 |
t origin:10 @ 10. |
|
326 |
t model:m. |
|
327 |
v open |
|
593 | 328 |
[exEnd] |
119 | 329 |
|
128 | 330 |
|
119 | 331 |
using a model with different changeSelector: |
593 | 332 |
[exBegin] |
377 | 333 |
|m v t| |
119 | 334 |
|
377 | 335 |
m := Plug new. |
336 |
m respondTo:#setValue1: with:[:value | Transcript show:'value 1 changed to: '; showCr:value]. |
|
337 |
m respondTo:#setValue2: with:[:value | Transcript show:'value 2 changed to: '; showCr:value]. |
|
119 | 338 |
|
377 | 339 |
v := StandardSystemView new extent:200@200. |
340 |
t := Toggle label:'press here for value1' in:v. |
|
341 |
t origin:10 @ 10. |
|
342 |
t model:m; change:#setValue1:. |
|
343 |
t := Toggle label:'press here for value2' in:v. |
|
344 |
t origin:10 @ 50. |
|
345 |
t model:m; change:#setValue2:. |
|
346 |
v open |
|
593 | 347 |
[exEnd] |
128 | 348 |
|
349 |
||
350 |
two toggles on the same model: |
|
593 | 351 |
[exBegin] |
377 | 352 |
|m v t| |
128 | 353 |
|
377 | 354 |
m := true asValue. |
128 | 355 |
|
377 | 356 |
v := StandardSystemView new extent:200@200. |
357 |
t := Toggle label:'press here' in:v. |
|
358 |
t origin:10 @ 10. |
|
359 |
t model:m. |
|
128 | 360 |
|
377 | 361 |
t := Toggle label:'or here' in:v. |
362 |
t origin:10 @ 50. |
|
363 |
t model:m. |
|
364 |
v open |
|
593 | 365 |
[exEnd] |
22 | 366 |
" |
367 |
! ! |
|
368 |
||
119 | 369 |
!Toggle class methodsFor:'defaults'! |
370 |
||
371 |
updateStyleCache |
|
441
e170bca66a78
added style resource directive
Claus Gittinger <cg@exept.de>
parents:
378
diff
changeset
|
372 |
"extract values from the styleSheet and cache them in class variables" |
e170bca66a78
added style resource directive
Claus Gittinger <cg@exept.de>
parents:
378
diff
changeset
|
373 |
|
e170bca66a78
added style resource directive
Claus Gittinger <cg@exept.de>
parents:
378
diff
changeset
|
374 |
<resource: #style (#toggleShowLamp |
e170bca66a78
added style resource directive
Claus Gittinger <cg@exept.de>
parents:
378
diff
changeset
|
375 |
#toggleLampColor)> |
e170bca66a78
added style resource directive
Claus Gittinger <cg@exept.de>
parents:
378
diff
changeset
|
376 |
|
119 | 377 |
DefaultShowLamp := StyleSheet at:'toggleShowLamp' default:false. |
378 |
DefaultLampColor := StyleSheet colorAt:'toggleLampColor' default:Color yellow. |
|
441
e170bca66a78
added style resource directive
Claus Gittinger <cg@exept.de>
parents:
378
diff
changeset
|
379 |
|
e170bca66a78
added style resource directive
Claus Gittinger <cg@exept.de>
parents:
378
diff
changeset
|
380 |
"Modified: 1.3.1996 / 13:50:30 / cg" |
119 | 381 |
! ! |
382 |
||
593 | 383 |
!Toggle methodsFor:'accessing-look'! |
205 | 384 |
|
385 |
lampColor:aColor |
|
386 |
"change the color of the toggle-lamp" |
|
387 |
||
388 |
lampColor := aColor. |
|
389 |
(shown and:[showLamp and:[controller pressed]]) ifTrue:[ |
|
390 |
self redraw |
|
391 |
] |
|
392 |
! |
|
393 |
||
394 |
showLamp |
|
395 |
"return true, if I show a lamp" |
|
396 |
||
397 |
^ showLamp |
|
398 |
! |
|
399 |
||
400 |
showLamp:aBoolean |
|
401 |
"enable/disable drawing of the lamp" |
|
402 |
||
403 |
showLamp ~~ aBoolean ifTrue:[ |
|
404 |
showLamp := aBoolean. |
|
405 |
self computeLabelSize. |
|
406 |
fixSize ifFalse:[ |
|
407 |
self resize |
|
408 |
] |
|
409 |
] |
|
410 |
! ! |
|
411 |
||
412 |
!Toggle methodsFor:'change & update'! |
|
413 |
||
414 |
update:something with:parameter from:changedObject |
|
415 |
something == aspectMsg ifTrue:[ |
|
416 |
self getValueFromModel. |
|
417 |
^ self |
|
418 |
]. |
|
419 |
super update:something with:parameter from:changedObject |
|
420 |
! ! |
|
421 |
||
22 | 422 |
!Toggle methodsFor:'initialization'! |
423 |
||
105 | 424 |
defaultControllerClass |
425 |
^ ToggleController |
|
426 |
! |
|
427 |
||
22 | 428 |
initStyle |
429 |
super initStyle. |
|
430 |
||
119 | 431 |
showLamp := DefaultShowLamp. |
22 | 432 |
showLamp ifTrue:[ |
59 | 433 |
onLevel := offLevel. |
22 | 434 |
|
59 | 435 |
"dont know, if I like this ..." |
436 |
" |
|
437 |
activeBgColor := bgColor |
|
438 |
" |
|
22 | 439 |
]. |
38 | 440 |
|
119 | 441 |
lampColor := DefaultLampColor on:device. |
38 | 442 |
lampWidth := (device horizontalPixelPerMillimeter * 1.8) rounded. |
443 |
lampHeight := (device verticalPixelPerMillimeter * 3.5) rounded. |
|
119 | 444 |
! |
445 |
||
446 |
realize |
|
447 |
"/ |
|
448 |
"/ get my initial state from the model (if there is one) |
|
449 |
"/ |
|
450 |
self getValueFromModel. |
|
451 |
super realize |
|
22 | 452 |
! ! |
453 |
||
454 |
!Toggle methodsFor:'private'! |
|
455 |
||
456 |
computeLabelOrigin |
|
457 |
"compute the origin of the form/text. |
|
458 |
redefined to move label to the right if there is a lamp." |
|
459 |
||
460 |
super computeLabelOrigin. |
|
461 |
showLamp ifTrue:[ |
|
59 | 462 |
labelOriginX := labelOriginX + hSpace + lampWidth + hSpace. |
22 | 463 |
] |
464 |
! |
|
465 |
||
466 |
computeLabelSize |
|
467 |
"compute the extent needed to hold the label plus the lamp" |
|
468 |
||
469 |
super computeLabelSize. |
|
470 |
showLamp ifTrue:[ |
|
59 | 471 |
labelWidth := labelWidth + hSpace + lampWidth + hSpace. |
472 |
labelHeight := labelHeight max: lampHeight |
|
22 | 473 |
] |
205 | 474 |
! |
0 | 475 |
|
205 | 476 |
getValueFromModel |
477 |
|val| |
|
121 | 478 |
|
205 | 479 |
(model notNil and:[aspectMsg notNil]) ifTrue:[ |
480 |
aspectMsg numArgs == 0 ifTrue:[ |
|
481 |
val := model perform:aspectMsg |
|
482 |
] ifFalse:[ |
|
483 |
val := model perform:aspectMsg with:self |
|
484 |
]. |
|
485 |
val ifTrue:[ |
|
486 |
self turnOn |
|
487 |
] ifFalse:[ |
|
488 |
self turnOff |
|
489 |
]. |
|
121 | 490 |
]. |
205 | 491 |
|
492 |
"Created: 14.11.1995 / 21:08:43 / cg" |
|
121 | 493 |
! ! |
494 |
||
105 | 495 |
!Toggle methodsFor:'redrawing'! |
496 |
||
497 |
drawWith:fg and:bg |
|
498 |
"redraw myself with fg/bg. Use super to draw the label, |
|
499 |
drawing of the lamp is done here." |
|
500 |
||
501 |
|x y clr| |
|
502 |
||
503 |
super drawWith:fg and:bg. "this draws the text" |
|
504 |
||
505 |
showLamp ifTrue:[ |
|
506 |
x := hSpace + margin. |
|
507 |
y := (height - lampHeight) // 2. |
|
508 |
self drawEdgesForX:x y:y width:lampWidth height:lampHeight level:-1. |
|
509 |
controller pressed ifTrue:[ |
|
510 |
clr := lampColor. |
|
511 |
] ifFalse:[ |
|
512 |
clr := bgColor. |
|
513 |
]. |
|
514 |
self paint:clr. |
|
515 |
self fillRectangleX:x+2 y:y+2 width:lampWidth - 4 height:lampHeight - 4 |
|
516 |
] |
|
517 |
! ! |
|
205 | 518 |
|
519 |
!Toggle class methodsFor:'documentation'! |
|
520 |
||
521 |
version |
|
593 | 522 |
^ '$Header: /cvs/stx/stx/libwidg/Toggle.st,v 1.28 1996-04-27 18:18:22 cg Exp $' |
205 | 523 |
! ! |