From 30d9439781bc7429c414438b6ca140f90fd45223 Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Fri, 14 Jan 2011 16:54:39 +0000 Subject: Better mouse handling for browser svn path=/trunk/netsurf/; revision=11318 --- cocoa/BrowserView.m | 42 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 3 deletions(-) (limited to 'cocoa/BrowserView.m') diff --git a/cocoa/BrowserView.m b/cocoa/BrowserView.m index d26fc8bdd..e2112cf95 100644 --- a/cocoa/BrowserView.m +++ b/cocoa/BrowserView.m @@ -122,28 +122,64 @@ static inline NSRect cocoa_get_caret_rect( BrowserView *view ) return YES; } +static browser_mouse_state cocoa_mouse_flags_for_event( NSEvent *evt ) +{ + browser_mouse_state result = 0; + + NSUInteger flags = [evt modifierFlags]; + + if (flags & NSShiftKeyMask) result |= BROWSER_MOUSE_MOD_1; + if (flags & NSAlternateKeyMask) result |= BROWSER_MOUSE_MOD_2; + + return result; +} + - (void) mouseDown: (NSEvent *)theEvent; { NSPoint location = [self convertPoint: [theEvent locationInWindow] fromView: nil]; + dragStart = location; - browser_window_mouse_click( browser, BROWSER_MOUSE_PRESS_1, location.x, location.y ); + browser_window_mouse_click( browser, BROWSER_MOUSE_PRESS_1 | cocoa_mouse_flags_for_event( theEvent ), location.x, location.y ); } - (void) mouseUp: (NSEvent *)theEvent; { NSPoint location = [self convertPoint: [theEvent locationInWindow] fromView: nil]; + + browser_mouse_state modifierFlags = cocoa_mouse_flags_for_event( theEvent ); - browser_window_mouse_click( browser, BROWSER_MOUSE_CLICK_1, location.x, location.y ); + if (isDragging) { + isDragging = NO; + browser_window_mouse_drag_end( browser, modifierFlags, location.x, location.y ); + } else { + browser_window_mouse_click( browser, BROWSER_MOUSE_CLICK_1 | modifierFlags, location.x, location.y ); + } } +#define squared(x) ((x)*(x)) +#define MinDragDistance (5.0) + - (void) mouseDragged: (NSEvent *)theEvent; { + NSPoint location = [self convertPoint: [theEvent locationInWindow] fromView: nil]; + + if (!isDragging) { + const CGFloat distance = squared( dragStart.x - location.x ) + squared( dragStart.y - location.y ); + if (distance >= squared( MinDragDistance)) isDragging = YES; + } + + if (isDragging) { + browser_mouse_state modifierFlags = cocoa_mouse_flags_for_event( theEvent ); + browser_window_mouse_click( browser, BROWSER_MOUSE_DRAG_1 | modifierFlags, location.x, location.y ); + browser_window_mouse_track( browser, BROWSER_MOUSE_HOLDING_1 | BROWSER_MOUSE_DRAG_ON | modifierFlags, location.x, location.y ); + } } - (void) mouseMoved: (NSEvent *)theEvent; { NSPoint location = [self convertPoint: [theEvent locationInWindow] fromView: nil]; - browser_window_mouse_click( browser, 0, location.x, location.y ); + + browser_window_mouse_track( browser, cocoa_mouse_flags_for_event( theEvent ), location.x, location.y ); } - (void) keyDown: (NSEvent *)theEvent; -- cgit v1.2.3