netsurf/cocoa/selection.m

106 lines
3.0 KiB
Mathematica
Raw Normal View History

/*
* 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 "cocoa/BrowserViewController.h"
#import "cocoa/selection.h"
2014-01-16 19:17:48 +04:00
#import "desktop/gui.h"
2012-08-22 15:38:16 +04:00
#import "desktop/browser_private.h"
static NSMutableString *cocoa_clipboard_string;
2013-01-08 21:06:37 +04:00
/**
* Core asks front end for clipboard contents.
*
* \param buffer UTF-8 text, allocated by front end, ownership yeilded to core
* \param length Byte length of UTF-8 text in buffer
*/
2014-01-16 19:26:48 +04:00
static void gui_get_clipboard(char **buffer, size_t *length)
{
NSPasteboard *pb = [NSPasteboard generalPasteboard];
NSString *string = [pb stringForType: NSStringPboardType];
2013-01-08 21:06:37 +04:00
*buffer = NULL;
*length = 0;
if (string) {
const char *text = [string UTF8String];
2013-01-08 21:06:37 +04:00
*length = strlen(text);
2013-01-08 21:52:12 +04:00
*buffer = malloc(*length);
2013-01-08 21:06:37 +04:00
if (*buffer != NULL) {
memcpy(*buffer, text, *length);
} else {
*length = 0;
}
}
}
2013-01-08 21:06:37 +04:00
/**
* Core tells front end to put given text in clipboard
*
* \param buffer UTF-8 text, owned by core
* \param length Byte length of UTF-8 text in buffer
* \param styles Array of styles given to text runs, owned by core, or NULL
* \param n_styles Number of text run styles in array
*/
2014-01-16 19:26:48 +04:00
static void gui_set_clipboard(const char *buffer, size_t length,
2013-01-08 21:06:37 +04:00
nsclipboard_styles styles[], int n_styles)
{
2013-01-08 21:06:37 +04:00
/* Empty clipboard string */
if (nil == cocoa_clipboard_string) {
cocoa_clipboard_string = [[NSMutableString alloc] init];
} else {
[cocoa_clipboard_string setString: @""];
}
2013-01-08 21:06:37 +04:00
/* Add text to clipboard string */
2013-02-22 18:53:57 +04:00
if (nil == cocoa_clipboard_string) return;
2013-01-08 21:06:37 +04:00
[cocoa_clipboard_string appendString: [[[NSString alloc]
initWithBytes: buffer
length: length
encoding: NSUTF8StringEncoding]
autorelease]];
2013-01-08 21:06:37 +04:00
/* Stick it on the pasteboard */
NSPasteboard *pb = [NSPasteboard generalPasteboard];
[pb declareTypes: [NSArray arrayWithObject: NSStringPboardType] owner: nil];
bool result = [pb setString: cocoa_clipboard_string forType: NSStringPboardType];
2013-01-08 21:06:37 +04:00
if (result) {
/* Empty clipboard string */
if (nil == cocoa_clipboard_string) {
cocoa_clipboard_string = [[NSMutableString alloc] init];
} else {
[cocoa_clipboard_string setString: @""];
}
}
}
2014-01-16 19:17:48 +04:00
static struct gui_clipboard_table clipboard_table = {
.get = gui_get_clipboard,
.set = gui_set_clipboard,
};
struct gui_clipboard_table *cocoa_clipboard_table = &clipboard_table;