author | Claus Gittinger <cg@exept.de> |
Sat, 27 Apr 1996 19:56:29 +0200 | |
changeset 620 | c840b7106a64 |
parent 612 | 8758d0c9933e |
child 693 | ed557e02b378 |
permissions | -rw-r--r-- |
128 | 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 |
||
14 |
Object subclass:#KeyboardForwarder |
|
400 | 15 |
instanceVariableNames:'sourceView destinationView destination condition filter' |
16 |
classVariableNames:'' |
|
17 |
poolDictionaries:'' |
|
18 |
category:'Interface-Support' |
|
128 | 19 |
! |
20 |
||
21 |
!KeyboardForwarder class methodsFor:'documentation'! |
|
22 |
||
244 | 23 |
copyright |
24 |
" |
|
25 |
COPYRIGHT (c) 1995 by Claus Gittinger |
|
26 |
All Rights Reserved |
|
27 |
||
28 |
This software is furnished under a license and may be used |
|
29 |
only in accordance with the terms of that license and with the |
|
30 |
inclusion of the above copyright notice. This software may not |
|
31 |
be provided or otherwise made available to, or used by, any |
|
32 |
other person. No title to or ownership of the software is |
|
33 |
hereby transferred. |
|
34 |
" |
|
35 |
||
128 | 36 |
! |
37 |
||
38 |
documentation |
|
39 |
" |
|
144 | 40 |
Instances of this class can be used to forward keyboard |
41 |
events to some object or other view (install them via aView delegate:). |
|
42 |
Event delegation allows to add additional keyboard functions to views |
|
43 |
WITHOUT modifying their code or changing controllers. |
|
44 |
Also, it allows to catch certain individual keys to ignore them or |
|
45 |
perform different functions. |
|
46 |
||
128 | 47 |
Notice, that delegates dont have to be instances of |
48 |
myself; any object with a protocol similar to mine can be used as |
|
49 |
a delegate. |
|
142 | 50 |
(i.e. any object that responds to |
51 |
handlesKeyPress:view: / keyPress:x:y:view: / delegatesTo: etc.) |
|
139 | 52 |
|
53 |
However, I provide the most common functions required for conditional |
|
54 |
event delegation and instances of myself are for example used to forward |
|
55 |
events from elements of some dialogBoxes to the enterfield/fieldGroup - |
|
144 | 56 |
(which allows keyboard input to be forwarded to the active text-field even |
57 |
while the mouse pointer is somewhere else) |
|
139 | 58 |
|
59 |
Another application of delegation is to catch keyboard input to some standard |
|
60 |
widget, optionally processing and resending the key-event. |
|
128 | 61 |
|
144 | 62 |
I can either forward the key event to another view, or to some general |
63 |
object - the difference is that views are sent a #keyXXX:x:y: message, |
|
64 |
while general destinations get a #keyXXX:x:y:view: message, with the original |
|
65 |
view (the one in which the event occurred) as additional argument. |
|
66 |
||
598 | 67 |
[Instance Variables:] |
139 | 68 |
|
598 | 69 |
destinationView <View> the view which shall receive |
70 |
the forwarded keyboard events. |
|
134 | 71 |
|
598 | 72 |
destination <Object> the object which shall receive |
73 |
the forwarded keyboard events. |
|
139 | 74 |
|
598 | 75 |
sourceView <ViewOrNil> if non-nil, only events from this |
76 |
view are forwarded. |
|
77 |
(currently nowhere used) |
|
134 | 78 |
|
598 | 79 |
condition <Symbol> an additional condition for forwarding |
80 |
currently only #noFocus is implemented. |
|
81 |
The #noFocus condition will only forward |
|
82 |
events if no explicit focus has been |
|
83 |
set in the windowGroup. |
|
144 | 84 |
|
598 | 85 |
<Block> condition block - event is forwarded, |
86 |
if it evaluates to true. |
|
87 |
The block gets 3 arguments: |
|
88 |
eventType #keyPress or #keyRelease |
|
89 |
key the key |
|
90 |
view the originating view |
|
144 | 91 |
|
92 |
For more info on event handling/forwarding, see the documentation in |
|
93 |
WindowSensor, WindowGroup and especially in WindowEvent. |
|
598 | 94 |
|
95 |
[see also:] |
|
96 |
WindowEvent WindowSensor WindowGroup |
|
612 | 97 |
|
98 |
[author:] |
|
99 |
Claus Gittinger |
|
144 | 100 |
" |
101 |
! |
|
102 |
||
103 |
examples |
|
104 |
" |
|
105 |
catch an enterfields input: |
|
620 | 106 |
[exBegin] |
107 |
|top field forwarder catcher| |
|
144 | 108 |
|
620 | 109 |
catcher := Plug new. |
110 |
catcher respondTo:#keyPress:x:y:view: |
|
111 |
with:[:key :x :y :view | Transcript showCr:'cought keyPress: ' , key printString.]. |
|
112 |
catcher respondTo:#keyRelease:x:y:view: |
|
113 |
with:[:key :x :y :view | Transcript showCr:'cought keyRelease: ' , key printString.]. |
|
144 | 114 |
|
620 | 115 |
top := StandardSystemView new. |
116 |
top extent:200@200. |
|
117 |
||
118 |
field := EditField in:top. |
|
119 |
field origin:0.0@0.0; width:1.0. |
|
144 | 120 |
|
620 | 121 |
forwarder := KeyboardForwarder to:catcher. |
122 |
field delegate:forwarder. |
|
144 | 123 |
|
620 | 124 |
top open |
125 |
[exEnd] |
|
144 | 126 |
|
127 |
||
128 |
catch an enterfields return-key only: |
|
620 | 129 |
[exBegin] |
130 |
|top field forwarder catcher| |
|
144 | 131 |
|
620 | 132 |
catcher := Plug new. |
133 |
catcher respondTo:#keyPress:x:y:view: |
|
134 |
with:[:key :x :y :view | Transcript showCr:'cought return']. |
|
144 | 135 |
|
620 | 136 |
top := StandardSystemView new. |
137 |
top extent:200@200. |
|
144 | 138 |
|
620 | 139 |
field := EditField in:top. |
140 |
field origin:0.0@0.0; width:1.0. |
|
144 | 141 |
|
620 | 142 |
forwarder := KeyboardForwarder |
143 |
to:catcher |
|
144 |
condition:[:type :key :view | (type == #keyPress) & (key == #Return)]. |
|
145 |
field delegate:forwarder. |
|
144 | 146 |
|
620 | 147 |
top open |
148 |
[exEnd] |
|
144 | 149 |
|
150 |
||
151 |
catch all of an enterfields input and ignore it: |
|
620 | 152 |
[exBegin] |
153 |
|top field forwarder| |
|
144 | 154 |
|
620 | 155 |
top := StandardSystemView new. |
156 |
top extent:200@200. |
|
144 | 157 |
|
620 | 158 |
field := EditField in:top. |
159 |
field origin:0.0@0.0; width:1.0. |
|
144 | 160 |
|
620 | 161 |
forwarder := KeyboardForwarder to:nil. |
162 |
field delegate:forwarder. |
|
144 | 163 |
|
620 | 164 |
top open |
165 |
[exEnd] |
|
128 | 166 |
" |
167 |
! ! |
|
168 |
||
169 |
!KeyboardForwarder class methodsFor:'instance creation'! |
|
170 |
||
139 | 171 |
from:sourceView to:destination |
172 |
"create and return a new KeyboardForwarder to redirect key events |
|
173 |
for sourceView to destination. Events from other than the sourceView |
|
174 |
will not be forwarded. The forwarded event will be reported including |
|
175 |
the original view as argument (i.e. as #keyPress:x:y:view:). |
|
176 |
Use this, if the destination is not a view." |
|
177 |
||
178 |
^ self new sourceView:sourceView; destination:destination |
|
179 |
! |
|
180 |
||
244 | 181 |
from:sourceView toView:destinationView |
182 |
"create and return a new KeyboardForwarder to redirect key events |
|
183 |
for sourceView to destinationView. Events from other than the sourceView |
|
184 |
will not be forwarded. The forwarded event will be reported excluding |
|
139 | 185 |
the original view as argument (i.e. as #keyPress:x:y:). |
186 |
Use this, if the destination is a view." |
|
187 |
||
244 | 188 |
^ self new sourceView:sourceView; destinationView:destinationView |
134 | 189 |
! |
190 |
||
139 | 191 |
to:destination |
192 |
"create and return a new KeyboardForwarder to redirect any key event |
|
193 |
to destination (Independent of the view in which the event originally |
|
194 |
occurred). The forwarded event will be reported including |
|
195 |
the original view as argument (i.e. as #keyPress:x:y:view:). |
|
196 |
Use this, if the destination is not a view." |
|
197 |
||
400 | 198 |
^ self to:destination condition:nil filter:nil |
139 | 199 |
! |
200 |
||
244 | 201 |
to:destination condition:aCondition |
202 |
"create and return a new KeyboardForwarder to redirect any key event |
|
203 |
to destinationView (Independent of the view in which the event originally |
|
204 |
occurred) but only, if some condition as specified by aCondition |
|
205 |
is met. The forwarded event will be reported including |
|
206 |
the original view as argument (i.e. as #keyPress:x:y:view:). |
|
207 |
Use this, if the destination is not a view." |
|
208 |
||
400 | 209 |
^ self to:destination condition:aCondition filter:nil |
210 |
! |
|
211 |
||
212 |
to:destination condition:aCondition filter:aFilterBlock |
|
213 |
"create and return a new KeyboardForwarder to redirect any key event |
|
214 |
to destinationView (Independent of the view in which the event originally |
|
215 |
occurred) but only, if some condition as specified by aCondition |
|
216 |
is met and aFilterBlock returns true for that key. |
|
217 |
The forwarded event will be reported including |
|
218 |
the original view as argument (i.e. as #keyPress:x:y:view:). |
|
219 |
Use this, if the destination is not a view." |
|
220 |
||
221 |
^ self new destination:destination; condition:aCondition; filter:aFilterBlock |
|
244 | 222 |
! |
223 |
||
224 |
toView:destinationView |
|
225 |
"create and return a new KeyboardForwarder to redirect any key event |
|
226 |
to destinationView (Independent of the view in which the event originally |
|
227 |
occurred). The forwarded event will be reported excluding |
|
228 |
the original view as argument (i.e. as #keyPress:x:y:). |
|
229 |
Use this, if the destination is a view." |
|
230 |
||
400 | 231 |
^ self toView:destinationView condition:nil filter:nil |
244 | 232 |
! |
233 |
||
144 | 234 |
toView:destinationView condition:aCondition |
139 | 235 |
"create and return a new KeyboardForwarder to redirect any key event |
236 |
to destinationView (Independent of the view in which the event originally |
|
144 | 237 |
occurred) but only, if some condition as specified by aCondition |
139 | 238 |
is met. The forwarded event will be reported excluding |
239 |
the original view as argument (i.e. as #keyPress:x:y:). |
|
240 |
Use this, if the destination is a view." |
|
241 |
||
400 | 242 |
^ self toView:destinationView condition:aCondition filter:nil |
243 |
! |
|
244 |
||
245 |
toView:destinationView condition:aCondition filter:aFilterBlock |
|
246 |
"create and return a new KeyboardForwarder to redirect any key event |
|
247 |
to destinationView (Independent of the view in which the event originally |
|
248 |
occurred) but only, if some condition as specified by aCondition |
|
249 |
is met and aFilterBlock returns true for that key. |
|
250 |
The forwarded event will be reported excluding |
|
251 |
the original view as argument (i.e. as #keyPress:x:y:). |
|
252 |
Use this, if the destination is a view." |
|
253 |
||
254 |
^ self new destinationView:destinationView; condition:aCondition; filter:aFilterBlock |
|
128 | 255 |
! ! |
256 |
||
144 | 257 |
!KeyboardForwarder methodsFor:'accessing'! |
128 | 258 |
|
134 | 259 |
condition:aCondition |
144 | 260 |
"set the condition - typically a block" |
261 |
||
134 | 262 |
condition := aCondition |
263 |
! |
|
264 |
||
244 | 265 |
destination |
266 |
"return the destination" |
|
144 | 267 |
|
244 | 268 |
^ destination |
139 | 269 |
! |
270 |
||
271 |
destination:anObject |
|
144 | 272 |
"set the destination |
273 |
- that one will get the event with an additional view arg." |
|
274 |
||
139 | 275 |
destination := anObject |
276 |
! |
|
277 |
||
244 | 278 |
destinationView |
279 |
"return the destination view" |
|
144 | 280 |
|
244 | 281 |
^ destinationView |
139 | 282 |
! |
283 |
||
244 | 284 |
destinationView:aView |
285 |
"set the destination view" |
|
144 | 286 |
|
244 | 287 |
destinationView := aView |
128 | 288 |
! |
289 |
||
400 | 290 |
filter:aOneArgBlock |
291 |
"set the filter - if non-nil, only keys for which it returns true are forwarded" |
|
292 |
||
293 |
filter := aOneArgBlock |
|
294 |
||
295 |
"Created: 4.2.1996 / 20:35:15 / cg" |
|
296 |
! |
|
297 |
||
128 | 298 |
sourceView |
144 | 299 |
"get the sourceView - if nonNil, only events from this view will be forwarded" |
300 |
||
128 | 301 |
^ sourceView |
142 | 302 |
! |
303 |
||
244 | 304 |
sourceView:aView |
305 |
"set the sourceView - if nonNil, only events from this view will be forwarded" |
|
128 | 306 |
|
244 | 307 |
sourceView := aView |
128 | 308 |
! ! |
309 |
||
310 |
!KeyboardForwarder methodsFor:'event forwarding'! |
|
311 |
||
312 |
keyPress:key x:x y:y view:aView |
|
139 | 313 |
"handle a delegated event - this is sent by the sensor to actually |
314 |
forward the event (i.e. after I returned true on handlesKeyPress:. |
|
315 |
Take care of cyclic delegation (via a kludge-test for negative coordinate)." |
|
316 |
||
490
1d2ed059a063
delegated keyboardMessages get nil as x/y
Claus Gittinger <cg@exept.de>
parents:
400
diff
changeset
|
317 |
x isNil ifTrue:[ |
400 | 318 |
" |
319 |
already delegated ... ignore |
|
320 |
" |
|
321 |
^ self |
|
322 |
]. |
|
323 |
||
324 |
filter notNil ifTrue:[ |
|
325 |
(filter value:key) ifFalse:[^ self]. |
|
128 | 326 |
]. |
327 |
||
139 | 328 |
destination notNil ifTrue:[ |
490
1d2ed059a063
delegated keyboardMessages get nil as x/y
Claus Gittinger <cg@exept.de>
parents:
400
diff
changeset
|
329 |
destination keyPress:key x:nil y:nil view:aView. |
139 | 330 |
] ifFalse:[ |
400 | 331 |
destinationView notNil ifTrue:[ |
332 |
WindowEvent |
|
333 |
sendEvent:#keyPress:x:y: |
|
490
1d2ed059a063
delegated keyboardMessages get nil as x/y
Claus Gittinger <cg@exept.de>
parents:
400
diff
changeset
|
334 |
arguments:(Array with:key with:nil with:nil) |
400 | 335 |
view:destinationView |
336 |
] |
|
139 | 337 |
] |
400 | 338 |
|
490
1d2ed059a063
delegated keyboardMessages get nil as x/y
Claus Gittinger <cg@exept.de>
parents:
400
diff
changeset
|
339 |
"Modified: 4.3.1996 / 22:18:37 / cg" |
128 | 340 |
! |
341 |
||
342 |
keyRelease:key x:x y:y view:aView |
|
139 | 343 |
"handle a delegated event - this is sent by the sensor to actually |
344 |
forward the event (i.e. after I returned true on handlesKeyRelease:. |
|
345 |
Take care of cyclic delegation (via a kludge-test for negative coordinate)." |
|
346 |
||
128 | 347 |
x < 0 ifTrue:[ |
400 | 348 |
" |
349 |
already delegated ... ignore |
|
350 |
" |
|
351 |
^ self |
|
352 |
]. |
|
353 |
||
354 |
filter notNil ifTrue:[ |
|
355 |
(filter value:key) ifFalse:[^ self]. |
|
128 | 356 |
]. |
357 |
||
139 | 358 |
destination notNil ifTrue:[ |
400 | 359 |
destination keyRelease:key x:-1 y:-1 view:aView |
139 | 360 |
] ifFalse:[ |
400 | 361 |
destinationView notNil ifTrue:[ |
362 |
WindowEvent |
|
363 |
sendEvent:#keyRelease:x:y: |
|
364 |
arguments:(Array with:key with:-1 with:-1) |
|
365 |
view:destinationView |
|
366 |
] |
|
139 | 367 |
] |
400 | 368 |
|
369 |
"Modified: 4.2.1996 / 20:35:55 / cg" |
|
128 | 370 |
! ! |
244 | 371 |
|
385
15fd1c806d2a
pass info if focus change was explicit (i.e. Tab-key) or by window manager.
Claus Gittinger <cg@exept.de>
parents:
251
diff
changeset
|
372 |
!KeyboardForwarder methodsFor:'focus forwarding'! |
15fd1c806d2a
pass info if focus change was explicit (i.e. Tab-key) or by window manager.
Claus Gittinger <cg@exept.de>
parents:
251
diff
changeset
|
373 |
|
15fd1c806d2a
pass info if focus change was explicit (i.e. Tab-key) or by window manager.
Claus Gittinger <cg@exept.de>
parents:
251
diff
changeset
|
374 |
showFocus:explicit |
15fd1c806d2a
pass info if focus change was explicit (i.e. Tab-key) or by window manager.
Claus Gittinger <cg@exept.de>
parents:
251
diff
changeset
|
375 |
destinationView notNil ifTrue:[destinationView showFocus:explicit] |
15fd1c806d2a
pass info if focus change was explicit (i.e. Tab-key) or by window manager.
Claus Gittinger <cg@exept.de>
parents:
251
diff
changeset
|
376 |
|
15fd1c806d2a
pass info if focus change was explicit (i.e. Tab-key) or by window manager.
Claus Gittinger <cg@exept.de>
parents:
251
diff
changeset
|
377 |
"Created: 27.1.1996 / 13:06:53 / cg" |
15fd1c806d2a
pass info if focus change was explicit (i.e. Tab-key) or by window manager.
Claus Gittinger <cg@exept.de>
parents:
251
diff
changeset
|
378 |
! |
15fd1c806d2a
pass info if focus change was explicit (i.e. Tab-key) or by window manager.
Claus Gittinger <cg@exept.de>
parents:
251
diff
changeset
|
379 |
|
15fd1c806d2a
pass info if focus change was explicit (i.e. Tab-key) or by window manager.
Claus Gittinger <cg@exept.de>
parents:
251
diff
changeset
|
380 |
showNoFocus:explicit |
15fd1c806d2a
pass info if focus change was explicit (i.e. Tab-key) or by window manager.
Claus Gittinger <cg@exept.de>
parents:
251
diff
changeset
|
381 |
destinationView notNil ifTrue:[destinationView showNoFocus:explicit] |
15fd1c806d2a
pass info if focus change was explicit (i.e. Tab-key) or by window manager.
Claus Gittinger <cg@exept.de>
parents:
251
diff
changeset
|
382 |
|
15fd1c806d2a
pass info if focus change was explicit (i.e. Tab-key) or by window manager.
Claus Gittinger <cg@exept.de>
parents:
251
diff
changeset
|
383 |
"Created: 27.1.1996 / 13:07:52 / cg" |
15fd1c806d2a
pass info if focus change was explicit (i.e. Tab-key) or by window manager.
Claus Gittinger <cg@exept.de>
parents:
251
diff
changeset
|
384 |
! ! |
15fd1c806d2a
pass info if focus change was explicit (i.e. Tab-key) or by window manager.
Claus Gittinger <cg@exept.de>
parents:
251
diff
changeset
|
385 |
|
244 | 386 |
!KeyboardForwarder methodsFor:'queries'! |
387 |
||
388 |
checkCondition:type key:key view:aView |
|
400 | 389 |
filter notNil ifTrue:[ |
390 |
(filter value:key) ifFalse:[^ false]. |
|
391 |
]. |
|
392 |
||
244 | 393 |
condition notNil ifTrue:[ |
400 | 394 |
condition == #noFocus ifTrue:[ |
395 |
aView windowGroup focusView notNil ifTrue:[^ false] |
|
396 |
]. |
|
397 |
condition isBlock ifTrue:[ |
|
398 |
(condition value:type value:key value:aView) ifFalse:[^ false] |
|
399 |
] |
|
244 | 400 |
]. |
401 |
sourceView notNil ifTrue:[ |
|
400 | 402 |
^ aView == sourceView |
244 | 403 |
]. |
404 |
^ true |
|
400 | 405 |
|
406 |
"Modified: 4.2.1996 / 20:36:16 / cg" |
|
244 | 407 |
! |
408 |
||
409 |
delegatesTo:someone |
|
410 |
"return true, if I delegate events to someone" |
|
411 |
||
412 |
^ destination == someone or:[destinationView == someone] |
|
413 |
! |
|
414 |
||
415 |
handlesKeyPress:key inView:aView |
|
416 |
"this is the query from the sensor to ask me if I would like to |
|
417 |
get a keyPress event for key from aView. Return true, if I want so, |
|
418 |
false otherwise." |
|
419 |
||
420 |
^ self checkCondition:#keyPress key:key view:aView |
|
421 |
! |
|
422 |
||
423 |
handlesKeyRelease:key inView:aView |
|
424 |
"this is the query from the sensor to ask me if I would like to |
|
425 |
get a keyRelease event for key from aView. Return true, if I want so, |
|
426 |
false otherwise." |
|
427 |
||
428 |
^ self checkCondition:#keyRelease key:key view:aView |
|
429 |
! ! |
|
430 |
||
251 | 431 |
!KeyboardForwarder class methodsFor:'documentation'! |
432 |
||
433 |
version |
|
620 | 434 |
^ '$Header: /cvs/stx/stx/libview/KeyboardForwarder.st,v 1.15 1996-04-27 17:56:29 cg Exp $' |
251 | 435 |
! ! |