diff --git a/redo/common_darwin.go b/redo/common_darwin.go index 0c49a58..bf8c0c0 100644 --- a/redo/common_darwin.go +++ b/redo/common_darwin.go @@ -5,6 +5,13 @@ package ui // #include "objc_darwin.h" import "C" +func fromBOOL(b C.BOOL) bool { + if b != C.NO { + return true + } + return false +} + func toBOOL(b bool) C.BOOL { if b == true { return C.YES diff --git a/redo/controls_darwin.go b/redo/controls_darwin.go index 6d5edf8..9fef658 100644 --- a/redo/controls_darwin.go +++ b/redo/controls_darwin.go @@ -43,17 +43,22 @@ type button struct { clicked *event } -func newButton(text string) *button { +func finishNewButton(id C.id, text string) *button { ctext := C.CString(text) defer C.free(unsafe.Pointer(ctext)) b := &button{ - widgetbase: newWidget(C.newButton(ctext)), + widgetbase: newWidget(id), clicked: newEvent(), } + C.buttonSetText(b.id, ctext) C.buttonSetDelegate(b.id, unsafe.Pointer(b)) return b } +func newButton(text string) *button { + return finishNewButton(C.newButton(), text) +} + func (b *button) OnClicked(e func()) { b.clicked.set(e) } @@ -74,3 +79,24 @@ func (b *button) SetText(text string) { defer C.free(unsafe.Pointer(ctext)) C.buttonSetText(b.id, ctext) } + +type checkbox struct { + *button +} + +func newCheckbox(text string) *checkbox { + return &checkbox{ + button: finishNewButton(C.newCheckbox(), text), + } +} + +// we don't need to define our own event here; we can just reuse Button's +// (it's all target-action anyway) + +type (c *checkbox) Checked() bool { + return fromBOOL(C.checkboxChecked(c.id)) +} + +type (c *checkbox) SetChecked(checked bool) { + C.checkboxSetChecked(c.id, toBOOL(checked)) +} diff --git a/redo/controls_darwin.m b/redo/controls_darwin.m index 5233129..85de6c8 100644 --- a/redo/controls_darwin.m +++ b/redo/controls_darwin.m @@ -49,7 +49,7 @@ static inline void setStandardControlFont(id control) @end -id newButton(char *text) +id newButton(void) { NSButton *b; @@ -57,11 +57,10 @@ id newButton(char *text) b = [[NSButton alloc] initWithFrame:NSMakeRect(0, 0, 100, 100)]; // TODO verify all of these against Interface Builder [b setButtonType:NSMomentaryLightButton]; - [b setTitle:[NSString stringWithUTF8String:text]]; [b setBordered:YES]; [b setBezelStyle:NSRoundedBezelStyle]; setStandardControlFont(b); - return b; + return (id) b; } void buttonSetDelegate(id button, void *b) @@ -83,3 +82,32 @@ void buttonSetText(id button, char *text) { [toNSButton(button) setTitle:[NSString stringWithUTF8String:text]]; } + +id newCheckbox(void) +{ + NSButton *c; + + c = [[NSButton alloc] initWithFrame:NSMakeRect(0, 0, 100, 100)]; + // TODO verify all of these against Interface Builder + [c setButtonType:NSSwitchButton]; + [c setBordered:NO]; + setStandardControlFont(c); + return (id) c; +} + +BOOL checkboxChecked(id c) +{ + if ([toNSButton(c) state] == NSOnState) + return YES; + return NO; +} + +void checkboxSetChecked(id c, BOOL checked) +{ + NSInteger state; + + state = NSOnState; + if (checked == NO) + state = NSOffState; + [toNSButton(c) setState:state]; +} diff --git a/redo/objc_darwin.h b/redo/objc_darwin.h index a2cdc04..ec0abf0 100644 --- a/redo/objc_darwin.h +++ b/redo/objc_darwin.h @@ -32,10 +32,13 @@ extern void windowClose(id); /* controls_darwin.m */ extern void unparent(id); extern void parent(id, id, BOOL); -extern id newButton(char *); +extern id newButton(void); extern void buttonSetDelegate(id, void *); extern const char *buttonText(id); extern void buttonSetText(id, char *); +extern id newCheckbox(void); +extern BOOL checkboxChecked(id); +extern void checkboxSetChecked(id, BOOL); /* sizing_darwin.m */ extern void moveControl(id, intptr_t, intptr_t, intptr_t, intptr_t);