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