5 // Created by Uli Kusterer on 04.10.04.
6 // Copyright 2004 M. Uli Kusterer. All rights reserved.
9 #import "UKFirstResponder.h"
11 @interface UKFirstResponder (PrivateMethods)
13 -(id) responderForAction: (SEL)itemAction;
17 static UKFirstResponder* sFirstResponder = nil;
20 @implementation UKFirstResponder
24 if( !sFirstResponder )
25 sFirstResponder = [[[self class] alloc] init];
27 return sFirstResponder;
31 // This is a singleton:
34 if( !sFirstResponder )
37 sFirstResponder = self;
44 return sFirstResponder;
48 // -----------------------------------------------------------------------------
49 // Finding out what messages the first responder understands/sending messages:
50 // -----------------------------------------------------------------------------
52 -(id) performSelector: (SEL)itemAction withObject: (id)obj
54 BOOL does = [super respondsToSelector: itemAction];
56 return [super performSelector: itemAction withObject: obj];
58 id resp = [self responderForAction: itemAction];
60 [self doesNotRecognizeSelector: itemAction];
61 return [resp performSelector: itemAction withObject: obj];
64 -(id) performSelector: (SEL)itemAction
66 BOOL does = [super respondsToSelector: itemAction];
68 return [super performSelector: itemAction];
70 id resp = [self responderForAction: itemAction];
72 [self doesNotRecognizeSelector: itemAction];
73 return [resp performSelector: itemAction];
76 -(BOOL) respondsToSelector: (SEL)itemAction
78 BOOL does = [super respondsToSelector: itemAction];
80 return( does || ([self responderForAction: itemAction] != nil) );
84 // -----------------------------------------------------------------------------
85 // Forwarding unknown methods to the first responder:
86 // -----------------------------------------------------------------------------
88 -(NSMethodSignature*) methodSignatureForSelector: (SEL)itemAction
90 NSMethodSignature* sig = [super methodSignatureForSelector: itemAction];
91 id friend = [self responderForAction: itemAction];
97 return [friend methodSignatureForSelector: itemAction];
102 -(void) forwardInvocation: (NSInvocation*)invocation
104 SEL itemAction = [invocation selector];
105 id friend = [self responderForAction: itemAction];
108 [invocation invokeWithTarget: friend];
110 [self doesNotRecognizeSelector: itemAction];
116 @implementation UKFirstResponder (PrivateMethods)
118 -(id) responderForAction: (SEL)itemAction
120 NSResponder* resp = [[NSApp keyWindow] firstResponder];
121 id actualResponder = nil;
125 if( [resp respondsToSelector: itemAction] )
126 actualResponder = resp;
127 else if( [resp respondsToSelector: @selector(delegate)] )
129 id del = [(id)resp delegate];
130 if( [del respondsToSelector: itemAction] )
131 actualResponder = del;
134 while( !actualResponder && (resp = [resp nextResponder]) );
136 if( !actualResponder )
138 if( [NSApp respondsToSelector: itemAction] )
139 actualResponder = NSApp;
142 id del = [NSApp delegate];
143 if( [del respondsToSelector: itemAction] )
144 actualResponder = del;
148 return actualResponder;