KeyboardForwarder.st
author claus
Fri, 12 May 1995 20:01:16 +0200
changeset 142 8473a0af99ac
parent 140 0db355079dc4
child 144 cf645a1ebbb3
permissions -rw-r--r--
.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
128
9c2378152500 Initial revision
claus
parents:
diff changeset
     1
"
9c2378152500 Initial revision
claus
parents:
diff changeset
     2
 COPYRIGHT (c) 1995 by Claus Gittinger
9c2378152500 Initial revision
claus
parents:
diff changeset
     3
	      All Rights Reserved
9c2378152500 Initial revision
claus
parents:
diff changeset
     4
9c2378152500 Initial revision
claus
parents:
diff changeset
     5
 This software is furnished under a license and may be used
9c2378152500 Initial revision
claus
parents:
diff changeset
     6
 only in accordance with the terms of that license and with the
9c2378152500 Initial revision
claus
parents:
diff changeset
     7
 inclusion of the above copyright notice.   This software may not
9c2378152500 Initial revision
claus
parents:
diff changeset
     8
 be provided or otherwise made available to, or used by, any
9c2378152500 Initial revision
claus
parents:
diff changeset
     9
 other person.  No title to or ownership of the software is
9c2378152500 Initial revision
claus
parents:
diff changeset
    10
 hereby transferred.
9c2378152500 Initial revision
claus
parents:
diff changeset
    11
"
9c2378152500 Initial revision
claus
parents:
diff changeset
    12
9c2378152500 Initial revision
claus
parents:
diff changeset
    13
9c2378152500 Initial revision
claus
parents:
diff changeset
    14
'From Smalltalk/X, Version:2.10.5 on 30-mar-1995 at 5:32:55 am'!
9c2378152500 Initial revision
claus
parents:
diff changeset
    15
9c2378152500 Initial revision
claus
parents:
diff changeset
    16
Object subclass:#KeyboardForwarder
139
claus
parents: 134
diff changeset
    17
	 instanceVariableNames:'sourceView destinationView destination condition'
128
9c2378152500 Initial revision
claus
parents:
diff changeset
    18
	 classVariableNames:''
9c2378152500 Initial revision
claus
parents:
diff changeset
    19
	 poolDictionaries:''
9c2378152500 Initial revision
claus
parents:
diff changeset
    20
	 category:'Interface-Support'
9c2378152500 Initial revision
claus
parents:
diff changeset
    21
!
9c2378152500 Initial revision
claus
parents:
diff changeset
    22
9c2378152500 Initial revision
claus
parents:
diff changeset
    23
!KeyboardForwarder class methodsFor:'documentation'!
9c2378152500 Initial revision
claus
parents:
diff changeset
    24
9c2378152500 Initial revision
claus
parents:
diff changeset
    25
version
9c2378152500 Initial revision
claus
parents:
diff changeset
    26
"
142
claus
parents: 140
diff changeset
    27
$Header: /cvs/stx/stx/libview/KeyboardForwarder.st,v 1.5 1995-05-12 18:00:26 claus Exp $
128
9c2378152500 Initial revision
claus
parents:
diff changeset
    28
"
9c2378152500 Initial revision
claus
parents:
diff changeset
    29
!
9c2378152500 Initial revision
claus
parents:
diff changeset
    30
9c2378152500 Initial revision
claus
parents:
diff changeset
    31
documentation
9c2378152500 Initial revision
claus
parents:
diff changeset
    32
"
9c2378152500 Initial revision
claus
parents:
diff changeset
    33
    Instances of this class can be used as delegates to forward keyboard
139
claus
parents: 134
diff changeset
    34
    events to some object or other view (via aView delegate:).
128
9c2378152500 Initial revision
claus
parents:
diff changeset
    35
    Notice, that delegates dont have to be instances of
9c2378152500 Initial revision
claus
parents:
diff changeset
    36
    myself; any object with a protocol similar to mine can be used as
