summaryrefslogtreecommitdiff
path: root/cocoa/selection.m
diff options
context:
space:
mode:
authorSven Weidauer <sven.weidauer@gmail.com>2011-01-14 17:50:11 +0000
committerSven Weidauer <sven.weidauer@gmail.com>2011-01-14 17:50:11 +0000
commit7bd51ba0ba97a56bdee4cefecf3c1db68c53b0b8 (patch)
tree2849302b40c19636309761996ae2ef60a843c2a2 /cocoa/selection.m
parent30d9439781bc7429c414438b6ca140f90fd45223 (diff)
downloadnetsurf-7bd51ba0ba97a56bdee4cefecf3c1db68c53b0b8.tar.gz
netsurf-7bd51ba0ba97a56bdee4cefecf3c1db68c53b0b8.tar.bz2
Implementing selection and clipboard functions.
svn path=/trunk/netsurf/; revision=11319
Diffstat (limited to 'cocoa/selection.m')
-rw-r--r--cocoa/selection.m95
1 files changed, 95 insertions, 0 deletions
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 <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 <Cocoa/Cocoa.h>
+
+#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;
+}
+