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