using System;
using Gwen.Input;
namespace Gwen.Control
{
///
/// RadioButton with label.
///
public class LabeledRadioButton : Base
{
private readonly RadioButton m_RadioButton;
private readonly LabelClickable m_Label;
///
/// Label text.
///
public String Text { get { return m_Label.Text; } set { m_Label.Text = value; } }
///
/// Initializes a new instance of the class.
///
/// Parent control.
public LabeledRadioButton(Base parent)
: base(parent)
{
SetSize(100, 20);
m_RadioButton = new RadioButton(this);
//m_RadioButton.Dock = Pos.Left; // no docking, it causes resizing
//m_RadioButton.Margin = new Margin(0, 2, 2, 2);
m_RadioButton.IsTabable = false;
m_RadioButton.KeyboardInputEnabled = false;
m_Label = new LabelClickable(this);
m_Label.Alignment = Pos.Bottom | Pos.Left;
m_Label.Text = "Radio Button";
//m_Label.Dock = Pos.Fill;
m_Label.Clicked += m_RadioButton.Press;
m_Label.IsTabable = false;
m_Label.KeyboardInputEnabled = false;
m_Label.AutoSizeToContents = true;
}
protected override void Layout(Skin.Base skin)
{
// ugly stuff because we don't have anchoring without docking (docking resizes children)
if (m_Label.Height > m_RadioButton.Height) // usually radio is smaller than label so it gets repositioned to avoid clipping with negative Y
{
m_RadioButton.Y = (m_Label.Height - m_RadioButton.Height)/2;
}
Align.PlaceRightBottom(m_Label, m_RadioButton);
SizeToChildren();
base.Layout(skin);
}
///
/// Renders the focus overlay.
///
/// Skin to use.
protected override void RenderFocus(Skin.Base skin)
{
if (InputHandler.KeyboardFocus != this) return;
if (!IsTabable) return;
skin.DrawKeyboardHighlight(this, RenderBounds, 0);
}
// todo: would be nice to remove that
internal RadioButton RadioButton { get { return m_RadioButton; } }
///
/// Handler for Space keyboard event.
///
/// Indicates whether the key was pressed or released.
///
/// True if handled.
///
protected override bool OnKeySpace(bool down)
{
if (down)
m_RadioButton.IsChecked = !m_RadioButton.IsChecked;
return true;
}
///
/// Selects the radio button.
///
public virtual void Select()
{
m_RadioButton.IsChecked = true;
}
}
}