author | Claus Gittinger <cg@exept.de> |
Sat, 16 Dec 1995 17:38:02 +0100 | |
changeset 262 | d2c946b6a5b4 |
parent 258 | ae4b8f1a6738 |
child 266 | 8b8e9c60fa1d |
permissions | -rw-r--r-- |
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 |
|
262 | 14 |
instanceVariableNames:'enableChannel pressChannel releaseChannel pressed active entered |
220
635d3f583f61
fast repeat if shift is pressed
Claus Gittinger <cg@exept.de>
parents:
203
diff
changeset
|
15 |
isTriggerOnDown autoRepeat repeatBlock initialDelay repeatDelay |
635d3f583f61
fast repeat if shift is pressed
Claus Gittinger <cg@exept.de>
parents:
203
diff
changeset
|
16 |
pressActionBlock releaseActionBlock isToggle' |
262 | 17 |
classVariableNames:'' |
18 |
poolDictionaries:'' |
|
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 |
" |
|
78 |
! ! |
|
79 |
||
80 |
!ButtonController class methodsFor:'defaults'! |
|
81 |
||
82 |
defaultInitialDelay |
|
83 |
"when autorepeat is enabled, and button is not released, |
|
84 |
start repeating after initialDelay seconds" |
|
85 |
||
86 |
^ 0.2 |
|
200 | 87 |
! |
88 |
||
89 |
defaultRepeatDelay |
|
90 |
"when autorepeat is enabled, and button is not released, |
|
91 |
repeat every repeatDelay seconds" |
|
92 |
||
93 |
^ 0.025 |
|
76 | 94 |
! ! |
95 |
||
200 | 96 |
!ButtonController methodsFor:'accessing-behavior'! |
97 |
||
98 |
action:aBlock |
|
99 |
"convenient method: depending on the setting the triggerOnDown flag, |
|
100 |
either set the press-action clear any release-action or |
|
101 |
vice versa, set the release-action and clear the press-action." |
|
76 | 102 |
|
200 | 103 |
isTriggerOnDown ifTrue:[ |
104 |
releaseActionBlock := nil. |
|
105 |
pressActionBlock := aBlock |
|
106 |
] ifFalse:[ |
|
107 |
releaseActionBlock := aBlock. |
|
108 |
pressActionBlock := nil |
|
109 |
] |
|
110 |
! |
|
118 | 111 |
|
200 | 112 |
autoRepeat |
113 |
"turn on autorepeat. OBSOLETE" |
|
114 |
||
115 |
autoRepeat := true. |
|
116 |
repeatBlock := [self repeat] |
|
118 | 117 |
! |
118 |
||
200 | 119 |
autoRepeat:aBoolean |
120 |
"turn on/off autorepeat" |
|
121 |
||
122 |
autoRepeat := aBoolean. |
|
123 |
repeatBlock := [self repeat] |
|
121 | 124 |
|
200 | 125 |
"Modified: 5.9.1995 / 22:06:00 / claus" |
126 |
! |
|
127 |
||
128 |
beToggle |
|
129 |
"make the receiver act like a toggle" |
|
130 |
||
131 |
isTriggerOnDown := true. |
|
132 |
isToggle := true |
|
121 | 133 |
! |
76 | 134 |
|
200 | 135 |
beTriggerOnDown |
136 |
"make the receiver act on button press" |
|
137 |
||
138 |
isTriggerOnDown := true |
|
139 |
! |
|
140 |
||
141 |
beTriggerOnUp |
|
142 |
"make the receiver act on button release" |
|
121 | 143 |
|
200 | 144 |
isTriggerOnDown := false |
145 |
! |
|
146 |
||
147 |
disable |
|
148 |
"disable the button" |
|
149 |
||
150 |
enableChannel value ifTrue:[ |
|
151 |
enableChannel value:false. |
|
152 |
"/ view redraw - not needed; I listen to enableChannel |
|
153 |
] |
|
121 | 154 |
! |
155 |
||
200 | 156 |
enable |
157 |
"enable the button" |
|
121 | 158 |
|
200 | 159 |
enableChannel value ifFalse:[ |
160 |
enableChannel value:true. |
|
161 |
"/ view redraw - not needed; I listen to enableChannel |
|
162 |
] |
|
76 | 163 |
! |
164 |
||
200 | 165 |
isTriggerOnDown |
166 |
"return true, if I trigger on press |
|
167 |
(in contrast to triggering on up, which is the default)" |
|
168 |
||
169 |
^ isTriggerOnDown |
|
170 |
! |
|
171 |
||
172 |
pressAction |
|
173 |
"return the pressAction; thats the block which gets evaluated |
|
174 |
when the button is pressed (if non-nil)" |
|
175 |
||
176 |
^ pressActionBlock |
|
177 |
! |
|
178 |
||
179 |
pressAction:aBlock |
|
180 |
"define the action to be performed on press" |
|
181 |
||
182 |
pressActionBlock := aBlock |
|
128 | 183 |
! |
184 |
||
200 | 185 |
releaseAction |
186 |
"return the releaseAction; thats the block which gets evaluated |
|
187 |
when the button is relreased (if non-nil)" |
|
188 |
||
189 |
^ releaseActionBlock |
|
190 |
! |
|
191 |
||
192 |
releaseAction:aBlock |
|
193 |
"define the action to be performed on release" |
|
194 |
||
195 |
releaseActionBlock := aBlock |
|
128 | 196 |
! |
197 |
||
200 | 198 |
toggle |
199 |
"toggle and perform the action" |
|
177 | 200 |
|
200 | 201 |
enableChannel value ifTrue:[ |
202 |
self toggleNoAction. |
|
203 |
self performAction. |
|
204 |
view changed:#toggle with:pressed |
|
205 |
] |
|
177 | 206 |
! |
207 |
||
200 | 208 |
toggleNoAction |
209 |
"toggle, but do NOT perform any action" |
|
210 |
||
211 |
pressed ifTrue:[ |
|
212 |
view turnOff. |
|
213 |
pressed := false. |
|
214 |
] ifFalse:[ |
|
215 |
view turnOn. |
|
216 |
pressed := true. |
|
177 | 217 |
]. |
200 | 218 |
! |
219 |
||
220 |
triggerOnDown:aBoolean |
|
221 |
"set/clear the flag which controls if the action block is to be evaluated |
|
222 |
on press or on release. |
|
223 |
(see also ST-80 compatibility methods beTriggerOn*)" |
|
224 |
||
225 |
isTriggerOnDown := aBoolean |
|
128 | 226 |
! ! |
227 |
||
145 | 228 |
!ButtonController methodsFor:'accessing-channels'! |
229 |
||
230 |
enableChannel |
|
231 |
"return the valueHolder which holdes the enable |
|
232 |
boolean value" |
|
233 |
||
234 |
^ enableChannel |
|
235 |
! |
|
236 |
||
237 |
enableChannel:aValueHolder |
|
238 |
"set the valueHolder, which holds the enable boolean value" |
|
239 |
||
162 | 240 |
|wasEnabled| |
241 |
||
145 | 242 |
enableChannel notNil ifTrue:[ |
162 | 243 |
wasEnabled := enableChannel value. |
258
ae4b8f1a6738
interest is written with one 'r' (shame on me)
Claus Gittinger <cg@exept.de>
parents:
220
diff
changeset
|
244 |
enableChannel retractInterestsFor:self. |
162 | 245 |
] ifFalse:[ |
246 |
wasEnabled := true |
|
145 | 247 |
]. |
248 |
enableChannel := aValueHolder. |
|
249 |
aValueHolder onChangeSend:#enableStateChange to:self. |
|
162 | 250 |
enableChannel value ~~ wasEnabled ifTrue:[ |
251 |
self enableStateChange |
|
145 | 252 |
] |
162 | 253 |
|
254 |
"Modified: 17.9.1995 / 19:41:18 / claus" |
|
145 | 255 |
! |
256 |
||
257 |
pressChannel:aChannel |
|
258 |
pressChannel := aChannel |
|
259 |
! |
|
260 |
||
261 |
releaseChannel:aChannel |
|
262 |
releaseChannel := aChannel |
|
263 |
! ! |
|
264 |
||
200 | 265 |
!ButtonController methodsFor:'accessing-state'! |
76 | 266 |
|
200 | 267 |
active |
268 |
"return true, if I am active; |
|
269 |
that is: currently performing my action. |
|
270 |
This query can be used to avoid multiple redraws." |
|
76 | 271 |
|
200 | 272 |
^ active |
128 | 273 |
! |
274 |
||
200 | 275 |
active:aBoolean |
276 |
active := aBoolean |
|
76 | 277 |
! |
278 |
||
200 | 279 |
enabled |
280 |
"return true, if I am enabled" |
|
128 | 281 |
|
200 | 282 |
^ enableChannel value |
76 | 283 |
! |
284 |
||
200 | 285 |
entered |
286 |
"return true, if the mouse pointer is currently in my view" |
|
155 | 287 |
|
200 | 288 |
^ entered |
289 |
! |
|
155 | 290 |
|
200 | 291 |
entered:aBoolean |
292 |
entered := aBoolean |
|
155 | 293 |
! |
294 |
||
200 | 295 |
pressed |
296 |
"return true, if I am pressed" |
|
76 | 297 |
|
200 | 298 |
^ pressed |
76 | 299 |
! |
300 |
||
200 | 301 |
pressed:aBoolean |
302 |
pressed ~~ aBoolean ifTrue:[ |
|
303 |
pressed := aBoolean. |
|
304 |
self performAction. |
|
118 | 305 |
]. |
306 |
! |
|
307 |
||
200 | 308 |
setPressed:aBoolean |
309 |
pressed := aBoolean. |
|
121 | 310 |
|
200 | 311 |
"Created: 14.11.1995 / 21:37:08 / cg" |
76 | 312 |
! ! |
313 |
||
314 |
!ButtonController methodsFor:'event handling'! |
|
315 |
||
200 | 316 |
buttonMultiPress:button x:x y:y |
317 |
^ self buttonPress:button x:x y:y |
|
318 |
! |
|
319 |
||
76 | 320 |
buttonPress:button x:x y:y |
321 |
(button == 1 or:[button == #select]) ifFalse:[ |
|
262 | 322 |
^ super buttonPress:button x:x y:y |
76 | 323 |
]. |
324 |
||
133 | 325 |
enableChannel value ifTrue:[ |
262 | 326 |
isToggle ifTrue:[ |
327 |
self toggle. |
|
328 |
^ self |
|
329 |
]. |
|
118 | 330 |
|
262 | 331 |
pressed ifFalse:[ |
332 |
pressed := true. |
|
333 |
view showActive. |
|
76 | 334 |
|
262 | 335 |
(pressActionBlock notNil or:[model notNil]) ifTrue:[ |
336 |
" |
|
337 |
force output - so that button is drawn correctly in case |
|
338 |
of any long-computation (at high priority) |
|
339 |
" |
|
340 |
view flush. |
|
341 |
]. |
|
76 | 342 |
|
262 | 343 |
self performAction. |
76 | 344 |
|
262 | 345 |
autoRepeat ifTrue:[ |
346 |
Processor addTimedBlock:repeatBlock afterSeconds:initialDelay |
|
347 |
] |
|
348 |
] |
|
76 | 349 |
] |
262 | 350 |
|
351 |
"Modified: 16.12.1995 / 17:31:55 / cg" |
|
76 | 352 |
! |
353 |
||
354 |
buttonRelease:button x:x y:y |
|
355 |
"button was released - if enabled, perform releaseaction" |
|
356 |
||
357 |
(button == 1 or:[button == #select]) ifFalse:[ |
|
262 | 358 |
^ super buttonRelease:button x:x y:y |
76 | 359 |
]. |
118 | 360 |
|
361 |
isToggle ifTrue:[ |
|
262 | 362 |
^ self |
118 | 363 |
]. |
364 |
||
76 | 365 |
pressed ifTrue:[ |
262 | 366 |
autoRepeat ifTrue:[ |
367 |
Processor removeTimedBlock:repeatBlock |
|
368 |
]. |
|
369 |
pressed := false. |
|
370 |
view showPassive. |
|
76 | 371 |
|
262 | 372 |
enableChannel value ifTrue:[ |
373 |
" |
|
374 |
only perform action if released within myself |
|
375 |
" |
|
376 |
((x >= 0) |
|
377 |
and:[x <= view width |
|
378 |
and:[y >= 0 |
|
379 |
and:[y <= view height]]]) ifTrue:[ |
|
380 |
(releaseActionBlock notNil or:[model notNil]) ifTrue:[ |
|
381 |
" |
|
382 |
force output - so that button is drawn correctly in case |
|
383 |
of any long-computation (at high priority) |
|
384 |
" |
|
385 |
view flush. |
|
386 |
]. |
|
76 | 387 |
|
262 | 388 |
self performAction. |
389 |
] |
|
390 |
] |
|
76 | 391 |
] |
262 | 392 |
|
393 |
"Modified: 16.12.1995 / 17:32:09 / cg" |
|
76 | 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 |
|
220
635d3f583f61
fast repeat if shift is pressed
Claus Gittinger <cg@exept.de>
parents:
203
diff
changeset
|
520 |
|dly| |
635d3f583f61
fast repeat if shift is pressed
Claus Gittinger <cg@exept.de>
parents:
203
diff
changeset
|
521 |
|
76 | 522 |
pressed ifTrue:[ |
133 | 523 |
enableChannel value ifTrue:[ |
121 | 524 |
active ifFalse:[ |
155 | 525 |
self performAction. |
526 |
"/ active := true. |
|
527 |
"/ releaseActionBlock notNil ifTrue:[releaseActionBlock value]. |
|
528 |
"/ pressActionBlock notNil ifTrue:[pressActionBlock value]. |
|
529 |
"/ active := false. |
|
121 | 530 |
|
531 |
autoRepeat ifTrue:[ |
|
220
635d3f583f61
fast repeat if shift is pressed
Claus Gittinger <cg@exept.de>
parents:
203
diff
changeset
|
532 |
self sensor shiftDown ifTrue:[ |
635d3f583f61
fast repeat if shift is pressed
Claus Gittinger <cg@exept.de>
parents:
203
diff
changeset
|
533 |
dly := repeatDelay / 4. |
635d3f583f61
fast repeat if shift is pressed
Claus Gittinger <cg@exept.de>
parents:
203
diff
changeset
|
534 |
] ifFalse:[ |
635d3f583f61
fast repeat if shift is pressed
Claus Gittinger <cg@exept.de>
parents:
203
diff
changeset
|
535 |
dly := repeatDelay |
635d3f583f61
fast repeat if shift is pressed
Claus Gittinger <cg@exept.de>
parents:
203
diff
changeset
|
536 |
]. |
635d3f583f61
fast repeat if shift is pressed
Claus Gittinger <cg@exept.de>
parents:
203
diff
changeset
|
537 |
Processor addTimedBlock:repeatBlock afterSeconds:dly |
121 | 538 |
] |
539 |
] |
|
76 | 540 |
] |
541 |
] |
|
542 |
! ! |
|
121 | 543 |
|
544 |
!ButtonController methodsFor:'initialization'! |
|
545 |
||
546 |
initialize |
|
547 |
super initialize. |
|
548 |
||
133 | 549 |
self enableChannel:(true asValue). |
550 |
||
121 | 551 |
active := false. |
552 |
pressed := false. |
|
553 |
entered := false. |
|
554 |
autoRepeat := false. |
|
555 |
initialDelay := self class defaultInitialDelay. |
|
556 |
repeatDelay := self class defaultRepeatDelay. |
|
557 |
isTriggerOnDown := false. |
|
558 |
isToggle := false. |
|
559 |
! ! |
|
200 | 560 |
|
203 | 561 |
!ButtonController class methodsFor:'documentation'! |
562 |
||
563 |
version |
|
262 | 564 |
^ '$Header: /cvs/stx/stx/libwidg/ButtonController.st,v 1.26 1995-12-16 16:37:04 cg Exp $' |
203 | 565 |
! ! |