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