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/selection.m | 95 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 cocoa/selection.m (limited to 'cocoa/selection.m') 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