Skip to content

Commit

Permalink
Image preview in the list.
Browse files Browse the repository at this point in the history
  • Loading branch information
TBXin committed Mar 9, 2016
1 parent 964380c commit 3da1c7b
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 61 deletions.
2 changes: 1 addition & 1 deletion src/NFirmwareEditor/Core/Consts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
internal static class Consts
{
internal const string ApplicationTitle = "NFirmwareEditor";
internal const string ApplicationVersion = "2.1";
internal const string ApplicationVersion = "2.2";
internal const string FirmwareFilter = "Firmware file|*.bin";
internal const string ExportImageFilter = "Exported images|*.images";
internal const string Encrypted = "encrypted";
Expand Down
48 changes: 28 additions & 20 deletions src/NFirmwareEditor/MainWindow.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

89 changes: 81 additions & 8 deletions src/NFirmwareEditor/MainWindow.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Windows.Forms;
using JetBrains.Annotations;
Expand All @@ -15,6 +16,9 @@ namespace NFirmwareEditor
{
public partial class MainWindow : Form
{
private const int ImagedListBoxItemMaxHeight = 24 * 2;
private const int ImagedListBoxItemImageMargin = 6;

private readonly ConfigurationManager m_configurationManager = new ConfigurationManager();
private readonly FirmwareDefinitionManager m_firmwareDefinitionManager = new FirmwareDefinitionManager();
private readonly FirmwareLoader m_loader = new FirmwareLoader(new FirmwareEncoder());
Expand Down Expand Up @@ -50,7 +54,7 @@ public IDictionary<int, Image> ImageCacheForStringListBox

public IEnumerable<FirmwareImageMetadata> CurrentImageBlockForStrings
{
get { return Block1StringListBox.Visible ? m_firmware.Block1Images : m_firmware.Block1Images; }
get { return Block1StringListBox.Visible ? m_firmware.Block1Images : m_firmware.Block2Images; }
}

[NotNull]
Expand All @@ -70,9 +74,10 @@ public FirmwareImageMetadata LastSelectedImageMetadata
{
get
{
return ImageListBox.Items.Count == 0 || ImageListBox.SelectedIndices.Count == 0
? null
: ImageListBox.Items[ImageListBox.SelectedIndices[ImageListBox.SelectedIndices.Count - 1]] as FirmwareImageMetadata;
if (ImageListBox.Items.Count == 0 || ImageListBox.SelectedIndices.Count == 0) return null;

var item = ImageListBox.Items[ImageListBox.SelectedIndices[ImageListBox.SelectedIndices.Count - 1]] as ImagedListBoxItem<FirmwareImageMetadata>;
return item != null ? item.Value : null;
}
}

Expand All @@ -97,10 +102,10 @@ public List<FirmwareImageMetadata> SelectedImageMetadata
var result = new List<FirmwareImageMetadata>();
foreach (int selectedIndex in ImageListBox.SelectedIndices)
{
var metadata = ImageListBox.Items[selectedIndex] as FirmwareImageMetadata;
var metadata = ImageListBox.Items[selectedIndex] as ImagedListBoxItem<FirmwareImageMetadata>;
if (metadata == null) continue;

result.Add(metadata);
result.Add(metadata.Value);
}
return result;
}
Expand Down Expand Up @@ -134,6 +139,59 @@ private void InitializeControls()
StringListBox.Focus();
StringListBox_SelectedValueChanged(StringListBox, EventArgs.Empty);
};

Block1ImageListBox.DrawMode = DrawMode.OwnerDrawVariable;
Block1ImageListBox.MeasureItem += ImageListBox_MeasureItem;
Block1ImageListBox.DrawItem += ImageListBox_DrawItem;

Block2ImageListBox.DrawMode = DrawMode.OwnerDrawVariable;
Block2ImageListBox.MeasureItem += ImageListBox_MeasureItem;
Block2ImageListBox.DrawItem += ImageListBox_DrawItem;
}

private void ImageListBox_DrawItem(object sender, DrawItemEventArgs e)
{
var listBox = sender as ListBox;

if (listBox == null) return;
if (e.Index < 0) return;

var item = listBox.Items[e.Index] as ImagedListBoxItem<FirmwareImageMetadata>;
if (item == null) return;

e.Graphics.SmoothingMode = SmoothingMode.None;
e.Graphics.InterpolationMode = InterpolationMode.Low;
e.Graphics.CompositingQuality = CompositingQuality.HighSpeed;
e.DrawBackground();

var itemText = item.ToString();
var textSize = e.Graphics.MeasureString(itemText, e.Font);

try
{
if (item.Image != null)
{
if (item.Image.Width > ImagedListBoxItemMaxHeight || item.Image.Height > ImagedListBoxItemMaxHeight)
{
e.Graphics.DrawImage(item.Image, e.Bounds.X + ImagedListBoxItemImageMargin, e.Bounds.Y + ImagedListBoxItemImageMargin, ImagedListBoxItemMaxHeight, ImagedListBoxItemMaxHeight);
}
else
{
e.Graphics.DrawImage(item.Image, e.Bounds.X + ImagedListBoxItemImageMargin, e.Bounds.Y + (int)(e.Bounds.Height / 2f - item.Image.Height / 2f), item.Image.Width, item.Image.Height);
}
}
}
catch(ObjectDisposedException)
{
}

e.Graphics.DrawString(itemText, e.Font, new SolidBrush(e.ForeColor), e.Bounds.X + ImagedListBoxItemMaxHeight + ImagedListBoxItemImageMargin * 2, e.Bounds.Y + (int)(e.Bounds.Height / 2f - textSize.Height / 2f));
e.DrawFocusRectangle();
}

