using System;
namespace Gwen.Control
{
///
/// CollapsibleList control. Groups CollapsibleCategory controls.
///
public class CollapsibleList : ScrollControl
{
///
/// Invoked when an entry has been selected.
///
public event GwenEventHandler ItemSelected;
///
/// Invoked when a category collapsed state has been changed (header button has been pressed).
///
public event GwenEventHandler CategoryCollapsed;
///
/// Initializes a new instance of the class.
///
/// Parent control.
public CollapsibleList(Base parent) : base(parent)
{
EnableScroll(false, true);
AutoHideBars = true;
}
// todo: iterator, make this as function? check if works
///
/// Selected entry.
///
public Button GetSelectedButton()
{
foreach (Base child in Children)
{
CollapsibleCategory cat = child as CollapsibleCategory;
if (cat == null)
continue;
Button button = cat.GetSelectedButton();
if (button != null)
return button;
}
return null;
}
///
/// Adds a category to the list.
///
/// Category control to add.
protected virtual void Add(CollapsibleCategory category)
{
category.Parent = this;
category.Dock = Pos.Top;
category.Margin = new Margin(1, 0, 1, 1);
category.Selected += OnCategorySelected;
category.Collapsed += OnCategoryCollapsed;
// this relies on fact that category.m_List is set to its parent
}
///
/// Adds a new category to the list.
///
/// Name of the category.
/// Newly created control.
public virtual CollapsibleCategory Add(String categoryName)
{
CollapsibleCategory cat = new CollapsibleCategory(this);
cat.Text = categoryName;
Add(cat);
return cat;
}
///
/// Renders the control using specified skin.
///
/// Skin to use.
protected override void Render(Skin.Base skin)
{
skin.DrawCategoryHolder(this);
base.Render(skin);
}
///
/// Unselects all entries.
///
public virtual void UnselectAll()
{
foreach (Base child in Children)
{
CollapsibleCategory cat = child as CollapsibleCategory;
if (cat == null)
continue;
cat.UnselectAll();
}
}
///
/// Handler for ItemSelected event.
///
/// Event source: .
protected virtual void OnCategorySelected(Base control)
{
CollapsibleCategory cat = control as CollapsibleCategory;
if (cat == null) return;
if (ItemSelected != null)
ItemSelected.Invoke(this);
}
///
/// Handler for category collapsed event.
///
/// Event source: .
protected virtual void OnCategoryCollapsed(Base control)
{
CollapsibleCategory cat = control as CollapsibleCategory;
if (cat == null) return;
if (CategoryCollapsed != null)
CategoryCollapsed.Invoke(control);
}
}
}