diff --git a/ui/internal/cocoa/textview.go b/ui/internal/cocoa/textview.go index 880d415..e496b48 100644 --- a/ui/internal/cocoa/textview.go +++ b/ui/internal/cocoa/textview.go @@ -9,7 +9,7 @@ import "unsafe" // TextView - represents a textView control that can trigger actions. type TextView struct { ptr C.TextViewPtr - callback func() + callback func(string) } var textviews []*TextView @@ -26,6 +26,11 @@ func NewTextView(x int, y int, width int, height int) *TextView { return tv } +// GetText gets the text of the text view +func (textview *TextView) GetText() string { + return C.GoString(C.TextView_Text(textview.ptr)) +} + // SetText sets the text of the text view func (textview *TextView) SetText(text string) { cText := C.CString(text) @@ -50,3 +55,16 @@ func (c *TextView) SetEditable(editable bool) { C.TextView_SetEditable(c.ptr, 0) } } + +func (c *TextView) OnChange(fn func(value string)) { + c.callback = fn +} + +//export onTextViewDidChange +func onTextViewDidChange(id C.int) { + textViewId := int(id) + if textViewId < len(textviews) && textviews[textViewId].callback != nil { + tf := textviews[textViewId] + tf.callback(tf.GetText()) + } +} diff --git a/ui/internal/cocoa/textview.h b/ui/internal/cocoa/textview.h index 41eec67..e94cadf 100644 --- a/ui/internal/cocoa/textview.h +++ b/ui/internal/cocoa/textview.h @@ -11,6 +11,7 @@ typedef void *TextViewPtr; TextViewPtr TextView_New(int goTextViewId, int x, int y, int w, int h); +const char *TextView_Text(TextViewPtr textViewPtr); void TextView_SetText(TextViewPtr textViewPtr, const char *text); void TextView_Remove(TextViewPtr textViewPtr); void TextView_SetFontSize(TextViewPtr textViewPtr, int size); diff --git a/ui/internal/cocoa/textview.m b/ui/internal/cocoa/textview.m index beb1ccf..ad1bae1 100644 --- a/ui/internal/cocoa/textview.m +++ b/ui/internal/cocoa/textview.m @@ -1,5 +1,6 @@ #import "textview.h" #include "_cgo_export.h" +#import "textviewdelegate.h" @implementation TextViewHandler @end @@ -9,6 +10,10 @@ TextViewPtr TextView_New(int goTextViewId, int x, int y, int w, int h) { NSTextView *textView = [[[NSTextView alloc] initWithFrame:NSMakeRect(x, y, w, h)] autorelease]; + id d = [[TextViewDelegate alloc] init]; + [d setGoTextViewId:goTextViewId]; + [textView setDelegate:d]; + NSScrollView *scrollView = [[[NSScrollView alloc] initWithFrame:NSMakeRect(x, y, w, h)] autorelease]; [scrollView setDocumentView:textView]; @@ -20,6 +25,12 @@ TextViewPtr TextView_New(int goTextViewId, int x, int y, int w, int h) { return (TextViewPtr)scrollView; } +const char *TextView_Text(TextViewPtr ptr) { + NSTextView *c = ((NSScrollView *)ptr).documentView; + return + [[[c textStorage] string] cStringUsingEncoding:NSISOLatin1StringEncoding]; +} + void TextView_SetText(TextViewPtr ptr, const char *text) { NSTextView *c = ((NSScrollView *)ptr).documentView; [c setString:[NSString stringWithUTF8String:text]]; diff --git a/ui/internal/cocoa/textviewdelegate.h b/ui/internal/cocoa/textviewdelegate.h new file mode 100644 index 0000000..8338d64 --- /dev/null +++ b/ui/internal/cocoa/textviewdelegate.h @@ -0,0 +1,7 @@ +#import + +@interface TextViewDelegate : NSObject + +@property(assign) int goTextViewId; + +@end diff --git a/ui/internal/cocoa/textviewdelegate.m b/ui/internal/cocoa/textviewdelegate.m new file mode 100644 index 0000000..861f15a --- /dev/null +++ b/ui/internal/cocoa/textviewdelegate.m @@ -0,0 +1,17 @@ +#import "textviewdelegate.h" +#include "_cgo_export.h" + +@implementation TextViewDelegate + +- (void)dealloc { + [super dealloc]; +} + +- (void)textDidChange:(NSNotification *)aNotification { + // NSTextField *textField = [aNotification object]; + // const char *text = [[textField stringValue] + // cStringUsingEncoding:NSUTF8StringEncoding]; + onTextViewDidChange([self goTextViewId]); +} + +@end diff --git a/ui/texteditor_cocoa.go b/ui/texteditor_cocoa.go index dc242f2..2411f71 100644 --- a/ui/texteditor_cocoa.go +++ b/ui/texteditor_cocoa.go @@ -25,6 +25,8 @@ func (w *TextEditor) Update(nextComponent spot.Control) bool { w.ref.SetFontSize(w.FontSize) } + w.ref.OnChange(next.OnChange) + return true } @@ -41,6 +43,7 @@ func (w *TextEditor) Mount(parent spot.Control) any { if w.FontSize > 0 { w.ref.SetFontSize(w.FontSize) } + w.ref.OnChange(w.OnChange) if window, ok := parent.(*Window); ok && window != nil && window.ref != nil { window.ref.AddTextView(w.ref)