//
//  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

- (NSString *)selectedString;
- (NSRect)selectionRect;

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

@end

// from WebKit, private
@interface WebHTMLView : NSObject

- (WebCoreBridge *)_bridge;

- (NSRange)selectedRange;

- (NSDictionary *)elementAtPoint:(NSPoint)point;

@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;
    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];
        if (abs(downPt.x - upPt.x) > kICHysteresisPixels && abs(downPt.y - upPt.y) > kICHysteresisPixels)
            return;

        NS_DURING
            NSPoint viewClickPt = [self convertPoint: downPt fromView: nil];
            NSDictionary *elementDict = [(WebHTMLView *)self elementAtPoint: viewClickPt];
            WebCoreBridge *bridge = [(WebHTMLView *)self _bridge];
            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();
	    BOOL canSetSelection = [bridge respondsToSelector: @selector(selectNSRange:)];
	    if (canSetSelection) {
		// may have become deselected in mouseDown
		[bridge selectNSRange: selectedRange];
	    }
	    if (ICCF_LaunchURL(selectedString, ICCF_KeyboardAction(downEvent)) && ICCF_prefs.textBlinkEnabled && canSetSelection) {
		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

	[downEvent release]; downEvent = nil;
        ICCF_StopIC();
    }        
}

@end
