Started conversion of delegate_darwin.go to use Objective-C directly.

This commit is contained in:
Pietro Gagliardi 2014-05-13 07:56:37 -04:00
parent 791345fa97
commit de97125c54
3 changed files with 62 additions and 32 deletions

View File

@ -18,10 +18,9 @@ This creates a class goAppDelegate that will be used as the delegate for /everyt
// #cgo LDFLAGS: -lobjc -framework Foundation -framework AppKit // #cgo LDFLAGS: -lobjc -framework Foundation -framework AppKit
// #include <stdlib.h> // #include <stdlib.h>
// #include "objc_darwin.h" // #include "objc_darwin.h"
// extern void appDelegate_uitask(id, SEL, id); /* from uitask_darwin.go */ // /* TODO this goes in objc_darwin.h once I take care of everything else */
// extern BOOL appDelegate_windowShouldClose(id, SEL, id); // extern id makeAppDelegate(void);
// extern void appDelegate_windowDidResize(id, SEL, id); // extern id windowGetContentView(id);
// extern void appDelegate_buttonClicked(id, SEL, id);
import "C" import "C"
var ( var (
@ -37,34 +36,15 @@ var (
_buttonClicked = sel_getUid("buttonClicked:") // used by sysdata_darwin.go _buttonClicked = sel_getUid("buttonClicked:") // used by sysdata_darwin.go
) )
var appDelegateSels = []selector{
selector{"uitask:", uintptr(C.appDelegate_uitask), sel_void_id,
"performing/dispatching UI events"},
selector{"windowShouldClose:", uintptr(C.appDelegate_windowShouldClose), sel_bool_id,
"handling window close button events"},
selector{"windowDidResize:", uintptr(C.appDelegate_windowDidResize), sel_void_id,
"handling window resize events"},
selector{"buttonClicked:", uintptr(C.appDelegate_buttonClicked), sel_bool_id,
"handling button clicks"},
selector{"applicationShouldTerminate:", uintptr(C._appDelegate_applicationShouldTerminate), sel_terminatereply_id,
"handling Quit menu items (such as from the Dock)/the AppQuit channel"},
}
func mkAppDelegate() error { func mkAppDelegate() error {
id, err := makeClass(_goAppDelegate, _NSObject, appDelegateSels, appDelegate = C.makeAppDelegate()
"application delegate (handles events)")
if err != nil {
return err
}
appDelegate = C.objc_msgSend_noargs(id, _new)
return nil return nil
} }
//export appDelegate_windowShouldClose //export appDelegate_windowShouldClose
func appDelegate_windowShouldClose(self C.id, sel C.SEL, win C.id) C.BOOL { func appDelegate_windowShouldClose(win C.id) {
sysData := getSysData(win) sysData := getSysData(win)
sysData.signal() sysData.signal()
return C.BOOL(C.NO) // don't close
} }
var ( var (
@ -73,10 +53,9 @@ var (
) )
//export appDelegate_windowDidResize //export appDelegate_windowDidResize
func appDelegate_windowDidResize(self C.id, sel C.SEL, notification C.id) { func appDelegate_windowDidResize(win C.id) {
win := C.objc_msgSend_noargs(notification, _object)
s := getSysData(win) s := getSysData(win)
wincv := C.objc_msgSend_noargs(win, _contentView) // we want the content view's size, not the window's; selector defined in sysdata_darwin.go wincv := C.windowGetContentView(win) // we want the content view's size, not the window's
r := C.objc_msgSend_stret_rect_noargs(wincv, _frame) r := C.objc_msgSend_stret_rect_noargs(wincv, _frame)
// winheight is used here because (0,0) is the bottom-left corner, not the top-left corner // winheight is used here because (0,0) is the bottom-left corner, not the top-left corner
s.doResize(0, 0, int(r.width), int(r.height), int(r.height)) s.doResize(0, 0, int(r.width), int(r.height), int(r.height))
@ -84,7 +63,7 @@ func appDelegate_windowDidResize(self C.id, sel C.SEL, notification C.id) {
} }
//export appDelegate_buttonClicked //export appDelegate_buttonClicked
func appDelegate_buttonClicked(self C.id, sel C.SEL, button C.id) { func appDelegate_buttonClicked(button C.id) {
sysData := getSysData(button) sysData := getSysData(button)
sysData.signal() sysData.signal()
} }
@ -95,5 +74,4 @@ func appDelegate_applicationShouldTerminate() {
go func() { go func() {
AppQuit <- struct{}{} AppQuit <- struct{}{}
}() }()
// xxx in bleh_darwin.m tells Cocoa not to quit
} }

53
delegate_darwin.m Normal file
View File

@ -0,0 +1,53 @@
// 13 may 2014
#include "objc_darwin.h"
#include "_cgo_export.h"
#include <Foundation/NSObject.h>
#include <Foundation/NSValue.h>
#include <Foundation/NSNotification.h>
#include <AppKit/NSApplication.h>
#include <AppKit/NSWindow.h>
@interface appDelegate : NSObject
@end
@implementation appDelegate
- (void)uitask:(NSValue *)fp
{
appDelegate_uitask([fp pointerValue]);
}
- (BOOL)windowShouldClose:(id)win
{
appDelegate_windowShouldClose(win);
return NO; // don't close
}
- (void)windowDidResize:(NSNotification *)n
{
appDelegate_windowDidResize([n object]);
}
- (void)buttonClicked:(id)button
{
appDelegate_buttonClicked(button);
}
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)app
{
appDelegate_applicationShouldTerminate();
return NSTerminateCancel;
}
@end
id makeAppDelegate(void)
{
return [appDelegate new];
}
id windowGetContentView(id window)
{
return [((NSWindow *) window) contentView];
}

View File

@ -104,8 +104,7 @@ func initCocoa() (NSApp C.id, err error) {
} }
//export appDelegate_uitask //export appDelegate_uitask
func appDelegate_uitask(self C.id, sel C.SEL, arg C.id) { func appDelegate_uitask(p unsafe.Pointer) {
p := C.objc_msgSend_noargs(arg, _pointerValue)
f := (*func ())(unsafe.Pointer(p)) f := (*func ())(unsafe.Pointer(p))
(*f)() (*f)()
} }