From 7bd51ba0ba97a56bdee4cefecf3c1db68c53b0b8 Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Fri, 14 Jan 2011 17:50:11 +0000 Subject: Implementing selection and clipboard functions. svn path=/trunk/netsurf/; revision=11319 --- cocoa/BrowserView.m | 41 ++++++++++++++ cocoa/NetSurf.xcodeproj/project.pbxproj | 4 ++ cocoa/gui.m | 39 +------------- cocoa/selection.m | 95 +++++++++++++++++++++++++++++++++ 4 files changed, 142 insertions(+), 37 deletions(-) create mode 100644 cocoa/selection.m diff --git a/cocoa/BrowserView.m b/cocoa/BrowserView.m index e2112cf95..d95fc81e7 100644 --- a/cocoa/BrowserView.m +++ b/cocoa/BrowserView.m @@ -23,6 +23,7 @@ #import "desktop/plotters.h" #import "desktop/textinput.h" #import "desktop/options.h" +#import "desktop/selection.h" @implementation BrowserView @@ -275,6 +276,26 @@ static browser_mouse_state cocoa_mouse_flags_for_event( NSEvent *evt ) browser_window_key_press( browser, KEY_NL ); } +- (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 ); +} + - (void) setFrame: (NSRect)frameRect; { [super setFrame: frameRect]; @@ -337,6 +358,26 @@ static browser_mouse_state cocoa_mouse_flags_for_event( NSEvent *evt ) return YES; } + +- (BOOL) validateUserInterfaceItem: (id) item; +{ + SEL action = [item action]; + + if (action == @selector(copy:)) { + return selection_defined( browser->sel ); + } + + if (action == @selector(cut:)) { + return selection_defined( browser->sel ) && browser->caret_callback != NULL; + } + + if (action == @selector(paste:)) { + return browser->paste_callback != NULL; + } + + return YES; +} + - (BOOL) acceptsFirstResponder; { return YES; diff --git a/cocoa/NetSurf.xcodeproj/project.pbxproj b/cocoa/NetSurf.xcodeproj/project.pbxproj index aefe384f3..7d9cd5e51 100644 --- a/cocoa/NetSurf.xcodeproj/project.pbxproj +++ b/cocoa/NetSurf.xcodeproj/project.pbxproj @@ -115,6 +115,7 @@ 26AFE97C12DF514C005AD082 /* NetSurfAppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 26AFE97B12DF514C005AD082 /* NetSurfAppDelegate.m */; }; 26AFEAEB12E04253005AD082 /* DownloadWindowController.m in Sources */ = {isa = PBXBuildFile; fileRef = 26AFEAEA12E04253005AD082 /* DownloadWindowController.m */; }; 26AFEAF112E042F9005AD082 /* DownloadWindow.xib in Resources */ = {isa = PBXBuildFile; fileRef = 26AFEAF012E042F9005AD082 /* DownloadWindow.xib */; }; + 26AFED0412E09916005AD082 /* selection.m in Sources */ = {isa = PBXBuildFile; fileRef = 26AFED0312E09916005AD082 /* selection.m */; }; 8D11072F0486CEB800E47090 /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1058C7A1FEA54F0111CA2CBB /* Cocoa.framework */; }; /* End PBXBuildFile section */ @@ -336,6 +337,7 @@ 26AFEAE912E04253005AD082 /* DownloadWindowController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DownloadWindowController.h; sourceTree = ""; }; 26AFEAEA12E04253005AD082 /* DownloadWindowController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DownloadWindowController.m; sourceTree = ""; }; 26AFEAF012E042F9005AD082 /* DownloadWindow.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = DownloadWindow.xib; sourceTree = ""; }; + 26AFED0312E09916005AD082 /* selection.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = selection.m; sourceTree = ""; }; 8D1107320486CEB800E47090 /* NetSurf.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = NetSurf.app; sourceTree = BUILT_PRODUCTS_DIR; }; /* End PBXFileReference section */ @@ -617,6 +619,7 @@ 26AFE97B12DF514C005AD082 /* NetSurfAppDelegate.m */, 26AFEAE912E04253005AD082 /* DownloadWindowController.h */, 26AFEAEA12E04253005AD082 /* DownloadWindowController.m */, + 26AFED0312E09916005AD082 /* selection.m */, ); name = cocoa; sourceTree = ""; @@ -867,6 +870,7 @@ 26AFE8E412DF4200005AD082 /* ScrollableView.m in Sources */, 26AFE97C12DF514C005AD082 /* NetSurfAppDelegate.m in Sources */, 26AFEAEB12E04253005AD082 /* DownloadWindowController.m in Sources */, + 26AFED0412E09916005AD082 /* selection.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/cocoa/gui.m b/cocoa/gui.m index 09c80c7b2..a2b254d75 100644 --- a/cocoa/gui.m +++ b/cocoa/gui.m @@ -25,6 +25,8 @@ #import "desktop/netsurf.h" #import "desktop/browser.h" #import "desktop/options.h" +#import "desktop/textinput.h" +#import "desktop/selection.h" char *default_stylesheet_url; char *adblock_stylesheet_url; @@ -261,43 +263,6 @@ void gui_drag_save_selection(struct selection *s, struct gui_window *g) { } -void gui_start_selection(struct gui_window *g) -{ -} - -void gui_clear_selection(struct gui_window *g) -{ -} - - -void gui_paste_from_clipboard(struct gui_window *g, int x, int y) -{ - UNIMPL(); -} - -bool gui_empty_clipboard(void) -{ - return false; -} - -bool gui_add_to_clipboard(const char *text, size_t length, bool space) -{ - UNIMPL(); - return false; -} - -bool gui_commit_clipboard(void) -{ - UNIMPL(); - return false; -} - -bool gui_copy_to_clipboard(struct selection *s) -{ - UNIMPL(); - return false; -} - void gui_create_form_select_menu(struct browser_window *bw, struct form_control *control) diff --git a/cocoa/selection.m b/cocoa/selection.m new file mode 100644 index 000000000..b19d69f39 --- /dev/null +++ b/cocoa/selection.m @@ -0,0 +1,95 @@ +/* + * 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 "BrowserWindow.h" + +#import "desktop/selection.h" + + +static NSMutableString *cocoa_clipboard_string; + +void gui_start_selection(struct gui_window *g) +{ + gui_empty_clipboard(); +} + +void gui_clear_selection(struct gui_window *g) +{ +} + + +void gui_paste_from_clipboard(struct gui_window *g, int x, int y) +{ + NSPasteboard *pb = [NSPasteboard generalPasteboard]; + NSString *string = [pb stringForType: NSStringPboardType]; + if (string) { + const char *text = [string UTF8String]; + browser_window_paste_text( [(BrowserWindow *)g browser], text, strlen(text), true ); + } +} + +bool gui_empty_clipboard(void) +{ + if (nil == cocoa_clipboard_string) { + cocoa_clipboard_string = [[NSMutableString alloc] init]; + } else { + [cocoa_clipboard_string setString: @""]; + } + return true; +} + +bool gui_add_to_clipboard(const char *text, size_t length, bool space) +{ + if (nil == cocoa_clipboard_string) return false; + + [cocoa_clipboard_string appendString: [[[NSString alloc] initWithBytes: text + length: length + encoding: NSUTF8StringEncoding] + autorelease]]; + if (space) [cocoa_clipboard_string appendString: @" "]; + + return true; +} + +static bool cocoa_clipboard_copy_handler(const char *text, size_t length, struct box *box, + void *handle, const char *whitespace_text, + size_t whitespace_length) +{ + if (whitespace_text && !gui_add_to_clipboard( whitespace_text, + whitespace_length, false )) return false; + return gui_add_to_clipboard( text, length, box->space ); +} + +bool gui_commit_clipboard(void) +{ + NSPasteboard *pb = [NSPasteboard generalPasteboard]; + [pb declareTypes: [NSArray arrayWithObject: NSStringPboardType] owner: nil]; + bool result = [pb setString: cocoa_clipboard_string forType: NSStringPboardType]; + if (result) gui_empty_clipboard(); + return result; +} + +bool gui_copy_to_clipboard(struct selection *s) +{ + if (selection_defined( s ) && selection_traverse( s, cocoa_clipboard_copy_handler, NULL )) + gui_commit_clipboard(); + return true; +} + -- cgit v1.2.3