Skip to content

Commit

Permalink
Implement marker editing
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterTh committed Sep 27, 2015
1 parent 7690ca2 commit aae9612
Show file tree
Hide file tree
Showing 10 changed files with 247 additions and 65 deletions.
33 changes: 13 additions & 20 deletions AddressInspector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,14 @@ public string Caption

private bool editable = true;
[Description("Is value editable?"), Category("Hex")]
public bool Editable {
public bool Editable
{
get { return editable; }
set
{
editable = value;
foreach(var c in Controls)
{
if(c is TextBox && c != addressTextBox)
{
foreach(var c in Controls) {
if(c is TextBox && c != addressTextBox) {
var ct = c as TextBox;
ct.ReadOnly = !editable;
}
Expand All @@ -60,11 +59,9 @@ public DataFragment Target

private void RecomputeStrings()
{
using (new SuspendDrawing(this))
{
using(new SuspendDrawing(this)) {
addressTextBox.Text = DataType.AddressToString(target.Address);
foreach (var dt in DataType.GetKnownDataTypes())
{
foreach(var dt in DataType.GetKnownDataTypes()) {
var tb = Controls[dt.Name + "TextBox"] as TextBox;
tb.Text = dt.DecodeToString(target);
}
Expand All @@ -75,14 +72,12 @@ private void RecomputeStrings()
public AddressInspector()
{
InitializeComponent();

int y = 29;
int lblX = 5;
int textBoxX = 50;
foreach (var dt in DataType.GetKnownDataTypesAndSeperators())
{
if (dt.Seperator)
{
foreach(var dt in DataType.GetKnownDataTypesAndSeperators()) {
if(dt.Seperator) {
var lbl = new Label();
lbl.AutoSize = true;
lbl.Font = new System.Drawing.Font("Microsoft Sans Serif", 6.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
Expand All @@ -94,8 +89,7 @@ public AddressInspector()
Controls.Add(lbl);
y += 15;
}
else
{
else {
// text box
var textBox = new TextBox();
textBox.Anchor |= System.Windows.Forms.AnchorStyles.Right;
Expand All @@ -109,9 +103,9 @@ public AddressInspector()
// label
var lbl = new Label();
lbl.AutoSize = false;
lbl.Location = new System.Drawing.Point(lblX, y+3);
lbl.Location = new System.Drawing.Point(lblX, y + 3);
lbl.Name = dt.Name + "Label";
lbl.Size = new System.Drawing.Size(textBoxX-lblX-4, 13);
lbl.Size = new System.Drawing.Size(textBoxX - lblX - 4, 13);
lbl.Text = dt.Name;
lbl.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
Controls.Add(lbl);
Expand All @@ -122,8 +116,7 @@ public AddressInspector()

private void TextBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
if(e.KeyCode == Keys.Enter) {
var tb = sender as TextBox;
var dt = tb.Tag as DataType;

Expand Down
9 changes: 8 additions & 1 deletion DataType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
Expand Down Expand Up @@ -59,7 +60,13 @@ public string DecodeToString(DataFragment target)

public DataFragment EncodeString(int address, string str)
{
return encoder(address, str);
DataFragment df = new DataFragment();
try {
df = encoder(address, str);
} catch(Exception) {
MessageBox.Show("Error converting string \"" + str + "\" to type " + Name, "Conversion Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
return df;
}

public static DataType CreateSeparator(string caption)
Expand Down
13 changes: 8 additions & 5 deletions HexView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,15 @@ private int StartLine
private int visibleLines = 1;
private Interval<int> visibleAddresses = new Interval<int>(0, 0);

private string fileName;
private string fileName = "";
[Description("Hex file name"), Category("Hex")]
public string FileName
{
get { return fileName; }
set
{
fileName = value;
if(value != null) fileName = value;
else fileName = "";
if(fileName.Length > 0) LoadFile(fileName);
}
}
Expand Down Expand Up @@ -348,7 +349,7 @@ public DataFragment GetDataAt(int address)
}
public DataFragment GetSelectedData() { return GetDataAt(SelectedAddress); }
public DataFragment GetHoverData() { return GetDataAt(HoverAddress); }

public void NavigateToAddress(int address)
{
if(address < 0) address = 0;
Expand Down Expand Up @@ -394,8 +395,10 @@ internal bool Search(DataFragment toSearch)

internal void ApplyEdit(DataFragment data)
{
Array.Copy(data.Data, 0, fileBytes, data.Address / 8, data.Length);
NavigateToAddress();
if(data.Length > 0) {
Array.Copy(data.Data, 0, fileBytes, data.Address / 8, data.Length);
NavigateToAddress();
}
}
}
}
37 changes: 20 additions & 17 deletions MainForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 11 additions & 21 deletions MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,11 @@ private void SelectedAddressInspector_DataChanged(DataFragment data)
#region Mousewheel
public bool PreFilterMessage(ref Message m)
{
if (m.Msg == WM_MOUSEWHEEL)
{
if(m.Msg == WM_MOUSEWHEEL) {
// WM_MOUSEWHEEL, find the control at screen position m.LParam
var hWnd = WindowFromPoint(Cursor.Position);

if (hWnd != IntPtr.Zero && hWnd != m.HWnd && Control.FromHandle(hWnd) != null)
{
if(hWnd != IntPtr.Zero && hWnd != m.HWnd && Control.FromHandle(hWnd) != null) {
SendMessage(hWnd, WM_MOUSEWHEEL, m.WParam, m.LParam);
return true;
}
Expand All @@ -55,8 +53,7 @@ public bool PreFilterMessage(ref Message m)
private void goToToolStripMenuItem_Click(object sender, EventArgs e)
{
var gt = new GoToForm();
if (gt.ShowDialog() == DialogResult.OK)
{
if(gt.ShowDialog() == DialogResult.OK) {
hexView.NavigateToAddress(gt.Address);
}
}
Expand All @@ -68,14 +65,11 @@ private void goToSelectedToolStripMenuItem_Click(object sender, EventArgs e)
private void searchToolStripMenuItem_Click(object sender, EventArgs e)
{
var sf = new SearchForm();
if (sf.ShowDialog() == DialogResult.OK)
{
if (hexView.Search(sf.ToSearch))
{
if(sf.ShowDialog() == DialogResult.OK) {
if(hexView.Search(sf.ToSearch)) {
statusStrip.Text = "Found search data";
}
else
{
else {
statusStrip.Text = "No occurance found";
}
}
Expand All @@ -96,17 +90,15 @@ private void hexView_HoverAddressChanged(object sender, EventArgs e)
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
var fd = new OpenFileDialog();
if (fd.ShowDialog() == DialogResult.OK)
{
if(fd.ShowDialog() == DialogResult.OK) {
hexView.FileName = fd.FileName;
}
}
private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
{
var sfd = new SaveFileDialog();
sfd.FileName = hexView.FileName;
if(sfd.ShowDialog() == DialogResult.OK)
{
if(sfd.ShowDialog() == DialogResult.OK) {
hexView.SaveToFile(sfd.FileName);
toolStripStatusLabel.Text = "Saved to " + sfd.FileName;
}
Expand All @@ -129,8 +121,7 @@ private void openMarkersToolStripMenuItem_Click(object sender, EventArgs e)
{
var ofd = new OpenFileDialog();
ConfigureMarkersDialog(ofd);
if (ofd.ShowDialog() == DialogResult.OK)
{
if(ofd.ShowDialog() == DialogResult.OK) {
MarkerRepository.Instance.LoadFromFile(ofd.FileName);
hexView.Refresh();
toolStripStatusLabel.Text = "Loaded Markers from " + ofd.FileName;
Expand All @@ -140,15 +131,14 @@ private void saveMarkersAsToolStripMenuItem_Click(object sender, EventArgs e)
{
var sfd = new SaveFileDialog();
ConfigureMarkersDialog(sfd);
if (sfd.ShowDialog() == DialogResult.OK)
{
if(sfd.ShowDialog() == DialogResult.OK) {
MarkerRepository.Instance.SaveToFile(sfd.FileName);
toolStripStatusLabel.Text = "Saved Markers to " + sfd.FileName;
}
}
private void saveMarkersToolStripMenuItem_Click(object sender, EventArgs e)
{
if (MarkerRepository.Instance.HasFileName()) MarkerRepository.Instance.SaveToFile();
if(MarkerRepository.Instance.HasFileName()) MarkerRepository.Instance.SaveToFile();
else saveMarkersAsToolStripMenuItem_Click(sender, e);
}
#endregion
Expand Down
Loading

0 comments on commit aae9612

Please sign in to comment.