Implemented Checkbox on Mac OS X. Also untested; will test next.

This commit is contained in:
Pietro Gagliardi 2014-07-22 23:32:32 -04:00
parent f21fdfd7dc
commit 300835a1b4
4 changed files with 70 additions and 6 deletions

View File

@ -5,6 +5,13 @@ package ui
// #include "objc_darwin.h" // #include "objc_darwin.h"
import "C" import "C"
func fromBOOL(b C.BOOL) bool {
if b != C.NO {
return true
}
return false
}
func toBOOL(b bool) C.BOOL { func toBOOL(b bool) C.BOOL {
if b == true { if b == true {
return C.YES return C.YES

View File

@ -43,17 +43,22 @@ type button struct {
clicked *event clicked *event
} }
func newButton(text string) *button { func finishNewButton(id C.id, text string) *button {
ctext := C.CString(text) ctext := C.CString(text)
defer C.free(unsafe.Pointer(ctext)) defer C.free(unsafe.Pointer(ctext))
b := &button{ b := &button{
widgetbase: newWidget(C.newButton(ctext)), widgetbase: newWidget(id),
clicked: newEvent(), clicked: newEvent(),
} }
C.buttonSetText(b.id, ctext)
C.buttonSetDelegate(b.id, unsafe.Pointer(b)) C.buttonSetDelegate(b.id, unsafe.Pointer(b))
return b return b
} }
func newButton(text string) *button {
return finishNewButton(C.newButton(), text)
}
func (b *button) OnClicked(e func()) { func (b *button) OnClicked(e func()) {
b.clicked.set(e) b.clicked.set(e)
} }
@ -74,3 +79,24 @@ func (b *button) SetText(text string) {
defer C.free(unsafe.Pointer(ctext)) defer C.free(unsafe.Pointer(ctext))
C.buttonSetText(b.id, 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))
}

View File

@ -49,7 +49,7 @@ static inline void setStandardControlFont(id control)
@end @end
id newButton(char *text) id newButton(void)
{ {
NSButton *b; NSButton *b;
@ -57,11 +57,10 @@ id newButton(char *text)
b = [[NSButton alloc] initWithFrame:NSMakeRect(0, 0, 100, 100)]; b = [[NSButton alloc] initWithFrame:NSMakeRect(0, 0, 100, 100)];
// TODO verify all of these against Interface Builder // TODO verify all of these against Interface Builder
[b setButtonType:NSMomentaryLightButton]; [b setButtonType:NSMomentaryLightButton];
[b setTitle:[NSString stringWithUTF8String:text]];
[b setBordered:YES]; [b setBordered:YES];
[b setBezelStyle:NSRoundedBezelStyle]; [b setBezelStyle:NSRoundedBezelStyle];
setStandardControlFont(b); setStandardControlFont(b);
return b; return (id) b;
} }
void buttonSetDelegate(id button, void *b) void buttonSetDelegate(id button, void *b)
@ -83,3 +82,32 @@ void buttonSetText(id button, char *text)
{ {
[toNSButton(button) setTitle:[NSString stringWithUTF8String: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];
}

View File

@ -32,10 +32,13 @@ extern void windowClose(id);
/* controls_darwin.m */ /* controls_darwin.m */
extern void unparent(id); extern void unparent(id);
extern void parent(id, id, BOOL); extern void parent(id, id, BOOL);
extern id newButton(char *); extern id newButton(void);
extern void buttonSetDelegate(id, void *); extern void buttonSetDelegate(id, void *);
extern const char *buttonText(id); extern const char *buttonText(id);
extern void buttonSetText(id, char *); extern void buttonSetText(id, char *);
extern id newCheckbox(void);
extern BOOL checkboxChecked(id);
extern void checkboxSetChecked(id, BOOL);
/* sizing_darwin.m */ /* sizing_darwin.m */
extern void moveControl(id, intptr_t, intptr_t, intptr_t, intptr_t); extern void moveControl(id, intptr_t, intptr_t, intptr_t, intptr_t);