diff options
| -rw-r--r-- | newctrl/window_darwin.go | 90 | ||||
| -rw-r--r-- | newctrl/window_darwin.m | 91 |
2 files changed, 181 insertions, 0 deletions
diff --git a/newctrl/window_darwin.go b/newctrl/window_darwin.go new file mode 100644 index 0000000..4758ac5 --- /dev/null +++ b/newctrl/window_darwin.go @@ -0,0 +1,90 @@ +// 8 july 2014 + +package ui + +import ( + "unsafe" +) + +// #include "objc_darwin.h" +import "C" + +type window struct { + id C.id + + closing *event + + child Control + container *container + + margined bool +} + +func newWindow(title string, width int, height int, control Control) *window { + id := C.newWindow(C.intptr_t(width), C.intptr_t(height)) + ctitle := C.CString(title) + defer C.free(unsafe.Pointer(ctitle)) + C.windowSetTitle(id, ctitle) + w := &window{ + id: id, + closing: newEvent(), + child: control, + container: newContainer(), + } + C.windowSetDelegate(w.id, unsafe.Pointer(w)) + C.windowSetContentView(w.id, w.container.id) + w.child.setParent(w.container.parent()) + return w +} + +func (w *window) Title() string { + return C.GoString(C.windowTitle(w.id)) +} + +func (w *window) SetTitle(title string) { + ctitle := C.CString(title) + defer C.free(unsafe.Pointer(ctitle)) + C.windowSetTitle(w.id, ctitle) +} + +func (w *window) Show() { + C.windowShow(w.id) +} + +func (w *window) Hide() { + C.windowHide(w.id) +} + +func (w *window) Close() { + C.windowClose(w.id) +} + +func (w *window) OnClosing(e func() bool) { + w.closing.setbool(e) +} + +func (w *window) Margined() bool { + return w.margined +} + +func (w *window) SetMargined(margined bool) { + w.margined = margined +} + +//export windowClosing +func windowClosing(xw unsafe.Pointer) C.BOOL { + w := (*window)(unsafe.Pointer(xw)) + close := w.closing.fire() + if close { + return C.YES + } + return C.NO +} + +//export windowResized +func windowResized(data unsafe.Pointer) { + w := (*window)(data) + a := w.container.allocation(w.margined) + d := w.beginResize() + w.child.resize(int(a.x), int(a.y), int(a.width), int(a.height), d) +} diff --git a/newctrl/window_darwin.m b/newctrl/window_darwin.m new file mode 100644 index 0000000..b156392 --- /dev/null +++ b/newctrl/window_darwin.m @@ -0,0 +1,91 @@ +// 8 july 2014 + +#import "objc_darwin.h" +#import "_cgo_export.h" +#import <Cocoa/Cocoa.h> + +#define toNSWindow(x) ((NSWindow *) (x)) +#define toNSView(x) ((NSView *) (x)) + +@interface goWindowDelegate : NSObject <NSWindowDelegate> { +@public + void *gowin; +} +@end + +@implementation goWindowDelegate + +- (BOOL)windowShouldClose:(id)win +{ + return windowClosing(self->gowin); +} + +- (void)windowDidResize:(NSNotification *)note +{ + windowResized(self->gowin); +} + +@end + +id newWindow(intptr_t width, intptr_t height) +{ + NSWindow *w; + NSTextView *tv; + + w = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, (CGFloat) width, (CGFloat) height) + styleMask:(NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSResizableWindowMask) + backing:NSBackingStoreBuffered + defer:YES]; + // we do not want substitutions + // text fields, labels, etc. take their smart quotes and other autocorrect settings from their parent window, which provides a shared "field editor" + // so we have to turn them off here + // thanks akempgen in irc.freenode.net/#macdev + // for some reason, this selector returns NSText but is documented to return NSTextView... + disableAutocorrect((id) [w fieldEditor:YES forObject:nil]); + return w; +} + +void windowSetDelegate(id win, void *w) +{ + goWindowDelegate *d; + + d = [goWindowDelegate new]; + d->gowin = w; + [toNSWindow(win) setDelegate:d]; +} + +void windowSetContentView(id win, id view) +{ + [toNSWindow(win) setContentView:toNSView(view)]; +} + +const char *windowTitle(id win) +{ + return [[toNSWindow(win) title] UTF8String]; +} + +void windowSetTitle(id win, const char * title) +{ + [toNSWindow(win) setTitle:[NSString stringWithUTF8String:title]]; +} + +void windowShow(id win) +{ + [toNSWindow(win) makeKeyAndOrderFront:toNSWindow(win)]; + // no need to worry about reshowing the window initially; that's handled by our container view (container_darwin.m) +} + +void windowHide(id win) +{ + [toNSWindow(win) orderOut:toNSWindow(win)]; +} + +void windowClose(id win) +{ + [toNSWindow(win) close]; +} + +id windowContentView(id win) +{ + return (id) [toNSWindow(win) contentView]; +} |
