2011-01-12 23:21:17 +03:00
|
|
|
/*
|
|
|
|
* Copyright 2011 Sven Weidauer <sven.weidauer@gmail.com>
|
|
|
|
*
|
|
|
|
* This file is part of NetSurf, http://www.netsurf-browser.org/
|
|
|
|
*
|
|
|
|
* NetSurf is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; version 2 of the License.
|
|
|
|
*
|
|
|
|
* NetSurf is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#import "BrowserView.h"
|
2011-01-24 18:01:34 +03:00
|
|
|
#import "HistoryView.h"
|
2011-01-12 23:21:17 +03:00
|
|
|
|
|
|
|
#import "desktop/browser.h"
|
|
|
|
#import "desktop/history_core.h"
|
|
|
|
#import "desktop/plotters.h"
|
|
|
|
#import "desktop/textinput.h"
|
2011-01-13 20:20:13 +03:00
|
|
|
#import "desktop/options.h"
|
2011-01-14 20:50:11 +03:00
|
|
|
#import "desktop/selection.h"
|
2011-01-12 23:21:17 +03:00
|
|
|
|
2011-01-17 18:00:18 +03:00
|
|
|
#import "cocoa/font.h"
|
2011-01-27 14:43:48 +03:00
|
|
|
#import "cocoa/plotter.h"
|
2011-01-24 18:01:34 +03:00
|
|
|
|
2011-01-28 16:22:40 +03:00
|
|
|
@interface BrowserView ()
|
|
|
|
|
|
|
|
- (void) scrollHorizontal: (CGFloat) amount;
|
|
|
|
- (void) scrollVertical: (CGFloat) amount;
|
|
|
|
- (CGFloat) pageScroll;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2011-01-12 23:21:17 +03:00
|
|
|
@implementation BrowserView
|
|
|
|
|
|
|
|
@synthesize browser;
|
|
|
|
@synthesize caretTimer;
|
2011-01-19 23:19:43 +03:00
|
|
|
@synthesize resizing = isResizing;
|
2011-01-12 23:21:17 +03:00
|
|
|
|
|
|
|
static const CGFloat CaretWidth = 1.0;
|
|
|
|
static const NSTimeInterval CaretBlinkTime = 0.8;
|
|
|
|
|
2011-01-20 14:38:10 +03:00
|
|
|
- (void) dealloc;
|
|
|
|
{
|
|
|
|
[self setCaretTimer: nil];
|
2011-01-24 18:01:34 +03:00
|
|
|
[history release];
|
|
|
|
|
2011-01-20 14:38:10 +03:00
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setCaretTimer: (NSTimer *)newTimer;
|
|
|
|
{
|
|
|
|
if (newTimer != caretTimer) {
|
|
|
|
[caretTimer invalidate];
|
|
|
|
[caretTimer release];
|
|
|
|
caretTimer = [newTimer retain];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-12 23:21:17 +03:00
|
|
|
static inline NSRect cocoa_get_caret_rect( BrowserView *view )
|
|
|
|
{
|
|
|
|
NSRect caretRect = {
|
2011-01-17 18:00:18 +03:00
|
|
|
.origin = NSMakePoint( view->caretPoint.x * view->browser->scale, view->caretPoint.y * view->browser->scale ),
|
|
|
|
.size = NSMakeSize( CaretWidth, view->caretHeight * view->browser->scale )
|
2011-01-12 23:21:17 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
return caretRect;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) removeCaret;
|
|
|
|
{
|
|
|
|
hasCaret = NO;
|
|
|
|
[self setNeedsDisplayInRect: cocoa_get_caret_rect( self )];
|
|
|
|
|
|
|
|
[self setCaretTimer: nil];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) addCaretAt: (NSPoint) point height: (CGFloat) height;
|
|
|
|
{
|
|
|
|
if (hasCaret) {
|
|
|
|
[self setNeedsDisplayInRect: cocoa_get_caret_rect( self )];
|
|
|
|
}
|
|
|
|
|
|
|
|
caretPoint = point;
|
|
|
|
caretHeight = height;
|
|
|
|
hasCaret = YES;
|
|
|
|
caretVisible = YES;
|
|
|
|
|
|
|
|
if (nil == caretTimer) {
|
|
|
|
[self setCaretTimer: [NSTimer scheduledTimerWithTimeInterval: CaretBlinkTime target: self selector: @selector(caretBlink:) userInfo: nil repeats: YES]];
|
|
|
|
} else {
|
|
|
|
[caretTimer setFireDate: [NSDate dateWithTimeIntervalSinceNow: CaretBlinkTime]];
|
|
|
|
}
|
|
|
|
|
|
|
|
[self setNeedsDisplayInRect: cocoa_get_caret_rect( self )];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
- (void) caretBlink: (NSTimer *)timer;
|
|
|
|
{
|
|
|
|
if (hasCaret) {
|
|
|
|
caretVisible = !caretVisible;
|
|
|
|
[self setNeedsDisplayInRect: cocoa_get_caret_rect( self )];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)drawRect:(NSRect)dirtyRect;
|
|
|
|
{
|
|
|
|
if (NULL == browser->current_content) return;
|
|
|
|
|
2011-01-20 14:38:14 +03:00
|
|
|
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
|
|
|
|
2011-01-28 15:18:58 +03:00
|
|
|
current_redraw_browser = browser;
|
2011-01-17 18:00:18 +03:00
|
|
|
cocoa_set_font_scale_factor( browser->scale );
|
|
|
|
|
2011-01-12 23:21:17 +03:00
|
|
|
NSRect frame = [self bounds];
|
|
|
|
|
2011-01-13 01:07:21 +03:00
|
|
|
const NSRect *rects = NULL;
|
|
|
|
NSInteger count = 0;
|
|
|
|
[self getRectsBeingDrawn: &rects count: &count];
|
|
|
|
|
|
|
|
for (NSInteger i = 0; i < count; i++) {
|
2011-01-13 14:15:40 +03:00
|
|
|
plot.clip( NSMinX( rects[i] ), NSMinY( rects[i]), NSMaxX( rects[i] ), NSMaxY( rects[i] ) );
|
|
|
|
|
2011-01-13 01:07:21 +03:00
|
|
|
content_redraw(browser->current_content,
|
|
|
|
0,
|
|
|
|
0,
|
2011-01-27 14:43:48 +03:00
|
|
|
cocoa_pt_to_px( NSWidth( frame ) ),
|
|
|
|
cocoa_pt_to_px( NSHeight( frame ) ),
|
|
|
|
cocoa_pt_to_px( NSMinX( rects[i] ) ),
|
|
|
|
cocoa_pt_to_px( NSMinY( rects[i] ) ),
|
|
|
|
cocoa_pt_to_px( NSMaxX( rects[i] ) ),
|
|
|
|
cocoa_pt_to_px( NSMaxY( rects[i] ) ),
|
2011-01-13 01:07:21 +03:00
|
|
|
browser->scale,
|
|
|
|
0xFFFFFF);
|
|
|
|
}
|
2011-01-28 15:18:58 +03:00
|
|
|
current_redraw_browser = NULL;
|
2011-01-12 23:21:17 +03:00
|
|
|
|
|
|
|
NSRect caretRect = cocoa_get_caret_rect( self );
|
|
|
|
if (hasCaret && caretVisible && [self needsToDrawRect: caretRect]) {
|
|
|
|
[[NSColor blackColor] set];
|
|
|
|
[NSBezierPath fillRect: caretRect];
|
|
|
|
}
|
|
|
|
|
2011-01-20 14:38:14 +03:00
|
|
|
[pool release];
|
2011-01-12 23:21:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) isFlipped;
|
|
|
|
{
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
2011-01-14 19:54:39 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-01-14 22:03:16 +03:00
|
|
|
- (NSPoint) convertMousePoint: (NSEvent *)event;
|
|
|
|
{
|
|
|
|
NSPoint location = [self convertPoint: [event locationInWindow] fromView: nil];
|
|
|
|
if (NULL != browser) {
|
|
|
|
location.x /= browser->scale;
|
|
|
|
location.y /= browser->scale;
|
|
|
|
}
|
2011-01-27 14:43:48 +03:00
|
|
|
location.x = cocoa_pt_to_px( location.x );
|
|
|
|
location.y = cocoa_pt_to_px( location.y );
|
2011-01-14 22:03:16 +03:00
|
|
|
return location;
|
|
|
|
}
|
|
|
|
|
2011-01-12 23:21:17 +03:00
|
|
|
- (void) mouseDown: (NSEvent *)theEvent;
|
|
|
|
{
|
2011-01-14 22:03:16 +03:00
|
|
|
dragStart = [self convertMousePoint: theEvent];
|
2011-01-12 23:21:17 +03:00
|
|
|
|
2011-01-14 22:03:16 +03:00
|
|
|
browser_window_mouse_click( browser, BROWSER_MOUSE_PRESS_1 | cocoa_mouse_flags_for_event( theEvent ), dragStart.x, dragStart.y );
|
2011-01-12 23:21:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) mouseUp: (NSEvent *)theEvent;
|
|
|
|
{
|
2011-01-24 18:01:34 +03:00
|
|
|
if (historyVisible) {
|
2011-01-25 12:53:39 +03:00
|
|
|
[self setHistoryVisible: NO];
|
2011-01-24 18:01:34 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-01-14 22:03:16 +03:00
|
|
|
NSPoint location = [self convertMousePoint: theEvent];
|
2011-01-14 19:54:39 +03:00
|
|
|
|
|
|
|
browser_mouse_state modifierFlags = cocoa_mouse_flags_for_event( theEvent );
|
2011-01-12 23:21:17 +03:00
|
|
|
|
2011-01-14 19:54:39 +03:00
|
|
|
if (isDragging) {
|
|
|
|
isDragging = NO;
|
|
|
|
browser_window_mouse_drag_end( browser, modifierFlags, location.x, location.y );
|
|
|
|
} else {
|
2011-01-14 21:18:57 +03:00
|
|
|
modifierFlags |= BROWSER_MOUSE_CLICK_1;
|
|
|
|
if ([theEvent clickCount] == 2) modifierFlags |= BROWSER_MOUSE_DOUBLE_CLICK;
|
|
|
|
browser_window_mouse_click( browser, modifierFlags, location.x, location.y );
|
2011-01-14 19:54:39 +03:00
|
|
|
}
|
2011-01-12 23:21:17 +03:00
|
|
|
}
|
|
|
|
|
2011-01-14 19:54:39 +03:00
|
|
|
#define squared(x) ((x)*(x))
|
|
|
|
#define MinDragDistance (5.0)
|
|
|
|
|
2011-01-12 23:21:17 +03:00
|
|
|
- (void) mouseDragged: (NSEvent *)theEvent;
|
|
|
|
{
|
2011-01-14 22:03:16 +03:00
|
|
|
NSPoint location = [self convertMousePoint: theEvent];
|
2011-01-28 15:18:58 +03:00
|
|
|
browser_mouse_state modifierFlags = cocoa_mouse_flags_for_event( theEvent );
|
2011-01-14 19:54:39 +03:00
|
|
|
|
|
|
|
if (!isDragging) {
|
|
|
|
const CGFloat distance = squared( dragStart.x - location.x ) + squared( dragStart.y - location.y );
|
2011-01-28 15:18:58 +03:00
|
|
|
|
|
|
|
if (distance >= squared( MinDragDistance)) {
|
|
|
|
isDragging = YES;
|
|
|
|
browser_window_mouse_click( browser, BROWSER_MOUSE_DRAG_1 | modifierFlags, dragStart.x, dragStart.y );
|
|
|
|
}
|
2011-01-14 19:54:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isDragging) {
|
|
|
|
browser_window_mouse_track( browser, BROWSER_MOUSE_HOLDING_1 | BROWSER_MOUSE_DRAG_ON | modifierFlags, location.x, location.y );
|
|
|
|
}
|
2011-01-12 23:21:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) mouseMoved: (NSEvent *)theEvent;
|
|
|
|
{
|
2011-01-14 22:03:16 +03:00
|
|
|
NSPoint location = [self convertMousePoint: theEvent];
|
2011-01-14 19:54:39 +03:00
|
|
|
|
|
|
|
browser_window_mouse_track( browser, cocoa_mouse_flags_for_event( theEvent ), location.x, location.y );
|
2011-01-12 23:21:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) keyDown: (NSEvent *)theEvent;
|
|
|
|
{
|
|
|
|
[self interpretKeyEvents: [NSArray arrayWithObject: theEvent]];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) insertText: (id)string;
|
|
|
|
{
|
|
|
|
for (NSUInteger i = 0, length = [string length]; i < length; i++) {
|
|
|
|
unichar ch = [string characterAtIndex: i];
|
|
|
|
browser_window_key_press( browser, ch );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) moveLeft: (id)sender;
|
|
|
|
{
|
2011-01-28 16:22:40 +03:00
|
|
|
if (browser_window_key_press( browser, KEY_LEFT )) return;
|
|
|
|
[self scrollHorizontal: -[[self enclosingScrollView] horizontalLineScroll]];
|
2011-01-12 23:21:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) moveRight: (id)sender;
|
|
|
|
{
|
2011-01-28 16:22:40 +03:00
|
|
|
if (browser_window_key_press( browser, KEY_RIGHT )) return;
|
|
|
|
[self scrollHorizontal: [[self enclosingScrollView] horizontalLineScroll]];
|
2011-01-12 23:21:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) moveUp: (id)sender;
|
|
|
|
{
|
2011-01-28 16:22:40 +03:00
|
|
|
if (browser_window_key_press( browser, KEY_UP )) return;
|
|
|
|
[self scrollVertical: -[[self enclosingScrollView] lineScroll]];
|
2011-01-12 23:21:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) moveDown: (id)sender;
|
|
|
|
{
|
2011-01-28 15:18:58 +03:00
|
|
|
if (browser_window_key_press( browser, KEY_DOWN )) return;
|
2011-01-28 16:22:40 +03:00
|
|
|
[self scrollVertical: [[self enclosingScrollView] lineScroll]];
|
2011-01-12 23:21:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) deleteBackward: (id)sender;
|
|
|
|
{
|
|
|
|
browser_window_key_press( browser, KEY_DELETE_LEFT );
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) deleteForward: (id)sender;
|
|
|
|
{
|
|
|
|
browser_window_key_press( browser, KEY_DELETE_RIGHT );
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) cancelOperation: (id)sender;
|
|
|
|
{
|
|
|
|
browser_window_key_press( browser, KEY_ESCAPE );
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) scrollPageUp: (id)sender;
|
|
|
|
{
|
2011-01-28 16:22:40 +03:00
|
|
|
if (browser_window_key_press( browser, KEY_PAGE_UP )) return;
|
|
|
|
[self scrollVertical: -[self pageScroll]];
|
2011-01-12 23:21:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) scrollPageDown: (id)sender;
|
|
|
|
{
|
2011-01-28 16:22:40 +03:00
|
|
|
if (browser_window_key_press( browser, KEY_PAGE_DOWN )) return;
|
|
|
|
[self scrollVertical: [self pageScroll]];
|
2011-01-12 23:21:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) insertTab: (id)sender;
|
|
|
|
{
|
|
|
|
browser_window_key_press( browser, KEY_TAB );
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) insertBacktab: (id)sender;
|
|
|
|
{
|
|
|
|
browser_window_key_press( browser, KEY_SHIFT_TAB );
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) moveToBeginningOfLine: (id)sender;
|
|
|
|
{
|
|
|
|
browser_window_key_press( browser, KEY_LINE_START );
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) moveToEndOfLine: (id)sender;
|
|
|
|
{
|
|
|
|
browser_window_key_press( browser, KEY_LINE_END );
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) moveToBeginningOfDocument: (id)sender;
|
|
|
|
{
|
2011-01-28 16:22:40 +03:00
|
|
|
if (browser_window_key_press( browser, KEY_TEXT_START )) return;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) scrollToBeginningOfDocument: (id) sender;
|
|
|
|
{
|
|
|
|
NSPoint origin = [self visibleRect].origin;
|
|
|
|
origin.y = 0;
|
|
|
|
[self scrollPoint: origin];
|
2011-01-12 23:21:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) moveToEndOfDocument: (id)sender;
|
|
|
|
{
|
|
|
|
browser_window_key_press( browser, KEY_TEXT_END );
|
|
|
|
}
|
|
|
|
|
2011-01-28 16:22:40 +03:00
|
|
|
- (void) scrollToEndOfDocument: (id) sender;
|
|
|
|
{
|
|
|
|
NSPoint origin = [self visibleRect].origin;
|
|
|
|
origin.y = NSHeight( [self frame] );
|
|
|
|
[self scrollPoint: origin];
|
|
|
|
}
|
|
|
|
|
2011-01-12 23:21:17 +03:00
|
|
|
- (void) insertNewline: (id)sender;
|
|
|
|
{
|
|
|
|
browser_window_key_press( browser, KEY_NL );
|
|
|
|
}
|
|
|
|
|
2011-01-14 20:50:11 +03:00
|
|
|
- (void) selectAll: (id)sender;
|
|
|
|
{
|
|
|
|
browser_window_key_press( browser, KEY_SELECT_ALL );
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) copy: (id) sender;
|
|
|
|
{
|
|
|
|
browser_window_key_press( browser, KEY_COPY_SELECTION );
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) cut: (id) sender;
|
|
|
|
{
|
|
|
|
browser_window_key_press( browser, KEY_CUT_SELECTION );
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) paste: (id) sender;
|
|
|
|
{
|
|
|
|
browser_window_key_press( browser, KEY_PASTE );
|
|
|
|
}
|
|
|
|
|
2011-01-19 23:19:43 +03:00
|
|
|
- (BOOL) acceptsFirstResponder;
|
2011-01-12 23:21:17 +03:00
|
|
|
{
|
|
|
|
return YES;
|
|
|
|
}
|
2011-01-14 20:50:11 +03:00
|
|
|
|
2011-01-19 23:19:43 +03:00
|
|
|
- (void) adjustFrame;
|
2011-01-18 11:26:56 +03:00
|
|
|
{
|
2011-01-19 23:19:43 +03:00
|
|
|
if (!isResizing) {
|
|
|
|
NSSize frameSize = [[self superview] frame].size;
|
2011-01-27 14:43:48 +03:00
|
|
|
browser_window_reformat( browser, cocoa_pt_to_px( frameSize.width ), cocoa_pt_to_px( frameSize.height ) );
|
2011-01-18 11:26:56 +03:00
|
|
|
}
|
|
|
|
|
2011-01-19 23:19:43 +03:00
|
|
|
[super adjustFrame];
|
2011-01-12 23:21:17 +03:00
|
|
|
}
|
|
|
|
|
2011-01-25 12:53:39 +03:00
|
|
|
- (BOOL) isHistoryVisible;
|
|
|
|
{
|
|
|
|
return historyVisible;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setHistoryVisible: (BOOL) newVisible;
|
|
|
|
{
|
|
|
|
if (newVisible == historyVisible) return;
|
|
|
|
historyVisible = newVisible;
|
|
|
|
|
|
|
|
if (historyVisible) {
|
|
|
|
if (nil == history) history = [[HistoryView alloc] initWithBrowser: browser];
|
|
|
|
[history fadeIntoView: self];
|
|
|
|
} else {
|
|
|
|
[history fadeOut];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-28 16:22:40 +03:00
|
|
|
- (void) scrollHorizontal: (CGFloat) amount;
|
|
|
|
{
|
|
|
|
NSPoint currentPoint = [self visibleRect].origin;
|
|
|
|
currentPoint.x += amount;
|
|
|
|
[self scrollPoint: currentPoint];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) scrollVertical: (CGFloat) amount;
|
|
|
|
{
|
|
|
|
NSPoint currentPoint = [self visibleRect].origin;
|
|
|
|
currentPoint.y += amount;
|
|
|
|
[self scrollPoint: currentPoint];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (CGFloat) pageScroll;
|
|
|
|
{
|
|
|
|
return NSHeight( [[self superview] frame] ) - [[self enclosingScrollView] pageScroll];
|
|
|
|
}
|
|
|
|
|
2011-01-12 23:21:17 +03:00
|
|
|
@end
|