1
0
mirror of https://github.com/fadden/6502bench.git synced 2024-07-30 15:29:01 +00:00

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.
This commit is contained in:
Andy McFadden 2019-06-07 14:03:34 -07:00
parent 0f7e0e8d21
commit fdbd5b89e9
3 changed files with 81 additions and 12 deletions

View File

@ -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.
/// </summary>
private void DoOpen() {
if (!DoClose()) {
if (!CloseProject()) {
return;
}
@ -609,7 +609,7 @@ namespace SourceGenWPF {
/// opportunity to cancel.
/// </summary>
/// <returns>True if the project was closed, false if the user chose to cancel.</returns>
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

View File

@ -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">
<Window.Resources>
@ -45,6 +45,7 @@ limitations under the License.
<KeyGesture>Ctrl+Shift+A</KeyGesture>
</RoutedUICommand.InputGestures>
</RoutedUICommand>
<RoutedUICommand x:Key="CloseCmd" Text="Close"/>
<RoutedUICommand x:Key="HintAsCodeEntryPoint" Text="Hint As Code Entry Point"/>
<RoutedUICommand x:Key="HintAsDataStart" Text="Hint As Data Start"/>
<RoutedUICommand x:Key="HintAsInlineData" Text="Hint As Inline Data"/>
@ -59,14 +60,22 @@ limitations under the License.
</Window.Resources>
<Window.CommandBindings>
<CommandBinding Command="{StaticResource AssembleCmd}" Executed="AssembleCmd_Executed"/>
<CommandBinding Command="{StaticResource HintAsCodeEntryPoint}" Executed="HintAsCodeEntryPoint_Executed"/>
<CommandBinding Command="{StaticResource HintAsDataStart}" Executed="HintAsDataStart_Executed"/>
<CommandBinding Command="{StaticResource HintAsInlineData}" Executed="HintAsInlineData_Executed"/>
<CommandBinding Command="{StaticResource RemoveHints}" Executed="RemoveHints_Executed"/>
<CommandBinding Command="{StaticResource AssembleCmd}"
CanExecute="IsProjectOpen" Executed="AssembleCmd_Executed"/>
<CommandBinding Command="{StaticResource CloseCmd}"
CanExecute="IsProjectOpen" Executed="CloseCmd_Executed"/>
<CommandBinding Command="{StaticResource HintAsCodeEntryPoint}"
CanExecute="IsProjectOpen" Executed="HintAsCodeEntryPoint_Executed"/>
<CommandBinding Command="{StaticResource HintAsDataStart}"
CanExecute="IsProjectOpen" Executed="HintAsDataStart_Executed"/>
<CommandBinding Command="{StaticResource HintAsInlineData}"
CanExecute="IsProjectOpen" Executed="HintAsInlineData_Executed"/>
<CommandBinding Command="{StaticResource RemoveHints}"
CanExecute="IsProjectOpen" Executed="RemoveHints_Executed"/>
<CommandBinding Command="{StaticResource RecentProjectCmd}" Executed="RecentProjectCmd_Executed"/>
<!-- ListView has a built-in Ctrl+A handler; this only fires when codeListView not in focus -->
<CommandBinding Command="{StaticResource SelectAllCmd}" Executed="SelectAllCmd_Executed"/>
<CommandBinding Command="{StaticResource SelectAllCmd}"
CanExecute="IsProjectOpen" Executed="SelectAllCmd_Executed"/>
</Window.CommandBindings>
<DockPanel>
@ -76,7 +85,7 @@ limitations under the License.
<MenuItem Header="Open"/>
<MenuItem Command="Save"/>
<MenuItem Command="SaveAs"/>
<MenuItem Command="Close"/>
<MenuItem Command="{StaticResource CloseCmd}"/>
<Separator/>
<MenuItem Command="{StaticResource AssembleCmd}"/>
<MenuItem Command="Print"/>
@ -104,7 +113,7 @@ limitations under the License.
<Separator/>
<MenuItem Header="Settings..."/>
</MenuItem>
<MenuItem Header="_Actions">
<MenuItem Name="ActionsMenu" Header="_Actions">
<MenuItem Header="Set Address..."/>
<MenuItem Header="Override Status Flags..."/>
<MenuItem Header="Edit Label..."/>
@ -259,6 +268,11 @@ limitations under the License.
</GridView>
</ListView.View>
<ListView.ContextMenu>
<!-- this is populated as a clone of the Actions menu during init -->
<ContextMenu/>
</ListView.ContextMenu>
</ListView>
<Grid Name="rightPanel" Grid.Column="4">

View File

@ -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);
}
}
}
/// <summary>
/// INotifyPropertyChanged event
/// </summary>
@ -162,11 +200,24 @@ namespace SourceGenWPF.ProjWin {
get { return App.ProgramVersion.ToString(); }
}
/// <summary>
/// Returns true if the project is open. Intended for use in XAML CommandBindings.
/// </summary>
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");
}