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