Skip to content

Commit

Permalink
Replaced hex grid with simpler design, less controls. Fixes #386
Browse files Browse the repository at this point in the history
  • Loading branch information
distantcam committed Sep 22, 2014
1 parent b7188b1 commit 7233fd2
Show file tree
Hide file tree
Showing 13 changed files with 247 additions and 273 deletions.
Original file line number Diff line number Diff line change
@@ -1,73 +1,41 @@
<UserControl x:Class="Particular.ServiceInsight.Desktop.MessageViewers.HexViewer.HexContentView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Particular.ServiceInsight.Desktop.MessageViewers.HexViewer"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">

<UserControl.Resources>
<DataTemplate DataType="{x:Type local:HexPart}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="60"/>
<ColumnDefinition MinWidth="350" Width="520" />
<ColumnDefinition Width="200"/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding LineNumber, Mode=OneWay}"
Margin="5"
FontFamily="{StaticResource MessageBodyFontFamily}"
FontSize="{StaticResource MessageBodyFontSize}"
Foreground="DarkCyan" Grid.Column="0"/>
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
d:DesignHeight="300"
d:DesignWidth="300"
mc:Ignorable="d">

<!--Hex display of the byte Representative of the Hex char-->
<ItemsControl ItemsSource="{Binding Numbers}"
Grid.Column="1"
Background="{x:Null}"
BorderThickness="0"
BorderBrush="{x:Null}"
IsHitTestVisible="False"
ScrollViewer.HorizontalScrollBarVisibility="Hidden">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Margin="5" Text="{Binding Hex}"
FontFamily="{StaticResource MessageBodyFontFamily}"
FontSize="{StaticResource MessageBodyFontSize}"
Foreground="Black" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<ScrollViewer>

<!--Text display of the byte-->
<ItemsControl ItemsSource="{Binding Numbers}" Grid.Column="2" Margin="20,0,0,0" BorderBrush="{x:Null}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Margin="5" Background="Transparent" Text="{Binding Text}"
FontFamily="{StaticResource MessageBodyFontFamily}"
FontSize="{StaticResource MessageBodyFontSize}"
Foreground="Gray" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<local:HexControl Data="{Binding SelectedMessage}">
<local:HexControl.HexLineStyle>
<Style TargetType="TextBlock">
<Setter Property="Margin" Value="5" />
<Setter Property="FontFamily" Value="{StaticResource MessageBodyFontFamily}" />
<Setter Property="FontSize" Value="{StaticResource MessageBodyFontSize}" />
<Setter Property="Foreground" Value="DarkCyan" />
</Style>
</local:HexControl.HexLineStyle>
<local:HexControl.HexNumberStyle>
<Style TargetType="TextBlock">
<Setter Property="Margin" Value="5" />
<Setter Property="FontFamily" Value="{StaticResource MessageBodyFontFamily}" />
<Setter Property="FontSize" Value="{StaticResource MessageBodyFontSize}" />
<Setter Property="Foreground" Value="Black" />
</Style>
</local:HexControl.HexNumberStyle>
<local:HexControl.HexCharStyle>
<Style TargetType="TextBlock">
<Setter Property="Margin" Value="5" />
<Setter Property="FontFamily" Value="{StaticResource MessageBodyFontFamily}" />
<Setter Property="FontSize" Value="{StaticResource MessageBodyFontSize}" />
<Setter Property="Foreground" Value="Gray" />
</Style>
</local:HexControl.HexCharStyle>
</local:HexControl>

</Grid>
</DataTemplate>
</UserControl.Resources>

