Don't link with Carbon.framework.
[MacTF.git] / UKFirstResponder.m
blob15f107766cd2014b662070ac7b50dc76e7df6101
1 //
2 //  UKFirstResponder.m
3 //  Shovel
4 //
5 //  Created by Uli Kusterer on 04.10.04.
6 //  Copyright 2004 M. Uli Kusterer. All rights reserved.
7 //
9 #import "UKFirstResponder.h"
11 @interface UKFirstResponder (PrivateMethods)
13 -(id)   responderForAction: (SEL)itemAction;
15 @end
17 static UKFirstResponder*        sFirstResponder = nil;
20 @implementation UKFirstResponder
22 +(id)   firstResponder
24         if( !sFirstResponder )
25                 sFirstResponder = [[[self class] alloc] init];
26         
27         return sFirstResponder;
31 // This is a singleton:
32 +(id)   init
34         if( !sFirstResponder )
35         {
36                 self = [super init];
37                 sFirstResponder = self;
38                 
39                 return self;
40         }
41         else
42         {
43                 [self autorelease];
44                 return sFirstResponder;
45         }
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];
55         if( does )
56                 return [super performSelector: itemAction withObject: obj];
57         
58         id      resp = [self responderForAction: itemAction];
59         if( !resp )
60                 [self doesNotRecognizeSelector: itemAction];
61         return [resp performSelector: itemAction withObject: obj];
64 -(id)   performSelector: (SEL)itemAction
66         BOOL    does = [super respondsToSelector: itemAction];
67         if( does )
68                 return [super performSelector: itemAction];
69         
70         id      resp = [self responderForAction: itemAction];
71         if( !resp )
72                 [self doesNotRecognizeSelector: itemAction];
73         return [resp performSelector: itemAction];
76 -(BOOL) respondsToSelector: (SEL)itemAction
78         BOOL    does = [super respondsToSelector: itemAction];
79         
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];
93         if( sig )
94                 return sig;
95         
96         if( friend )
97                 return [friend methodSignatureForSelector: itemAction];
98         
99         return nil;
102 -(void) forwardInvocation: (NSInvocation*)invocation
104     SEL itemAction = [invocation selector];
105         id      friend = [self responderForAction: itemAction];
107     if( friend )
108         [invocation invokeWithTarget: friend];
109     else
110         [self doesNotRecognizeSelector: itemAction];
113 @end
116 @implementation UKFirstResponder (PrivateMethods)
118 -(id)   responderForAction: (SEL)itemAction
120         NSResponder*    resp = [[NSApp keyWindow] firstResponder];
121         id                              actualResponder = nil;
123         do
124         {
125                 if( [resp respondsToSelector: itemAction] )
126                         actualResponder = resp;
127                 else if( [resp respondsToSelector: @selector(delegate)] )
128                 {
129                         id del = [(id)resp delegate];
130                         if( [del respondsToSelector: itemAction] )
131                                 actualResponder = del;
132                 }
133         }
134         while( !actualResponder && (resp = [resp nextResponder]) );
135         
136         if( !actualResponder )
137         {
138                 if( [NSApp respondsToSelector: itemAction] )
139                         actualResponder = NSApp;
140                 else
141                 {
142                         id del = [NSApp delegate];
143                         if( [del respondsToSelector: itemAction] )
144                                 actualResponder = del;
145                 }
146         }
147         
148         return actualResponder;
151 @end