From 25f2a5f457af27da495a24312452f6918a7983f4 Mon Sep 17 00:00:00 2001
From: hendricks266 <hendricks266@1a8010ca-5511-0410-912e-c29ae57300e0>
Date: Mon, 26 Dec 2016 06:02:27 +0000
Subject: [PATCH] Mac: Change the startup windows display the window normally
 and start a mini runloop instead of calling using runModalForWindow.

This way, the Quit function works.

git-svn-id: https://svn.eduke32.com/eduke32@5965 1a8010ca-5511-0410-912e-c29ae57300e0
---
 polymer/eduke32/build/src/startosx.editor.m | 45 ++++++++-------------
 polymer/eduke32/source/startosx.game.m      | 45 ++++++++-------------
 2 files changed, 32 insertions(+), 58 deletions(-)

diff --git a/polymer/eduke32/build/src/startosx.editor.m b/polymer/eduke32/build/src/startosx.editor.m
index f834501e4..e5c02e53b 100644
--- a/polymer/eduke32/build/src/startosx.editor.m
+++ b/polymer/eduke32/build/src/startosx.editor.m
@@ -79,6 +79,8 @@ static NSPopUpButton * makeComboBox(void)
 
 static id nsapp;
 
+static int retval = -1;
+
 static struct {
     int fullscreen;
     int xdim2d, ydim2d;
@@ -134,21 +136,6 @@ static struct {
         // window properties
         [self setDelegate:self];
         [self setReleasedWhenClosed:NO];
-#if defined MAC_OS_X_VERSION_10_6 && MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_6
-        [self setPreventsApplicationTerminationWhenModal:NO];
-#else
-        SEL selector = @selector(setPreventsApplicationTerminationWhenModal:);
-        if ([self respondsToSelector:selector])
-        {
-            BOOL argument = NO;
-            NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:selector]];
-            [invocation setSelector:selector];
-            [invocation setTarget:self];
-            [invocation setArgument:&argument atIndex:2];
-            [invocation invoke];
-            [invocation release];
-        }
-#endif
 #if defined MAC_OS_X_VERSION_10_3 && MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_3
         [self setContentMinSize:[[self contentView] frame].size];
 #else
@@ -314,7 +301,7 @@ static struct {
 {
     UNREFERENCED_PARAMETER(sender);
 
-    [nsapp abortModal];
+    retval = 0;
 
     return YES;
 }
@@ -414,7 +401,7 @@ static struct {
 {
     UNREFERENCED_PARAMETER(sender);
 
-    [nsapp abortModal];
+    retval = 0;
 }
 
 - (void)start:(id)sender
@@ -438,7 +425,7 @@ static struct {
 
     settings.forcesetup = [alwaysShowButton state] == NSOnState;
 
-    [nsapp stopModal];
+    retval = 1;
 }
 
 - (void)setupRunMode
@@ -575,8 +562,6 @@ int startwin_idle(void *v)
 
 int startwin_run(void)
 {
-    int retval;
-
     if (startwin == nil) return 0;
 
     settings.fullscreen = fullscreen;
@@ -589,16 +574,18 @@ int startwin_run(void)
 
     [startwin setupRunMode];
 
-    switch ([nsapp runModalForWindow:startwin]) {
-#ifdef MAC_OS_X_VERSION_10_9
-        case NSModalResponseStop: retval = 1; break;
-        case NSModalResponseAbort: retval = 0; break;
-#else
-        case NSRunStoppedResponse: retval = 1; break;
-        case NSRunAbortedResponse: retval = 0; break;
-#endif
-        default: retval = -1;
+    [nsapp finishLaunching];
+
+    [startwin center];
+    [startwin makeKeyAndOrderFront:nil];
+
+    do
+    {
+        NSEvent *event = [nsapp nextEventMatchingMask:NSAnyEventMask untilDate:[NSDate distantFuture] inMode:NSDefaultRunLoopMode dequeue:YES];
+        [nsapp sendEvent:event];
+        [nsapp updateWindows];
     }
+    while (retval == -1);
 
     [startwin setupMessagesMode];
 
diff --git a/polymer/eduke32/source/startosx.game.m b/polymer/eduke32/source/startosx.game.m
index 7b1535f6f..28916412e 100644
--- a/polymer/eduke32/source/startosx.game.m
+++ b/polymer/eduke32/source/startosx.game.m
@@ -86,6 +86,8 @@ static NSPopUpButton * makeComboBox(void)
 
 static id nsapp;
 
+static int retval = -1;
+
 static struct {
     grpfile_t const * grp;
     int fullscreen;
@@ -141,21 +143,6 @@ static struct {
         // window properties
         [self setDelegate:self];
         [self setReleasedWhenClosed:NO];
-#if defined MAC_OS_X_VERSION_10_6 && MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_6
-        [self setPreventsApplicationTerminationWhenModal:NO];
-#else
-        SEL selector = @selector(setPreventsApplicationTerminationWhenModal:);
-        if ([self respondsToSelector:selector])
-        {
-            BOOL argument = NO;
-            NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:selector]];
-            [invocation setSelector:selector];
-            [invocation setTarget:self];
-            [invocation setArgument:&argument atIndex:2];
-            [invocation invoke];
-            [invocation release];
-        }
-#endif
 #if defined MAC_OS_X_VERSION_10_3 && MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_3
         [self setContentMinSize:[[self contentView] frame].size];
 #else
@@ -339,7 +326,7 @@ static struct {
 {
     UNREFERENCED_PARAMETER(sender);
 
-    [nsapp abortModal];
+    retval = 0;
 
     return YES;
 }
@@ -409,7 +396,7 @@ static struct {
 {
     UNREFERENCED_PARAMETER(sender);
 
-    [nsapp abortModal];
+    retval = 0;
 }
 
 - (void)start:(id)sender
@@ -431,7 +418,7 @@ static struct {
 
     settings.forcesetup = [alwaysShowButton state] == NSOnState;
 
-    [nsapp stopModal];
+    retval = 1;
 }
 
 - (void)setupRunMode
@@ -582,8 +569,6 @@ int startwin_idle(void *v)
 
 int startwin_run(void)
 {
-    int retval;
-
     if (startwin == nil) return 0;
 
     settings.fullscreen = ud.config.ScreenMode;
@@ -595,16 +580,18 @@ int startwin_run(void)
 
     [startwin setupRunMode];
 
-    switch ([nsapp runModalForWindow:startwin]) {
-#ifdef MAC_OS_X_VERSION_10_9
-        case NSModalResponseStop: retval = 1; break;
-        case NSModalResponseAbort: retval = 0; break;
-#else
-        case NSRunStoppedResponse: retval = 1; break;
-        case NSRunAbortedResponse: retval = 0; break;
-#endif
-        default: retval = -1;
+    [nsapp finishLaunching];
+
+    [startwin center];
+    [startwin makeKeyAndOrderFront:nil];
+
+    do
+    {
+        NSEvent *event = [nsapp nextEventMatchingMask:NSAnyEventMask untilDate:[NSDate distantFuture] inMode:NSDefaultRunLoopMode dequeue:YES];
+        [nsapp sendEvent:event];
+        [nsapp updateWindows];
     }
+    while (retval == -1);
 
     [startwin setupMessagesMode];