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 rebuild node from return value..
53 var ret = Palete.Javascript.singleton().executeFile(
54 BuilderApplication.configDirectory() + "/resources/node_to_gtk.js",
58 var new_node = new Node();
59 var pa = new Json.Parser();
60 pa.load_from_data(ret);
61 var rnode = pa.get_root();
64 new_node.loadFromJson(rnode.get_object(), 2);
67 } catch (Palete.JavascriptError e) {
68 print("Error: %s\n", e.message);
76 public Object? munge ( )
78 var ret = this.mungeNode();
83 return ret.wrapped_object;
86 public NodeToGtk? mungeChild( Node cnode)
88 var x = new NodeToGtk(cnode, this);
94 public NodeToGtk? mungeNode()
97 var parent = this.parentObj != null ? this.parentObj.wrapped_object : null;
98 var cls = this.node.fqn().replace(".", "");
99 var ns = this.node.fqn().split(".")[0];
100 var gtkbuilder = new global::Gtk.Builder();
102 var cls_gtype = gtkbuilder.get_type_from_name(cls);
103 print("Type: %s ?= %s\n", this.node.fqn(), cls_gtype.name());
105 if (cls_gtype == GLib.Type.INVALID) {
106 print("SKIP - gtype is invalid\n");
109 // if it's a window... -- things we can not render....
111 if (cls_gtype.is_a(typeof(global::Gtk.Window))) {
112 // what if it has none...
113 if (this.node.items.size < 1) {
116 return this.mungeChild(this.node.items.get(0));
118 if (cls_gtype.is_a(typeof(global::Gtk.Popover))) {
119 // what if it has none...
120 if (this.node.items.size < 1) {
123 return this.mungeChild(this.node.items.get(0));
126 var ret = Object.new(cls_gtype);
127 ret.ref(); //??? problematic?
128 this.wrapped_object = ret;
133 //case "GtkTreeStore": // top level.. - named and referenced
134 case "GtkListStore": // top level.. - named and referenced
135 //case "GtkTreeViewColumn": // part of liststore?!?!
136 //case "GtkMenu": // top level..
137 //case "GtkCellRendererText":
138 case "GtkSourceBuffer":
139 case "GtkClutterActor"://fixme..
140 case "GtkClutterEmbed"://fixme.. -- we can not nest embedded.. need to solve..
151 if (parent != null && parent.get_type().is_a(typeof(global::Gtk.Container))) {
152 this.packContainerParams();
155 var cls_gir =Palete.Gir.factoryFqn(this.node.fqn());
156 if (cls_gir == null) {
159 //var id = this.node.uid();
160 //var ret = @"$pad<object class=\"$cls\" id=\"$id\">\n";
162 var props = cls_gir.props;
165 var pviter = props.map_iterator();
166 while (pviter.next()) {
168 // print("Check: " +cls + "::(" + pviter.get_value().propertyof + ")" + pviter.get_key() + " " );
169 var k = pviter.get_key();
170 // skip items we have already handled..
171 if (!this.node.has(k)) {
174 // find out the type of the property...
175 var type = pviter.get_value().type;
176 type = Palete.Gir.fqtypeLookup(type, ns);
178 var val = this.toValue(this.node.get(k).strip(), type);
180 print("skip (failed to transform value %s type = %s from %s\n",
181 cls + "." + k, type, this.node.get(k).strip());
184 print ("set_property ( %s , %s / %s)\n", k, this.node.get(k).strip(), val.strdup_contents());
187 ret.set_property(k, val);
192 // for now... - just try the builder style packing
196 if (this.node.items.size < 1) {
200 for (var i = 0; i < this.node.items.size; i++ ) {
202 var ch = this.mungeChild(this.node.items.get(i));
204 this.children.add(ch);
209 this.afterChildren();
218 public void afterChildren()
220 // things like GtkNotebook - we have to pack children after they have been created..
221 var cls = this.node.fqn().replace(".", "");
223 if (cls == "GtkNotebook") {
224 this.afterChildrenGtkNotebook();
231 public void afterChildrenGtkNotebook()
233 // we have a number of children..
234 // some are labels - this might need to be more complex...
235 // perhaps labels should be a special property labels[] of the notebook..
236 var labels = new Gee.ArrayList<NodeToGtk>();
237 var bodies = new Gee.ArrayList<NodeToGtk>();
238 for (var i = 0; i < this.children.size; i++) {
239 var cn = this.children.get(i).node.fqn().replace(".", "");
240 if (cn != "GtkLabel") {
241 bodies.add(this.children.get(i));
244 labels.add(this.children.get(i));
246 for (var i = 0; i < bodies.size; i++) {
247 ((global::Gtk.Notebook)this.wrapped_object).append_page(
248 (global::Gtk.Notebook) bodies.get(i).wrapped_object,
249 (labels.size > i - 1) ?
250 (global::Gtk.Notebook) labels.get(i).wrapped_object :
263 * called after the this.object has been created
264 * and it needs to be packed onto parent.
266 public void packParent()
268 var cls = this.node.fqn().replace(".", "");
270 var gtkbuilder = new global::Gtk.Builder();
271 var cls_gtype = gtkbuilder.get_type_from_name(cls);
273 if (this.parentObj == null) {
278 var parent = this.parentObj.wrapped_object;
282 if (parent == null) { // no parent.. can not pack.
285 // ------------- handle various special parents .. -----------
287 var par_type = this.parentObj.node.fqn().replace(".", "");
289 if (par_type == "GtkNotebook") {
290 // do not pack - it's done afterwards...
294 // ------------- handle various child types.. -----------
296 if (cls == "GtkMenu") {
301 if (cls == "GtkTreeStore") { // other stores?
302 // tree store is buildable??? ---
303 this.packTreeStore();
306 if (cls =="GtkTreeViewColumn") { // other stores?
307 //?? treeview column is actually buildable -- but we do not use the builder???
308 this.packTreeViewColumn();
311 if (cls_gtype.is_a(typeof(global::Gtk.CellRenderer))) { // other stores?
312 this.packCellRenderer();
318 // -- handle buildable add_child..
319 if ( cls_gtype.is_a(typeof(global::Gtk.Buildable))
321 parent.get_type().is_a(typeof(global::Gtk.Buildable))
324 ((global::Gtk.Buildable)parent).add_child(gtkbuilder,
325 this.wrapped_object, null);
334 public void packMenu()
338 var parent = this.parentObj.wrapped_object;
339 if (!parent.get_type().is_a(typeof(global::Gtk.Widget))) {
340 print("skip menu pack - parent is not a widget");
344 var p = (global::Gtk.Menu)this.wrapped_object;
345 ((global::Gtk.Widget)parent).button_press_event.connect((s, ev) => {
346 p.set_screen(Gdk.Screen.get_default());
348 p.popup(null, null, null, ev.button, ev.time);
353 public void packTreeStore()
355 var parent = this.parentObj.wrapped_object;
356 if (!parent.get_type().is_a(typeof(global::Gtk.TreeView))) {
357 print("skip treestore pack - parent is not a treeview");
360 ((global::Gtk.TreeView)parent).set_model((global::Gtk.TreeModel)this.wrapped_object);
363 public void packTreeViewColumn()
365 var parent = this.parentObj.wrapped_object;
366 if (!parent.get_type().is_a(typeof(global::Gtk.TreeView))) {
367 print("skip packGtkViewColumn pack - parent is not a treeview");
370 ((global::Gtk.TreeView)parent).append_column((global::Gtk.TreeViewColumn)this.wrapped_object);
371 // init contains the add_attribute for what to render...
376 public void packCellRenderer()
378 var parent = this.parentObj.wrapped_object;
379 if (!parent.get_type().is_a(typeof(global::Gtk.TreeViewColumn))) {
380 print("skip packGtkViewColumn pack - parent is not a treeview");
383 ((global::Gtk.TreeViewColumn)parent).pack_start((global::Gtk.CellRenderer)this.wrapped_object, false);
384 // init contains the add_attribute for what to render...
389 public void packContainerParams()
392 if (this.parentObj == null) {
395 // child must be a widget..
396 if (!this.wrapped_object.get_type().is_a(typeof(global::Gtk.Widget))) {
400 var parent_gir = Palete.Gir.factoryFqn(this.parentObj.node.fqn());
402 var parent = this.parentObj.wrapped_object;
404 if (parent_gir == null) {
408 // let's test just setting expand to false...
409 var cls_methods = parent_gir.methods;
410 if (cls_methods == null) {
414 if (!this.node.props.has_key("* pack") ||
415 this.node.props.get("* pack").length < 1) {
419 var ns = this.parentObj.node.fqn().split(".")[0];
421 var pack = this.node.props.get("* pack").split(",");
424 if (cls_methods.has_key(pack[0])) {
425 var mparams = cls_methods.get(pack[0]).paramset.params;
426 for (var i = 1; i < mparams.size; i++ ) {
427 if (i > (pack.length -1)) {
431 var k = mparams.get(i).name;
435 var type = mparams.get(i).type;
436 type = Palete.Gir.fqtypeLookup(type, ns);
438 var val = this.toValue(pack[i].strip(), type);
440 print("skip (failed to transform value %s type = %s from %s\n",
441 this.parentObj.node.fqn() + "." + k, type, pack[i].strip());
444 print ("pack:set_property ( %s , %s / %s)\n", k, pack[i].strip(), val.strdup_contents());
446 ((global::Gtk.Container)parent).child_set_property(
447 (global::Gtk.Widget)this.wrapped_object , k, val);
459 public GLib.Value? toValue(string val, string type) {
463 if (type == "string") {
464 var qret = GLib.Value(typeof(string));
465 qret.set_string(val);
471 * var gtkbuilder = new global::Gtk.Builder();
472 var prop_gtype = gtkbuilder.get_type_from_name(type);
475 if (prop_gtype == GLib.Type.INVALID) {
486 var ret = GLib.Value(typeof(bool));
487 ret.set_boolean(val.down() == "false" ? false : true);
491 var ret = GLib.Value(typeof(uint));
492 ret.set_uint(int.parse(val));
496 var ret = GLib.Value(typeof(int));
497 ret.set_int(int.parse(val));
503 var ret = GLib.Value(typeof(long));
504 ret.set_long((long)int64.parse(val));
508 var ret = GLib.Value(typeof(ulong));
509 ret.set_ulong((ulong) uint64.parse(val));
513 var ret = GLib.Value(typeof(float));
514 ret.set_float((float)double.parse(val));
518 var ret = GLib.Value(typeof(string));
525 var sval = GLib.Value(typeof(string));
526 sval.set_string(val);
528 if (!sval.transform(ref ret)) {
535 // should not get here..