shithub: drawterm

Download patch

ref: 347c54dbd3495d7414c6149c828d276efd232f7d
parent: 6c519f33091bdee3a5c2f65e89510239825f76a8
author: Russ Cox <[email protected]>
date: Tue Jun 24 13:50:12 EDT 2008

better handling of quit events (underspecified@gmail)

--- a/gui-osx/screen.c
+++ b/gui-osx/screen.c
@@ -111,6 +111,8 @@
 	ksleep(&rend, isready, 0);
 }
 
+// No wonder Apple sells so many wide displays!
+static OSStatus ApplicationQuitEventHandler(EventHandlerCallRef nextHandler, EventRef event, void *userData);
 static OSStatus MainWindowEventHandler(EventHandlerCallRef nextHandler, EventRef event, void *userData);
 static OSStatus MainWindowCommandHandler(EventHandlerCallRef nextHandler, EventRef event, void *userData);
 
@@ -165,6 +167,9 @@
 	if(PasteboardCreate(kPasteboardClipboard, &appleclip) != noErr)
 		sysfatal("pasteboard create failed");
 
+	const EventTypeSpec quit_events[] = {
+		{ kEventClassApplication, kEventAppQuit }
+	};
 	const EventTypeSpec commands[] = {
 		{ kEventClassWindow, kEventWindowClosed },
 		{ kEventClassWindow, kEventWindowBoundsChanged },
@@ -181,6 +186,13 @@
 		{ kEventClassMouse, kEventMouseWheelMoved },
 	};
 
+	InstallApplicationEventHandler (
+								NewEventHandlerUPP (ApplicationQuitEventHandler),
+								GetEventTypeCount(quit_events),
+								quit_events,
+								NULL,
+								NULL);
+
  	InstallApplicationEventHandler (
  								NewEventHandlerUPP (MainWindowEventHandler),
 								GetEventTypeCount(events),
@@ -328,6 +340,14 @@
 	}
 }
 
+// catch quit events to handle quits from menu, Cmd+Q, applescript, and task switcher
+static OSStatus ApplicationQuitEventHandler(EventHandlerCallRef nextHandler, EventRef event, void *userData)
+{
+	exit(0);
+//	QuitApplicationEventLoop();
+	return noErr;
+}
+ 
 static OSStatus MainWindowEventHandler(EventHandlerCallRef nextHandler, EventRef event, void *userData)
 {
 	OSStatus result = noErr;
@@ -517,9 +537,21 @@
 		switch (kind)
 		{
 			case kEventWindowClosed:
-				theWindow = NULL;
-				exit(0); // only one window
-				break;
+				// send a quit carbon event instead of directly calling cleanexit 
+				// so that all quits are done in ApplicationQuitEventHandler
+				{
+				EventRef quitEvent;
+				CreateEvent(NULL,
+							kEventClassApplication,
+							kEventAppQuit,
+							0,
+							kEventAttributeNone,
+							&quitEvent);
+				EventTargetRef target;
+				target = GetApplicationEventTarget();
+				SendEventToEventTarget(quitEvent, target);
+				}
+ 				break;
 
 			//resize window
 			case kEventWindowBoundsChanged: