WpfHexEditorControl

Wpf Hexeditor is a powerful and fully customisable user control for editing file or stream as hexadecimal, decimal and binary. Can be used in Wpf or WinForm application

View project on GitHub

Core/Interfaces

Interface definitions for core hex editor components.

๐Ÿ“ Contents

  • IByte.cs - Byte control interface
    • Defines: Common byte display properties
    • Implemented by: Byte_8bit, Byte_16bit, Byte_32bit controls
    • Properties: Byte value, action type, position, read-only state
    • Events: ByteModified, MouseSelection, RightClick
  • IByteControl.cs - Byte manipulation interface
    • Defines: Byte editing operations
    • Methods: UpdateByte, Clear, GetByte, SetByte
    • Properties: IsEditable, IsSelected, IsFocused
    • Used by: All byte display controls
  • IByteModified.cs - Modification tracking interface
    • Defines: Change tracking contract
    • Properties: Original value, modified value, action type
    • Implemented by: ByteModified class
    • Used by: Undo/redo system

๐ŸŽฏ Purpose

These interfaces provide contracts for core hex editor functionality, enabling:

  • Polymorphism: Different byte controls with common interface
  • Testability: Mock implementations for unit tests
  • Extensibility: Create custom byte controls
  • Type Safety: Compile-time checking of implementations

๐Ÿ”— Implementation Hierarchy

IByte + IByteControl
    โ”œโ”€โ”€ Implemented by: Byte_8bit
    โ”œโ”€โ”€ Implemented by: Byte_16bit
    โ””โ”€โ”€ Implemented by: Byte_32bit

IByteModified
    โ””โ”€โ”€ Implemented by: ByteModified

๐ŸŽ“ Usage Example

IByte Interface:

public interface IByte
{
    byte? Byte { get; set; }
    long Position { get; set; }
    ByteAction Action { get; set; }
    bool IsSelected { get; set; }
    bool ReadOnlyMode { get; set; }

    event EventHandler ByteModified;
    event EventHandler MouseSelection;
    event EventHandler RightClick;
}

// Implementation
public class Byte_8bit : UserControl, IByte, IByteControl
{
    public byte? Byte { get; set; }
    public long Position { get; set; }
    // ... other properties and methods
}

IByteModified Interface:

public interface IByteModified
{
    byte OriginalByte { get; }
    byte ModifiedByte { get; set; }
    ByteAction Action { get; set; }
    long Position { get; }
}

// Implementation
public class ByteModified : IByteModified
{
    public byte OriginalByte { get; }
    public byte ModifiedByte { get; set; }
    public ByteAction Action { get; set; }
    public long Position { get; }
}

Polymorphic Usage:

// Work with any byte control
void ProcessByteControl(IByte byteControl)
{
    if (byteControl.Byte.HasValue)
    {
        Console.WriteLine($"Position {byteControl.Position}: 0x{byteControl.Byte:X2}");
    }
}

// Use with different control types
ProcessByteControl(new Byte_8bit());
ProcessByteControl(new Byte_16bit());
ProcessByteControl(new Byte_32bit());

๐Ÿ“ Interface Design Principles

IByte - Display Contract:

  • Purpose: Define what a byte control must display
  • Properties: Value, position, state, appearance
  • Events: User interaction notifications
  • Pattern: View-oriented interface

IByteControl - Behavior Contract:

  • Purpose: Define how to manipulate a byte control
  • Methods: CRUD operations for byte values
  • Pattern: Controller-oriented interface

IByteModified - Data Contract:

  • Purpose: Track change history
  • Properties: Before/after values, action type
  • Pattern: Data-oriented interface

โœจ Benefits

  • Decoupling: UI separated from data model
  • Flexibility: Swap implementations without breaking code
  • Testing: Easy to create mocks and stubs
  • Documentation: Interfaces serve as API contracts
  • IntelliSense: Full IDE support for implementers

๐Ÿงช Testing with Interfaces

// Mock implementation for testing
public class MockByteControl : IByte, IByteControl
{
    public byte? Byte { get; set; }
    public long Position { get; set; }
    public bool ReadOnlyMode { get; set; }
    // ... minimal implementation for tests

    public event EventHandler ByteModified;
    public void TriggerByteModified() => ByteModified?.Invoke(this, EventArgs.Empty);
}

// Test with mock
[Fact]
public void TestByteModification()
{
    var mock = new MockByteControl { Byte = 0xFF };
    bool eventRaised = false;
    mock.ByteModified += (s, e) => eventRaised = true;

    mock.TriggerByteModified();

    Assert.True(eventRaised);
}

๐ŸŽจ Extension Point

Want to create a custom byte control?

public class MyCustomByteControl : UserControl, IByte, IByteControl
{
    // Implement interface members
    // Add custom visualization or behavior
}

โœจ Interface contracts for extensible and testable hex editor components