source: trunk/ICeCoffEE/ICeCoffEE/ICeCoffEEWebKit.m@ 448

Last change on this file since 448 was 448, checked in by Nicholas Riley, 15 years ago

Disable spelling and grammar correction marks during selection

File size: 6.5 KB
Line 
1//
2// ICeCoffEEWebKit.m
3// ICeCoffEE APE
4//
5// Created by Nicholas Riley on Sun Jan 19 2003.
6// Copyright (c) 2003 Nicholas Riley. All rights reserved.
7//
8
9#import "ICeCoffEEWebKit.h"
10#import "ICeCoffEEWebPolicyDelegate.h"
11#import "ICeCoffEEParser.h"
12#import "ICeCoffEETrigger.h"
13#import <WebKit/WebKit.h>
14#import <unistd.h>
15
16// XXX WebCoreFrameBridge is going away
17// (see <http://trac.webkit.org/projects/webkit/changeset/30712>)
18// WebCoreBridge, from WebCoreBridge.h (Safari 2)
19// Web(Core)FrameBridge, from WebCoreFrameBridge.h (Safari 3)
20@interface WebCoreBridge : NSObject
21
22// XXX move to DOM-based accessors
23- (DOMRange *)convertNSRangeToDOMRange:(NSRange)range;
24- (NSString *)stringForRange:(DOMRange *)range;
25
26@end
27
28// XXX WebHTMLView is going away
29@interface WebHTMLView : NSObject
30
31- (NSRect)selectionRect;
32- (DOMRange *)_documentRange;
33
34- (WebView *)_webView;
35- (WebCoreBridge *)_bridge; // WebFrameBridge in Safari 3 (see above)
36
37@end
38
39// from WebViewPrivate.h (will become public, part of WebViewEditing)
40@interface WebView (webViewGrammarChecking)
41- (BOOL)isGrammarCheckingEnabled;
42- (void)setGrammarCheckingEnabled:(BOOL)flag;
43@end
44
45@implementation ICeCoffEEWebKit
46
47- (NSMenu *)menuForEvent:(NSEvent *)e;
48{
49 NSMenu *myMenu = [super menuForEvent: e];
50 return ICCF_MenuForEvent(self, myMenu, e);
51}
52
53static NSEvent *downEvent = nil;
54static id /* (WebPolicyDelegate) */ policyDelegate;
55static id /* DOMRange */ selectedRange;
56
57- (void)mouseDown:(NSEvent *)e;
58{
59 [downEvent release]; downEvent = nil;
60 if (ICCF_enabled && ICCF_prefs.commandClickEnabled && ICCF_EventIsCommandMouseDown(e)) {
61 if ([self respondsToSelector: @selector(_webView)]) {
62 WebView *webView = [(WebHTMLView *)self _webView];
63
64 // save selection: it may be deselected on super mouseDown
65 selectedRange = [[webView selectedDOMRange] retain];
66
67 // stop any URL launching from happening
68 if ([webView isEditable]) {
69 policyDelegate = [[webView policyDelegate] retain];
70 [webView setPolicyDelegate: [ICeCoffEEWebPolicyDelegate sharedDelegate]];
71 }
72
73 downEvent = [e retain];
74 }
75 }
76 [super mouseDown: e];
77}
78
79- (void)mouseUp:(NSEvent *)e;
80{
81 [super mouseUp: e];
82
83 if (downEvent == nil)
84 return;
85
86 WebView *webView = [(WebHTMLView *)self _webView];
87 BOOL isEditable = [webView isEditable];
88 BOOL isContinuousSpellCheckingEnabled = NO, isGrammarCheckingEnabled = NO;
89
90 if (isEditable) {
91 [webView setPolicyDelegate: policyDelegate];
92 [policyDelegate release]; policyDelegate = nil;
93 }
94
95 NSPoint downPt = [downEvent locationInWindow];
96 NSPoint upPt = [e locationInWindow];
97 if (abs(downPt.x - upPt.x) > kICHysteresisPixels && abs(downPt.y - upPt.y) > kICHysteresisPixels)
98 return;
99
100 @try {
101 if (!isEditable) {
102 [webView setEditable: YES];
103
104 // don't want spelling/grammar marks to persist when view is made uneditable again
105 if ([webView respondsToSelector: @selector(isContinuousSpellCheckingEnabled)] &&
106 (isContinuousSpellCheckingEnabled = [webView isContinuousSpellCheckingEnabled])) {
107 if ([webView respondsToSelector: @selector(setContinuousSpellCheckingEnabled:)])
108 [webView setContinuousSpellCheckingEnabled: NO];
109 else
110 isContinuousSpellCheckingEnabled = NO; // don't restore
111 }
112
113 if ([webView respondsToSelector: @selector(isGrammarCheckingEnabled)] &&
114 (isGrammarCheckingEnabled = [webView isGrammarCheckingEnabled])) {
115 if ([webView respondsToSelector: @selector(setGrammarCheckingEnabled:)])
116 [webView setGrammarCheckingEnabled: NO];
117 else
118 isGrammarCheckingEnabled = NO; // don't restore
119 }
120 }
121
122
123 NSPoint viewClickPt = [webView convertPoint: downPt fromView: nil];
124 NSDictionary *elementDict = [webView elementAtPoint: viewClickPt];
125 NSLog(@"elementDict: %@", elementDict);
126 NSAssert([elementDict count] != 0, ICCF_LocalizedString(@"Sorry, ICeCoffEE was unable to find anything to select"));
127
128 WebCoreBridge *bridge = [(WebHTMLView *)self _bridge];
129
130 id link = [elementDict objectForKey: @"WebElementLinkURL"];
131 NSString *url = [link isKindOfClass: [NSURL class]] ? [link absoluteString] : nil;
132
133 ICCF_StartIC();
134
135 id /* DOMRange */ domRange = nil;
136
137 if (url != nil) {
138 ICLog(@"got a link");
139 if (!isEditable) {
140 ICCF_StopIC();
141 return;
142 }
143 // XXX handle existing selection
144 domRange = [webView selectedDOMRange];
145 [domRange selectNode: [elementDict objectForKey:@"WebElementDOMNode"]];
146 [webView setSelectedDOMRange: domRange affinity: NSSelectionAffinityDownstream];
147 } else {
148 // may have become deselected in mouseDown
149 [webView setSelectedDOMRange: selectedRange affinity: NSSelectionAffinityDownstream];
150
151 // XXX at some point, characterIndexForPoint: wasn't implemented, so this will fail
152 NSRange range = [ICeCoffEETrigger rangeForEvent: downEvent onTarget: (NSView<NSTextInput> *)self];
153
154 NSAssert(range.location != NSNotFound, ICCF_LocalizedString(@"Sorry, ICeCoffEE was unable to find anything to select"));
155
156 // XXX limit to a reasonable size
157 // NSRange fetchRange = [bridge convertDOMRangeToNSRange: [(WebHTMLView *)self _documentRange]];
158 NSString *s = [bridge stringForRange: [(WebHTMLView *)self _documentRange]];
159
160 if (range.length == 0) {
161 range.length = 1;
162 range = ICCF_URLEnclosingRange(s, range);
163 domRange = [bridge convertNSRangeToDOMRange: range];
164 [webView setSelectedDOMRange: domRange affinity: NSSelectionAffinityDownstream];
165 } else {
166 domRange = [bridge convertNSRangeToDOMRange: range];
167 }
168
169 url = [s substringWithRange: range];
170 }
171
172 if (ICCF_LaunchURL(url, ICCF_KeyboardAction(downEvent)) && ICCF_prefs.textBlinkEnabled && domRange != nil) {
173 NSRect selectionRect = [(WebHTMLView *)self selectionRect];
174 for (int i = 0 ; i < ICCF_prefs.textBlinkCount ; i++) {
175 [webView setSelectedDOMRange: nil affinity: NSSelectionAffinityDownstream];
176 [self setNeedsDisplayInRect: selectionRect];
177 [self display];
178 usleep(kICBlinkDelayUsecs);
179 [webView setSelectedDOMRange: domRange affinity: NSSelectionAffinityDownstream];
180 [self setNeedsDisplayInRect: selectionRect];
181 [self display];
182 usleep(kICBlinkDelayUsecs);
183 }
184 }
185 } @catch (NSException *e) {
186 ICCF_HandleException(e, downEvent);
187 } @finally {
188 [selectedRange release]; selectedRange = nil;
189 [downEvent release]; downEvent = nil;
190 if (!isEditable) {
191 [webView setEditable: NO];
192 if (isContinuousSpellCheckingEnabled)
193 [webView setContinuousSpellCheckingEnabled: YES];
194 if (isGrammarCheckingEnabled)
195 [webView setGrammarCheckingEnabled: YES];
196 }
197 }
198
199 ICCF_StopIC();
200}
201
202@end
Note: See TracBrowser for help on using the repository browser.