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