author | Claus Gittinger <cg@exept.de> |
Tue, 28 May 1996 22:11:10 +0200 | |
changeset 711 | c830bb66598b |
parent 655 | acad3ef3a46c |
child 936 | 0e8811726930 |
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. |
|
655 | 91 |
t action:[:value | Transcript show:'toggle state: '; showCR:value.]. |
377 | 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. |
|
655 | 103 |
t pressAction:[Transcript showCR:'toggle pressed'.]. |
104 |
t releaseAction:[Transcript showCR:'toggle released'.]. |
|
377 | 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. |
|
655 | 116 |
t pressAction:[Transcript showCR:'smaller'. t label:'drink me']. |
117 |
t releaseAction:[Transcript showCR:'larger'. t label:'eat me']. |
|
377 | 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. |
655 | 176 |
t pressAction:[Transcript showCR:'smaller'. t label:'drink me']. |
177 |
t releaseAction:[Transcript showCR:'larger'. t label:'eat me']. |
|
377 | 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. |
|
655 | 190 |
t pressAction:[Transcript showCR:'on'. t label:'on']. |
191 |
t releaseAction:[Transcript showCR:'off'. t label:'off']. |
|
377 | 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. |
|
655 | 205 |
t pressAction:[Transcript showCR:'on'. t label:'on'.]. |
206 |
t releaseAction:[Transcript showCR:'off'. t label:'off'.]. |
|
377 | 207 |
v open |
593 | 208 |
[exEnd] |
119 | 209 |
|
128 | 210 |
|
119 | 211 |
lamp only - no '3D going-in' (this is the default with IRIS style) |
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 onLevel:(t offLevel). |
|
219 |
t origin:10 @ 10. |
|
655 | 220 |
t pressAction:[Transcript showCR:'on'. t label:'on']. |
221 |
t releaseAction:[Transcript showCR:'off'. t label:'off']. |
|
377 | 222 |
v open |
593 | 223 |
[exEnd] |
38 | 224 |
|
128 | 225 |
|
38 | 226 |
lamp and freezing size of the label (looks better): |
593 | 227 |
[exBegin] |
377 | 228 |
|v t| |
38 | 229 |
|
377 | 230 |
v := StandardSystemView new extent:200@200. |
231 |
t := Toggle label:'off' in:v. |
|
232 |
t showLamp:true. |
|
233 |
t sizeFixed:true. |
|
234 |
t origin:10 @ 10. |
|
655 | 235 |
t pressAction:[Transcript showCR:'on'. t label:'on']. |
236 |
t releaseAction:[Transcript showCR:'off'. t label:'off']. |
|
377 | 237 |
v open |
593 | 238 |
[exEnd] |
38 | 239 |
|
128 | 240 |
|
38 | 241 |
another variation: |
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 onLevel:(t offLevel). |
|
250 |
t origin:10 @ 10. |
|
655 | 251 |
t pressAction:[Transcript showCR:'on'. t label:'on']. |
252 |
t releaseAction:[Transcript showCR:'off'. t label:'off']. |
|
377 | 253 |
v open |
593 | 254 |
[exEnd] |
38 | 255 |
|
128 | 256 |
|
38 | 257 |
and another one: |
593 | 258 |
[exBegin] |
377 | 259 |
|v t| |
38 | 260 |
|
377 | 261 |
v := StandardSystemView new extent:200@200. |
262 |
t := Toggle label:'off' in:v. |
|
263 |
t showLamp:true. |
|
264 |
t sizeFixed:true. |
|
38 | 265 |
|
377 | 266 |
t showLamp:false. |
267 |
t offLevel:3. |
|
268 |
t onLevel:3. |
|
269 |
t origin:10 @ 10. |
|
655 | 270 |
t pressAction:[Transcript showCR:'on'. t showLamp:true. t label:'on']. |
271 |
t releaseAction:[Transcript showCR:'off'. t showLamp:false. t label:'off']. |
|
377 | 272 |
v open |
593 | 273 |
[exEnd] |
38 | 274 |
|
128 | 275 |
|
38 | 276 |
another font: |
593 | 277 |
[exBegin] |
377 | 278 |
|v t| |
38 | 279 |
|
377 | 280 |
v := StandardSystemView new extent:200@200. |
281 |
t := Toggle label:'off' in:v. |
|
282 |
t font:(Font family:'times' face:'bold' style:'roman' size:24). |
|
283 |
t label:'hello'. |
|
284 |
t origin:10 @ 10. |
|
655 | 285 |
t pressAction:[Transcript showCR:'on'.]. |
286 |
t releaseAction:[Transcript showCR:'off'.]. |
|
377 | 287 |
v open |
593 | 288 |
[exEnd] |
38 | 289 |
|
128 | 290 |
|
38 | 291 |
another font (no, I dont know what it means :-): |
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:'k14' face:nil style:nil size:nil). |
|
298 |
t label:(TwoByteString with:(Character value:16r3021)). |
|
38 | 299 |
|
377 | 300 |
t origin:10 @ 10. |
655 | 301 |
t pressAction:[Transcript showCR:'on'.]. |
302 |
t releaseAction:[Transcript showCR:'off'.]. |
|
377 | 303 |
v open |
593 | 304 |
[exEnd] |
119 | 305 |
|
128 | 306 |
|
307 |
using a model (look at 'value' in the inspector): |
|
593 | 308 |
[exBegin] |
377 | 309 |
|m v t| |
119 | 310 |
|
377 | 311 |
m := ValueHolder newBoolean. |
312 |
m inspect. |
|
119 | 313 |
|
377 | 314 |
v := StandardSystemView new extent:200@200. |
315 |
t := Toggle label:'press here to change value' in:v. |
|
316 |
t origin:10 @ 10. |
|
317 |
t model:m. |
|
318 |
v open |
|
593 | 319 |
[exEnd] |
119 | 320 |
|
128 | 321 |
|
119 | 322 |
using a model with different changeSelector: |
593 | 323 |
[exBegin] |
377 | 324 |
|m v t| |
119 | 325 |
|
377 | 326 |
m := Plug new. |
655 | 327 |
m respondTo:#setValue1: with:[:value | Transcript show:'value 1 changed to: '; showCR:value]. |
328 |
m respondTo:#setValue2: with:[:value | Transcript show:'value 2 changed to: '; showCR:value]. |
|
119 | 329 |
|
377 | 330 |
v := StandardSystemView new extent:200@200. |
331 |
t := Toggle label:'press here for value1' in:v. |
|
332 |
t origin:10 @ 10. |
|
333 |
t model:m; change:#setValue1:. |
|
334 |
t := Toggle label:'press here for value2' in:v. |
|
335 |
t origin:10 @ 50. |
|
336 |
t model:m; change:#setValue2:. |
|
337 |
v open |
|
593 | 338 |
[exEnd] |
128 | 339 |
|
340 |
||
341 |
two toggles on the same model: |
|
593 | 342 |
[exBegin] |
377 | 343 |
|m v t| |
128 | 344 |
|
377 | 345 |
m := true asValue. |
128 | 346 |
|
377 | 347 |
v := StandardSystemView new extent:200@200. |
348 |
t := Toggle label:'press here' in:v. |
|
349 |
t origin:10 @ 10. |
|
350 |
t model:m. |
|
128 | 351 |
|
377 | 352 |
t := Toggle label:'or here' in:v. |
353 |
t origin:10 @ 50. |
|
354 |
t model:m. |
|
355 |
v open |
|
593 | 356 |
[exEnd] |
22 | 357 |
" |
358 |
! ! |
|
359 |
||
119 | 360 |
!Toggle class methodsFor:'defaults'! |
361 |
||
362 |
updateStyleCache |
|
441
e170bca66a78
added style resource directive
Claus Gittinger <cg@exept.de>
parents:
378
diff
changeset
|
363 |
"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
|
364 |
|
e170bca66a78
added style resource directive
Claus Gittinger <cg@exept.de>
parents:
378
diff
changeset
|
365 |
<resource: #style (#toggleShowLamp |
e170bca66a78
added style resource directive
Claus Gittinger <cg@exept.de>
parents:
378
diff
changeset
|
366 |
#toggleLampColor)> |
e170bca66a78
added style resource directive
Claus Gittinger <cg@exept.de>
parents:
378
diff
changeset
|
367 |
|
119 | 368 |
DefaultShowLamp := StyleSheet at:'toggleShowLamp' default:false. |
369 |
DefaultLampColor := StyleSheet colorAt:'toggleLampColor' default:Color yellow. |
|
441
e170bca66a78
added style resource directive
Claus Gittinger <cg@exept.de>
parents:
378
diff
changeset
|
370 |
|
e170bca66a78
added style resource directive
Claus Gittinger <cg@exept.de>
parents:
378
diff
changeset
|
371 |
"Modified: 1.3.1996 / 13:50:30 / cg" |
119 | 372 |
! ! |
373 |
||
593 | 374 |
!Toggle methodsFor:'accessing-look'! |
205 | 375 |
|
376 |
lampColor:aColor |
|
377 |
"change the color of the toggle-lamp" |
|
378 |
||
379 |
lampColor := aColor. |
|
380 |
(shown and:[showLamp and:[controller pressed]]) ifTrue:[ |
|
711
c830bb66598b
use new #invalidate instead of redraw
Claus Gittinger <cg@exept.de>
parents:
655
diff
changeset
|
381 |
self invalidate |
205 | 382 |
] |
711
c830bb66598b
use new #invalidate instead of redraw
Claus Gittinger <cg@exept.de>
parents:
655
diff
changeset
|
383 |
|
c830bb66598b
use new #invalidate instead of redraw
Claus Gittinger <cg@exept.de>
parents:
655
diff
changeset
|
384 |
"Modified: 28.5.1996 / 21:39:44 / cg" |
205 | 385 |
! |
386 |
||
387 |
showLamp |
|
388 |
"return true, if I show a lamp" |
|
389 |
||
390 |
^ showLamp |
|
391 |
! |
|
392 |
||
393 |
showLamp:aBoolean |
|
394 |
"enable/disable drawing of the lamp" |
|
395 |
||
396 |
showLamp ~~ aBoolean ifTrue:[ |
|
397 |
showLamp := aBoolean. |
|
398 |
self computeLabelSize. |
|
399 |
fixSize ifFalse:[ |
|
400 |
self resize |
|
401 |
] |
|
402 |
] |
|
403 |
! ! |
|
404 |
||
405 |
!Toggle methodsFor:'change & update'! |
|
406 |
||
407 |
update:something with:parameter from:changedObject |
|
408 |
something == aspectMsg ifTrue:[ |
|
409 |
self getValueFromModel. |
|
410 |
^ self |
|
411 |
]. |
|
412 |
super update:something with:parameter from:changedObject |
|
413 |
! ! |
|
414 |
||
22 | 415 |
!Toggle methodsFor:'initialization'! |
416 |
||
105 | 417 |
defaultControllerClass |
418 |
^ ToggleController |
|
419 |
! |
|
420 |
||
22 | 421 |
initStyle |
422 |
super initStyle. |
|
423 |
||
119 | 424 |
showLamp := DefaultShowLamp. |
22 | 425 |
showLamp ifTrue:[ |
59 | 426 |
onLevel := offLevel. |
22 | 427 |
|
59 | 428 |
"dont know, if I like this ..." |
429 |
" |
|
430 |
activeBgColor := bgColor |
|
431 |
" |
|
22 | 432 |
]. |
38 | 433 |
|
119 | 434 |
lampColor := DefaultLampColor on:device. |
38 | 435 |
lampWidth := (device horizontalPixelPerMillimeter * 1.8) rounded. |
436 |
lampHeight := (device verticalPixelPerMillimeter * 3.5) rounded. |
|
119 | 437 |
! |
438 |
||
439 |
realize |
|
440 |
"/ |
|
441 |
"/ get my initial state from the model (if there is one) |
|
442 |
"/ |
|
443 |
self getValueFromModel. |
|
444 |
super realize |
|
22 | 445 |
! ! |
446 |
||
447 |
!Toggle methodsFor:'private'! |
|
448 |
||
449 |
computeLabelOrigin |
|
450 |
"compute the origin of the form/text. |
|
451 |
redefined to move label to the right if there is a lamp." |
|
452 |
||
453 |
super computeLabelOrigin. |
|
454 |
showLamp ifTrue:[ |
|
59 | 455 |
labelOriginX := labelOriginX + hSpace + lampWidth + hSpace. |
22 | 456 |
] |
457 |
! |
|
458 |
||
459 |
computeLabelSize |
|
460 |
"compute the extent needed to hold the label plus the lamp" |
|
461 |
||
462 |
super computeLabelSize. |
|
463 |
showLamp ifTrue:[ |
|
59 | 464 |
labelWidth := labelWidth + hSpace + lampWidth + hSpace. |
465 |
labelHeight := labelHeight max: lampHeight |
|
22 | 466 |
] |
205 | 467 |
! |
0 | 468 |
|
205 | 469 |
getValueFromModel |
470 |
|val| |
|
121 | 471 |
|
205 | 472 |
(model notNil and:[aspectMsg notNil]) ifTrue:[ |
473 |
aspectMsg numArgs == 0 ifTrue:[ |
|
474 |
val := model perform:aspectMsg |
|
475 |
] ifFalse:[ |
|
476 |
val := model perform:aspectMsg with:self |
|
477 |
]. |
|
478 |
val ifTrue:[ |
|
479 |
self turnOn |
|
480 |
] ifFalse:[ |
|
481 |
self turnOff |
|
482 |
]. |
|
121 | 483 |
]. |
205 | 484 |
|
485 |
"Created: 14.11.1995 / 21:08:43 / cg" |
|
121 | 486 |
! ! |
487 |
||
105 | 488 |
!Toggle methodsFor:'redrawing'! |
489 |
||
490 |
drawWith:fg and:bg |
|
491 |
"redraw myself with fg/bg. Use super to draw the label, |
|
492 |
drawing of the lamp is done here." |
|
493 |
||
494 |
|x y clr| |
|
495 |
||
496 |
super drawWith:fg and:bg. "this draws the text" |
|
497 |
||
498 |
showLamp ifTrue:[ |
|
499 |
x := hSpace + margin. |
|
500 |
y := (height - lampHeight) // 2. |
|
501 |
self drawEdgesForX:x y:y width:lampWidth height:lampHeight level:-1. |
|
502 |
controller pressed ifTrue:[ |
|
503 |
clr := lampColor. |
|
504 |
] ifFalse:[ |
|
505 |
clr := bgColor. |
|
506 |
]. |
|
507 |
self paint:clr. |
|
508 |
self fillRectangleX:x+2 y:y+2 width:lampWidth - 4 height:lampHeight - 4 |
|
509 |
] |
|
510 |
! ! |
|
205 | 511 |
|
512 |
!Toggle class methodsFor:'documentation'! |
|
513 |
||
514 |
version |
|
711
c830bb66598b
use new #invalidate instead of redraw
Claus Gittinger <cg@exept.de>
parents:
655
diff
changeset
|
515 |
^ '$Header: /cvs/stx/stx/libwidg/Toggle.st,v 1.31 1996-05-28 20:10:12 cg Exp $' |
205 | 516 |
! ! |