From 8d0edb13bb7d3bf80a0ee56914e8da7fbf9a6689 Mon Sep 17 00:00:00 2001 From: Michael Becker Date: Fri, 1 Nov 2019 07:04:19 -0400 Subject: [PATCH] implement start/end/length seeking and endianness --- .../Editors/Binary/BinaryEditor.cs | 745 +++++++++++------- 1 file changed, 464 insertions(+), 281 deletions(-) diff --git a/CSharp/Libraries/UniversalEditor.UserInterface/Editors/Binary/BinaryEditor.cs b/CSharp/Libraries/UniversalEditor.UserInterface/Editors/Binary/BinaryEditor.cs index a5434ae8..208bea7c 100644 --- a/CSharp/Libraries/UniversalEditor.UserInterface/Editors/Binary/BinaryEditor.cs +++ b/CSharp/Libraries/UniversalEditor.UserInterface/Editors/Binary/BinaryEditor.cs @@ -65,6 +65,16 @@ namespace UniversalEditor.Editors.Binary private ListView lvFieldDefinitions; private DefaultTreeModel tmFieldDefinitions; + private Container pnlSettings; + private Label lblEndianness; + private ComboBox cboEndianness; + private Label lblStart; + private TextBox txtStart; + private Label lblEnd; + private TextBox txtEnd; + private Label lblLength; + private TextBox txtLength; + private struct CONVERSION_DATA { public string Label; @@ -85,293 +95,30 @@ namespace UniversalEditor.Editors.Binary } } - private CONVERSION_DATA[] converters = new CONVERSION_DATA[] + private CONVERSION_DATA[] converters = null; + + private byte[] ClampWithEndianness(byte[] input, int length) { - new CONVERSION_DATA(typeof(sbyte), "Signed 8-bit", delegate(byte[] input) - { - if (input.Length < 1) - return null; + if (input.Length < length) + return input; - sbyte b = (sbyte)input[0]; - return b.ToString(); - }, delegate(string input) + byte[] output = new byte[length]; + for (int i = 0; i < length; i++) { - System.Globalization.NumberStyles ns = System.Globalization.NumberStyles.None; - if (input.StartsWith("&H") && input.EndsWith("&")) - { - input = input.Substring(2, input.Length - 3); - ns |= System.Globalization.NumberStyles.HexNumber; - } - else if (input.ToLower().StartsWith("0x")) - { - input = input.Substring(2); - ns |= System.Globalization.NumberStyles.HexNumber; - } - sbyte b = SByte.Parse(input, ns); - return new byte[] { (byte)b }; - }, 1), - new CONVERSION_DATA(typeof(byte), "Unsigned 8-bit", delegate(byte[] input) - { - if (input.Length < 1) - return null; + output[i] = input[i]; + } - byte b = input[0]; - return b.ToString(); - }, delegate(string input) + if (Endianness == IO.Endianness.BigEndian) { - System.Globalization.NumberStyles ns = System.Globalization.NumberStyles.None; - if (input.StartsWith("&H") && input.EndsWith("&")) + byte[] realOutput = new byte[output.Length]; + for (int j = 0; j < output.Length; j++) { - input = input.Substring(2, input.Length - 3); - ns |= System.Globalization.NumberStyles.HexNumber; + realOutput[j] = output[output.Length - (j + 1)]; } - else if (input.ToLower().StartsWith("0x")) - { - input = input.Substring(2); - ns |= System.Globalization.NumberStyles.HexNumber; - } - byte b = Byte.Parse(input, ns); - return new byte[] { b }; - }, 1), - new CONVERSION_DATA(typeof(short), "Signed 16-bit", delegate(byte[] input) - { - if (input.Length < 2) - return null; - - short b = BitConverter.ToInt16(input, 0); - return b.ToString(); - }, delegate(string input) - { - System.Globalization.NumberStyles ns = System.Globalization.NumberStyles.None; - if (input.StartsWith("&H") && input.EndsWith("&")) - { - input = input.Substring(2, input.Length - 3); - ns |= System.Globalization.NumberStyles.HexNumber; - } - else if (input.ToLower().StartsWith("0x")) - { - input = input.Substring(2); - ns |= System.Globalization.NumberStyles.HexNumber; - } - short b = Int16.Parse(input, ns); - return BitConverter.GetBytes(b); - }, 2), - new CONVERSION_DATA(typeof(ushort), "Unsigned 16-bit", delegate(byte[] input) - { - if (input.Length < 2) - return null; - - ushort b = BitConverter.ToUInt16(input, 0); - return b.ToString(); - }, delegate(string input) - { - System.Globalization.NumberStyles ns = System.Globalization.NumberStyles.None; - if (input.StartsWith("&H") && input.EndsWith("&")) - { - input = input.Substring(2, input.Length - 3); - ns |= System.Globalization.NumberStyles.HexNumber; - } - else if (input.ToLower().StartsWith("0x")) - { - input = input.Substring(2); - ns |= System.Globalization.NumberStyles.HexNumber; - } - ushort b = UInt16.Parse(input); - return BitConverter.GetBytes(b); - }, 2), - // Second column - new CONVERSION_DATA(typeof(int), "Signed 32-bit", delegate(byte[] input) - { - if (input.Length < 4) - return null; - - int b = BitConverter.ToInt32(input, 0); - return b.ToString(); - }, delegate(string input) - { - System.Globalization.NumberStyles ns = System.Globalization.NumberStyles.None; - if (input.StartsWith("&H") && input.EndsWith("&")) - { - input = input.Substring(2, input.Length - 3); - ns |= System.Globalization.NumberStyles.HexNumber; - } - else if (input.ToLower().StartsWith("0x")) - { - input = input.Substring(2); - ns |= System.Globalization.NumberStyles.HexNumber; - } - int b = Int32.Parse(input, ns); - return BitConverter.GetBytes(b); - }, 4), - new CONVERSION_DATA(typeof(uint), "Unsigned 32-bit", delegate(byte[] input) - { - if (input.Length < 4) - return null; - - uint b = BitConverter.ToUInt32(input, 0); - return b.ToString(); - }, delegate(string input) - { - System.Globalization.NumberStyles ns = System.Globalization.NumberStyles.None; - if (input.StartsWith("&H") && input.EndsWith("&")) - { - input = input.Substring(2, input.Length - 3); - ns |= System.Globalization.NumberStyles.HexNumber; - } - else if (input.ToLower().StartsWith("0x")) - { - input = input.Substring(2); - ns |= System.Globalization.NumberStyles.HexNumber; - } - - uint b = UInt32.Parse(input, ns); - return BitConverter.GetBytes(b); - }, 4), - new CONVERSION_DATA(typeof(float), "Float 32-bit", delegate(byte[] input) - { - if (input.Length < 4) - return null; - - float b = BitConverter.ToSingle(input, 0); - return b.ToString(); - }, delegate(string input) - { - System.Globalization.NumberStyles ns = System.Globalization.NumberStyles.None; - if (input.StartsWith("&H") && input.EndsWith("&")) - { - input = input.Substring(2, input.Length - 3); - ns |= System.Globalization.NumberStyles.HexNumber; - } - else if (input.ToLower().StartsWith("0x")) - { - input = input.Substring(2); - ns |= System.Globalization.NumberStyles.HexNumber; - } - - float b = Single.Parse(input, ns); - return BitConverter.GetBytes(b); - }, 4), - new CONVERSION_DATA(typeof(double), "Float 64-bit", delegate(byte[] input) - { - if (input.Length < 8) - return null; - - double b = BitConverter.ToDouble(input, 0); - return b.ToString(); - }, delegate(string input) - { - System.Globalization.NumberStyles ns = System.Globalization.NumberStyles.None; - if (input.StartsWith("&H") && input.EndsWith("&")) - { - input = input.Substring(2, input.Length - 3); - ns |= System.Globalization.NumberStyles.HexNumber; - } - else if (input.ToLower().StartsWith("0x")) - { - input = input.Substring(2); - ns |= System.Globalization.NumberStyles.HexNumber; - } - double b = Double.Parse(input, ns); - return BitConverter.GetBytes(b); - }, 8), - // Third column - new CONVERSION_DATA(null, "Hexadecimal", delegate(byte[] input) - { - StringBuilder sb = new StringBuilder(); - if (input.Length < 1) - return null; - - int len = Math.Min(input.Length, 4); - for (int i = 0; i < len; i++) - { - sb.Append(input[i].ToString("X").PadLeft(2, '0')); - if (i < len - 1) - sb.Append(' '); - } - return sb.ToString(); - }, delegate(string input) - { - if (input.ToLower().StartsWith("0x")) - { - input = input.Substring(2); - } - else if (input.ToLower().StartsWith("&h") && input.ToLower().EndsWith("&")) - { - input = input.Substring(2, input.Length - 3); - } - input = input.Replace(" ", String.Empty); - - List list = new List(); - if ((input.Length % 2) == 0) - { - int nbytes = (input.Length / 2); - for (int i = 0; i < nbytes; i++) - { - string s = input.Substring(i * 2, 2); - byte b = Byte.Parse(s, System.Globalization.NumberStyles.HexNumber); - list.Add(b); - } - } - return list.ToArray(); - }, 4), - new CONVERSION_DATA(null, "Decimal", delegate(byte[] input) - { - StringBuilder sb = new StringBuilder(); - if (input.Length < 1) - return null; - - int len = Math.Min(input.Length, 4); - for (int i = 0; i < len; i++) - { - sb.Append(input[i].ToString().PadLeft(3, '0')); - if (i < len - 1) - sb.Append(' '); - } - return sb.ToString(); - }, delegate(string input) - { - long b = Int64.Parse(input); - return BitConverter.GetBytes(b); - }, 4), - new CONVERSION_DATA(null, "Octal", delegate(byte[] input) - { - StringBuilder sb = new StringBuilder(); - if (input.Length < 1) - return null; - - int len = Math.Min(input.Length, 4); - for (int i = 0; i < len; i++) - { - sb.Append(Convert.ToString(input[i], 8).PadLeft(3, '0')); - if (i < len - 1) - sb.Append(' '); - } - return sb.ToString(); - }, delegate(string input) - { - // unsupported right now - return null; - }, 4), - new CONVERSION_DATA(null, "Binary", delegate(byte[] input) - { - StringBuilder sb = new StringBuilder(); - if (input.Length < 1) - return null; - - int len = Math.Min(input.Length, 4); - for (int i = 0; i < len; i++) - { - sb.Append(Convert.ToString(input[i], 2).PadLeft(8, '0')); - if (i < len - 1) - sb.Append(' '); - } - return sb.ToString(); - }, delegate(string input) - { - // unsupported right now - return null; - }, 4) - }; + output = realOutput; + } + return output; + } public BinaryEditor() { @@ -380,11 +127,12 @@ namespace UniversalEditor.Editors.Binary this.hexedit = new HexEditorControl(); this.hexedit.SelectionChanged += Hexedit_SelectionChanged; this.hexedit.Changed += hexedit_Changed; - this.Controls.Add(hexedit, new BoxLayout.Constraints(true, true)); this.conversionPanel = new Container(); this.conversionPanel.Layout = new GridLayout(); + InitializeConverters(); + int r = 0, c = 0; for (int i = 0; i < converters.Length; i++) { @@ -413,6 +161,61 @@ namespace UniversalEditor.Editors.Binary } } + this.pnlSettings = new Container(); + this.pnlSettings.Layout = new BoxLayout(Orientation.Horizontal); + + this.lblEndianness = new Label(); + this.lblEndianness.Text = "_Endianness: "; + this.pnlSettings.Controls.Add(this.lblEndianness, new BoxLayout.Constraints(false, false)); + this.cboEndianness = new ComboBox(); + this.cboEndianness.ReadOnly = true; + + DefaultTreeModel tmEndianness = new DefaultTreeModel(new Type[] { typeof(string) }); + tmEndianness.Rows.AddRange(new TreeModelRow[] + { + new TreeModelRow(new TreeModelRowColumn[] + { + new TreeModelRowColumn(tmEndianness.Columns[0], "Little-endian") + }), + new TreeModelRow(new TreeModelRowColumn[] + { + new TreeModelRowColumn(tmEndianness.Columns[0], "Big-endian") + }) + }); + tmEndianness.Rows[0].SetExtraData("value", IO.Endianness.LittleEndian); + tmEndianness.Rows[1].SetExtraData("value", IO.Endianness.BigEndian); + + this.cboEndianness.Model = tmEndianness; + this.cboEndianness.SelectedItem = tmEndianness.Rows[0]; + this.cboEndianness.Changed += cboEndianness_Changed; + + this.pnlSettings.Controls.Add(this.cboEndianness, new BoxLayout.Constraints(false, false)); + + + this.lblStart = new Label(); + this.lblStart.Text = "_Start: "; + this.pnlSettings.Controls.Add(this.lblStart, new BoxLayout.Constraints(false, false)); + + this.txtStart = new TextBox(); + this.txtStart.KeyDown += txtStart_KeyDown; + this.pnlSettings.Controls.Add(this.txtStart, new BoxLayout.Constraints(false, false)); + + this.lblEnd = new Label(); + this.lblEnd.Text = "_End: "; + this.pnlSettings.Controls.Add(this.lblEnd, new BoxLayout.Constraints(false, false)); + + this.txtEnd = new TextBox(); + this.txtEnd.KeyDown += txtEnd_KeyDown; + this.pnlSettings.Controls.Add(this.txtEnd, new BoxLayout.Constraints(false, false)); + + this.lblLength = new Label(); + this.lblLength.Text = "_Length: "; + this.pnlSettings.Controls.Add(this.lblLength, new BoxLayout.Constraints(false, false)); + + this.txtLength = new TextBox(); + this.txtLength.KeyDown += txtLength_KeyDown; + this.pnlSettings.Controls.Add(this.txtLength, new BoxLayout.Constraints(false, false)); + this.tabs = new TabContainer(); TabPage tabPageConverters = new TabPage(); @@ -447,9 +250,386 @@ namespace UniversalEditor.Editors.Binary this.tabs.TabPages.Add(tabPageFields); + this.Controls.Add(pnlSettings, new BoxLayout.Constraints(false, false, 0, BoxLayout.PackType.Start)); + this.Controls.Add(hexedit, new BoxLayout.Constraints(true, true)); this.Controls.Add(tabs, new BoxLayout.Constraints(false, false, 0, BoxLayout.PackType.End)); } + private void InitializeConverters() + { + // why the hell can't I reference non-static METHODS in DELEGATE METHODS in a field initializer? + // on the off chance they might actually be called within the constructor ??? + // no wait, that doesn't make sense, this is a frickin' FIELD INITIALIZER... + converters = new CONVERSION_DATA[] + { + new CONVERSION_DATA(typeof(sbyte), "Signed 8-bit", delegate(byte[] input) + { + if (input.Length < 1) + return null; + + sbyte b = (sbyte)input[0]; + return b.ToString(); + }, delegate(string input) + { + System.Globalization.NumberStyles ns = System.Globalization.NumberStyles.None; + if (input.StartsWith("&H") && input.EndsWith("&")) + { + input = input.Substring(2, input.Length - 3); + ns |= System.Globalization.NumberStyles.HexNumber; + } + else if (input.ToLower().StartsWith("0x")) + { + input = input.Substring(2); + ns |= System.Globalization.NumberStyles.HexNumber; + } + sbyte b = SByte.Parse(input, ns); + return new byte[] { (byte)b }; + }, 1), + new CONVERSION_DATA(typeof(byte), "Unsigned 8-bit", delegate(byte[] input) + { + if (input.Length < 1) + return null; + + byte b = input[0]; + return b.ToString(); + }, delegate(string input) + { + System.Globalization.NumberStyles ns = System.Globalization.NumberStyles.None; + if (input.StartsWith("&H") && input.EndsWith("&")) + { + input = input.Substring(2, input.Length - 3); + ns |= System.Globalization.NumberStyles.HexNumber; + } + else if (input.ToLower().StartsWith("0x")) + { + input = input.Substring(2); + ns |= System.Globalization.NumberStyles.HexNumber; + } + byte b = Byte.Parse(input, ns); + return new byte[] { b }; + }, 1), + new CONVERSION_DATA(typeof(short), "Signed 16-bit", delegate(byte[] input) + { + if (input.Length < 2) + return null; + + input = ClampWithEndianness(input, 2); + + short b = BitConverter.ToInt16(input, 0); + return b.ToString(); + }, delegate(string input) + { + System.Globalization.NumberStyles ns = System.Globalization.NumberStyles.None; + if (input.StartsWith("&H") && input.EndsWith("&")) + { + input = input.Substring(2, input.Length - 3); + ns |= System.Globalization.NumberStyles.HexNumber; + } + else if (input.ToLower().StartsWith("0x")) + { + input = input.Substring(2); + ns |= System.Globalization.NumberStyles.HexNumber; + } + short b = Int16.Parse(input, ns); + return BitConverter.GetBytes(b); + }, 2), + new CONVERSION_DATA(typeof(ushort), "Unsigned 16-bit", delegate(byte[] input) + { + if (input.Length < 2) + return null; + + input = ClampWithEndianness(input, 2); + + ushort b = BitConverter.ToUInt16(input, 0); + return b.ToString(); + }, delegate(string input) + { + System.Globalization.NumberStyles ns = System.Globalization.NumberStyles.None; + if (input.StartsWith("&H") && input.EndsWith("&")) + { + input = input.Substring(2, input.Length - 3); + ns |= System.Globalization.NumberStyles.HexNumber; + } + else if (input.ToLower().StartsWith("0x")) + { + input = input.Substring(2); + ns |= System.Globalization.NumberStyles.HexNumber; + } + ushort b = UInt16.Parse(input); + return BitConverter.GetBytes(b); + }, 2), + // Second column + new CONVERSION_DATA(typeof(int), "Signed 32-bit", delegate(byte[] input) + { + if (input.Length < 4) + return null; + + input = ClampWithEndianness(input, 4); + + int b = BitConverter.ToInt32(input, 0); + return b.ToString(); + }, delegate(string input) + { + System.Globalization.NumberStyles ns = System.Globalization.NumberStyles.None; + if (input.StartsWith("&H") && input.EndsWith("&")) + { + input = input.Substring(2, input.Length - 3); + ns |= System.Globalization.NumberStyles.HexNumber; + } + else if (input.ToLower().StartsWith("0x")) + { + input = input.Substring(2); + ns |= System.Globalization.NumberStyles.HexNumber; + } + int b = Int32.Parse(input, ns); + return BitConverter.GetBytes(b); + }, 4), + new CONVERSION_DATA(typeof(uint), "Unsigned 32-bit", delegate(byte[] input) + { + if (input.Length < 4) + return null; + + input = ClampWithEndianness(input, 4); + + uint b = BitConverter.ToUInt32(input, 0); + return b.ToString(); + }, delegate(string input) + { + System.Globalization.NumberStyles ns = System.Globalization.NumberStyles.None; + if (input.StartsWith("&H") && input.EndsWith("&")) + { + input = input.Substring(2, input.Length - 3); + ns |= System.Globalization.NumberStyles.HexNumber; + } + else if (input.ToLower().StartsWith("0x")) + { + input = input.Substring(2); + ns |= System.Globalization.NumberStyles.HexNumber; + } + + uint b = UInt32.Parse(input, ns); + return BitConverter.GetBytes(b); + }, 4), + new CONVERSION_DATA(typeof(float), "Float 32-bit", delegate(byte[] input) + { + if (input.Length < 4) + return null; + + input = ClampWithEndianness(input, 4); + + float b = BitConverter.ToSingle(input, 0); + return b.ToString(); + }, delegate(string input) + { + System.Globalization.NumberStyles ns = System.Globalization.NumberStyles.None; + if (input.StartsWith("&H") && input.EndsWith("&")) + { + input = input.Substring(2, input.Length - 3); + ns |= System.Globalization.NumberStyles.HexNumber; + } + else if (input.ToLower().StartsWith("0x")) + { + input = input.Substring(2); + ns |= System.Globalization.NumberStyles.HexNumber; + } + + float b = Single.Parse(input, ns); + return BitConverter.GetBytes(b); + }, 4), + new CONVERSION_DATA(typeof(double), "Float 64-bit", delegate(byte[] input) + { + if (input.Length < 8) + return null; + + input = ClampWithEndianness(input, 8); + + double b = BitConverter.ToDouble(input, 0); + return b.ToString(); + }, delegate(string input) + { + System.Globalization.NumberStyles ns = System.Globalization.NumberStyles.None; + if (input.StartsWith("&H") && input.EndsWith("&")) + { + input = input.Substring(2, input.Length - 3); + ns |= System.Globalization.NumberStyles.HexNumber; + } + else if (input.ToLower().StartsWith("0x")) + { + input = input.Substring(2); + ns |= System.Globalization.NumberStyles.HexNumber; + } + double b = Double.Parse(input, ns); + return BitConverter.GetBytes(b); + }, 8), + // Third column + new CONVERSION_DATA(null, "Hexadecimal", delegate(byte[] input) + { + StringBuilder sb = new StringBuilder(); + if (input.Length < 1) + return null; + + int len = Math.Min(input.Length, 4); + for (int i = 0; i < len; i++) + { + sb.Append(input[i].ToString("X").PadLeft(2, '0')); + if (i < len - 1) + sb.Append(' '); + } + return sb.ToString(); + }, delegate(string input) + { + if (input.ToLower().StartsWith("0x")) + { + input = input.Substring(2); + } + else if (input.ToLower().StartsWith("&h") && input.ToLower().EndsWith("&")) + { + input = input.Substring(2, input.Length - 3); + } + input = input.Replace(" ", String.Empty); + + List list = new List(); + if ((input.Length % 2) == 0) + { + int nbytes = (input.Length / 2); + for (int i = 0; i < nbytes; i++) + { + string s = input.Substring(i * 2, 2); + byte b = Byte.Parse(s, System.Globalization.NumberStyles.HexNumber); + list.Add(b); + } + } + return list.ToArray(); + }, 4), + new CONVERSION_DATA(null, "Decimal", delegate(byte[] input) + { + StringBuilder sb = new StringBuilder(); + if (input.Length < 1) + return null; + + int len = Math.Min(input.Length, 4); + for (int i = 0; i < len; i++) + { + sb.Append(input[i].ToString().PadLeft(3, '0')); + if (i < len - 1) + sb.Append(' '); + } + return sb.ToString(); + }, delegate(string input) + { + long b = Int64.Parse(input); + return BitConverter.GetBytes(b); + }, 4), + new CONVERSION_DATA(null, "Octal", delegate(byte[] input) + { + StringBuilder sb = new StringBuilder(); + if (input.Length < 1) + return null; + + int len = Math.Min(input.Length, 4); + for (int i = 0; i < len; i++) + { + sb.Append(Convert.ToString(input[i], 8).PadLeft(3, '0')); + if (i < len - 1) + sb.Append(' '); + } + return sb.ToString(); + }, delegate(string input) + { + // unsupported right now + return null; + }, 4), + new CONVERSION_DATA(null, "Binary", delegate(byte[] input) + { + StringBuilder sb = new StringBuilder(); + if (input.Length < 1) + return null; + + int len = Math.Min(input.Length, 4); + for (int i = 0; i < len; i++) + { + sb.Append(Convert.ToString(input[i], 2).PadLeft(8, '0')); + if (i < len - 1) + sb.Append(' '); + } + return sb.ToString(); + }, delegate(string input) + { + // unsupported right now + return null; + }, 4) + }; + } + + private IO.Endianness _Endianness = IO.Endianness.LittleEndian; + public IO.Endianness Endianness + { + get { return _Endianness; } + set + { + _Endianness = value; + Hexedit_SelectionChanged(this, EventArgs.Empty); + } + } + + void cboEndianness_Changed(object sender, EventArgs e) + { + Endianness = cboEndianness.SelectedItem.GetExtraData("value", IO.Endianness.LittleEndian); + } + + void txtStart_KeyDown(object sender, MBS.Framework.UserInterface.Input.Keyboard.KeyEventArgs e) + { + if (e.Key == MBS.Framework.UserInterface.Input.Keyboard.KeyboardKey.Enter) + { + if (Int32.TryParse(txtStart.Text, out int val)) + { + hexedit.SelectionStart = new HexEditorPosition(val, 0); + } + else + { + if (MessageDialog.ShowDialog("Invalid value for Int32", "error", MessageDialogButtons.RetryCancel, MessageDialogIcon.Error) == DialogResult.Cancel) + { + txtStart.Text = hexedit.SelectionStart.ByteIndex.ToString(); + } + } + } + } + void txtEnd_KeyDown(object sender, MBS.Framework.UserInterface.Input.Keyboard.KeyEventArgs e) + { + if (e.Key == MBS.Framework.UserInterface.Input.Keyboard.KeyboardKey.Enter) + { + if (Int32.TryParse(txtEnd.Text, out int val)) + { + hexedit.SelectionLength = new HexEditorPosition(val - hexedit.SelectionStart, 0); + } + else + { + if (MessageDialog.ShowDialog("Invalid value for Int32", "error", MessageDialogButtons.RetryCancel, MessageDialogIcon.Error) == DialogResult.Cancel) + { + txtEnd.Text = (hexedit.SelectionStart.ByteIndex + hexedit.SelectionLength.ByteIndex).ToString(); + } + } + } + } + void txtLength_KeyDown(object sender, MBS.Framework.UserInterface.Input.Keyboard.KeyEventArgs e) + { + if (e.Key == MBS.Framework.UserInterface.Input.Keyboard.KeyboardKey.Enter) + { + if (Int32.TryParse(txtLength.Text, out int val)) + { + hexedit.SelectionLength = new HexEditorPosition(val, 0); + } + else + { + if (MessageDialog.ShowDialog("Invalid value for Int32", "error", MessageDialogButtons.RetryCancel, MessageDialogIcon.Error) == DialogResult.Cancel) + { + txtEnd.Text = hexedit.SelectionLength.ByteIndex.ToString(); + } + } + } + } + + private void hexedit_Changed(object sender, EventArgs e) { foreach (TreeModelRow row in tmFieldDefinitions.Rows) @@ -646,6 +826,9 @@ namespace UniversalEditor.Editors.Binary if (converters[i].TextBox != null) converters[i].TextBox.Text = converters[i].ByteToStringFunc(data); } + txtStart.Text = hexedit.SelectionStart.ByteIndex.ToString(); + txtEnd.Text = (hexedit.SelectionStart.ByteIndex + hexedit.SelectionLength.ByteIndex).ToString(); + txtLength.Text = hexedit.SelectionLength.ByteIndex.ToString(); UpdateFieldDefinitions(); }