author | Claus Gittinger <cg@exept.de> |
Mon, 04 Mar 1996 23:31:36 +0100 | |
changeset 490 | 1d2ed059a063 |
parent 400 | 47fa508fd78b |
child 598 | 1a2339e902d4 |
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 |
||
128 | 67 |
Instance Variables: |
139 | 68 |
|
128 | 69 |
destinationView <View> the view which shall receive |
70 |
the forwarded keyboard events. |
|
134 | 71 |
|
139 | 72 |
destination <Object> the object which shall receive |
73 |
the forwarded keyboard events. |
|
74 |
||
134 | 75 |
sourceView <ViewOrNil> if non-nil, only events from this |
76 |
view are forwarded. |
|
77 |
(currently nowhere used) |
|
78 |
||
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 |
|
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 |
|
91 |
||
92 |
For more info on event handling/forwarding, see the documentation in |
|
93 |
WindowSensor, WindowGroup and especially in WindowEvent. |
|
94 |
" |
|
95 |
! |
|
96 |
||
97 |
examples |
|
98 |
" |
|
99 |
catch an enterfields input: |
|
100 |
||
101 |
|top field forwarder catcher| |
|
102 |
||
103 |
catcher := Plug new. |
|
104 |
catcher respondTo:#keyPress:x:y:view: |
|
105 |
with:[:key :x :y :view | Transcript showCr:'cought keyPress: ' , key printString.]. |
|
106 |
catcher respondTo:#keyRelease:x:y:view: |
|
107 |
with:[:key :x :y :view | Transcript showCr:'cought keyRelease: ' , key printString.]. |
|
108 |
||
109 |
top := StandardSystemView new. |
|
110 |
top extent:200@200. |
|
111 |
||
112 |
field := EditField in:top. |
|
113 |
field origin:0.0@0.0; width:1.0. |
|
114 |
||
115 |
forwarder := KeyboardForwarder to:catcher. |
|
116 |
field delegate:forwarder. |
|
117 |
||
118 |
top open |
|
119 |
||
120 |
||
121 |
catch an enterfields return-key only: |
|
122 |
||
123 |
|top field forwarder catcher| |
|
124 |
||
125 |
catcher := Plug new. |
|
126 |
catcher respondTo:#keyPress:x:y:view: |
|
127 |
with:[:key :x :y :view | Transcript showCr:'cought return']. |
|
128 |
||
129 |
top := StandardSystemView new. |
|
130 |
top extent:200@200. |
|
131 |
||
132 |
field := EditField in:top. |
|
133 |
field origin:0.0@0.0; width:1.0. |
|
134 |
||
135 |
forwarder := KeyboardForwarder |
|
136 |
to:catcher |
|
137 |
condition:[:type :key :view | (type == #keyPress) & (key == #Return)]. |
|
138 |
field delegate:forwarder. |
|
139 |
||
140 |
top open |
|
141 |
||
142 |
||
143 |
catch all of an enterfields input and ignore it: |
|
144 |
||
145 |
|top field forwarder| |
|
146 |
||
147 |
top := StandardSystemView new. |
|
148 |
top extent:200@200. |
|
149 |
||
150 |
field := EditField in:top. |
|
151 |
field origin:0.0@0.0; width:1.0. |
|
152 |
||
153 |
forwarder := KeyboardForwarder to:nil. |
|
154 |
field delegate:forwarder. |
|
155 |
||
156 |
top open |
|
128 | 157 |
" |
158 |
! ! |
|
159 |
||
160 |
!KeyboardForwarder class methodsFor:'instance creation'! |
|
161 |
||
139 | 162 |
from:sourceView to:destination |
163 |
"create and return a new KeyboardForwarder to redirect key events |
|
164 |
for sourceView to destination. Events from other than the sourceView |
|
165 |
will not be forwarded. The forwarded event will be reported including |
|
166 |
the original view as argument (i.e. as #keyPress:x:y:view:). |
|
167 |
Use this, if the destination is not a view." |
|
168 |
||
169 |
^ self new sourceView:sourceView; destination:destination |
|
170 |
! |
|
171 |
||
244 | 172 |
from:sourceView toView:destinationView |
173 |
"create and return a new KeyboardForwarder to redirect key events |
|
174 |
for sourceView to destinationView. Events from other than the sourceView |
|
175 |
will not be forwarded. The forwarded event will be reported excluding |
|
139 | 176 |
the original view as argument (i.e. as #keyPress:x:y:). |
177 |
Use this, if the destination is a view." |
|
178 |
||
244 | 179 |
^ self new sourceView:sourceView; destinationView:destinationView |
134 | 180 |
! |
181 |
||
139 | 182 |
to:destination |
183 |
"create and return a new KeyboardForwarder to redirect any key event |
|
184 |
to destination (Independent of the view in which the event originally |
|
185 |
occurred). The forwarded event will be reported including |
|
186 |
the original view as argument (i.e. as #keyPress:x:y:view:). |
|
187 |
Use this, if the destination is not a view." |
|
188 |
||
400 | 189 |
^ self to:destination condition:nil filter:nil |
139 | 190 |
! |
191 |
||
244 | 192 |
to:destination condition:aCondition |
193 |
"create and return a new KeyboardForwarder to redirect any key event |
|
194 |
to destinationView (Independent of the view in which the event originally |
|
195 |
occurred) but only, if some condition as specified by aCondition |
|
196 |
is met. The forwarded event will be reported including |
|
197 |
the original view as argument (i.e. as #keyPress:x:y:view:). |
|
198 |
Use this, if the destination is not a view." |
|
199 |
||
400 | 200 |
^ self to:destination condition:aCondition filter:nil |
201 |
! |
|
202 |
||
203 |
to:destination condition:aCondition filter:aFilterBlock |
|
204 |
"create and return a new KeyboardForwarder to redirect any key event |
|
205 |
to destinationView (Independent of the view in which the event originally |
|
206 |
occurred) but only, if some condition as specified by aCondition |
|
207 |
is met and aFilterBlock returns true for that key. |
|
208 |
The forwarded event will be reported including |
|
209 |
the original view as argument (i.e. as #keyPress:x:y:view:). |
|
210 |
Use this, if the destination is not a view." |
|
211 |
||
212 |
^ self new destination:destination; condition:aCondition; filter:aFilterBlock |
|
244 | 213 |
! |
214 |
||
215 |
toView:destinationView |
|
216 |
"create and return a new KeyboardForwarder to redirect any key event |
|
217 |
to destinationView (Independent of the view in which the event originally |
|
218 |
occurred). The forwarded event will be reported excluding |
|
219 |
the original view as argument (i.e. as #keyPress:x:y:). |
|
220 |
Use this, if the destination is a view." |
|
221 |
||
400 | 222 |
^ self toView:destinationView condition:nil filter:nil |
244 | 223 |
! |
224 |
||
144 | 225 |
toView:destinationView condition:aCondition |
139 | 226 |
"create and return a new KeyboardForwarder to redirect any key event |
227 |
to destinationView (Independent of the view in which the event originally |
|
144 | 228 |
occurred) but only, if some condition as specified by aCondition |
139 | 229 |
is met. The forwarded event will be reported excluding |
230 |
the original view as argument (i.e. as #keyPress:x:y:). |
|
231 |
Use this, if the destination is a view." |
|
232 |
||
400 | 233 |
^ self toView:destinationView condition:aCondition filter:nil |
234 |
! |
|
235 |
||
236 |
toView:destinationView condition:aCondition filter:aFilterBlock |
|
237 |
"create and return a new KeyboardForwarder to redirect any key event |
|
238 |
to destinationView (Independent of the view in which the event originally |
|
239 |
occurred) but only, if some condition as specified by aCondition |
|
240 |
is met and aFilterBlock returns true for that key. |
|
241 |
The forwarded event will be reported excluding |
|
242 |
the original view as argument (i.e. as #keyPress:x:y:). |
|
243 |
Use this, if the destination is a view." |
|
244 |
||
245 |
^ self new destinationView:destinationView; condition:aCondition; filter:aFilterBlock |
|
128 | 246 |
! ! |
247 |
||
144 | 248 |
!KeyboardForwarder methodsFor:'accessing'! |
128 | 249 |
|
134 | 250 |
condition:aCondition |
144 | 251 |
"set the condition - typically a block" |
252 |
||
134 | 253 |
condition := aCondition |
254 |
! |
|
255 |
||
244 | 256 |
destination |
257 |
"return the destination" |
|
144 | 258 |
|
244 | 259 |
^ destination |
139 | 260 |
! |
261 |
||
262 |
destination:anObject |
|
144 | 263 |
"set the destination |
264 |
- that one will get the event with an additional view arg." |
|
265 |
||
139 | 266 |
destination := anObject |
267 |
! |
|
268 |
||
244 | 269 |
destinationView |
270 |
"return the destination view" |
|
144 | 271 |
|
244 | 272 |
^ destinationView |
139 | 273 |
! |
274 |
||
244 | 275 |
destinationView:aView |
276 |
"set the destination view" |
|
144 | 277 |
|
244 | 278 |
destinationView := aView |
128 | 279 |
! |
280 |
||
400 | 281 |
filter:aOneArgBlock |
282 |
"set the filter - if non-nil, only keys for which it returns true are forwarded" |
|
283 |
||
284 |
filter := aOneArgBlock |
|
285 |
||
286 |
"Created: 4.2.1996 / 20:35:15 / cg" |
|
287 |
! |
|
288 |
||
128 | 289 |
sourceView |
144 | 290 |
"get the sourceView - if nonNil, only events from this view will be forwarded" |
291 |
||
128 | 292 |
^ sourceView |
142 | 293 |
! |
294 |
||
244 | 295 |
sourceView:aView |
296 |
"set the sourceView - if nonNil, only events from this view will be forwarded" |
|
128 | 297 |
|
244 | 298 |
sourceView := aView |
128 | 299 |
! ! |
300 |
||
301 |
!KeyboardForwarder methodsFor:'event forwarding'! |
|
302 |
||
303 |
keyPress:key x:x y:y view:aView |
|
139 | 304 |
"handle a delegated event - this is sent by the sensor to actually |
305 |
forward the event (i.e. after I returned true on handlesKeyPress:. |
|
306 |
Take care of cyclic delegation (via a kludge-test for negative coordinate)." |
|
307 |
||
490
1d2ed059a063
delegated keyboardMessages get nil as x/y
Claus Gittinger <cg@exept.de>
parents:
400
diff
changeset
|
308 |
x isNil ifTrue:[ |
400 | 309 |
" |
310 |
already delegated ... ignore |
|
311 |
" |
|
312 |
^ self |
|
313 |
]. |
|
314 |
||
315 |
filter notNil ifTrue:[ |
|
316 |
(filter value:key) ifFalse:[^ self]. |
|
128 | 317 |
]. |
318 |
||
139 | 319 |
destination notNil ifTrue:[ |
490
1d2ed059a063
delegated keyboardMessages get nil as x/y
Claus Gittinger <cg@exept.de>
parents:
400
diff
changeset
|
320 |
destination keyPress:key x:nil y:nil view:aView. |
139 | 321 |
] ifFalse:[ |
400 | 322 |
destinationView notNil ifTrue:[ |
323 |
WindowEvent |
|
324 |
sendEvent:#keyPress:x:y: |
|
490
1d2ed059a063
delegated keyboardMessages get nil as x/y
Claus Gittinger <cg@exept.de>
parents:
400
diff
changeset
|
325 |
arguments:(Array with:key with:nil with:nil) |
400 | 326 |
view:destinationView |
327 |
] |
|
139 | 328 |
] |
400 | 329 |
|
490
1d2ed059a063
delegated keyboardMessages get nil as x/y
Claus Gittinger <cg@exept.de>
parents:
400
diff
changeset
|
330 |
"Modified: 4.3.1996 / 22:18:37 / cg" |
128 | 331 |
! |
332 |
||
333 |
keyRelease:key x:x y:y view:aView |
|
139 | 334 |
"handle a delegated event - this is sent by the sensor to actually |
335 |
forward the event (i.e. after I returned true on handlesKeyRelease:. |
|
336 |
Take care of cyclic delegation (via a kludge-test for negative coordinate)." |
|
337 |
||
128 | 338 |
x < 0 ifTrue:[ |
400 | 339 |
" |
340 |
already delegated ... ignore |
|
341 |
" |
|
342 |
^ self |
|
343 |
]. |
|
344 |
||
345 |
filter notNil ifTrue:[ |
|
346 |
(filter value:key) ifFalse:[^ self]. |
|
128 | 347 |
]. |
348 |
||
139 | 349 |
destination notNil ifTrue:[ |
400 | 350 |
destination keyRelease:key x:-1 y:-1 view:aView |
139 | 351 |
] ifFalse:[ |
400 | 352 |
destinationView notNil ifTrue:[ |
353 |
WindowEvent |
|
354 |
sendEvent:#keyRelease:x:y: |
|
355 |
arguments:(Array with:key with:-1 with:-1) |
|
356 |
view:destinationView |
|
357 |
] |
|
139 | 358 |
] |
400 | 359 |
|
360 |
"Modified: 4.2.1996 / 20:35:55 / cg" |
|
128 | 361 |
! ! |
244 | 362 |
|
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
|
363 |
!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
|
364 |
|
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
|
365 |
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
|
366 |
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
|
367 |
|
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
|
368 |
"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
|
369 |
! |
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
|
370 |
|
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
|
371 |
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
|
372 |
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
|
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 |
"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
|
375 |
! ! |
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 |
|
244 | 377 |
!KeyboardForwarder methodsFor:'queries'! |
378 |
||
379 |
checkCondition:type key:key view:aView |
|
400 | 380 |
filter notNil ifTrue:[ |
381 |
(filter value:key) ifFalse:[^ false]. |
|
382 |
]. |
|
383 |
||
244 | 384 |
condition notNil ifTrue:[ |
400 | 385 |
condition == #noFocus ifTrue:[ |
386 |
aView windowGroup focusView notNil ifTrue:[^ false] |
|
387 |
]. |
|
388 |
condition isBlock ifTrue:[ |
|
389 |
(condition value:type value:key value:aView) ifFalse:[^ false] |
|
390 |
] |
|
244 | 391 |
]. |
392 |
sourceView notNil ifTrue:[ |
|
400 | 393 |
^ aView == sourceView |
244 | 394 |
]. |
395 |
^ true |
|
400 | 396 |
|
397 |
"Modified: 4.2.1996 / 20:36:16 / cg" |
|
244 | 398 |
! |
399 |
||
400 |
delegatesTo:someone |
|
401 |
"return true, if I delegate events to someone" |
|
402 |
||
403 |
^ destination == someone or:[destinationView == someone] |
|
404 |
! |
|
405 |
||
406 |
handlesKeyPress:key inView:aView |
|
407 |
"this is the query from the sensor to ask me if I would like to |
|
408 |
get a keyPress event for key from aView. Return true, if I want so, |
|
409 |
false otherwise." |
|
410 |
||
411 |
^ self checkCondition:#keyPress key:key view:aView |
|
412 |
! |
|
413 |
||
414 |
handlesKeyRelease:key inView:aView |
|
415 |
"this is the query from the sensor to ask me if I would like to |
|
416 |
get a keyRelease event for key from aView. Return true, if I want so, |
|
417 |
false otherwise." |
|
418 |
||
419 |
^ self checkCondition:#keyRelease key:key view:aView |
|
420 |
! ! |
|
421 |
||
251 | 422 |
!KeyboardForwarder class methodsFor:'documentation'! |
423 |
||
424 |
version |
|
490
1d2ed059a063
delegated keyboardMessages get nil as x/y
Claus Gittinger <cg@exept.de>
parents:
400
diff
changeset
|
425 |
^ '$Header: /cvs/stx/stx/libview/KeyboardForwarder.st,v 1.12 1996-03-04 22:31:36 cg Exp $' |
251 | 426 |
! ! |