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
|
22
|
14 |
instanceVariableNames:'showLamp lampColor lampWidth lampHeight'
|
0
|
15 |
classVariableNames:''
|
|
16 |
poolDictionaries:''
|
|
17 |
category:'Views-Interactors'
|
|
18 |
!
|
|
19 |
|
|
20 |
Toggle comment:'
|
5
|
21 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
59
|
22 |
All Rights Reserved
|
0
|
23 |
|
74
|
24 |
$Header: /cvs/stx/stx/libwidg/Toggle.st,v 1.10 1995-01-26 16:05:02 claus Exp $
|
22
|
25 |
'!
|
|
26 |
|
|
27 |
!Toggle class methodsFor:'documentation'!
|
|
28 |
|
38
|
29 |
copyright
|
|
30 |
"
|
|
31 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
59
|
32 |
All Rights Reserved
|
38
|
33 |
|
|
34 |
This software is furnished under a license and may be used
|
|
35 |
only in accordance with the terms of that license and with the
|
|
36 |
inclusion of the above copyright notice. This software may not
|
|
37 |
be provided or otherwise made available to, or used by, any
|
|
38 |
other person. No title to or ownership of the software is
|
|
39 |
hereby transferred.
|
|
40 |
"
|
|
41 |
!
|
|
42 |
|
|
43 |
version
|
|
44 |
"
|
74
|
45 |
$Header: /cvs/stx/stx/libwidg/Toggle.st,v 1.10 1995-01-26 16:05:02 claus Exp $
|
38
|
46 |
"
|
|
47 |
!
|
|
48 |
|
22
|
49 |
documentation
|
|
50 |
"
|
38
|
51 |
this button changes state whenever pressed and stays pressed until pressed
|
|
52 |
again. All the main action is in Button, Toggle just redefines buttonpress/
|
|
53 |
release behavior.
|
|
54 |
|
|
55 |
The toggle may optionally display a little kind-of-lamp (or LED), which
|
|
56 |
is turned on when the toggle is pressed. (i.e. as in the Interviews toolkit).
|
|
57 |
|
|
58 |
whenever the Toggle changes its change, it will evaluate one of
|
|
59 |
pressAction or releaseAction.
|
|
60 |
|
|
61 |
For ST-80 compatibility, if the model is nonNil, this one gets a new
|
|
62 |
value and is sent a changed message.
|
|
63 |
If nonNil, the model is supposed to be a ValueHolder holding true or false.
|
|
64 |
|
|
65 |
|
|
66 |
instance variables:
|
59
|
67 |
showLamp <Boolean> true if a lamp should be displayed
|
|
68 |
lampColor <Color> color of the lamp
|
|
69 |
lampWidth <Integer> width of the lamp in pixel
|
|
70 |
lampHeight <Integer> height of the lamp in pixel
|
63
|
71 |
"
|
|
72 |
!
|
38
|
73 |
|
63
|
74 |
examples
|
|
75 |
"
|
38
|
76 |
Examples:
|
59
|
77 |
Try these, to see what is possible.
|
38
|
78 |
|
59
|
79 |
(notice, that these examples are meant to show what can be done;
|
|
80 |
usually, all style-related stuff id preinitialized - you should not
|
|
81 |
normally play around with onLevel, offLevel, showLamp: etc)
|
38
|
82 |
|
|
83 |
simple:
|
|
84 |
|
59
|
85 |
|v t|
|
38
|
86 |
|
59
|
87 |
v := View new.
|
|
88 |
t := Toggle label:'press here' in:v.
|
|
89 |
t origin:10 @ 10.
|
|
90 |
t pressAction:[Transcript showCr:'toggle pressed'.].
|
|
91 |
t releaseAction:[Transcript showCr:'toggle released'.].
|
|
92 |
v realize
|
38
|
93 |
|
|
94 |
changing logo:
|
|
95 |
|
59
|
96 |
|v t|
|
38
|
97 |
|
59
|
98 |
v := View new.
|
|
99 |
t := Toggle label:'eat me' in:v.
|
|
100 |
t origin:10 @ 10.
|
|
101 |
t pressAction:[Transcript showCr:'smaller'. t label:'drink me'].
|
|
102 |
t releaseAction:[Transcript showCr:'larger'. t label:'eat me'].
|
|
103 |
v realize
|
38
|
104 |
|
|
105 |
changing logo and freezing size (looks better):
|
|
106 |
|
59
|
107 |
|v t|
|
38
|
108 |
|
59
|
109 |
v := View new.
|
|
110 |
'create with large logo; freeze; change to small logo'.
|
|
111 |
t := Toggle label:'drink me' in:v.
|
|
112 |
t sizeFixed:true.
|
|
113 |
t label:'eat me'.
|
38
|
114 |
|
59
|
115 |
t origin:10 @ 10.
|
|
116 |
t pressAction:[Transcript showCr:'smaller'. t label:'drink me'].
|
|
117 |
t releaseAction:[Transcript showCr:'larger'. t label:'eat me'].
|
|
118 |
v realize
|
38
|
119 |
|
|
120 |
adding lamp (on by default in some view styles):
|
|
121 |
|
59
|
122 |
|v t|
|
38
|
123 |
|
59
|
124 |
v := View new.
|
|
125 |
t := Toggle label:'off' in:v.
|
|
126 |
t showLamp:true.
|
|
127 |
t origin:10 @ 10.
|
|
128 |
t pressAction:[Transcript showCr:'on'. t label:'on'].
|
|
129 |
t releaseAction:[Transcript showCr:'off'. t label:'off'].
|
|
130 |
v realize
|
38
|
131 |
|
|
132 |
lamp only - no 'going-in'
|
0
|
133 |
|
59
|
134 |
|v t|
|
38
|
135 |
|
59
|
136 |
v := View new.
|
|
137 |
t := Toggle label:'off' in:v.
|
|
138 |
t showLamp:true.
|
|
139 |
t onLevel:(t offLevel).
|
|
140 |
t origin:10 @ 10.
|
|
141 |
t pressAction:[Transcript showCr:'on'. t label:'on'].
|
|
142 |
t releaseAction:[Transcript showCr:'off'. t label:'off'].
|
|
143 |
v realize
|
38
|
144 |
|
|
145 |
lamp and freezing size of the label (looks better):
|
|
146 |
|
59
|
147 |
|v t|
|
38
|
148 |
|
59
|
149 |
v := View new.
|
|
150 |
t := Toggle label:'off' in:v.
|
|
151 |
t showLamp:true.
|
|
152 |
t sizeFixed:true.
|
|
153 |
t origin:10 @ 10.
|
|
154 |
t pressAction:[Transcript showCr:'on'. t label:'on'].
|
|
155 |
t releaseAction:[Transcript showCr:'off'. t label:'off'].
|
|
156 |
v realize
|
38
|
157 |
|
|
158 |
another variation:
|
|
159 |
|
59
|
160 |
|v t|
|
38
|
161 |
|
59
|
162 |
v := View new.
|
|
163 |
t := Toggle label:'off' in:v.
|
|
164 |
t showLamp:true.
|
|
165 |
t sizeFixed:true.
|
|
166 |
t onLevel:(t offLevel).
|
|
167 |
t origin:10 @ 10.
|
|
168 |
t pressAction:[Transcript showCr:'on'. t label:'on'].
|
|
169 |
t releaseAction:[Transcript showCr:'off'. t label:'off'].
|
|
170 |
v realize
|
38
|
171 |
|
|
172 |
and another one:
|
|
173 |
|
59
|
174 |
|v t|
|
38
|
175 |
|
59
|
176 |
v := View new.
|
|
177 |
t := Toggle label:'off' in:v.
|
|
178 |
t showLamp:true.
|
|
179 |
t sizeFixed:true.
|
38
|
180 |
|
59
|
181 |
t showLamp:false.
|
|
182 |
t offLevel:3.
|
|
183 |
t onLevel:3.
|
|
184 |
t origin:10 @ 10.
|
|
185 |
t pressAction:[Transcript showCr:'on'. t showLamp:true. t label:'on'].
|
|
186 |
t releaseAction:[Transcript showCr:'off'. t showLamp:false. t label:'off'].
|
|
187 |
v realize
|
38
|
188 |
|
|
189 |
another font:
|
|
190 |
|
59
|
191 |
|v t|
|
38
|
192 |
|
59
|
193 |
v := View new.
|
|
194 |
t := Toggle label:'off' in:v.
|
|
195 |
t font:(Font family:'times' face:'bold' style:'roman' size:24).
|
|
196 |
t label:'hello'.
|
|
197 |
t origin:10 @ 10.
|
|
198 |
t pressAction:[Transcript showCr:'on'.].
|
|
199 |
t releaseAction:[Transcript showCr:'off'.].
|
|
200 |
v realize
|
38
|
201 |
|
|
202 |
another font (no, I dont know what it means :-):
|
|
203 |
|
59
|
204 |
|v t|
|
38
|
205 |
|
59
|
206 |
v := View new.
|
|
207 |
t := Toggle label:'off' in:v.
|
|
208 |
t font:(Font family:'k14' face:nil style:nil size:nil).
|
|
209 |
t label:(TwoByteString with:(Character value:16r3021)).
|
38
|
210 |
|
59
|
211 |
t origin:10 @ 10.
|
|
212 |
t pressAction:[Transcript showCr:'on'.].
|
|
213 |
t releaseAction:[Transcript showCr:'off'.].
|
|
214 |
v realize
|
22
|
215 |
"
|
|
216 |
! !
|
|
217 |
|
|
218 |
!Toggle methodsFor:'initialization'!
|
|
219 |
|
59
|
220 |
initialize
|
|
221 |
super initialize.
|
|
222 |
actionWhenPressed := true
|
|
223 |
!
|
|
224 |
|
22
|
225 |
initStyle
|
|
226 |
super initStyle.
|
|
227 |
|
59
|
228 |
showLamp := StyleSheet at:#toggleShowLamp default:false.
|
22
|
229 |
showLamp ifTrue:[
|
59
|
230 |
onLevel := offLevel.
|
22
|
231 |
|
59
|
232 |
"dont know, if I like this ..."
|
|
233 |
"
|
|
234 |
activeBgColor := bgColor
|
|
235 |
"
|
22
|
236 |
].
|
38
|
237 |
|
59
|
238 |
lampColor := StyleSheet at:#toggleLampColor default:Color yellow.
|
38
|
239 |
lampWidth := (device horizontalPixelPerMillimeter * 1.8) rounded.
|
|
240 |
lampHeight := (device verticalPixelPerMillimeter * 3.5) rounded.
|
22
|
241 |
! !
|
|
242 |
|
|
243 |
!Toggle methodsFor:'accessing'!
|
|
244 |
|
59
|
245 |
showLamp
|
|
246 |
"return true, if I show a lamp"
|
|
247 |
|
|
248 |
^ showLamp
|
|
249 |
!
|
|
250 |
|
38
|
251 |
showLamp:aBoolean
|
|
252 |
"enable/disable drawing of the lamp"
|
|
253 |
|
|
254 |
showLamp ~~ aBoolean ifTrue:[
|
59
|
255 |
showLamp := aBoolean.
|
|
256 |
self computeLabelSize.
|
|
257 |
fixSize ifFalse:[
|
|
258 |
self resize
|
|
259 |
]
|
38
|
260 |
]
|
|
261 |
!
|
|
262 |
|
22
|
263 |
lampColor:aColor
|
|
264 |
"change the color of the toggle-lamp"
|
|
265 |
|
|
266 |
lampColor := aColor.
|
|
267 |
(shown and:[showLamp and:[pressed]]) ifTrue:[
|
59
|
268 |
self redraw
|
22
|
269 |
]
|
|
270 |
! !
|
|
271 |
|
|
272 |
|
|
273 |
!Toggle methodsFor:'private'!
|
|
274 |
|
|
275 |
computeLabelOrigin
|
|
276 |
"compute the origin of the form/text.
|
|
277 |
redefined to move label to the right if there is a lamp."
|
|
278 |
|
|
279 |
super computeLabelOrigin.
|
|
280 |
showLamp ifTrue:[
|
59
|
281 |
labelOriginX := labelOriginX + hSpace + lampWidth + hSpace.
|
22
|
282 |
]
|
|
283 |
!
|
|
284 |
|
|
285 |
computeLabelSize
|
|
286 |
"compute the extent needed to hold the label plus the lamp"
|
|
287 |
|
|
288 |
super computeLabelSize.
|
|
289 |
showLamp ifTrue:[
|
59
|
290 |
labelWidth := labelWidth + hSpace + lampWidth + hSpace.
|
|
291 |
labelHeight := labelHeight max: lampHeight
|
22
|
292 |
]
|
|
293 |
! !
|
0
|
294 |
|
|
295 |
!Toggle methodsFor:'changing state'!
|
|
296 |
|
|
297 |
toggleNoAction
|
|
298 |
"toggle, but do NOT perform any action - can be used to change a toggle
|
|
299 |
under program control (i.e. turn one toggle off from another one)"
|
|
300 |
|
38
|
301 |
|newLevel|
|
|
302 |
|
0
|
303 |
pressed := pressed not.
|
7
|
304 |
pressed ifTrue:[
|
59
|
305 |
newLevel := onLevel.
|
7
|
306 |
] ifFalse:[
|
59
|
307 |
newLevel := offLevel.
|
7
|
308 |
].
|
38
|
309 |
self level:newLevel.
|
|
310 |
|
22
|
311 |
shown ifTrue:[
|
59
|
312 |
self redraw
|
38
|
313 |
].
|
|
314 |
model notNil ifTrue:[
|
59
|
315 |
model value:pressed
|
22
|
316 |
]
|
0
|
317 |
!
|
|
318 |
|
|
319 |
toggle
|
|
320 |
"toggle and perform the action"
|
|
321 |
|
7
|
322 |
|action|
|
|
323 |
|
0
|
324 |
enabled ifTrue:[
|
59
|
325 |
self toggleNoAction.
|
|
326 |
pressed ifTrue:[
|
|
327 |
action := pressActionBlock
|
|
328 |
] ifFalse:[
|
|
329 |
action := releaseActionBlock
|
|
330 |
].
|
|
331 |
action notNil ifTrue:[action value].
|
|
332 |
model notNil ifTrue:[
|
|
333 |
model value:pressed.
|
|
334 |
model changed
|
|
335 |
].
|
|
336 |
self changed
|
0
|
337 |
]
|
|
338 |
! !
|
|
339 |
|
70
|
340 |
!Toggle methodsFor:'event handling'!
|
0
|
341 |
|
|
342 |
buttonPress:button x:x y:y
|
38
|
343 |
((button == 1) or:[button == #select]) ifTrue:[
|
59
|
344 |
self toggle
|
38
|
345 |
] ifFalse:[
|
59
|
346 |
^ super buttonPress:button x:x y:y
|
0
|
347 |
].
|
|
348 |
!
|
|
349 |
|
|
350 |
buttonRelease:button x:x y:y
|
38
|
351 |
((button == 1) or:[button == #select]) ifFalse:[
|
59
|
352 |
^ super buttonRelease:button x:x y:y
|
0
|
353 |
].
|
|
354 |
"ignore"
|
|
355 |
! !
|
22
|
356 |
|
|
357 |
!Toggle methodsFor:'redrawing'!
|
|
358 |
|
|
359 |
drawWith:fg and:bg
|
|
360 |
"redraw myself with fg/bg. Use super to draw the label,
|
38
|
361 |
drawing of the lamp is done here."
|
22
|
362 |
|
|
363 |
|x y|
|
|
364 |
|
|
365 |
super drawWith:fg and:bg. "this draws the text"
|
|
366 |
|
|
367 |
showLamp ifTrue:[
|
59
|
368 |
x := hSpace + margin.
|
|
369 |
y := (height - lampHeight) // 2.
|
|
370 |
self drawEdgesForX:x y:y width:lampWidth height:lampHeight level:-1.
|
|
371 |
pressed ifTrue:[
|
|
372 |
self paint:lampColor.
|
|
373 |
] ifFalse:[
|
|
374 |
self paint:bgColor.
|
|
375 |
].
|
|
376 |
self fillRectangleX:x+2 y:y+2 width:lampWidth - 4 height:lampHeight - 4
|
22
|
377 |
]
|
|
378 |
! !
|