ui/cocoa.m: verify with user before quitting QEMU
PATCH v3 1/1 ui/cocoa.m: verify with user before quitting QEMU
This patch prevents the user from accidentally quitting QEMU by pushing Command-Q or by pushing the close button on the main window. When the user does one of these two things, a dialog box appears verifying with the user if he or she wants to quit QEMU.
---
Replaced NSRunAlertPanel() with NSAlert.
Added more informative comment to windowShouldClose: method.
ui/cocoa.m | 39 ++++++++++++++++++++++++++++++++++++++- 1 files changed, 38 insertions(+), 1 deletions(-)
ui/cocoa.m +38 -1
@@ -809,7 +809,7 @@ QemuCocoaView *cocoaView; */ @interface QemuCocoaAppController : NSObject #if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6) - <NSApplicationDelegate> + <NSWindowDelegate, NSApplicationDelegate> #endif { }
@@ -829,6 +829,7 @@ QemuCocoaView *cocoaView; - (void)powerDownQEMU:(id)sender; - (void)ejectDeviceMedia:(id)sender; - (void)changeDeviceMedia:(id)sender; +- (BOOL)verifyQuit; @end @implementation QemuCocoaAppController
@@ -862,6 +863,7 @@ QemuCocoaView *cocoaView; #endif [normalWindow makeKeyAndOrderFront:self]; [normalWindow center]; + [normalWindow setDelegate: self]; stretch_video = false; /* Used for displaying pause on the screen */
@@ -933,6 +935,26 @@ QemuCocoaView *cocoaView; return YES; } +- (NSApplicationTerminateReply)applicationShouldTerminate: + (NSApplication *)sender +{ + COCOA_DEBUG("QemuCocoaAppController: applicationShouldTerminate\n"); + return [self verifyQuit]; +} + +/* Called when the user clicks on a window's close button */ +- (BOOL)windowShouldClose:(id)sender +{ + COCOA_DEBUG("QemuCocoaAppController: windowShouldClose\n"); + [NSApp terminate: sender]; + /* If the user allows the application to quit then the call to + * NSApp terminate will never return. If we get here then the user + * cancelled the quit, so we should return NO to not permit the + * closing of this window. + */ + return NO; +} + - (void)startEmulationWithArgc:(int)argc argv:(char**)argv { COCOA_DEBUG("QemuCocoaAppController: startEmulationWithArgc\n");
@@ -1125,6 +1147,21 @@ QemuCocoaView *cocoaView; } } +/* Verifies if the user really wants to quit */ +- (BOOL)verifyQuit +{ + NSAlert *alert = [NSAlert new]; + [alert autorelease]; + [alert setMessageText: @"Are you sure you want to quit QEMU?"]; + [alert addButtonWithTitle: @"Cancel"]; + [alert addButtonWithTitle: @"Quit"]; + if([alert runModal] == NSAlertSecondButtonReturn) { + return YES; + } else { + return NO; + } +} + @end
Signature · 1 line
1.7.5.4
Quoted 10 lines · Programmingkid
This patch prevents the user from accidentally quitting QEMU by pushing Command-Q or by pushing the close button on the main window. When the user does one of these two things, a dialog box appears verifying with the user if he or she wants to quit QEMU.
Signed-off-by John Arbuckle <[email protected]>---
ReplacedNSRunAlertPanel()with NSAlert.
Added more informative comment to windowShouldClose: method.
Thanks, I have applied this to my cocoa.next branch.
Sending these as plain text has definitely helped with my patch tool workflow, so thank you for sorting that out. There is one more thing which you could fix which would mean I could avoid another manual fixup: your From: address for these emails is "Programmingkid <[email protected]>", which means I have to manually edit the authorship of the git commit to match up with your Signed-off-by: line. If you could make the from match up with the signed-off-by line it would save me having to do it at my end.
(If you use git format-patch/git send-email then it should take the authorship from your local git commit, assuming you have it correct there, and put a suitable From line in the body of the mail in the right format so that it goes through even if your from-line-for-email is different.)
thanks
-- PMM