From 5a2177dbc5ae18bcee4ef0f28fb2fd59c1f9271c Mon Sep 17 00:00:00 2001
From: Siegfried Pammer <siegfriedpammer@gmail.com>
Date: Sat, 22 Feb 2014 10:16:00 +0100
Subject: [PATCH] fixed bugs in hex editor related to "split view"

---
 .../HexEditor/Project/Src/Editor.Designer.cs  |  2 +-
 .../HexEditor/Project/Src/Editor.cs           | 19 +++++++++++++++----
 .../Project/Src/Util/BufferManager.cs         |  1 +
 .../Project/Src/Util/SelectionManager.cs      | 10 ++++++++++
 4 files changed, 27 insertions(+), 5 deletions(-)

diff --git a/src/AddIns/DisplayBindings/HexEditor/Project/Src/Editor.Designer.cs b/src/AddIns/DisplayBindings/HexEditor/Project/Src/Editor.Designer.cs
index e3c1e6893d..a375b0f44a 100644
--- a/src/AddIns/DisplayBindings/HexEditor/Project/Src/Editor.Designer.cs
+++ b/src/AddIns/DisplayBindings/HexEditor/Project/Src/Editor.Designer.cs
@@ -112,11 +112,11 @@ namespace HexEditor
 			// Editor
 			// 
 			this.BackColor = System.Drawing.Color.White;
+			this.Controls.Add(this.VScrollBar);
 			this.Controls.Add(this.header);
 			this.Controls.Add(this.textView);
 			this.Controls.Add(this.hexView);
 			this.Controls.Add(this.side);
-			this.Controls.Add(this.VScrollBar);
 			this.DoubleBuffered = true;
 			this.MinimumSize = new System.Drawing.Size(1, 1);
 			this.Name = "Editor";
diff --git a/src/AddIns/DisplayBindings/HexEditor/Project/Src/Editor.cs b/src/AddIns/DisplayBindings/HexEditor/Project/Src/Editor.cs
index c5f7bfe230..58c8c56282 100644
--- a/src/AddIns/DisplayBindings/HexEditor/Project/Src/Editor.cs
+++ b/src/AddIns/DisplayBindings/HexEditor/Project/Src/Editor.cs
@@ -136,6 +136,12 @@ namespace HexEditor
 		
 		void BufferChanged(object sender, EventArgs e)
 		{
+			if (!Focused) {
+				selection.ValidateSelection();
+				caret.Offset = Math.Max(0, Math.Min(caret.Offset, buffer.BufferSize));
+				Point pos = GetPositionForOffset(caret.Offset, charwidth);
+				caret.SetToPosition(pos);
+			}
 			Invalidate();
 			OnDocumentChanged(EventArgs.Empty);
 		}
@@ -1301,6 +1307,7 @@ namespace HexEditor
 					break;
 				case Keys.Back:
 					e.Handled = true;
+					e.SuppressKeyPress = true;
 					if (hexinputmode) return;
 					if (selection.HasSomethingSelected) {
 						byte[] bytes = selection.GetSelectionBytes();
@@ -1314,8 +1321,7 @@ namespace HexEditor
 					} else {
 						byte b = buffer.GetByte(caret.Offset - 1);
 						
-						if (buffer.RemoveByte(caret.Offset - 1))
-						{
+						if (buffer.RemoveByte(caret.Offset - 1)) {
 							if (caret.Offset > -1) caret.Offset--;
 							if (GetLineForOffset(caret.Offset) < topline) topline = GetLineForOffset(caret.Offset);
 							if (GetLineForOffset(caret.Offset) > topline + GetMaxVisibleLines() - 2) topline = GetLineForOffset(caret.Offset) - GetMaxVisibleLines() + 2;
@@ -1328,6 +1334,7 @@ namespace HexEditor
 					break;
 				case Keys.Delete:
 					e.Handled = true;
+					e.SuppressKeyPress = true;
 					if (hexinputmode) return;
 					if (selection.HasSomethingSelected) {
 						byte[] old = selection.GetSelectionBytes();
@@ -1353,6 +1360,7 @@ namespace HexEditor
 				case Keys.CapsLock:
 				case Keys.ShiftKey:
 				case Keys.ControlKey:
+				case Keys.Enter:
 					break;
 				case Keys.Tab:
 					if (activeView == hexView) {
@@ -1405,6 +1413,7 @@ namespace HexEditor
 					if (activeView == hexView) {
 						ProcessHexInput(e);
 						e.Handled = true;
+						e.SuppressKeyPress = true;
 						return;
 					}
 					
@@ -1773,7 +1782,6 @@ namespace HexEditor
 		/// <summary>
 		/// Calculates the max possible bytes per line.
 		/// </summary>
-		/// <returns>Int32, containing the result</returns>
 		internal int CalculateMaxBytesPerLine()
 		{
 			int width = Width - side.Width - 90;
@@ -1836,6 +1844,8 @@ namespace HexEditor
 			if (offset < buffer.BufferSize) {
 				return offset;
 			} else {
+				hexinputmodepos = 0;
+				hexinputmode = false;
 				return buffer.BufferSize;
 			}
 		}
@@ -1852,7 +1862,8 @@ namespace HexEditor
 			int line = (int)(offset / BytesPerLine) - topline;
 			int pline = line * fontheight - 1 * (line - 1) - 1;
 			int col = (offset % BytesPerLine) * underscorewidth * charwidth + 4;
-			if (hexinputmode && !selectionmode && !selection.HasSomethingSelected && insertmode) col += (hexinputmodepos * underscorewidth);
+			if (hexinputmode && !selectionmode && !selection.HasSomethingSelected && insertmode)
+				col += (hexinputmodepos * underscorewidth);
 			
 			return new Point(col, pline);
 		}
diff --git a/src/AddIns/DisplayBindings/HexEditor/Project/Src/Util/BufferManager.cs b/src/AddIns/DisplayBindings/HexEditor/Project/Src/Util/BufferManager.cs
index 721f017e5b..0f8a421c38 100644
--- a/src/AddIns/DisplayBindings/HexEditor/Project/Src/Util/BufferManager.cs
+++ b/src/AddIns/DisplayBindings/HexEditor/Project/Src/Util/BufferManager.cs
@@ -153,6 +153,7 @@ namespace HexEditor.Util
 			if ((offset < buffer.Count) & (offset > -1)) {
 				buffer.RemoveAt(offset);
 				file.MakeDirty(HexEditFileModelProvider.Instance);
+				OnBufferChanged(EventArgs.Empty);
 				return true;
 			}
 			return false;
diff --git a/src/AddIns/DisplayBindings/HexEditor/Project/Src/Util/SelectionManager.cs b/src/AddIns/DisplayBindings/HexEditor/Project/Src/Util/SelectionManager.cs
index 98184243e2..2811cadda8 100644
--- a/src/AddIns/DisplayBindings/HexEditor/Project/Src/Util/SelectionManager.cs
+++ b/src/AddIns/DisplayBindings/HexEditor/Project/Src/Util/SelectionManager.cs
@@ -80,6 +80,16 @@ namespace HexEditor.Util
 		{
 			 return buffer.GetBytes(Math.Min(Start, End), Math.Abs(End - Start)); 
 		}
+
+		public void ValidateSelection()
+		{
+			int start = Math.Min(this.start, this.end);
+			int end = Math.Max(this.start, this.end);
+			start = Math.Min(Math.Max(0, start), buffer.BufferSize);
+			end = Math.Min(Math.Max(0, end), buffer.BufferSize);
+			this.start = start;
+			this.end = end;
+		}
 		
 		/// <summary>
 		/// Cleares the selection (no text is altered)