9c2378152500 Initial revision
claus
parents:
diff changeset
    37
    a delegate. 
142
claus
parents: 140
diff changeset
    38
    (i.e. any object that responds to 
claus
parents: 140
diff changeset
    39
     handlesKeyPress:view: / keyPress:x:y:view: / delegatesTo: etc.)
139
claus
parents: 134
diff changeset
    40
claus
parents: 134
diff changeset
    41
    However, I provide the most common functions required for conditional
claus
parents: 134
diff changeset
    42
    event delegation and instances of myself are for example used to forward 
claus
parents: 134
diff changeset
    43
    events from elements of some dialogBoxes to the enterfield/fieldGroup - 
claus
parents: 134
diff changeset
    44
    this allows keyboard input to be forwarded to the active text-field even 
claus
parents: 134
diff changeset
    45
    while the mouse pointer is somewhere else.
claus
parents: 134
diff changeset
    46
claus
parents: 134
diff changeset
    47
    Another application of delegation is to catch keyboard input to some standard
claus
parents: 134
diff changeset
    48
    widget, optionally processing and resending the key-event.
128
9c2378152500 Initial revision
claus
parents:
diff changeset
    49
9c2378152500 Initial revision
claus
parents:
diff changeset
    50
    Instance Variables:
139
claus
parents: 134
diff changeset
    51
128
9c2378152500 Initial revision
claus
parents:
diff changeset
    52
	destinationView         <View>          the view which shall receive
9c2378152500 Initial revision
claus
parents:
diff changeset
    53
						the forwarded keyboard events.
134
claus
parents: 128
diff changeset
    54
139
claus
parents: 134
diff changeset
    55
	destination             <Object>        the object which shall receive
claus
parents: 134
diff changeset
    56
						the forwarded keyboard events.
claus
parents: 134
diff changeset
    57
134
claus
parents: 128
diff changeset
    58
	sourceView              <ViewOrNil>     if non-nil, only events from this
claus
parents: 128
diff changeset
    59
						view are forwarded.
claus
parents: 128
diff changeset
    60
						(currently nowhere used)
claus
parents: 128
diff changeset
    61
claus
parents: 128
diff changeset
    62
	condition               <Symbol>        an additional condition for forwarding
claus
parents: 128
diff changeset
    63
						currently only #noFocus is implemented.
claus
parents: 128
diff changeset
    64
						The #noFocus condition will only forward
claus
parents: 128
diff changeset
    65
						events if no explicit focus has been
claus
parents: 128
diff changeset
    66
						set in the windowGroup.
128
9c2378152500 Initial revision
claus
parents:
diff changeset
    67
"
9c2378152500 Initial revision
claus
parents:
diff changeset
    68
!
9c2378152500 Initial revision
claus
parents:
diff changeset
    69
9c2378152500 Initial revision
claus
parents:
diff changeset
    70
copyright
9c2378152500 Initial revision
claus
parents:
diff changeset
    71
"
9c2378152500 Initial revision
claus
parents:
diff changeset
    72
 COPYRIGHT (c) 1995 by Claus Gittinger
9c2378152500 Initial revision
claus
parents:
diff changeset
    73
	      All Rights Reserved
9c2378152500 Initial revision
claus
parents:
diff changeset
    74
9c2378152500 Initial revision
claus
parents:
diff changeset
    75
 This software is furnished under a license and may be used
9c2378152500 Initial revision
claus
parents:
diff changeset
    76
 only in accordance with the terms of that license and with the
9c2378152500 Initial revision
claus
parents:
diff changeset
    77
 inclusion of the above copyright notice.   This software may not
9c2378152500 Initial revision
claus
parents:
diff changeset
    78
 be provided or otherwise made available to, or used by, any
9c2378152500 Initial revision
claus
parents:
diff changeset
    79
 other person.  No title to or ownership of the software is
