author | Claus Gittinger <cg@exept.de> |
Fri, 15 Dec 1995 14:04:06 +0100 | |
changeset 258 | ae4b8f1a6738 |
parent 220 | 635d3f583f61 |
child 262 | d2c946b6a5b4 |
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 |
|
200 | 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' |
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 |
" |
|
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:[ |
|
125 | 322 |
^ super buttonPress:button x:x y:y |
76 | 323 |
]. |
324 |
||
133 | 325 |
enableChannel value ifTrue:[ |
125 | 326 |
isToggle ifTrue:[ |
327 |
self toggle. |
|
328 |
^ self |
|
329 |
]. |
|
118 | 330 |
|
125 | 331 |
pressed ifFalse:[ |
332 |
pressed := true. |
|
333 |
view showActive. |
|
76 | 334 |
|
125 | 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 |
" |
|
130 | 340 |
view device flush. |
125 | 341 |
]. |
76 | 342 |
|
125 | 343 |
self performAction. |
76 | 344 |
|
125 | 345 |
autoRepeat ifTrue:[ |
346 |
Processor addTimedBlock:repeatBlock afterSeconds:initialDelay |
|
347 |
] |
|
348 |
] |
|
76 | 349 |
] |
350 |
! |
|
351 |
||
352 |
buttonRelease:button x:x y:y |
|
353 |
"button was released - if enabled, perform releaseaction" |
|
354 |
||
355 |
(button == 1 or:[button == #select]) ifFalse:[ |
|
125 | 356 |
^ super buttonRelease:button x:x y:y |
76 | 357 |
]. |
118 | 358 |
|
359 |
isToggle ifTrue:[ |
|
125 | 360 |
^ self |
118 | 361 |
]. |
362 |
||
76 | 363 |
pressed ifTrue:[ |
125 | 364 |
autoRepeat ifTrue:[ |
365 |
Processor removeTimedBlock:repeatBlock |
|
366 |
]. |
|
367 |
pressed := false. |
|
368 |
view showPassive. |
|
76 | 369 |
|
133 | 370 |
enableChannel value ifTrue:[ |
125 | 371 |
" |
372 |
only perform action if released within myself |
|
373 |
" |
|
374 |
((x >= 0) |
|
375 |
and:[x <= view width |
|
376 |
and:[y >= 0 |
|
377 |
and:[y <= view height]]]) ifTrue:[ |
|
378 |
(releaseActionBlock notNil or:[model notNil]) ifTrue:[ |
|
379 |
" |
|
380 |
force output - so that button is drawn correctly in case |
|
381 |
of any long-computation (at high priority) |
|
382 |
" |
|
130 | 383 |
view device flush. |
125 | 384 |
]. |
76 | 385 |
|
125 | 386 |
self performAction. |
387 |
] |
|
388 |
] |
|
76 | 389 |
] |
390 |
! |
|
391 |
||
200 | 392 |
enableStateChange |
393 |
"this is sent, whenever the enable value has changed" |
|
76 | 394 |
|
200 | 395 |
view notNil ifTrue:[view enableStateChangeRedraw] |
396 |
||
397 |
"Modified: 17.9.1995 / 19:55:52 / claus" |
|
76 | 398 |
! |
399 |
||
200 | 400 |
keyPress:key x:x y:y |
401 |
"trigger on Return and space, if I am the focusView of my group |
|
402 |
(i.e. if I got an explicit focus)" |
|
121 | 403 |
|
200 | 404 |
(key == #Return or:[key == Character space]) ifTrue:[ |
405 |
view hasFocus ifTrue:[ |
|
406 |
"just simulate a buttonPress/release here." |
|
407 |
self buttonPress:1 x:0 y:0. |
|
408 |
self buttonRelease:1 x:0 y:0. |
|
409 |
^ self. |
|
121 | 410 |
] |
200 | 411 |
]. |
412 |
view keyPress:key x:x y:y |
|
121 | 413 |
! |
414 |
||
415 |
performAction |
|
146 | 416 |
|action value| |
133 | 417 |
|
418 |
isToggle ifTrue:[ |
|
419 |
value := pressed |
|
420 |
] ifFalse:[ |
|
421 |
value := true |
|
422 |
]. |
|
76 | 423 |
|
125 | 424 |
" |
145 | 425 |
ST/X style actionBlock evaluation & channel notification ... |
125 | 426 |
" |
76 | 427 |
pressed ifTrue:[ |
145 | 428 |
action := pressActionBlock. |
121 | 429 |
] ifFalse:[ |
145 | 430 |
action := releaseActionBlock. |
121 | 431 |
]. |
125 | 432 |
action notNil ifTrue:[ |
433 |
active := true. |
|
434 |
action numArgs == 0 ifTrue:[ |
|
435 |
action value |
|
436 |
] ifFalse:[ |
|
133 | 437 |
action value:value |
125 | 438 |
]. |
439 |
active := false. |
|
440 |
]. |
|
146 | 441 |
pressChannel notNil ifTrue:[ |
442 |
pressChannel value:pressed |
|
443 |
]. |
|
444 |
releaseChannel notNil ifTrue:[ |
|
445 |
releaseChannel value:pressed not |
|
145 | 446 |
]. |
76 | 447 |
|
125 | 448 |
" |
449 |
ST-80 style model notification ... |
|
450 |
" |
|
121 | 451 |
(isToggle |
452 |
or:[(isTriggerOnDown and:[pressed]) |
|
453 |
or:[isTriggerOnDown not and:[pressed not]]]) ifTrue:[ |
|
125 | 454 |
"the ST-80 way of doing things" |
455 |
view notNil ifTrue:[ |
|
456 |
active := true. |
|
133 | 457 |
view sendChangeMessageWith:value. |
125 | 458 |
active := false. |
459 |
]. |
|
121 | 460 |
]. |
76 | 461 |
! |
462 |
||
200 | 463 |
pointerEnter:state x:x y:y |
464 |
"mouse pointer entered my view. |
|
465 |
Redraw with enteredColors if they differ from the normal colors" |
|
466 |
||
467 |
entered := true. |
|
468 |
enableChannel value ifTrue:[ |
|
469 |
pressed ifTrue:[ |
|
470 |
" |
|
471 |
reentered after a leave with mouse-button down; |
|
472 |
restart autorepeating and/or if I am a button with |
|
473 |
triggerOnDown, show active again. |
|
474 |
" |
|
475 |
autoRepeat ifTrue:[ |
|
476 |
Processor addTimedBlock:repeatBlock afterSeconds:initialDelay |
|
477 |
]. |
|
478 |
isTriggerOnDown ifFalse:[ |
|
479 |
view showActive. |
|
480 |
] |
|
481 |
] ifFalse:[ |
|
482 |
view redraw |
|
483 |
] |
|
484 |
] |
|
76 | 485 |
! |
486 |
||
200 | 487 |
pointerLeave:state |
488 |
"mouse pointer left my view. |
|
489 |
Redraw with normal colors if they differ from enteredColors" |
|
76 | 490 |
|
200 | 491 |
entered := false. |
492 |
pressed ifTrue:[ |
|
493 |
" |
|
494 |
leave with mouse-button down; |
|
495 |
stop autorepeating and/or if I am a button with |
|
496 |
action on release, show passive |
|
497 |
" |
|
498 |
autoRepeat ifTrue:[ |
|
499 |
Processor removeTimedBlock:repeatBlock |
|
500 |
]. |
|
501 |
isTriggerOnDown ifFalse:[ |
|
502 |
view showPassive. |
|
76 | 503 |
] |
200 | 504 |
] ifFalse:[ |
505 |
enableChannel value ifTrue:[ |
|
506 |
view redraw |
|
507 |
] |
|
508 |
] |
|
76 | 509 |
! |
510 |
||
121 | 511 |
repeat |
512 |
"this is sent from the autorepeat-block, when the button has been pressed long |
|
513 |
enough; it simulates a release-press, by evaluating both release |
|
514 |
and press actions." |
|
76 | 515 |
|
220
635d3f583f61
fast repeat if shift is pressed
Claus Gittinger <cg@exept.de>
parents:
203
diff
changeset
|
516 |
|dly| |
635d3f583f61
fast repeat if shift is pressed
Claus Gittinger <cg@exept.de>
parents:
203
diff
changeset
|
517 |
|
76 | 518 |
pressed ifTrue:[ |
133 | 519 |
enableChannel value ifTrue:[ |
121 | 520 |
active ifFalse:[ |
155 | 521 |
self performAction. |
522 |
"/ active := true. |
|
523 |
"/ releaseActionBlock notNil ifTrue:[releaseActionBlock value]. |
|
524 |
"/ pressActionBlock notNil ifTrue:[pressActionBlock value]. |
|
525 |
"/ active := false. |
|
121 | 526 |
|
527 |
autoRepeat ifTrue:[ |
|
220
635d3f583f61
fast repeat if shift is pressed
Claus Gittinger <cg@exept.de>
parents:
203
diff
changeset
|
528 |
self sensor shiftDown ifTrue:[ |
635d3f583f61
fast repeat if shift is pressed
Claus Gittinger <cg@exept.de>
parents:
203
diff
changeset
|
529 |
dly := repeatDelay / 4. |
635d3f583f61
fast repeat if shift is pressed
Claus Gittinger <cg@exept.de>
parents:
203
diff
changeset
|
530 |
] ifFalse:[ |
635d3f583f61
fast repeat if shift is pressed
Claus Gittinger <cg@exept.de>
parents:
203
diff
changeset
|
531 |
dly := repeatDelay |
635d3f583f61
fast repeat if shift is pressed
Claus Gittinger <cg@exept.de>
parents:
203
diff
changeset
|
532 |
]. |
635d3f583f61
fast repeat if shift is pressed
Claus Gittinger <cg@exept.de>
parents:
203
diff
changeset
|
533 |
Processor addTimedBlock:repeatBlock afterSeconds:dly |
121 | 534 |
] |
535 |
] |
|
76 | 536 |
] |
537 |
] |
|
538 |
! ! |
|
121 | 539 |
|
540 |
!ButtonController methodsFor:'initialization'! |
|
541 |
||
542 |
initialize |
|
543 |
super initialize. |
|
544 |
||
133 | 545 |
self enableChannel:(true asValue). |
546 |
||
121 | 547 |
active := false. |
548 |
pressed := false. |
|
549 |
entered := false. |
|
550 |
autoRepeat := false. |
|
551 |
initialDelay := self class defaultInitialDelay. |
|
552 |
repeatDelay := self class defaultRepeatDelay. |
|
553 |
isTriggerOnDown := false. |
|
554 |
isToggle := false. |
|
555 |
! ! |
|
200 | 556 |
|
203 | 557 |
!ButtonController class methodsFor:'documentation'! |
558 |
||
559 |
version |
|
258
ae4b8f1a6738
interest is written with one 'r' (shame on me)
Claus Gittinger <cg@exept.de>
parents:
220
diff
changeset
|
560 |
^ '$Header: /cvs/stx/stx/libwidg/ButtonController.st,v 1.25 1995-12-15 13:03:59 cg Exp $' |
203 | 561 |
! ! |