initial commit
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Pulsar.Server.Controls.HexEditor
|
||||
{
|
||||
public class ByteCollection
|
||||
{
|
||||
private List<byte> _bytes;
|
||||
|
||||
#region Properties
|
||||
|
||||
public int Length
|
||||
{
|
||||
get { return _bytes.Count; }
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructor
|
||||
|
||||
public ByteCollection()
|
||||
{
|
||||
_bytes = new List<byte>();
|
||||
}
|
||||
|
||||
public ByteCollection(byte[] bytes)
|
||||
{
|
||||
_bytes = new List<byte>(bytes);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
public void Add(byte item)
|
||||
{
|
||||
_bytes.Add(item);
|
||||
}
|
||||
|
||||
public void Insert(int index, byte item)
|
||||
{
|
||||
_bytes.Insert(index, item);
|
||||
}
|
||||
|
||||
public void Remove(byte item)
|
||||
{
|
||||
_bytes.Remove(item);
|
||||
}
|
||||
|
||||
public void RemoveAt(int index)
|
||||
{
|
||||
_bytes.RemoveAt(index);
|
||||
}
|
||||
|
||||
public void RemoveRange(int startIndex, int count)
|
||||
{
|
||||
_bytes.RemoveRange(startIndex, count);
|
||||
}
|
||||
|
||||
public byte GetAt(int index)
|
||||
{
|
||||
return _bytes[index];
|
||||
}
|
||||
|
||||
public void SetAt(int index, byte item)
|
||||
{
|
||||
_bytes[index] = item;
|
||||
}
|
||||
|
||||
public char GetCharAt(int index)
|
||||
{
|
||||
return Convert.ToChar(_bytes[index]);
|
||||
}
|
||||
|
||||
public byte[] ToArray()
|
||||
{
|
||||
return _bytes.ToArray();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,232 @@
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Pulsar.Server.Controls.HexEditor
|
||||
{
|
||||
public class Caret
|
||||
{
|
||||
#region Field
|
||||
|
||||
/// <summary>
|
||||
/// Contains the start index
|
||||
/// where the caret started
|
||||
/// </summary>
|
||||
int _startIndex;
|
||||
|
||||
/// <summary>
|
||||
/// Contains the end index
|
||||
/// where the caret is
|
||||
/// currently located
|
||||
/// </summary>
|
||||
int _endIndex;
|
||||
|
||||
/// <summary>
|
||||
/// Tells if the given caret
|
||||
/// is active in the controller
|
||||
/// (control is in focus)
|
||||
/// </summary>
|
||||
bool _isCaretActive;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Tells if the caret is
|
||||
/// currently hidden or
|
||||
/// not (out of view)
|
||||
/// </summary>
|
||||
bool _isCaretHidden;
|
||||
|
||||
/// <summary>
|
||||
/// Holds the actual position
|
||||
/// of the caret
|
||||
/// </summary>
|
||||
Point _location;
|
||||
|
||||
private HexEditor _editor;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Properties
|
||||
|
||||
public int SelectionStart
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_endIndex < _startIndex)
|
||||
return _endIndex;
|
||||
return _startIndex;
|
||||
}
|
||||
}
|
||||
|
||||
public int SelectionLength
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_endIndex < _startIndex)
|
||||
return _startIndex - _endIndex;
|
||||
return _endIndex - _startIndex;
|
||||
}
|
||||
}
|
||||
|
||||
public bool Focused
|
||||
{
|
||||
get { return _isCaretActive; }
|
||||
}
|
||||
|
||||
public int CurrentIndex
|
||||
{
|
||||
get { return _endIndex; }
|
||||
}
|
||||
|
||||
public Point Location
|
||||
{
|
||||
get { return _location; }
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region EventHandlers
|
||||
|
||||
public event EventHandler SelectionStartChanged;
|
||||
|
||||
public event EventHandler SelectionLengthChanged;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructor
|
||||
|
||||
public Caret(HexEditor editor)
|
||||
{
|
||||
_editor = editor;
|
||||
_isCaretActive = false;
|
||||
_startIndex = 0;
|
||||
_endIndex = 0;
|
||||
_isCaretHidden = true;
|
||||
_location = new Point(0, 0);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
#region Caret
|
||||
|
||||
private bool Create(IntPtr hWHandler)
|
||||
{
|
||||
if (!_isCaretActive)
|
||||
{
|
||||
_isCaretActive = true;
|
||||
return CreateCaret(hWHandler, IntPtr.Zero, 0, (int)_editor.CharSize.Height - 2);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private bool Show(IntPtr hWnd)
|
||||
{
|
||||
if (_isCaretActive)
|
||||
{
|
||||
_isCaretHidden = false;
|
||||
return ShowCaret(hWnd);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool Hide(IntPtr hWnd)
|
||||
{
|
||||
if (_isCaretActive && !_isCaretHidden)
|
||||
{
|
||||
_isCaretHidden = true;
|
||||
return HideCaret(hWnd);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool Destroy()
|
||||
{
|
||||
if (_isCaretActive)
|
||||
{
|
||||
_isCaretActive = false;
|
||||
DeSelect();
|
||||
DestroyCaret();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
public void SetStartIndex(int index)
|
||||
{
|
||||
_startIndex = index;
|
||||
_endIndex = _startIndex;
|
||||
|
||||
if (SelectionStartChanged != null)
|
||||
SelectionStartChanged(this, EventArgs.Empty);
|
||||
|
||||
if (SelectionLengthChanged != null)
|
||||
SelectionLengthChanged(this, EventArgs.Empty);
|
||||
|
||||
}
|
||||
|
||||
public void SetEndIndex(int index)
|
||||
{
|
||||
_endIndex = index;
|
||||
|
||||
if (SelectionStartChanged != null)
|
||||
SelectionStartChanged(this, EventArgs.Empty);
|
||||
|
||||
if (SelectionLengthChanged != null)
|
||||
SelectionLengthChanged(this, EventArgs.Empty);
|
||||
}
|
||||
|
||||
public void SetCaretLocation(Point start)
|
||||
{
|
||||
Create(_editor.Handle);
|
||||
_location = start;
|
||||
SetCaretPos(_location.X, _location.Y);
|
||||
Show(_editor.Handle);
|
||||
}
|
||||
|
||||
public bool IsSelected(int byteIndex)
|
||||
{
|
||||
return (SelectionStart <= byteIndex && byteIndex < (SelectionStart + SelectionLength));
|
||||
}
|
||||
|
||||
private void DeSelect()
|
||||
{
|
||||
if (_endIndex < _startIndex)
|
||||
_startIndex = _endIndex;
|
||||
else
|
||||
_endIndex = _startIndex;
|
||||
|
||||
if (SelectionStartChanged != null)
|
||||
SelectionStartChanged(this, EventArgs.Empty);
|
||||
|
||||
if (SelectionLengthChanged != null)
|
||||
SelectionLengthChanged(this, EventArgs.Empty);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Caret import
|
||||
|
||||
[DllImport("user32.dll", SetLastError = true)]
|
||||
static extern bool CreateCaret(IntPtr hWnd, IntPtr hBitmap, int nWidth, int nHeight);
|
||||
|
||||
[DllImport("user32.dll", SetLastError = true)]
|
||||
static extern bool DestroyCaret();
|
||||
|
||||
[DllImport("user32.dll", SetLastError = true)]
|
||||
static extern bool SetCaretPos(int x, int y);
|
||||
|
||||
[DllImport("user32.dll", SetLastError = true)]
|
||||
static extern bool ShowCaret(IntPtr hWnd);
|
||||
|
||||
[DllImport("user32.dll", SetLastError = true)]
|
||||
static extern bool HideCaret(IntPtr hWnd);
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,177 @@
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace Pulsar.Server.Controls.HexEditor
|
||||
{
|
||||
public class EditView : IKeyMouseEventHandler
|
||||
{
|
||||
#region Fields
|
||||
|
||||
/// <summary>
|
||||
/// Contains the handler for the hex
|
||||
/// view.
|
||||
/// </summary>
|
||||
private HexViewHandler _hexView;
|
||||
|
||||
/// <summary>
|
||||
/// Contains the handler for the
|
||||
/// string view
|
||||
/// </summary>
|
||||
private StringViewHandler _stringView;
|
||||
|
||||
private HexEditor _editor;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Contructor
|
||||
|
||||
public EditView(HexEditor editor)
|
||||
{
|
||||
_editor = editor;
|
||||
_hexView = new HexViewHandler(editor);
|
||||
_stringView = new StringViewHandler(editor);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region KeyMouseEvent
|
||||
|
||||
#region Key
|
||||
|
||||
public void OnKeyPress(KeyPressEventArgs e)
|
||||
{
|
||||
if (InHexView(_editor.CaretPosX))
|
||||
{
|
||||
_hexView.OnKeyPress(e);
|
||||
}
|
||||
else
|
||||
{
|
||||
_stringView.OnKeyPress(e);
|
||||
}
|
||||
}
|
||||
|
||||
public void OnKeyDown(KeyEventArgs e)
|
||||
{
|
||||
if (InHexView(_editor.CaretPosX))
|
||||
{
|
||||
_hexView.OnKeyDown(e);
|
||||
}
|
||||
else
|
||||
{
|
||||
_stringView.OnKeyDown(e);
|
||||
}
|
||||
}
|
||||
|
||||
public void OnKeyUp(KeyEventArgs e)
|
||||
{ /* ... */ }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Mouse
|
||||
|
||||
public void OnMouseDown(MouseEventArgs e)
|
||||
{
|
||||
if (e.Button == MouseButtons.Left)
|
||||
{
|
||||
if (InHexView(e.X))
|
||||
{
|
||||
_hexView.OnMouseDown(e.X, e.Y);
|
||||
}
|
||||
else
|
||||
{
|
||||
_stringView.OnMouseDown(e.X, e.Y);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void OnMouseDragged(MouseEventArgs e)
|
||||
{
|
||||
if (e.Button == MouseButtons.Left)
|
||||
{
|
||||
if (InHexView(e.X))
|
||||
{
|
||||
_hexView.OnMouseDragged(e.X, e.Y);
|
||||
}
|
||||
else
|
||||
{
|
||||
_stringView.OnMouseDragged(e.X, e.Y);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void OnMouseUp(MouseEventArgs e)
|
||||
{ /* ... */ }
|
||||
|
||||
public void OnMouseDoubleClick(MouseEventArgs e)
|
||||
{
|
||||
if (e.Button == MouseButtons.Left)
|
||||
{
|
||||
if (InHexView(e.X))
|
||||
{
|
||||
_hexView.OnMouseDoubleClick();
|
||||
}
|
||||
else
|
||||
{
|
||||
_stringView.OnMouseDoubleClick();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Focus
|
||||
|
||||
public void OnGotFocus(EventArgs e)
|
||||
{
|
||||
if (InHexView(_editor.CaretPosX))
|
||||
_hexView.Focus();
|
||||
else
|
||||
_stringView.Focus();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#endregion
|
||||
|
||||
#region UpdateActions
|
||||
public void SetLowerCase()
|
||||
{
|
||||
_hexView.SetLowerCase();
|
||||
}
|
||||
|
||||
public void SetUpperCase()
|
||||
{
|
||||
_hexView.SetUpperCase();
|
||||
}
|
||||
|
||||
public void Update(int startPositionX, Rectangle area)
|
||||
{
|
||||
_hexView.Update(startPositionX, area);
|
||||
_stringView.Update(_hexView.MaxWidth, area);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region PaintActions
|
||||
|
||||
public void Paint(Graphics g, int startIndex, int endIndex)
|
||||
{
|
||||
for (int i = 0; (i + startIndex) < endIndex; i++)
|
||||
{
|
||||
_hexView.Paint(g, i, startIndex);
|
||||
_stringView.Paint(g, i, startIndex);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Misc
|
||||
|
||||
private bool InHexView(int x)
|
||||
{
|
||||
return (x < (_hexView.MaxWidth + _editor.EntityMargin - 2));
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,442 @@
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace Pulsar.Server.Controls.HexEditor
|
||||
{
|
||||
public class HexViewHandler
|
||||
{
|
||||
#region Fields
|
||||
|
||||
bool _isEditing;
|
||||
|
||||
/// <summary>
|
||||
/// Contains info about how to
|
||||
/// present the hex values
|
||||
/// (Upper or Lower case)
|
||||
/// </summary>
|
||||
string _hexType = "X2";
|
||||
|
||||
/// <summary>
|
||||
/// Contains the boundary for one single
|
||||
/// hexa value that is visible
|
||||
/// </summary>
|
||||
Rectangle _recHexValue;
|
||||
|
||||
/// <summary>
|
||||
/// Contains the format of the hexadecimal
|
||||
/// strings that are presented
|
||||
/// </summary>
|
||||
StringFormat _stringFormat;
|
||||
|
||||
private HexEditor _editor;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Properties
|
||||
|
||||
public int MaxWidth
|
||||
{
|
||||
get { return _recHexValue.X + (_recHexValue.Width * _editor.BytesPerLine); }
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructor
|
||||
|
||||
public HexViewHandler(HexEditor editor)
|
||||
{
|
||||
_editor = editor;
|
||||
|
||||
//Set String format for the hex values
|
||||
_stringFormat = new StringFormat(StringFormat.GenericTypographic);
|
||||
_stringFormat.Alignment = StringAlignment.Center;
|
||||
_stringFormat.LineAlignment = StringAlignment.Center;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Method
|
||||
|
||||
#region KeyMouseEvents
|
||||
|
||||
#region KeyEvents
|
||||
|
||||
public void OnKeyPress(KeyPressEventArgs e)
|
||||
{
|
||||
if (IsHex(e.KeyChar))
|
||||
HandleUserInput(e.KeyChar);
|
||||
}
|
||||
|
||||
public void OnKeyDown(KeyEventArgs e)
|
||||
{
|
||||
if (e.KeyCode == Keys.Delete || e.KeyCode == Keys.Back)
|
||||
{
|
||||
if (_editor.SelectionLength > 0)
|
||||
{
|
||||
//Remove the selected bytes
|
||||
HandleUserRemove();
|
||||
int index = _editor.CaretIndex;
|
||||
Point newLocation = GetCaretLocation(index);
|
||||
_editor.SetCaretStart(index, newLocation);
|
||||
}
|
||||
else if (_editor.CaretIndex < _editor.LastVisibleByte && e.KeyCode == Keys.Delete)
|
||||
{
|
||||
//Remove the byte after the caret
|
||||
_editor.RemoveByteAt(_editor.CaretIndex);
|
||||
Point newLocation = GetCaretLocation(_editor.CaretIndex);
|
||||
_editor.SetCaretStart(_editor.CaretIndex, newLocation);
|
||||
}
|
||||
else if (_editor.CaretIndex > 0 && e.KeyCode == Keys.Back)
|
||||
{
|
||||
//Remove byte before the caret
|
||||
int index = _editor.CaretIndex - 1;
|
||||
if (_isEditing)
|
||||
{
|
||||
//Remove the byte that is being edited
|
||||
index = _editor.CaretIndex;
|
||||
}
|
||||
_editor.RemoveByteAt(index);
|
||||
Point newLocation = GetCaretLocation(index);
|
||||
_editor.SetCaretStart(index, newLocation);
|
||||
}
|
||||
_isEditing = false;
|
||||
}
|
||||
else if (e.KeyCode == Keys.Up && (_editor.CaretIndex - _editor.BytesPerLine) >= 0)
|
||||
{
|
||||
int index = _editor.CaretIndex - _editor.BytesPerLine;
|
||||
|
||||
//Check ig caret is att the end of the line
|
||||
if (index % _editor.BytesPerLine == 0 && _editor.CaretPosX >= _recHexValue.X + _recHexValue.Width * _editor.BytesPerLine)
|
||||
{
|
||||
Point position = new Point(_editor.CaretPosX, _editor.CaretPosY - _recHexValue.Height);
|
||||
|
||||
//check that this is not the last row (nothing above)
|
||||
if (index == 0)
|
||||
{
|
||||
//Last row do not change index and position
|
||||
position = new Point(_editor.CaretPosX, _editor.CaretPosY);
|
||||
index = _editor.BytesPerLine;
|
||||
}
|
||||
|
||||
if (e.Shift)
|
||||
_editor.SetCaretEnd(index, position);
|
||||
else
|
||||
_editor.SetCaretStart(index, position);
|
||||
_isEditing = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
HandleArrowKeys(index, e.Shift);
|
||||
}
|
||||
}
|
||||
else if (e.KeyCode == Keys.Down && (_editor.CaretIndex - 1) / _editor.BytesPerLine < _editor.HexTableLength / _editor.BytesPerLine)
|
||||
{
|
||||
int index = _editor.CaretIndex + _editor.BytesPerLine;
|
||||
|
||||
if (index > _editor.HexTableLength)
|
||||
{
|
||||
index = _editor.HexTableLength;
|
||||
HandleArrowKeys(index, e.Shift);
|
||||
}
|
||||
else
|
||||
{
|
||||
Point position = new Point(_editor.CaretPosX, _editor.CaretPosY + _recHexValue.Height);
|
||||
|
||||
if (e.Shift)
|
||||
_editor.SetCaretEnd(index, position);
|
||||
else
|
||||
_editor.SetCaretStart(index, position);
|
||||
_isEditing = false;
|
||||
}
|
||||
}
|
||||
else if (e.KeyCode == Keys.Left && (_editor.CaretIndex - 1) >= 0)
|
||||
{
|
||||
int index = _editor.CaretIndex - 1;
|
||||
HandleArrowKeys(index, e.Shift);
|
||||
}
|
||||
else if (e.KeyCode == Keys.Right && (_editor.CaretIndex + 1) <= _editor.HexTableLength)
|
||||
{
|
||||
int index = _editor.CaretIndex + 1;
|
||||
HandleArrowKeys(index, e.Shift);
|
||||
}
|
||||
}
|
||||
|
||||
public void HandleArrowKeys(int index, bool isShiftDown)
|
||||
{
|
||||
Point position = GetCaretLocation(index);
|
||||
|
||||
if (isShiftDown)
|
||||
_editor.SetCaretEnd(index, position);
|
||||
else
|
||||
_editor.SetCaretStart(index, position);
|
||||
_isEditing = false;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region MouseEvent
|
||||
|
||||
public void OnMouseDown(int x, int y)
|
||||
{
|
||||
int iX = (x - _recHexValue.X) / _recHexValue.Width;
|
||||
int iY = (y - _recHexValue.Y) / _recHexValue.Height;
|
||||
|
||||
//Check that values are good
|
||||
iX = iX > _editor.BytesPerLine ? _editor.BytesPerLine : iX;
|
||||
iX = iX < 0 ? 0 : iX;
|
||||
iY = iY > _editor.MaxBytesV ? _editor.MaxBytesV : iY;
|
||||
iY = iY < 0 ? 0 : iY;
|
||||
|
||||
//Make sure values are withing the given bounds
|
||||
if ((_editor.LastVisibleByte - _editor.FirstVisibleByte) / _editor.BytesPerLine <= iY)
|
||||
{
|
||||
//Check that column is not greater than max
|
||||
if ((_editor.LastVisibleByte - _editor.FirstVisibleByte) % _editor.BytesPerLine <= iX)
|
||||
{
|
||||
iX = (_editor.LastVisibleByte - _editor.FirstVisibleByte) % _editor.BytesPerLine;
|
||||
}
|
||||
iY = (_editor.LastVisibleByte - _editor.FirstVisibleByte) / _editor.BytesPerLine;
|
||||
}
|
||||
|
||||
//Get the smallest possible location (do not want to exceed the max)
|
||||
int index = Math.Min(_editor.LastVisibleByte, _editor.FirstVisibleByte + iX + (iY * _editor.BytesPerLine));
|
||||
|
||||
int xPos = (iX * _recHexValue.Width) + _recHexValue.X;
|
||||
int yPos = (iY * _recHexValue.Height) + _recHexValue.Y;
|
||||
|
||||
_editor.SetCaretStart(index, new Point(xPos, yPos));
|
||||
_isEditing = false;
|
||||
}
|
||||
|
||||
public void OnMouseDragged(int x, int y)
|
||||
{
|
||||
int iX = (x - _recHexValue.X) / _recHexValue.Width;
|
||||
int iY = (y - _recHexValue.Y) / _recHexValue.Height;
|
||||
|
||||
//Check that values are good
|
||||
iX = iX > _editor.BytesPerLine ? _editor.BytesPerLine : iX;
|
||||
iX = iX < 0 ? 0 : iX;
|
||||
iY = iY > _editor.MaxBytesV ? _editor.MaxBytesV : iY;
|
||||
|
||||
if (_editor.FirstVisibleByte > 0)
|
||||
{
|
||||
iY = iY < 0 ? -1 : iY;
|
||||
}
|
||||
else
|
||||
{
|
||||
iY = iY < 0 ? 0 : iY;
|
||||
}
|
||||
|
||||
//Make sure values are withing the given bounds
|
||||
if ((_editor.LastVisibleByte - _editor.FirstVisibleByte) / _editor.BytesPerLine <= iY)
|
||||
{
|
||||
//Check that column is not greater than max
|
||||
if ((_editor.LastVisibleByte - _editor.FirstVisibleByte) % _editor.BytesPerLine <= iX)
|
||||
{
|
||||
iX = (_editor.LastVisibleByte - _editor.FirstVisibleByte) % _editor.BytesPerLine;
|
||||
}
|
||||
iY = (_editor.LastVisibleByte - _editor.FirstVisibleByte) / _editor.BytesPerLine;
|
||||
}
|
||||
|
||||
//Get the smallest possible location (do not want to exceed the max)
|
||||
int index = Math.Min(_editor.LastVisibleByte, _editor.FirstVisibleByte + iX + (iY * _editor.BytesPerLine));
|
||||
|
||||
int xPos = (iX * _recHexValue.Width) + _recHexValue.X;
|
||||
int yPos = (iY * _recHexValue.Height) + _recHexValue.Y;
|
||||
|
||||
_editor.SetCaretEnd(index, new Point(xPos, yPos));
|
||||
}
|
||||
|
||||
public void OnMouseDoubleClick()
|
||||
{
|
||||
if (_editor.CaretIndex < _editor.LastVisibleByte)
|
||||
{
|
||||
int index = _editor.CaretIndex + 1;
|
||||
Point newLocation = GetCaretLocation(index);
|
||||
_editor.SetCaretEnd(index, newLocation);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#endregion
|
||||
|
||||
#region PaintMethod
|
||||
|
||||
public void Update(int startPositionX, Rectangle area)
|
||||
{
|
||||
_recHexValue = new Rectangle(
|
||||
startPositionX,
|
||||
area.Y,
|
||||
(int)(_editor.CharSize.Width * 3),
|
||||
(int)(_editor.CharSize.Height) - 2
|
||||
);
|
||||
|
||||
_recHexValue.X += _editor.EntityMargin;
|
||||
}
|
||||
|
||||
public void Paint(Graphics g, int index, int startIndex)
|
||||
{
|
||||
Point columnAndRow = GetByteColumnAndRow(index);
|
||||
|
||||
if (_editor.IsSelected(index + startIndex))
|
||||
{
|
||||
PaintByteAsSelected(g, columnAndRow, (index + startIndex));
|
||||
}
|
||||
else
|
||||
{
|
||||
PaintByte(g, columnAndRow, (index + startIndex));
|
||||
}
|
||||
}
|
||||
|
||||
private void PaintByteAsSelected(Graphics g, Point point, int index)
|
||||
{
|
||||
SolidBrush backBrush = new SolidBrush(_editor.SelectionBackColor);
|
||||
SolidBrush textBrush = new SolidBrush(_editor.SelectionForeColor);
|
||||
RectangleF drawSurface = GetBound(point);
|
||||
string hexValue = _editor.GetByte(index).ToString(_hexType);
|
||||
|
||||
g.FillRectangle(backBrush, drawSurface);
|
||||
g.DrawString(hexValue, _editor.Font, textBrush, drawSurface, _stringFormat);
|
||||
}
|
||||
|
||||
private void PaintByte(Graphics g, Point point, int index)
|
||||
{
|
||||
SolidBrush brush = new SolidBrush(_editor.ForeColor);
|
||||
RectangleF drawSurface = GetBound(point);
|
||||
string hexValue = _editor.GetByte(index).ToString(_hexType);
|
||||
|
||||
g.DrawString(hexValue, _editor.Font, brush, drawSurface, _stringFormat);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
public void SetLowerCase()
|
||||
{
|
||||
_hexType = "x2";
|
||||
}
|
||||
|
||||
public void SetUpperCase()
|
||||
{
|
||||
_hexType = "X2";
|
||||
}
|
||||
|
||||
public void Focus()
|
||||
{
|
||||
int index = _editor.CaretIndex;
|
||||
Point location = GetCaretLocation(index);
|
||||
_editor.SetCaretStart(index, location);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Caret
|
||||
|
||||
/// <summary>
|
||||
/// Get the caret current location
|
||||
/// in the given bound.
|
||||
/// </summary>
|
||||
private Point GetCaretLocation(int index)
|
||||
{
|
||||
int xPos = _recHexValue.X + (_recHexValue.Width * (index % _editor.BytesPerLine));
|
||||
int yPos = _recHexValue.Y + (_recHexValue.Height * ((index - (_editor.FirstVisibleByte + index % _editor.BytesPerLine)) / _editor.BytesPerLine));
|
||||
|
||||
Point ret = new Point(xPos, yPos);
|
||||
return ret;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Misc
|
||||
|
||||
private void HandleUserRemove()
|
||||
{
|
||||
//Calculate where to position the caret after the removal
|
||||
int index = _editor.SelectionStart;
|
||||
Point position = GetCaretLocation(index);
|
||||
//Remove all of the selected bytes
|
||||
_editor.RemoveSelectedBytes();
|
||||
|
||||
//Set the new position of the caret
|
||||
_editor.SetCaretStart(index, position);
|
||||
}
|
||||
|
||||
private void HandleUserInput(char key)
|
||||
{
|
||||
if (!_editor.CaretFocused)
|
||||
return;
|
||||
|
||||
//Perform overwrite
|
||||
HandleUserRemove();
|
||||
|
||||
if (_isEditing)
|
||||
{
|
||||
//Editing has already started, should change the second nibble
|
||||
_isEditing = false;
|
||||
//Load old bytes to allow change
|
||||
byte oldByte = _editor.GetByte(_editor.CaretIndex);
|
||||
//Append the new nibble
|
||||
oldByte += Convert.ToByte(key.ToString(), 16);
|
||||
_editor.SetByte(_editor.CaretIndex, oldByte);
|
||||
//Relocate the caret
|
||||
int index = _editor.CaretIndex + 1;
|
||||
Point newLocation = GetCaretLocation(index);
|
||||
_editor.SetCaretStart(index, newLocation);
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
//Begin new edit phase
|
||||
_isEditing = true;
|
||||
string hexByte = key.ToString() + "0";
|
||||
byte newByte = Convert.ToByte(hexByte, 16);
|
||||
|
||||
if (_editor.HexTable.Length <= 0)
|
||||
{
|
||||
_editor.AppendByte(newByte);
|
||||
}
|
||||
else
|
||||
{
|
||||
_editor.InsertByte(_editor.CaretIndex, newByte);
|
||||
}
|
||||
|
||||
//Relocate the caret to the middle of the hex value (provide illusion of editing the second value)
|
||||
int xPos = (_recHexValue.X + (_recHexValue.Width * ((_editor.CaretIndex) % _editor.BytesPerLine)) + (_recHexValue.Width / 2));
|
||||
int yPos = _recHexValue.Y + (_recHexValue.Height * ((_editor.CaretIndex - (_editor.FirstVisibleByte + _editor.CaretIndex % _editor.BytesPerLine)) / _editor.BytesPerLine));
|
||||
|
||||
_editor.SetCaretStart(_editor.CaretIndex, new Point(xPos, yPos));
|
||||
}
|
||||
}
|
||||
|
||||
private Point GetByteColumnAndRow(int index)
|
||||
{
|
||||
int column = index % _editor.BytesPerLine;
|
||||
int row = index / _editor.BytesPerLine;
|
||||
|
||||
Point ret = new Point(column, row);
|
||||
return ret;
|
||||
}
|
||||
|
||||
private RectangleF GetBound(Point point)
|
||||
{
|
||||
RectangleF ret = new RectangleF(
|
||||
_recHexValue.X + (point.X * _recHexValue.Width),
|
||||
_recHexValue.Y + (point.Y * _recHexValue.Height),
|
||||
_recHexValue.Width,
|
||||
_recHexValue.Height
|
||||
);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
private bool IsHex(char c)
|
||||
{
|
||||
return (c >= 'a' && c <= 'f') ||
|
||||
(c >= 'A' && c <= 'F') ||
|
||||
Char.IsDigit(c);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
using System;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace Pulsar.Server.Controls.HexEditor
|
||||
{
|
||||
public interface IKeyMouseEventHandler
|
||||
{
|
||||
void OnKeyPress(KeyPressEventArgs e);
|
||||
|
||||
void OnKeyDown(KeyEventArgs e);
|
||||
|
||||
void OnKeyUp(KeyEventArgs e);
|
||||
|
||||
void OnMouseDown(MouseEventArgs e);
|
||||
|
||||
void OnMouseDragged(MouseEventArgs e);
|
||||
|
||||
void OnMouseUp(MouseEventArgs e);
|
||||
|
||||
void OnMouseDoubleClick(MouseEventArgs e);
|
||||
|
||||
void OnGotFocus(EventArgs e);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,382 @@
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace Pulsar.Server.Controls.HexEditor
|
||||
{
|
||||
public class StringViewHandler
|
||||
{
|
||||
#region Field
|
||||
|
||||
/// <summary>
|
||||
/// Contains the boundary of
|
||||
/// a single line
|
||||
/// </summary>
|
||||
Rectangle _recStringView;
|
||||
|
||||
/// <summary>
|
||||
/// Contains the format of the
|
||||
/// string to be used in the
|
||||
/// string view
|
||||
/// </summary>
|
||||
StringFormat _stringFormat;
|
||||
|
||||
private HexEditor _editor;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Properties
|
||||
|
||||
public int MaxWidth
|
||||
{
|
||||
get { return _recStringView.X + _recStringView.Width; }
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructor
|
||||
|
||||
public StringViewHandler(HexEditor editor)
|
||||
{
|
||||
_editor = editor;
|
||||
|
||||
//Set String format for the values
|
||||
_stringFormat = new StringFormat(StringFormat.GenericTypographic);
|
||||
_stringFormat.Alignment = StringAlignment.Center;
|
||||
_stringFormat.LineAlignment = StringAlignment.Center;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region KeyMouseEvents
|
||||
|
||||
#region Key
|
||||
|
||||
public void OnKeyPress(KeyPressEventArgs e)
|
||||
{
|
||||
if (!Char.IsControl(e.KeyChar))
|
||||
{
|
||||
HandleUserInput(e.KeyChar);
|
||||
}
|
||||
}
|
||||
|
||||
public void OnKeyDown(KeyEventArgs e)
|
||||
{
|
||||
if (e.KeyCode == Keys.Delete || e.KeyCode == Keys.Back)
|
||||
{
|
||||
if (_editor.SelectionLength > 0)
|
||||
{
|
||||
//Remove the selected bytes
|
||||
HandleUserRemove();
|
||||
int index = _editor.CaretIndex;
|
||||
Point newLocation = GetCaretLocation(index);
|
||||
_editor.SetCaretStart(index, newLocation);
|
||||
}
|
||||
else if (_editor.CaretIndex < _editor.LastVisibleByte && e.KeyCode == Keys.Delete)
|
||||
{
|
||||
//Remove the byte after the caret
|
||||
_editor.RemoveByteAt(_editor.CaretIndex);
|
||||
Point newLocation = GetCaretLocation(_editor.CaretIndex);
|
||||
_editor.SetCaretStart(_editor.CaretIndex, newLocation);
|
||||
}
|
||||
else if (_editor.CaretIndex > 0 && e.KeyCode == Keys.Back)
|
||||
{
|
||||
//Remove byte before the caret
|
||||
int index = _editor.CaretIndex - 1;
|
||||
_editor.RemoveByteAt(index);
|
||||
Point newLocation = GetCaretLocation(index);
|
||||
_editor.SetCaretStart(index, newLocation);
|
||||
}
|
||||
}
|
||||
else if (e.KeyCode == Keys.Up && (_editor.CaretIndex - _editor.BytesPerLine) >= 0)
|
||||
{
|
||||
int index = _editor.CaretIndex - _editor.BytesPerLine;
|
||||
|
||||
//Check ig caret is att the end of the line
|
||||
if (index % _editor.BytesPerLine == 0 && _editor.CaretPosX >= _recStringView.X + _recStringView.Width)
|
||||
{
|
||||
Point position = new Point(_editor.CaretPosX, _editor.CaretPosY - _recStringView.Height);
|
||||
|
||||
//check that this is not the last row (nothing above)
|
||||
if (index == 0)
|
||||
{
|
||||
//Last row do not change index and position
|
||||
position = new Point(_editor.CaretPosX, _editor.CaretPosY);
|
||||
index = _editor.BytesPerLine;
|
||||
}
|
||||
|
||||
if (e.Shift)
|
||||
_editor.SetCaretEnd(index, position);
|
||||
else
|
||||
_editor.SetCaretStart(index, position);
|
||||
}
|
||||
else
|
||||
{
|
||||
HandleArrowKeys(index, e.Shift);
|
||||
}
|
||||
}
|
||||
else if (e.KeyCode == Keys.Down && (_editor.CaretIndex - 1) / _editor.BytesPerLine < _editor.HexTableLength / _editor.BytesPerLine)
|
||||
{
|
||||
int index = _editor.CaretIndex + _editor.BytesPerLine;
|
||||
|
||||
if (index > _editor.HexTableLength)
|
||||
{
|
||||
index = _editor.HexTableLength;
|
||||
HandleArrowKeys(index, e.Shift);
|
||||
}
|
||||
else
|
||||
{
|
||||
Point position = new Point(_editor.CaretPosX, _editor.CaretPosY + _recStringView.Height);
|
||||
|
||||
if (e.Shift)
|
||||
_editor.SetCaretEnd(index, position);
|
||||
else
|
||||
_editor.SetCaretStart(index, position);
|
||||
}
|
||||
}
|
||||
else if (e.KeyCode == Keys.Left && (_editor.CaretIndex - 1) >= 0)
|
||||
{
|
||||
int index = _editor.CaretIndex - 1;
|
||||
HandleArrowKeys(index, e.Shift);
|
||||
}
|
||||
else if (e.KeyCode == Keys.Right && (_editor.CaretIndex + 1) <= _editor.LastVisibleByte)
|
||||
{
|
||||
int index = _editor.CaretIndex + 1;
|
||||
HandleArrowKeys(index, e.Shift);
|
||||
}
|
||||
}
|
||||
|
||||
public void HandleArrowKeys(int index, bool isShiftDown)
|
||||
{
|
||||
Point newLocation = GetCaretLocation(index);
|
||||
if (isShiftDown)
|
||||
_editor.SetCaretEnd(index, newLocation);
|
||||
else
|
||||
_editor.SetCaretStart(index, newLocation);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Mouse
|
||||
|
||||
public void OnMouseDown(int x, int y)
|
||||
{
|
||||
int iX = (x - _recStringView.X) / (int)_editor.CharSize.Width;
|
||||
int iY = (y - _recStringView.Y) / _recStringView.Height;
|
||||
|
||||
//Check that values are good
|
||||
iX = iX > _editor.BytesPerLine ? _editor.BytesPerLine : iX;
|
||||
iX = iX < 0 ? 0 : iX;
|
||||
iY = iY > _editor.MaxBytesV ? _editor.MaxBytesV : iY;
|
||||
iY = iY < 0 ? 0 : iY;
|
||||
|
||||
//Make sure values are withing the given bounds
|
||||
if ((_editor.LastVisibleByte - _editor.FirstVisibleByte) / _editor.BytesPerLine <= iY)
|
||||
{
|
||||
//Check that column is not greater than max
|
||||
if ((_editor.LastVisibleByte - _editor.FirstVisibleByte) % _editor.BytesPerLine <= iX)
|
||||
{
|
||||
iX = (_editor.LastVisibleByte - _editor.FirstVisibleByte) % _editor.BytesPerLine;
|
||||
}
|
||||
iY = (_editor.LastVisibleByte - _editor.FirstVisibleByte) / _editor.BytesPerLine;
|
||||
}
|
||||
|
||||
//Get the smallest possible location (do not want to exceed the max)
|
||||
int index = Math.Min(_editor.LastVisibleByte, _editor.FirstVisibleByte + iX + iY * _editor.BytesPerLine);
|
||||
|
||||
int xPos = (iX * (int)_editor.CharSize.Width) + _recStringView.X;
|
||||
int yPos = (iY * _recStringView.Height) + _recStringView.Y;
|
||||
|
||||
_editor.SetCaretStart(index, new Point(xPos, yPos));
|
||||
}
|
||||
|
||||
public void OnMouseDragged(int x, int y)
|
||||
{
|
||||
int iX = (x - _recStringView.X) / (int)_editor.CharSize.Width;
|
||||
int iY = (y - _recStringView.Y) / _recStringView.Height;
|
||||
|
||||
//Check that values are good
|
||||
iX = iX > _editor.BytesPerLine ? _editor.BytesPerLine : iX;
|
||||
iX = iX < 0 ? 0 : iX;
|
||||
iY = iY > _editor.MaxBytesV ? _editor.MaxBytesV : iY;
|
||||
|
||||
if (_editor.FirstVisibleByte > 0)
|
||||
{
|
||||
iY = iY < 0 ? -1 : iY;
|
||||
}
|
||||
else
|
||||
{
|
||||
iY = iY < 0 ? 0 : iY;
|
||||
}
|
||||
|
||||
//Make sure values are withing the given bounds
|
||||
if ((_editor.LastVisibleByte - _editor.FirstVisibleByte) / _editor.BytesPerLine <= iY)
|
||||
{
|
||||
//Check that column is not greater than max
|
||||
if ((_editor.LastVisibleByte - _editor.FirstVisibleByte) % _editor.BytesPerLine <= iX)
|
||||
{
|
||||
iX = (_editor.LastVisibleByte - _editor.FirstVisibleByte) % _editor.BytesPerLine;
|
||||
}
|
||||
iY = (_editor.LastVisibleByte - _editor.FirstVisibleByte) / _editor.BytesPerLine;
|
||||
}
|
||||
|
||||
//Get the smallest possible location (do not want to exceed the max)
|
||||
int index = Math.Min(_editor.LastVisibleByte, _editor.FirstVisibleByte + iX + iY * _editor.BytesPerLine);
|
||||
|
||||
int xPos = (iX * (int)_editor.CharSize.Width) + _recStringView.X;
|
||||
int yPos = (iY * _recStringView.Height) + _recStringView.Y;
|
||||
|
||||
_editor.SetCaretEnd(index, new Point(xPos, yPos));
|
||||
}
|
||||
|
||||
public void OnMouseDoubleClick()
|
||||
{
|
||||
if (_editor.CaretIndex < _editor.LastVisibleByte)
|
||||
{
|
||||
int index = _editor.CaretIndex + 1;
|
||||
Point newLocation = GetCaretLocation(index);
|
||||
_editor.SetCaretEnd(index, newLocation);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Focus
|
||||
|
||||
public void Focus()
|
||||
{
|
||||
int index = _editor.CaretIndex;
|
||||
Point location = GetCaretLocation(index);
|
||||
_editor.SetCaretStart(index, location);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#endregion
|
||||
|
||||
#region Paint
|
||||
|
||||
public void Update(int startPositionX, Rectangle area)
|
||||
{
|
||||
_recStringView = new Rectangle(
|
||||
startPositionX,
|
||||
area.Y,
|
||||
(int)(_editor.CharSize.Width * _editor.BytesPerLine),
|
||||
(int)(_editor.CharSize.Height) - 2
|
||||
);
|
||||
|
||||
_recStringView.X += _editor.EntityMargin;
|
||||
}
|
||||
|
||||
public void Paint(Graphics g, int index, int startIndex)
|
||||
{
|
||||
Point columnAndRow = GetByteColumnAndRow(index);
|
||||
|
||||
if (_editor.IsSelected(index + startIndex))
|
||||
{
|
||||
PaintByteAsSelected(g, columnAndRow, (index + startIndex));
|
||||
}
|
||||
else
|
||||
{
|
||||
PaintByte(g, columnAndRow, (index + startIndex));
|
||||
}
|
||||
}
|
||||
|
||||
private void PaintByteAsSelected(Graphics g, Point point, int index)
|
||||
{
|
||||
SolidBrush backBrush = new SolidBrush(_editor.SelectionBackColor);
|
||||
SolidBrush textBrush = new SolidBrush(_editor.SelectionForeColor);
|
||||
RectangleF drawSurface = GetBound(point);
|
||||
char value = _editor.GetByteAsChar(index);
|
||||
string strValue = (Char.IsControl(value) ? "." : value.ToString());
|
||||
|
||||
g.FillRectangle(backBrush, drawSurface);
|
||||
g.DrawString(strValue, _editor.Font, textBrush, drawSurface, _stringFormat);
|
||||
}
|
||||
|
||||
private void PaintByte(Graphics g, Point point, int index)
|
||||
{
|
||||
SolidBrush brush = new SolidBrush(_editor.ForeColor);
|
||||
RectangleF drawLocation = GetBound(point);
|
||||
char value = _editor.GetByteAsChar(index);
|
||||
string strValue = (Char.IsControl(value) ? "." : value.ToString());
|
||||
|
||||
g.DrawString(strValue, _editor.Font, brush, drawLocation, _stringFormat);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Caret
|
||||
|
||||
/// <summary>
|
||||
/// Get the caret current location
|
||||
/// in the given bound.
|
||||
/// </summary>
|
||||
private Point GetCaretLocation(int index)
|
||||
{
|
||||
int xPos = _recStringView.X + ((int)_editor.CharSize.Width * (index % _editor.BytesPerLine));
|
||||
int yPos = _recStringView.Y + ((int)_recStringView.Height * ((index - (_editor.FirstVisibleByte + index % _editor.BytesPerLine)) / _editor.BytesPerLine));
|
||||
|
||||
Point ret = new Point(xPos, yPos);
|
||||
return ret;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Misc
|
||||
|
||||
private void HandleUserRemove()
|
||||
{
|
||||
//Calculate where to position the caret after the removal
|
||||
int index = _editor.SelectionStart;
|
||||
Point position = GetCaretLocation(index);
|
||||
//Remove all of the selected bytes
|
||||
_editor.RemoveSelectedBytes();
|
||||
|
||||
//Set the new position of the caret
|
||||
_editor.SetCaretStart(index, position);
|
||||
}
|
||||
|
||||
private void HandleUserInput(char key)
|
||||
{
|
||||
if (!_editor.CaretFocused)
|
||||
return;
|
||||
|
||||
HandleUserRemove();
|
||||
|
||||
byte newByte = Convert.ToByte(key);
|
||||
|
||||
if (_editor.HexTableLength <= 0)
|
||||
_editor.AppendByte(newByte);
|
||||
else
|
||||
_editor.InsertByte(_editor.CaretIndex, newByte);
|
||||
|
||||
int index = _editor.CaretIndex + 1;
|
||||
Point newLocation = GetCaretLocation(index);
|
||||
_editor.SetCaretStart(index, newLocation);
|
||||
}
|
||||
|
||||
private Point GetByteColumnAndRow(int index)
|
||||
{
|
||||
int column = index % _editor.BytesPerLine;
|
||||
int row = index / _editor.BytesPerLine;
|
||||
|
||||
Point ret = new Point(column, row);
|
||||
return ret;
|
||||
}
|
||||
|
||||
private RectangleF GetBound(Point point)
|
||||
{
|
||||
RectangleF ret = new RectangleF(
|
||||
_recStringView.X + (point.X * (int)_editor.CharSize.Width),
|
||||
_recStringView.Y + (point.Y * _recStringView.Height),
|
||||
_editor.CharSize.Width,
|
||||
_recStringView.Height
|
||||
);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user