From fdbd5b89e9c16787bac7414affd6839cff0083cf Mon Sep 17 00:00:00 2001 From: Andy McFadden Date: Fri, 7 Jun 2019 14:03:34 -0700 Subject: [PATCH] More menu stuff Create context menu as a clone of the Actions menu. This is a bit easier than it was in WinForms because the ICommand stuff provides common routing and enable/disable logic for all instances. (It's one of the few places where WPF has made my life easier. Yay WPF.) Added CanExecute tests to existing items. Currently they just check to see if the project is open. Wired up File > Close. --- SourceGenWPF/MainController.cs | 10 +++-- SourceGenWPF/ProjWin/MainWindow.xaml | 32 +++++++++++----- SourceGenWPF/ProjWin/MainWindow.xaml.cs | 51 +++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 12 deletions(-) diff --git a/SourceGenWPF/MainController.cs b/SourceGenWPF/MainController.cs index 7b714d0..90e8d00 100644 --- a/SourceGenWPF/MainController.cs +++ b/SourceGenWPF/MainController.cs @@ -358,7 +358,7 @@ namespace SourceGenWPF { public void OpenRecentProject(int projIndex) { - if (!DoClose()) { + if (!CloseProject()) { return; } //DoOpenFile(mRecentProjectPaths[projIndex]); @@ -373,7 +373,7 @@ namespace SourceGenWPF { /// Handles opening an existing project by letting the user select the project file. /// private void DoOpen() { - if (!DoClose()) { + if (!CloseProject()) { return; } @@ -609,7 +609,7 @@ namespace SourceGenWPF { /// opportunity to cancel. /// /// True if the project was closed, false if the user chose to cancel. - private bool DoClose() { + public bool CloseProject() { Debug.WriteLine("ProjectView.DoClose() - dirty=" + (mProject == null ? "N/A" : mProject.IsDirty.ToString())); if (mProject != null && mProject.IsDirty) { @@ -667,6 +667,10 @@ namespace SourceGenWPF { return true; } + public bool IsProjectOpen() { + return mProject != null; + } + #region Project management diff --git a/SourceGenWPF/ProjWin/MainWindow.xaml b/SourceGenWPF/ProjWin/MainWindow.xaml index fc7f655..8a28c0c 100644 --- a/SourceGenWPF/ProjWin/MainWindow.xaml +++ b/SourceGenWPF/ProjWin/MainWindow.xaml @@ -22,7 +22,7 @@ limitations under the License. mc:Ignorable="d" Title="6502bench SourceGen" Icon="/SourceGenWPF;component/Res/SourceGenIcon.ico" - Width="810" Height="510" MinWidth="800" MinHeight="500" + Width="1000" Height="600" MinWidth="800" MinHeight="500" Loaded="Window_Loaded"> @@ -45,6 +45,7 @@ limitations under the License. Ctrl+Shift+A + @@ -59,14 +60,22 @@ limitations under the License. - - - - - + + + + + + - + @@ -76,7 +85,7 @@ limitations under the License. - + @@ -104,7 +113,7 @@ limitations under the License. - + @@ -259,6 +268,11 @@ limitations under the License. + + + + + diff --git a/SourceGenWPF/ProjWin/MainWindow.xaml.cs b/SourceGenWPF/ProjWin/MainWindow.xaml.cs index bb90350..310573e 100644 --- a/SourceGenWPF/ProjWin/MainWindow.xaml.cs +++ b/SourceGenWPF/ProjWin/MainWindow.xaml.cs @@ -100,6 +100,7 @@ namespace SourceGenWPF.ProjWin { private void Window_Loaded(object sender, RoutedEventArgs e) { mMainCtrl.WindowLoaded(); + CreateCodeListContextMenu(); #if DEBUG // Get more info on CollectionChanged events that do not agree with current @@ -109,6 +110,43 @@ namespace SourceGenWPF.ProjWin { #endif } + private void CreateCodeListContextMenu() { + // Find Actions menu. + ItemCollection mainItems = this.appMenu.Items; + MenuItem actionsMenu = null; + foreach (object obj in mainItems) { + if (!(obj is MenuItem)) { + continue; + } + MenuItem mi = (MenuItem)obj; + if (mi.Name.Equals("ActionsMenu")) { + actionsMenu = mi; + break; + } + } + Debug.Assert(actionsMenu != null); + + // Clone the Actions menu into the codeListView context menu. + ContextMenu ctxt = this.codeListView.ContextMenu; + foreach (object item in actionsMenu.Items) { + if (item is MenuItem) { + MenuItem oldItem = (MenuItem)item; + MenuItem newItem = new MenuItem(); + // I don't see a "clone" method, so just copy the fields we think we care about + newItem.Name = oldItem.Name; + newItem.Header = oldItem.Header; + newItem.Icon = oldItem.Icon; + newItem.InputGestureText = oldItem.InputGestureText; + newItem.Command = oldItem.Command; + ctxt.Items.Add(newItem); + } else if (item is Separator) { + ctxt.Items.Add(new Separator()); + } else { + Debug.Assert(false, "Found weird thing in menu: " + item); + } + } + } + /// /// INotifyPropertyChanged event /// @@ -162,11 +200,24 @@ namespace SourceGenWPF.ProjWin { get { return App.ProgramVersion.ToString(); } } + /// + /// Returns true if the project is open. Intended for use in XAML CommandBindings. + /// + private void IsProjectOpen(object sender, CanExecuteRoutedEventArgs e) { + e.CanExecute = mMainCtrl.IsProjectOpen(); + } + private void AssembleCmd_Executed(object sender, ExecutedRoutedEventArgs e) { // test Debug.WriteLine("assembling"); } + private void CloseCmd_Executed(object sender, ExecutedRoutedEventArgs e) { + if (!mMainCtrl.CloseProject()) { + Debug.WriteLine("Close canceled"); + } + } + private void HintAsCodeEntryPoint_Executed(object sender, ExecutedRoutedEventArgs e) { Debug.WriteLine("hint as code entry point"); }