2 * Copyright (C) 2005 Apple Computer, Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #import "TextInputController.h"
31 #import <AppKit/NSInputManager.h>
32 #import <WebKit/WebDocument.h>
33 #import <WebKit/WebFrame.h>
34 #import <WebKit/WebFrameView.h>
35 #import <WebKit/WebView.h>
37 @implementation TextInputController
39 // FIXME: need to support attributed strings
41 + (BOOL)isSelectorExcludedFromWebScript:(SEL)aSelector
43 if (aSelector == @selector(insertText:)
44 || aSelector == @selector(doCommand:)
45 || aSelector == @selector(setMarkedText:selectedFrom:length:)
46 || aSelector == @selector(unmarkText)
47 || aSelector == @selector(hasMarkedText)
48 || aSelector == @selector(conversationIdentifier)
49 || aSelector == @selector(substringFrom:length:)
50 || aSelector == @selector(markedRange)
51 || aSelector == @selector(selectedRange)
52 || aSelector == @selector(firstRectForCharactersFrom:length:)
53 || aSelector == @selector(characterIndexForPointX:Y:)
54 || aSelector == @selector(validAttributesForMarkedText))
59 + (NSString *)webScriptNameForSelector:(SEL)aSelector
61 if (aSelector == @selector(insertText:))
63 else if (aSelector == @selector(doCommand:))
65 else if (aSelector == @selector(setMarkedText:selectedFrom:length:))
66 return @"setMarkedText";
67 else if (aSelector == @selector(substringFrom:length:))
68 return @"substringFromRange";
69 else if (aSelector == @selector(firstRectForCharactersFrom:length:))
70 return @"firstRectForCharacterRange";
71 else if (aSelector == @selector(characterIndexForPointX:Y:))
72 return @"characterIndexForPoint";
77 - (id)initWithWebView:(WebView *)wv
84 - (NSObject <NSTextInput> *)textInput
86 NSView <NSTextInput> *view = [[[webView mainFrame] frameView] documentView];
87 return [view conformsToProtocol:@protocol(NSTextInput)] ? view : nil;
90 - (void)insertText:(NSString *)aString
92 NSObject <NSTextInput> *textInput = [self textInput];
95 [textInput insertText:aString];
98 - (void)doCommand:(NSString *)aCommand
100 NSObject <NSTextInput> *textInput = [self textInput];
103 [textInput doCommandBySelector:NSSelectorFromString(aCommand)];
106 - (void)setMarkedText:(NSString *)aString selectedFrom:(int)from length:(int)length
108 NSObject <NSTextInput> *textInput = [self textInput];
111 [textInput setMarkedText:aString selectedRange:NSMakeRange(from, length)];
116 NSObject <NSTextInput> *textInput = [self textInput];
119 [textInput unmarkText];
122 - (BOOL)hasMarkedText
124 NSObject <NSTextInput> *textInput = [self textInput];
127 return [textInput hasMarkedText];
132 - (long)conversationIdentifier
134 NSObject <NSTextInput> *textInput = [self textInput];
137 return [textInput conversationIdentifier];
142 - (NSString *)substringFrom:(int)from length:(int)length
144 NSObject <NSTextInput> *textInput = [self textInput];
147 return [[textInput attributedSubstringFromRange:NSMakeRange(from, length)] string];
152 - (NSArray *)markedRange
154 NSObject <NSTextInput> *textInput = [self textInput];
157 NSRange range = [textInput markedRange];
158 return [NSArray arrayWithObjects:[NSNumber numberWithUnsignedInt:range.location], [NSNumber numberWithUnsignedInt:range.length], nil];
164 - (NSArray *)selectedRange
166 NSObject <NSTextInput> *textInput = [self textInput];
169 NSRange range = [textInput selectedRange];
170 return [NSArray arrayWithObjects:[NSNumber numberWithUnsignedInt:range.location], [NSNumber numberWithUnsignedInt:range.length], nil];
177 - (NSArray *)firstRectForCharactersFrom:(int)from length:(int)length
179 NSObject <NSTextInput> *textInput = [self textInput];
182 NSRect rect = [textInput firstRectForCharacterRange:NSMakeRange(from, length)];
183 return [NSArray arrayWithObjects:
184 [NSNumber numberWithFloat:rect.origin.x],
185 [NSNumber numberWithFloat:rect.origin.y],
186 [NSNumber numberWithFloat:rect.size.width],
187 [NSNumber numberWithFloat:rect.size.height],
194 - (int)characterIndexForPointX:(float)x Y:(float)y
196 NSObject <NSTextInput> *textInput = [self textInput];
199 return [textInput characterIndexForPoint:NSMakePoint(x, y)];
204 - (NSArray *)validAttributesForMarkedText
206 NSObject <NSTextInput> *textInput = [self textInput];
209 return [textInput validAttributesForMarkedText];