This snippet of code lets you get a TreePath from a nodeview when you only know the ending node. This seems to be a problem I run into rather often where I want to manipulate the tree by expanding a path but I only have the node I want to expand to. The is a GetPath function in the NodeStore class however it is marked as internal with no easy access, so this provides a work around for that.
In order to use this you will need to change out the Node class used here with whatever TreeNode based class your using in your NodeView.
So nifty code in mono/c#:
public Gtk.TreePath GetPath (Node target)
{
int Count = 0;
foreach(Node n in view.NodeStore)
{
ArrayList path = RecursiveIndexSearch(target,n);
if (path != null)
{
path.Insert(0,Count);
if(path.Count == 1 && target != n)
{
return null;
}
else
{
return new Gtk.TreePath((int[])path.ToArray(typeof(int)));
}
}
Count++;
}
return null;
}
public ArrayList RecursiveIndexSearch(Node target, Node Start)
{
if(Start.ChildCount > 0)
{
for (int i = 0; i < Start.ChildCount; i++)
{
ArrayList Path = RecursiveIndexSearch(target,Start[i] as Node);
if (Path != null)
{
Path.Insert(0,i);
return Path;
}
}
}
if(Start.ChildCount == 0)
{
if (Start == target)
{
return new ArrayList();
}
}
return null;
}