From 953e5de5bf59c1a453d059fc813cbaf50d8c0f77 Mon Sep 17 00:00:00 2001 From: Ian C Date: Sun, 12 Jul 2020 20:16:41 +0000 Subject: Added closing window checks. Also moved Save code to ViewController. --- SpriteEd/AppDelegate.cs | 60 ++++++++++------------- SpriteEd/NSSpriteEdit.cs | 20 ++++++++ SpriteEd/NSSpriteEditWindowDelegate.cs | 87 ++++++++++++++++++++++------------ SpriteEd/ViewController.cs | 75 ++++++++++++++++++++++++++++- 4 files changed, 176 insertions(+), 66 deletions(-) diff --git a/SpriteEd/AppDelegate.cs b/SpriteEd/AppDelegate.cs index c213a25..1969e7e 100644 --- a/SpriteEd/AppDelegate.cs +++ b/SpriteEd/AppDelegate.cs @@ -67,29 +67,7 @@ namespace SpriteEd [Export("saveDocumentAs:")] private void OnSaveDocumentAs(NSObject sender) { - ViewController view = ViewController; - - NSSavePanel fsel = new NSSavePanel(); - - fsel.Title = "Save Sprite Set"; - fsel.AllowedFileTypes = new string[] {"sprset"}; - fsel.ShowsTagField = false; - - if (fsel.RunModal() == 1) - { - view.Url = fsel.Url; - - try - { - FileStream stream = File.Create(view.Url.Path); - view.SpriteSet.Save(stream); - stream.Close(); - } - catch (Exception e) - { - Util.DisplayAlert("Error saving sprite set", e.Message); - } - } + ViewController.SaveAs(); } /// @@ -103,20 +81,11 @@ namespace SpriteEd if (view.Untitled) { - OnSaveDocumentAs(sender); + view.SaveAs(); } else { - try - { - FileStream stream = File.Create(view.Url.Path); - view.SpriteSet.Save(stream); - stream.Close(); - } - catch (Exception e) - { - Util.DisplayAlert("Error saving sprite set", e.Message); - } + view.Save(); } } @@ -157,6 +126,29 @@ namespace SpriteEd } } + /// + /// Handle Revert Document To Saved. + /// + /// The event sender. + [Export("revertDocumentToSaved:")] + private void OnRevertDocumentToSaved(NSObject sender) + { + ViewController view = ViewController; + + try + { + FileStream stream = File.OpenRead(view.Url.Path); + SpriteSet set = SpriteSet.Load(stream); + stream.Close(); + + view.SpriteSet = set; + } + catch (Exception e) + { + Util.DisplayAlert("Error reverting sprite set", e.Message); + } + } + /// /// Handle Cut. /// diff --git a/SpriteEd/NSSpriteEdit.cs b/SpriteEd/NSSpriteEdit.cs index 8d9a41f..53e9ab3 100644 --- a/SpriteEd/NSSpriteEdit.cs +++ b/SpriteEd/NSSpriteEdit.cs @@ -131,6 +131,24 @@ namespace SpriteEd /// public bool DoubleHeight {get; set;} + /// + /// Event called when sprite edited. + /// + public event EventHandler SpriteEdited; + + /// + /// Calls SpriteEdited event. + /// + public void OnSpriteEdited(EventArgs e) + { + EventHandler handler = SpriteEdited; + + if (handler != null) + { + handler(this, e); + } + } + /// /// Default constructor /// @@ -253,6 +271,8 @@ namespace SpriteEd // but this seems to cause a crash deep in the runtime. // del.UndoManager.RegisterUndo(this, Undo); + + OnSpriteEdited(new EventArgs()); } } diff --git a/SpriteEd/NSSpriteEditWindowDelegate.cs b/SpriteEd/NSSpriteEditWindowDelegate.cs index d0cf4e0..eb1579b 100644 --- a/SpriteEd/NSSpriteEditWindowDelegate.cs +++ b/SpriteEd/NSSpriteEditWindowDelegate.cs @@ -22,42 +22,21 @@ namespace SpriteEd { public class NSSpriteEditWindowDelegate : NSWindowDelegate { - private NSUndoManager m_manager; - /// - /// Default constructor. + /// Get the undo manager /// - public NSSpriteEditWindowDelegate() : base() - { - Init(); - } + public NSUndoManager UndoManager {get; private set;} - /// - /// Construct given handle. - /// - /// The handle. - public NSSpriteEditWindowDelegate(IntPtr handle) : base(handle) - { - Init(); - } + private NSWindow Window {get; set;} /// - /// Initialise the delegate. + /// Construct given a window. /// - private void Init() + /// The window. + public NSSpriteEditWindowDelegate(NSWindow window) { - m_manager = new NSUndoManager(); - } - - /// - /// Get the Undo Manager. - /// - public NSUndoManager UndoManager - { - get - { - return m_manager; - } + UndoManager = new NSUndoManager(); + Window = window; } /// @@ -78,9 +57,55 @@ namespace SpriteEd { get { - NSWindowController controller = NSApplication.SharedApplication.KeyWindow.WindowController as NSWindowController; - return controller.ContentViewController as ViewController; + return Window.ContentViewController as ViewController; + } + } + + [Export("windowShouldClose:")] + public override bool WindowShouldClose(NSObject sender) + { + bool close = false; + + if (Window.DocumentEdited) + { + NSAlert alert = new NSAlert () + { + AlertStyle = NSAlertStyle.Critical, + InformativeText = "Save changes to document before closing window?", + MessageText = "Save Document", + }; + + alert.AddButton ("Save"); + alert.AddButton ("Delete"); + alert.AddButton ("Cancel"); + + switch (alert.RunModal()) + { + case 1000: + if (ViewController.Untitled) + { + close = ViewController.SaveAs(); + } + else + { + close = ViewController.Save(); + } + break; + + case 1001: + close = true; + break; + + default: + break; + } } + else + { + close = true; + } + + return close; } } } diff --git a/SpriteEd/ViewController.cs b/SpriteEd/ViewController.cs index 588f9d8..d555dd1 100644 --- a/SpriteEd/ViewController.cs +++ b/SpriteEd/ViewController.cs @@ -15,6 +15,7 @@ // along with this program. If not, see . // using System; +using System.IO; using AppKit; using Foundation; @@ -49,6 +50,8 @@ namespace SpriteEd Untitled = true; SpriteSet = new SpriteSet(SpriteSet.SetType.C64CharacterSet, 8, 8, palette, false, false, CodePoints.C64); + + m_SpriteEdit.SpriteEdited += SpriteEdited; } /// @@ -58,7 +61,7 @@ namespace SpriteEd { base.ViewDidAppear(); - View.Window.Delegate = new NSSpriteEditWindowDelegate(); + View.Window.Delegate = new NSSpriteEditWindowDelegate(View.Window); if (Untitled) { @@ -251,5 +254,75 @@ namespace SpriteEd m_SpriteEdit.NeedsDisplay = true; } + + /// + /// Event to handle sprite edited. + /// + /// The object raising the event. + /// The event arguments. + private void SpriteEdited(object o, EventArgs e) + { + View.Window.DocumentEdited = true; + } + + /// + /// Select a file name and save the current sprite set. + /// + /// True if the sprite set was saved. + public bool SaveAs() + { + bool ok = false; + + NSSavePanel fsel = new NSSavePanel(); + + fsel.Title = "Save Sprite Set"; + fsel.AllowedFileTypes = new string[] {"sprset"}; + fsel.ShowsTagField = false; + + if (fsel.RunModal() == 1) + { + Url = fsel.Url; + + try + { + FileStream stream = File.Create(Url.Path); + SpriteSet.Save(stream); + stream.Close(); + ok = true; + } + catch (Exception e) + { + Util.DisplayAlert("Error saving sprite set", e.Message); + } + } + + return ok; + } + + /// + /// Save the current sprite set. + /// + /// True if the sprite set was saved. + public bool Save() + { + bool ok = false; + + try + { + if (Url != null) + { + FileStream stream = File.Create(Url.Path); + SpriteSet.Save(stream); + stream.Close(); + ok = true; + } + } + catch (Exception e) + { + Util.DisplayAlert("Error saving sprite set", e.Message); + } + + return ok; + } } } -- cgit v1.2.3