From f32135e2c78268d2a654f92474c170ac9f8c5ac9 Mon Sep 17 00:00:00 2001 From: Andy McFadden Date: Sun, 16 Jun 2019 16:46:40 -0700 Subject: [PATCH] Set Owner for all modal dialogs This turns out to be really important. Otherwise the modal dialog doesn't stay on top of the application's window stack, which can make things awkward when ShowInTaskbar is set to false. The easiest way to ensure this is getting done is to make it part of the constructor arguments. The code now passes the parent window in explicitly. WPF MessageBox avoids this by calling UnsafeNativeMethods.GetActiveWindow(), but that feels weird. We could assume Application.Current.MainWindow is the parent, but that seems like it could go quietly and horribly wrong. --- SourceGenWPF/MainController.cs | 13 ++++++------- SourceGenWPF/ProjWin/DataFileLoadIssue.xaml.cs | 3 ++- SourceGenWPF/ProjWin/DiscardChanges.xaml.cs | 3 ++- SourceGenWPF/ProjWin/EditAddress.xaml.cs | 6 ++---- SourceGenWPF/ProjWin/ProjectLoadIssue.xaml.cs | 3 ++- 5 files changed, 14 insertions(+), 14 deletions(-) diff --git a/SourceGenWPF/MainController.cs b/SourceGenWPF/MainController.cs index a2a90f7..9279367 100644 --- a/SourceGenWPF/MainController.cs +++ b/SourceGenWPF/MainController.cs @@ -402,7 +402,7 @@ namespace SourceGenWPF { string messages = mProject.LoadExternalFiles(); if (messages.Length != 0) { // ProjectLoadIssues isn't quite the right dialog, but it'll do. - ProjectLoadIssues dlg = new ProjectLoadIssues(messages, + ProjectLoadIssues dlg = new ProjectLoadIssues(mMainWin, messages, ProjectLoadIssues.Buttons.Continue); dlg.ShowDialog(); } @@ -711,7 +711,7 @@ namespace SourceGenWPF { // Should probably use a less-busy dialog for something simple like // "permission denied", but the open file dialog handles most simple // stuff directly. - ProjectLoadIssues dlg = new ProjectLoadIssues(report.Format(), + ProjectLoadIssues dlg = new ProjectLoadIssues(mMainWin, report.Format(), ProjectLoadIssues.Buttons.Cancel); dlg.ShowDialog(); // ignore dlg.DialogResult @@ -741,7 +741,7 @@ namespace SourceGenWPF { // If there were warnings, notify the user and give the a chance to cancel. if (report.Count != 0) { - ProjectLoadIssues dlg = new ProjectLoadIssues(report.Format(), + ProjectLoadIssues dlg = new ProjectLoadIssues(mMainWin, report.Format(), ProjectLoadIssues.Buttons.ContinueOrCancel); bool? ok = dlg.ShowDialog(); @@ -818,7 +818,7 @@ namespace SourceGenWPF { /// Message to display in the message box. /// Full path of file to open. private string ChooseDataFile(string origPath, string errorMsg) { - DataFileLoadIssue dlg = new DataFileLoadIssue(origPath, errorMsg); + DataFileLoadIssue dlg = new DataFileLoadIssue(mMainWin, origPath, errorMsg); bool? ok = dlg.ShowDialog(); if (ok != true) { return null; @@ -909,7 +909,7 @@ namespace SourceGenWPF { Debug.WriteLine("ProjectView.DoClose() - dirty=" + (mProject == null ? "N/A" : mProject.IsDirty.ToString())); if (mProject != null && mProject.IsDirty) { - DiscardChanges dlg = new DiscardChanges(); + DiscardChanges dlg = new DiscardChanges(mMainWin); bool? ok = dlg.ShowDialog(); if (ok != true) { return false; @@ -1118,8 +1118,7 @@ namespace SourceGenWPF { int offset = CodeLineList[selIndex].FileOffset; Anattrib attr = mProject.GetAnattrib(offset); - EditAddress dlg = new EditAddress(attr.Address, mProject.CpuDef.MaxAddressValue); - dlg.Owner = mMainWin; + EditAddress dlg = new EditAddress(mMainWin, attr.Address, mProject.CpuDef.MaxAddressValue); bool? ok = dlg.ShowDialog(); if (ok != true) { return; diff --git a/SourceGenWPF/ProjWin/DataFileLoadIssue.xaml.cs b/SourceGenWPF/ProjWin/DataFileLoadIssue.xaml.cs index f88a038..d7ae4ea 100644 --- a/SourceGenWPF/ProjWin/DataFileLoadIssue.xaml.cs +++ b/SourceGenWPF/ProjWin/DataFileLoadIssue.xaml.cs @@ -32,8 +32,9 @@ namespace SourceGenWPF.ProjWin { private string mMessage; - public DataFileLoadIssue(string pathName, string message) { + public DataFileLoadIssue(Window owner, string pathName, string message) { InitializeComponent(); + Owner = owner; mPathName = pathName; mMessage = message; diff --git a/SourceGenWPF/ProjWin/DiscardChanges.xaml.cs b/SourceGenWPF/ProjWin/DiscardChanges.xaml.cs index 6652875..191dcc1 100644 --- a/SourceGenWPF/ProjWin/DiscardChanges.xaml.cs +++ b/SourceGenWPF/ProjWin/DiscardChanges.xaml.cs @@ -31,8 +31,9 @@ namespace SourceGenWPF.ProjWin { } public Choice UserChoice { get; private set; } - public DiscardChanges() { + public DiscardChanges(Window owner) { InitializeComponent(); + Owner = owner; } // TODO: diff --git a/SourceGenWPF/ProjWin/EditAddress.xaml.cs b/SourceGenWPF/ProjWin/EditAddress.xaml.cs index 612285e..3331447 100644 --- a/SourceGenWPF/ProjWin/EditAddress.xaml.cs +++ b/SourceGenWPF/ProjWin/EditAddress.xaml.cs @@ -14,10 +14,7 @@ * limitations under the License. */ using System; -using System.ComponentModel; using System.Diagnostics; -using System.Globalization; -using System.Runtime.CompilerServices; using System.Windows; using System.Windows.Controls; @@ -43,7 +40,7 @@ namespace SourceGenWPF.ProjWin { public string AddressText { get; set; } - public EditAddress(int initialAddr, int maxAddressValue) { + public EditAddress(Window owner, int initialAddr, int maxAddressValue) { // Set the property before initializing the window -- we don't have a property // change notifier. Address = -2; @@ -52,6 +49,7 @@ namespace SourceGenWPF.ProjWin { this.DataContext = this; InitializeComponent(); + Owner = owner; } private void OkButton_Click(object sender, RoutedEventArgs e) { diff --git a/SourceGenWPF/ProjWin/ProjectLoadIssue.xaml.cs b/SourceGenWPF/ProjWin/ProjectLoadIssue.xaml.cs index 5c59abd..24f15a7 100644 --- a/SourceGenWPF/ProjWin/ProjectLoadIssue.xaml.cs +++ b/SourceGenWPF/ProjWin/ProjectLoadIssue.xaml.cs @@ -36,8 +36,9 @@ namespace SourceGenWPF.ProjWin { } - public ProjectLoadIssues(string msgs, Buttons allowedButtons) { + public ProjectLoadIssues(Window owner, string msgs, Buttons allowedButtons) { InitializeComponent(); + Owner = owner; mMessages = msgs; mAllowedButtons = allowedButtons;