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

Last change on this file since 436 was 436, checked in by Nicholas Riley, 12 years ago

Use @try/@catch/@finally rather than macro-based exceptions

File size: 4.1 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 <unistd.h>
11
12// WebCoreBridge, from WebCoreBridge.h (Safari 2)
13// Web(Core)FrameBridge, from WebCoreFrameBridge.h (Safari 3)
14@interface WebCoreBridge : NSObject
15
16// can only use setMarkedText:selectedRange: if WebHTMLView is editable
17// or, should we move to DOM-based accessors, i.e. "setMarkDOMRange"?
18- (void)selectNSRange:(NSRange)range;
19
20@end
21
22// from WebKit, private -- XXX maybe I'm better off going back the other way, relying on public methods in the bridge?
23@interface WebHTMLView : NSObject
24
25- (NSString *)selectedString;
26- (NSRect)selectionRect;
27- (NSRect)_selectionRect; // Safari 2, supported in Safari 3 only for use with Mail
28- (NSRange)selectedRange; // XXX same as selectedNSRange in WebCoreFrameBridge.h?
29- (void)deselectAll;
30
31- (WebCoreBridge *)_bridge; /* WebFrameBridge in Safari 3 (see above) */
32
33- (NSDictionary *)elementAtPoint:(NSPoint)point;
34
35@end
36
37@implementation ICeCoffEEWebKit
38
39- (NSMenu *)menuForEvent:(NSEvent *)e;
40{
41    NSMenu *myMenu = [super menuForEvent: e];
42    return ICCF_MenuForEvent(self, myMenu, e);
43}
44
45static NSEvent *downEvent = nil;
46static NSString *selectedString = nil;
47static NSRange selectedRange;
48
49- (void)mouseDown:(NSEvent *)e;
50{
51    [downEvent release]; downEvent = nil;
52    if (ICCF_enabled && ICCF_prefs.commandClickEnabled && ICCF_EventIsCommandMouseDown(e)) {
53        if ([self respondsToSelector: @selector(selectedRange)]) {
54            // save selection: it may be deselected on super mouseDown
55            selectedRange = [(WebHTMLView *)self selectedRange];
56        }
57        [selectedString release]; selectedString = nil;
58        selectedString = [[(WebHTMLView *)self selectedString] retain];
59        downEvent = [e retain];
60    }
61    [super mouseDown: e];
62}
63
64- (void)mouseUp:(NSEvent *)e;
65{
66    [super mouseUp: e];
67    if (downEvent != nil) {
68        NSPoint downPt = [downEvent locationInWindow];
69        NSPoint upPt = [e locationInWindow];
70        if (abs(downPt.x - upPt.x) > kICHysteresisPixels && abs(downPt.y - upPt.y) > kICHysteresisPixels)
71            return;
72
73        @try {
74            NSPoint viewClickPt = [self convertPoint: downPt fromView: nil];
75            NSDictionary *elementDict = [(WebHTMLView *)self elementAtPoint: viewClickPt];
76            WebCoreBridge *bridge = [(WebHTMLView *)self _bridge];
77            ICLog(@"elementDict: %@", elementDict);
78            NSAssert([elementDict count] != 0, @"Internal error: Got empty element dictionary from WebHTMLView");
79            if ([elementDict objectForKey: @"WebElementLinkURL"] != nil) {
80                ICLog(@"got a link");
81                return; // don't activate on links
82            }
83            if (selectedString == nil || [selectedString length] == 0) {
84                ICLog(@"no selected string");
85                return;
86            }
87            ICCF_StartIC();
88            BOOL canSetSelection = [bridge respondsToSelector: @selector(selectNSRange:)];
89            if (canSetSelection) {
90                // may have become deselected in mouseDown
91                [bridge selectNSRange: selectedRange];
92            }
93            if (ICCF_LaunchURL(selectedString, ICCF_KeyboardAction(downEvent)) && ICCF_prefs.textBlinkEnabled && canSetSelection) {
94                int i;
95                NSRect selectionRect;
96                if ([self respondsToSelector: @selector(selectionRect)])
97                    selectionRect = [(WebHTMLView *)self selectionRect];
98                else
99                    selectionRect = [(WebHTMLView *)self _selectionRect];
100                ICLog(@"selectedRange %@ selectionRect %@ textBlinkCount %d", NSStringFromRange(selectedRange), NSStringFromRect(selectionRect), ICCF_prefs.textBlinkCount);
101                for (i = 0 ; i < ICCF_prefs.textBlinkCount ; i++) {
102                    [(WebHTMLView *)self deselectAll];
103                    [self setNeedsDisplayInRect: selectionRect];
104                    [self display];
105                    usleep(kICBlinkDelayUsecs);
106                    [bridge selectNSRange: selectedRange];
107                    [self setNeedsDisplayInRect: selectionRect];
108                    [self display];
109                    usleep(kICBlinkDelayUsecs);
110                }
111            }
112        } @catch (NSException *e) {
113            ICCF_HandleException(e, downEvent);
114        } @finally {
115            [downEvent release]; downEvent = nil;
116        }
117
118        ICCF_StopIC();
119    }       
120}
121
122@end
Note: See TracBrowser for help on using the repository browser.