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