better handling of quit events (underspecified@gmail)

This commit is contained in:
Russ Cox 2008-06-24 17:50:12 +00:00
parent 6c519f3309
commit 347c54dbd3
1 changed files with 35 additions and 3 deletions

View File

@ -111,6 +111,8 @@ void screeninit(void)
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 @@ void winproc(void *a)
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 @@ void winproc(void *a)
{ kEventClassMouse, kEventMouseWheelMoved },
};
InstallApplicationEventHandler (
NewEventHandlerUPP (ApplicationQuitEventHandler),
GetEventTypeCount(quit_events),
quit_events,
NULL,
NULL);
InstallApplicationEventHandler (
NewEventHandlerUPP (MainWindowEventHandler),
GetEventTypeCount(events),
@ -328,6 +340,14 @@ full_screen()
}
}
// 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 @@ static OSStatus MainWindowCommandHandler(EventHandlerCallRef nextHandler,
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: