2014-03-01 15:56:22 -06:00
|
|
|
// 27 february 2014
|
2014-03-12 20:55:45 -05:00
|
|
|
|
2014-03-01 15:56:22 -06:00
|
|
|
package ui
|
|
|
|
|
|
|
|
import (
|
2014-04-04 20:32:10 -05:00
|
|
|
// ...
|
2014-03-01 15:56:22 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
/*
|
|
|
|
This creates a class goAppDelegate that will be used as the delegate for /everything/. Specifically, it:
|
|
|
|
- runs uitask requests (uitask:)
|
|
|
|
- handles window close events (windowShouldClose:)
|
2014-03-11 10:54:32 -05:00
|
|
|
- handles window resize events (windowDidResize:)
|
2014-03-01 20:34:37 -06:00
|
|
|
- handles button click events (buttonClicked:)
|
2014-03-01 15:56:22 -06:00
|
|
|
*/
|
|
|
|
|
|
|
|
// #cgo LDFLAGS: -lobjc -framework Foundation -framework AppKit
|
|
|
|
// #include <stdlib.h>
|
|
|
|
// #include "objc_darwin.h"
|
|
|
|
// extern void appDelegate_uitask(id, SEL, id); /* from uitask_darwin.go */
|
|
|
|
// extern BOOL appDelegate_windowShouldClose(id, SEL, id);
|
2014-03-01 17:01:30 -06:00
|
|
|
// extern void appDelegate_windowDidResize(id, SEL, id);
|
2014-03-01 20:34:37 -06:00
|
|
|
// extern void appDelegate_buttonClicked(id, SEL, id);
|
2014-03-01 15:56:22 -06:00
|
|
|
import "C"
|
|
|
|
|
|
|
|
var (
|
|
|
|
appDelegate C.id
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
_goAppDelegate = "goAppDelegate"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2014-04-04 20:06:01 -05:00
|
|
|
_uitask = sel_getUid("uitask:") // used by uitask_darwin.go
|
|
|
|
_buttonClicked = sel_getUid("buttonClicked:") // used by sysdata_darwin.go
|
2014-03-01 15:56:22 -06:00
|
|
|
)
|
|
|
|
|
2014-04-04 20:06:01 -05:00
|
|
|
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"},
|
|
|
|
}
|
|
|
|
|
2014-03-01 15:56:22 -06:00
|
|
|
func mkAppDelegate() error {
|
2014-04-04 20:32:10 -05:00
|
|
|
id, _, err := makeClass(_goAppDelegate, _NSObject, appDelegateSels,
|
2014-04-04 20:06:01 -05:00
|
|
|
"application delegate (handles events)")
|
2014-03-01 15:56:22 -06:00
|
|
|
if err != nil {
|
2014-04-04 20:06:01 -05:00
|
|
|
return err
|
2014-03-01 20:34:37 -06:00
|
|
|
}
|
2014-04-04 20:32:10 -05:00
|
|
|
appDelegate = C.objc_msgSend_noargs(id, _new)
|
2014-03-01 15:56:22 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//export appDelegate_windowShouldClose
|
|
|
|
func appDelegate_windowShouldClose(self C.id, sel C.SEL, win C.id) C.BOOL {
|
|
|
|
sysData := getSysData(win)
|
|
|
|
sysData.signal()
|
|
|
|
return C.BOOL(C.NO) // don't close
|
|
|
|
}
|
|
|
|
|
2014-03-01 17:01:30 -06:00
|
|
|
var (
|
|
|
|
_object = sel_getUid("object")
|
2014-03-02 18:13:26 -06:00
|
|
|
_display = sel_getUid("display")
|
2014-03-01 17:01:30 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
//export appDelegate_windowDidResize
|
|
|
|
func appDelegate_windowDidResize(self C.id, sel C.SEL, notification C.id) {
|
|
|
|
win := C.objc_msgSend_noargs(notification, _object)
|
2014-03-29 22:57:49 -05:00
|
|
|
s := getSysData(win)
|
2014-03-02 18:16:36 -06:00
|
|
|
wincv := C.objc_msgSend_noargs(win, _contentView) // we want the content view's size, not the window's; selector defined in sysdata_darwin.go
|
|
|
|
r := C.objc_msgSend_stret_rect_noargs(wincv, _frame)
|
2014-03-29 22:57:49 -05:00
|
|
|
if s.resize != nil {
|
2014-03-03 16:44:03 -06:00
|
|
|
// winheight is used here because (0,0) is the bottom-left corner, not the top-left corner
|
2014-03-17 20:09:03 -05:00
|
|
|
s.resizes = s.resizes[0:0] // set len to 0 without changing cap
|
2014-03-29 22:57:49 -05:00
|
|
|
s.resize(0, 0, int(r.width), int(r.height), &s.resizes)
|
2014-03-17 20:09:03 -05:00
|
|
|
for _, s := range s.resizes {
|
2014-03-17 19:42:36 -05:00
|
|
|
err := s.sysData.setRect(s.x, s.y, s.width, s.height, int(r.height))
|
|
|
|
if err != nil {
|
|
|
|
panic("child resize failed: " + err.Error())
|
|
|
|
}
|
2014-03-01 17:01:30 -06:00
|
|
|
}
|
|
|
|
}
|
2014-03-02 18:13:26 -06:00
|
|
|
C.objc_msgSend_noargs(win, _display) // redraw everything; TODO only if resize() was called?
|
2014-03-01 17:01:30 -06:00
|
|
|
}
|
|
|
|
|
2014-03-01 20:34:37 -06:00
|
|
|
//export appDelegate_buttonClicked
|
|
|
|
func appDelegate_buttonClicked(self C.id, sel C.SEL, button C.id) {
|
|
|
|
sysData := getSysData(button)
|
|
|
|
sysData.signal()
|
|
|
|
}
|