146 lines
3.8 KiB
C#
146 lines
3.8 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using Dashboard.Drawing;
|
|
using Dashboard.Events;
|
|
using Dashboard.Layout;
|
|
using Dashboard.Pal;
|
|
|
|
namespace Dashboard.Controls
|
|
{
|
|
public class Container : Control, IList<Control>, ILayoutContainer
|
|
{
|
|
private readonly List<Control> _controls = new List<Control>();
|
|
|
|
public int Count => _controls.Count;
|
|
|
|
public bool IsReadOnly => false;
|
|
|
|
public ContainerLayoutInfo ContainerLayout { get; } = new ContainerLayoutInfo();
|
|
|
|
public event EventHandler<ContainerChildAddedEventArgs>? ChildAdded;
|
|
public event EventHandler<ContainerChildRemovedEventArgs>? ChildRemoved;
|
|
|
|
|
|
public Control this[int index]
|
|
{
|
|
get => _controls[index];
|
|
set => _controls[index] = value;
|
|
}
|
|
|
|
protected override void ValidateLayout()
|
|
{
|
|
if (!IsLayoutEnabled || IsLayoutValid)
|
|
return;
|
|
|
|
// LayoutSolution solution = LayoutSolution.CalculateLayout(this, ClientArea.Size);
|
|
|
|
base.ValidateLayout();
|
|
}
|
|
|
|
public override void OnPaint(DeviceContext dc)
|
|
{
|
|
base.OnPaint(dc);
|
|
|
|
var dcb = dc.ExtensionRequire<IDeviceContextBase>();
|
|
dcb.PushClip(ClientArea);
|
|
ValidateLayout();
|
|
|
|
foreach (Control child in _controls)
|
|
{
|
|
if (child.Layout.DisplayMode == DisplayMode.None)
|
|
continue;
|
|
|
|
child.SendEvent(this, new PaintEventArgs(dc));
|
|
}
|
|
|
|
dcb.PopClip();
|
|
}
|
|
|
|
IEnumerator<ILayoutItem> IEnumerable<ILayoutItem>.GetEnumerator()
|
|
{
|
|
return GetEnumerator();
|
|
}
|
|
|
|
public IEnumerator<Control> GetEnumerator()
|
|
{
|
|
return _controls.GetEnumerator();
|
|
}
|
|
|
|
IEnumerator IEnumerable.GetEnumerator()
|
|
{
|
|
return ((IEnumerable)_controls).GetEnumerator();
|
|
}
|
|
|
|
public void Add(Control item)
|
|
{
|
|
SetParent(this, item);
|
|
|
|
_controls.Add(item);
|
|
ChildAdded?.Invoke(this, new ContainerChildAddedEventArgs(this, item));
|
|
}
|
|
|
|
public void Clear()
|
|
{
|
|
foreach (Control control in this)
|
|
{
|
|
ChildRemoved?.Invoke(this, new ContainerChildRemovedEventArgs(this, control));
|
|
}
|
|
_controls.Clear();
|
|
}
|
|
|
|
public bool Contains(Control item)
|
|
{
|
|
return _controls.Contains(item);
|
|
}
|
|
|
|
public void CopyTo(Control[] array, int arrayIndex)
|
|
{
|
|
_controls.CopyTo(array, arrayIndex);
|
|
}
|
|
|
|
public bool Remove(Control item)
|
|
{
|
|
if (!_controls.Remove(item))
|
|
return false;
|
|
|
|
ChildRemoved?.Invoke(this, new ContainerChildRemovedEventArgs(this, item));
|
|
return true;
|
|
}
|
|
|
|
public int IndexOf(Control item)
|
|
{
|
|
return _controls.IndexOf(item);
|
|
}
|
|
|
|
public void Insert(int index, Control item)
|
|
{
|
|
SetParent(this, item);
|
|
|
|
_controls.Insert(index, item);
|
|
ChildAdded?.Invoke(this, new ContainerChildAddedEventArgs(this, item));
|
|
}
|
|
|
|
public void RemoveAt(int index)
|
|
{
|
|
Control child = _controls[index];
|
|
_controls.RemoveAt(index);
|
|
ChildRemoved?.Invoke(this, new ContainerChildRemovedEventArgs(this, child));
|
|
}
|
|
|
|
|
|
}
|
|
|
|
public class ContainerChildAddedEventArgs(Container parent, Control child) : EventArgs
|
|
{
|
|
public Container Parent { get; } = parent;
|
|
public Control Child { get; } = child;
|
|
}
|
|
|
|
public class ContainerChildRemovedEventArgs(Container parent, Control child) : EventArgs
|
|
{
|
|
public Container Parent { get; } = parent;
|
|
public Control Child { get; } = child;
|
|
}
|
|
}
|