2015-08-16 21:30:44 -05:00
|
|
|
// 6 april 2015
|
|
|
|
#import "uipriv_darwin.h"
|
|
|
|
|
|
|
|
static BOOL canQuit = NO;
|
|
|
|
|
|
|
|
@implementation applicationClass
|
|
|
|
|
2016-01-09 14:05:19 -06:00
|
|
|
- (void)sendEvent:(NSEvent *)e
|
|
|
|
{
|
|
|
|
if (sendAreaEvents(e) != 0)
|
|
|
|
return;
|
|
|
|
[super sendEvent:e];
|
|
|
|
}
|
|
|
|
|
2016-04-14 13:01:33 -05:00
|
|
|
// NSColorPanel always sends changeColor: to the first responder regardless of whether there's a target set on it
|
|
|
|
// we can override it here (see colorbutton.m)
|
|
|
|
// thanks to mikeash in irc.freenode.net/#macdev for informing me this is how the first responder chain is initiated
|
|
|
|
// it turns out NSFontManager also sends changeFont: through this; let's inhibit that here too (see fontbutton.m)
|
|
|
|
- (BOOL)sendAction:(SEL)sel to:(id)to from:(id)from
|
|
|
|
{
|
2016-05-15 19:51:33 -05:00
|
|
|
if (colorButtonInhibitSendAction(sel, from, to))
|
|
|
|
return NO;
|
2016-04-14 13:01:33 -05:00
|
|
|
if (fontButtonInhibitSendAction(sel, from, to))
|
|
|
|
return NO;
|
|
|
|
return [super sendAction:sel to:to from:from];
|
|
|
|
}
|
|
|
|
|
|
|
|
// likewise, NSFontManager also sends NSFontPanelValidation messages to the first responder, however it does NOT use sendAction:from:to:!
|
|
|
|
// instead, it uses this one (thanks swillits in irc.freenode.net/#macdev)
|
|
|
|
// we also need to override it (see fontbutton.m)
|
|
|
|
- (id)targetForAction:(SEL)sel to:(id)to from:(id)from
|
|
|
|
{
|
|
|
|
id override;
|
|
|
|
|
|
|
|
if (fontButtonOverrideTargetForAction(sel, from, to, &override))
|
|
|
|
return override;
|
|
|
|
return [super targetForAction:sel to:to from:from];
|
|
|
|
}
|
|
|
|
|
2015-08-16 21:30:44 -05:00
|
|
|
// hey look! we're overriding terminate:!
|
|
|
|
// we're going to make sure we can go back to main() whether Cocoa likes it or not!
|
|
|
|
// and just how are we going to do that, hm?
|
|
|
|
// (note: this is called after applicationShouldTerminate:)
|
|
|
|
- (void)terminate:(id)sender
|
|
|
|
{
|
|
|
|
// yes that's right folks: DO ABSOLUTELY NOTHING.
|
|
|
|
// the magic is [NSApp run] will just... stop.
|
|
|
|
|
|
|
|
// well let's not do nothing; let's actually quit our graceful way
|
|
|
|
NSEvent *e;
|
|
|
|
|
|
|
|
if (!canQuit)
|
2016-05-13 19:14:46 -05:00
|
|
|
implbug("call to [NSApp terminate:] when not ready to terminate; definitely contact andlabs");
|
2015-08-16 21:30:44 -05:00
|
|
|
|
|
|
|
[realNSApp() stop:realNSApp()];
|
|
|
|
// stop: won't register until another event has passed; let's synthesize one
|
|
|
|
e = [NSEvent otherEventWithType:NSApplicationDefined
|
|
|
|
location:NSZeroPoint
|
|
|
|
modifierFlags:0
|
|
|
|
timestamp:[[NSProcessInfo processInfo] systemUptime]
|
|
|
|
windowNumber:0
|
|
|
|
context:[NSGraphicsContext currentContext]
|
|
|
|
subtype:0
|
|
|
|
data1:0
|
|
|
|
data2:0];
|
|
|
|
[realNSApp() postEvent:e atStart:NO]; // let pending events take priority (this is what PostQuitMessage() on Windows does so we have to do it here too for parity; thanks to mikeash in irc.freenode.net/#macdev for confirming that this parameter should indeed be NO)
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation appDelegate
|
|
|
|
|
|
|
|
- (void)dealloc
|
|
|
|
{
|
|
|
|
[self.menuManager release];
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)app
|
|
|
|
{
|
|
|
|
// for debugging
|
|
|
|
NSLog(@"in applicationShouldTerminate:");
|
|
|
|
if (shouldQuit()) {
|
|
|
|
canQuit = YES;
|
|
|
|
// this will call terminate:, which is the same as uiQuit()
|
|
|
|
return NSTerminateNow;
|
|
|
|
}
|
|
|
|
return NSTerminateCancel;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)app
|
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
uiInitOptions options;
|
|
|
|
|
|
|
|
const char *uiInit(uiInitOptions *o)
|
|
|
|
{
|
|
|
|
options = *o;
|
|
|
|
[applicationClass sharedApplication];
|
|
|
|
// don't check for a NO return; something (launch services?) causes running from application bundles to always return NO when asking to change activation policy, even if the change is to the same activation policy!
|
|
|
|
// see https://github.com/andlabs/ui/issues/6
|
|
|
|
[realNSApp() setActivationPolicy:NSApplicationActivationPolicyRegular];
|
|
|
|
[realNSApp() setDelegate:[appDelegate new]];
|
|
|
|
|
|
|
|
initAlloc();
|
|
|
|
|
|
|
|
// always do this so we always have an application menu
|
|
|
|
appDelegate().menuManager = [menuManager new];
|
|
|
|
[realNSApp() setMainMenu:[appDelegate().menuManager makeMenubar]];
|
|
|
|
|
2016-05-15 18:56:01 -05:00
|
|
|
setupFontPanel();
|
|
|
|
|
2015-08-16 21:30:44 -05:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void uiUninit(void)
|
|
|
|
{
|
|
|
|
uninitMenus();
|
2015-08-22 10:17:13 -05:00
|
|
|
[realNSApp() setDelegate:nil];
|
|
|
|
[appDelegate() release];
|
|
|
|
[realNSApp() release];
|
2015-08-16 21:30:44 -05:00
|
|
|
uninitAlloc();
|
|
|
|
}
|
|
|
|
|
|
|
|
void uiFreeInitError(const char *err)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void uiMain(void)
|
|
|
|
{
|
|
|
|
[realNSApp() run];
|
|
|
|
}
|
|
|
|
|
2015-12-04 22:42:03 -06:00
|
|
|
// TODO make this delayed
|
2015-08-16 21:30:44 -05:00
|
|
|
void uiQuit(void)
|
|
|
|
{
|
|
|
|
canQuit = YES;
|
|
|
|
[realNSApp() terminate:realNSApp()];
|
|
|
|
}
|
2015-12-04 22:42:03 -06:00
|
|
|
|
|
|
|
// thanks to mikeash in irc.freenode.net/#macdev for suggesting the use of Grand Central Dispatch for this
|
|
|
|
// TODO will dispatch_get_main_queue() break after _CFRunLoopSetCurrent()?
|
|
|
|
void uiQueueMain(void (*f)(void *data), void *data)
|
|
|
|
{
|
|
|
|
// dispatch_get_main_queue() is a serial queue so it will not execute multiple uiQueueMain() functions concurrently
|
|
|
|
// the signature of f matches dispatch_function_t
|
|
|
|
dispatch_async_f(dispatch_get_main_queue(), data, f);
|
|
|
|
}
|