author | Claus Gittinger <cg@exept.de> |
Sat, 27 Jan 1996 16:35:09 +0100 | |
changeset 385 | 15fd1c806d2a |
parent 251 | 915de9a65169 |
child 400 | 47fa508fd78b |
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 |
|
139 | 15 |
instanceVariableNames:'sourceView destinationView destination condition' |
128 | 16 |
classVariableNames:'' |
17 |
poolDictionaries:'' |
|
18 |
category:'Interface-Support' |
|
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 |
||
189 |
^ self new destination:destination |
|
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 |
||
200 |
^ self new destination:destination; condition:aCondition |
|
201 |
! |
|
202 |
||
203 |
toView:destinationView |
|
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). The forwarded event will be reported excluding |
|
207 |
the original view as argument (i.e. as #keyPress:x:y:). |
|
208 |
Use this, if the destination is a view." |
|
209 |
||
210 |
^ self new destinationView:destinationView |
|
211 |
! |
|
212 |
||
144 | 213 |
toView:destinationView condition:aCondition |
139 | 214 |
"create and return a new KeyboardForwarder to redirect any key event |
215 |
to destinationView (Independent of the view in which the event originally |
|
144 | 216 |
occurred) but only, if some condition as specified by aCondition |
139 | 217 |
is met. The forwarded event will be reported excluding |
218 |
the original view as argument (i.e. as #keyPress:x:y:). |
|
219 |
Use this, if the destination is a view." |
|
220 |
||
144 | 221 |
^ self new destinationView:destinationView; condition:aCondition |
128 | 222 |
! ! |
223 |
||
144 | 224 |
!KeyboardForwarder methodsFor:'accessing'! |
128 | 225 |
|
134 | 226 |
condition:aCondition |
144 | 227 |
"set the condition - typically a block" |
228 |
||
134 | 229 |
condition := aCondition |
230 |
! |
|
231 |
||
244 | 232 |
destination |
233 |
"return the destination" |
|
144 | 234 |
|
244 | 235 |
^ destination |
139 | 236 |
! |
237 |
||
238 |
destination:anObject |
|
144 | 239 |
"set the destination |
240 |
- that one will get the event with an additional view arg." |
|
241 |
||
139 | 242 |
destination := anObject |
243 |
! |
|
244 |
||
244 | 245 |
destinationView |
246 |
"return the destination view" |
|
144 | 247 |
|
244 | 248 |
^ destinationView |
139 | 249 |
! |
250 |
||
244 | 251 |
destinationView:aView |
252 |
"set the destination view" |
|
144 | 253 |
|
244 | 254 |
destinationView := aView |
128 | 255 |
! |
256 |
||
257 |
sourceView |
|
144 | 258 |
"get the sourceView - if nonNil, only events from this view will be forwarded" |
259 |
||
128 | 260 |
^ sourceView |
142 | 261 |
! |
262 |
||
244 | 263 |
sourceView:aView |
264 |
"set the sourceView - if nonNil, only events from this view will be forwarded" |
|
128 | 265 |
|
244 | 266 |
sourceView := aView |
128 | 267 |
! ! |
268 |
||
269 |
!KeyboardForwarder methodsFor:'event forwarding'! |
|
270 |
||
271 |
keyPress:key x:x y:y view:aView |
|
139 | 272 |
"handle a delegated event - this is sent by the sensor to actually |
273 |
forward the event (i.e. after I returned true on handlesKeyPress:. |
|
274 |
Take care of cyclic delegation (via a kludge-test for negative coordinate)." |
|
275 |
||
128 | 276 |
x < 0 ifTrue:[ |
277 |
" |
|
278 |
already delegated ... ignore |
|
279 |
" |
|
280 |
^ self |
|
281 |
]. |
|
282 |
||
139 | 283 |
destination notNil ifTrue:[ |
284 |
destination keyPress:key x:-1 y:-1 view:aView. |
|
285 |
] ifFalse:[ |
|
144 | 286 |
destinationView notNil ifTrue:[ |
287 |
WindowEvent |
|
288 |
sendEvent:#keyPress:x:y: |
|
289 |
arguments:(Array with:key with:-1 with:-1) |
|
290 |
view:destinationView |
|
291 |
] |
|
139 | 292 |
] |
128 | 293 |
! |
294 |
||
295 |
keyRelease:key x:x y:y view:aView |
|
139 | 296 |
"handle a delegated event - this is sent by the sensor to actually |
297 |
forward the event (i.e. after I returned true on handlesKeyRelease:. |
|
298 |
Take care of cyclic delegation (via a kludge-test for negative coordinate)." |
|
299 |
||
128 | 300 |
x < 0 ifTrue:[ |
301 |
" |
|
302 |
already delegated ... ignore |
|
303 |
" |
|
304 |
^ self |
|
305 |
]. |
|
306 |
||
139 | 307 |
destination notNil ifTrue:[ |
144 | 308 |
destination keyRelease:key x:-1 y:-1 view:aView |
139 | 309 |
] ifFalse:[ |
144 | 310 |
destinationView notNil ifTrue:[ |
311 |
WindowEvent |
|
312 |
sendEvent:#keyRelease:x:y: |
|
313 |
arguments:(Array with:key with:-1 with:-1) |
|
314 |
view:destinationView |
|
315 |
] |
|
139 | 316 |
] |
128 | 317 |
! ! |
244 | 318 |
|
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
|
319 |
!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
|
320 |
|
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
|
321 |
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
|
322 |
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
|
323 |
|
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
|
324 |
"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
|
325 |
! |
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
|
326 |
|
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
|
327 |
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
|
328 |
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
|
329 |
|
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
|
330 |
"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
|
331 |
! ! |
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
|
332 |
|
244 | 333 |
!KeyboardForwarder methodsFor:'queries'! |
334 |
||
335 |
checkCondition:type key:key view:aView |
|
336 |
condition notNil ifTrue:[ |
|
337 |
condition == #noFocus ifTrue:[ |
|
338 |
aView windowGroup focusView notNil ifTrue:[^ false] |
|
339 |
]. |
|
340 |
condition isBlock ifTrue:[ |
|
341 |
(condition value:type value:key value:aView) ifFalse:[^ false] |
|
342 |
] |
|
343 |
]. |
|
344 |
sourceView notNil ifTrue:[ |
|
345 |
^ aView == sourceView |
|
346 |
]. |
|
347 |
^ true |
|
348 |
! |
|
349 |
||
350 |
delegatesTo:someone |
|
351 |
"return true, if I delegate events to someone" |
|
352 |
||
353 |
^ destination == someone or:[destinationView == someone] |
|
354 |
! |
|
355 |
||
356 |
handlesKeyPress:key inView:aView |
|
357 |
"this is the query from the sensor to ask me if I would like to |
|
358 |
get a keyPress event for key from aView. Return true, if I want so, |
|
359 |
false otherwise." |
|
360 |
||
361 |
^ self checkCondition:#keyPress key:key view:aView |
|
362 |
! |
|
363 |
||
364 |
handlesKeyRelease:key inView:aView |
|
365 |
"this is the query from the sensor to ask me if I would like to |
|
366 |
get a keyRelease event for key from aView. Return true, if I want so, |
|
367 |
false otherwise." |
|
368 |
||
369 |
^ self checkCondition:#keyRelease key:key view:aView |
|
370 |
! ! |
|
371 |
||
251 | 372 |
!KeyboardForwarder class methodsFor:'documentation'! |
373 |
||
374 |
version |
|
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
|
375 |
^ '$Header: /cvs/stx/stx/libview/KeyboardForwarder.st,v 1.10 1996-01-27 15:34:55 cg Exp $' |
251 | 376 |
! ! |