author | Claus Gittinger <cg@exept.de> |
Sat, 11 Nov 1995 16:54:10 +0100 | |
changeset 219 | 9ff0660f447f |
parent 152 | 17cc0709e898 |
child 244 | 83218faf792c |
permissions | -rw-r--r-- |
26 | 1 |
" |
2 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
72 | 3 |
All Rights Reserved |
26 | 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 |
" |
|
11 | 12 |
|
13 |
Object subclass:#WindowEvent |
|
72 | 14 |
instanceVariableNames:'view type arguments' |
15 |
classVariableNames:'' |
|
16 |
poolDictionaries:'' |
|
17 |
category:'Interface-Support' |
|
11 | 18 |
! |
19 |
||
46 | 20 |
!WindowEvent class methodsFor:'documentation'! |
21 |
||
22 |
copyright |
|
23 |
" |
|
24 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
72 | 25 |
All Rights Reserved |
46 | 26 |
|
27 |
This software is furnished under a license and may be used |
|
28 |
only in accordance with the terms of that license and with the |
|
29 |
inclusion of the above copyright notice. This software may not |
|
30 |
be provided or otherwise made available to, or used by, any |
|
31 |
other person. No title to or ownership of the software is |
|
32 |
hereby transferred. |
|
33 |
" |
|
34 |
! |
|
35 |
||
36 |
version |
|
219
9ff0660f447f
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
152
diff
changeset
|
37 |
^ '$Header: /cvs/stx/stx/libview/WindowEvent.st,v 1.20 1995-11-11 15:53:22 cg Exp $' |
46 | 38 |
! |
39 |
||
40 |
documentation |
|
41 |
" |
|
42 |
Instances of WindowEvent are created for every event coming from |
|
144 | 43 |
the graphics device. |
44 |
Usually, they are enqueued by the event dispatcher process into |
|
45 |
a sensors input queue, and dequeued & processed by a windowGroup process |
|
46 |
in its event loop. |
|
47 |
||
48 |
WindowEvents hold the event type and additional information (such as key, |
|
49 |
x/y coordinates etc). Also, windowEvents know how to send themself to some |
|
50 |
destination. To provide a common (single) place where event dispatching is |
|
51 |
implemented, this forwarding is done by a clas method here (i.e. not by the |
|
52 |
window group itself). |
|
53 |
||
54 |
The algorithm for event dispatching is: |
|
55 |
||
56 |
- if the destination view has a keyboard focus set, |
|
57 |
AND the event is a keyboard event, |
|
58 |
THEN recursively invoke the event dispatching method, |
|
59 |
sending the event to the focus view (or its delegate, as below) |
|
60 |
||
61 |
- if the destination view has a delegate, |
|
62 |
AND its a keyboard, button or pointer event, |
|
63 |
AND the delegate is interrested in that event |
|
64 |
(i.e. implements & responds to #handlesXXX with true) |
|
65 |
THEN send the event to the delegate, passing the original view |
|
66 |
as additional argument |
|
67 |
||
68 |
- if the view has a nonNil controller, |
|
69 |
AND its a key, button or pointer event, |
|
70 |
THEN send the event to the controller |
|
71 |
||
72 |
- otherwise send the event to the view |
|
73 |
||
74 |
||
75 |
If the view has a non-nil transformation, the event is sent as a |
|
76 |
#deviceXXX message, passing device coordinates. Typically, subclasses |
|
77 |
of view do not redefine this method and the inherited default method |
|
78 |
translates these device coordinates into logical coordinates and resends |
|
79 |
an XXX message. |
|
80 |
If the view has no transformation, the XXX message is directly sent here. |
|
81 |
||
82 |
For example, a 'buttonPress:button x:x y:y' event leads to sending of |
|
83 |
'aView deviceButtonPress:button x:x y:y' which resends |
|
84 |
'aView buttonPress:button x:(x-logical) y:(y-logical)' |
|
85 |
||
86 |
This allows views which are interrested in deviceCoordinates to get them |
|
87 |
(by redefining #deviceXXX) and other views to transparently get & use |
|
88 |
logical coordinates. |
|
89 |
||
90 |
Therefore, for a delegated keyPress messages, the flow is: |
|
91 |
||
92 |
sendEvent |
|
93 |
view has delegate |
|
94 |
------> ask delegate via 'handlesKeyPress:key inView:view' |
|
95 |
<------ returns true |
|
96 |
------> 'delegate keyPress:key x:x y:y view:view' |
|
97 |
-----> delegate does whatever it wants to do |
|
98 |
(typically sends the event to some other view) |
|
99 |
||
100 |
for an undelegated message: |
|
101 |
||
102 |
sendEvent |
|
103 |
view has delegate |
|
104 |
------> ask delegate via 'handlesKeyPress:key inView:view' |
|
105 |
<------ returns false |
|
106 |
view has controller |
|
107 |
------> 'controller keyPress:key x:x y:y' |
|
108 |
view has no controller |
|
109 |
view has transformation |
|
110 |
----> 'view deviceKeyPress:key x:x y:y' |
|
111 |
inverse transform x/y |
|
112 |
----> 'self keyPress:key x:xLogical y:yLogical' |
|
113 |
view has no transformation |
|
114 |
----> 'view keyPress:key x:x y:y' |
|
46 | 115 |
" |
116 |
! ! |
|
117 |
||
11 | 118 |
!WindowEvent class methodsFor:'instance creation'! |
119 |
||
120 |
for:aView type:aSymbol arguments:argArray |
|
121 |
"create and return a new windowEvent for sending |
|
122 |
aSymbol-message with arguments to aView" |
|
123 |
||
46 | 124 |
^ (self new) for:aView type:aSymbol arguments:argArray |
125 |
! |
|
126 |
||
72 | 127 |
for:aView type:aSymbol |
128 |
"create and return a new windowEvent for sending |
|
129 |
aSymbol-message with no arguments to aView" |
|
130 |
||
131 |
^ (self new) for:aView type:aSymbol arguments:#() |
|
132 |
! |
|
133 |
||
46 | 134 |
damageFor:aView rectangle:aRectangle |
135 |
"create and return a new damage Event for aRectangle |
|
136 |
in aView" |
|
137 |
||
138 |
^ (self new) for:aView type:#damage arguments:aRectangle |
|
139 |
||
11 | 140 |
! ! |
141 |
||
72 | 142 |
!WindowEvent methodsFor:'queries'! |
143 |
||
144 |
isKeyEvent |
|
145 |
"return true, if this event is a keyboard event" |
|
146 |
||
89 | 147 |
^ (type == #keyPress:x:y:) or:[type == #keyRelease:x:y:] |
148 |
! |
|
149 |
||
150 |
isKeyPressEvent |
|
151 |
"return true, if this event is a keyboard event" |
|
152 |
||
153 |
^ (type == #keyPress:x:y:) |
|
154 |
! |
|
155 |
||
156 |
isButtonEvent |
|
157 |
"return true, if this event is a button event" |
|
158 |
||
159 |
^ (type == #buttonPress:x:y:) |
|
160 |
or:[type == #buttonRelease:x:y: |
|
161 |
or:[type == #'buttonShiftPress:x:y:' |
|
162 |
or:[type == #'buttonMultiPress:x:y:' |
|
163 |
or:[type == #'buttonMotion:x:y:']]]] |
|
72 | 164 |
! |
165 |
||
166 |
isDamage |
|
167 |
"return true, if this is a damage event" |
|
168 |
||
169 |
^ type == #damage |
|
170 |
! ! |
|
171 |
||
28 | 172 |
!WindowEvent methodsFor:'accessing'! |
173 |
||
174 |
view |
|
175 |
"return the view, for which the event is for" |
|
176 |
||
177 |
^ view |
|
178 |
! |
|
179 |
||
72 | 180 |
view:aView |
181 |
"set the view, for which the event is for" |
|
182 |
||
183 |
view := aView |
|
184 |
! |
|
185 |
||
28 | 186 |
type |
187 |
"return the type of the event" |
|
188 |
||
189 |
^ type |
|
190 |
! |
|
191 |
||
192 |
arguments |
|
193 |
"return the arguments of the event" |
|
194 |
||
195 |
^ arguments |
|
196 |
! |
|
197 |
||
198 |
arguments:anArray |
|
199 |
"set the arguments" |
|
200 |
||
201 |
arguments := anArray |
|
46 | 202 |
! |
203 |
||
204 |
rectangle |
|
205 |
"return the damage rectangle" |
|
206 |
||
207 |
^ arguments "consider this a kludge" |
|
89 | 208 |
! |
209 |
||
210 |
key |
|
211 |
"return the key of the key-event. For non key-events, nil is returned." |
|
212 |
||
213 |
((type == #keyPress:x:y:) |
|
214 |
or:[type == #keyRelease:x:y:]) ifTrue:[ |
|
215 |
^ arguments at:1 |
|
216 |
]. |
|
217 |
^ nil |
|
28 | 218 |
! ! |
219 |
||
115 | 220 |
!WindowEvent class methodsFor:'forwarding events'! |
221 |
||
222 |
sendEvent:type arguments:arguments view:view |
|
223 |
"forward the event represented by type and arguments to the views delegate, |
|
224 |
the views controller or the view. |
|
11 | 225 |
|
115 | 226 |
If there is a delegate, only messages which are understood by it are |
227 |
forwarded. Also, the delegate is asked if it is willing to handle the event |
|
228 |
before. |
|
229 |
Delegated messages get the original view as an extra argument. |
|
230 |
Delegation has higher priority than controller forwarding." |
|
11 | 231 |
|
135 | 232 |
^ self |
233 |
sendEvent:type |
|
234 |
arguments:arguments |
|
235 |
view:view |
|
236 |
withFocusOn:nil |
|
237 |
delegate:true |
|
89 | 238 |
! |
239 |
||
115 | 240 |
sendEvent:type arguments:argArray view:view withFocusOn:focusView |
241 |
"forward the event represented by type and arguments to the views delegate, |
|
242 |
the views controller or the view. |
|
243 |
If focusView is nonNil, and it is a keyboard event, it is forwarded to this |
|
141 | 244 |
view (even if there is a delegate). |
115 | 245 |
|
89 | 246 |
If there is a delegate, only messages which are understood by it are |
115 | 247 |
forwarded. Also, the delegate is asked if it is willing to handle the event |
248 |
before. |
|
249 |
Delegated messages get the original view as an extra argument. |
|
89 | 250 |
Delegation has higher priority than both controller or focusView |
251 |
forwarding." |
|
252 |
||
135 | 253 |
^ self |
254 |
sendEvent:type |
|
255 |
arguments:argArray |
|
256 |
view:view |
|
257 |
withFocusOn:focusView |
|
258 |
delegate:true |
|
259 |
! |
|
260 |
||
261 |
sendEvent:type arguments:argArray view:view withFocusOn:focusView delegate:doDelegate |
|
262 |
"forward the event represented by type and arguments to the views delegate, |
|
263 |
the views controller or the view. |
|
264 |
If focusView is nonNil, and it is a keyboard event, it is forwarded to this |
|
265 |
view (but not if there was a delegate in the first place). |
|
266 |
||
267 |
If doDelegate is true, keyboard and button events are forwarded to a |
|
268 |
delegate object (if non-nil). DoDelegate may be passed as true, to |
|
269 |
handle events which are already delegated. |
|
270 |
If there is a delegate, only messages which are understood by it are |
|
271 |
forwarded. Also, the delegate is asked if it is willing to handle the event |
|
272 |
before. |
|
273 |
Delegated messages get the original view as an extra argument. |
|
274 |
Delegation has higher priority than both controller or focusView |
|
275 |
forwarding." |
|
276 |
||
115 | 277 |
|delegate selector delegateMessage delegateQuery |
278 |
eventReceiver controller deviceMessage |
|
279 |
isKeyEvent isButtonEvent isPointerEvent trans| |
|
280 |
||
281 |
isKeyEvent := isButtonEvent := isPointerEvent := false. |
|
11 | 282 |
|
115 | 283 |
(type == #'keyPress:x:y:') ifTrue:[ |
284 |
isKeyEvent := true. |
|
285 |
deviceMessage := #'deviceKeyPress:x:y:'. |
|
286 |
delegateMessage := #'keyPress:x:y:view:'. |
|
287 |
delegateQuery := #'handlesKeyPress:inView:'. |
|
288 |
] ifFalse:[ (type == #'keyRelease:x:y:') ifTrue:[ |
|
289 |
isKeyEvent := true. |
|
290 |
deviceMessage := #'deviceKeyRelease:x:y:'. |
|
291 |
delegateMessage := #'keyRelease:x:y:view:'. |
|
292 |
delegateQuery := #'handlesKeyRelease:inView:'. |
|
141 | 293 |
] ifFalse:[ (type == #'buttonMotion:x:y:') ifTrue:[ |
294 |
isButtonEvent := true. |
|
295 |
deviceMessage := #'deviceButtonMotion:x:y:'. |
|
296 |
delegateMessage := #'buttonMotion:x:y:view:'. |
|
297 |
delegateQuery := #'handlesButtonMotion:inView:'. |
|
115 | 298 |
] ifFalse:[ (type == #'buttonPress:x:y:') ifTrue:[ |
299 |
isButtonEvent := true. |
|
300 |
deviceMessage := #'deviceButtonPress:x:y:'. |
|
301 |
delegateMessage := #'buttonPress:x:y:view:'. |
|
302 |
delegateQuery := #'handlesButtonPress:inView:'. |
|
303 |
] ifFalse:[ (type == #'buttonRelease:x:y:') ifTrue:[ |
|
304 |
isButtonEvent := true. |
|
305 |
deviceMessage := #'deviceButtonRelease:x:y:'. |
|
306 |
delegateMessage := #'buttonRelease:x:y:view:'. |
|
307 |
delegateQuery := #'handlesButtonRelease:inView:'. |
|
308 |
] ifFalse:[ (type == #'buttonShiftPress:x:y:') ifTrue:[ |
|
309 |
isButtonEvent := true. |
|
310 |
deviceMessage := #'deviceButtonShiftPress:x:y:'. |
|
311 |
delegateMessage := #'buttonShiftPress:x:y:view:'. |
|
312 |
delegateQuery := #'handlesButtonShiftPress:inView:'. |
|
313 |
] ifFalse:[ (type == #'buttonMultiPress:x:y:') ifTrue:[ |
|
314 |
isButtonEvent := true. |
|
315 |
deviceMessage := #'deviceButtonMultiPress:x:y:'. |
|
316 |
delegateMessage := #'buttonMultiPress:x:y:view:'. |
|
317 |
delegateQuery := #'handlesButtonMultiPress:inView:'. |
|
318 |
] ifFalse:[ (type == #'pointerEnter:x:y:') ifTrue:[ |
|
319 |
isPointerEvent := true. |
|
320 |
deviceMessage := #'devicePointerEnter:x:y:'. |
|
321 |
delegateMessage := #'pointerEnter:x:y:view:'. |
|
322 |
delegateQuery := #'handlesPointerEnter:inView:'. |
|
323 |
] ifFalse:[ (type == #'pointerLeave:') ifTrue:[ |
|
324 |
isPointerEvent := true. |
|
152 | 325 |
deviceMessage := type. |
115 | 326 |
delegateMessage := #'pointerLeave:view:'. |
327 |
delegateQuery := #'handlesPointerLeave:inView:'. |
|
328 |
] ifFalse:[ (type == #'exposeX:y:width:height:') ifTrue:[ |
|
329 |
deviceMessage := #'deviceExposeX:y:width:height:'. |
|
330 |
] ifFalse:[ (type == #'graphicExposeX:y:width:height:') ifTrue:[ |
|
331 |
deviceMessage := #'deviceGraphicExposeX:y:width:height:'. |
|
332 |
]]]]]]]]]]]. |
|
89 | 333 |
|
141 | 334 |
" |
335 |
if there is a focusView, and its a keyboard event, pass it |
|
336 |
to that view (or its controller, or its delegate). |
|
337 |
In this case, a coordinate which is outside of |
|
143 | 338 |
the focusView (0 @ 0) is passed as x/y coordinates. |
141 | 339 |
" |
340 |
(focusView notNil |
|
341 |
and:[isKeyEvent]) ifTrue:[ |
|
342 |
self sendEvent:type |
|
143 | 343 |
arguments:(Array with:(argArray at:1) with:0 with:0) |
141 | 344 |
view:focusView |
345 |
withFocusOn:nil |
|
346 |
delegate:doDelegate. |
|
347 |
^ self |
|
348 |
]. |
|
349 |
||
135 | 350 |
doDelegate ifTrue:[ |
351 |
" |
|
352 |
handle delegated messages |
|
78 | 353 |
" |
135 | 354 |
(isKeyEvent |
355 |
or:[isButtonEvent |
|
356 |
or:[isPointerEvent]]) ifTrue:[ |
|
357 |
delegate := view delegate. |
|
358 |
||
89 | 359 |
" |
135 | 360 |
what a kludge - sending to delegate requires |
361 |
another selector and an additional argument ... |
|
115 | 362 |
" |
140 | 363 |
(delegate notNil |
364 |
and:[delegate respondsTo:delegateMessage]) ifTrue:[ |
|
135 | 365 |
" |
366 |
is the delegate interrested in that event ? |
|
367 |
(if it does not respond to the handlesXXX message, |
|
368 |
we assume: NO) |
|
115 | 369 |
" |
135 | 370 |
((delegate respondsTo:delegateQuery) |
371 |
and:[delegate perform:delegateQuery with:(argArray at:1) with:view]) ifTrue:[ |
|
372 |
" |
|
373 |
mhmh ... have to convert to logical coordinates |
|
374 |
" |
|
375 |
(trans := view transformation) notNil ifTrue:[ |
|
376 |
argArray size > 2 ifTrue:[ |
|
377 |
argArray at:2 put:(trans applyInverseToX:(argArray at:2)). |
|
378 |
argArray at:3 put:(trans applyInverseToY:(argArray at:3)). |
|
379 |
]. |
|
89 | 380 |
]. |
135 | 381 |
argArray isNil ifTrue:[ |
382 |
delegate perform:delegateMessage with:view |
|
383 |
] ifFalse:[ |
|
384 |
delegate perform:delegateMessage withArguments:(argArray copyWith:view) |
|
385 |
]. |
|
386 |
^ self |
|
387 |
] |
|
388 |
]. |
|
89 | 389 |
]. |
390 |
]. |
|
391 |
||
392 |
" |
|
393 |
if there is a controller, that one gets all user events |
|
394 |
" |
|
395 |
eventReceiver := view. |
|
396 |
(controller := view controller) notNil ifTrue:[ |
|
115 | 397 |
(isKeyEvent |
398 |
or:[isButtonEvent |
|
399 |
or:[isPointerEvent |
|
400 |
or:[(type == #focusIn) |
|
401 |
or:[(type == #focusOut)]]]]) ifTrue:[ |
|
89 | 402 |
eventReceiver := controller. |
72 | 403 |
] |
89 | 404 |
]. |
405 |
||
406 |
" |
|
115 | 407 |
finally, another one: |
89 | 408 |
if the view has a transformation, edit the selector |
409 |
from #foo to #deviceFoo... |
|
410 |
This allows the view to handle the event either in device or |
|
411 |
logical coordinates. (since the deviceFoo-messages default implementation |
|
412 |
in PseudoView translates and resends). |
|
413 |
Actually, I could always send deviceXXX without speed penalty |
|
414 |
(event sending is no high frequency operation), but that just adds |
|
415 |
another context to any debuggers walkback, making things less clear. |
|
416 |
" |
|
115 | 417 |
selector := type. |
418 |
||
89 | 419 |
view transformation notNil ifTrue:[ |
115 | 420 |
(isKeyEvent |
421 |
or:[isButtonEvent |
|
144 | 422 |
or:[isPointerEvent |
115 | 423 |
or:[(type == #'exposeX:y:width:height:') |
424 |
or:[(type == #'graphicExposeX:y:width:height:')]]]]) ifTrue:[ |
|
425 |
selector := deviceMessage |
|
89 | 426 |
] |
427 |
]. |
|
115 | 428 |
eventReceiver perform:selector withArguments:argArray |
429 |
! ! |
|
430 |
||
431 |
!WindowEvent methodsFor:'sending'! |
|
432 |
||
433 |
sendEvent |
|
144 | 434 |
"forward the event represented by the receiver to the views delegate, |
435 |
the views controller or the view. Ignore any focusView." |
|
115 | 436 |
|
437 |
self sendEventWithFocusOn:nil |
|
438 |
! |
|
439 |
||
440 |
sendEventWithFocusOn:focusView |
|
144 | 441 |
"forward the event represented by the receiver to the views delegate, |
442 |
the views controller or the view. |
|
443 |
If focusView is nonNil, and the receiver is a keyboard event, |
|
444 |
the event will be forwarded to the focusView instead |
|
445 |
(or its delegate, or its controller)." |
|
115 | 446 |
|
135 | 447 |
self class |
448 |
sendEvent:type |
|
449 |
arguments:arguments |
|
450 |
view:view |
|
451 |
withFocusOn:focusView |
|
452 |
delegate:true |
|
11 | 453 |
! ! |
454 |
||
455 |
!WindowEvent methodsFor:'private accessing'! |
|
456 |
||
457 |
for:aView type:aSymbol arguments:argArray |
|
144 | 458 |
"set the instance variables of the event" |
11 | 459 |
|
460 |
view := aView. |
|
461 |
type := aSymbol. |
|
462 |
arguments := argArray |
|
463 |
! ! |