|
1 /* |
|
2 * Copyright (C) 2005, 2007 Apple Inc. All rights reserved. |
|
3 * |
|
4 * Redistribution and use in source and binary forms, with or without |
|
5 * modification, are permitted provided that the following conditions |
|
6 * are met: |
|
7 * |
|
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. |
|
16 * |
|
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. |
|
27 */ |
|
28 |
|
29 #import "TextInputController.h" |
|
30 |
|
31 #import <AppKit/NSInputManager.h> |
|
32 #import <WebKit/WebDocument.h> |
|
33 #import <WebKit/WebFrame.h> |
|
34 #import <WebKit/WebFrameView.h> |
|
35 #import <WebKit/WebHTMLViewPrivate.h> |
|
36 #import <WebKit/WebScriptObject.h> |
|
37 #import <WebKit/WebView.h> |
|
38 |
|
39 @interface TextInputController (DumpRenderTreeInputMethodHandler) |
|
40 - (BOOL)interpretKeyEvents:(NSArray *)eventArray withSender:(WebHTMLView *)sender; |
|
41 @end |
|
42 |
|
43 @interface WebHTMLView (DumpRenderTreeInputMethodHandler) |
|
44 - (void)interpretKeyEvents:(NSArray *)eventArray; |
|
45 @end |
|
46 |
|
47 @interface WebHTMLView (WebKitSecretsTextInputControllerIsAwareOf) |
|
48 - (WebFrame *)_frame; |
|
49 @end |
|
50 |
|
51 @implementation WebHTMLView (DumpRenderTreeInputMethodHandler) |
|
52 - (void)interpretKeyEvents:(NSArray *)eventArray |
|
53 { |
|
54 WebScriptObject *obj = [[self _frame] windowObject]; |
|
55 TextInputController *tic = [obj valueForKey:@"textInputController"]; |
|
56 if (![tic interpretKeyEvents:eventArray withSender:self]) |
|
57 [super interpretKeyEvents:eventArray]; |
|
58 } |
|
59 @end |
|
60 |
|
61 @implementation NSMutableAttributedString (TextInputController) |
|
62 |
|
63 + (BOOL)isSelectorExcludedFromWebScript:(SEL)aSelector |
|
64 { |
|
65 if (aSelector == @selector(string) |
|
66 || aSelector == @selector(getLength) |
|
67 || aSelector == @selector(attributeNamesAtIndex:) |
|
68 || aSelector == @selector(valueOfAttribute:atIndex:) |
|
69 || aSelector == @selector(addAttribute:value:) |
|
70 || aSelector == @selector(addAttribute:value:from:length:) |
|
71 || aSelector == @selector(addColorAttribute:red:green:blue:alpha:) |
|
72 || aSelector == @selector(addColorAttribute:red:green:blue:alpha:from:length:) |
|
73 || aSelector == @selector(addFontAttribute:fontName:size:) |
|
74 || aSelector == @selector(addFontAttribute:fontName:size:from:length:)) |
|
75 return NO; |
|
76 return YES; |
|
77 } |
|
78 |
|
79 + (NSString *)webScriptNameForSelector:(SEL)aSelector |
|
80 { |
|
81 if (aSelector == @selector(getLength)) |
|
82 return @"length"; |
|
83 if (aSelector == @selector(attributeNamesAtIndex:)) |
|
84 return @"getAttributeNamesAtIndex"; |
|
85 if (aSelector == @selector(valueOfAttribute:atIndex:)) |
|
86 return @"getAttributeValueAtIndex"; |
|
87 if (aSelector == @selector(addAttribute:value:)) |
|
88 return @"addAttribute"; |
|
89 if (aSelector == @selector(addAttribute:value:from:length:)) |
|
90 return @"addAttributeForRange"; |
|
91 if (aSelector == @selector(addColorAttribute:red:green:blue:alpha:)) |
|
92 return @"addColorAttribute"; |
|
93 if (aSelector == @selector(addColorAttribute:red:green:blue:alpha:from:length:)) |
|
94 return @"addColorAttributeForRange"; |
|
95 if (aSelector == @selector(addFontAttribute:fontName:size:)) |
|
96 return @"addFontAttribute"; |
|
97 if (aSelector == @selector(addFontAttribute:fontName:size:from:length:)) |
|
98 return @"addFontAttributeForRange"; |
|
99 |
|
100 return nil; |
|
101 } |
|
102 |
|
103 - (int)getLength |
|
104 { |
|
105 return (int)[self length]; |
|
106 } |
|
107 |
|
108 - (NSArray *)attributeNamesAtIndex:(int)index |
|
109 { |
|
110 NSDictionary *attributes = [self attributesAtIndex:(unsigned)index effectiveRange:nil]; |
|
111 return [attributes allKeys]; |
|
112 } |
|
113 |
|
114 - (id)valueOfAttribute:(NSString *)attrName atIndex:(int)index |
|
115 { |
|
116 return [self attribute:attrName atIndex:(unsigned)index effectiveRange:nil]; |
|
117 } |
|
118 |
|
119 - (void)addAttribute:(NSString *)attrName value:(id)value |
|
120 { |
|
121 [self addAttribute:attrName value:value range:NSMakeRange(0, [self length])]; |
|
122 } |
|
123 |
|
124 - (void)addAttribute:(NSString *)attrName value:(id)value from:(int)from length:(int)length |
|
125 { |
|
126 [self addAttribute:attrName value:value range:NSMakeRange((unsigned)from, (unsigned)length)]; |
|
127 } |
|
128 |
|
129 - (void)addColorAttribute:(NSString *)attrName red:(float)red green:(float)green blue:(float)blue alpha:(float)alpha |
|
130 { |
|
131 [self addAttribute:attrName value:[NSColor colorWithDeviceRed:red green:green blue:blue alpha:alpha] range:NSMakeRange(0, [self length])]; |
|
132 } |
|
133 |
|
134 - (void)addColorAttribute:(NSString *)attrName red:(float)red green:(float)green blue:(float)blue alpha:(float)alpha from:(int)from length:(int)length |
|
135 { |
|
136 [self addAttribute:attrName value:[NSColor colorWithDeviceRed:red green:green blue:blue alpha:alpha] range:NSMakeRange((unsigned)from, (unsigned)length)]; |
|
137 } |
|
138 |
|
139 - (void)addFontAttribute:(NSString *)attrName fontName:(NSString *)fontName size:(float)fontSize |
|
140 { |
|
141 [self addAttribute:attrName value:[NSFont fontWithName:fontName size:fontSize] range:NSMakeRange(0, [self length])]; |
|
142 } |
|
143 |
|
144 - (void)addFontAttribute:(NSString *)attrName fontName:(NSString *)fontName size:(float)fontSize from:(int)from length:(int)length |
|
145 { |
|
146 [self addAttribute:attrName value:[NSFont fontWithName:fontName size:fontSize] range:NSMakeRange((unsigned)from, (unsigned)length)]; |
|
147 } |
|
148 |
|
149 @end |
|
150 |
|
151 @implementation TextInputController |
|
152 |
|
153 + (BOOL)isSelectorExcludedFromWebScript:(SEL)aSelector |
|
154 { |
|
155 if (aSelector == @selector(insertText:) |
|
156 || aSelector == @selector(doCommand:) |
|
157 || aSelector == @selector(setMarkedText:selectedFrom:length:) |
|
158 || aSelector == @selector(unmarkText) |
|
159 || aSelector == @selector(hasMarkedText) |
|
160 || aSelector == @selector(conversationIdentifier) |
|
161 || aSelector == @selector(substringFrom:length:) |
|
162 || aSelector == @selector(attributedSubstringFrom:length:) |
|
163 || aSelector == @selector(markedRange) |
|
164 || aSelector == @selector(selectedRange) |
|
165 || aSelector == @selector(firstRectForCharactersFrom:length:) |
|
166 || aSelector == @selector(characterIndexForPointX:Y:) |
|
167 || aSelector == @selector(validAttributesForMarkedText) |
|
168 || aSelector == @selector(attributedStringWithString:) |
|
169 || aSelector == @selector(setInputMethodHandler:)) |
|
170 return NO; |
|
171 return YES; |
|
172 } |
|
173 |
|
174 + (NSString *)webScriptNameForSelector:(SEL)aSelector |
|
175 { |
|
176 if (aSelector == @selector(insertText:)) |
|
177 return @"insertText"; |
|
178 else if (aSelector == @selector(doCommand:)) |
|
179 return @"doCommand"; |
|
180 else if (aSelector == @selector(setMarkedText:selectedFrom:length:)) |
|
181 return @"setMarkedText"; |
|
182 else if (aSelector == @selector(substringFrom:length:)) |
|
183 return @"substringFromRange"; |
|
184 else if (aSelector == @selector(attributedSubstringFrom:length:)) |
|
185 return @"attributedSubstringFromRange"; |
|
186 else if (aSelector == @selector(firstRectForCharactersFrom:length:)) |
|
187 return @"firstRectForCharacterRange"; |
|
188 else if (aSelector == @selector(characterIndexForPointX:Y:)) |
|
189 return @"characterIndexForPoint"; |
|
190 else if (aSelector == @selector(attributedStringWithString:)) |
|
191 return @"makeAttributedString"; // just a factory method, doesn't call into NSTextInput |
|
192 else if (aSelector == @selector(setInputMethodHandler:)) |
|
193 return @"setInputMethodHandler"; |
|
194 |
|
195 return nil; |
|
196 } |
|
197 |
|
198 - (id)initWithWebView:(WebView *)wv |
|
199 { |
|
200 self = [super init]; |
|
201 webView = wv; |
|
202 inputMethodView = nil; |
|
203 inputMethodHandler = nil; |
|
204 return self; |
|
205 } |
|
206 |
|
207 - (void)dealloc |
|
208 { |
|
209 [inputMethodHandler release]; |
|
210 inputMethodHandler = nil; |
|
211 |
|
212 [super dealloc]; |
|
213 } |
|
214 |
|
215 - (NSObject <NSTextInput> *)textInput |
|
216 { |
|
217 NSView <NSTextInput> *view = inputMethodView ? inputMethodView : (id)[[[webView mainFrame] frameView] documentView]; |
|
218 return [view conformsToProtocol:@protocol(NSTextInput)] ? view : nil; |
|
219 } |
|
220 |
|
221 - (void)insertText:(id)aString |
|
222 { |
|
223 NSObject <NSTextInput> *textInput = [self textInput]; |
|
224 |
|
225 if (textInput) |
|
226 [textInput insertText:aString]; |
|
227 } |
|
228 |
|
229 - (void)doCommand:(NSString *)aCommand |
|
230 { |
|
231 NSObject <NSTextInput> *textInput = [self textInput]; |
|
232 |
|
233 if (textInput) |
|
234 [textInput doCommandBySelector:NSSelectorFromString(aCommand)]; |
|
235 } |
|
236 |
|
237 - (void)setMarkedText:(NSString *)aString selectedFrom:(int)from length:(int)length |
|
238 { |
|
239 NSObject <NSTextInput> *textInput = [self textInput]; |
|
240 |
|
241 if (textInput) |
|
242 [textInput setMarkedText:aString selectedRange:NSMakeRange(from, length)]; |
|
243 } |
|
244 |
|
245 - (void)unmarkText |
|
246 { |
|
247 NSObject <NSTextInput> *textInput = [self textInput]; |
|
248 |
|
249 if (textInput) |
|
250 [textInput unmarkText]; |
|
251 } |
|
252 |
|
253 - (BOOL)hasMarkedText |
|
254 { |
|
255 NSObject <NSTextInput> *textInput = [self textInput]; |
|
256 |
|
257 if (textInput) |
|
258 return [textInput hasMarkedText]; |
|
259 |
|
260 return FALSE; |
|
261 } |
|
262 |
|
263 - (long)conversationIdentifier |
|
264 { |
|
265 NSObject <NSTextInput> *textInput = [self textInput]; |
|
266 |
|
267 if (textInput) |
|
268 return [textInput conversationIdentifier]; |
|
269 |
|
270 return 0; |
|
271 } |
|
272 |
|
273 - (NSString *)substringFrom:(int)from length:(int)length |
|
274 { |
|
275 NSObject <NSTextInput> *textInput = [self textInput]; |
|
276 |
|
277 if (textInput) |
|
278 return [[textInput attributedSubstringFromRange:NSMakeRange(from, length)] string]; |
|
279 |
|
280 return @""; |
|
281 } |
|
282 |
|
283 - (NSMutableAttributedString *)attributedSubstringFrom:(int)from length:(int)length |
|
284 { |
|
285 NSObject <NSTextInput> *textInput = [self textInput]; |
|
286 |
|
287 NSMutableAttributedString *ret = [[[NSMutableAttributedString alloc] init] autorelease]; |
|
288 |
|
289 if (textInput) |
|
290 [ret setAttributedString:[textInput attributedSubstringFromRange:NSMakeRange(from, length)]]; |
|
291 |
|
292 return ret; |
|
293 } |
|
294 |
|
295 - (NSArray *)markedRange |
|
296 { |
|
297 NSObject <NSTextInput> *textInput = [self textInput]; |
|
298 |
|
299 if (textInput) { |
|
300 NSRange range = [textInput markedRange]; |
|
301 return [NSArray arrayWithObjects:[NSNumber numberWithUnsignedInt:range.location], [NSNumber numberWithUnsignedInt:range.length], nil]; |
|
302 } |
|
303 |
|
304 return nil; |
|
305 } |
|
306 |
|
307 - (NSArray *)selectedRange |
|
308 { |
|
309 NSObject <NSTextInput> *textInput = [self textInput]; |
|
310 |
|
311 if (textInput) { |
|
312 NSRange range = [textInput selectedRange]; |
|
313 return [NSArray arrayWithObjects:[NSNumber numberWithUnsignedInt:range.location], [NSNumber numberWithUnsignedInt:range.length], nil]; |
|
314 } |
|
315 |
|
316 return nil; |
|
317 } |
|
318 |
|
319 |
|
320 - (NSArray *)firstRectForCharactersFrom:(int)from length:(int)length |
|
321 { |
|
322 NSObject <NSTextInput> *textInput = [self textInput]; |
|
323 |
|
324 if (textInput) { |
|
325 NSRect rect = [textInput firstRectForCharacterRange:NSMakeRange(from, length)]; |
|
326 if (rect.origin.x || rect.origin.y || rect.size.width || rect.size.height) { |
|
327 rect.origin = [[webView window] convertScreenToBase:rect.origin]; |
|
328 rect = [webView convertRect:rect fromView:nil]; |
|
329 } |
|
330 return [NSArray arrayWithObjects: |
|
331 [NSNumber numberWithFloat:rect.origin.x], |
|
332 [NSNumber numberWithFloat:rect.origin.y], |
|
333 [NSNumber numberWithFloat:rect.size.width], |
|
334 [NSNumber numberWithFloat:rect.size.height], |
|
335 nil]; |
|
336 } |
|
337 |
|
338 return nil; |
|
339 } |
|
340 |
|
341 - (int)characterIndexForPointX:(float)x Y:(float)y |
|
342 { |
|
343 NSObject <NSTextInput> *textInput = [self textInput]; |
|
344 |
|
345 if (textInput) { |
|
346 NSPoint point = NSMakePoint(x, y); |
|
347 point = [webView convertPoint:point toView:nil]; |
|
348 point = [[webView window] convertBaseToScreen:point]; |
|
349 return [textInput characterIndexForPoint:point]; |
|
350 } |
|
351 |
|
352 return 0; |
|
353 } |
|
354 |
|
355 - (NSArray *)validAttributesForMarkedText |
|
356 { |
|
357 NSObject <NSTextInput> *textInput = [self textInput]; |
|
358 |
|
359 if (textInput) |
|
360 return [textInput validAttributesForMarkedText]; |
|
361 |
|
362 return nil; |
|
363 } |
|
364 |
|
365 - (NSMutableAttributedString *)attributedStringWithString:(NSString *)aString |
|
366 { |
|
367 return [[[NSMutableAttributedString alloc] initWithString:aString] autorelease]; |
|
368 } |
|
369 |
|
370 - (void)setInputMethodHandler:(WebScriptObject *)handler |
|
371 { |
|
372 if (inputMethodHandler == handler) |
|
373 return; |
|
374 [handler retain]; |
|
375 [inputMethodHandler release]; |
|
376 inputMethodHandler = handler; |
|
377 } |
|
378 |
|
379 - (BOOL)interpretKeyEvents:(NSArray *)eventArray withSender:(WebHTMLView *)sender |
|
380 { |
|
381 if (!inputMethodHandler) |
|
382 return NO; |
|
383 |
|
384 inputMethodView = sender; |
|
385 |
|
386 NSEvent *event = [eventArray objectAtIndex:0]; |
|
387 unsigned modifierFlags = [event modifierFlags]; |
|
388 NSMutableArray *modifiers = [[NSMutableArray alloc] init]; |
|
389 if (modifierFlags & NSAlphaShiftKeyMask) |
|
390 [modifiers addObject:@"NSAlphaShiftKeyMask"]; |
|
391 if (modifierFlags & NSShiftKeyMask) |
|
392 [modifiers addObject:@"NSShiftKeyMask"]; |
|
393 if (modifierFlags & NSControlKeyMask) |
|
394 [modifiers addObject:@"NSControlKeyMask"]; |
|
395 if (modifierFlags & NSAlternateKeyMask) |
|
396 [modifiers addObject:@"NSAlternateKeyMask"]; |
|
397 if (modifierFlags & NSCommandKeyMask) |
|
398 [modifiers addObject:@"NSCommandKeyMask"]; |
|
399 if (modifierFlags & NSNumericPadKeyMask) |
|
400 [modifiers addObject:@"NSNumericPadKeyMask"]; |
|
401 if (modifierFlags & NSHelpKeyMask) |
|
402 [modifiers addObject:@"NSHelpKeyMask"]; |
|
403 if (modifierFlags & NSFunctionKeyMask) |
|
404 [modifiers addObject:@"NSFunctionKeyMask"]; |
|
405 |
|
406 WebScriptObject* eventParam = [inputMethodHandler evaluateWebScript:@"new Object();"]; |
|
407 [eventParam setValue:[event characters] forKey:@"characters"]; |
|
408 [eventParam setValue:[event charactersIgnoringModifiers] forKey:@"charactersIgnoringModifiers"]; |
|
409 [eventParam setValue:[NSNumber numberWithBool:[event isARepeat]] forKey:@"isARepeat"]; |
|
410 [eventParam setValue:[NSNumber numberWithUnsignedShort:[event keyCode]] forKey:@"keyCode"]; |
|
411 [eventParam setValue:modifiers forKey:@"modifierFlags"]; |
|
412 |
|
413 [modifiers release]; |
|
414 |
|
415 id result = [inputMethodHandler callWebScriptMethod:@"call" withArguments:[NSArray arrayWithObjects:inputMethodHandler, eventParam, nil]]; |
|
416 if (![result respondsToSelector:@selector(boolValue)] || ![result boolValue]) |
|
417 [sender doCommandBySelector:@selector(noop:)]; // AppKit sends noop: if the ime does not handle an event |
|
418 |
|
419 inputMethodView = nil; |
|
420 return YES; |
|
421 } |
|
422 |
|
423 @end |