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 |
"
|
146
|
43 |
$Header: /cvs/stx/stx/libwidg/ButtonController.st,v 1.15 1995-08-27 00:33:47 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 |
|
|
157 |
enableChannel notNil ifTrue:[
|
|
158 |
enableChannel retractInterrestFor:self.
|
|
159 |
].
|
|
160 |
enableChannel := aValueHolder.
|
|
161 |
aValueHolder onChangeSend:#enableStateChange to:self.
|
|
162 |
view notNil ifTrue:[
|
|
163 |
view redraw
|
|
164 |
]
|
|
165 |
!
|
|
166 |
|
|
167 |
pressChannel:aChannel
|
|
168 |
pressChannel := aChannel
|
|
169 |
!
|
|
170 |
|
|
171 |
releaseChannel:aChannel
|
|
172 |
releaseChannel := aChannel
|
|
173 |
! !
|
|
174 |
|
128
|
175 |
!ButtonController methodsFor:'accessing-behavior'!
|
|
176 |
|
76
|
177 |
beTriggerOnDown
|
|
178 |
"make the receiver act on button press"
|
|
179 |
|
118
|
180 |
isTriggerOnDown := true
|
76
|
181 |
!
|
|
182 |
|
128
|
183 |
beTriggerOnUp
|
|
184 |
"make the receiver act on button release"
|
|
185 |
|
|
186 |
isTriggerOnDown := false
|
|
187 |
!
|
|
188 |
|
|
189 |
triggerOnDown:aBoolean
|
|
190 |
"set/clear the flag which controls if the action block is to be evaluated
|
|
191 |
on press or on release.
|
|
192 |
(see also ST-80 compatibility methods beTriggerOn*)"
|
|
193 |
|
|
194 |
isTriggerOnDown := aBoolean
|
76
|
195 |
!
|
|
196 |
|
|
197 |
isTriggerOnDown
|
88
|
198 |
"return true, if I trigger on press
|
|
199 |
(in contrast to triggering on up, which is the default)"
|
|
200 |
|
118
|
201 |
^ isTriggerOnDown
|
76
|
202 |
!
|
|
203 |
|
128
|
204 |
beToggle
|
|
205 |
"make the receiver act like a toggle"
|
|
206 |
|
|
207 |
isTriggerOnDown := true.
|
|
208 |
isToggle := true
|
|
209 |
!
|
|
210 |
|
|
211 |
autoRepeat
|
|
212 |
"turn on autorepeat"
|
|
213 |
|
|
214 |
autoRepeat := true.
|
|
215 |
repeatBlock := [self repeat]
|
76
|
216 |
!
|
|
217 |
|
|
218 |
action:aBlock
|
|
219 |
"convenient method: depending on the setting the triggerOnDown flag,
|
|
220 |
either set the press-action clear any release-action or
|
|
221 |
vice versa, set the release-action and clear the press-action."
|
|
222 |
|
118
|
223 |
isTriggerOnDown ifTrue:[
|
76
|
224 |
releaseActionBlock := nil.
|
|
225 |
pressActionBlock := aBlock
|
|
226 |
] ifFalse:[
|
|
227 |
releaseActionBlock := aBlock.
|
|
228 |
pressActionBlock := nil
|
|
229 |
]
|
|
230 |
!
|
|
231 |
|
121
|
232 |
pressAction:aBlock
|
|
233 |
"define the action to be performed on press"
|
|
234 |
|
|
235 |
pressActionBlock := aBlock
|
|
236 |
!
|
|
237 |
|
|
238 |
releaseAction:aBlock
|
|
239 |
"define the action to be performed on release"
|
|
240 |
|
|
241 |
releaseActionBlock := aBlock
|
|
242 |
!
|
|
243 |
|
118
|
244 |
toggleNoAction
|
|
245 |
"toggle, but do NOT perform any action"
|
|
246 |
|
|
247 |
pressed ifTrue:[
|
|
248 |
view turnOff.
|
|
249 |
pressed := false.
|
|
250 |
] ifFalse:[
|
|
251 |
view turnOn.
|
|
252 |
pressed := true.
|
|
253 |
].
|
|
254 |
!
|
|
255 |
|
|
256 |
toggle
|
|
257 |
"toggle and perform the action"
|
|
258 |
|
133
|
259 |
enableChannel value ifTrue:[
|
125
|
260 |
self toggleNoAction.
|
|
261 |
self performAction.
|
|
262 |
view changed:#toggle with:pressed
|
121
|
263 |
]
|
|
264 |
!
|
|
265 |
|
|
266 |
pressAction
|
|
267 |
"return the pressAction; thats the block which gets evaluated
|
|
268 |
when the button is pressed (if non-nil)"
|
|
269 |
|
|
270 |
^ pressActionBlock
|
|
271 |
!
|
|
272 |
|
|
273 |
releaseAction
|
|
274 |
"return the releaseAction; thats the block which gets evaluated
|
|
275 |
when the button is relreased (if non-nil)"
|
|
276 |
|
|
277 |
^ releaseActionBlock
|
|
278 |
!
|
|
279 |
|
128
|
280 |
enable
|
|
281 |
"enable the button"
|
|
282 |
|
133
|
283 |
enableChannel value ifFalse:[
|
|
284 |
enableChannel value:true.
|
|
285 |
"/ view redraw - not needed; button listenes to enableChannel
|
128
|
286 |
]
|
|
287 |
!
|
|
288 |
|
121
|
289 |
disable
|
|
290 |
"disable the button"
|
|
291 |
|
133
|
292 |
enableChannel value ifTrue:[
|
|
293 |
enableChannel value:false.
|
|
294 |
"/ view redraw - not needed; button listenes to enableChannel
|
|
295 |
]
|
76
|
296 |
! !
|
|
297 |
|
|
298 |
!ButtonController methodsFor:'event handling'!
|
|
299 |
|
|
300 |
buttonPress:button x:x y:y
|
|
301 |
(button == 1 or:[button == #select]) ifFalse:[
|
125
|
302 |
^ super buttonPress:button x:x y:y
|
76
|
303 |
].
|
|
304 |
|
133
|
305 |
enableChannel value ifTrue:[
|
125
|
306 |
isToggle ifTrue:[
|
|
307 |
self toggle.
|
|
308 |
^ self
|
|
309 |
].
|
118
|
310 |
|
125
|
311 |
pressed ifFalse:[
|
|
312 |
pressed := true.
|
|
313 |
view showActive.
|
76
|
314 |
|
125
|
315 |
(pressActionBlock notNil or:[model notNil]) ifTrue:[
|
|
316 |
"
|
|
317 |
force output - so that button is drawn correctly in case
|
|
318 |
of any long-computation (at high priority)
|
|
319 |
"
|
130
|
320 |
view device flush.
|
125
|
321 |
].
|
76
|
322 |
|
125
|
323 |
self performAction.
|
76
|
324 |
|
125
|
325 |
autoRepeat ifTrue:[
|
|
326 |
Processor addTimedBlock:repeatBlock afterSeconds:initialDelay
|
|
327 |
]
|
|
328 |
]
|
76
|
329 |
]
|
|
330 |
!
|
|
331 |
|
|
332 |
buttonRelease:button x:x y:y
|
|
333 |
"button was released - if enabled, perform releaseaction"
|
|
334 |
|
|
335 |
(button == 1 or:[button == #select]) ifFalse:[
|
125
|
336 |
^ super buttonRelease:button x:x y:y
|
76
|
337 |
].
|
118
|
338 |
|
|
339 |
isToggle ifTrue:[
|
125
|
340 |
^ self
|
118
|
341 |
].
|
|
342 |
|
76
|
343 |
pressed ifTrue:[
|
125
|
344 |
autoRepeat ifTrue:[
|
|
345 |
Processor removeTimedBlock:repeatBlock
|
|
346 |
].
|
|
347 |
pressed := false.
|
|
348 |
view showPassive.
|
76
|
349 |
|
133
|
350 |
enableChannel value ifTrue:[
|
125
|
351 |
"
|
|
352 |
only perform action if released within myself
|
|
353 |
"
|
|
354 |
((x >= 0)
|
|
355 |
and:[x <= view width
|
|
356 |
and:[y >= 0
|
|
357 |
and:[y <= view height]]]) ifTrue:[
|
|
358 |
(releaseActionBlock notNil or:[model notNil]) ifTrue:[
|
|
359 |
"
|
|
360 |
force output - so that button is drawn correctly in case
|
|
361 |
of any long-computation (at high priority)
|
|
362 |
"
|
130
|
363 |
view device flush.
|
125
|
364 |
].
|
76
|
365 |
|
125
|
366 |
self performAction.
|
|
367 |
]
|
|
368 |
]
|
76
|
369 |
]
|
|
370 |
!
|
|
371 |
|
|
372 |
pointerEnter:state x:x y:y
|
125
|
373 |
"mouse pointer entered my view.
|
|
374 |
Redraw with enteredColors if they differ from the normal colors"
|
76
|
375 |
|
|
376 |
entered := true.
|
133
|
377 |
enableChannel value ifTrue:[
|
125
|
378 |
pressed ifTrue:[
|
|
379 |
"
|
|
380 |
reentered after a leave with mouse-button down;
|
|
381 |
restart autorepeating and/or if I am a button with
|
|
382 |
triggerOnDown, show active again.
|
|
383 |
"
|
76
|
384 |
autoRepeat ifTrue:[
|
|
385 |
Processor addTimedBlock:repeatBlock afterSeconds:initialDelay
|
|
386 |
].
|
118
|
387 |
isTriggerOnDown ifFalse:[
|
76
|
388 |
view showActive.
|
|
389 |
]
|
125
|
390 |
] ifFalse:[
|
76
|
391 |
view redraw
|
|
392 |
]
|
|
393 |
]
|
|
394 |
!
|
|
395 |
|
121
|
396 |
pointerLeave:state
|
125
|
397 |
"mouse pointer left my view.
|
|
398 |
Redraw with normal colors if they differ from enteredColors"
|
121
|
399 |
|
|
400 |
entered := false.
|
|
401 |
pressed ifTrue:[
|
|
402 |
"
|
|
403 |
leave with mouse-button down;
|
|
404 |
stop autorepeating and/or if I am a button with
|
|
405 |
action on release, show passive
|
|
406 |
"
|
|
407 |
autoRepeat ifTrue:[
|
|
408 |
Processor removeTimedBlock:repeatBlock
|
|
409 |
].
|
|
410 |
isTriggerOnDown ifFalse:[
|
|
411 |
view showPassive.
|
|
412 |
]
|
|
413 |
] ifFalse:[
|
133
|
414 |
enableChannel value ifTrue:[
|
121
|
415 |
view redraw
|
|
416 |
]
|
|
417 |
]
|
|
418 |
!
|
|
419 |
|
|
420 |
performAction
|
146
|
421 |
|action value|
|
133
|
422 |
|
|
423 |
isToggle ifTrue:[
|
|
424 |
value := pressed
|
|
425 |
] ifFalse:[
|
|
426 |
value := true
|
|
427 |
].
|
76
|
428 |
|
125
|
429 |
"
|
145
|
430 |
ST/X style actionBlock evaluation & channel notification ...
|
125
|
431 |
"
|
76
|
432 |
pressed ifTrue:[
|
145
|
433 |
action := pressActionBlock.
|
121
|
434 |
] ifFalse:[
|
145
|
435 |
action := releaseActionBlock.
|
121
|
436 |
].
|
125
|
437 |
action notNil ifTrue:[
|
|
438 |
active := true.
|
|
439 |
action numArgs == 0 ifTrue:[
|
|
440 |
action value
|
|
441 |
] ifFalse:[
|
133
|
442 |
action value:value
|
125
|
443 |
].
|
|
444 |
active := false.
|
|
445 |
].
|
146
|
446 |
pressChannel notNil ifTrue:[
|
|
447 |
pressChannel value:pressed
|
|
448 |
].
|
|
449 |
releaseChannel notNil ifTrue:[
|
|
450 |
releaseChannel value:pressed not
|
145
|
451 |
].
|
76
|
452 |
|
125
|
453 |
"
|
|
454 |
ST-80 style model notification ...
|
|
455 |
"
|
121
|
456 |
(isToggle
|
|
457 |
or:[(isTriggerOnDown and:[pressed])
|
|
458 |
or:[isTriggerOnDown not and:[pressed not]]]) ifTrue:[
|
125
|
459 |
"the ST-80 way of doing things"
|
|
460 |
view notNil ifTrue:[
|
|
461 |
active := true.
|
133
|
462 |
view sendChangeMessageWith:value.
|
125
|
463 |
active := false.
|
|
464 |
].
|
121
|
465 |
].
|
76
|
466 |
!
|
|
467 |
|
|
468 |
buttonMultiPress:button x:x y:y
|
|
469 |
^ self buttonPress:button x:x y:y
|
|
470 |
!
|
|
471 |
|
|
472 |
keyPress:key x:x y:y
|
125
|
473 |
"trigger on Return and space, if I am the focusView of my group
|
|
474 |
(i.e. if I got an explicit focus)"
|
76
|
475 |
|
125
|
476 |
(key == #Return or:[key == Character space]) ifTrue:[
|
|
477 |
view hasFocus ifTrue:[
|
76
|
478 |
"just simulate a buttonPress/release here."
|
|
479 |
self buttonPress:1 x:0 y:0.
|
|
480 |
self buttonRelease:1 x:0 y:0.
|
|
481 |
^ self.
|
|
482 |
]
|
|
483 |
].
|
|
484 |
view keyPress:key x:x y:y
|
|
485 |
!
|
|
486 |
|
121
|
487 |
repeat
|
|
488 |
"this is sent from the autorepeat-block, when the button has been pressed long
|
|
489 |
enough; it simulates a release-press, by evaluating both release
|
|
490 |
and press actions."
|
76
|
491 |
|
|
492 |
pressed ifTrue:[
|
133
|
493 |
enableChannel value ifTrue:[
|
121
|
494 |
active ifFalse:[
|
|
495 |
active := true.
|
|
496 |
releaseActionBlock notNil ifTrue:[releaseActionBlock value].
|
|
497 |
pressActionBlock notNil ifTrue:[pressActionBlock value].
|
|
498 |
active := false.
|
|
499 |
|
|
500 |
autoRepeat ifTrue:[
|
|
501 |
Processor addTimedBlock:repeatBlock afterSeconds:repeatDelay
|
|
502 |
]
|
|
503 |
]
|
76
|
504 |
]
|
|
505 |
]
|
133
|
506 |
!
|
|
507 |
|
|
508 |
enableStateChange
|
|
509 |
"this is sent, whenever the enable value has changed"
|
|
510 |
|
|
511 |
view redraw
|
76
|
512 |
! !
|
121
|
513 |
|
|
514 |
!ButtonController methodsFor:'initialization'!
|
|
515 |
|
|
516 |
initialize
|
|
517 |
super initialize.
|
|
518 |
|
133
|
519 |
self enableChannel:(true asValue).
|
|
520 |
|
121
|
521 |
active := false.
|
|
522 |
pressed := false.
|
|
523 |
entered := false.
|
|
524 |
autoRepeat := false.
|
|
525 |
initialDelay := self class defaultInitialDelay.
|
|
526 |
repeatDelay := self class defaultRepeatDelay.
|
|
527 |
isTriggerOnDown := false.
|
|
528 |
isToggle := false.
|
|
529 |
! !
|