//
//  ICeCoffEEWebKit.m
//  ICeCoffEE APE
//
//  Created by Nicholas Riley on Sun Jan 19 2003.
//  Copyright (c) 2003 Nicholas Riley. All rights reserved.
//

#import "ICeCoffEEWebKit.h"
#import <unistd.h>

// from WebCoreBridge.h
@interface WebCoreBridge : NSObject

- (NSDictionary *)elementAtPoint:(NSPoint)point;
- (NSString *)selectedString;
- (NSRect)selectionRect;

- (void)selectNSRange:(NSRange)range;
- (void)deselectAll;

@end

// from WebKit, private
@interface WebHTMLView : NSObject

- (WebCoreBridge *)_bridge;

- (NSRange)selectedRange;

@end

@implementation ICeCoffEEWebKit

- (NSMenu *)menuForEvent:(NSEvent *)e;
{
    NSMenu *myMenu = [super menuForEvent: e];
    return ICCF_MenuForEvent(self, myMenu, e);
}

static NSEvent *downEvent = nil;
static NSString *selectedString = nil;
static NSRange selectedRange;

- (void)mouseDown:(NSEvent *)e;
{
    [downEvent release]; downEvent = nil;
    // don't want command-control-click, command-shift-click, etc. to trigger
    if (ICCF_enabled && ICCF_prefs.commandClickEnabled && ICCF_EventIsCommandMouseDown(e)) {
        WebCoreBridge *bridge = [(WebHTMLView *)self _bridge];
	if ([self respondsToSelector: @selector(selectedRange)]) {
	    // save selection: it may be deselected on super mouseDown
	    selectedRange = [(WebHTMLView *)self selectedRange];
	}
	[selectedString release]; selectedString = nil;
	selectedString = [[bridge selectedString] retain];
        downEvent = [e retain];
    }
    [super mouseDown: e];
}

- (void)mouseUp:(NSEvent *)e;
{
    [super mouseUp: e];
    if (downEvent != nil) {
        NSPoint downPt = [downEvent locationInWindow];
        NSPoint upPt = [e locationInWindow];
		[downEvent release]; downEvent = nil;
        if (abs(downPt.x - upPt.x) > kICHysteresisPixels && abs(downPt.y - upPt.y) > kICHysteresisPixels)
            return;

        NS_DURING
            WebCoreBridge *bridge = [(WebHTMLView *)self _bridge];
            NSPoint viewClickPt = [self convertPoint: downPt fromView: nil];
            NSDictionary *elementDict = [bridge elementAtPoint: viewClickPt];
            ICLog(@"elementDict: %@", elementDict);
            NSAssert([elementDict count] != 0, @"Internal error: Got empty element dictionary from WebHTMLView");
            if ([elementDict objectForKey: @"WebElementLinkURL"] != nil) {
                ICLog(@"got a link");
                NS_VOIDRETURN; // donÕt activate on links
            }
	    if (selectedString == nil || [selectedString length] == 0) {
		ICLog(@"no selected string");
		NS_VOIDRETURN;
	    }
	    ICCF_StartIC();
	    ICCF_LaunchURL(selectedString, ICCF_KeyboardAction());
	    if (ICCF_prefs.textBlinkEnabled && [bridge respondsToSelector: @selector(selectNSRange:)]) {
		[bridge selectNSRange: selectedRange];
		int i;
		NSRect selectionRect = [bridge selectionRect];
		ICLog(@"selectedRange %@ selectionRect %@ textBlinkCount %d", NSStringFromRange(selectedRange), NSStringFromRect(selectionRect), ICCF_prefs.textBlinkCount);
		for (i = 0 ; i < ICCF_prefs.textBlinkCount ; i++) {
		    [bridge deselectAll];
		    [self setNeedsDisplayInRect: selectionRect];
		    [self display];
		    usleep(kICBlinkDelayUsecs);
		    [bridge selectNSRange: selectedRange];
		    [self setNeedsDisplayInRect: selectionRect];
		    [self display];
		    usleep(kICBlinkDelayUsecs);
		}
	    }
        NS_HANDLER
            ICCF_HandleException(localException);
        NS_ENDHANDLER

        ICCF_StopIC();
    }        
}

@end
