From 94f13d85551f45d323ed8658f8e849214a5c60e6 Mon Sep 17 00:00:00 2001 From: Michael Drake Date: Tue, 8 Jan 2013 17:06:37 +0000 Subject: Untested update for new clipboard API. --- cocoa/selection.m | 67 ++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 47 insertions(+), 20 deletions(-) (limited to 'cocoa') diff --git a/cocoa/selection.m b/cocoa/selection.m index 6bba74d34..69707ba33 100644 --- a/cocoa/selection.m +++ b/cocoa/selection.m @@ -36,47 +36,74 @@ void gui_clear_selection(struct gui_window *g) } -void gui_paste_from_clipboard(struct gui_window *g, int x, int y) +/** + * 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 + */ +void gui_get_clipboard(char **buffer, size_t *length) { NSPasteboard *pb = [NSPasteboard generalPasteboard]; NSString *string = [pb stringForType: NSStringPboardType]; + + *buffer = NULL; + *length = 0; + if (string) { const char *text = [string UTF8String]; - browser_window_paste_text( [(BrowserViewController *)g browser], text, strlen(text), true ); + *length = strlen(text); + + *buffer = malloc(gui_clipboard.length); + + if (*buffer != NULL) { + memcpy(*buffer, text, *length); + } else { + *length = 0; + } } } -bool gui_empty_clipboard(void) +/** + * 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 + */ +void gui_set_clipboard(const char *buffer, size_t length, + nsclipboard_styles styles[], int n_styles) { + /* Empty clipboard string */ 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, - const plot_font_style_t *fstyle) -{ + /* Add text to clipboard string */ 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: @" "]; + [cocoa_clipboard_string appendString: [[[NSString alloc] + initWithBytes: buffer + length: length + encoding: NSUTF8StringEncoding] + autorelease]]; - return true; -} - -bool gui_commit_clipboard(void) -{ + /* 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]; - if (result) gui_empty_clipboard(); - return result; + + if (result) { + /* Empty clipboard string */ + if (nil == cocoa_clipboard_string) { + cocoa_clipboard_string = [[NSMutableString alloc] init]; + } else { + [cocoa_clipboard_string setString: @""]; + } + } } bool gui_copy_to_clipboard(struct selection *s) -- cgit v1.2.3