view TreeUtils.cs @ 34:ef1cf7814a6b

* Add ATK# as a dependency, which seems to be necessary as of openSUSE 11.4 (may be Mono 2.10, may be newer GTK#) no-open-ticket
author IBBoard <dev@ibboard.co.uk>
date Sun, 03 Apr 2011 13:11:00 +0000
parents f6126047dd8c
children
line wrap: on
line source

//  This file (TreeUtils.cs) is a part of the IBBoard.GtkSharp project and is copyright 2009 IBBoard
// 
//  The file and the library/program it is in are licensed under the GNU LGPL license, either version 3 of the License or (at your option) any later version. Please see COPYING.LGPL for more information and the full license.
// 

using System;
using Gtk;

namespace IBBoard.GtkSharp
{
	/// <summary>
	/// Utility methods for working with GTK TreeViews and TreeModels
	/// </summary>	
	public class TreeUtils
	{
		public static object GetItemAtPath(TreeView tree, TreePath path)
		{
			TreeModel model = tree.Model;
			TreeIter iter;
			model.GetIter(out iter, path);
			return model.GetValue(iter, 0);
		}

		public static object GetSelectedItem(TreeView tree)
		{
			TreeSelection selection = tree.Selection;
			TreeModel model;
			TreeIter iter;
			object selected = null;
			
			if (selection.CountSelectedRows() > 0)
			{
				selection.GetSelected(out model, out iter);
				selected = model.GetValue(iter, 0);
			}

			return selected;
		}
		
		/// <summary>
		/// Gets the <see cref="TreeIter"/> for an item in a tree, or the Zero TreeIter if the item can't be found
		/// </summary>
		/// <returns>
		/// The TreeIter for the item, or TreeIter.Zero if it can't be found
		/// </returns>
		/// <param name='tree'>
		/// The <see cref="TreeView"/> to search the model of
		/// </param>
		/// <param name='item'>
		/// The item to find in the model
		/// </param>
		public static TreeIter GetItemIter(TreeView tree, object item)
		{
			TreeModel model = tree.Model;
			TreeIter iter;
			model.GetIterFirst(out iter);
			TreeIter itemIter = TreeIter.Zero;
			
			do
			{
				if (model.GetValue(iter, 0).Equals(item))
				{
					itemIter = iter;
					break;
				}
			}
			while (model.IterNext(ref iter));
			
			return itemIter;
		}
	}
}