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
|
200
|
14 |
instanceVariableNames:'enableChannel pressChannel releaseChannel pressed active entered
|
|
15 |
isTriggerOnDown autoRepeat repeatBlock initialDelay repeatDelay
|
|
16 |
pressActionBlock releaseActionBlock isToggle'
|
76
|
17 |
classVariableNames:''
|
|
18 |
poolDictionaries:''
|
126
|
19 |
category:'Interface-Support-Controllers'
|
76
|
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 |
documentation
|
|
39 |
"
|
|
40 |
ButtonControllers are used with buttons and handle all user interaction.
|
|
41 |
These are automatically created when a Button is created, therefore no manual
|
|
42 |
action is required for creation.
|
133
|
43 |
In normal applications, you dont have to care for the controller;
|
|
44 |
access to the controllers behavior is possible via messages to the button.
|
125
|
45 |
(setting actions, controlling autorepeat etc.)
|
76
|
46 |
|
133
|
47 |
See examples in the Button class.
|
|
48 |
|
76
|
49 |
Instance variables:
|
|
50 |
|
133
|
51 |
enableChannel <ValueHolder pressing is allowed (default: true)
|
|
52 |
on Boolean>
|
125
|
53 |
|
76
|
54 |
pressed <Boolean> true if currently pressed (read-only)
|
125
|
55 |
|
76
|
56 |
entered <Boolean> true if the cursor is currently in this view
|
125
|
57 |
|
118
|
58 |
isTriggerOnDown <Boolean> controls if the action should be executed on
|
76
|
59 |
press or on release (default: on release).
|
125
|
60 |
|
|
61 |
isToggle <Boolean> controls if the button should show toggle
|
|
62 |
behavior (as opposed to one-shot behavior)
|
|
63 |
|
76
|
64 |
pressActionBlock <Block> block to evaluate when pressed (default: noop)
|
125
|
65 |
|
76
|
66 |
releaseActionBlock <Block> block to evaluate when released (default: noop)
|
125
|
67 |
|
76
|
68 |
autoRepeat <Boolean> auto-repeats when pressed long enough (default: false)
|
125
|
69 |
|
76
|
70 |
initialDelay <Number> seconds till first auto-repeat (default: 0.2)
|
125
|
71 |
|
76
|
72 |
repeatDelay <Number> seconds of repeat intervall (default: 0.025)
|
125
|
73 |
|
76
|
74 |
repeatBlock <Block> block evaluated for auto-repeat (internal)
|
125
|
75 |
|
76
|
76 |
active <Boolean> true during action evaluation (internal)
|
|
77 |
"
|
200
|
78 |
!
|
|
79 |
|
|
80 |
version
|
|
81 |
^ '$Header: /cvs/stx/stx/libwidg/ButtonController.st,v 1.22 1995-11-23 10:39:34 cg Exp $'
|
76
|
82 |
! !
|
|
83 |
|
|
84 |
!ButtonController class methodsFor:'defaults'!
|
|
85 |
|
|
86 |
defaultInitialDelay
|
|
87 |
"when autorepeat is enabled, and button is not released,
|
|
88 |
start repeating after initialDelay seconds"
|
|
89 |
|
|
90 |
^ 0.2
|
200
|
91 |
!
|
|
92 |
|
|
93 |
defaultRepeatDelay
|
|
94 |
"when autorepeat is enabled, and button is not released,
|
|
95 |
repeat every repeatDelay seconds"
|
|
96 |
|
|
97 |
^ 0.025
|
76
|
98 |
! !
|
|
99 |
|
200
|
100 |
!ButtonController methodsFor:'accessing-behavior'!
|
|
101 |
|
|
102 |
action:aBlock
|
|
103 |
"convenient method: depending on the setting the triggerOnDown flag,
|
|
104 |
either set the press-action clear any release-action or
|
|
105 |
vice versa, set the release-action and clear the press-action."
|
76
|
106 |
|
200
|
107 |
isTriggerOnDown ifTrue:[
|
|
108 |
releaseActionBlock := nil.
|
|
109 |
pressActionBlock := aBlock
|
|
110 |
] ifFalse:[
|
|
111 |
releaseActionBlock := aBlock.
|
|
112 |
pressActionBlock := nil
|
|
113 |
]
|
|
114 |
!
|
118
|
115 |
|
200
|
116 |
autoRepeat
|
|
117 |
"turn on autorepeat. OBSOLETE"
|
|
118 |
|
|
119 |
autoRepeat := true.
|
|
120 |
repeatBlock := [self repeat]
|
118
|
121 |
!
|
|
122 |
|
200
|
123 |
autoRepeat:aBoolean
|
|
124 |
"turn on/off autorepeat"
|
|
125 |
|
|
126 |
autoRepeat := aBoolean.
|
|
127 |
repeatBlock := [self repeat]
|
121
|
128 |
|
200
|
129 |
"Modified: 5.9.1995 / 22:06:00 / claus"
|
|
130 |
!
|
|
131 |
|
|
132 |
beToggle
|
|
133 |
"make the receiver act like a toggle"
|
|
134 |
|
|
135 |
isTriggerOnDown := true.
|
|
136 |
isToggle := true
|
121
|
137 |
!
|
76
|
138 |
|
200
|
139 |
beTriggerOnDown
|
|
140 |
"make the receiver act on button press"
|
|
141 |
|
|
142 |
isTriggerOnDown := true
|
|
143 |
!
|
|
144 |
|
|
145 |
beTriggerOnUp
|
|
146 |
"make the receiver act on button release"
|
121
|
147 |
|
200
|
148 |
isTriggerOnDown := false
|
|
149 |
!
|
|
150 |
|
|
151 |
disable
|
|
152 |
"disable the button"
|
|
153 |
|
|
154 |
enableChannel value ifTrue:[
|
|
155 |
enableChannel value:false.
|
|
156 |
"/ view redraw - not needed; I listen to enableChannel
|
|
157 |
]
|
121
|
158 |
!
|
|
159 |
|
200
|
160 |
enable
|
|
161 |
"enable the button"
|
121
|
162 |
|
200
|
163 |
enableChannel value ifFalse:[
|
|
164 |
enableChannel value:true.
|
|
165 |
"/ view redraw - not needed; I listen to enableChannel
|
|
166 |
]
|
76
|
167 |
!
|
|
168 |
|
200
|
169 |
isTriggerOnDown
|
|
170 |
"return true, if I trigger on press
|
|
171 |
(in contrast to triggering on up, which is the default)"
|
|
172 |
|
|
173 |
^ isTriggerOnDown
|
|
174 |
!
|
|
175 |
|
|
176 |
pressAction
|
|
177 |
"return the pressAction; thats the block which gets evaluated
|
|
178 |
when the button is pressed (if non-nil)"
|
|
179 |
|
|
180 |
^ pressActionBlock
|
|
181 |
!
|
|
182 |
|
|
183 |
pressAction:aBlock
|
|
184 |
"define the action to be performed on press"
|
|
185 |
|
|
186 |
pressActionBlock := aBlock
|
128
|
187 |
!
|
|
188 |
|
200
|
189 |
releaseAction
|
|
190 |
"return the releaseAction; thats the block which gets evaluated
|
|
191 |
when the button is relreased (if non-nil)"
|
|
192 |
|
|
193 |
^ releaseActionBlock
|
|
194 |
!
|
|
195 |
|
|
196 |
releaseAction:aBlock
|
|
197 |
"define the action to be performed on release"
|
|
198 |
|
|
199 |
releaseActionBlock := aBlock
|
128
|
200 |
!
|
|
201 |
|
200
|
202 |
toggle
|
|
203 |
"toggle and perform the action"
|
177
|
204 |
|
200
|
205 |
enableChannel value ifTrue:[
|
|
206 |
self toggleNoAction.
|
|
207 |
self performAction.
|
|
208 |
view changed:#toggle with:pressed
|
|
209 |
]
|
177
|
210 |
!
|
|
211 |
|
200
|
212 |
toggleNoAction
|
|
213 |
"toggle, but do NOT perform any action"
|
|
214 |
|
|
215 |
pressed ifTrue:[
|
|
216 |
view turnOff.
|
|
217 |
pressed := false.
|
|
218 |
] ifFalse:[
|
|
219 |
view turnOn.
|
|
220 |
pressed := true.
|
177
|
221 |
].
|
200
|
222 |
!
|
|
223 |
|
|
224 |
triggerOnDown:aBoolean
|
|
225 |
"set/clear the flag which controls if the action block is to be evaluated
|
|
226 |
on press or on release.
|
|
227 |
(see also ST-80 compatibility methods beTriggerOn*)"
|
|
228 |
|
|
229 |
isTriggerOnDown := aBoolean
|
128
|
230 |
! !
|
|
231 |
|
145
|
232 |
!ButtonController methodsFor:'accessing-channels'!
|
|
233 |
|
|
234 |
enableChannel
|
|
235 |
"return the valueHolder which holdes the enable
|
|
236 |
boolean value"
|
|
237 |
|
|
238 |
^ enableChannel
|
|
239 |
!
|
|
240 |
|
|
241 |
enableChannel:aValueHolder
|
|
242 |
"set the valueHolder, which holds the enable boolean value"
|
|
243 |
|
162
|
244 |
|wasEnabled|
|
|
245 |
|
145
|
246 |
enableChannel notNil ifTrue:[
|
162
|
247 |
wasEnabled := enableChannel value.
|
145
|
248 |
enableChannel retractInterrestFor:self.
|
162
|
249 |
] ifFalse:[
|
|
250 |
wasEnabled := true
|
145
|
251 |
].
|
|
252 |
enableChannel := aValueHolder.
|
|
253 |
aValueHolder onChangeSend:#enableStateChange to:self.
|
162
|
254 |
enableChannel value ~~ wasEnabled ifTrue:[
|
|
255 |
self enableStateChange
|
145
|
256 |
]
|
162
|
257 |
|
|
258 |
"Modified: 17.9.1995 / 19:41:18 / claus"
|
145
|
259 |
!
|
|
260 |
|
|
261 |
pressChannel:aChannel
|
|
262 |
pressChannel := aChannel
|
|
263 |
!
|
|
264 |
|
|
265 |
releaseChannel:aChannel
|
|
266 |
releaseChannel := aChannel
|
|
267 |
! !
|
|
268 |
|
200
|
269 |
!ButtonController methodsFor:'accessing-state'!
|
76
|
270 |
|
200
|
271 |
active
|
|
272 |
"return true, if I am active;
|
|
273 |
that is: currently performing my action.
|
|
274 |
This query can be used to avoid multiple redraws."
|
76
|
275 |
|
200
|
276 |
^ active
|
128
|
277 |
!
|
|
278 |
|
200
|
279 |
active:aBoolean
|
|
280 |
active := aBoolean
|
76
|
281 |
!
|
|
282 |
|
200
|
283 |
enabled
|
|
284 |
"return true, if I am enabled"
|
128
|
285 |
|
200
|
286 |
^ enableChannel value
|
76
|
287 |
!
|
|
288 |
|
200
|
289 |
entered
|
|
290 |
"return true, if the mouse pointer is currently in my view"
|
155
|
291 |
|
200
|
292 |
^ entered
|
|
293 |
!
|
155
|
294 |
|
200
|
295 |
entered:aBoolean
|
|
296 |
entered := aBoolean
|
155
|
297 |
!
|
|
298 |
|
200
|
299 |
pressed
|
|
300 |
"return true, if I am pressed"
|
76
|
301 |
|
200
|
302 |
^ pressed
|
76
|
303 |
!
|
|
304 |
|
200
|
305 |
pressed:aBoolean
|
|
306 |
pressed ~~ aBoolean ifTrue:[
|
|
307 |
pressed := aBoolean.
|
|
308 |
self performAction.
|
118
|
309 |
].
|
|
310 |
!
|
|
311 |
|
200
|
312 |
setPressed:aBoolean
|
|
313 |
pressed := aBoolean.
|
121
|
314 |
|
200
|
315 |
"Created: 14.11.1995 / 21:37:08 / cg"
|
76
|
316 |
! !
|
|
317 |
|
|
318 |
!ButtonController methodsFor:'event handling'!
|
|
319 |
|
200
|
320 |
buttonMultiPress:button x:x y:y
|
|
321 |
^ self buttonPress:button x:x y:y
|
|
322 |
!
|
|
323 |
|
76
|
324 |
buttonPress:button x:x y:y
|
|
325 |
(button == 1 or:[button == #select]) ifFalse:[
|
125
|
326 |
^ super buttonPress:button x:x y:y
|
76
|
327 |
].
|
|
328 |
|
133
|
329 |
enableChannel value ifTrue:[
|
125
|
330 |
isToggle ifTrue:[
|
|
331 |
self toggle.
|
|
332 |
^ self
|
|
333 |
].
|
118
|
334 |
|
125
|
335 |
pressed ifFalse:[
|
|
336 |
pressed := true.
|
|
337 |
view showActive.
|
76
|
338 |
|
125
|
339 |
(pressActionBlock notNil or:[model notNil]) ifTrue:[
|
|
340 |
"
|
|
341 |
force output - so that button is drawn correctly in case
|
|
342 |
of any long-computation (at high priority)
|
|
343 |
"
|
130
|
344 |
view device flush.
|
125
|
345 |
].
|
76
|
346 |
|
125
|
347 |
self performAction.
|
76
|
348 |
|
125
|
349 |
autoRepeat ifTrue:[
|
|
350 |
Processor addTimedBlock:repeatBlock afterSeconds:initialDelay
|
|
351 |
]
|
|
352 |
]
|
76
|
353 |
]
|
|
354 |
!
|
|
355 |
|
|
356 |
buttonRelease:button x:x y:y
|
|
357 |
"button was released - if enabled, perform releaseaction"
|
|
358 |
|
|
359 |
(button == 1 or:[button == #select]) ifFalse:[
|
125
|
360 |
^ super buttonRelease:button x:x y:y
|
76
|
361 |
].
|
118
|
362 |
|
|
363 |
isToggle ifTrue:[
|
125
|
364 |
^ self
|
118
|
365 |
].
|
|
366 |
|
76
|
367 |
pressed ifTrue:[
|
125
|
368 |
autoRepeat ifTrue:[
|
|
369 |
Processor removeTimedBlock:repeatBlock
|
|
370 |
].
|
|
371 |
pressed := false.
|
|
372 |
view showPassive.
|
76
|
373 |
|
133
|
374 |
enableChannel value ifTrue:[
|
125
|
375 |
"
|
|
376 |
only perform action if released within myself
|
|
377 |
"
|
|
378 |
((x >= 0)
|
|
379 |
and:[x <= view width
|
|
380 |
and:[y >= 0
|
|
381 |
and:[y <= view height]]]) ifTrue:[
|
|
382 |
(releaseActionBlock notNil or:[model notNil]) ifTrue:[
|
|
383 |
"
|
|
384 |
force output - so that button is drawn correctly in case
|
|
385 |
of any long-computation (at high priority)
|
|
386 |
"
|
130
|
387 |
view device flush.
|
125
|
388 |
].
|
76
|
389 |
|
125
|
390 |
self performAction.
|
|
391 |
]
|
|
392 |
]
|
76
|
393 |
]
|
|
394 |
!
|
|
395 |
|
200
|
396 |
enableStateChange
|
|
397 |
"this is sent, whenever the enable value has changed"
|
76
|
398 |
|
200
|
399 |
view notNil ifTrue:[view enableStateChangeRedraw]
|
|
400 |
|
|
401 |
"Modified: 17.9.1995 / 19:55:52 / claus"
|
76
|
402 |
!
|
|
403 |
|
200
|
404 |
keyPress:key x:x y:y
|
|
405 |
"trigger on Return and space, if I am the focusView of my group
|
|
406 |
(i.e. if I got an explicit focus)"
|
121
|
407 |
|
200
|
408 |
(key == #Return or:[key == Character space]) ifTrue:[
|
|
409 |
view hasFocus ifTrue:[
|
|
410 |
"just simulate a buttonPress/release here."
|
|
411 |
self buttonPress:1 x:0 y:0.
|
|
412 |
self buttonRelease:1 x:0 y:0.
|
|
413 |
^ self.
|
121
|
414 |
]
|
200
|
415 |
].
|
|
416 |
view keyPress:key x:x y:y
|
121
|
417 |
!
|
|
418 |
|
|
419 |
performAction
|
146
|
420 |
|action value|
|
133
|
421 |
|
|
422 |
isToggle ifTrue:[
|
|
423 |
value := pressed
|
|
424 |
] ifFalse:[
|
|
425 |
value := true
|
|
426 |
].
|
76
|
427 |
|
125
|
428 |
"
|
145
|
429 |
ST/X style actionBlock evaluation & channel notification ...
|
125
|
430 |
"
|
76
|
431 |
pressed ifTrue:[
|
145
|
432 |
action := pressActionBlock.
|
121
|
433 |
] ifFalse:[
|
145
|
434 |
action := releaseActionBlock.
|
121
|
435 |
].
|
125
|
436 |
action notNil ifTrue:[
|
|
437 |
active := true.
|
|
438 |
action numArgs == 0 ifTrue:[
|
|
439 |
action value
|
|
440 |
] ifFalse:[
|
133
|
441 |
action value:value
|
125
|
442 |
].
|
|
443 |
active := false.
|
|
444 |
].
|
146
|
445 |
pressChannel notNil ifTrue:[
|
|
446 |
pressChannel value:pressed
|
|
447 |
].
|
|
448 |
releaseChannel notNil ifTrue:[
|
|
449 |
releaseChannel value:pressed not
|
145
|
450 |
].
|
76
|
451 |
|
125
|
452 |
"
|
|
453 |
ST-80 style model notification ...
|
|
454 |
"
|
121
|
455 |
(isToggle
|
|
456 |
or:[(isTriggerOnDown and:[pressed])
|
|
457 |
or:[isTriggerOnDown not and:[pressed not]]]) ifTrue:[
|
125
|
458 |
"the ST-80 way of doing things"
|
|
459 |
view notNil ifTrue:[
|
|
460 |
active := true.
|
133
|
461 |
view sendChangeMessageWith:value.
|
125
|
462 |
active := false.
|
|
463 |
].
|
121
|
464 |
].
|
76
|
465 |
!
|
|
466 |
|
200
|
467 |
pointerEnter:state x:x y:y
|
|
468 |
"mouse pointer entered my view.
|
|
469 |
Redraw with enteredColors if they differ from the normal colors"
|
|
470 |
|
|
471 |
entered := true.
|
|
472 |
enableChannel value ifTrue:[
|
|
473 |
pressed ifTrue:[
|
|
474 |
"
|
|
475 |
reentered after a leave with mouse-button down;
|
|
476 |
restart autorepeating and/or if I am a button with
|
|
477 |
triggerOnDown, show active again.
|
|
478 |
"
|
|
479 |
autoRepeat ifTrue:[
|
|
480 |
Processor addTimedBlock:repeatBlock afterSeconds:initialDelay
|
|
481 |
].
|
|
482 |
isTriggerOnDown ifFalse:[
|
|
483 |
view showActive.
|
|
484 |
]
|
|
485 |
] ifFalse:[
|
|
486 |
view redraw
|
|
487 |
]
|
|
488 |
]
|
76
|
489 |
!
|
|
490 |
|
200
|
491 |
pointerLeave:state
|
|
492 |
"mouse pointer left my view.
|
|
493 |
Redraw with normal colors if they differ from enteredColors"
|
76
|
494 |
|
200
|
495 |
entered := false.
|
|
496 |
pressed ifTrue:[
|
|
497 |
"
|
|
498 |
leave with mouse-button down;
|
|
499 |
stop autorepeating and/or if I am a button with
|
|
500 |
action on release, show passive
|
|
501 |
"
|
|
502 |
autoRepeat ifTrue:[
|
|
503 |
Processor removeTimedBlock:repeatBlock
|
|
504 |
].
|
|
505 |
isTriggerOnDown ifFalse:[
|
|
506 |
view showPassive.
|
76
|
507 |
]
|
200
|
508 |
] ifFalse:[
|
|
509 |
enableChannel value ifTrue:[
|
|
510 |
view redraw
|
|
511 |
]
|
|
512 |
]
|
76
|
513 |
!
|
|
514 |
|
121
|
515 |
repeat
|
|
516 |
"this is sent from the autorepeat-block, when the button has been pressed long
|
|
517 |
enough; it simulates a release-press, by evaluating both release
|
|
518 |
and press actions."
|
76
|
519 |
|
|
520 |
pressed ifTrue:[
|
133
|
521 |
enableChannel value ifTrue:[
|
121
|
522 |
active ifFalse:[
|
155
|
523 |
self performAction.
|
|
524 |
"/ active := true.
|
|
525 |
"/ releaseActionBlock notNil ifTrue:[releaseActionBlock value].
|
|
526 |
"/ pressActionBlock notNil ifTrue:[pressActionBlock value].
|
|
527 |
"/ active := false.
|
121
|
528 |
|
|
529 |
autoRepeat ifTrue:[
|
|
530 |
Processor addTimedBlock:repeatBlock afterSeconds:repeatDelay
|
|
531 |
]
|
|
532 |
]
|
76
|
533 |
]
|
|
534 |
]
|
|
535 |
! !
|
121
|
536 |
|
|
537 |
!ButtonController methodsFor:'initialization'!
|
|
538 |
|
|
539 |
initialize
|
|
540 |
super initialize.
|
|
541 |
|
133
|
542 |
self enableChannel:(true asValue).
|
|
543 |
|
121
|
544 |
active := false.
|
|
545 |
pressed := false.
|
|
546 |
entered := false.
|
|
547 |
autoRepeat := false.
|
|
548 |
initialDelay := self class defaultInitialDelay.
|
|
549 |
repeatDelay := self class defaultRepeatDelay.
|
|
550 |
isTriggerOnDown := false.
|
|
551 |
isToggle := false.
|
|
552 |
! !
|
200
|
553 |
|