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