76
|
1 |
"
|
|
2 |
COPYRIGHT (c) 1995 by Claus Gittinger
|
|
3 |
All Rights Reserved
|
|
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 |
Controller subclass:#ButtonController
|
118
|
14 |
instanceVariableNames:'enabled pressed active entered isTriggerOnDown autoRepeat
|
76
|
15 |
repeatBlock initialDelay repeatDelay
|
118
|
16 |
pressActionBlock releaseActionBlock isToggle'
|
76
|
17 |
classVariableNames:''
|
|
18 |
poolDictionaries:''
|
|
19 |
category:'Interface-Support'
|
|
20 |
!
|
|
21 |
|
|
22 |
!ButtonController class methodsFor:'documentation'!
|
|
23 |
|
|
24 |
copyright
|
|
25 |
"
|
|
26 |
COPYRIGHT (c) 1995 by Claus Gittinger
|
|
27 |
All Rights Reserved
|
|
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
|
|
39 |
"
|
118
|
40 |
$Header: /cvs/stx/stx/libwidg/ButtonController.st,v 1.6 1995-05-03 00:28:46 claus Exp $
|
76
|
41 |
"
|
|
42 |
!
|
|
43 |
|
|
44 |
documentation
|
|
45 |
"
|
|
46 |
ButtonControllers are used with buttons and handle all user interaction.
|
|
47 |
These are automatically created when a Button is created, therefore no manual
|
|
48 |
action is required for creation.
|
|
49 |
|
|
50 |
Instance variables:
|
|
51 |
|
|
52 |
enabled <Boolean> pressing is allowed (default: true)
|
|
53 |
pressed <Boolean> true if currently pressed (read-only)
|
|
54 |
entered <Boolean> true if the cursor is currently in this view
|
118
|
55 |
isTriggerOnDown <Boolean> controls if the action should be executed on
|
76
|
56 |
press or on release (default: on release).
|
|
57 |
pressActionBlock <Block> block to evaluate when pressed (default: noop)
|
|
58 |
releaseActionBlock <Block> block to evaluate when released (default: noop)
|
|
59 |
autoRepeat <Boolean> auto-repeats when pressed long enough (default: false)
|
|
60 |
initialDelay <Number> seconds till first auto-repeat (default: 0.2)
|
|
61 |
repeatDelay <Number> seconds of repeat intervall (default: 0.025)
|
|
62 |
repeatBlock <Block> block evaluated for auto-repeat (internal)
|
|
63 |
active <Boolean> true during action evaluation (internal)
|
|
64 |
"
|
|
65 |
! !
|
|
66 |
|
|
67 |
!ButtonController class methodsFor:'defaults'!
|
|
68 |
|
|
69 |
defaultRepeatDelay
|
|
70 |
"when autorepeat is enabled, and button is not released,
|
|
71 |
repeat every repeatDelay seconds"
|
|
72 |
|
|
73 |
^ 0.025
|
|
74 |
!
|
|
75 |
|
|
76 |
defaultInitialDelay
|
|
77 |
"when autorepeat is enabled, and button is not released,
|
|
78 |
start repeating after initialDelay seconds"
|
|
79 |
|
|
80 |
^ 0.2
|
|
81 |
! !
|
|
82 |
|
|
83 |
!ButtonController methodsFor:'initialization'!
|
|
84 |
|
|
85 |
initialize
|
|
86 |
super initialize.
|
|
87 |
|
|
88 |
enabled := true.
|
|
89 |
active := false.
|
|
90 |
pressed := false.
|
|
91 |
entered := false.
|
|
92 |
autoRepeat := false.
|
|
93 |
initialDelay := self class defaultInitialDelay.
|
|
94 |
repeatDelay := self class defaultRepeatDelay.
|
118
|
95 |
isTriggerOnDown := false.
|
|
96 |
isToggle := false.
|
76
|
97 |
! !
|
|
98 |
|
|
99 |
!ButtonController methodsFor:'accessing'!
|
|
100 |
|
118
|
101 |
beToggle
|
|
102 |
"make the receiver act like a toggle"
|
|
103 |
|
|
104 |
isTriggerOnDown := true.
|
|
105 |
isToggle := true
|
|
106 |
!
|
|
107 |
|
76
|
108 |
beTriggerOnUp
|
|
109 |
"make the receiver act on button release"
|
|
110 |
|
118
|
111 |
isTriggerOnDown := false
|
76
|
112 |
!
|
|
113 |
|
|
114 |
beTriggerOnDown
|
|
115 |
"make the receiver act on button press"
|
|
116 |
|
118
|
117 |
isTriggerOnDown := true
|
76
|
118 |
!
|
|
119 |
|
|
120 |
triggerOnDown:aBoolean
|
|
121 |
"set/clear the flag which controls if the action block is to be evaluated
|
|
122 |
on press or on release.
|
|
123 |
(see also ST-80 compatibility methods beTriggerOn*)"
|
|
124 |
|
118
|
125 |
isTriggerOnDown := aBoolean
|
76
|
126 |
!
|
|
127 |
|
|
128 |
isTriggerOnDown
|
88
|
129 |
"return true, if I trigger on press
|
|
130 |
(in contrast to triggering on up, which is the default)"
|
|
131 |
|
118
|
132 |
^ isTriggerOnDown
|
76
|
133 |
!
|
|
134 |
|
|
135 |
pressAction
|
|
136 |
"return the pressAction; thats the block which gets evaluated
|
|
137 |
when the button is pressed (if non-nil)"
|
|
138 |
|
|
139 |
^ pressActionBlock
|
|
140 |
!
|
|
141 |
|
|
142 |
pressAction:aBlock
|
|
143 |
"define the action to be performed on press"
|
|
144 |
|
|
145 |
pressActionBlock := aBlock
|
|
146 |
!
|
|
147 |
|
|
148 |
releaseAction
|
|
149 |
"return the releaseAction; thats the block which gets evaluated
|
|
150 |
when the button is relreased (if non-nil)"
|
|
151 |
|
|
152 |
^ releaseActionBlock
|
|
153 |
!
|
|
154 |
|
|
155 |
releaseAction:aBlock
|
|
156 |
"define the action to be performed on release"
|
|
157 |
|
|
158 |
releaseActionBlock := aBlock
|
|
159 |
!
|
|
160 |
|
|
161 |
action:aBlock
|
|
162 |
"convenient method: depending on the setting the triggerOnDown flag,
|
|
163 |
either set the press-action clear any release-action or
|
|
164 |
vice versa, set the release-action and clear the press-action."
|
|
165 |
|
118
|
166 |
isTriggerOnDown ifTrue:[
|
76
|
167 |
releaseActionBlock := nil.
|
|
168 |
pressActionBlock := aBlock
|
|
169 |
] ifFalse:[
|
|
170 |
releaseActionBlock := aBlock.
|
|
171 |
pressActionBlock := nil
|
|
172 |
]
|
|
173 |
!
|
|
174 |
|
|
175 |
autoRepeat
|
|
176 |
"turn on autorepeat"
|
|
177 |
|
|
178 |
autoRepeat := true.
|
|
179 |
repeatBlock := [self repeat]
|
|
180 |
!
|
|
181 |
|
|
182 |
disable
|
|
183 |
"disable the button"
|
|
184 |
|
|
185 |
enabled ifTrue:[
|
|
186 |
enabled := false.
|
|
187 |
view redraw
|
|
188 |
]
|
|
189 |
!
|
|
190 |
|
|
191 |
enable
|
|
192 |
"enable the button"
|
|
193 |
|
|
194 |
enabled ifFalse:[
|
|
195 |
enabled := true.
|
|
196 |
view redraw
|
|
197 |
]
|
|
198 |
!
|
|
199 |
|
|
200 |
enabled
|
88
|
201 |
"return true, if I am enabled"
|
|
202 |
|
76
|
203 |
^ enabled
|
|
204 |
!
|
|
205 |
|
|
206 |
active
|
88
|
207 |
"return true, if I am active; that is currently performing my
|
|
208 |
action."
|
|
209 |
|
76
|
210 |
^ active
|
|
211 |
!
|
|
212 |
|
|
213 |
pressed
|
88
|
214 |
"return true, if I am pressed"
|
|
215 |
|
76
|
216 |
^ pressed
|
|
217 |
!
|
|
218 |
|
|
219 |
entered
|
88
|
220 |
"return true, if the mouse pointer is currently in my view"
|
|
221 |
|
76
|
222 |
^ entered
|
|
223 |
!
|
|
224 |
|
|
225 |
entered:aBoolean
|
|
226 |
entered := aBoolean
|
|
227 |
!
|
|
228 |
|
|
229 |
pressed:aBoolean
|
|
230 |
pressed := aBoolean
|
|
231 |
!
|
|
232 |
|
|
233 |
active:aBoolean
|
|
234 |
active := aBoolean
|
118
|
235 |
!
|
|
236 |
|
|
237 |
toggleNoAction
|
|
238 |
"toggle, but do NOT perform any action"
|
|
239 |
|
|
240 |
pressed ifTrue:[
|
|
241 |
view turnOff.
|
|
242 |
pressed := false.
|
|
243 |
] ifFalse:[
|
|
244 |
view turnOn.
|
|
245 |
pressed := true.
|
|
246 |
].
|
|
247 |
!
|
|
248 |
|
|
249 |
toggle
|
|
250 |
"toggle and perform the action"
|
|
251 |
|
|
252 |
enabled ifTrue:[
|
|
253 |
self toggleNoAction.
|
|
254 |
self performAction.
|
|
255 |
view sendChangeMessageWith:pressed.
|
|
256 |
view changed:#toggle with:pressed
|
|
257 |
]
|
76
|
258 |
! !
|
|
259 |
|
|
260 |
!ButtonController methodsFor:'event handling'!
|
|
261 |
|
118
|
262 |
performAction
|
|
263 |
|action|
|
|
264 |
|
|
265 |
pressed ifTrue:[
|
|
266 |
action := pressActionBlock
|
|
267 |
] ifFalse:[
|
|
268 |
action := releaseActionBlock
|
|
269 |
].
|
|
270 |
action notNil ifTrue:[action value].
|
|
271 |
!
|
|
272 |
|
76
|
273 |
buttonPress:button x:x y:y
|
118
|
274 |
|sym action|
|
76
|
275 |
|
|
276 |
(button == 1 or:[button == #select]) ifFalse:[
|
|
277 |
^ super buttonPress:button x:x y:y
|
|
278 |
].
|
|
279 |
|
118
|
280 |
enabled ifTrue:[
|
|
281 |
isToggle ifTrue:[
|
|
282 |
self toggle.
|
|
283 |
^ self
|
|
284 |
].
|
|
285 |
|
|
286 |
pressed ifFalse:[
|
76
|
287 |
pressed := true.
|
|
288 |
view showActive.
|
|
289 |
|
|
290 |
(pressActionBlock notNil or:[model notNil]) ifTrue:[
|
|
291 |
"
|
|
292 |
force output - so that button is drawn correctly in case
|
|
293 |
of any long-computation (at high priority)
|
|
294 |
"
|
|
295 |
view device synchronizeOutput.
|
|
296 |
].
|
|
297 |
|
|
298 |
active := true.
|
|
299 |
|
118
|
300 |
self performAction.
|
76
|
301 |
|
118
|
302 |
isTriggerOnDown ifTrue:[
|
76
|
303 |
"the ST-80 way of doing things"
|
97
|
304 |
view notNil ifTrue:[
|
|
305 |
view sendChangeMessageWith:true
|
|
306 |
]
|
76
|
307 |
].
|
|
308 |
|
|
309 |
active := false.
|
|
310 |
|
|
311 |
autoRepeat ifTrue:[
|
|
312 |
Processor addTimedBlock:repeatBlock afterSeconds:initialDelay
|
|
313 |
]
|
|
314 |
]
|
|
315 |
]
|
|
316 |
!
|
|
317 |
|
|
318 |
buttonRelease:button x:x y:y
|
|
319 |
"button was released - if enabled, perform releaseaction"
|
|
320 |
|
|
321 |
|sym|
|
|
322 |
|
|
323 |
(button == 1 or:[button == #select]) ifFalse:[
|
|
324 |
^ super buttonRelease:button x:x y:y
|
|
325 |
].
|
118
|
326 |
|
|
327 |
isToggle ifTrue:[
|
|
328 |
^ self
|
|
329 |
].
|
|
330 |
|
76
|
331 |
pressed ifTrue:[
|
|
332 |
autoRepeat ifTrue:[
|
|
333 |
Processor removeTimedBlock:repeatBlock
|
|
334 |
].
|
|
335 |
pressed := false.
|
|
336 |
view showPassive.
|
|
337 |
|
|
338 |
enabled ifTrue:[
|
|
339 |
"
|
|
340 |
only perform action if released within myself
|
|
341 |
"
|
|
342 |
((x >= 0)
|
|
343 |
and:[x <= view width
|
|
344 |
and:[y >= 0
|
|
345 |
and:[y <= view height]]]) ifTrue:[
|
|
346 |
(releaseActionBlock notNil or:[model notNil]) ifTrue:[
|
|
347 |
"
|
|
348 |
force output - so that button is drawn correctly in case
|
|
349 |
of any long-computation (at high priority)
|
|
350 |
"
|
|
351 |
view device synchronizeOutput.
|
|
352 |
].
|
|
353 |
|
|
354 |
active := true.
|
|
355 |
|
118
|
356 |
self performAction.
|
|
357 |
|
|
358 |
isTriggerOnDown ifFalse:[
|
76
|
359 |
"the ST-80 way of doing things"
|
97
|
360 |
view notNil ifTrue:[
|
|
361 |
view sendChangeMessageWith:false.
|
|
362 |
].
|
76
|
363 |
].
|
|
364 |
|
|
365 |
active := false.
|
|
366 |
]
|
|
367 |
]
|
|
368 |
]
|
|
369 |
!
|
|
370 |
|
|
371 |
pointerEnter:state x:x y:y
|
|
372 |
"redraw with enteredColors if they differ from the normal colors"
|
|
373 |
|
|
374 |
entered := true.
|
|
375 |
pressed ifTrue:[
|
|
376 |
"
|
|
377 |
reentered after a leave with mouse-button down;
|
|
378 |
restart autorepeating and/or if I am a button with
|
118
|
379 |
isTriggerOnDown, show active again.
|
76
|
380 |
"
|
|
381 |
enabled ifTrue:[
|
|
382 |
autoRepeat ifTrue:[
|
|
383 |
Processor addTimedBlock:repeatBlock afterSeconds:initialDelay
|
|
384 |
].
|
118
|
385 |
isTriggerOnDown ifFalse:[
|
76
|
386 |
view showActive.
|
|
387 |
]
|
|
388 |
]
|
|
389 |
] ifFalse:[
|
|
390 |
enabled ifTrue:[
|
|
391 |
view redraw
|
|
392 |
]
|
|
393 |
]
|
|
394 |
!
|
|
395 |
|
|
396 |
repeat
|
|
397 |
"this is sent from the autorepeat-block, when the button has been pressed long
|
|
398 |
enough; it simulates a release-press, by evaluating both release
|
|
399 |
and press actions."
|
|
400 |
|
|
401 |
pressed ifTrue:[
|
|
402 |
enabled ifTrue:[
|
|
403 |
active ifFalse:[
|
|
404 |
active := true.
|
|
405 |
releaseActionBlock notNil ifTrue:[releaseActionBlock value].
|
|
406 |
pressActionBlock notNil ifTrue:[pressActionBlock value].
|
|
407 |
active := false.
|
|
408 |
|
|
409 |
autoRepeat ifTrue:[
|
|
410 |
Processor addTimedBlock:repeatBlock afterSeconds:repeatDelay
|
|
411 |
]
|
|
412 |
]
|
|
413 |
]
|
|
414 |
]
|
|
415 |
!
|
|
416 |
|
|
417 |
buttonMultiPress:button x:x y:y
|
|
418 |
^ self buttonPress:button x:x y:y
|
|
419 |
!
|
|
420 |
|
|
421 |
keyPress:key x:x y:y
|
|
422 |
"only trigger, if I am the focusView of my group"
|
|
423 |
|
|
424 |
|group|
|
|
425 |
|
|
426 |
((group := view windowGroup) notNil
|
|
427 |
and:[group focusView == view]) ifTrue:[
|
|
428 |
(key == #Return or:[key == Character space]) ifTrue:[
|
|
429 |
"just simulate a buttonPress/release here."
|
|
430 |
self buttonPress:1 x:0 y:0.
|
|
431 |
self buttonRelease:1 x:0 y:0.
|
|
432 |
^ self.
|
|
433 |
]
|
|
434 |
].
|
|
435 |
view keyPress:key x:x y:y
|
|
436 |
!
|
|
437 |
|
|
438 |
pointerLeave:state
|
|
439 |
"redraw with normal colors if they differ from enteredColors"
|
|
440 |
|
|
441 |
entered := false.
|
|
442 |
pressed ifTrue:[
|
|
443 |
"
|
|
444 |
leave with mouse-button down;
|
|
445 |
stop autorepeating and/or if I am a button with
|
|
446 |
action on release, show passive
|
|
447 |
"
|
|
448 |
autoRepeat ifTrue:[
|
|
449 |
Processor removeTimedBlock:repeatBlock
|
|
450 |
].
|
118
|
451 |
isTriggerOnDown ifFalse:[
|
76
|
452 |
view showPassive.
|
|
453 |
]
|
|
454 |
] ifFalse:[
|
|
455 |
enabled ifTrue:[
|
|
456 |
view redraw
|
|
457 |
]
|
|
458 |
]
|
|
459 |
! !
|