Sun, 11 May 2025 18:56:49 +0200
add textfield (Cocoa)
--- a/make/xcode/toolkit/toolkit/main.m Tue May 06 15:24:39 2025 +0200 +++ b/make/xcode/toolkit/toolkit/main.m Sun May 11 18:56:49 2025 +0200 @@ -44,6 +44,7 @@ ui_vbox(obj, .spacing = 0, .fill = UI_OFF) { + ui_textfield(obj, .varname = "textfield1"); ui_radiobutton(obj, .label = "V Button 1", .varname = "radio1"); ui_radiobutton(obj, .label = "V Button 2", .varname = "radio1"); ui_radiobutton(obj, .label = "V Button 3", .varname = "radio1");
--- a/ui/cocoa/text.h Tue May 06 15:24:39 2025 +0200 +++ b/ui/cocoa/text.h Sun May 11 18:56:49 2025 +0200 @@ -44,3 +44,6 @@ void ui_textarea_selection(UiText *text, int *begin, int *end); int ui_textarea_length(UiText *text); void ui_textarea_remove(UiText *text, int begin, int end); + +char* ui_textfield_get(UiString *s); +void ui_textfield_set(UiString *s, const char *value);
--- a/ui/cocoa/text.m Tue May 06 15:24:39 2025 +0200 +++ b/ui/cocoa/text.m Sun May 11 18:56:49 2025 +0200 @@ -132,3 +132,73 @@ void ui_textarea_remove(UiText *text, int begin, int end) { } + + + +/* -------------------------- TextField -------------------------- */ + +static UIWIDGET textfield_create(UiObject *obj, UiTextFieldArgs args, BOOL password, BOOL frameless) { + NSTextField *textfield; + if(password) { + textfield = [[NSSecureTextField alloc] init]; + } else { + textfield = [[NSTextField alloc] init]; + } + + if(frameless) { + [textfield setBezeled: NO]; + } + + UiLayout layout = UI_INIT_LAYOUT(args); + ui_container_add(obj, textfield, &layout, FALSE); + + UiVar* var = uic_widget_var(obj->ctx, obj->ctx, args.value, args.varname, UI_VAR_STRING); + if(var) { + UiString *s = var->value; + if(s->value.ptr) { + textfield.stringValue = [[NSString alloc] initWithUTF8String:s->value.ptr]; + if(s->value.free) { + s->value.free(s->value.ptr); + } + } + s->obj = (__bridge void*)textfield; + s->get = ui_textfield_get; + s->set = ui_textfield_set; + } + + return (__bridge void*)textfield; +} + +UIWIDGET ui_textfield_create(UiObject *obj, UiTextFieldArgs args) { + return textfield_create(obj, args, FALSE, FALSE); +} + +UIWIDGET ui_frameless_textfield_create(UiObject* obj, UiTextFieldArgs args) { + return textfield_create(obj, args, FALSE, TRUE); +} + +UIWIDGET ui_passwordfield_create(UiObject* obj, UiTextFieldArgs args) { + return textfield_create(obj, args, TRUE, FALSE); +} + +char* ui_textfield_get(UiString *s) { + NSTextField *textfield = (__bridge NSTextField*)s->obj; + NSString *str = textfield.stringValue; + const char *cstr = str.UTF8String; + if(s->value.free) { + s->value.free(s->value.ptr); + } + s->value.ptr = strdup(cstr); + s->value.free = free; + return s->value.ptr; +} + +void ui_textfield_set(UiString *s, const char *value) { + if(s->value.free) { + s->value.free(s->value.ptr); + } + s->value.ptr = NULL; + s->value.free = NULL; + NSTextField *textfield = (__bridge NSTextField*)s->obj; + textfield.stringValue = [[NSString alloc] initWithUTF8String:value]; +}