1
0
mirror of https://github.com/fadden/6502bench.git synced 2024-05-31 22:41:37 +00:00

Open damaged projects in read-only mode

If we detect a problem that requires intervention during loading,
e.g. we find unknown elements because we're loading a file created
by a newer version, default to read-only mode.

Read only mode (1) refuses to apply changes, (2) refuses to add
changes to the undo/redo list, and (3) disables Save/SaveAs.  The
mode is indicated in the title bar.

Also, flipped the order of items in the title bar so that "6502bench
SourceGen" comes last.  This allows you to read the project name in
short window title snippets.  (Visual Studio, Notepad, and others
do it this way as well.)
This commit is contained in:
Andy McFadden 2019-10-26 13:07:31 -07:00
parent 6344ea59bb
commit d99eec0d4f
9 changed files with 84 additions and 17 deletions

View File

@ -120,6 +120,11 @@ namespace SourceGen {
/// </summary>
public CpuDef CpuDef { get; private set; }
/// <summary>
/// If set, changes are ignored.
/// </summary>
public bool IsReadOnly { get; set; }
/// <summary>
/// If true, plugins will execute in the main application's AppDomain instead of
/// the sandbox. Must be set before calling Initialize().
@ -1785,6 +1790,9 @@ namespace SourceGen {
/// </summary>
/// <param name="changeSet">Set to push.</param>
public void PushChangeSet(ChangeSet changeSet) {
if (IsReadOnly) {
return;
}
Debug.WriteLine("PushChangeSet: adding " + changeSet);
// Remove all of the "redo" entries from the current position to the end.
@ -1850,6 +1858,9 @@ namespace SourceGen {
public UndoableChange.ReanalysisScope ApplyChanges(ChangeSet cs, bool backward,
out RangeSet affectedOffsets) {
affectedOffsets = new RangeSet();
if (IsReadOnly) {
return UndoableChange.ReanalysisScope.None;
}
UndoableChange.ReanalysisScope needReanalysis = UndoableChange.ReanalysisScope.None;

View File

@ -626,9 +626,7 @@ namespace SourceGen {
private void UpdateTitle() {
// Update main window title.
StringBuilder sb = new StringBuilder();
sb.Append(Res.Strings.TITLE_BASE);
if (mProject != null) {
sb.Append(" - ");
if (string.IsNullOrEmpty(mProjectPathName)) {
sb.Append(Res.Strings.TITLE_NEW_PROJECT);
} else {
@ -639,7 +637,14 @@ namespace SourceGen {
sb.Append(" ");
sb.Append(Res.Strings.TITLE_MODIFIED);
}
if (mProject.IsReadOnly) {
sb.Append(" ");
sb.Append(Res.Strings.TITLE_READ_ONLY);
}
sb.Append(" - ");
}
sb.Append(Res.Strings.TITLE_BASE);
mMainWin.Title = sb.ToString();
}
@ -1082,6 +1087,8 @@ namespace SourceGen {
if (ok != true) {
return;
}
newProject.IsReadOnly = dlg.WantReadOnly;
}
mProject = newProject;
@ -1175,6 +1182,7 @@ namespace SourceGen {
/// </summary>
/// <returns>True on success, false if the save attempt failed or was canceled.</returns>
public bool SaveProjectAs() {
Debug.Assert(!mProject.IsReadOnly);
SaveFileDialog fileDlg = new SaveFileDialog() {
Filter = ProjectFile.FILENAME_FILTER + "|" + Res.Strings.FILE_FILTER_ALL,
FilterIndex = 1,
@ -1205,6 +1213,7 @@ namespace SourceGen {
/// </summary>
/// <returns>True on success, false if the save attempt failed.</returns>
public bool SaveProject() {
Debug.Assert(!mProject.IsReadOnly);
if (string.IsNullOrEmpty(mProjectPathName)) {
return SaveProjectAs();
}
@ -1317,8 +1326,11 @@ namespace SourceGen {
return true;
}
public bool IsProjectOpen() {
return mProject != null;
public bool IsProjectOpen {
get { return mProject != null; }
}
public bool IsProjectReadOnly {
get { return mProject != null && mProject.IsReadOnly; }
}
public void AssembleProject() {

View File

@ -53,7 +53,7 @@ limitations under the License.
<system:String x:Key="str_ErrBadSymbolSt">Unknown Source or Type in symbol</system:String>
<system:String x:Key="str_ErrBadSymrefPart">Bad symbol reference part</system:String>
<system:String x:Key="str_ErrBadTypeHint">Type hint not recognized</system:String>
<system:String x:Key="str_ErrDuplicateLabelFmt">Removing duplicate label '{0}' (offset +{1:x6})</system:String>
<system:String x:Key="str_ErrDuplicateLabelFmt">Removed duplicate label '{0}' (offset +{1:x6})</system:String>
<system:String x:Key="str_ErrFileCopyFailedFmt">Failed copying {0} to {1}: {2}.</system:String>
<system:String x:Key="str_ErrFileExistsNotDirFmt">The file {0} exists, but is not a directory.</system:String>
<system:String x:Key="str_ErrFileGenericCaption">File Error</system:String>
@ -164,5 +164,6 @@ limitations under the License.
<system:String x:Key="str_TitleBase">6502bench SourceGen</system:String>
<system:String x:Key="str_TitleModified">(save needed)</system:String>
<system:String x:Key="str_TitleNewProject">[new project]</system:String>
<system:String x:Key="str_TitleReadOnly">*READ-ONLY*</system:String>
<system:String x:Key="str_Unset">[unset]</system:String>
</ResourceDictionary>

View File

@ -309,6 +309,8 @@ namespace SourceGen.Res {
(string)Application.Current.FindResource("str_TitleModified");
public static string TITLE_NEW_PROJECT =
(string)Application.Current.FindResource("str_TitleNewProject");
public static string TITLE_READ_ONLY =
(string)Application.Current.FindResource("str_TitleReadOnly");
public static string UNSET =
(string)Application.Current.FindResource("str_Unset");
}

View File

@ -54,8 +54,9 @@ shown. If it's something simple, like a missing .sym65 or extension
script file, you'll be notified. If it's something more complicated,
e.g. the project has a comment on an offset that doesn't exist, you
will be warned that the problematic data has been deleted, and will be
lost if the project is saved. You will also be given the opportunity
to cancel the loading of the project.</p>
lost if the project is saved. By default, such a project will be opened
in read-only mode, though you can override this in the dialog. You will
also be given the opportunity to simply cancel loading the project.</p>
<p>The locations of the last few projects you've worked with are saved
in the application settings. You can access them from

View File

@ -264,9 +264,9 @@ limitations under the License.
<CommandBinding Command="{StaticResource RedoCmd}"
CanExecute="CanRedo" Executed="RedoCmd_Executed"/>
<CommandBinding Command="Save"
CanExecute="IsProjectOpen" Executed="SaveCmd_Executed"/>
CanExecute="CanSaveProject" Executed="SaveCmd_Executed"/>
<CommandBinding Command="SaveAs"
CanExecute="IsProjectOpen" Executed="SaveAsCmd_Executed"/>
CanExecute="CanSaveProject" Executed="SaveAsCmd_Executed"/>
<!-- ListView has a built-in Ctrl+A handler; this only fires when codeListView is not in focus -->
<CommandBinding Command="{StaticResource SelectAllCmd}"
CanExecute="IsProjectOpen" Executed="SelectAllCmd_Executed"/>

View File

@ -934,7 +934,7 @@ namespace SourceGen.WpfGui {
/// </summary>
/// <returns></returns>
private bool IsProjectOpen() {
return mMainCtrl != null && mMainCtrl.IsProjectOpen();
return mMainCtrl != null && mMainCtrl.IsProjectOpen;
}
/// <summary>
@ -1033,6 +1033,11 @@ namespace SourceGen.WpfGui {
(counts.mCodeHints != 0 || counts.mDataHints != 0 || counts.mInlineDataHints != 0);
}
private void CanSaveProject(object sender, CanExecuteRoutedEventArgs e) {
e.CanExecute = mMainCtrl != null && mMainCtrl.IsProjectOpen &&
!mMainCtrl.IsProjectReadOnly;
}
private void CanToggleSingleByteFormat(object sender, CanExecuteRoutedEventArgs e) {
e.CanExecute = IsProjectOpen() && mMainCtrl.CanToggleSingleByteFormat();
}

View File

@ -25,14 +25,23 @@ limitations under the License.
SizeToContent="WidthAndHeight" ResizeMode="NoResize"
ShowInTaskbar="False" WindowStartupLocation="CenterOwner"
Loaded="ProjectLoadIssues_Loaded">
<Window.Resources>
<BooleanToVisibilityConverter x:Key="BoolToVis"/>
</Window.Resources>
<DockPanel LastChildFill="False" Margin="8">
<TextBlock DockPanel.Dock="Top">Problems were detected while loading the project file:</TextBlock>
<TextBox Name="messageTextBox" DockPanel.Dock="Top" Margin="0,8,0,0"
Width="540" Height="110" IsReadOnly="True" TextWrapping="Wrap" Text=""
VerticalScrollBarVisibility="Auto"/>
<TextBlock Name="invalidDiscardLabel" DockPanel.Dock="Top" Margin="0,8,0,0" Foreground="Red">
<TextBlock Name="invalidDiscardLabel" DockPanel.Dock="Top" Margin="0,8,0,0" Foreground="Red"
Visibility="{Binding ShowItemWarning, Converter={StaticResource BoolToVis}}">
Invalid data items will be discarded when you save the project.
</TextBlock>
<CheckBox Content="Open project in read-only mode" DockPanel.Dock="Top" Margin="0,4,0,0"
IsChecked="{Binding WantReadOnly}"
Visibility="{Binding ShowSaveWarning, Converter={StaticResource BoolToVis}}"/>
<StackPanel DockPanel.Dock="Bottom" Margin="0,8,0,0"
Orientation="Horizontal" HorizontalAlignment="Right">
<Button Name="okButton" Width="70" Content="Continue" IsDefault="True" Click="OkButton_Click"/>

View File

@ -14,13 +14,28 @@
* limitations under the License.
*/
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Windows;
namespace SourceGen.WpfGui {
/// <summary>
/// Display errors and warnings generated while attempting to open a project.
/// </summary>
public partial class ProjectLoadIssues : Window {
public partial class ProjectLoadIssues : Window, INotifyPropertyChanged {
public bool WantReadOnly {
get { return mWantReadOnly; }
set { mWantReadOnly = value; OnPropertyChanged(); }
}
private bool mWantReadOnly;
public bool ShowSaveWarning {
get { return mShowItemWarning; }
set { mShowItemWarning = value; OnPropertyChanged(); }
}
private bool mShowItemWarning;
/// <summary>
/// Multi-line message for text box.
/// </summary>
@ -35,10 +50,17 @@ namespace SourceGen.WpfGui {
Unknown = 0, Continue, Cancel, ContinueOrCancel
}
// INotifyPropertyChanged implementation
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged([CallerMemberName] string propertyName = "") {
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public ProjectLoadIssues(Window owner, string msgs, Buttons allowedButtons) {
InitializeComponent();
Owner = owner;
DataContext = this;
mMessages = msgs;
mAllowedButtons = allowedButtons;
@ -52,14 +74,18 @@ namespace SourceGen.WpfGui {
okButton.IsEnabled = false;
// No point warning them about invalid data if they can't continue.
invalidDiscardLabel.Visibility = Visibility.Hidden;
}
if (mAllowedButtons == Buttons.Continue) {
ShowSaveWarning = false;
} else if (mAllowedButtons == Buttons.Continue) {
// Cancel not allowed.
cancelButton.IsEnabled = false;
// They're stuck with the problem.
invalidDiscardLabel.Visibility = Visibility.Hidden;
// Problem is outside the scope of the project (e.g. bad platform symbol file),
// so saving the project won't change anything.
ShowSaveWarning = false;
} else {
Debug.Assert(mAllowedButtons == Buttons.ContinueOrCancel);
ShowSaveWarning = true;
WantReadOnly = true;
}
}