private void ImageListBox_MeasureItem(object sender, MeasureItemEventArgs e)
{
e.ItemHeight = ImagedListBoxItemMaxHeight + ImagedListBoxItemImageMargin * 2;
}

private void ResetWorkspace()
Expand Down Expand Up @@ -250,8 +308,8 @@ private void OpenDialogAndReadFirmwareOnOk(string firmwareName, Func<string, Fir
}

RebuildImageCache(m_firmware);
FillListBox(Block2ImageListBox, m_firmware.Block2Images, true);
FillListBox(Block1ImageListBox, m_firmware.Block1Images, true);
FillListBox(Block2ImageListBox, m_firmware.Block2Images.Select(x => new ImagedListBoxItem<FirmwareImageMetadata>(x, m_block2ImageCache[x.Index], string.Format("0x{0:X2}", x.Index))), true);
FillListBox(Block1ImageListBox, m_firmware.Block1Images.Select(x => new ImagedListBoxItem<FirmwareImageMetadata>(x, m_block1ImageCache[x.Index], string.Format("0x{0:X2}", x.Index))), true);
FillListBox(Block2StringListBox, m_firmware.Block2Strings, false);
FillListBox(Block1StringListBox, m_firmware.Block1Strings, false);

Expand Down Expand Up @@ -318,6 +376,19 @@ private void FillListBox(ListBox listBox, IEnumerable<object> items, bool select
try
{
var item = ImageListBox.Items[imageMetadata.Index - 1] as ImagedListBoxItem<FirmwareImageMetadata>;
if (item != null)
{
if(item.Image != null) item.Image.Dispose();
item.Image = cachedImage;
ImageListBox.Invoke(new Action(() =>
{
var itemRect = ImageListBox.GetItemRectangle(imageMetadata.Index - 1);
ImageListBox.Invalidate(itemRect);
}));
}
foreach (var icb in CharLayoutPanel.Controls.OfType<ImagedComboBox>())
{
icb.Items[imageMetadata.Index - 1].Image.Dispose();
Expand Down Expand Up @@ -368,8 +439,10 @@ private void BlockImageCheckBox_CheckedChanged(object sender, EventArgs e)
if (currentListBoxSelectedIndices.Count != 0)
{
m_imageListBoxIsUpdating = true;
ImageListBox.BeginUpdate();
ImageListBox.SelectedIndices.Clear();
ImageListBox.SelectedIndices.AddRange(currentListBoxSelectedIndices.Where(x => ImageListBox.Items.Count > x));
ImageListBox.EndUpdate();

ImagePixelGrid.Data = ImagePreviewPixelGrid.Data = LastSelectedImageMetadata != null
? m_firmware.ReadImage(LastSelectedImageMetadata)
Expand Down
1 change: 1 addition & 0 deletions src/NFirmwareEditor/NFirmwareEditor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
<Compile Include="UI\ImagedComboBox.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="UI\ImagedListBoxItem.cs" />
<Compile Include="UI\PixelGrid.cs">
<SubType>Component</SubType>
</Compile>
Expand Down
50 changes: 18 additions & 32 deletions src/NFirmwareEditor/UI/ImagedComboBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,13 @@ protected override void OnDrawItem(DrawItemEventArgs e)

try
{
e.Graphics.DrawImage(item.Image, e.Bounds.X + 40, e.Bounds.Y + 2, item.Image.Width, item.Image.Height);
if (item.Image != null)
{
e.Graphics.DrawImage(item.Image, e.Bounds.X + 40, e.Bounds.Y + 2, item.Image.Width, item.Image.Height);
}
}
catch
catch(ObjectDisposedException)
{
// Ignore
}
}
e.DrawFocusRectangle();
Expand All @@ -96,6 +98,19 @@ public class ImagedComboBoxItem
private Image m_image;
private readonly string m_dispayName;

/// <summary>
/// Constructor item with image.
/// </summary>
/// <param name="value">Item value.</param>
/// <param name="image">Item image.</param>
/// <param name="dispayName">Display name.</param>
public ImagedComboBoxItem(object value, Image image, string dispayName = null)
{
m_value = value;
m_image = image;
m_dispayName = dispayName;
}

/// <summary>
/// ComobBox Item.
/// </summary>
Expand All @@ -114,35 +129,6 @@ public Image Image
set { m_image = value; }
}

public ImagedComboBoxItem()
{
m_value = string.Empty;
m_image = new Bitmap(1, 1);
}

/// <summary>
/// Constructor item without image.
/// </summary>
/// <param name="value">Item value.</param>
public ImagedComboBoxItem(object value)
{
m_value = value;
m_image = new Bitmap(1, 1);
}

/// <summary>
/// Constructor item with image.
/// </summary>
/// <param name="value">Item value.</param>
/// <param name="image">Item image.</param>
/// <param name="dispayName">Display name.</param>
public ImagedComboBoxItem(object value, Image image, string dispayName = null)
{
m_value = value;
m_image = image;
m_dispayName = dispayName;
}

public override string ToString()
{
return m_dispayName ?? (m_value != null ? m_value.ToString() : string.Empty);
Expand Down
Loading

0 comments on commit 3da1c7b

Please sign in to comment.