From a7e6b101c4ef002eba4dd3bcd4986d10729fb5c4 Mon Sep 17 00:00:00 2001 From: Andy McFadden Date: Sat, 3 Nov 2018 14:34:05 -0700 Subject: [PATCH] Upgrade "discard changes" dialog Was a yes/no MessageBox, now it's a three-way with the option to save your changes before continuing. --- SourceGen/AppForms/DiscardChanges.Designer.cs | 119 +++++++++++++++++ SourceGen/AppForms/DiscardChanges.cs | 48 +++++++ SourceGen/AppForms/DiscardChanges.resx | 120 ++++++++++++++++++ SourceGen/AppForms/ProjectView.cs | 35 +++-- SourceGen/Properties/Resources.Designer.cs | 18 --- SourceGen/Properties/Resources.resx | 6 - SourceGen/SourceGen.csproj | 9 ++ 7 files changed, 319 insertions(+), 36 deletions(-) create mode 100644 SourceGen/AppForms/DiscardChanges.Designer.cs create mode 100644 SourceGen/AppForms/DiscardChanges.cs create mode 100644 SourceGen/AppForms/DiscardChanges.resx diff --git a/SourceGen/AppForms/DiscardChanges.Designer.cs b/SourceGen/AppForms/DiscardChanges.Designer.cs new file mode 100644 index 0000000..cc81dfa --- /dev/null +++ b/SourceGen/AppForms/DiscardChanges.Designer.cs @@ -0,0 +1,119 @@ +/* + * Copyright 2018 faddenSoft + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +namespace SourceGen.AppForms { + partial class DiscardChanges { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) { + if (disposing && (components != null)) { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() { + this.cancelButton = new System.Windows.Forms.Button(); + this.dontSaveButton = new System.Windows.Forms.Button(); + this.saveButton = new System.Windows.Forms.Button(); + this.warningLabel = new System.Windows.Forms.Label(); + this.SuspendLayout(); + // + // cancelButton + // + this.cancelButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.cancelButton.DialogResult = System.Windows.Forms.DialogResult.Cancel; + this.cancelButton.Location = new System.Drawing.Point(243, 74); + this.cancelButton.Name = "cancelButton"; + this.cancelButton.Size = new System.Drawing.Size(110, 23); + this.cancelButton.TabIndex = 3; + this.cancelButton.Text = "Cancel"; + this.cancelButton.UseVisualStyleBackColor = true; + // + // dontSaveButton + // + this.dontSaveButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.dontSaveButton.Location = new System.Drawing.Point(127, 74); + this.dontSaveButton.Name = "dontSaveButton"; + this.dontSaveButton.Size = new System.Drawing.Size(110, 23); + this.dontSaveButton.TabIndex = 2; + this.dontSaveButton.Text = "&Discard && Continue"; + this.dontSaveButton.UseVisualStyleBackColor = true; + this.dontSaveButton.Click += new System.EventHandler(this.dontSaveButton_Click); + // + // saveButton + // + this.saveButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.saveButton.Location = new System.Drawing.Point(11, 74); + this.saveButton.Name = "saveButton"; + this.saveButton.Size = new System.Drawing.Size(110, 23); + this.saveButton.TabIndex = 1; + this.saveButton.Text = "&Save && Continue"; + this.saveButton.UseVisualStyleBackColor = true; + this.saveButton.Click += new System.EventHandler(this.saveButton_Click); + // + // warningLabel + // + this.warningLabel.AutoSize = true; + this.warningLabel.Location = new System.Drawing.Point(13, 13); + this.warningLabel.Name = "warningLabel"; + this.warningLabel.Size = new System.Drawing.Size(288, 39); + this.warningLabel.TabIndex = 0; + this.warningLabel.Text = "You have unsaved changes that will be lost if you continue.\r\n\r\nHow do you wish to" + + " proceed?"; + // + // DiscardChanges + // + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.CancelButton = this.cancelButton; + this.ClientSize = new System.Drawing.Size(365, 109); + this.Controls.Add(this.warningLabel); + this.Controls.Add(this.saveButton); + this.Controls.Add(this.dontSaveButton); + this.Controls.Add(this.cancelButton); + this.MaximizeBox = false; + this.MinimizeBox = false; + this.Name = "DiscardChanges"; + this.ShowInTaskbar = false; + this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent; + this.Text = "Discard Changes?"; + this.Load += new System.EventHandler(this.DiscardChanges_Load); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private System.Windows.Forms.Button cancelButton; + private System.Windows.Forms.Button dontSaveButton; + private System.Windows.Forms.Button saveButton; + private System.Windows.Forms.Label warningLabel; + } +} \ No newline at end of file diff --git a/SourceGen/AppForms/DiscardChanges.cs b/SourceGen/AppForms/DiscardChanges.cs new file mode 100644 index 0000000..b34477b --- /dev/null +++ b/SourceGen/AppForms/DiscardChanges.cs @@ -0,0 +1,48 @@ +/* + * Copyright 2018 faddenSoft + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +using System; +using System.Windows.Forms; + +namespace SourceGen.AppForms { + /// + /// Prompt the user before discarding changes. + /// + /// Dialog result will be: + /// DialogResult.Yes: save before continuing + /// DialogResult.No: don't save before continuing + /// DialogResult.Cancel: don't continue + /// + public partial class DiscardChanges : Form { + public DiscardChanges() { + InitializeComponent(); + } + + private void DiscardChanges_Load(object sender, EventArgs e) { + // Make this the default. + cancelButton.Select(); + } + + private void saveButton_Click(object sender, EventArgs e) { + DialogResult = DialogResult.Yes; + Close(); + } + + private void dontSaveButton_Click(object sender, EventArgs e) { + DialogResult = DialogResult.No; + Close(); + } + } +} diff --git a/SourceGen/AppForms/DiscardChanges.resx b/SourceGen/AppForms/DiscardChanges.resx new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ b/SourceGen/AppForms/DiscardChanges.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/SourceGen/AppForms/ProjectView.cs b/SourceGen/AppForms/ProjectView.cs index 5158641..770cdf3 100644 --- a/SourceGen/AppForms/ProjectView.cs +++ b/SourceGen/AppForms/ProjectView.cs @@ -1495,20 +1495,18 @@ namespace SourceGen.AppForms { } // File > Save (Ctrl+S) - private void saveToolStripMenuItem_Click(object sender, EventArgs e) { - if (string.IsNullOrEmpty(mProjectPathName)) { - saveAsToolStripMenuItem_Click(sender, e); - return; - } - - DoSave(mProjectPathName); - } private void saveToolStripButton_Click(object sender, EventArgs e) { saveToolStripMenuItem_Click(sender, e); } - + private void saveToolStripMenuItem_Click(object sender, EventArgs e) { + DoSave(); + } // File > Save As... private void saveAsToolStripMenuItem_Click(object sender, EventArgs e) { + DoSaveAs(); + } + + private bool DoSaveAs() { SaveFileDialog fileDlg = new SaveFileDialog() { Filter = ProjectFile.FILENAME_FILTER + "|" + Properties.Resources.FILE_FILTER_ALL, FilterIndex = 1, @@ -1525,8 +1523,18 @@ namespace SourceGen.AppForms { // add it to the title bar UpdateMenuItemsAndTitle(); + return true; } } + return false; + } + + // Save the project. If it hasn't been saved before, use save-as behavior instead. + private bool DoSave() { + if (string.IsNullOrEmpty(mProjectPathName)) { + return DoSaveAs(); + } + return DoSave(mProjectPathName); } private bool DoSave(string pathName) { @@ -1588,11 +1596,14 @@ namespace SourceGen.AppForms { Debug.WriteLine("ProjectView.DoClose() - dirty=" + (mProject == null ? "N/A" : mProject.IsDirty.ToString())); if (mProject != null && mProject.IsDirty) { - DialogResult result = MessageBox.Show(this, Properties.Resources.UNSAVED_CHANGES, - Properties.Resources.UNSAVED_CHANGES_CAPTION, MessageBoxButtons.OKCancel, - MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2); + DiscardChanges dlg = new DiscardChanges(); + DialogResult result = dlg.ShowDialog(this); if (result == DialogResult.Cancel) { return false; + } else if (result == DialogResult.Yes) { + if (!DoSave()) { + return false; + } } } diff --git a/SourceGen/Properties/Resources.Designer.cs b/SourceGen/Properties/Resources.Designer.cs index bbfc6ff..17fcca7 100644 --- a/SourceGen/Properties/Resources.Designer.cs +++ b/SourceGen/Properties/Resources.Designer.cs @@ -996,24 +996,6 @@ namespace SourceGen.Properties { } } - /// - /// Looks up a localized string similar to The current project has unsaved changes that will be lost. Are you sure you wish to continue?. - /// - internal static string UNSAVED_CHANGES { - get { - return ResourceManager.GetString("UNSAVED_CHANGES", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Unsaved Changes. - /// - internal static string UNSAVED_CHANGES_CAPTION { - get { - return ResourceManager.GetString("UNSAVED_CHANGES_CAPTION", resourceCulture); - } - } - /// /// Looks up a localized string similar to [unset]. /// diff --git a/SourceGen/Properties/Resources.resx b/SourceGen/Properties/Resources.resx index c499c0c..a50c74b 100644 --- a/SourceGen/Properties/Resources.resx +++ b/SourceGen/Properties/Resources.resx @@ -429,12 +429,6 @@ [new project] - - The current project has unsaved changes that will be lost. Are you sure you wish to continue? - - - Unsaved Changes - [unset] diff --git a/SourceGen/SourceGen.csproj b/SourceGen/SourceGen.csproj index d3e5d53..14090d1 100644 --- a/SourceGen/SourceGen.csproj +++ b/SourceGen/SourceGen.csproj @@ -71,6 +71,12 @@ AboutBox.cs + + Form + + + DiscardChanges.cs + Form @@ -269,6 +275,9 @@ AboutBox.cs + + DiscardChanges.cs + EditAppSettings.cs