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(
58 var new_node = new Node();
59 new_node.loadFromJson(Json.Object obj, 2);
69 public Object? munge ( )
71 var ret = this.mungeNode();
76 return ret.wrapped_object;
79 public NodeToGtk? mungeChild( Node cnode)
81 var x = new NodeToGtk(cnode, this);
87 public NodeToGtk? mungeNode()
90 var parent = this.parentObj != null ? this.parentObj.wrapped_object : null;
91 var cls = this.node.fqn().replace(".", "");
92 var ns = this.node.fqn().split(".")[0];
93 var gtkbuilder = new global::Gtk.Builder();
95 var cls_gtype = gtkbuilder.get_type_from_name(cls);
96 print("Type: %s ?= %s\n", this.node.fqn(), cls_gtype.name());
98 if (cls_gtype == GLib.Type.INVALID) {
99 print("SKIP - gtype is invalid\n");
102 // if it's a window...
104 if (cls_gtype.is_a(typeof(global::Gtk.Window))) {
105 // what if it has none...
106 if (this.node.items.size < 1) {
109 return this.mungeChild(this.node.items.get(0));
112 var ret = Object.new(cls_gtype);
113 ret.ref(); //??? problematic?
114 this.wrapped_object = ret;
119 //case "GtkTreeStore": // top level.. - named and referenced
120 case "GtkListStore": // top level.. - named and referenced
121 //case "GtkTreeViewColumn": // part of liststore?!?!
122 //case "GtkMenu": // top level..
123 //case "GtkCellRendererText":
124 case "GtkSourceBuffer":
125 case "GtkClutterActor"://fixme..
126 case "GtkClutterEmbed"://fixme.. -- we can not nest embedded.. need to solve..
137 if (parent != null && parent.get_type().is_a(typeof(global::Gtk.Container))) {
138 this.packContainerParams();
141 var cls_gir =Palete.Gir.factoryFqn(this.node.fqn());
142 if (cls_gir == null) {
145 //var id = this.node.uid();
146 //var ret = @"$pad<object class=\"$cls\" id=\"$id\">\n";
148 var props = cls_gir.props;
151 var pviter = props.map_iterator();
152 while (pviter.next()) {
154 // print("Check: " +cls + "::(" + pviter.get_value().propertyof + ")" + pviter.get_key() + " " );
155 var k = pviter.get_key();
156 // skip items we have already handled..
157 if (!this.node.has(k)) {
160 // find out the type of the property...
161 var type = pviter.get_value().type;
162 type = Palete.Gir.fqtypeLookup(type, ns);
164 var val = this.toValue(this.node.get(k).strip(), type);
166 print("skip (failed to transform value %s type = %s from %s\n",
167 cls + "." + k, type, this.node.get(k).strip());
170 print ("set_property ( %s , %s / %s)\n", k, this.node.get(k).strip(), val.strdup_contents());
173 ret.set_property(k, val);
178 // for now... - just try the builder style packing
182 if (this.node.items.size < 1) {
186 for (var i = 0; i < this.node.items.size; i++ ) {
188 var ch = this.mungeChild(this.node.items.get(i));
190 this.children.add(ch);
195 this.afterChildren();
204 public void afterChildren()
206 // things like GtkNotebook - we have to pack children after they have been created..
207 var cls = this.node.fqn().replace(".", "");
209 if (cls == "GtkNotebook") {
210 this.afterChildrenGtkNotebook();
217 public void afterChildrenGtkNotebook()
219 // we have a number of children..
220 // some are labels - this might need to be more complex...
221 // perhaps labels should be a special property labels[] of the notebook..
222 var labels = new Gee.ArrayList<NodeToGtk>();
223 var bodies = new Gee.ArrayList<NodeToGtk>();
224 for (var i = 0; i < this.children.size; i++) {
225 var cn = this.children.get(i).node.fqn().replace(".", "");
226 if (cn != "GtkLabel") {
227 bodies.add(this.children.get(i));
230 labels.add(this.children.get(i));
232 for (var i = 0; i < bodies.size; i++) {
233 ((global::Gtk.Notebook)this.wrapped_object).append_page(
234 (global::Gtk.Notebook) bodies.get(i).wrapped_object,
235 (labels.size > i - 1) ?
236 (global::Gtk.Notebook) labels.get(i).wrapped_object :
249 * called after the this.object has been created
250 * and it needs to be packed onto parent.
252 public void packParent()
254 var cls = this.node.fqn().replace(".", "");
256 var gtkbuilder = new global::Gtk.Builder();
257 var cls_gtype = gtkbuilder.get_type_from_name(cls);
259 if (this.parentObj == null) {
264 var parent = this.parentObj.wrapped_object;
268 if (parent == null) { // no parent.. can not pack.
271 // ------------- handle various special parents .. -----------
273 var par_type = this.parentObj.node.fqn().replace(".", "");
275 if (par_type == "GtkNotebook") {
276 // do not pack - it's done afterwards...
280 // ------------- handle various child types.. -----------
282 if (cls == "GtkMenu") {
287 if (cls == "GtkTreeStore") { // other stores?
288 // tree store is buildable??? ---
289 this.packTreeStore();
292 if (cls =="GtkTreeViewColumn") { // other stores?
293 //?? treeview column is actually buildable -- but we do not use the builder???
294 this.packTreeViewColumn();
297 if (cls_gtype.is_a(typeof(global::Gtk.CellRenderer))) { // other stores?
298 this.packCellRenderer();
304 // -- handle buildable add_child..
305 if ( cls_gtype.is_a(typeof(global::Gtk.Buildable))
307 parent.get_type().is_a(typeof(global::Gtk.Buildable))
310 ((global::Gtk.Buildable)parent).add_child(gtkbuilder,
311 this.wrapped_object, null);
320 public void packMenu()
324 var parent = this.parentObj.wrapped_object;
325 if (!parent.get_type().is_a(typeof(global::Gtk.Widget))) {
326 print("skip menu pack - parent is not a widget");
330 var p = (global::Gtk.Menu)this.wrapped_object;
331 ((global::Gtk.Widget)parent).button_press_event.connect((s, ev) => {
332 p.set_screen(Gdk.Screen.get_default());
334 p.popup(null, null, null, ev.button, ev.time);
339 public void packTreeStore()
341 var parent = this.parentObj.wrapped_object;
342 if (!parent.get_type().is_a(typeof(global::Gtk.TreeView))) {
343 print("skip treestore pack - parent is not a treeview");
346 ((global::Gtk.TreeView)parent).set_model((global::Gtk.TreeModel)this.wrapped_object);
349 public void packTreeViewColumn()
351 var parent = this.parentObj.wrapped_object;
352 if (!parent.get_type().is_a(typeof(global::Gtk.TreeView))) {
353 print("skip packGtkViewColumn pack - parent is not a treeview");
356 ((global::Gtk.TreeView)parent).append_column((global::Gtk.TreeViewColumn)this.wrapped_object);
357 // init contains the add_attribute for what to render...
362 public void packCellRenderer()
364 var parent = this.parentObj.wrapped_object;
365 if (!parent.get_type().is_a(typeof(global::Gtk.TreeViewColumn))) {
366 print("skip packGtkViewColumn pack - parent is not a treeview");
369 ((global::Gtk.TreeViewColumn)parent).pack_start((global::Gtk.CellRenderer)this.wrapped_object, false);
370 // init contains the add_attribute for what to render...
375 public void packContainerParams()
378 if (this.parentObj == null) {
381 // child must be a widget..
382 if (!this.wrapped_object.get_type().is_a(typeof(global::Gtk.Widget))) {
386 var parent_gir = Palete.Gir.factoryFqn(this.parentObj.node.fqn());
388 var parent = this.parentObj.wrapped_object;
390 if (parent_gir == null) {
394 // let's test just setting expand to false...
395 var cls_methods = parent_gir.methods;
396 if (cls_methods == null) {
400 if (!this.node.props.has_key("* pack") ||
401 this.node.props.get("* pack").length < 1) {
405 var ns = this.parentObj.node.fqn().split(".")[0];
407 var pack = this.node.props.get("* pack").split(",");
410 if (cls_methods.has_key(pack[0])) {
411 var mparams = cls_methods.get(pack[0]).paramset.params;
412 for (var i = 1; i < mparams.size; i++ ) {
413 if (i > (pack.length -1)) {
417 var k = mparams.get(i).name;
421 var type = mparams.get(i).type;
422 type = Palete.Gir.fqtypeLookup(type, ns);
424 var val = this.toValue(pack[i].strip(), type);
426 print("skip (failed to transform value %s type = %s from %s\n",
427 this.parentObj.node.fqn() + "." + k, type, pack[i].strip());
430 print ("pack:set_property ( %s , %s / %s)\n", k, pack[i].strip(), val.strdup_contents());
432 ((global::Gtk.Container)parent).child_set_property(
433 (global::Gtk.Widget)this.wrapped_object , k, val);
445 public GLib.Value? toValue(string val, string type) {
447 var gtkbuilder = new global::Gtk.Builder();
449 if (type == "utf8") {
450 var qret = GLib.Value(typeof(string));
451 qret.set_string(val);
455 var prop_gtype = gtkbuilder.get_type_from_name(type);
458 if (prop_gtype == GLib.Type.INVALID) {
464 var ret = GLib.Value(prop_gtype);
469 ret.set_boolean(val.down() == "false" ? false : true);
472 ret.set_uint(int.parse(val));
476 ret.set_int(int.parse(val));
480 ret.set_float(long.parse(val));
489 var sval = GLib.Value(typeof(string));
490 sval.set_string(val);
492 if (!sval.transform(ref ret)) {