This repository has been archived on 2023-07-11. You can view files and clone it, but cannot push or open issues or pull requests.
gwen-dotnet/Gwen/Control/CheckBox.cs
Gered 10e057953e initial commit
Contains changes from "gwen-dotnet" removing dependancies on Windows,
which ultimately means certain features (e.g. file load/save dialogs)
do not work. Those classes still exist, but the code has been commented
out.
2013-03-28 18:47:01 -04:00

114 lines
2.8 KiB
C#

using System;
namespace Gwen.Control
{
/// <summary>
/// CheckBox control.
/// </summary>
public class CheckBox : Button
{
private bool m_Checked;
/// <summary>
/// Indicates whether the checkbox is checked.
/// </summary>
public bool IsChecked
{
get { return m_Checked; }
set
{
if (m_Checked == value) return;
m_Checked = value;
OnCheckChanged();
}
}
/// <summary>
/// Initializes a new instance of the <see cref="CheckBox"/> class.
/// </summary>
/// <param name="parent">Parent control.</param>
public CheckBox(Base parent)
: base(parent)
{
SetSize(15, 15);
//m_Checked = true; // [omeg] why?!
//Toggle();
}
/// <summary>
/// Toggles the checkbox.
/// </summary>
public override void Toggle()
{
base.Toggle();
IsChecked = !IsChecked;
}
/// <summary>
/// Invoked when the checkbox has been checked.
/// </summary>
public event GwenEventHandler Checked;
/// <summary>
/// Invoked when the checkbox has been unchecked.
/// </summary>
public event GwenEventHandler UnChecked;
/// <summary>
/// Invoked when the checkbox state has been changed.
/// </summary>
public event GwenEventHandler CheckChanged;
/// <summary>
/// Determines whether unchecking is allowed.
/// </summary>
protected virtual bool AllowUncheck { get { return true; } }
/// <summary>
/// Handler for CheckChanged event.
/// </summary>
protected virtual void OnCheckChanged()
{
if (IsChecked)
{
if (Checked != null)
Checked.Invoke(this);
}
else
{
if (UnChecked != null)
UnChecked.Invoke(this);
}
if (CheckChanged != null)
CheckChanged.Invoke(this);
}
/// <summary>
/// Renders the control using specified skin.
/// </summary>
/// <param name="skin">Skin to use.</param>
protected override void Render(Skin.Base skin)
{
base.Render(skin);
skin.DrawCheckBox(this, m_Checked, IsDepressed);
}
/// <summary>
/// Internal OnPressed implementation.
/// </summary>
protected override void OnClicked()
{
if (IsDisabled)
return;
if (IsChecked && !AllowUncheck)
{
return;
}
Toggle();
}
}
}