From 40a9e8e70ff908ffd59b643d0d399d74d0eda8b8 Mon Sep 17 00:00:00 2001 From: Ian C Date: Sun, 12 Jul 2020 21:39:46 +0000 Subject: Added code to check if app should quit. --- SpriteEd/AppDelegate.cs | 67 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/SpriteEd/AppDelegate.cs b/SpriteEd/AppDelegate.cs index 1969e7e..073bfc9 100644 --- a/SpriteEd/AppDelegate.cs +++ b/SpriteEd/AppDelegate.cs @@ -49,6 +49,73 @@ namespace SpriteEd { } + public override NSApplicationTerminateReply ApplicationShouldTerminate(NSApplication sender) + { + NSApplicationTerminateReply reply = NSApplicationTerminateReply.Cancel; + bool changed = false; + + foreach (NSWindow window in NSApplication.SharedApplication.DangerousWindows) + { + changed = changed || window.DocumentEdited; + } + + if (changed) + { + NSAlert alert = new NSAlert () + { + AlertStyle = NSAlertStyle.Critical, + InformativeText = "Save changes to document(s) before closing window?", + MessageText = "Save Document", + }; + + alert.AddButton ("Save"); + alert.AddButton ("Delete"); + alert.AddButton ("Cancel"); + + switch (alert.RunModal()) + { + case 1000: + bool saved = true; + + foreach (NSWindow window in NSApplication.SharedApplication.DangerousWindows) + { + if (window.DocumentEdited) + { + ViewController view = window.ContentViewController as ViewController; + + if (view.Untitled) + { + saved = saved && view.SaveAs(); + } + else + { + saved = saved && view.Save(); + } + } + } + + if (saved) + { + reply = NSApplicationTerminateReply.Now; + } + break; + + case 1001: + reply = NSApplicationTerminateReply.Now; + break; + + default: + break; + } + } + else + { + reply = NSApplicationTerminateReply.Now; + } + + return reply; + } + /// /// Get the current view controller. /// -- cgit v1.2.3