From 28ff0cfb0af7d58244dfd06546b2c8ec37f01c75 Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Tue, 1 Feb 2011 08:39:32 +0000 Subject: [PATCH] Factored Tree class out of TreeView svn path=/trunk/netsurf/; revision=11577 --- cocoa/Makefile.target | 1 + cocoa/NetSurf.xcodeproj/project.pbxproj | 16 ++- cocoa/Tree.h | 58 ++++++++++ cocoa/Tree.m | 140 ++++++++++++++++++++++++ cocoa/TreeView.h | 5 +- cocoa/TreeView.m | 124 ++++++++++----------- 6 files changed, 272 insertions(+), 72 deletions(-) create mode 100644 cocoa/Tree.h create mode 100644 cocoa/Tree.m diff --git a/cocoa/Makefile.target b/cocoa/Makefile.target index b6a3d2511..2c429f3f6 100644 --- a/cocoa/Makefile.target +++ b/cocoa/Makefile.target @@ -74,6 +74,7 @@ S_COCOA := \ ScrollableView.m \ SearchWindowController.m \ URLFieldCell.m \ + Tree.m \ TreeView.m \ HistoryView.m \ FormSelectMenu.m \ diff --git a/cocoa/NetSurf.xcodeproj/project.pbxproj b/cocoa/NetSurf.xcodeproj/project.pbxproj index 8df9dfa12..b15887b9f 100644 --- a/cocoa/NetSurf.xcodeproj/project.pbxproj +++ b/cocoa/NetSurf.xcodeproj/project.pbxproj @@ -184,6 +184,8 @@ 263629C812F69B120048542C /* NetsurfApp.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NetsurfApp.h; sourceTree = ""; }; 263629C912F69B120048542C /* NetsurfApp.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NetsurfApp.m; sourceTree = ""; }; 263629CA12F69B120048542C /* system_colour.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = system_colour.m; sourceTree = ""; }; + 263769A912F7EBE2000F45FE /* Tree.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Tree.h; sourceTree = ""; }; + 263769AA12F7EBE2000F45FE /* Tree.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Tree.m; sourceTree = ""; }; 2639E20512F2ADEE00699678 /* coordinates.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = coordinates.h; sourceTree = ""; }; 264C344112F0987E00D11246 /* gui.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = gui.h; sourceTree = ""; }; 265F30A712D6637E0048B600 /* NetSurf-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "NetSurf-Info.plist"; sourceTree = ""; }; @@ -519,12 +521,24 @@ name = Makefiles; sourceTree = ""; }; + 263769A812F7EBC3000F45FE /* Tree View */ = { + isa = PBXGroup; + children = ( + 2622F1D512DCD84600CD5A62 /* TreeView.h */, + 2622F1D612DCD84600CD5A62 /* TreeView.m */, + 263769A912F7EBE2000F45FE /* Tree.h */, + 263769AA12F7EBE2000F45FE /* Tree.m */, + ); + name = "Tree View"; + sourceTree = ""; + }; 265F303F12D6637E0048B600 /* Cocoa Frontend */ = { isa = PBXGroup; children = ( 26CDD26512E74402004FC66B /* Browser */, 26CDD26712E74453004FC66B /* Download */, 26CDD26612E7441E004FC66B /* Views */, + 263769A812F7EBC3000F45FE /* Tree View */, 26CDD26812E74461004FC66B /* NSApplication */, 26CDD26912E7446E004FC66B /* Platform Interface */, 265F310F12D663C20048B600 /* Resources */, @@ -674,8 +688,6 @@ children = ( 26AFE8E212DF4200005AD082 /* ScrollableView.h */, 26AFE8E312DF4200005AD082 /* ScrollableView.m */, - 2622F1D512DCD84600CD5A62 /* TreeView.h */, - 2622F1D612DCD84600CD5A62 /* TreeView.m */, 26EC3B6812ED62C0000A960C /* URLFieldCell.h */, 26EC3B6912ED62C0000A960C /* URLFieldCell.m */, 26EC3C4212ED8202000A960C /* HistoryView.h */, diff --git a/cocoa/Tree.h b/cocoa/Tree.h new file mode 100644 index 000000000..b03f48be0 --- /dev/null +++ b/cocoa/Tree.h @@ -0,0 +1,58 @@ +/* + * Copyright 2011 Sven Weidauer + * + * 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 . + */ + +#import + +#import "desktop/tree.h" + +@class Tree; + +@protocol TreeDelegate + +- (void) tree: (Tree *)tree requestedRedrawInRect: (NSRect) rect; +- (void) tree: (Tree *)tree resized: (NSSize) size; +- (void) tree: (Tree *)tree scrollPoint: (NSPoint) point; +- (NSSize) treeWindowSize: (Tree *)tree; + +@end + + +@interface Tree : NSObject { + id delegate; + struct tree *tree; +} + +@property (readwrite, assign, nonatomic) id delegate; +@property (readwrite, assign, nonatomic, getter=isRedrawing) BOOL redrawing; + +- initWithFlags: (unsigned int) flags; + +- (struct node *) rootNode; +- (struct tree *) tree; + +@end + + +@interface Tree (ViewInterface) + +- (void) drawRect: (NSRect) rect inView: (NSView *) view; +- (void) mouseAction: (browser_mouse_state)state atPoint: (NSPoint)point; +- (void) mouseDragEnd: (browser_mouse_state)state fromPoint: (NSPoint)p0 toPoint: (NSPoint) p1; +- (void) keyPress: (uint32_t) key; + +@end diff --git a/cocoa/Tree.m b/cocoa/Tree.m new file mode 100644 index 000000000..b5cbf2068 --- /dev/null +++ b/cocoa/Tree.m @@ -0,0 +1,140 @@ +/* + * Copyright 2011 Sven Weidauer + * + * 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 . + */ + +#import "cocoa/Tree.h" +#import "cocoa/coordinates.h" + +#import "desktop/tree.h" + +@implementation Tree + +@synthesize delegate; + +static void tree_redraw_request( int x, int y, int w, int h, void *data ); +static void tree_resized( struct tree *tree, int w, int h, void *data ); +static void tree_scroll_visible( int y, int height, void *data ); +static void tree_get_window_dimensions( int *width, int *height, void *data ); + +static const struct treeview_table cocoa_tree_callbacks = { + .redraw_request = tree_redraw_request, + .resized = tree_resized, + .scroll_visible = tree_scroll_visible, + .get_window_dimensions = tree_get_window_dimensions +}; + +- initWithFlags: (unsigned int)flags; +{ + if ((self = [super init]) == nil) return nil; + + tree = tree_create( flags, &cocoa_tree_callbacks, self ); + if (tree == NULL) { + [self release]; + return nil; + } + + return self; +} + + +- (void) dealloc; +{ + tree_delete( tree ); + [super dealloc]; +} + +- (struct node *) rootNode; +{ + return tree_get_root( tree ); +} + +- (struct tree *) tree; +{ + return tree; +} + +- (BOOL) isRedrawing; +{ + return tree_get_redraw( tree ); +} + +- (void) setRedrawing: (BOOL) newRedrawing; +{ + tree_set_redraw( tree, newRedrawing ); +} + +//MARK: - +//MARK: Callbacks + +static void tree_redraw_request( int x, int y, int w, int h, void *data ) +{ + id delegate = ((Tree *)data)->delegate; + [delegate tree: (Tree *)data requestedRedrawInRect: cocoa_rect_wh( x, y, w, h )]; +} + +static void tree_resized( struct tree *tree, int w, int h, void *data ) +{ + id delegate = ((Tree *)data)->delegate; + [delegate tree: (Tree *)data resized: cocoa_size( w, h )]; +} + +static void tree_scroll_visible( int y, int height, void *data ) +{ + id delegate = ((Tree *)data)->delegate; + [delegate tree: (Tree *)data scrollPoint: cocoa_point( 0, y )]; +} + +static void tree_get_window_dimensions( int *width, int *height, void *data ) +{ + id delegate = ((Tree *)data)->delegate; + if (delegate == nil) return; + + NSSize size = [delegate treeWindowSize: (Tree *)data]; + + *width = cocoa_pt_to_px( size.width ); + *height = cocoa_pt_to_px( size.height ); +} + +@end + +@implementation Tree (ViewInterface) + +- (void) drawRect: (NSRect) rect inView: (NSView *) view; +{ + tree_draw( tree, 0, 0, cocoa_pt_to_px( NSMinX( rect ) ), cocoa_pt_to_px( NSMinY( rect )), + cocoa_pt_to_px( NSWidth( rect ) ), cocoa_pt_to_px( NSHeight( rect ) ) ); +} + +- (void) mouseAction: (browser_mouse_state)state atPoint: (NSPoint)point; +{ + tree_mouse_action( tree, state, cocoa_pt_to_px( point.x ), cocoa_pt_to_px( point.y ) ); +} + +- (void) mouseDragEnd: (browser_mouse_state)state fromPoint: (NSPoint)p0 toPoint: (NSPoint) p1; +{ + tree_drag_end( tree, state, + cocoa_pt_to_px( p0.x ), cocoa_pt_to_px( p0.y ), + cocoa_pt_to_px( p1.x ), cocoa_pt_to_px( p1.y ) ); +} + +- (void) keyPress: (uint32_t) key; +{ + tree_keypress( tree, key ); +} + +@end + diff --git a/cocoa/TreeView.h b/cocoa/TreeView.h index 3e6f18496..31dedbb0f 100644 --- a/cocoa/TreeView.h +++ b/cocoa/TreeView.h @@ -19,13 +19,16 @@ #import #import "cocoa/ScrollableView.h" +@class Tree; @interface TreeView : ScrollableView { - struct tree *treeHandle; + Tree *tree; BOOL isDragging; NSPoint dragStart; } +@property (readwrite, retain, nonatomic) Tree *tree; + @end diff --git a/cocoa/TreeView.m b/cocoa/TreeView.m index 633ef649e..434ceecd4 100644 --- a/cocoa/TreeView.m +++ b/cocoa/TreeView.m @@ -17,52 +17,20 @@ */ #import "cocoa/TreeView.h" -#import "cocoa/coordinates.h" +#import "cocoa/Tree.h" -#import "desktop/tree.h" #import "desktop/plotters.h" -#import "desktop/history_global_core.h" + +@interface TreeView () +@end @implementation TreeView -static void tree_redraw_request( int x, int y, int w, int h, void *data ); -static void tree_resized( struct tree *tree, int w, int h, void *data ); -static void tree_scroll_visible( int y, int height, void *data ); -static void tree_get_window_dimensions( int *width, int *height, void *data ); - -static const struct treeview_table cocoa_tree_callbacks = { - .redraw_request = tree_redraw_request, - .resized = tree_resized, - .scroll_visible = tree_scroll_visible, - .get_window_dimensions = tree_get_window_dimensions -}; - -- (id)initWithFrame:(NSRect)frame -{ - if ((self = [super initWithFrame:frame]) == nil) return nil; - - treeHandle = tree_create( history_global_get_tree_flags(), &cocoa_tree_callbacks, self ); - if (NULL == treeHandle) { - [self release]; - return nil; - } - history_global_initialise( treeHandle, "" ); - return self; -} - -- (void) dealloc; -{ - tree_delete( treeHandle ); - - [super dealloc]; -} +@synthesize tree; - (void)drawRect:(NSRect)dirtyRect { - tree_set_redraw( treeHandle, true ); - tree_draw( treeHandle, 0, 0, - cocoa_pt_to_px( NSMinX( dirtyRect ) ), cocoa_pt_to_px( NSMinY( dirtyRect ) ), - cocoa_pt_to_px( NSWidth( dirtyRect ) ), cocoa_pt_to_px( NSHeight( dirtyRect ) ) ); + [tree drawRect: dirtyRect inView: self]; } - (BOOL) isFlipped; @@ -70,68 +38,86 @@ static const struct treeview_table cocoa_tree_callbacks = { return YES; } -- (void) mouseDown: (NSEvent *)theEvent; +- (void) dealloc; { - NSPoint point = [self convertPoint: [theEvent locationInWindow] fromView: nil]; - dragStart = point; - - tree_mouse_action( treeHandle, BROWSER_MOUSE_PRESS_1, - cocoa_pt_to_px( point.x ), cocoa_pt_to_px( point.y ) ); + [self setTree: nil]; + [super dealloc]; } -- (void) mouseUp: (NSEvent *)theEvent; +- (void) setTree: (Tree *)newTree; { - NSPoint point = [self convertPoint: [theEvent locationInWindow] fromView: nil]; - if (isDragging) { - tree_drag_end( treeHandle, BROWSER_MOUSE_DRAG_1, - cocoa_pt_to_px( dragStart.x ), cocoa_pt_to_px( dragStart.y ), - cocoa_pt_to_px( point.x ), cocoa_pt_to_px( point.y ) ); - isDragging = NO; - } else { - tree_mouse_action( treeHandle, BROWSER_MOUSE_CLICK_1, - cocoa_pt_to_px( point.x ), cocoa_pt_to_px( point.y ) ); + if (tree != newTree) { + [tree setRedrawing: NO]; + [tree setDelegate: nil]; + [tree release]; + + tree = [newTree retain]; + [tree setDelegate: self]; + [tree setRedrawing: YES]; } } +//MARK: - +//MARK: Event handlers + +- (void)mouseDown: (NSEvent *)event; +{ + isDragging = NO; + dragStart = [self convertPoint: [event locationInWindow] fromView: nil]; + [tree mouseAction: BROWSER_MOUSE_PRESS_1 atPoint: dragStart]; +} #define squared(x) ((x)*(x)) #define MinDragDistance (5.0) -- (void) mouseDragged: (NSEvent *)theEvent; +- (void) mouseDragged: (NSEvent *)event; { - NSPoint point = [self convertPoint: [theEvent locationInWindow] fromView: nil]; + const NSPoint point = [self convertPoint: [event locationInWindow] fromView: nil]; if (!isDragging) { const CGFloat distance = squared( dragStart.x - point.x ) + squared( dragStart.y - point.y ); if (distance >= squared( MinDragDistance)) isDragging = YES; } +} + +- (void) mouseUp: (NSEvent *)event; +{ + const NSPoint point = [self convertPoint: [event locationInWindow] fromView: nil]; + + browser_mouse_state modifierFlags = 0; if (isDragging) { - tree_mouse_action( treeHandle, BROWSER_MOUSE_DRAG_1, - cocoa_pt_to_px( point.x ), cocoa_pt_to_px( point.y ) ); - } + isDragging = NO; + [tree mouseDragEnd: modifierFlags fromPoint: dragStart toPoint: point]; + } else { + modifierFlags |= BROWSER_MOUSE_CLICK_1; + if ([event clickCount] == 2) modifierFlags |= BROWSER_MOUSE_DOUBLE_CLICK; + [tree mouseAction: modifierFlags atPoint: point]; + } } -static void tree_redraw_request( int x, int y, int w, int h, void *data ) + +//MARK: - +//MARK: Tree delegate methods + +- (void) tree: (Tree *)t requestedRedrawInRect: (NSRect) rect; { - [(TreeView *)data setNeedsDisplayInRect: cocoa_rect_wh( x, y, w, h )]; + [self setNeedsDisplayInRect: rect]; } -static void tree_resized( struct tree *tree, int w, int h, void *data ) +- (void) tree: (Tree *)t resized: (NSSize) size; { - [(TreeView *)data setMinimumSize: cocoa_size( w, h )]; + [self setMinimumSize: size]; } -static void tree_scroll_visible( int y, int height, void *data ) +- (void) tree: (Tree *)t scrollPoint: (NSPoint) point; { - [(TreeView *)data scrollPoint: cocoa_point( 0, y )]; + [self scrollPoint: point]; } -static void tree_get_window_dimensions( int *width, int *height, void *data ) +- (NSSize) treeWindowSize: (Tree *)t; { - NSSize size = [(TreeView *)data frame].size; - *width = cocoa_pt_to_px( size.width ); - *height = cocoa_pt_to_px( size.height ); + return [self frame].size; } @end