summaryrefslogtreecommitdiff
path: root/cocoa/selection.m
blob: 208776ebbda871be61418d786e2031b92b076700 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
 * 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 "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( [(BrowserViewController *)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)
{
	bool add_space = box != NULL ? box->space : false;

	if (whitespace_text && !gui_add_to_clipboard( whitespace_text, 
												 whitespace_length, false )) return false;
	return gui_add_to_clipboard( text, length, add_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;
}