9c2378152500 Initial revision
claus
parents:
diff changeset
    80
 hereby transferred.
9c2378152500 Initial revision
claus
parents:
diff changeset
    81
"
9c2378152500 Initial revision
claus
parents:
diff changeset
    82
9c2378152500 Initial revision
claus
parents:
diff changeset
    83
! !
9c2378152500 Initial revision
claus
parents:
diff changeset
    84
9c2378152500 Initial revision
claus
parents:
diff changeset
    85
!KeyboardForwarder class methodsFor:'instance creation'!
9c2378152500 Initial revision
claus
parents:
diff changeset
    86
139
claus
parents: 134
diff changeset
    87
from:sourceView toView:destinationView
claus
parents: 134
diff changeset
    88
    "create and return a new KeyboardForwarder to redirect key events
claus
parents: 134
diff changeset
    89
     for sourceView to destinationView. Events from other than the sourceView
claus
parents: 134
diff changeset
    90
     will not be forwarded. The forwarded event will be reported excluding
claus
parents: 134
diff changeset
    91
     the original view as argument (i.e. as #keyPress:x:y:). 
claus
parents: 134
diff changeset
    92
     Use this, if the destination is a view."
claus
parents: 134
diff changeset
    93
128
9c2378152500 Initial revision
claus
parents:
diff changeset
    94
    ^ self new sourceView:sourceView; destinationView:destinationView
9c2378152500 Initial revision
claus
parents:
diff changeset
    95
!
9c2378152500 Initial revision
claus
parents:
diff changeset
    96
139
claus
parents: 134
diff changeset
    97
from:sourceView to:destination
claus
parents: 134
diff changeset
    98
    "create and return a new KeyboardForwarder to redirect key events
claus
parents: 134
diff changeset
    99
     for sourceView to destination. Events from other than the sourceView
claus
parents: 134
diff changeset
   100
     will not be forwarded. The forwarded event will be reported including
claus
parents: 134
diff changeset
   101
     the original view as argument (i.e. as #keyPress:x:y:view:). 
claus
parents: 134
diff changeset
   102
     Use this, if the destination is not a view."
claus
parents: 134
diff changeset
   103
claus
parents: 134
diff changeset
   104
    ^ self new sourceView:sourceView; destination:destination
claus
parents: 134
diff changeset
   105
!
claus
parents: 134
diff changeset
   106
claus
parents: 134
diff changeset
   107
toView:destinationView
claus
parents: 134
diff changeset
   108
    "create and return a new KeyboardForwarder to redirect any key event
claus
parents: 134
diff changeset
   109
     to destinationView (Independent of the view in which the event originally
claus
parents: 134
diff changeset
   110
     occurred). The forwarded event will be reported excluding
claus
parents: 134
diff changeset
   111
     the original view as argument (i.e. as #keyPress:x:y:). 
claus
parents: 134
diff changeset
   112
     Use this, if the destination is a view."
claus
parents: 134
diff changeset
   113
128
9c2378152500 Initial revision
claus
parents:
diff changeset
   114
    ^ self new destinationView:destinationView
134
claus
parents: 128
diff changeset
   115
!
claus
parents: 128
diff changeset
   116
139
claus
parents: 134
diff changeset
   117
to:destination
claus
parents: 134
diff changeset
   118
    "create and return a new KeyboardForwarder to redirect any key event
claus
parents: 134
diff changeset
   119
     to destination (Independent of the view in which the event originally
claus
parents: 134
diff changeset
   120
     occurred). The forwarded event will be reported including
claus
parents: 134
diff changeset
   121
     the original view as argument (i.e. as #keyPress:x:y:view:). 
claus
parents: 134
diff changeset
   122
     Use this, if the destination is not a view."
claus
parents: 134
diff changeset
   123
claus
parents: 134
diff changeset
   124
    ^ self new destination:destination
claus
parents: 134
diff changeset
   125
!
claus
parents: 134
diff changeset
   126
claus
parents: 134
diff changeset
   127
toView:destinationView condition:aConditionSymbol
claus
parents: 134
diff changeset
   128
    "create and return a new KeyboardForwarder to redirect any key event
claus
parents: 134
diff changeset
   129
     to destinationView (Independent of the view in which the event originally
claus
parents: 134
diff changeset
   130
     occurred) but only, if some condition as specified by aConditionSymbol
claus
parents: 134
diff changeset
   131
     is met. The forwarded event will be reported excluding
claus
parents: 134
diff changeset
   132
     the original view as argument (i.e. as #keyPress:x:y:). 
claus
parents: 134
diff changeset
   133
     Use this, if the destination is a view."
claus
parents: 134
diff changeset
   134
claus
parents: 134
diff changeset
   135
    ^ self new destinationView:destinationView; condition:aConditionSymbol
claus
parents: 134
diff changeset
   136
!
claus
parents: 134
diff changeset
   137
claus
parents: 134
diff changeset
   138
to:destination condition:aConditionSymbol
claus
parents: 134
diff changeset
   139
    "create and return a new KeyboardForwarder to redirect any key event
claus
parents: 134
diff changeset
   140
     to destinationView (Independent of the view in which the event originally
claus
parents: 134
diff changeset
   141
     occurred) but only, if some condition as specified by aConditionSymbol
claus
parents: 134
diff changeset
   142
     is met. The forwarded event will be reported including
claus
parents: 134
diff changeset
   143
     the original view as argument (i.e. as #keyPress:x:y:view:). 
claus
parents: 134
diff changeset
   144
     Use this, if the destination is not a view."
claus
parents: 134
diff changeset
   145
claus
parents: 134
diff changeset
   146
    ^ self new destination:destination; condition:aConditionSymbol
128
9c2378152500 Initial revision
claus
parents:
diff changeset
   147
! !
9c2378152500 Initial revision
claus
parents:
diff changeset
   148
139
claus
parents: 134
diff changeset
   149
!KeyboardForwarder methodsFor:'private accessing'!
128
9c2378152500 Initial revision
claus
parents:
diff changeset
   150
134
claus
parents: 128
diff changeset
   151
condition:aCondition
claus
parents: 128
diff changeset
   152
    condition := aCondition
claus
parents: 128
diff changeset
   153
!
claus
parents: 128
diff changeset
   154
128
9c2378152500 Initial revision
claus
parents:
diff changeset
   155
destinationView:aView
9c2378152500 Initial revision
claus
parents:
diff changeset
   156
    destinationView := aView
9c2378152500 Initial revision
claus
parents:
diff changeset
   157
!
9c2378152500 Initial revision
claus
parents:
diff changeset
   158
139
claus
parents: 134
diff changeset
   159
destinationView
claus
parents: 134
diff changeset
   160
    ^ destinationView
claus
parents: 134
diff changeset
   161
!
claus
parents: 134
diff changeset
   162
claus
parents: 134
diff changeset
   163
destination:anObject 
claus
parents: 134
diff changeset
   164
    destination := anObject
claus
parents: 134
diff changeset
   165
!
claus
parents: 134
diff changeset
   166
claus
parents: 134
diff changeset
   167
destination
claus
parents: 134
diff changeset
   168
    ^ destination
claus
parents: 134
diff changeset
   169
!
claus
parents: 134
diff changeset
   170
128
9c2378152500 Initial revision
claus
parents:
diff changeset
   171
sourceView:aView
9c2378152500 Initial revision
claus
parents:
diff changeset
   172
    sourceView := aView
9c2378152500 Initial revision
claus
parents:
diff changeset
   173
!
9c2378152500 Initial revision
claus
parents:
diff changeset
   174
9c2378152500 Initial revision
claus
parents:
diff changeset
   175
sourceView
9c2378152500 Initial revision
claus
parents:
diff changeset
   176
    ^ sourceView
9c2378152500 Initial revision
claus
parents:
diff changeset
   177
! !
9c2378152500 Initial revision
claus
parents:
diff changeset
   178
9c2378152500 Initial revision
claus
parents:
diff changeset
   179
!KeyboardForwarder methodsFor:'queries'!
9c2378152500 Initial revision
claus
parents:
diff changeset
   180
142
claus
parents: 140
diff changeset
   181
delegatesTo:someone
claus
parents: 140
diff changeset
   182
    "return true, if I delegate events to someone"
claus
parents: 140
diff changeset
   183
claus
parents: 140
diff changeset
   184
    ^ destination == someone or:[destinationView == someone]
claus
parents: 140
diff changeset
   185
!
claus
parents: 140
diff changeset
   186
128
9c2378152500 Initial revision
claus
parents:
diff changeset
   187
handlesKeyPress:key inView:aView
139
claus
parents: 134
diff changeset
   188
    "this is the query from the sensor to ask me if I would like to
claus
parents: 134
diff changeset
   189
     get a keyPress event for key from aView. Return true, if I want so,
claus
parents: 134
diff changeset
   190
     false otherwise."
claus
parents: 134
diff changeset
   191
134
claus
parents: 128
diff changeset
   192
    condition notNil ifTrue:[
claus
parents: 128
diff changeset
   193
	condition == #noFocus ifTrue:[
claus
parents: 128
diff changeset
   194
	    aView windowGroup focusView notNil ifTrue:[^ false]
claus
parents: 128
diff changeset
   195
	]
claus
parents: 128
diff changeset
   196
    ].
128
9c2378152500 Initial revision
claus
parents:
diff changeset
   197
    sourceView notNil ifTrue:[
9c2378152500 Initial revision
claus
parents:
diff changeset
   198
	^ aView == sourceView
9c2378152500 Initial revision
claus
parents:
diff changeset
   199
    ].
9c2378152500 Initial revision
claus
parents:
diff changeset
   200
    ^ true
9c2378152500 Initial revision
claus
parents:
diff changeset
   201
!
9c2378152500 Initial revision
claus
parents:
diff changeset
   202
9c2378152500 Initial revision
claus
parents:
diff changeset
   203
handlesKeyRelease:key inView:aView
139
claus
parents: 134
diff changeset
   204
    "this is the query from the sensor to ask me if I would like to
claus
parents: 134
diff changeset
   205
     get a keyRelease event for key from aView. Return true, if I want so,
claus
parents: 134
diff changeset
   206
     false otherwise."
claus
parents: 134
diff changeset
   207
134
claus
parents: 128
diff changeset
   208
    condition notNil ifTrue:[
claus
parents: 128
diff changeset
   209
	condition == #noFocus ifTrue:[
claus
parents: 128
diff changeset
   210
	    aView windowGroup focusView notNil ifTrue:[^ false]
claus
parents: 128
diff changeset
   211
	]
claus
parents: 128
diff changeset
   212
    ].
128
9c2378152500 Initial revision
claus
parents:
diff changeset
   213
    sourceView notNil ifTrue:[
9c2378152500 Initial revision
claus
parents:
diff changeset
   214
	^ aView == sourceView
9c2378152500 Initial revision
claus
parents:
diff changeset
   215
    ].
9c2378152500 Initial revision
claus
parents:
diff changeset
   216
    ^ true
9c2378152500 Initial revision
claus
parents:
diff changeset
   217
! !
9c2378152500 Initial revision
claus
parents:
diff changeset
   218
9c2378152500 Initial revision
claus
parents:
diff changeset
   219
!KeyboardForwarder methodsFor:'event forwarding'!
9c2378152500 Initial revision
claus
parents:
diff changeset
   220
9c2378152500 Initial revision
claus
parents:
diff changeset
   221
keyPress:key x:x y:y view:aView
139
claus
parents: 134
diff changeset
   222
    "handle a delegated event - this is sent by the sensor to actually
claus
parents: 134
diff changeset
   223
     forward the event (i.e. after I returned true on handlesKeyPress:.
claus
parents: 134
diff changeset
   224
     Take care of cyclic delegation (via a kludge-test for negative coordinate)."
claus
parents: 134
diff changeset
   225
128
9c2378152500 Initial revision
claus
parents:
diff changeset
   226
    x < 0 ifTrue:[
9c2378152500 Initial revision
claus
parents:
diff changeset
   227
	"
9c2378152500 Initial revision
claus
parents:
diff changeset
   228
	 already delegated ... ignore
9c2378152500 Initial revision
claus
parents:
diff changeset
   229
	"
9c2378152500 Initial revision
claus
parents:
diff changeset
   230
	^ self
9c2378152500 Initial revision
claus
parents:
diff changeset
   231
    ].
9c2378152500 Initial revision
claus
parents:
diff changeset
   232
139
claus
parents: 134
diff changeset
   233
    destination notNil ifTrue:[
claus
parents: 134
diff changeset
   234
	destination keyPress:key x:-1 y:-1 view:aView.
claus
parents: 134
diff changeset
   235
    ] ifFalse:[
140
claus
parents: 139
diff changeset
   236
	WindowEvent
claus
parents: 139
diff changeset
   237
	    sendEvent:#keyPress:x:y:
claus
parents: 139
diff changeset
   238
	    arguments:(Array with:key with:-1 with:-1)
claus
parents: 139
diff changeset
   239
	    view:destinationView
claus
parents: 139
diff changeset
   240
"/        destinationView keyPress:key x:-1 y:-1 
139
claus
parents: 134
diff changeset
   241
    ]
128
9c2378152500 Initial revision
claus
parents:
diff changeset
   242
!
9c2378152500 Initial revision
claus
parents:
diff changeset
   243
9c2378152500 Initial revision
claus
parents:
diff changeset
   244
keyRelease:key x:x y:y view:aView
139
claus
parents: 134
diff changeset
   245
    "handle a delegated event - this is sent by the sensor to actually
claus
parents: 134
diff changeset
   246
     forward the event (i.e. after I returned true on handlesKeyRelease:.
claus
parents: 134
diff changeset
   247
     Take care of cyclic delegation (via a kludge-test for negative coordinate)."
claus
parents: 134
diff changeset
   248
128
9c2378152500 Initial revision
claus
parents:
diff changeset
   249
    x < 0 ifTrue:[
9c2378152500 Initial revision
claus
parents:
diff changeset
   250
	"
9c2378152500 Initial revision
claus
parents:
diff changeset
   251
	 already delegated ... ignore
9c2378152500 Initial revision
claus
parents:
diff changeset
   252
	"
9c2378152500 Initial revision
claus
parents:
diff changeset
   253
	^ self
9c2378152500 Initial revision
claus
parents:
diff changeset
   254
    ].
9c2378152500 Initial revision
claus
parents:
diff changeset
   255
139
claus
parents: 134
diff changeset
   256
    destination notNil ifTrue:[
140
claus
parents: 139
diff changeset
   257
	WindowEvent
claus
parents: 139
diff changeset
   258
	    sendEvent:#keyRelease:x:y:
claus
parents: 139
diff changeset
   259
	    arguments:(Array with:key with:-1 with:-1)
claus
parents: 139
diff changeset
   260
	    view:destinationView
claus
parents: 139
diff changeset
   261
"/        destinationView keyRelease:key x:-1 y:-1 view:aView
139
claus
parents: 134
diff changeset
   262
    ] ifFalse:[
claus
parents: 134
diff changeset
   263
	destinationView keyRelease:key x:-1 y:-1 
claus
parents: 134
diff changeset
   264
    ]
128
9c2378152500 Initial revision
claus
parents:
diff changeset
   265
! !