using System;
using System.Collections.Generic;
using Gtk;
using System.Security.Policy;
///
/// A namespace that contains everything I've pulled from SilentorBit's website.
/// Currently contains: class ListView (https://silentorbit.com/notes/2011/05/gtk-sharp-listview/).
///
namespace Ext.SilentorBit
{
///
/// A single column list, text comes from object.ToString()
/// For multicolumns, see ListView<ClassType>
/// By SilentorBit from https://silentorbit.com/notes/2011/05/gtk-sharp-listview/
///
public class ListView : ListView
{
public ListView (string columnTitle) : base(columnTitle)
{
}
protected override void RenderCell (CellRendererText render, int index, object item)
{
render.Text = item.ToString ();
}
}
///
/// The list only contains one type of object.
/// To get multiple columns, pass that number of parameters to the constructor
/// and implement RenderCell accordingly.
///
public abstract class ListView : TreeView
{
///
/// List of all items selected
///
public T[] SelectedItems { get; private set; }
public event Action ItemSelected;
public event Action ItemActivated;
public bool ResizeableColumns { get; set; } = true;
protected abstract void RenderCell (CellRendererText render, int index, T item);
ListStore store = new ListStore (typeof(T));
Dictionary storeList = new Dictionary ();
List columns = new List ();
///
/// Pass one string parameter for every column
///
///
/// Column Titles, one parameter for each column.
///
public ListView (params string[] columnNames)
{
this.Model = store;
foreach (string s in columnNames) {
TreeViewColumn c = this.AppendColumn (s, new CellRendererText (), this.ColumnCellData);
c.Resizable = ResizeableColumns; // Make the column resizeable - Added by Starbeamrainbowlabs
columns.Add (c);
}
this.SelectedItems = new T[0];
this.Selection.Changed += HandleSelectionChanged;
}
private void ColumnCellData (TreeViewColumn column, CellRenderer renderer, ITreeModel model, TreeIter iter)
{
T item = (T)model.GetValue (iter, 0);
CellRendererText textRender = (CellRendererText)renderer;
int index = columns.IndexOf (column);
RenderCell (textRender, index, item);
}
#region Add, Remove and Clear Items
public void AddItem (T item)
{
var iter = store.AppendValues (item);
storeList.Add (item, iter);
}
public void ClearItems ()
{
store.Clear ();
storeList.Clear ();
}
public void RemoveItem (T item)
{
if (! storeList.ContainsKey (item))
return;
TreeIter iter = storeList[item];
store.Remove (ref iter);
storeList.Remove (item);
}
#endregion
#region Selection and Aktivation triggers
void HandleSelectionChanged (object sender, EventArgs e)
{
TreeSelection selection = (TreeSelection)sender;
TreePath[] paths = selection.GetSelectedRows ();
T[] items = new T[paths.Length];
for (int n = 0; n < paths.Length; n++) {
TreeIter iter;
Model.GetIter (out iter, paths[n]);
items[n] = (T)Model.GetValue (iter, 0);
}
SelectedItems = items;
var itemEvent = ItemSelected;
if (itemEvent != null)
itemEvent (items);
}
protected override void OnRowActivated (TreePath path, TreeViewColumn column)
{
TreeIter iter;
Model.GetIter (out iter, path);
T item = (T)Model.GetValue (iter, 0);
var e = ItemActivated;
if (e != null)
e (item);
base.OnRowActivated (path, column);
}
#endregion
}
}