1
0
mirror of https://github.com/fadden/6502bench.git synced 2024-06-25 05:29:31 +00:00

Implement Toggle ASCII Chart

While I was at it, I noticed that the ASCII chart and hex dump
viewer windows were always in front of the main app window.  It looks
like child windows are always in front.  The easy fix is to not
set an owner.

This causes a new problem: the windows don't get closed automatically
when the parent window closes, and the app won't exit until all
windows are closed.  So we now explicitly close the hex dump and
ASCII chart windows when the main window is closed, and we now keep
track of all the external-file hex dump windows.  (It always sort of
bothered me that we were creating hex dump windows and not keeping
track of them.  Itch has now been scratched.)

Also, changed the ascch-mode setting to be an enum rather than an
integer.  Renamed the setting to ascch-mode1.
This commit is contained in:
Andy McFadden 2019-07-14 18:12:03 -07:00
parent 93dd5b41c6
commit 8a483f06c2
8 changed files with 279 additions and 10 deletions

View File

@ -94,7 +94,7 @@ namespace SourceGenWPF {
public const string HEXD_CHAR_CONV = "hexd-char-conv1";
// ASCII chart viewer settings.
public const string ASCCH_MODE = "ascch-mode";
public const string ASCCH_MODE = "ascch-mode1";
// Source generation settings.
public const string SRCGEN_DEFAULT_ASM = "srcgen-default-asm";

View File

@ -64,10 +64,27 @@ namespace SourceGenWPF {
private MainWindow mMainWin;
/// <summary>
/// Hex dump viewer window.
/// Hex dump viewer window. This is used for the currently open project.
/// </summary>
private Tools.WpfGui.HexDumpViewer mHexDumpDialog;
/// <summary>
/// This holds any un-owned Windows that we don't otherwise track. It's used for
/// hex dump windows of arbitrary files. We need to close them when the main window
/// is closed.
/// </summary>
private List<Window> mUnownedWindows = new List<Window>();
/// <summary>
/// ASCII chart reference window. Not tied to the project.
/// </summary>
private Tools.WpfGui.AsciiChart mAsciiChartDialog;
/// <summary>
/// Returns true if the ASCII chart window is currently open.
/// </summary>
public bool IsAsciiChartOpen { get { return mAsciiChartDialog != null; } }
/// <summary>
/// List of recently-opened projects.
/// </summary>
@ -1108,7 +1125,30 @@ namespace SourceGenWPF {
/// <returns>True if it's okay for the window to close, false to cancel it.</returns>
public bool WindowClosing() {
SaveAppSettings();
return CloseProject();
if (!CloseProject()) {
return false;
}
// WPF won't exit until all windows are closed, so any unowned windows need
// to be cleaned up here.
if (mAsciiChartDialog != null) {
mAsciiChartDialog.Close();
}
if (mHexDumpDialog != null) {
mHexDumpDialog.Close();
}
while (mUnownedWindows.Count > 0) {
int count = mUnownedWindows.Count;
mUnownedWindows[0].Close();
if (count == mUnownedWindows.Count) {
// Window failed to remove itself; this will cause an infinite loop.
// The user will have to close them manually.
Debug.Assert(false, "Failed to close window " + mUnownedWindows[0]);
break;
}
}
return true;
}
/// <summary>
@ -2500,18 +2540,26 @@ namespace SourceGenWPF {
return;
}
// Fire and forget.
Tools.WpfGui.HexDumpViewer dlg = new Tools.WpfGui.HexDumpViewer(mMainWin,
// Create the dialog without an owner, and add it to the "unowned" list.
Tools.WpfGui.HexDumpViewer dlg = new Tools.WpfGui.HexDumpViewer(null,
data, mOutputFormatter);
dlg.SetFileName(Path.GetFileName(fileName));
dlg.Closing += (sender, e) => {
Debug.WriteLine("Window " + dlg + " closed, removing from unowned list");
mUnownedWindows.Remove(dlg);
};
mUnownedWindows.Add(dlg);
dlg.Show();
}
public void ShowHexDump() {
if (mHexDumpDialog == null) {
// Create and show modeless dialog. This one is "always on top" by default,
// to allow the user to click around to various points.
mHexDumpDialog = new Tools.WpfGui.HexDumpViewer(mMainWin,
// to allow the user to click around to various points. Note that "on top"
// means on top of *everything*. We create this without an owner so that,
// when it's not on top, it can sit behind the main app window until you
// double-click something else.
mHexDumpDialog = new Tools.WpfGui.HexDumpViewer(null,
mProject.FileData, mOutputFormatter);
mHexDumpDialog.Closing += (sender, e) => {
Debug.WriteLine("Hex dump dialog closed");
@ -2633,6 +2681,20 @@ namespace SourceGenWPF {
dlg.ShowDialog();
}
public void ToggleAsciiChart() {
if (mAsciiChartDialog == null) {
// Create without owner so it doesn't have to be in front of main window.
mAsciiChartDialog = new Tools.WpfGui.AsciiChart(null);
mAsciiChartDialog.Closing += (sender, e) => {
Debug.WriteLine("ASCII chart closed");
mAsciiChartDialog = null;
};
mAsciiChartDialog.Show();
} else {
mAsciiChartDialog.Close();
}
}
public void ToggleDataScan() {
ProjectProperties oldProps = mProject.ProjectProps;
ProjectProperties newProps = new ProjectProperties(oldProps);

View File

@ -79,6 +79,9 @@
<DependentUpon>GenTestRunner.xaml</DependentUpon>
</Compile>
<Compile Include="Tools\VirtualHexDump.cs" />
<Compile Include="Tools\WpfGui\AsciiChart.xaml.cs">
<DependentUpon>AsciiChart.xaml</DependentUpon>
</Compile>
<Compile Include="WpfGui\AboutBox.xaml.cs">
<DependentUpon>AboutBox.xaml</DependentUpon>
</Compile>
@ -219,6 +222,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Tools\WpfGui\AsciiChart.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="WpfGui\AboutBox.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>

View File

@ -0,0 +1,45 @@
<!--
Copyright 2019 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.
-->
<Window x:Class="SourceGenWPF.Tools.WpfGui.AsciiChart"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:system="clr-namespace:System;assembly=mscorlib"
xmlns:local="clr-namespace:SourceGenWPF.Tools.WpfGui"
mc:Ignorable="d"
Title="ASCII Chart"
SizeToContent="WidthAndHeight" ResizeMode="NoResize"
ShowInTaskbar="True"
Loaded="Window_Loaded"
PreviewKeyDown="Window_KeyEventHandler">
<Window.Resources>
<system:String x:Key="str_Standard">Standard ASCII</system:String>
<system:String x:Key="str_High">High ASCII</system:String>
</Window.Resources>
<StackPanel Margin="8">
<!-- using a TextBox instead of a TextBlock so it's copyable -->
<TextBox Name="chartTextBox" IsReadOnly="True"
Text="Hex Dec Chr | Hex Dec Chr | Hex Dec Chr | Hex Dec Chr"
FontFamily="{StaticResource GeneralMonoFont}"/>
<ComboBox Name="chartModeComboBox" Width="120" HorizontalAlignment="Left" Margin="0,4,0,0"
ItemsSource="{Binding ChartModeItems}" DisplayMemberPath="Name"
SelectionChanged="ChartModeComboBox_SelectionChanged"/>
</StackPanel>
</Window>

View File

@ -0,0 +1,144 @@
/*
* Copyright 2019 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.Diagnostics;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace SourceGenWPF.Tools.WpfGui {
/// <summary>
/// ASCII chart.
/// </summary>
public partial class AsciiChart : Window {
public enum ChartMode {
Unknown = 0,
Standard,
High
};
public class ChartModeItem {
public string Name { get; private set; }
public ChartMode Mode { get; private set; }
public ChartModeItem(string name, ChartMode mode) {
Name = name;
Mode = mode;
}
}
public ChartModeItem[] ChartModeItems { get; private set; }
public AsciiChart(Window owner) {
InitializeComponent();
Owner = owner;
DataContext = this;
ChartModeItems = new ChartModeItem[] {
new ChartModeItem((string)FindResource("str_Standard"), ChartMode.Standard),
new ChartModeItem((string)FindResource("str_High"), ChartMode.High),
};
}
public void Window_Loaded(object sender, RoutedEventArgs e) {
// Restore chart mode setting.
ChartMode mode = (ChartMode)AppSettings.Global.GetEnum(
AppSettings.ASCCH_MODE, typeof(ChartMode), (int)ChartMode.Standard);
int index = 0;
for (int i = 0; i < ChartModeItems.Length; i++) {
if (ChartModeItems[i].Mode == mode) {
index = i;
break;
}
}
chartModeComboBox.SelectedIndex = index;
// should call UpdateControls via SelectionChanged
}
// Catch ESC key.
private void Window_KeyEventHandler(object sender, KeyEventArgs e) {
if (e.Key == Key.Escape) {
Close();
}
}
private void ChartModeComboBox_SelectionChanged(object sender,
SelectionChangedEventArgs e) {
UpdateControls();
}
private void UpdateControls() {
ChartModeItem item = (ChartModeItem)chartModeComboBox.SelectedItem;
if (item == null) {
// initializing
return;
}
AppSettings.Global.SetEnum(AppSettings.ASCCH_MODE, typeof(ChartMode), (int)item.Mode);
//
// Draw box contents.
//
const string hdr = "Dec Hex Chr";
const string div = " | ";
const string eol = "\r\n";
StringBuilder sb = new StringBuilder(
(hdr.Length * 4 + div.Length * 3 + eol.Length) * 32);
sb.Append(hdr);
sb.Append(div);
sb.Append(hdr);
sb.Append(div);
sb.Append(hdr);
sb.Append(div);
sb.Append(hdr);
sb.Append(eol);
for (int i = 0; i < 32; i++) {
DrawEntry(item.Mode, i, sb);
sb.Append(div);
DrawEntry(item.Mode, i + 32, sb);
sb.Append(div);
DrawEntry(item.Mode, i + 64, sb);
sb.Append(div);
DrawEntry(item.Mode, i + 96, sb);
sb.Append(eol);
}
chartTextBox.Text = sb.ToString();
chartTextBox.SelectionStart = sb.Length;
chartTextBox.SelectionLength = 0;
}
private void DrawEntry(ChartMode mode, int val, StringBuilder sb) {
// Format is: Dec Hex Chr
int modVal = (mode == ChartMode.High) ? val | 0x80 : val;
sb.AppendFormat("{0,3:D} {1,3:X2} ", modVal, modVal);
if (val < 0x20) {
sb.Append('^');
sb.Append((char)(val + 0x40));
sb.Append(' ');
} else if (val == 0x20) {
sb.Append("' '");
} else if (val < 0x7f) {
sb.Append(' ');
sb.Append((char)val);
sb.Append(' ');
} else {
sb.Append("DEL");
}
}
}
}

View File

@ -25,7 +25,7 @@ limitations under the License.
Title="Hex Dump Viewer"
Icon="/SourceGenWPF;component/Res/SourceGenIcon.ico"
Width="542" Height="600" MinWidth="542" MinHeight="180"
ShowInTaskbar="False" WindowStartupLocation="CenterOwner"
ShowInTaskbar="True"
Loaded="Window_Loaded">
<Window.Resources>

View File

@ -140,6 +140,7 @@ limitations under the License.
</RoutedUICommand>
<RoutedUICommand x:Key="ShowFileHexDumpCmd" Text="Hex Dump..."/>
<RoutedUICommand x:Key="ShowHexDumpCmd" Text="Show Hex Dump"/>
<RoutedUICommand x:Key="ToggleAsciiChartCmd" Text="ASCII Chart"/>
<RoutedUICommand x:Key="ToggleDataScanCmd" Text="Toggle Data Scan">
<RoutedUICommand.InputGestures>
<KeyGesture>Ctrl+D</KeyGesture>
@ -241,6 +242,8 @@ limitations under the License.
Executed="ShowFileHexDumpCmd_Executed"/>
<CommandBinding Command="{StaticResource ShowHexDumpCmd}"
CanExecute="IsProjectOpen" Executed="ShowHexDumpCmd_Executed"/>
<CommandBinding Command="{StaticResource ToggleAsciiChartCmd}"
Executed="ToggleAsciiChartCmd_Executed"/>
<CommandBinding Command="{StaticResource ToggleDataScanCmd}"
CanExecute="IsProjectOpen" Executed="ToggleDataScanCmd_Executed"/>
<CommandBinding Command="{StaticResource ToggleSingleByteFormatCmd}"
@ -308,9 +311,10 @@ limitations under the License.
<Separator/>
<MenuItem Command="{StaticResource ShowHexDumpCmd}"/>
</MenuItem>
<MenuItem Header="_Tools">
<MenuItem Header="_Tools" SubmenuOpened="ToolsMenu_SubmenuOpened">
<MenuItem Command="{StaticResource ShowFileHexDumpCmd}"/>
<MenuItem Header="ASCII Chart"/>
<MenuItem Name="toggleAsciiChartMenuItem"
Command="{StaticResource ToggleAsciiChartCmd}" IsCheckable="True"/>
</MenuItem>
<MenuItem Header="_Help">
<MenuItem Command="Help"/>

View File

@ -1083,6 +1083,10 @@ namespace SourceGenWPF.WpfGui {
mMainCtrl.ShowHexDump();
}
private void ToggleAsciiChartCmd_Executed(object sender, ExecutedRoutedEventArgs e) {
mMainCtrl.ToggleAsciiChart();
}
private void ToggleDataScanCmd_Executed(object sender, ExecutedRoutedEventArgs e) {
mMainCtrl.ToggleDataScan();
}
@ -1175,6 +1179,9 @@ namespace SourceGenWPF.WpfGui {
// it rather than try to push changes around.
toggleDataScanMenuItem.IsChecked = mMainCtrl.IsAnalyzeUncategorizedDataEnabled;
}
private void ToolsMenu_SubmenuOpened(object sender, RoutedEventArgs e) {
toggleAsciiChartMenuItem.IsChecked = mMainCtrl.IsAsciiChartOpen;
}
#endregion Misc