Skip to content

Commit

Permalink
New feature: modify values in memory directly in the memory view
Browse files Browse the repository at this point in the history
  • Loading branch information
Baltasarq committed Dec 14, 2017
1 parent 2467c4e commit 7f70d67
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 12 deletions.
2 changes: 1 addition & 1 deletion C-Sim/Core/AppInfo.cs
Expand Up @@ -9,7 +9,7 @@ public static class AppInfo
/// <summary>Author of the app.</summary>
public const string Author = "jbgarcia@uvigo.es";
/// <summary>Version of the app.</summary>
public const string Version = "1.7.4 20171201";
public const string Version = "1.7.5 20171213";
/// <summary>Website for this app.</summary>
public const string Web = "http://webs.uvigo.es/jbgarcia/prys/csim/";
/// <summary>Extension for files created by this app.</summary>
Expand Down
48 changes: 48 additions & 0 deletions C-Sim/Ui/MainWindowCore.cs
Expand Up @@ -38,6 +38,7 @@ public partial class MainWindow: Form {
/// </summary>
public MainWindow(Machine m)
{
this.updatingMemoryView = false;
this.machine = m;
this.machine.Inputter = (msg) => this.DoUserInput( msg );
this.machine.Outputter = (string s) => this.rtbOutput.Text += s;
Expand Down Expand Up @@ -256,6 +257,49 @@ private void DoDisplayInHex()
Literal.Display = Literal.DisplayType.Hex;
this.UpdateView();
}

private void DoCellFormat(DataGridViewCellFormattingEventArgs e)
{
string val = (string) e.Value;

e.Value = val.ToLower();
e.FormattingApplied = true;
}

private void DoCellEdited(int rowIndex, int colIndex)
{
if ( !this.updatingMemoryView
&& colIndex > 0 ) // First row is for indexes
{
string val = (string)
this.grdMemory.Rows[ rowIndex ].
Cells[ colIndex ].Value;

if ( byte.TryParse(
val,
NumberStyles.HexNumber,
NumberFormatInfo.InvariantInfo,
out byte x ) )
{
int pos = ( rowIndex * MaxDataColumns ) + colIndex - 1;
this.machine.Memory.Write( pos, new []{ x } );
}
}

return;
}

private void DoCellValidate(DataGridViewCellValidatingEventArgs e)
{
int x = -1;
bool isHex = int.TryParse(
(string) e.FormattedValue,
NumberStyles.HexNumber,
NumberFormatInfo.InvariantInfo,
out x );

e.Cancel = ( !isHex || x < 0 || x >= byte.MaxValue );
}

/// <summary>
/// Updates the font in all visible controls.
Expand Down Expand Up @@ -586,6 +630,8 @@ private void UpdateMemoryView()
{
var memory = this.machine.Memory.Raw;

this.updatingMemoryView = true;

// Row indexes (first cell in each row)
for (int i = 0; i < this.grdMemory.RowCount; ++i) {
this.grdMemory.Rows[ i ].Cells[ 0 ].Value = FromIntToHex( i );
Expand All @@ -600,6 +646,7 @@ private void UpdateMemoryView()
this.grdMemory.Rows[ row ].Cells[ col ].ToolTipText = memory[ i ].ToString();
}

this.updatingMemoryView = false;
this.FocusOnInput();
return;
}
Expand Down Expand Up @@ -848,6 +895,7 @@ private static string FromIntToPrettyHex(BigInteger value, int wordSize = 1)
private Machine machine;
private SchemaDrawer sdDrawingBoard;
private string cfgFile = "";
private bool updatingMemoryView;

/// <summary>
/// The current dir, the last one from which a file was last loaded.
Expand Down
25 changes: 17 additions & 8 deletions C-Sim/Ui/MainWindowView.cs
Expand Up @@ -8,7 +8,7 @@ namespace CSim.Ui {

using Core;

/// <summary>The main window for the application. This builds the view.</summary>
/// <summary>The view builder for the application.</summary>
public partial class MainWindow {
private void BuildInputPanel()
{
Expand Down Expand Up @@ -131,25 +131,25 @@ private void BuildMemoryViewPanel()
BackColor = Color.Black,
ForeColor = Color.White
};

// Create columns
DataGridViewTextBoxColumn[] columns =
new DataGridViewTextBoxColumn[ MaxDataColumns +1 ];
var columns = new DataGridViewTextBoxColumn[ MaxDataColumns +1 ];

for(int i = 0; i < columns.Length; ++i) {
columns[ i ] = new DataGridViewTextBoxColumn();
columns[ i ] = new DataGridViewTextBoxColumn {
SortMode = DataGridViewColumnSortMode.NotSortable
};

if ( i == 0 ) {
columns[ i ].DefaultCellStyle = styleFirstColumn;
columns[ i ].HeaderText = "/";
columns[ i ].Width = columnWidth + ( (int) ( columnWidth * 0.2 ) );
columns[ i ].ReadOnly = true;
} else {
columns[ i ].HeaderText = FromIntToHex( i - 1 );
columns[ i ].Width = columnWidth;
columns[ i ].ReadOnly = false;
}

columns[ i ].SortMode = DataGridViewColumnSortMode.NotSortable;
columns[ i ].ReadOnly = true;
}

this.grdMemory.Columns.AddRange( columns );
Expand All @@ -162,6 +162,15 @@ private void BuildMemoryViewPanel()
}

this.grdMemory.Rows.AddRange( rows );
this.grdMemory.CellValueChanged +=
(object sender, DataGridViewCellEventArgs e)
=> this.DoCellEdited( e.RowIndex, e.ColumnIndex );
this.grdMemory.CellValidating +=
(object sender, DataGridViewCellValidatingEventArgs e)
=> this.DoCellValidate( e );
this.grdMemory.CellFormatting +=
(object sender, DataGridViewCellFormattingEventArgs e)
=> this.DoCellFormat( e );

this.grdMemory.Dock = DockStyle.Fill;
this.tcTabs.TabPages[ 0 ].Controls.Add( this.grdMemory );
Expand Down
6 changes: 3 additions & 3 deletions C-SimTests/C-SimTests.csproj
Expand Up @@ -43,14 +43,14 @@
<ItemGroup>
<Folder Include="C-SimTests\" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\C-Sim\C-Sim.csproj">
<Project>{7301AFF5-913F-46D4-A2AE-878100C67B5A}</Project>
<Name>C-Sim</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>

0 comments on commit 7f70d67

Please sign in to comment.