implement textarea UiText functions (Cocoa)

Fri, 01 Aug 2025 22:17:27 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 01 Aug 2025 22:17:27 +0200
changeset 685
36e08a9aff8e
parent 684
3c9b430fb160
child 686
1e2f3a44d455

implement textarea UiText functions (Cocoa)

ui/cocoa/text.m file | annotate | diff | comparison | revisions
--- a/ui/cocoa/text.m	Wed Jul 30 19:51:39 2025 +0200
+++ b/ui/cocoa/text.m	Fri Aug 01 22:17:27 2025 +0200
@@ -94,23 +94,44 @@
 }
 
 void ui_textarea_set(UiText *text, const char *str) {
-    
+    NSTextView *textview = (__bridge NSTextView*)text->obj;
+    if(text->value.free) {
+        text->value.free(text->value.ptr);
+    }
+    text->value.ptr = strdup(str);
+    text->value.free = free;
+    textview.string = [[NSString alloc] initWithUTF8String:str];
 }
 
 char* ui_textarea_get(UiText *text) {
-    return NULL;
+    NSTextView *textview = (__bridge NSTextView*)text->obj;
+    if(text->value.free) {
+        text->value.free(text->value.ptr);
+    }
+    text->value.ptr = strdup(textview.string.UTF8String);
+    text->value.free = free;
+    return text->value.ptr;
 }
 
 char* ui_textarea_getsubstr(UiText *text, int begin, int end) {
-    return NULL;
+    NSTextView *textview = (__bridge NSTextView*)text->obj;
+    NSString *str = textview.string;
+    NSRange range = NSMakeRange(begin, end-begin);
+    NSString *sub = [str substringWithRange:range];
+    return strdup(sub.UTF8String);
 }
 
 void ui_textarea_insert(UiText *text, int pos, char *str) {
-    
+    NSTextView *textview = (__bridge NSTextView*)text->obj;
+    NSString *s = [[NSString alloc] initWithUTF8String:str];
+    NSAttributedString *attributedStr = [[NSAttributedString alloc] initWithString:s];
+    [textview.textStorage insertAttributedString:attributedStr atIndex:pos];
 }
 
 void ui_textarea_setposition(UiText *text, int pos) {
-    
+    NSTextView *textview = (__bridge NSTextView*)text->obj;
+    NSRange range = NSMakeRange(pos, 0);
+    [textview setSelectedRange:range];
 }
 
 int ui_textarea_position(UiText *text) {
@@ -118,7 +139,9 @@
 }
 
 void ui_textarea_setselection(UiText *text, int begin, int end) {
-    
+    NSTextView *textview = (__bridge NSTextView*)text->obj;
+    NSRange range = NSMakeRange(begin, end-begin);
+    [textview setSelectedRange:range];
 }
 
 void ui_textarea_selection(UiText *text, int *begin, int *end) {

mercurial