Skip to content

Instantly share code, notes, and snippets.

@killerswan
Created September 22, 2011 23:01
Show Gist options
  • Save killerswan/1236280 to your computer and use it in GitHub Desktop.
Save killerswan/1236280 to your computer and use it in GitHub Desktop.
A messy example showing use of the GTK# TreeView
using System;
using System.IO;
using Gtk;
namespace DemoII
{
public class FruitTreePiece
{
public string name;
public string description;
public int quantity;
public bool isKindNotType;
public FruitTreePiece ( string name, string description, int quantity, bool isKindNotType )
{
this.name = name;
this.description = description;
this.quantity = quantity;
this.isKindNotType = isKindNotType;
}
public static void iwanttobealambda (TreeViewColumn column, CellRenderer cell, TreeModel model, TreeIter iter)
{
FruitTreePiece piece = (FruitTreePiece) model.GetValue (iter, 0);
(cell as Gtk.CellRendererText).Text = piece.name;
}
public static void makeMyTree (Gtk.TreeView kevinTree)
{
//kevinTree.AppendColumFn ("A column with strings...", new Gtk.CellRendererText (), "text", 0);
Gtk.TreeViewColumn col = new Gtk.TreeViewColumn ();
Gtk.CellRendererText r = new Gtk.CellRendererText ();
col.PackStart (r, true);
col.SetCellDataFunc (r, new Gtk.TreeCellDataFunc (iwanttobealambda));
kevinTree.AppendColumn (col);
// model
Gtk.TreeStore kevinModel;
Gtk.TreeIter fruit, redFruit, cherry;
kevinModel = new Gtk.TreeStore ( typeof (FruitTreePiece) );
fruit = kevinModel.AppendValues (new FruitTreePiece ("fruit", "not vegetables", 0, true));
redFruit = kevinModel.AppendValues (fruit, new FruitTreePiece ("apple", "Tasty red tree growing things.", 15, true));
cherry = kevinModel.AppendValues (redFruit, new FruitTreePiece ("cherry", "Sweet and maybe sour!", 12, true));
kevinModel.AppendValues (cherry, new FruitTreePiece ("bing cherry", "", 1, false));
kevinModel.AppendValues (redFruit, new FruitTreePiece ("strawberry", "", 1, false));
kevinModel.AppendValues (fruit, new FruitTreePiece ("pear", "", 1, false));
// model -> view
kevinTree.Model = kevinModel;
return;
}
}
public class FileTreeView : Gtk.TreeView {
public Gtk.TreeView target; // tree view we're going to asign a new model to
public FileTreeView (Gtk.TreeView target)
{
this.target = target;
}
protected override bool OnButtonPressEvent (Gdk.EventButton e)
{
Gtk.TreeIter iter;
Gtk.TreePath path;
System.Console.WriteLine ("There's been a button press!");
// given this click event, get its window path
path = new Gtk.TreePath ();
GetPathAtPos (System.Convert.ToInt16 (e.X), System.Convert.ToInt16 (e.Y), out path);
// given the path, get some data out of the model for this item
if (this.Model.GetIter (out iter, path)) {
FruitTreePiece piece = (FruitTreePiece) this.Model.GetValue (iter, 0);
string name = piece.name;
System.Console.WriteLine ("Selected row during click: " + name);
FruitTreePiece.makeMyTree(this.target);
}
return base.OnButtonPressEvent (e);
}
}
class Demo
{
public static void Main (string[] args)
{
Application.Init ();
Gtk.TreeView tree1, tree2;
tree2 = new Gtk.TreeView ();
tree1 = new FileTreeView (tree2);
FruitTreePiece.makeMyTree (tree1);
new TwoTreeWindow ("Reference", "", tree1, "Test", tree2);
Application.Run ();
}
}
class TwoTreeWindow
{
public TwoTreeWindow (string titleText, string labelText1, Gtk.TreeView treeview1, string labelText2, Gtk.TreeView treeview2)
{
// overall
Gtk.Window win;
Gtk.VBox box;
Gtk.HPaned panes;
win = new Gtk.Window (titleText);
box = new Gtk.VBox ();
box.Spacing = 6;
panes = new Gtk.HPaned ();
panes.Position = 400;
// frame1
Gtk.Frame fr1;
Gtk.Label label1;
Gtk.Alignment align1;
Gtk.ScrolledWindow sw1;
fr1 = new Gtk.Frame ();
fr1.ShadowType = (Gtk.ShadowType) 0;
label1 = new Gtk.Label ();
label1.UseMarkup = true;
label1.LabelProp = "<b>" + labelText1 + "</b>";
align1 = new Gtk.Alignment (0F, 0F, 1F, 1F);
sw1 = new Gtk.ScrolledWindow ();
sw1.ShadowType = (Gtk.ShadowType) 1;
treeview1.CanFocus = true;
// frame2
Gtk.Frame fr2;
Gtk.Label label2;
Gtk.Alignment align2;
Gtk.ScrolledWindow sw2;
fr2 = new Gtk.Frame ();
fr2.ShadowType = (Gtk.ShadowType) 0;
label2 = new Gtk.Label ();
label2.UseMarkup = true;
label2.LabelProp = "<b>" + labelText2 + "</b>";
align2 = new Gtk.Alignment (0F, 0F, 1F, 1F);
sw2 = new Gtk.ScrolledWindow ();
sw2.ShadowType = (Gtk.ShadowType) 1;
treeview2.CanFocus = true;
// frame 1 connections
sw1.Add (treeview1);
align1.Add (sw1);
fr1.Add (align1);
fr1.LabelWidget = label1;
panes.Add (fr1);
// frame 2 connections
sw2.Add (treeview2);
align2.Add (sw2);
fr2.Add (align2);
fr2.LabelWidget = label2;
panes.Add (fr2);
// status bar
Gtk.Statusbar sbar;
sbar = new Gtk.Statusbar ();
sbar.Spacing = 6;
// ???
Gtk.HPaned.PanedChild pc1;
pc1 = (Gtk.Paned.PanedChild) panes [fr1];
pc1.Resize = false;
// vbox connections
Gtk.Box.BoxChild boxchild1;
Gtk.Box.BoxChild boxchild2;
box.Add (panes);
box.Add (sbar);
boxchild1 = (Gtk.Box.BoxChild) box [panes];
boxchild1.Position = 0;
boxchild2 = (Gtk.Box.BoxChild) box [sbar];
boxchild2.Position = 1;
boxchild2.Expand = false;
boxchild2.Fill = false;
win.Add (box);
win.DefaultWidth = 800;
win.DefaultHeight = 600;
// display
if (win.Child != null) {
win.Child.ShowAll ();
}
win.Show ();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment