Skip to content

Commit

Permalink
Warn about unsaved changes on open and exit.
Browse files Browse the repository at this point in the history
  • Loading branch information
int19h committed Sep 15, 2018
1 parent 9e6c516 commit f8c04a8
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
1 change: 1 addition & 0 deletions WarBender.UI/MainForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 62 additions & 1 deletion WarBender.UI/MainForm.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Data;
using System.Diagnostics;
using System.IO;
Expand All @@ -17,7 +19,6 @@ namespace WarBender.UI {
public partial class MainForm : FormBase {
public static MainForm Instance;

public Module module;
private string _initialText;
private readonly ModelGetters _modelGetters = new ModelGetters();

Expand Down Expand Up @@ -57,10 +58,27 @@ public MainForm() {

internal GameDesignerHost DesignerHost { get; private set; }

public string FileName { get; private set; }

public bool IsDirty { get; private set; }

public Game Game {
get => DesignerHost?.Game;
set {
if (DesignerHost != null) {
DesignerHost.ComponentAdded -= DesignerHost_ComponentAdded;
DesignerHost.ComponentChanged -= DesignerHost_ComponentChanged;
DesignerHost.ComponentRemoved -= DesignerHost_ComponentRemoved;
}

DesignerHost = value == null ? null : new GameDesignerHost(value);

if (DesignerHost != null) {
DesignerHost.ComponentAdded += DesignerHost_ComponentAdded;
DesignerHost.ComponentChanged += DesignerHost_ComponentChanged;
DesignerHost.ComponentRemoved += DesignerHost_ComponentRemoved;
}

_modelGetters.Game = value;
}
}
Expand Down Expand Up @@ -250,6 +268,10 @@ private async Task WithModuleFor(Stopwatch sw, string fileName, Func<Module, Tas
}

public async Task OpenAsync(string fileName) {
if (!ConfirmDiscardingChanges()) {
return;
}

UpdateFileDialogs(fileName);
UseWaitCursor = true;
openToolStripMenuItem.Enabled = false;
Expand All @@ -266,6 +288,7 @@ public async Task OpenAsync(string fileName) {
mdiChild.Close();
}
Game = null;
IsDirty = false;
Text = _initialText;

Game game = null;
Expand Down Expand Up @@ -295,6 +318,7 @@ await Task.Run(() => {
treeListView.Expand(Game.Data);
ShowPropertyGrid(treeListView.Objects);

FileName = fileName;
Text = Path.GetFileName(fileName) + " - " + _initialText;

if (roundtripError != null) {
Expand Down Expand Up @@ -394,6 +418,8 @@ await Task.Run(() => {
}
}

IsDirty = false;
FileName = fileName;
Text = Path.GetFileName(fileName) + " - " + _initialText;

sw.Stop();
Expand Down Expand Up @@ -453,6 +479,23 @@ protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
return base.ProcessCmdKey(ref msg, keyData);
}

private void ProcessComponentChange(object component) {
if (component is IDataObjectChild) {
IsDirty = true;
}
}

private bool ConfirmDiscardingChanges() {
if (!IsDirty) {
return true;
}
var dr = MessageBox.Show(this,
"You have unsaved changes. If you continue with this operation, " +
"they will be lost. Are you sure you want to proceed?",
null, MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
return dr == DialogResult.Yes;
}

private void menuStrip_ItemAdded(object sender, ToolStripItemEventArgs e) {
// Hide MDI child icon.
if (e.Item.GetType().ToString() == "System.Windows.Forms.MdiControlStrip+SystemMenuItem") {
Expand Down Expand Up @@ -570,5 +613,23 @@ private void showConsoleToolStripMenuItem_Click(object sender, EventArgs e) {
mdiChild.WindowState = FormWindowState.Minimized;
}
}

private void DesignerHost_ComponentRemoved(object sender, ComponentEventArgs e) {
ProcessComponentChange(e.Component);
}

private void DesignerHost_ComponentChanged(object sender, ComponentChangedEventArgs e) {
ProcessComponentChange(e.Component);
}

private void DesignerHost_ComponentAdded(object sender, ComponentEventArgs e) {
ProcessComponentChange(e.Component);
}

private void MainForm_FormClosing(object sender, FormClosingEventArgs e) {
if (!ConfirmDiscardingChanges()) {
e.Cancel = true;
}
}
}
}

0 comments on commit f8c04a8

Please sign in to comment.