3 * This code renders the Gtk tree into a set of Gtk elements.
4 * principle = one NodeToGtk wraps around the original 'node'
6 * it's called by the view element with
7 * var x = new JsRender.NodeToGtk(file.tree);
8 var obj = x.munge() as Gtk.Widget;
12 * The idea behind the Javascript tools stuff is that we can
13 * transform what is actually being requested to be rendered
14 * -- eg. an AboutBox, and turn that into load of real widgets..
15 * that could be displayed..
21 public class JsRender.NodeToGtk : Object {
24 Object wrapped_object;
27 Gee.ArrayList<NodeToGtk> children;
29 Gee.ArrayList<string> els;
30 //Gee.ArrayList<string> skip;
31 Gee.HashMap<string,string> ar_props;
32 public static int vcnt = 0;
34 public NodeToGtk( Node node)
37 this.els = new Gee.ArrayList<string>();
38 this.children = new Gee.ArrayList<NodeToGtk>();
39 //this.skip = new Gee.ArrayList<string>();
40 this.ar_props = new Gee.HashMap<string,string>();
41 this.parentObj = null;
44 public Object? munge ( )
46 var ret = this.mungeNode();
51 return ret.wrapped_object;
54 public NodeToGtk? mungeChild( Node cnode)
56 var x = new NodeToGtk(cnode);
62 public NodeToGtk? mungeNode()
65 var parent = this.parentObj != null ? this.parentObj.wrapped_object : null;
66 var cls = this.node.fqn().replace(".", "");
67 var ns = this.node.fqn().split(".")[0];
68 var gtkbuilder = new global::Gtk.Builder();
70 var cls_gtype = gtkbuilder.get_type_from_name(cls);
71 print("Type: %s ?= %s\n", this.node.fqn(), cls_gtype.name());
73 if (cls_gtype == GLib.Type.INVALID) {
74 print("SKIP - gtype is invalid\n");
77 // if it's a window...
79 if (cls_gtype.is_a(typeof(global::Gtk.Window))) {
80 // what if it has none...
81 if (this.node.items.size < 1) {
84 return this.mungeChild(this.node.items.get(0));
87 var ret = Object.new(cls_gtype);
88 ret.ref(); //??? problematic?
89 this.wrapped_object = ret;
94 //case "GtkTreeStore": // top level.. - named and referenced
95 case "GtkListStore": // top level.. - named and referenced
96 //case "GtkTreeViewColumn": // part of liststore?!?!
97 //case "GtkMenu": // top level..
98 //case "GtkCellRendererText":
99 case "GtkSourceBuffer":
100 case "GtkClutterActor"://fixme..
101 case "GtkClutterEmbed"://fixme.. -- we can not nest embedded.. need to solve..
112 if (parent != null && parent.get_type().is_a(typeof(global::Gtk.Container))) {
113 this.packContainerParams();
116 var cls_gir =Palete.Gir.factoryFqn(this.node.fqn());
117 if (cls_gir == null) {
120 //var id = this.node.uid();
121 //var ret = @"$pad<object class=\"$cls\" id=\"$id\">\n";
123 var props = cls_gir.props;
126 var pviter = props.map_iterator();
127 while (pviter.next()) {
129 // print("Check: " +cls + "::(" + pviter.get_value().propertyof + ")" + pviter.get_key() + " " );
130 var k = pviter.get_key();
131 // skip items we have already handled..
132 if (!this.node.has(k)) {
135 // find out the type of the property...
136 var type = pviter.get_value().type;
137 type = Palete.Gir.fqtypeLookup(type, ns);
139 var val = this.toValue(this.node.get(k).strip(), type);
141 print("skip (failed to transform value %s type = %s from %s\n",
142 cls + "." + k, type, this.node.get(k).strip());
145 print ("set_property ( %s , %s / %s)\n", k, this.node.get(k).strip(), val.strdup_contents());
148 ret.set_property(k, val);
153 // for now... - just try the builder style packing
157 if (this.node.items.size < 1) {
161 for (var i = 0; i < this.node.items.size; i++ ) {
163 var ch = this.mungeChild(this.node.items.get(i));
165 this.children.add(ch);
170 this.afterChildren();
179 public void afterChildren()
181 // things like GtkNotebook - we have to pack children after they have been created..
182 var cls = this.node.fqn().replace(".", "");
184 if (cls == "GtkNotebook") {
185 this.afterChildrenGtkNotebook();
192 public void afterChildrenGtkNotebook()
194 // we have a number of children..
195 // some are labels - this might need to be more complex...
196 // perhaps labels should be a special property labels[] of the notebook..
197 var labels = new Gee.ArrayList<NodeToGtk>();
198 var bodies = new Gee.ArrayList<NodeToGtk>();
199 for (var i = 0; i < this.children.size; i++) {
200 var cn = this.children.get(i).node.fqn().replace(".", "");
201 if (cn != "GtkLabel") {
202 bodies.add(this.children.get(i));
205 labels.add(this.children.get(i));
207 for (var i = 0; i < bodies.size; i++) {
208 ((global::Gtk.Notebook)this.wrapped_object).append_page(
209 (global::Gtk.Notebook) bodies.get(i).wrapped_object,
210 (labels.size > i - 1) ?
211 (global::Gtk.Notebook) labels.get(i).wrapped_object :
224 * called after the this.object has been created
225 * and it needs to be packed onto parent.
227 public void packParent()
229 var cls = this.node.fqn().replace(".", "");
231 var gtkbuilder = new global::Gtk.Builder();
232 var cls_gtype = gtkbuilder.get_type_from_name(cls);
234 if (this.parentObj == null) {
239 var parent = this.parentObj.wrapped_object;
243 if (parent == null) { // no parent.. can not pack.
246 // ------------- handle various special parents .. -----------
248 var par_type = this.parentObj.node.fqn().replace(".", "");
250 if (par_type == "GtkNotebook") {
251 // do not pack - it's done afterwards...
255 // ------------- handle various child types.. -----------
257 if (cls == "GtkMenu") {
262 if (cls == "GtkTreeStore") { // other stores?
263 // tree store is buildable??? ---
264 this.packTreeStore();
267 if (cls =="GtkTreeViewColumn") { // other stores?
268 //?? treeview column is actually buildable -- but we do not use the builder???
269 this.packTreeViewColumn();
272 if (cls_gtype.is_a(typeof(global::Gtk.CellRenderer))) { // other stores?
273 this.packCellRenderer();
279 // -- handle buildable add_child..
280 if ( cls_gtype.is_a(typeof(global::Gtk.Buildable))
282 parent.get_type().is_a(typeof(global::Gtk.Buildable))
285 ((global::Gtk.Buildable)parent).add_child(gtkbuilder,
286 this.wrapped_object, null);
295 public void packMenu()
299 var parent = this.parentObj.wrapped_object;
300 if (!parent.get_type().is_a(typeof(global::Gtk.Widget))) {
301 print("skip menu pack - parent is not a widget");
305 var p = (global::Gtk.Menu)this.wrapped_object;
306 ((global::Gtk.Widget)parent).button_press_event.connect((s, ev) => {
307 p.set_screen(Gdk.Screen.get_default());
309 p.popup(null, null, null, ev.button, ev.time);
314 public void packTreeStore()
316 var parent = this.parentObj.wrapped_object;
317 if (!parent.get_type().is_a(typeof(global::Gtk.TreeView))) {
318 print("skip treestore pack - parent is not a treeview");
321 ((global::Gtk.TreeView)parent).set_model((global::Gtk.TreeModel)this.wrapped_object);
324 public void packTreeViewColumn()
326 var parent = this.parentObj.wrapped_object;
327 if (!parent.get_type().is_a(typeof(global::Gtk.TreeView))) {
328 print("skip packGtkViewColumn pack - parent is not a treeview");
331 ((global::Gtk.TreeView)parent).append_column((global::Gtk.TreeViewColumn)this.wrapped_object);
332 // init contains the add_attribute for what to render...
337 public void packCellRenderer()
339 var parent = this.parentObj.wrapped_object;
340 if (!parent.get_type().is_a(typeof(global::Gtk.TreeViewColumn))) {
341 print("skip packGtkViewColumn pack - parent is not a treeview");
344 ((global::Gtk.TreeViewColumn)parent).pack_start((global::Gtk.CellRenderer)this.wrapped_object, false);
345 // init contains the add_attribute for what to render...
350 public void packContainerParams()
353 if (this.parentObj == null) {
356 // child must be a widget..
357 if (!this.wrapped_object.get_type().is_a(typeof(global::Gtk.Widget))) {
361 var parent_gir = Palete.Gir.factoryFqn(this.parentObj.node.fqn());
363 var parent = this.parentObj.wrapped_object;
365 if (parent_gir == null) {
369 // let's test just setting expand to false...
370 var cls_methods = parent_gir.methods;
371 if (cls_methods == null) {
375 if (!this.node.props.has_key("* pack") ||
376 this.node.props.get("* pack").length < 1) {
380 var ns = this.parentObj.node.fqn().split(".")[0];
382 var pack = this.node.props.get("* pack").split(",");
385 if (cls_methods.has_key(pack[0])) {
386 var mparams = cls_methods.get(pack[0]).paramset.params;
387 for (var i = 1; i < mparams.size; i++ ) {
388 if (i > (pack.length -1)) {
392 var k = mparams.get(i).name;
396 var type = mparams.get(i).type;
397 type = Palete.Gir.fqtypeLookup(type, ns);
399 var val = this.toValue(pack[i].strip(), type);
401 print("skip (failed to transform value %s type = %s from %s\n",
402 this.parentObj.node.fqn() + "." + k, type, pack[i].strip());
405 print ("pack:set_property ( %s , %s / %s)\n", k, pack[i].strip(), val.strdup_contents());
407 ((global::Gtk.Container)parent).child_set_property(
408 (global::Gtk.Widget)this.wrapped_object , k, val);
420 public GLib.Value? toValue(string val, string type) {
422 var gtkbuilder = new global::Gtk.Builder();
424 if (type == "utf8") {
425 var qret = GLib.Value(typeof(string));
426 qret.set_string(val);
430 var prop_gtype = gtkbuilder.get_type_from_name(type);
433 if (prop_gtype == GLib.Type.INVALID) {
439 var ret = GLib.Value(prop_gtype);
444 ret.set_boolean(val.down() == "false" ? false : true);
447 ret.set_uint(int.parse(val));
451 ret.set_int(int.parse(val));
455 ret.set_float(long.parse(val));
464 var sval = GLib.Value(typeof(string));
465 sval.set_string(val);
467 if (!sval.transform(ref ret)) {