2014-07-08 15:47:28 -05:00
|
|
|
// 8 july 2014
|
|
|
|
|
|
|
|
#import "objc_darwin.h"
|
|
|
|
#import "_cgo_export.h"
|
|
|
|
#import <Cocoa/Cocoa.h>
|
|
|
|
|
|
|
|
#define toNSWindow(x) ((NSWindow *) (x))
|
2014-08-04 16:28:53 -05:00
|
|
|
#define toNSView(x) ((NSView *) (x))
|
2014-07-08 15:47:28 -05:00
|
|
|
|
2014-07-08 16:43:50 -05:00
|
|
|
@interface goWindowDelegate : NSObject <NSWindowDelegate> {
|
|
|
|
@public
|
|
|
|
void *gowin;
|
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation goWindowDelegate
|
|
|
|
|
|
|
|
- (BOOL)windowShouldClose:(id)win
|
|
|
|
{
|
|
|
|
return windowClosing(self->gowin);
|
|
|
|
}
|
|
|
|
|
2014-08-04 16:30:18 -05:00
|
|
|
// TODO there has to be a better way
|
2014-07-18 14:16:41 -05:00
|
|
|
- (void)doWindowResize:(id)win
|
2014-07-15 13:04:20 -05:00
|
|
|
{
|
|
|
|
NSWindow *w;
|
|
|
|
NSRect r;
|
|
|
|
|
2014-07-18 14:16:41 -05:00
|
|
|
w = toNSWindow(win);
|
2014-07-15 13:04:20 -05:00
|
|
|
r = [[w contentView] frame];
|
2014-08-04 16:30:18 -05:00
|
|
|
[[w contentView] setFrameSize:r.size];
|
2014-07-15 13:04:20 -05:00
|
|
|
}
|
|
|
|
|
2014-07-08 16:43:50 -05:00
|
|
|
@end
|
|
|
|
|
2014-07-08 15:47:28 -05:00
|
|
|
id newWindow(intptr_t width, intptr_t height)
|
|
|
|
{
|
2014-07-26 10:18:34 -05:00
|
|
|
NSWindow *w;
|
|
|
|
NSTextView *tv;
|
|
|
|
|
|
|
|
w = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, (CGFloat) width, (CGFloat) height)
|
2014-07-08 15:47:28 -05:00
|
|
|
styleMask:(NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSResizableWindowMask)
|
|
|
|
backing:NSBackingStoreBuffered
|
|
|
|
defer:YES];
|
2014-07-26 10:18:34 -05:00
|
|
|
// 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...
|
|
|
|
// TODO isolate into its own function when (if?) we add TextArea
|
|
|
|
tv = (NSTextView *) [w fieldEditor:YES forObject:nil];
|
|
|
|
[tv setEnabledTextCheckingTypes:0];
|
|
|
|
[tv setAutomaticDashSubstitutionEnabled:NO];
|
|
|
|
// don't worry about automatic data detection; it won't change stringValue (thanks pretty_function in irc.freenode.net/#macdev)
|
|
|
|
[tv setAutomaticSpellingCorrectionEnabled:NO];
|
|
|
|
[tv setAutomaticTextReplacementEnabled:NO];
|
|
|
|
return w;
|
2014-07-08 15:47:28 -05:00
|
|
|
}
|
|
|
|
|
2014-07-08 16:43:50 -05:00
|
|
|
void windowSetDelegate(id win, void *w)
|
2014-07-08 15:47:28 -05:00
|
|
|
{
|
2014-07-08 16:43:50 -05:00
|
|
|
goWindowDelegate *d;
|
|
|
|
|
|
|
|
d = [goWindowDelegate new];
|
|
|
|
d->gowin = w;
|
|
|
|
[toNSWindow(win) setDelegate:d];
|
2014-07-08 15:47:28 -05:00
|
|
|
}
|
|
|
|
|
2014-08-04 16:28:53 -05:00
|
|
|
void windowSetContentView(id win, id view)
|
|
|
|
{
|
|
|
|
[toNSWindow(win) setContentView:toNSView(view)];
|
|
|
|
}
|
|
|
|
|
2014-07-08 15:47:28 -05:00
|
|
|
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)];
|
2014-07-18 14:16:41 -05:00
|
|
|
// calling the above the first time won't emit a size changed event (unlike on Windows and GTK+), so fake one to get the controls laid out properly
|
2014-07-22 16:45:29 -05:00
|
|
|
windowRedraw(win);
|
2014-07-08 15:47:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void windowHide(id win)
|
|
|
|
{
|
|
|
|
[toNSWindow(win) orderOut:toNSWindow(win)];
|
|
|
|
}
|
|
|
|
|
|
|
|
void windowClose(id win)
|
|
|
|
{
|
2014-07-20 12:01:46 -05:00
|
|
|
[toNSWindow(win) close];
|
2014-07-08 15:47:28 -05:00
|
|
|
}
|
2014-07-22 16:45:29 -05:00
|
|
|
|
2014-07-25 22:11:41 -05:00
|
|
|
id windowContentView(id win)
|
|
|
|
{
|
|
|
|
return (id) [toNSWindow(win) contentView];
|
|
|
|
}
|
|
|
|
|
2014-07-22 16:45:29 -05:00
|
|
|
// fake a resize event under certain conditions; see each invocation for details
|
|
|
|
void windowRedraw(id win)
|
|
|
|
{
|
|
|
|
goWindowDelegate *d;
|
|
|
|
|
|
|
|
d = [toNSWindow(win) delegate];
|
|
|
|
[d doWindowResize:win];
|
2014-07-23 16:09:08 -05:00
|
|
|
// TODO new control sizes don't take effect properly, even with [toNSWindow(win) display];
|
|
|
|
}
|