Files
Dashboard/Dashboard/Controls/Form.cs
T

100 lines
2.4 KiB
C#

using System;
using System.Drawing;
using Dashboard.Drawing;
using Dashboard.Events;
using Dashboard.Pal;
using Dashboard.Windowing;
namespace Dashboard.Controls
{
public class Form : Container, IForm
{
private string? _title = "Untitled Form";
public IWindow Window { get; }
public Image? WindowIcon { get; set; }
public string? Title
{
get => _title;
set
{
_title = value;
Window.Title = _title ?? "";
}
}
public Brush Background { get; set; } = new SolidColorBrush(Color.SlateGray);
public Control? FocusedControl { get; private set; } = null;
public override Box2d ClientArea
{
get => new Box2d(0, 0, Window.ClientSize.Width, Window.ClientSize.Height);
set { }
}
public event EventHandler<WindowCloseEvent>? Closing;
public Form(IWindow window)
{
Window = window;
window.Form = this;
Window.Title = _title;
}
public void Focus(Control control)
{
if (FocusedControl != null)
InvokeFocusLost(this, FocusedControl);
FocusedControl = control;
InvokeFocusGained(this, control);
}
public override void OnPaint(DeviceContext dc)
{
dc.Begin();
var dcb = dc.ExtensionRequire<IDeviceContextBase>();
dcb.ResetClip();
dcb.ResetScissor();
dcb.ResetTransforms();
dcb.ClearDepth();
if (Background is SolidColorBrush solidColorBrush)
dcb.ClearColor(solidColorBrush.Color);
foreach (Control child in this)
child.SendEvent(this, new PaintEventArgs(dc));
base.OnPaint(dc);
dc.End();
}
protected virtual void OnClosing(WindowCloseEvent ea)
{
Closing?.Invoke(this, ea);
if (ea.Cancel)
return;
Dispose();
Window.Dispose();
}
protected override void OnEventRaised(object? sender, EventArgs args)
{
base.OnEventRaised(sender, args);
switch (args)
{
case WindowCloseEvent close:
OnClosing(close);
break;
}
}
}
}