using System;
using System.Drawing;
using System.Linq;
using Gwen.ControlInternal;
namespace Gwen.Control
{
///
/// Movable window with title bar.
///
public class WindowControl : ResizableControl
{
private readonly Dragger m_TitleBar;
private readonly Label m_Caption;
private readonly CloseButton m_CloseButton;
private bool m_DeleteOnClose;
private Modal m_Modal;
///
/// Window caption.
///
public String Caption { get { return m_Caption.Text; } set { m_Caption.Text = value; } }
///
/// Determines whether the window has close button.
///
public bool IsClosable { get { return !m_CloseButton.IsHidden; } set { m_CloseButton.IsHidden = !value; } }
///
/// Determines whether the control should be disposed on close.
///
public bool DeleteOnClose { get { return m_DeleteOnClose; } set { m_DeleteOnClose = value; } }
///
/// Indicates whether the control is hidden.
///
public override bool IsHidden
{
get { return base.IsHidden; }
set
{
if (!value)
BringToFront();
base.IsHidden = value;
}
}
///
/// Initializes a new instance of the class.
///
/// Parent control.
/// Window caption.
/// Determines whether the window should be modal.
public WindowControl(Base parent, String caption = "", bool modal = false)
: base(parent)
{
m_TitleBar = new Dragger(this);
m_TitleBar.Height = 24;
m_TitleBar.Padding = Gwen.Padding.Zero;
m_TitleBar.Margin = new Margin(0, 0, 0, 4);
m_TitleBar.Target = this;
m_TitleBar.Dock = Pos.Top;
m_Caption = new Label(m_TitleBar);
m_Caption.Alignment = Pos.Left | Pos.CenterV;
m_Caption.Text = caption;
m_Caption.Dock = Pos.Fill;
m_Caption.Padding = new Padding(8, 0, 0, 0);
m_Caption.TextColor = Skin.Colors.Window.TitleInactive;
m_CloseButton = new CloseButton(m_TitleBar, this);
//m_CloseButton.Text = String.Empty;
m_CloseButton.SetSize(24, 24);
m_CloseButton.Dock = Pos.Right;
m_CloseButton.Clicked += CloseButtonPressed;
m_CloseButton.IsTabable = false;
m_CloseButton.Name = "closeButton";
//Create a blank content control, dock it to the top - Should this be a ScrollControl?
m_InnerPanel = new Base(this);
m_InnerPanel.Dock = Pos.Fill;
GetResizer(8).Hide();
BringToFront();
IsTabable = false;
Focus();
MinimumSize = new Point(100, 40);
ClampMovement = true;
KeyboardInputEnabled = false;
if (modal)
MakeModal();
}
protected virtual void CloseButtonPressed(Base control)
{
IsHidden = true;
if (m_Modal != null)
{
m_Modal.DelayedDelete();
m_Modal = null;
}
if (m_DeleteOnClose)
{
Parent.RemoveChild(this, true);
}
}
///
/// Makes the window modal: covers the whole canvas and gets all input.
///
/// Determines whether all the background should be dimmed.
public void MakeModal(bool dim = false)
{
if (m_Modal != null)
return;
m_Modal = new Modal(GetCanvas());
Parent = m_Modal;
if (dim)
m_Modal.ShouldDrawBackground = true;
else
m_Modal.ShouldDrawBackground = false;
}
///
/// Indicates whether the control is on top of its parent's children.
///
public override bool IsOnTop
{
get { return Parent.Children.Where(x => x is WindowControl).Last() == this; }
}
///
/// Renders the control using specified skin.
///
/// Skin to use.
protected override void Render(Skin.Base skin)
{
bool hasFocus = IsOnTop;
if (hasFocus)
m_Caption.TextColor = Skin.Colors.Window.TitleActive;
else
m_Caption.TextColor = Skin.Colors.Window.TitleInactive;
skin.DrawWindow(this, m_TitleBar.Bottom, hasFocus);
}
///
/// Renders under the actual control (shadows etc).
///
/// Skin to use.
protected override void RenderUnder(Skin.Base skin)
{
base.RenderUnder(skin);
skin.DrawShadow(this);
}
public override void Touch()
{
base.Touch();
BringToFront();
}
///
/// Renders the focus overlay.
///
/// Skin to use.
protected override void RenderFocus(Skin.Base skin)
{
}
}
}