From bbb37bb2a6446e555dd92572e8e27157d1a7f7f4 Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Sat, 1 Mar 2014 21:34:37 -0500 Subject: [PATCH] Added Mac OS X Buttons. Code hangs due to a preferredSize() issue... --- delegate_darwin.go | 15 ++++++++++++++- sysdata_darwin.go | 23 +++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/delegate_darwin.go b/delegate_darwin.go index 8d27281..5c33e39 100644 --- a/delegate_darwin.go +++ b/delegate_darwin.go @@ -11,7 +11,7 @@ This creates a class goAppDelegate that will be used as the delegate for /everyt - runs uitask requests (uitask:) - handles window close events (windowShouldClose:) - handles window resize events (windowDidResize: (TODO also windowDidEndLiveResize:?)) - - handles button click events (buttonClick:) + - handles button click events (buttonClicked:) */ // #cgo LDFLAGS: -lobjc -framework Foundation -framework AppKit @@ -20,6 +20,7 @@ This creates a class goAppDelegate that will be used as the delegate for /everyt // extern void appDelegate_uitask(id, SEL, id); /* from uitask_darwin.go */ // extern BOOL appDelegate_windowShouldClose(id, SEL, id); // extern void appDelegate_windowDidResize(id, SEL, id); +// extern void appDelegate_buttonClicked(id, SEL, id); import "C" var ( @@ -34,6 +35,7 @@ var ( _uitask = sel_getUid("uitask:") _windowShouldClose = sel_getUid("windowShouldClose:") _windowDidResize = sel_getUid("windowDidResize:") + _buttonClicked = sel_getUid("buttonClicked:") ) func mkAppDelegate() error { @@ -56,6 +58,11 @@ func mkAppDelegate() error { if err != nil { return fmt.Errorf("error adding NSApplication delegate windowDidResize: method (to handle window resize events): %v", err) } + err = addDelegateMethod(appdelegateclass, _buttonClicked, + C.appDelegate_buttonClicked, delegate_void) + if err != nil { + return fmt.Errorf("error adding NSApplication delegate buttonClicked: method (to handle button clicks): %v", err) + } // TODO using objc_new() causes a segfault; find out why // TODO make alloc followed by init (I thought NSObject provided its own init?) appDelegate = objc_alloc(objc_getClass(_goAppDelegate)) @@ -86,6 +93,12 @@ func appDelegate_windowDidResize(self C.id, sel C.SEL, notification C.id) { } } +//export appDelegate_buttonClicked +func appDelegate_buttonClicked(self C.id, sel C.SEL, button C.id) { + sysData := getSysData(button) + sysData.signal() +} + // this actually constructs the delegate class var ( diff --git a/sysdata_darwin.go b/sysdata_darwin.go index 37352a3..1b99dce 100644 --- a/sysdata_darwin.go +++ b/sysdata_darwin.go @@ -27,8 +27,10 @@ type classData struct { var ( _NSWindow = objc_getClass("NSWindow") + _NSButton = objc_getClass("NSButton") _initWithContentRect = sel_getUid("initWithContentRect:styleMask:backing:defer:") + _initWithFrame = sel_getUid("initWithFrame:") _makeKeyAndOrderFront = sel_getUid("makeKeyAndOrderFront:") _orderOut = sel_getUid("orderOut:") _setHidden = sel_getUid("setHidden:") @@ -41,6 +43,11 @@ var ( // TODO others _frame = sel_getUid("frame") _setFrameDisplay = sel_getUid("setFrame:display:") + _setBezelStyle = sel_getUid("setBezelStyle:") + _setTarget = sel_getUid("setTarget:") + _setAction = sel_getUid("setAction:") + _contentView = sel_getUid("contentView") + _addSubview = sel_getUid("addSubview:") ) func controlShow(what C.id) { @@ -84,6 +91,22 @@ var classTypes = [nctypes]*classData{ textsel: _title, }, c_button: &classData{ + make: func(parentWindow C.id) C.id { + button := objc_alloc(_NSButton) + // NSControl requires that we specify a frame; dummy frame for now + button = objc_msgSend_rect(button, _initWithFrame, + 0, 0, 100, 100) + objc_msgSend_uint(button, _setBezelStyle, 1) // NSRoundedBezelStyle + C.objc_msgSend_id(button, _setTarget, appDelegate) + C.objc_msgSend_sel(button, _setAction, _buttonClicked) + windowView := C.objc_msgSend_noargs(parentWindow, _contentView) + C.objc_msgSend_id(windowView, _addSubview, button) + return button + }, + show: controlShow, + hide: controlHide, + settextsel: _setTitle, + textsel: _title, }, c_checkbox: &classData{ },