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
|
|
14 |
instanceVariableNames:'enabled pressed active entered triggerOnDown autoRepeat
|
|
15 |
repeatBlock initialDelay repeatDelay
|
|
16 |
pressActionBlock releaseActionBlock'
|
|
17 |
classVariableNames:''
|
|
18 |
poolDictionaries:''
|
|
19 |
category:'Interface-Support'
|
|
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 |
version
|
|
39 |
"
|
87
|
40 |
$Header: /cvs/stx/stx/libwidg/ButtonController.st,v 1.2 1995-02-16 16:31:37 claus Exp $
|
76
|
41 |
"
|
|
42 |
!
|
|
43 |
|
|
44 |
documentation
|
|
45 |
"
|
|
46 |
ButtonControllers are used with buttons and handle all user interaction.
|
|
47 |
These are automatically created when a Button is created, therefore no manual
|
|
48 |
action is required for creation.
|
|
49 |
|
|
50 |
Instance variables:
|
|
51 |
|
|
52 |
enabled <Boolean> pressing is allowed (default: true)
|
|
53 |
pressed <Boolean> true if currently pressed (read-only)
|
|
54 |
entered <Boolean> true if the cursor is currently in this view
|
|
55 |
triggerOnDown <Boolean> controls if the action should be executed on
|
|
56 |
press or on release (default: on release).
|
|
57 |
pressActionBlock <Block> block to evaluate when pressed (default: noop)
|
|
58 |
releaseActionBlock <Block> block to evaluate when released (default: noop)
|
|
59 |
autoRepeat <Boolean> auto-repeats when pressed long enough (default: false)
|
|
60 |
initialDelay <Number> seconds till first auto-repeat (default: 0.2)
|
|
61 |
repeatDelay <Number> seconds of repeat intervall (default: 0.025)
|
|
62 |
repeatBlock <Block> block evaluated for auto-repeat (internal)
|
|
63 |
active <Boolean> true during action evaluation (internal)
|
|
64 |
"
|
|
65 |
!
|
|
66 |
|
|
67 |
examples
|
|
68 |
"
|
87
|
69 |
Although you can specify a lot in a button,
|
|
70 |
use the default in most applications.
|
|
71 |
As you specify more things in your program,
|
|
72 |
the styleSheet settings are more and more ignored.
|
|
73 |
Also, even though it might look fancy, colorful
|
|
74 |
button panels are usually not a good GUI design;
|
|
75 |
they will attract the users attention - possibly to things
|
|
76 |
which are not worth it.
|
|
77 |
|
|
78 |
default settings:
|
|
79 |
|
76
|
80 |
|top b|
|
|
81 |
|
|
82 |
top := StandardSystemView new.
|
|
83 |
top extent:100@100.
|
|
84 |
|
|
85 |
b := Button label:'hello' in:top.
|
|
86 |
b action:[Transcript flash].
|
|
87 |
top open.
|
|
88 |
|
|
89 |
|
87
|
90 |
changing colors:
|
|
91 |
|
|
92 |
|top b|
|
|
93 |
|
|
94 |
top := StandardSystemView new.
|
|
95 |
top extent:100@100.
|
|
96 |
|
|
97 |
b := Button label:'hello' in:top.
|
|
98 |
b action:[Transcript flash].
|
|
99 |
b activeForegroundColor:(Color white).
|
|
100 |
b activeBackgroundColor:(Color red).
|
|
101 |
top open.
|
|
102 |
|
|
103 |
|
|
104 |
changing more colors:
|
|
105 |
|
76
|
106 |
|top b|
|
|
107 |
|
|
108 |
top := StandardSystemView new.
|
|
109 |
top extent:100@100.
|
|
110 |
|
|
111 |
b := Button label:'hello' in:top.
|
87
|
112 |
b action:[Transcript flash].
|
|
113 |
b enteredForegroundColor:(Color red).
|
|
114 |
b enteredBackgroundColor:(b backgroundColor).
|
|
115 |
b activeForegroundColor:(Color white).
|
|
116 |
b activeBackgroundColor:(Color red).
|
|
117 |
top open.
|
|
118 |
|
|
119 |
|
|
120 |
button with an image:
|
|
121 |
|
|
122 |
|top b|
|
|
123 |
|
|
124 |
top := StandardSystemView new.
|
|
125 |
top extent:100@100.
|
|
126 |
|
|
127 |
b := Button in:top.
|
|
128 |
b form:(Image fromFile:'bitmaps/SBrowser.xbm').
|
|
129 |
b action:[Transcript flash].
|
|
130 |
b enteredForegroundColor:(Color green darkened).
|
|
131 |
b enteredBackgroundColor:(b backgroundColor).
|
|
132 |
b activeForegroundColor:(Color white).
|
|
133 |
b activeBackgroundColor:(Color red).
|
|
134 |
top open.
|
|
135 |
|
|
136 |
|
|
137 |
changing the image:
|
|
138 |
|
|
139 |
|top b|
|
|
140 |
|
|
141 |
top := StandardSystemView new.
|
|
142 |
top extent:100@100.
|
|
143 |
|
|
144 |
b := Button in:top.
|
|
145 |
b passiveLogo:(Image fromFile:'bitmaps/SBrowser.xbm').
|
|
146 |
b activeLogo:(Image fromFile:'bitmaps/CBrowser.xbm').
|
|
147 |
b action:[Transcript flash].
|
|
148 |
b enteredForegroundColor:(Color red).
|
|
149 |
b enteredBackgroundColor:(b backgroundColor).
|
|
150 |
b activeForegroundColor:(Color white).
|
|
151 |
b activeBackgroundColor:(Color red).
|
|
152 |
top open.
|
|
153 |
|
|
154 |
|
|
155 |
well, even that is possible (but you should never do it):
|
|
156 |
(notice the changing size and the resulting problem when
|
|
157 |
pressed near the bottom)
|
|
158 |
|
|
159 |
|top b|
|
|
160 |
|
|
161 |
top := StandardSystemView new.
|
|
162 |
top extent:100@100.
|
|
163 |
|
|
164 |
b := Button in:top.
|
|
165 |
b passiveLogo:(Image fromFile:'bitmaps/SmalltalkX.xbm').
|
|
166 |
b activeLogo:#('start') asStringCollection.
|
|
167 |
b action:[Transcript flash].
|
|
168 |
b enteredForegroundColor:(Color red).
|
|
169 |
b enteredBackgroundColor:(b backgroundColor).
|
|
170 |
b activeForegroundColor:(Color white).
|
|
171 |
b activeBackgroundColor:(Color red).
|
|
172 |
top open.
|
|
173 |
|
|
174 |
|
|
175 |
same as above, with frozen size
|
|
176 |
(make certain, that the size is ok for all possible logos):
|
|
177 |
|
|
178 |
|top b|
|
|
179 |
|
|
180 |
top := StandardSystemView new.
|
|
181 |
top extent:100@100.
|
|
182 |
|
|
183 |
b := Button in:top.
|
|
184 |
b passiveLogo:(Image fromFile:'bitmaps/SmalltalkX.xbm').
|
|
185 |
b activeLogo:#('go') asStringCollection.
|
|
186 |
b action:[Transcript flash].
|
|
187 |
b enteredForegroundColor:(Color red).
|
|
188 |
b enteredBackgroundColor:(b backgroundColor).
|
|
189 |
b activeForegroundColor:(Color white).
|
|
190 |
b activeBackgroundColor:(Color red).
|
|
191 |
b sizeFixed:true.
|
|
192 |
top open.
|
|
193 |
|
|
194 |
|
|
195 |
more playing with colors:
|
|
196 |
|
|
197 |
|top b|
|
|
198 |
|
|
199 |
top := StandardSystemView new.
|
|
200 |
top extent:100@100.
|
|
201 |
|
|
202 |
b := Button in:top.
|
|
203 |
b passiveLogo:(Image fromFile:'bitmaps/SmalltalkX.xbm').
|
|
204 |
b action:[Transcript flash].
|
|
205 |
b foregroundColor:(Color red:0 green:80 blue:20) darkened.
|
|
206 |
b backgroundColor:(Color grey:10).
|
|
207 |
b enteredBackgroundColor:(Color grey:20).
|
|
208 |
b activeForegroundColor:(Color red).
|
|
209 |
b activeBackgroundColor:(Color grey:20).
|
|
210 |
top open.
|
|
211 |
|
|
212 |
|
|
213 |
fire on press (buttons in scrollbars do this):
|
|
214 |
|
|
215 |
|top b|
|
|
216 |
|
|
217 |
top := StandardSystemView new.
|
|
218 |
top extent:100@100.
|
|
219 |
|
|
220 |
b := Button label:'hello' in:top.
|
|
221 |
b controller beTriggerOnDown.
|
|
222 |
b action:[Transcript flash].
|
76
|
223 |
top open.
|
|
224 |
"
|
|
225 |
! !
|
|
226 |
|
|
227 |
!ButtonController class methodsFor:'defaults'!
|
|
228 |
|
|
229 |
defaultRepeatDelay
|
|
230 |
"when autorepeat is enabled, and button is not released,
|
|
231 |
repeat every repeatDelay seconds"
|
|
232 |
|
|
233 |
^ 0.025
|
|
234 |
|
|
235 |
!
|
|
236 |
|
|
237 |
defaultInitialDelay
|
|
238 |
"when autorepeat is enabled, and button is not released,
|
|
239 |
start repeating after initialDelay seconds"
|
|
240 |
|
|
241 |
^ 0.2
|
|
242 |
|
|
243 |
|
|
244 |
! !
|
|
245 |
|
|
246 |
!ButtonController methodsFor:'initialization'!
|
|
247 |
|
|
248 |
initialize
|
|
249 |
super initialize.
|
|
250 |
|
|
251 |
enabled := true.
|
|
252 |
active := false.
|
|
253 |
pressed := false.
|
|
254 |
entered := false.
|
|
255 |
autoRepeat := false.
|
|
256 |
initialDelay := self class defaultInitialDelay.
|
|
257 |
repeatDelay := self class defaultRepeatDelay.
|
|
258 |
triggerOnDown := false.
|
|
259 |
! !
|
|
260 |
|
|
261 |
!ButtonController methodsFor:'accessing'!
|
|
262 |
|
|
263 |
beTriggerOnUp
|
|
264 |
"make the receiver act on button release"
|
|
265 |
|
|
266 |
triggerOnDown := false
|
|
267 |
!
|
|
268 |
|
|
269 |
beTriggerOnDown
|
|
270 |
"make the receiver act on button press"
|
|
271 |
|
|
272 |
triggerOnDown := true
|
|
273 |
!
|
|
274 |
|
|
275 |
triggerOnDown:aBoolean
|
|
276 |
"set/clear the flag which controls if the action block is to be evaluated
|
|
277 |
on press or on release.
|
|
278 |
(see also ST-80 compatibility methods beTriggerOn*)"
|
|
279 |
|
|
280 |
triggerOnDown := aBoolean
|
|
281 |
!
|
|
282 |
|
|
283 |
isTriggerOnDown
|
|
284 |
^ triggerOnDown
|
|
285 |
!
|
|
286 |
|
|
287 |
pressAction
|
|
288 |
"return the pressAction; thats the block which gets evaluated
|
|
289 |
when the button is pressed (if non-nil)"
|
|
290 |
|
|
291 |
^ pressActionBlock
|
|
292 |
!
|
|
293 |
|
|
294 |
pressAction:aBlock
|
|
295 |
"define the action to be performed on press"
|
|
296 |
|
|
297 |
pressActionBlock := aBlock
|
|
298 |
!
|
|
299 |
|
|
300 |
releaseAction
|
|
301 |
"return the releaseAction; thats the block which gets evaluated
|
|
302 |
when the button is relreased (if non-nil)"
|
|
303 |
|
|
304 |
^ releaseActionBlock
|
|
305 |
!
|
|
306 |
|
|
307 |
releaseAction:aBlock
|
|
308 |
"define the action to be performed on release"
|
|
309 |
|
|
310 |
releaseActionBlock := aBlock
|
|
311 |
!
|
|
312 |
|
|
313 |
action:aBlock
|
|
314 |
"convenient method: depending on the setting the triggerOnDown flag,
|
|
315 |
either set the press-action clear any release-action or
|
|
316 |
vice versa, set the release-action and clear the press-action."
|
|
317 |
|
|
318 |
triggerOnDown ifTrue:[
|
|
319 |
releaseActionBlock := nil.
|
|
320 |
pressActionBlock := aBlock
|
|
321 |
] ifFalse:[
|
|
322 |
releaseActionBlock := aBlock.
|
|
323 |
pressActionBlock := nil
|
|
324 |
]
|
|
325 |
!
|
|
326 |
|
|
327 |
autoRepeat
|
|
328 |
"turn on autorepeat"
|
|
329 |
|
|
330 |
autoRepeat := true.
|
|
331 |
repeatBlock := [self repeat]
|
|
332 |
!
|
|
333 |
|
|
334 |
disable
|
|
335 |
"disable the button"
|
|
336 |
|
|
337 |
enabled ifTrue:[
|
|
338 |
enabled := false.
|
|
339 |
view redraw
|
|
340 |
]
|
|
341 |
!
|
|
342 |
|
|
343 |
enable
|
|
344 |
"enable the button"
|
|
345 |
|
|
346 |
enabled ifFalse:[
|
|
347 |
enabled := true.
|
|
348 |
view redraw
|
|
349 |
]
|
|
350 |
!
|
|
351 |
|
|
352 |
enabled
|
|
353 |
^ enabled
|
|
354 |
!
|
|
355 |
|
|
356 |
active
|
|
357 |
^ active
|
|
358 |
!
|
|
359 |
|
|
360 |
pressed
|
|
361 |
^ pressed
|
|
362 |
!
|
|
363 |
|
|
364 |
entered
|
|
365 |
^ entered
|
|
366 |
!
|
|
367 |
|
|
368 |
entered:aBoolean
|
|
369 |
entered := aBoolean
|
|
370 |
!
|
|
371 |
|
|
372 |
pressed:aBoolean
|
|
373 |
pressed := aBoolean
|
|
374 |
!
|
|
375 |
|
|
376 |
active:aBoolean
|
|
377 |
active := aBoolean
|
|
378 |
! !
|
|
379 |
|
|
380 |
!ButtonController methodsFor:'event handling'!
|
|
381 |
|
|
382 |
buttonPress:button x:x y:y
|
|
383 |
|sym|
|
|
384 |
|
|
385 |
(button == 1 or:[button == #select]) ifFalse:[
|
|
386 |
^ super buttonPress:button x:x y:y
|
|
387 |
].
|
|
388 |
|
|
389 |
pressed ifFalse:[
|
|
390 |
enabled ifTrue:[
|
|
391 |
pressed := true.
|
|
392 |
view showActive.
|
|
393 |
|
|
394 |
(pressActionBlock notNil or:[model notNil]) ifTrue:[
|
|
395 |
"
|
|
396 |
force output - so that button is drawn correctly in case
|
|
397 |
of any long-computation (at high priority)
|
|
398 |
"
|
|
399 |
view device synchronizeOutput.
|
|
400 |
].
|
|
401 |
|
|
402 |
active := true.
|
|
403 |
|
|
404 |
pressActionBlock notNil ifTrue:[
|
|
405 |
pressActionBlock value
|
|
406 |
].
|
|
407 |
|
|
408 |
triggerOnDown ifTrue:[
|
|
409 |
"the ST-80 way of doing things"
|
|
410 |
(model notNil and:[(sym := view changeSymbol) notNil]) ifTrue:[
|
|
411 |
model perform:sym
|
|
412 |
].
|
|
413 |
].
|
|
414 |
|
|
415 |
active := false.
|
|
416 |
|
|
417 |
autoRepeat ifTrue:[
|
|
418 |
Processor addTimedBlock:repeatBlock afterSeconds:initialDelay
|
|
419 |
]
|
|
420 |
]
|
|
421 |
]
|
|
422 |
!
|
|
423 |
|
|
424 |
buttonRelease:button x:x y:y
|
|
425 |
"button was released - if enabled, perform releaseaction"
|
|
426 |
|
|
427 |
|sym|
|
|
428 |
|
|
429 |
(button == 1 or:[button == #select]) ifFalse:[
|
|
430 |
^ super buttonRelease:button x:x y:y
|
|
431 |
].
|
|
432 |
pressed ifTrue:[
|
|
433 |
autoRepeat ifTrue:[
|
|
434 |
Processor removeTimedBlock:repeatBlock
|
|
435 |
].
|
|
436 |
pressed := false.
|
|
437 |
view showPassive.
|
|
438 |
|
|
439 |
enabled ifTrue:[
|
|
440 |
"
|
|
441 |
only perform action if released within myself
|
|
442 |
"
|
|
443 |
((x >= 0)
|
|
444 |
and:[x <= view width
|
|
445 |
and:[y >= 0
|
|
446 |
and:[y <= view height]]]) ifTrue:[
|
|
447 |
(releaseActionBlock notNil or:[model notNil]) ifTrue:[
|
|
448 |
"
|
|
449 |
force output - so that button is drawn correctly in case
|
|
450 |
of any long-computation (at high priority)
|
|
451 |
"
|
|
452 |
view device synchronizeOutput.
|
|
453 |
].
|
|
454 |
|
|
455 |
active := true.
|
|
456 |
|
|
457 |
releaseActionBlock notNil ifTrue:[
|
|
458 |
releaseActionBlock value
|
|
459 |
].
|
|
460 |
triggerOnDown ifFalse:[
|
|
461 |
"the ST-80 way of doing things"
|
|
462 |
(model notNil and:[(sym := view changeSymbol) notNil]) ifTrue:[
|
|
463 |
model perform:sym
|
|
464 |
].
|
|
465 |
].
|
|
466 |
|
|
467 |
active := false.
|
|
468 |
|
|
469 |
view redraw.
|
|
470 |
]
|
|
471 |
]
|
|
472 |
]
|
|
473 |
!
|
|
474 |
|
|
475 |
pointerEnter:state x:x y:y
|
|
476 |
"redraw with enteredColors if they differ from the normal colors"
|
|
477 |
|
|
478 |
entered := true.
|
|
479 |
pressed ifTrue:[
|
|
480 |
"
|
|
481 |
reentered after a leave with mouse-button down;
|
|
482 |
restart autorepeating and/or if I am a button with
|
|
483 |
triggerOnDown, show active again.
|
|
484 |
"
|
|
485 |
enabled ifTrue:[
|
|
486 |
autoRepeat ifTrue:[
|
|
487 |
Processor addTimedBlock:repeatBlock afterSeconds:initialDelay
|
|
488 |
].
|
|
489 |
triggerOnDown ifFalse:[
|
|
490 |
view showActive.
|
|
491 |
]
|
|
492 |
]
|
|
493 |
] ifFalse:[
|
|
494 |
enabled ifTrue:[
|
|
495 |
view redraw
|
|
496 |
]
|
|
497 |
]
|
|
498 |
!
|
|
499 |
|
|
500 |
repeat
|
|
501 |
"this is sent from the autorepeat-block, when the button has been pressed long
|
|
502 |
enough; it simulates a release-press, by evaluating both release
|
|
503 |
and press actions."
|
|
504 |
|
|
505 |
pressed ifTrue:[
|
|
506 |
enabled ifTrue:[
|
|
507 |
active ifFalse:[
|
|
508 |
active := true.
|
|
509 |
releaseActionBlock notNil ifTrue:[releaseActionBlock value].
|
|
510 |
pressActionBlock notNil ifTrue:[pressActionBlock value].
|
|
511 |
active := false.
|
|
512 |
|
|
513 |
autoRepeat ifTrue:[
|
|
514 |
Processor addTimedBlock:repeatBlock afterSeconds:repeatDelay
|
|
515 |
]
|
|
516 |
]
|
|
517 |
]
|
|
518 |
]
|
|
519 |
!
|
|
520 |
|
|
521 |
buttonMultiPress:button x:x y:y
|
|
522 |
^ self buttonPress:button x:x y:y
|
|
523 |
!
|
|
524 |
|
|
525 |
keyPress:key x:x y:y
|
|
526 |
"only trigger, if I am the focusView of my group"
|
|
527 |
|
|
528 |
|group|
|
|
529 |
|
|
530 |
((group := view windowGroup) notNil
|
|
531 |
and:[group focusView == view]) ifTrue:[
|
|
532 |
(key == #Return or:[key == Character space]) ifTrue:[
|
|
533 |
"just simulate a buttonPress/release here."
|
|
534 |
self buttonPress:1 x:0 y:0.
|
|
535 |
self buttonRelease:1 x:0 y:0.
|
|
536 |
^ self.
|
|
537 |
]
|
|
538 |
].
|
|
539 |
view keyPress:key x:x y:y
|
|
540 |
!
|
|
541 |
|
|
542 |
pointerLeave:state
|
|
543 |
"redraw with normal colors if they differ from enteredColors"
|
|
544 |
|
|
545 |
entered := false.
|
|
546 |
pressed ifTrue:[
|
|
547 |
"
|
|
548 |
leave with mouse-button down;
|
|
549 |
stop autorepeating and/or if I am a button with
|
|
550 |
action on release, show passive
|
|
551 |
"
|
|
552 |
autoRepeat ifTrue:[
|
|
553 |
Processor removeTimedBlock:repeatBlock
|
|
554 |
].
|
|
555 |
triggerOnDown ifFalse:[
|
|
556 |
view showPassive.
|
|
557 |
]
|
|
558 |
] ifFalse:[
|
|
559 |
enabled ifTrue:[
|
|
560 |
view redraw
|
|
561 |
]
|
|
562 |
]
|
|
563 |
! !
|
|
564 |
|