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