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;
}
Tags: gtk, nodestore, nodeview, treenode
Posted in Software | Comments (0)
So in my recent works with Gtk I’ve come across (and written) some useful snippets so I thought I’d post a few of them.
The first comes from this site (its in Spanish), this snippet allows you to take a System.Drawing.Image to a Gdk.Pixbuf and it looks like this (slightly modified to fix an ambiguous reference in my project).
static Gdk.Pixbuf ImageToPixbuf( System.Drawing.Image image )
{
using ( MemoryStream stream = new MemoryStream() )
{
image.Save( stream, System.Drawing.Imaging.ImageFormat.Png );
stream.Position = 0;
return new Gdk.Pixbuf( stream );
}
}
So, we can take this first function and we can use it in this second snippet which takes an list of images and makes a single pixbuf that contains all of the images. This allows us to take a group of icon files and string them into a single image to be used in a gtk nodeview so that it only requires one column to show a variety of status icons, which is what I’m using it for maybe you can find something more interesting for it to do. Any ways the second snippet I share with you today looks like this.
public Gdk.Pixbuf GenerateIconImage(List<System.Drawing.Image> images)
{
if(images != null && images.Count> 0)
{
Gdk.Pixbuf Composite = ImageToPixbuf(images[0]).Clone() as Gdk.Pixbuf;
images.RemoveAt(0);
foreach (System.Drawing.Image i in images)
{
Composite = CreateComposite(Composite,ImageToPixbuf(i).Clone() as Gdk.Pixbuf);
}
return Composite;
}
else
{
Gdk.Pixbuf Composite = new Gdk.Pixbuf(Gdk.Colorspace.Rgb,true,8,16,16);
Composite.Fill(0);
return Composite;
}
}
public Gdk.Pixbuf CreateComposite(Gdk.Pixbuf p1, Gdk.Pixbuf p2)
{
Gdk.Pixbuf composite = new Gdk.Pixbuf(Gdk.Colorspace.Rgb,true,8,p1.Width+ p2.Width,p1.Height);
p1.Composite(composite,0,0,p1.Width,p1.Height,0,0,1,1,Gdk.InterpType.Hyper,255);
p2.Composite(composite,p1.Width,0,p2.Width,p2.Height,p1.Width,0,1,1,Gdk.InterpType.Hyper,255);
return composite;
}
Tags: c#, gtk, image, mono, pixbuf
Posted in Software | Comments (0)