139 lines
3.7 KiB
C#
139 lines
3.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using Gtk;
|
|
using System.Security.Policy;
|
|
|
|
/// <summary>
|
|
/// <para>A namespace that contains everything I've pulled from SilentorBit's website.</para>
|
|
/// <para>Currently contains: class ListView (https://silentorbit.com/notes/2011/05/gtk-sharp-listview/).</para>
|
|
/// </summary>
|
|
namespace Ext.SilentorBit
|
|
{
|
|
/// <summary>
|
|
/// <para>A single column list, text comes from object.ToString()</para>
|
|
/// <para>For multicolumns, see ListView<ClassType></para>
|
|
/// <para>By SilentorBit from https://silentorbit.com/notes/2011/05/gtk-sharp-listview/</para>
|
|
/// </summary>
|
|
public class ListView : ListView<object>
|
|
{
|
|
public ListView (string columnTitle) : base(columnTitle)
|
|
{
|
|
}
|
|
|
|
protected override void RenderCell (CellRendererText render, int index, object item)
|
|
{
|
|
render.Text = item.ToString ();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// The list only contains one type of object.
|
|
/// To get multiple columns, pass that number of parameters to the constructor
|
|
/// and implement RenderCell accordingly.
|
|
/// </summary>
|
|
public abstract class ListView<T> : TreeView
|
|
{
|
|
/// <summary>
|
|
/// List of all items selected
|
|
/// </summary>
|
|
public T[] SelectedItems { get; private set; }
|
|
public event Action<T[]> ItemSelected;
|
|
public event Action<T> ItemActivated;
|
|
|
|
public bool ResizeableColumns { get; set; } = true;
|
|
|
|
protected abstract void RenderCell (CellRendererText render, int index, T item);
|
|
|
|
ListStore store = new ListStore (typeof(T));
|
|
Dictionary<T, TreeIter> storeList = new Dictionary<T, TreeIter> ();
|
|
List<TreeViewColumn> columns = new List<TreeViewColumn> ();
|
|
|
|
/// <summary>
|
|
/// Pass one string parameter for every column
|
|
/// </summary>
|
|
/// <param name="columnNames">
|
|
/// Column Titles, one parameter for each column.
|
|
/// </param>
|
|
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
|
|
}
|
|
}
|