# HG changeset patch # User Olaf Wintermann # Date 1396706021 -7200 # Node ID a137277f9173a470f1f7482ea0d8ed2cec50d9bb # Parent 78ae3efe463f25faa72bb79418e17c766b89696a added copy & paste (Cocoa) diff -r 78ae3efe463f -r a137277f9173 application/main.c --- a/application/main.c Sat Apr 05 13:02:37 2014 +0200 +++ b/application/main.c Sat Apr 05 15:53:41 2014 +0200 @@ -110,10 +110,23 @@ void action_copy(UiEvent *event, void *data) { printf("copy\n"); + TestWindowData *wd = event->window; + int begin; + int end; + wd->text.selection(&wd->text, &begin, &end); + char *selection = wd->text.getsubstr(&wd->text, begin, end); + ui_clipboard_set(selection); } void action_paste(UiEvent *event, void *data) { printf("paste\n"); + TestWindowData *wd = event->window; + char *str = ui_clipboard_get(); + if(str) { + int pos = wd->text.position(&wd->text); + wd->text.insert(&wd->text, pos, str); + free(str); + } } void action_delete(UiEvent *event, void *data) { diff -r 78ae3efe463f -r a137277f9173 ui/cocoa/text.h --- a/ui/cocoa/text.h Sat Apr 05 13:02:37 2014 +0200 +++ b/ui/cocoa/text.h Sat Apr 05 15:53:41 2014 +0200 @@ -58,3 +58,5 @@ void ui_textarea_set(UiText *text, char *str); char* ui_textarea_getsubstr(UiText *text, int begin, int end); void ui_textarea_insert(UiText *text, int pos, char *str); +int ui_textarea_position(UiText *text); +void ui_textarea_selection(UiText *text, int *begin, int *end); diff -r 78ae3efe463f -r a137277f9173 ui/cocoa/text.m --- a/ui/cocoa/text.m Sat Apr 05 13:02:37 2014 +0200 +++ b/ui/cocoa/text.m Sat Apr 05 15:53:41 2014 +0200 @@ -91,6 +91,8 @@ value->set = ui_textarea_set; value->getsubstr = ui_textarea_getsubstr; value->insert = ui_textarea_insert; + value->position = ui_textarea_position; + value->selection = ui_textarea_selection; value->value = NULL; value->obj = textview; @@ -154,9 +156,6 @@ if(text->value) { free(text->value); } - if(text->value) { - free(text->value); - } NSTextView *textview = (NSTextView*)text->obj; NSString *s = [[NSString alloc]initWithUTF8String:str]; NSAttributedString *as = [[NSAttributedString alloc]initWithString:s]; @@ -164,6 +163,16 @@ text->value = NULL; } +int ui_textarea_position(UiText *text) { + return [[[(NSTextView*)text->obj selectedRanges] objectAtIndex:0] rangeValue].location; +} + +void ui_textarea_selection(UiText *text, int *begin, int *end) { + NSRange range = [[[(NSTextView*)text->obj selectedRanges] objectAtIndex:0] rangeValue]; + *begin = range.location; + *end = range.location + range.length; +} + void ui_text_undo(UiText *text) { [(NSUndoManager*)text->undomgr undo]; } diff -r 78ae3efe463f -r a137277f9173 ui/cocoa/toolkit.m --- a/ui/cocoa/toolkit.m Sat Apr 05 13:02:37 2014 +0200 +++ b/ui/cocoa/toolkit.m Sat Apr 05 15:53:41 2014 +0200 @@ -86,6 +86,34 @@ } +void ui_clipboard_set(char *str) { + NSString *string = [[NSString alloc] initWithUTF8String:str]; + NSPasteboard * pasteBoard = [NSPasteboard generalPasteboard]; + [pasteBoard declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil]; + [pasteBoard setString:string forType:NSStringPboardType]; +} + +char* ui_clipboard_get() { + NSPasteboard * pasteBoard = [NSPasteboard generalPasteboard]; + NSArray *classes = [[NSArray alloc] initWithObjects:[NSString class], nil]; + NSDictionary *options = [NSDictionary dictionary]; + NSArray *data = [pasteBoard readObjectsForClasses:classes options:options]; + + if(data != nil) { + NSString *str = [data componentsJoinedByString: @""]; + + // copy C string + size_t length = [str lengthOfBytesUsingEncoding:NSUTF8StringEncoding]; + const char *cstr = [str UTF8String]; + char *value = malloc(length + 1); + memcpy(value, cstr, length); + value[length] = '\0'; + + return value; + } else { + return NULL; + } +} @implementation EventWrapper diff -r 78ae3efe463f -r a137277f9173 ui/ui/toolkit.h --- a/ui/ui/toolkit.h Sat Apr 05 13:02:37 2014 +0200 +++ b/ui/ui/toolkit.h Sat Apr 05 15:53:41 2014 +0200 @@ -140,6 +140,8 @@ char* (*get)(UiText*); char* (*getsubstr)(UiText*, int, int); // text, begin, end void (*insert)(UiText*, int, char*); + int (*position)(UiText*); + void (*selection)(UiText*, int*, int*); // text, begin, end char *value; void *obj; void *undomgr; @@ -218,6 +220,8 @@ void ui_list_addobsv(UiList *list, ui_callback f, void *data); void ui_list_notify(UiList *list); +void ui_clipboard_set(char *str); +char* ui_clipboard_get(); #ifdef __cplusplus }