diff --git a/redo/controls_darwin.go b/redo/controls_darwin.go index 02b2d64..a4e6c29 100644 --- a/redo/controls_darwin.go +++ b/redo/controls_darwin.go @@ -96,3 +96,29 @@ func (c *checkbox) Checked() bool { func (c *checkbox) SetChecked(checked bool) { C.checkboxSetChecked(c.id, toBOOL(checked)) } + +type textField struct { + *widgetbase +} + +func newTextField() *textField { + return &textField{ + widgetbase: newWidget(C.newTextField()), + } +} + +func newPasswordField() *textField { + return &textField{ + widgetbase: newWidget(C.newPasswordField()), + } +} + +func (t *textField) Text() string { + return C.GoString(C.textFieldText(t.id)) +} + +func (t *textField) SetText(text string) { + ctext := C.CString(text) + defer C.free(unsafe.Pointer(ctext)) + C.textFieldSetText(t.id, ctext) +} diff --git a/redo/controls_darwin.m b/redo/controls_darwin.m index ecb4abd..198e363 100644 --- a/redo/controls_darwin.m +++ b/redo/controls_darwin.m @@ -8,6 +8,7 @@ #define toNSWindow(x) ((NSWindow *) (x)) #define toNSControl(x) ((NSControl *) (x)) #define toNSButton(x) ((NSButton *) (x)) +#define toNSTextField(x) ((NSTextField *) (x)) void parent(id control, id parentid) { @@ -101,3 +102,42 @@ void checkboxSetChecked(id c, BOOL checked) state = NSOffState; [toNSButton(c) setState:state]; } + +static id finishNewTextField(NSTextField *t) +{ + // TODO font + // TODO smart quotes + // Interface Builder does this to make the text box behave properly + // this disables both word wrap AND ellipsizing in one fell swoop + // however, we need to send it to the control's cell, not to the control directly + [[t cell] setLineBreakMode:NSLineBreakByClipping]; + // Interface Builder also sets this to allow horizontal scrolling + [[t cell] setScrollable:YES]; + return (id) t; +} + +id newTextField(void) +{ + NSTextField *t; + + t = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 100, 100)]; + return finishNewTextField(t); +} + +id newPasswordField(void) +{ + NSSecureTextField *t; + + t = [[NSSecureTextField alloc] initWithFrame:NSMakeRect(0, 0, 100, 100)]; + return finishNewTextField(toNSTextField(t)); +} + +const char *textFieldText(id t) +{ + return [[toNSTextField(t) stringValue] UTF8String]; +} + +void textFieldSetText(id t, char *text) +{ + [toNSTextField(t) setStringValue:[NSString stringWithUTF8String:text]]; +} diff --git a/redo/objc_darwin.h b/redo/objc_darwin.h index 6795698..0daa468 100644 --- a/redo/objc_darwin.h +++ b/redo/objc_darwin.h @@ -41,6 +41,10 @@ extern void buttonSetText(id, char *); extern id newCheckbox(void); extern BOOL checkboxChecked(id); extern void checkboxSetChecked(id, BOOL); +extern id newTextField(void); +extern id newPasswordField(void); +extern const char *textFieldText(id); +extern void textFieldSetText(id, char *); /* sizing_darwin.m */ extern void moveControl(id, intptr_t, intptr_t, intptr_t, intptr_t);