using System;
using System.Drawing;
namespace Gwen.Control.Layout
{
///
/// Single table row.
///
public class TableRow : Base
{
// [omeg] todo: get rid of this
public const int MaxColumns = 5;
private int m_ColumnCount;
private bool m_EvenRow;
private readonly Label[] m_Columns;
internal Label GetColumn(int index)
{
return m_Columns[index];
}
///
/// Invoked when the row has been selected.
///
public event GwenEventHandler Selected;
///
/// Column count.
///
public int ColumnCount { get { return m_ColumnCount; } set { SetColumnCount(value); } }
///
/// Indicates whether the row is even or odd (used for alternate coloring).
///
public bool EvenRow { get { return m_EvenRow; } set { m_EvenRow = value; } }
///
/// Text of the first column.
///
public String Text { get { return GetText(0); } set { SetCellText(0, value); } }
///
/// Initializes a new instance of the class.
///
/// Parent control.
public TableRow(Base parent)
: base(parent)
{
m_Columns = new Label[MaxColumns];
m_ColumnCount = 0;
KeyboardInputEnabled = true;
}
///
/// Sets the number of columns.
///
/// Number of columns.
protected void SetColumnCount(int columnCount)
{
if (columnCount == m_ColumnCount) return;
if (columnCount >= MaxColumns)
throw new ArgumentException("Invalid column count", "columnCount");
for (int i = 0; i < MaxColumns; i++)
{
if (i < columnCount)
{
if (null == m_Columns[i])
{
m_Columns[i] = new Label(this);
m_Columns[i].Padding = Padding.Three;
m_Columns[i].Margin = new Margin(0, 0, 2, 0); // to separate them slightly
if (i == columnCount - 1)
{
// last column fills remaining space
m_Columns[i].Dock = Pos.Fill;
}
else
{
m_Columns[i].Dock = Pos.Left;
}
}
}
else if (null != m_Columns[i])
{
RemoveChild(m_Columns[i], true);
m_Columns[i] = null;
}
m_ColumnCount = columnCount;
}
}
///
/// Sets the column width (in pixels).
///
/// Column index.
/// Column width.
public void SetColumnWidth(int column, int width)
{
if (null == m_Columns[column])
return;
if (m_Columns[column].Width == width)
return;
m_Columns[column].Width = width;
}
///
/// Sets the text of a specified cell.
///
/// Column number.
/// Text to set.
public void SetCellText(int column, String text)
{
if (null == m_Columns[column])
return;
m_Columns[column].Text = text;
}
///
/// Sets the contents of a specified cell.
///
/// Column number.
/// Cell contents.
/// Determines whether mouse input should be enabled for the cell.
public void SetCellContents(int column, Base control, bool enableMouseInput = false)
{
if (null == m_Columns[column])
return;
control.Parent = m_Columns[column];
m_Columns[column].MouseInputEnabled = enableMouseInput;
}
///
/// Gets the contents of a specified cell.
///
/// Column number.
/// Control embedded in the cell.
public Base GetCellContents(int column)
{
return m_Columns[column];
}
protected virtual void OnRowSelected()
{
if (Selected != null)
Selected.Invoke(this);
}
///
/// Sizes all cells to fit contents.
///
public void SizeToContents()
{
int width = 0;
int height = 0;
for (int i = 0; i < m_ColumnCount; i++)
{
if (null == m_Columns[i])
continue;
// Note, more than 1 child here, because the
// label has a child built in ( The Text )
if (m_Columns[i].Children.Count > 1)
{
m_Columns[i].SizeToChildren();
}
else
{
m_Columns[i].SizeToContents();
}
//if (i == m_ColumnCount - 1) // last column
// m_Columns[i].Width = Parent.Width - width; // fill if not autosized
width += m_Columns[i].Width + m_Columns[i].Margin.Left + m_Columns[i].Margin.Right;
height = Math.Max(height, m_Columns[i].Height + m_Columns[i].Margin.Top + m_Columns[i].Margin.Bottom);
}
SetSize(width, height);
}
///
/// Sets the text color for all cells.
///
/// Text color.
public void SetTextColor(Color color)
{
for (int i = 0; i < m_ColumnCount; i++)
{
if (null == m_Columns[i]) continue;
m_Columns[i].TextColor = color;
}
}
///
/// Returns text of a specified row cell (default first).
///
/// Column index.
/// Column cell text.
public String GetText(int column = 0)
{
return m_Columns[column].Text;
}
///
/// Handler for Copy event.
///
/// Source control.
protected override void OnCopy(Base from)
{
Platform.Neutral.SetClipboardText(Text);
}
}
}