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..
17 * we could go on the theory that we send the whole tree to the 'plugin'
18 * and that would do all the transformations before rendering..
19 * -- this would make more sense...
20 * -- otherwise we would call it on each element, and might get really confused
26 public class JsRender.NodeToGtk : Object {
29 Object wrapped_object;
32 Gee.ArrayList<NodeToGtk> children;
34 Gee.ArrayList<string> els;
35 //Gee.ArrayList<string> skip;
36 Gee.HashMap<string,string> ar_props;
37 public static int vcnt = 0;
39 public NodeToGtk( Node node , NodeToGtk? parent_obj = null)
42 this.els = new Gee.ArrayList<string>();
43 this.children = new Gee.ArrayList<NodeToGtk>();
44 //this.skip = new Gee.ArrayList<string>();
45 this.ar_props = new Gee.HashMap<string,string>();
46 this.parentObj = parent_obj;
48 if (parent_obj == null) {
49 // then serialize up the node,
50 // send it to javascript for processsing,
51 // then set node to the return value..
56 public Object? munge ( )
58 var ret = this.mungeNode();
63 return ret.wrapped_object;
66 public NodeToGtk? mungeChild( Node cnode)
68 var x = new NodeToGtk(cnode, this);
74 public NodeToGtk? mungeNode()
77 var parent = this.parentObj != null ? this.parentObj.wrapped_object : null;
78 var cls = this.node.fqn().replace(".", "");
79 var ns = this.node.fqn().split(".")[0];
80 var gtkbuilder = new global::Gtk.Builder();
82 var cls_gtype = gtkbuilder.get_type_from_name(cls);
83 print("Type: %s ?= %s\n", this.node.fqn(), cls_gtype.name());
85 if (cls_gtype == GLib.Type.INVALID) {
86 print("SKIP - gtype is invalid\n");
89 // if it's a window...
91 if (cls_gtype.is_a(typeof(global::Gtk.Window))) {
92 // what if it has none...
93 if (this.node.items.size < 1) {
96 return this.mungeChild(this.node.items.get(0));
99 var ret = Object.new(cls_gtype);
100 ret.ref(); //??? problematic?
101 this.wrapped_object = ret;
106 //case "GtkTreeStore": // top level.. - named and referenced
107 case "GtkListStore": // top level.. - named and referenced
108 //case "GtkTreeViewColumn": // part of liststore?!?!
109 //case "GtkMenu": // top level..
110 //case "GtkCellRendererText":
111 case "GtkSourceBuffer":
112 case "GtkClutterActor"://fixme..
113 case "GtkClutterEmbed"://fixme.. -- we can not nest embedded.. need to solve..
124 if (parent != null && parent.get_type().is_a(typeof(global::Gtk.Container))) {
125 this.packContainerParams();
128 var cls_gir =Palete.Gir.factoryFqn(this.node.fqn());
129 if (cls_gir == null) {
132 //var id = this.node.uid();
133 //var ret = @"$pad<object class=\"$cls\" id=\"$id\">\n";
135 var props = cls_gir.props;
138 var pviter = props.map_iterator();
139 while (pviter.next()) {
141 // print("Check: " +cls + "::(" + pviter.get_value().propertyof + ")" + pviter.get_key() + " " );
142 var k = pviter.get_key();
143 // skip items we have already handled..
144 if (!this.node.has(k)) {
147 // find out the type of the property...
148 var type = pviter.get_value().type;
149 type = Palete.Gir.fqtypeLookup(type, ns);
151 var val = this.toValue(this.node.get(k).strip(), type);
153 print("skip (failed to transform value %s type = %s from %s\n",
154 cls + "." + k, type, this.node.get(k).strip());
157 print ("set_property ( %s , %s / %s)\n", k, this.node.get(k).strip(), val.strdup_contents());
160 ret.set_property(k, val);
165 // for now... - just try the builder style packing
169 if (this.node.items.size < 1) {
173 for (var i = 0; i < this.node.items.size; i++ ) {
175 var ch = this.mungeChild(this.node.items.get(i));
177 this.children.add(ch);
182 this.afterChildren();
191 public void afterChildren()
193 // things like GtkNotebook - we have to pack children after they have been created..
194 var cls = this.node.fqn().replace(".", "");
196 if (cls == "GtkNotebook") {
197 this.afterChildrenGtkNotebook();
204 public void afterChildrenGtkNotebook()
206 // we have a number of children..
207 // some are labels - this might need to be more complex...
208 // perhaps labels should be a special property labels[] of the notebook..
209 var labels = new Gee.ArrayList<NodeToGtk>();
210 var bodies = new Gee.ArrayList<NodeToGtk>();
211 for (var i = 0; i < this.children.size; i++) {
212 var cn = this.children.get(i).node.fqn().replace(".", "");
213 if (cn != "GtkLabel") {
214 bodies.add(this.children.get(i));
217 labels.add(this.children.get(i));
219 for (var i = 0; i < bodies.size; i++) {
220 ((global::Gtk.Notebook)this.wrapped_object).append_page(
221 (global::Gtk.Notebook) bodies.get(i).wrapped_object,
222 (labels.size > i - 1) ?
223 (global::Gtk.Notebook) labels.get(i).wrapped_object :
236 * called after the this.object has been created
237 * and it needs to be packed onto parent.
239 public void packParent()
241 var cls = this.node.fqn().replace(".", "");
243 var gtkbuilder = new global::Gtk.Builder();
244 var cls_gtype = gtkbuilder.get_type_from_name(cls);
246 if (this.parentObj == null) {
251 var parent = this.parentObj.wrapped_object;
255 if (parent == null) { // no parent.. can not pack.
258 // ------------- handle various special parents .. -----------
260 var par_type = this.parentObj.node.fqn().replace(".", "");
262 if (par_type == "GtkNotebook") {
263 // do not pack - it's done afterwards...
267 // ------------- handle various child types.. -----------
269 if (cls == "GtkMenu") {
274 if (cls == "GtkTreeStore") { // other stores?
275 // tree store is buildable??? ---
276 this.packTreeStore();
279 if (cls =="GtkTreeViewColumn") { // other stores?
280 //?? treeview column is actually buildable -- but we do not use the builder???
281 this.packTreeViewColumn();
284 if (cls_gtype.is_a(typeof(global::Gtk.CellRenderer))) { // other stores?
285 this.packCellRenderer();
291 // -- handle buildable add_child..
292 if ( cls_gtype.is_a(typeof(global::Gtk.Buildable))
294 parent.get_type().is_a(typeof(global::Gtk.Buildable))
297 ((global::Gtk.Buildable)parent).add_child(gtkbuilder,
298 this.wrapped_object, null);
307 public void packMenu()
311 var parent = this.parentObj.wrapped_object;
312 if (!parent.get_type().is_a(typeof(global::Gtk.Widget))) {
313 print("skip menu pack - parent is not a widget");
317 var p = (global::Gtk.Menu)this.wrapped_object;
318 ((global::Gtk.Widget)parent).button_press_event.connect((s, ev) => {
319 p.set_screen(Gdk.Screen.get_default());
321 p.popup(null, null, null, ev.button, ev.time);
326 public void packTreeStore()
328 var parent = this.parentObj.wrapped_object;
329 if (!parent.get_type().is_a(typeof(global::Gtk.TreeView))) {
330 print("skip treestore pack - parent is not a treeview");
333 ((global::Gtk.TreeView)parent).set_model((global::Gtk.TreeModel)this.wrapped_object);
336 public void packTreeViewColumn()
338 var parent = this.parentObj.wrapped_object;
339 if (!parent.get_type().is_a(typeof(global::Gtk.TreeView))) {
340 print("skip packGtkViewColumn pack - parent is not a treeview");
343 ((global::Gtk.TreeView)parent).append_column((global::Gtk.TreeViewColumn)this.wrapped_object);
344 // init contains the add_attribute for what to render...
349 public void packCellRenderer()
351 var parent = this.parentObj.wrapped_object;
352 if (!parent.get_type().is_a(typeof(global::Gtk.TreeViewColumn))) {
353 print("skip packGtkViewColumn pack - parent is not a treeview");
356 ((global::Gtk.TreeViewColumn)parent).pack_start((global::Gtk.CellRenderer)this.wrapped_object, false);
357 // init contains the add_attribute for what to render...
362 public void packContainerParams()
365 if (this.parentObj == null) {
368 // child must be a widget..
369 if (!this.wrapped_object.get_type().is_a(typeof(global::Gtk.Widget))) {
373 var parent_gir = Palete.Gir.factoryFqn(this.parentObj.node.fqn());
375 var parent = this.parentObj.wrapped_object;
377 if (parent_gir == null) {
381 // let's test just setting expand to false...
382 var cls_methods = parent_gir.methods;
383 if (cls_methods == null) {
387 if (!this.node.props.has_key("* pack") ||
388 this.node.props.get("* pack").length < 1) {
392 var ns = this.parentObj.node.fqn().split(".")[0];
394 var pack = this.node.props.get("* pack").split(",");
397 if (cls_methods.has_key(pack[0])) {
398 var mparams = cls_methods.get(pack[0]).paramset.params;
399 for (var i = 1; i < mparams.size; i++ ) {
400 if (i > (pack.length -1)) {
404 var k = mparams.get(i).name;
408 var type = mparams.get(i).type;
409 type = Palete.Gir.fqtypeLookup(type, ns);
411 var val = this.toValue(pack[i].strip(), type);
413 print("skip (failed to transform value %s type = %s from %s\n",
414 this.parentObj.node.fqn() + "." + k, type, pack[i].strip());
417 print ("pack:set_property ( %s , %s / %s)\n", k, pack[i].strip(), val.strdup_contents());
419 ((global::Gtk.Container)parent).child_set_property(
420 (global::Gtk.Widget)this.wrapped_object , k, val);
432 public GLib.Value? toValue(string val, string type) {
434 var gtkbuilder = new global::Gtk.Builder();
436 if (type == "utf8") {
437 var qret = GLib.Value(typeof(string));
438 qret.set_string(val);
442 var prop_gtype = gtkbuilder.get_type_from_name(type);
445 if (prop_gtype == GLib.Type.INVALID) {
451 var ret = GLib.Value(prop_gtype);
456 ret.set_boolean(val.down() == "false" ? false : true);
459 ret.set_uint(int.parse(val));
463 ret.set_int(int.parse(val));
467 ret.set_float(long.parse(val));
476 var sval = GLib.Value(typeof(string));
477 sval.set_string(val);
479 if (!sval.transform(ref ret)) {