using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using static Vanara.PInvoke.Shell32;
namespace Vanara.Windows.Shell
{
/// A collection of thumbnail toolbar buttons.
public class ThumbnailToolbarButtonCollection : ObservableCollection
{
private const int maxBtns = 7;
private readonly ThumbnailToolbar Parent;
internal ThumbnailToolbarButtonCollection(ThumbnailToolbar parent)
{
Parent = parent;
CollectionChanged += OnCollectionChanged;
}
/// Adds a sequence of instances to the collection.
/// The items to add.
public void AddRange(IEnumerable items)
{
foreach (var item in items)
Add(item);
}
internal THUMBBUTTON[] ToArray()
{
var ret = new THUMBBUTTON[maxBtns];
for (var i = 0; i < maxBtns; i++)
{
if (i < Count)
ret[i] = this[i].btn;
else
ret[i] = THUMBBUTTON.Default;
ret[i].iId = (uint)i;
}
return ret;
}
/// Inserts the item into the collection.
/// The index at which to insert.
/// The item to insert.
protected override void InsertItem(int index, ThumbnailToolbarButton item)
{
if (Count >= maxBtns)
throw new InvalidOperationException($"A maximum of {maxBtns} buttons may be added to a {nameof(ThumbnailToolbarButtonCollection)}.");
item.indexer.ImageList = Parent.ImageList;
base.InsertItem(index, item);
}
/// Called when the collection has changed.
/// The sender.
/// The instance containing the event data.
protected virtual void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e?.NewItems != null)
foreach (ThumbnailToolbarButton tbi in e.NewItems)
tbi.Parent = Parent;
}
}
}