Published 2007-06-13 08:21:04
var store = new Gtk.ListStore(At present I've used the Gtype names to define what is being stored where. - This may need simplifying, and integrating somehow with the writing mechanism as validating data types written to the tree is a bit haphazard at present.
"gchararray",
"gfloat",
"gpointer",
"gboolean",
"GdkPixbuf");
var iter = new Gtk.TreeIter();The append method loads the Row reader writer, so that it can access the new row.
store.append(iter);
store.set(iter, 0, new G.Value("test"));all this could easily be abstracted by defining a simple prototype
store.set(iter, 1, new G.Value(1.0));
store.set(iter, 2, new G.Value({ a: 1, c: 2}));
store.set(iter, 3, new G.Value(true) );
var pixbuf = Gdk.Pixbuf.newFromFile("gnome-logo-icon.png");
store.set(iter, 4, new G.Value(pixbuf) );
Gtk.ListStore.prototype.appendRow = function(rdata) {Next step is to build the Widget. And the renderers (the classes that turn the data into visable data). Gtk comes with a few renderers, It will be interesting to see if creating them in Javascript is feasible.
var iter = new Gtk.TreeIter();
this.append(iter);
for(var i =0;i<rdata.length;i++) {
this.set(iter,i, new G.Value(rdata[i]));
}
}
.....
store.appendRow(["test", 1.0, {a: 1}, true, Gtk.Pixbuf.newFromFile(....)]);
var view = new Gtk.TreeView();Adding columns is again a little raw, as we cant use the varargs methods the C does, so we have to combine a few calls.
var renderer = new Gtk.CellRendererText ();
var pixrenderer = new Gtk.CellRendererPixbuf ();
Again a nice wrapper could be written.
var col = new Gtk.TreeViewColumn();
col.setTitle("Name");
col.packStart(renderer, true);
col.addAttribute(renderer, "text", 0);
view.insertColumn( col, -1);
Gtk.TreeView.prototype.insertColumnAuto = function(config) {
var col = new Gtk.TreeViewColumn();
col.setTitle(config.title);
col.packStart(config.renderer, true);
col.addAttribute(config.renderer,
config.renderer.isPrototypeOf(GtkCellRenderText) ?
"text" : "pixbuf", 0);
this.insertColumn( col, -1);
}
...
view.insertColumnAuto({
title: "text here",
renderer: new GtkCellRendererText(),
}
view.setModel ( store );
window.add(view); // add it to a GtkWindow..
window.showAll();
Gtk.main();
view.connect("row-activated", function(view, path, col) {At present this is a little klunky, but again with some prototype modifications it should be pretty easy to solve.
var model = view.getModel();
var iter = new Gtk.TreeIter();
if (model.getIter(iter, path)) {
var name = new G.Value();
var name2 = new G.Value();
model.getValue(iter, 0, name);
model.getValue(iter, 1, name2);
println (
"The row containing the name '"+
name.getString() +
"' has been double-clicked. holding" +
name2.getFloat()
);
model = new Gtk.ListStore(model);
model.remove(iter);
println ("removed?");
}
});