Added button click events to the Mac OS X backend.

This commit is contained in:
Pietro Gagliardi 2014-07-17 12:02:39 -04:00
parent 659bc03f17
commit e4b379f84a
4 changed files with 49 additions and 3 deletions

View File

@ -47,6 +47,7 @@ func (w *widgetbase) parent(win *window) {
type button struct { type button struct {
*widgetbase *widgetbase
clicked *event
} }
func newButton(text string) *Request { func newButton(text string) *Request {
@ -55,17 +56,33 @@ func newButton(text string) *Request {
op: func() { op: func() {
ctext := C.CString(text) ctext := C.CString(text)
defer C.free(unsafe.Pointer(ctext)) defer C.free(unsafe.Pointer(ctext))
c <- &button{ b := &button{
widgetbase: newWidget(C.newButton(ctext)), widgetbase: newWidget(C.newButton(ctext)),
clicked: newEvent(),
} }
C.buttonSetDelegate(b.id, unsafe.Pointer(b))
c <- b
}, },
resp: c, resp: c,
} }
} }
func (b *button) OnClicked(e func(c Doer)) *Request { func (b *button) OnClicked(e func(c Doer)) *Request {
// TODO c := make(chan interface{})
return nil return &Request{
op: func() {
b.clicked.set(e)
c <- struct{}{}
},
resp: c,
}
}
//export buttonClicked
func buttonClicked(xb unsafe.Pointer) {
b := (*button)(unsafe.Pointer(xb))
b.clicked.fire()
println("button clicked")
} }
func (b *button) Text() *Request { func (b *button) Text() *Request {

View File

@ -22,6 +22,21 @@ void parent(id control, id parentid, BOOL floating)
[toNSView(control) release]; [toNSView(control) release];
} }
@interface goControlDelegate : NSObject {
@public
void *gocontrol;
}
@end
@implementation goControlDelegate
- (IBAction)buttonClicked:(id)sender
{
buttonClicked(self->gocontrol);
}
@end
id newButton(char *text) id newButton(char *text)
{ {
NSButton *b; NSButton *b;
@ -36,6 +51,16 @@ id newButton(char *text)
return b; return b;
} }
void buttonSetDelegate(id button, void *b)
{
goControlDelegate *d;
d = [goControlDelegate new];
d->gocontrol = b;
[toNSButton(button) setTarget:d];
[toNSButton(button) setAction:@selector(buttonClicked:)];
}
const char *buttonText(id button) const char *buttonText(id button)
{ {
return [[toNSButton(button) title] UTF8String]; return [[toNSButton(button) title] UTF8String];

View File

@ -30,6 +30,7 @@ extern void windowClose(id);
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(char *);
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 *);

View File

@ -20,6 +20,9 @@ func init() {
done <- struct{}{} done <- struct{}{}
return true return true
})) }))
Wait(Do, b.OnClicked(func(c Doer) {
println("in OnClicked()")
}))
Wait(Do, w.Show()) Wait(Do, w.Show())
<-done <-done
}() }()