<ScrollViewer>
<ItemsControl x:Name="listBox"
Background="White"
ItemsSource="{Binding HexParts}"/>
</ScrollViewer>
</UserControl>
</UserControl>
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
namespace Particular.ServiceInsight.Desktop.MessageViewers.HexViewer
{
public partial class HexContentView : IHexContentView
public partial class HexContentView
{
public HexContentView()
{
InitializeComponent();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,45 +1,13 @@
namespace Particular.ServiceInsight.Desktop.MessageViewers.HexViewer
{
using System;
using System.Text;
using Caliburn.Micro;
using Events;
using ExtensionMethods;

public class HexContentViewModel : Screen,
IHandle<SelectedMessageChanged>
public class HexContentViewModel : Screen, IHandle<SelectedMessageChanged>
{
internal static Func<byte, string> ByteToStringConverter;
static Encoding Encoding;

IHexContentView view;

static HexContentViewModel()
{
Encoding = new UTF8Encoding(false);
ByteToStringConverter = byteValue => Encoding.GetString(new[] { byteValue });
}

public HexContentViewModel()
{
HexParts = new BindableCollection<HexPart>();
}

public byte[] SelectedMessage { get; set; }

public IObservableCollection<HexPart> HexParts
{
get;
private set;
}

protected override void OnViewAttached(object view, object context)
{
base.OnViewAttached(view, context);
this.view = (IHexContentView)view;
OnSelectedMessageChanged();
}

protected override void OnActivate()
{
base.OnActivate();
Expand All @@ -57,70 +25,5 @@ public void Handle(SelectedMessageChanged @event)

SelectedMessage = body;
}

public void OnSelectedMessageChanged()
{
if (view == null || SelectedMessage == null)
return;

DisplayMessage();
}

void DisplayMessage()
{
ClearHexParts();
CreateHexParts();
}

void ClearHexParts()
{
HexParts.Clear();
}

void CreateHexParts()
{
var columnNumber = 0;
var lineNumber = 1;
var hexLine = new HexPart(lineNumber);

foreach (var currentByte in SelectedMessage)
{
if (!HexParts.Contains(hexLine))
HexParts.Add(hexLine);

var hexChar = new HexNumber();

AppendHex(hexChar, currentByte);
AppendText(hexChar, currentByte);

hexLine.Numbers.Add(hexChar);
columnNumber++;

if (columnNumber == 16)
{
lineNumber++;
columnNumber = 0;
hexLine = new HexPart(lineNumber);
}
}
}

static void AppendText(HexNumber number, byte b)
{
var c = ByteToStringConverter.TryGetValue(b, " ");
if (c == "\r" || c == "\n" || c == "\t")
{
number.Text = ".";
}
else
{
number.Text = c;
}
}

static void AppendHex(HexNumber hexValue, byte b)
{
hexValue.Hex = string.Format(b < 0x10 ? "0{0:X000} " : "{0:X000} ", b);
}
}
}
124 changes: 124 additions & 0 deletions src/ServiceInsight.Desktop/MessageViewers/HexViewer/HexControl.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
namespace Particular.ServiceInsight.Desktop.MessageViewers.HexViewer
{
using System;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using ExtensionMethods;

[TemplatePart(Name = "PART_HEXGRID", Type = typeof(Grid))]
public class HexControl : Control
{
public static readonly DependencyProperty DataProperty = DependencyProperty.Register(
"Data", typeof(byte[]), typeof(HexControl), new PropertyMetadata(default(byte[]), DataChanged));

static void DataChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
{
((HexControl)dependencyObject).OnDataChanged();
}

public byte[] Data
{
get { return (byte[])GetValue(DataProperty); }
set { SetValue(DataProperty, value); }
}

public static readonly DependencyProperty HexLineStyleProperty = DependencyProperty.Register(
"HexLineStyle", typeof(Style), typeof(HexControl), new PropertyMetadata(default(Style)));

public Style HexLineStyle
{
get { return (Style)GetValue(HexLineStyleProperty); }
set { SetValue(HexLineStyleProperty, value); }
}

public static readonly DependencyProperty HexNumberStyleProperty = DependencyProperty.Register(
"HexNumberStyle", typeof(Style), typeof(HexControl), new PropertyMetadata(default(Style)));

public Style HexNumberStyle
{
get { return (Style)GetValue(HexNumberStyleProperty); }
set { SetValue(HexNumberStyleProperty, value); }
}

public static readonly DependencyProperty HexCharStyleProperty = DependencyProperty.Register(
"HexCharStyle", typeof(Style), typeof(HexControl), new PropertyMetadata(default(Style)));

public Style HexCharStyle
{
get { return (Style)GetValue(HexCharStyleProperty); }
set { SetValue(HexCharStyleProperty, value); }
}

static HexControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(HexControl), new FrameworkPropertyMetadata(typeof(HexControl)));
}

private Grid hexGrid;

public override void OnApplyTemplate()
{
hexGrid = GetTemplateChild("PART_HEXGRID") as Grid;
}

private void OnDataChanged()
{
if (hexGrid == null || Data == null)
return;

hexGrid.Children.Clear();

var lines = (int)Math.Ceiling(Data.Length / 16.0);
var linePadding = (int)Math.Ceiling(Math.Log10(lines + 1));
hexGrid.RowDefinitions.Clear();
for (var i = 0; i < lines; i++)
{
hexGrid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
}

for (var i = 0; i < lines; i++)
{
var line = new TextBlock { Style = HexLineStyle, Text = (i + 1).ToString().PadLeft(linePadding) };
Grid.SetColumn(line, 0);
Grid.SetRow(line, i);
hexGrid.Children.Add(line);
}

for (var i = 0; i < Data.Length; i++)
{
var x = i % 16;
var y = i / 16;

var number = new TextBlock { Style = HexNumberStyle, Text = ToHex(Data[i]) };
Grid.SetColumn(number, x + 1);
Grid.SetRow(number, y);
hexGrid.Children.Add(number);

var chr = new TextBlock { Style = HexCharStyle, Text = ToText(Data[i]) };
Grid.SetColumn(chr, x + 18);
Grid.SetRow(chr, y);
hexGrid.Children.Add(chr);
}
}

static string ToText(byte b)
{
Func<byte, string> ByteToStringConverter = byteValue => Encoding.UTF8.GetString(new[] { byteValue });
var c = ByteToStringConverter.TryGetValue(b, " ");
if (c == "\r" || c == "\n" || c == "\t")
{
return ".";
}
else
{
return c;
}
}

static string ToHex(byte b)
{
return string.Format(b < 0x10 ? "0{0:X000} " : "{0:X000} ", b);
}
}
}

This file was deleted.

16 changes: 0 additions & 16 deletions src/ServiceInsight.Desktop/MessageViewers/HexViewer/HexPart.cs

This file was deleted.

Loading

0 comments on commit 7233fd2

Please sign in to comment.