diff --git a/OpenNest.WinForms.Tests/Controls/PlateListBoxTests.cs b/OpenNest.WinForms.Tests/Controls/PlateListBoxTests.cs
new file mode 100644
index 00000000..e2907070
--- /dev/null
+++ b/OpenNest.WinForms.Tests/Controls/PlateListBoxTests.cs
@@ -0,0 +1,83 @@
+using System.Drawing;
+using OpenNest.Controls;
+using OpenNest.Geometry;
+using OpenNest.Shapes;
+
+namespace OpenNest.WinForms.Tests.Controls;
+
+/// The Plates tab list draws each plate's picture once and redraws it only when it changes.
+public class PlateListBoxTests
+{
+ [Fact]
+ public void ThePictureIsReusedUntilAPartMoves() => RunSta(() =>
+ {
+ var (plate, part) = CreatePlate();
+ using var list = new PlateListBox();
+ list.SetPlates([plate]);
+
+ var first = list.GetPreview(plate);
+ Assert.Same(first, list.GetPreview(plate));
+ var before = Snapshot(first);
+
+ part.Offset(new Vector(30, 0));
+ var moved = list.GetPreview(plate);
+
+ Assert.NotSame(first, moved);
+ Assert.NotEqual(before, Snapshot(moved));
+ });
+
+ [Fact]
+ public void PartsShowInTheirDrawingsColorOnTheSheet() => RunSta(() =>
+ {
+ var (plate, part) = CreatePlate();
+ part.BaseDrawing.Color = Color.FromArgb(255, 200, 30, 30);
+ using var list = new PlateListBox();
+ list.SetPlates([plate]);
+
+ using var image = (Bitmap)list.GetPreview(plate).Clone();
+ var found = false;
+ for (var x = 0; x < image.Width && !found; x++)
+ for (var y = 0; y < image.Height && !found; y++)
+ found = image.GetPixel(x, y).ToArgb() == part.BaseDrawing.Color.ToArgb();
+
+ Assert.True(found);
+ });
+
+ [Fact]
+ public void RowsBelowTheLastPlateAreNoRow() => RunSta(() =>
+ {
+ var (plate, _) = CreatePlate();
+ using var list = new PlateListBox { Height = 400 };
+ list.CreateControl();
+ list.SetPlates([plate]);
+
+ Assert.Equal(0, list.RowAt(new Point(5, 5)));
+ Assert.Equal(-1, list.RowAt(new Point(5, list.ItemHeight + 10)));
+ });
+
+ private static (Plate Plate, Part Part) CreatePlate()
+ {
+ var nest = new Nest("preview");
+ var drawing = new RectangleShape { Name = "SYN", Length = 20, Width = 10 }.GetDrawing();
+ nest.Drawings.Add(drawing);
+ var plate = nest.CreatePlate();
+ plate.Size = new OpenNest.Geometry.Size(60, 120);
+ var part = new Part(drawing);
+ part.Offset(new Vector(5, 5));
+ plate.Parts.Add(part);
+ return (plate, part);
+ }
+
+ private static int[] Snapshot(Image image)
+ {
+ using var bitmap = new Bitmap(image);
+ var pixels = new int[bitmap.Width * bitmap.Height];
+ for (var y = 0; y < bitmap.Height; y++)
+ for (var x = 0; x < bitmap.Width; x++)
+ pixels[y * bitmap.Width + x] = bitmap.GetPixel(x, y).ToArgb();
+ return pixels;
+ }
+
+ private static void RunSta(System.Action testBody) =>
+ StaTestThread.Run(testBody, TimeSpan.FromSeconds(60), "The plate list STA test did not complete.");
+}
diff --git a/OpenNest.WinForms.Tests/Forms/EditNestFocusTraversalTests.cs b/OpenNest.WinForms.Tests/Forms/EditNestFocusTraversalTests.cs
index 6c80e121..cbac64ac 100644
--- a/OpenNest.WinForms.Tests/Forms/EditNestFocusTraversalTests.cs
+++ b/OpenNest.WinForms.Tests/Forms/EditNestFocusTraversalTests.cs
@@ -1,5 +1,6 @@
using System.Reflection;
using System.Windows.Forms;
+using OpenNest.Controls;
using OpenNest.Forms;
namespace OpenNest.WinForms.Tests.Forms;
@@ -136,10 +137,10 @@ public class EditNestFocusTraversalTests
return Assert.IsAssignableFrom(method.Invoke(form, null));
}
- private static ListView PlateList(EditNestForm form) =>
- Assert.IsType(Assert.Single(form.Controls.Find("platesListView", true)));
+ private static PlateListBox PlateList(EditNestForm form) =>
+ Assert.IsType(Assert.Single(form.Controls.Find("platesListView", true)));
- private static void SelectPlatesTab(EditNestForm form, ListView plates)
+ private static void SelectPlatesTab(EditNestForm form, PlateListBox plates)
{
var tabControl = Assert.IsType(Assert.Single(form.Controls.Find("tabControl1", true)));
tabControl.SelectedTab = Assert.IsType(plates.Parent);
diff --git a/OpenNest.WinForms.Tests/Forms/EditNestPlateStockTooltipTests.cs b/OpenNest.WinForms.Tests/Forms/EditNestPlateStockTooltipTests.cs
index c0afdd67..1d87e676 100644
--- a/OpenNest.WinForms.Tests/Forms/EditNestPlateStockTooltipTests.cs
+++ b/OpenNest.WinForms.Tests/Forms/EditNestPlateStockTooltipTests.cs
@@ -1,4 +1,5 @@
using System.Windows.Forms;
+using OpenNest.Controls;
using OpenNest.Forms;
using OpenNest.Shapes;
@@ -22,10 +23,9 @@ public class EditNestPlateStockTooltipTests
form.LedgerLocations["DROP-1182"] = "B3";
form.UpdatePlateList();
- var plates = Assert.IsType(Assert.Single(form.Controls.Find("platesListView", true)));
- Assert.True(plates.ShowItemToolTips);
- Assert.Equal("Stock: DROP-1182 (B3)", plates.Items[0].ToolTipText);
- Assert.Equal("", plates.Items[^1].ToolTipText);
+ var plates = Assert.IsType(Assert.Single(form.Controls.Find("platesListView", true)));
+ Assert.Equal("Stock: DROP-1182 (B3)", plates.GetToolTipText(0));
+ Assert.Equal("", plates.GetToolTipText(plates.Items.Count - 1));
});
private static void RunSta(System.Action testBody) =>
diff --git a/OpenNest.WinForms.Tests/Forms/PlateReorderTests.cs b/OpenNest.WinForms.Tests/Forms/PlateReorderTests.cs
index b53f2801..a15b329d 100644
--- a/OpenNest.WinForms.Tests/Forms/PlateReorderTests.cs
+++ b/OpenNest.WinForms.Tests/Forms/PlateReorderTests.cs
@@ -1,5 +1,6 @@
using System.Drawing;
using System.Windows.Forms;
+using OpenNest.Controls;
using OpenNest.Forms;
namespace OpenNest.WinForms.Tests.Forms;
@@ -18,15 +19,15 @@ public class PlateReorderTests
[Fact]
public void DropSlotFollowsTheRowHalvesAndStopsBeforeTheSentinel() => UndoTestEnv.Run(env =>
{
- var list = (ListView)Assert.Single(env.Editor.Controls.Find("platesListView", true));
+ var list = (PlateListBox)Assert.Single(env.Editor.Controls.Find("platesListView", true));
var movable = env.Editor.PlateManager.MovableCount;
Assert.Equal(env.Nest.Plates.Count - 1, movable);
- var first = list.Items[0].Bounds;
+ var first = list.GetItemRectangle(0);
Assert.Equal(0, env.Editor.GetPlateDropSlot(new Point(5, first.Top + 1)));
Assert.Equal(1, env.Editor.GetPlateDropSlot(new Point(5, first.Bottom - 1)));
- var sentinel = list.Items[movable].Bounds;
+ var sentinel = list.GetItemRectangle(movable);
Assert.Equal(movable, env.Editor.GetPlateDropSlot(new Point(5, sentinel.Top + 1)));
Assert.Equal(movable, env.Editor.GetPlateDropSlot(new Point(5, list.ClientSize.Height - 1)));
});
@@ -41,9 +42,9 @@ public class PlateReorderTests
Assert.Same(plate, env.Nest.Plates[1]);
Assert.Same(plate, env.Editor.PlateManager.CurrentPlate);
- var list = (ListView)Assert.Single(env.Editor.Controls.Find("platesListView", true));
- Assert.True(list.Items[1].Selected);
- Assert.Equal("2", list.Items[1].Text);
+ var list = (PlateListBox)Assert.Single(env.Editor.Controls.Find("platesListView", true));
+ Assert.Equal(1, list.SelectedIndex);
+ Assert.Same(plate, list.Items[1]);
});
[Fact]
@@ -63,8 +64,8 @@ public class PlateReorderTests
Assert.Same(copy, env.Editor.PlateManager.CurrentPlate);
if (source.CuttingParameters != null)
Assert.NotSame(source.CuttingParameters, copy.CuttingParameters);
- var list = (ListView)Assert.Single(env.Editor.Controls.Find("platesListView", true));
- Assert.True(list.Items[1].Selected);
+ var list = (PlateListBox)Assert.Single(env.Editor.Controls.Find("platesListView", true));
+ Assert.Equal(1, list.SelectedIndex);
Assert.NotNull(list.ContextMenuStrip);
});
@@ -74,7 +75,7 @@ public class PlateReorderTests
env.Editor.PlateManager.LoadAt(0);
var plate = env.Nest.Plates[0];
var count = env.Nest.Plates.Count;
- var list = (ListView)Assert.Single(env.Editor.Controls.Find("platesListView", true));
+ var list = (PlateListBox)Assert.Single(env.Editor.Controls.Find("platesListView", true));
var item = Assert.IsType(list.ContextMenuStrip!.Items["deletePlateMenuItem"]);
Assert.Equal("Delete Plate", item.Text);
diff --git a/OpenNest/Controls/PlateListBox.cs b/OpenNest/Controls/PlateListBox.cs
new file mode 100644
index 00000000..bf4ba8ce
--- /dev/null
+++ b/OpenNest/Controls/PlateListBox.cs
@@ -0,0 +1,400 @@
+using System;
+using System.Collections.Generic;
+using System.Drawing;
+using System.Drawing.Drawing2D;
+using System.Linq;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+using System.Windows.Forms;
+using OpenNest.CNC;
+using OpenNest.Geometry;
+using Size = System.Drawing.Size;
+
+namespace OpenNest.Controls
+{
+ ///
+ /// The Plates tab list: one row per plate with a picture of its sheet and parts, its number,
+ /// size, quantity, part count and utilization, drawn like the drawings list. Pictures are
+ /// cached per plate and redrawn only when the sheet or a part's placement changes.
+ ///
+ public class PlateListBox : ListBox
+ {
+ private const int WM_ERASEBKGND = 0x0014;
+ private const int DragThreshold = 3;
+
+ private readonly Size imageSize;
+ private readonly Font nameFont;
+ private readonly ToolTip toolTip = new ToolTip();
+ private readonly Dictionary previews = new Dictionary();
+ private int toolTipIndex = -1;
+ private int pressedIndex = -1;
+ private Point pressedLocation;
+
+ public PlateListBox()
+ {
+ DrawMode = DrawMode.OwnerDrawFixed;
+ ItemHeight = 85;
+ IntegralHeight = false;
+
+ imageSize = new Size(110, ItemHeight - 10);
+ nameFont = new Font(Font.FontFamily, 10, FontStyle.Bold);
+ }
+
+ /// The scheme the pictures take the sheet colors from.
+ public ColorScheme ColorScheme { get; set; } = ColorScheme.Default;
+
+ /// The tooltip shown over a plate's row; null or empty shows none.
+ public Func ToolTipProvider { get; set; }
+
+ ///
+ /// Raised when the left button is pressed on a row and the pointer then moves past the
+ /// drag threshold; the argument is the row's index.
+ ///
+ public event EventHandler RowDragStarted;
+
+ /// The tooltip text of the row at , "" when it has none.
+ public string GetToolTipText(int index) =>
+ index >= 0 && index < Items.Count && Items[index] is Plate plate
+ ? ToolTipProvider?.Invoke(plate) ?? ""
+ : "";
+
+ ///
+ /// Replaces the rows with , keeping the scroll position, and
+ /// forgets the pictures of plates no longer listed.
+ ///
+ public void SetPlates(IReadOnlyList plates)
+ {
+ var topIndex = TopIndex;
+
+ BeginUpdate();
+ Items.Clear();
+ Items.AddRange(plates.Cast