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 new_node.loadFromJson(Json.Object obj, 2);
62 } catch (Exception e) {
63 print("%s", e.toString());
71 public Object? munge ( )
73 var ret = this.mungeNode();
78 return ret.wrapped_object;
81 public NodeToGtk? mungeChild( Node cnode)
83 var x = new NodeToGtk(cnode, this);
89 public NodeToGtk? mungeNode()
92 var parent = this.parentObj != null ? this.parentObj.wrapped_object : null;
93 var cls = this.node.fqn().replace(".", "");
94 var ns = this.node.fqn().split(".")[0];
95 var gtkbuilder = new global::Gtk.Builder();
97 var cls_gtype = gtkbuilder.get_type_from_name(cls);
98 print("Type: %s ?= %s\n", this.node.fqn(), cls_gtype.name());
100 if (cls_gtype == GLib.Type.INVALID) {
101 print("SKIP - gtype is invalid\n");
104 // if it's a window...
106 if (cls_gtype.is_a(typeof(global::Gtk.Window))) {
107 // what if it has none...
108 if (this.node.items.size < 1) {
111 return this.mungeChild(this.node.items.get(0));
114 var ret = Object.new(cls_gtype);
115 ret.ref(); //??? problematic?
116 this.wrapped_object = ret;
121 //case "GtkTreeStore": // top level.. - named and referenced
122 case "GtkListStore": // top level.. - named and referenced
123 //case "GtkTreeViewColumn": // part of liststore?!?!
124 //case "GtkMenu": // top level..
125 //case "GtkCellRendererText":
126 case "GtkSourceBuffer":
127 case "GtkClutterActor"://fixme..
128 case "GtkClutterEmbed"://fixme.. -- we can not nest embedded.. need to solve..
139 if (parent != null && parent.get_type().is_a(typeof(global::Gtk.Container))) {
140 this.packContainerParams();
143 var cls_gir =Palete.Gir.factoryFqn(this.node.fqn());
144 if (cls_gir == null) {
147 //var id = this.node.uid();
148 //var ret = @"$pad<object class=\"$cls\" id=\"$id\">\n";
150 var props = cls_gir.props;
153 var pviter = props.map_iterator();
154 while (pviter.next()) {
156 // print("Check: " +cls + "::(" + pviter.get_value().propertyof + ")" + pviter.get_key() + " " );
157 var k = pviter.get_key();
158 // skip items we have already handled..
159 if (!this.node.has(k)) {
162 // find out the type of the property...
163 var type = pviter.get_value().type;
164 type = Palete.Gir.fqtypeLookup(type, ns);
166 var val = this.toValue(this.node.get(k).strip(), type);
168 print("skip (failed to transform value %s type = %s from %s\n",
169 cls + "." + k, type, this.node.get(k).strip());
172 print ("set_property ( %s , %s / %s)\n", k, this.node.get(k).strip(), val.strdup_contents());
175 ret.set_property(k, val);
180 // for now... - just try the builder style packing
184 if (this.node.items.size < 1) {
188 for (var i = 0; i < this.node.items.size; i++ ) {
190 var ch = this.mungeChild(this.node.items.get(i));
192 this.children.add(ch);
197 this.afterChildren();
206 public void afterChildren()
208 // things like GtkNotebook - we have to pack children after they have been created..
209 var cls = this.node.fqn().replace(".", "");
211 if (cls == "GtkNotebook") {
212 this.afterChildrenGtkNotebook();
219 public void afterChildrenGtkNotebook()
221 // we have a number of children..
222 // some are labels - this might need to be more complex...
223 // perhaps labels should be a special property labels[] of the notebook..
224 var labels = new Gee.ArrayList<NodeToGtk>();
225 var bodies = new Gee.ArrayList<NodeToGtk>();
226 for (var i = 0; i < this.children.size; i++) {
227 var cn = this.children.get(i).node.fqn().replace(".", "");
228 if (cn != "GtkLabel") {
229 bodies.add(this.children.get(i));
232 labels.add(this.children.get(i));
234 for (var i = 0; i < bodies.size; i++) {
235 ((global::Gtk.Notebook)this.wrapped_object).append_page(
236 (global::Gtk.Notebook) bodies.get(i).wrapped_object,
237 (labels.size > i - 1) ?
238 (global::Gtk.Notebook) labels.get(i).wrapped_object :
251 * called after the this.object has been created
252 * and it needs to be packed onto parent.
254 public void packParent()
256 var cls = this.node.fqn().replace(".", "");
258 var gtkbuilder = new global::Gtk.Builder();
259 var cls_gtype = gtkbuilder.get_type_from_name(cls);
261 if (this.parentObj == null) {
266 var parent = this.parentObj.wrapped_object;
270 if (parent == null) { // no parent.. can not pack.
273 // ------------- handle various special parents .. -----------
275 var par_type = this.parentObj.node.fqn().replace(".", "");
277 if (par_type == "GtkNotebook") {
278 // do not pack - it's done afterwards...
282 // ------------- handle various child types.. -----------
284 if (cls == "GtkMenu") {
289 if (cls == "GtkTreeStore") { // other stores?
290 // tree store is buildable??? ---
291 this.packTreeStore();
294 if (cls =="GtkTreeViewColumn") { // other stores?
295 //?? treeview column is actually buildable -- but we do not use the builder???
296 this.packTreeViewColumn();
299 if (cls_gtype.is_a(typeof(global::Gtk.CellRenderer))) { // other stores?
300 this.packCellRenderer();
306 // -- handle buildable add_child..
307 if ( cls_gtype.is_a(typeof(global::Gtk.Buildable))
309 parent.get_type().is_a(typeof(global::Gtk.Buildable))
312 ((global::Gtk.Buildable)parent).add_child(gtkbuilder,
313 this.wrapped_object, null);
322 public void packMenu()
326 var parent = this.parentObj.wrapped_object;
327 if (!parent.get_type().is_a(typeof(global::Gtk.Widget))) {
328 print("skip menu pack - parent is not a widget");
332 var p = (global::Gtk.Menu)this.wrapped_object;
333 ((global::Gtk.Widget)parent).button_press_event.connect((s, ev) => {
334 p.set_screen(Gdk.Screen.get_default());
336 p.popup(null, null, null, ev.button, ev.time);
341 public void packTreeStore()
343 var parent = this.parentObj.wrapped_object;
344 if (!parent.get_type().is_a(typeof(global::Gtk.TreeView))) {
345 print("skip treestore pack - parent is not a treeview");
348 ((global::Gtk.TreeView)parent).set_model((global::Gtk.TreeModel)this.wrapped_object);
351 public void packTreeViewColumn()
353 var parent = this.parentObj.wrapped_object;
354 if (!parent.get_type().is_a(typeof(global::Gtk.TreeView))) {
355 print("skip packGtkViewColumn pack - parent is not a treeview");
358 ((global::Gtk.TreeView)parent).append_column((global::Gtk.TreeViewColumn)this.wrapped_object);
359 // init contains the add_attribute for what to render...
364 public void packCellRenderer()
366 var parent = this.parentObj.wrapped_object;
367 if (!parent.get_type().is_a(typeof(global::Gtk.TreeViewColumn))) {
368 print("skip packGtkViewColumn pack - parent is not a treeview");
371 ((global::Gtk.TreeViewColumn)parent).pack_start((global::Gtk.CellRenderer)this.wrapped_object, false);
372 // init contains the add_attribute for what to render...
377 public void packContainerParams()
380 if (this.parentObj == null) {
383 // child must be a widget..
384 if (!this.wrapped_object.get_type().is_a(typeof(global::Gtk.Widget))) {
388 var parent_gir = Palete.Gir.factoryFqn(this.parentObj.node.fqn());
390 var parent = this.parentObj.wrapped_object;
392 if (parent_gir == null) {
396 // let's test just setting expand to false...
397 var cls_methods = parent_gir.methods;
398 if (cls_methods == null) {
402 if (!this.node.props.has_key("* pack") ||
403 this.node.props.get("* pack").length < 1) {
407 var ns = this.parentObj.node.fqn().split(".")[0];
409 var pack = this.node.props.get("* pack").split(",");
412 if (cls_methods.has_key(pack[0])) {
413 var mparams = cls_methods.get(pack[0]).paramset.params;
414 for (var i = 1; i < mparams.size; i++ ) {
415 if (i > (pack.length -1)) {
419 var k = mparams.get(i).name;
423 var type = mparams.get(i).type;
424 type = Palete.Gir.fqtypeLookup(type, ns);
426 var val = this.toValue(pack[i].strip(), type);
428 print("skip (failed to transform value %s type = %s from %s\n",
429 this.parentObj.node.fqn() + "." + k, type, pack[i].strip());
432 print ("pack:set_property ( %s , %s / %s)\n", k, pack[i].strip(), val.strdup_contents());
434 ((global::Gtk.Container)parent).child_set_property(
435 (global::Gtk.Widget)this.wrapped_object , k, val);
447 public GLib.Value? toValue(string val, string type) {
449 var gtkbuilder = new global::Gtk.Builder();
451 if (type == "utf8") {
452 var qret = GLib.Value(typeof(string));
453 qret.set_string(val);
457 var prop_gtype = gtkbuilder.get_type_from_name(type);
460 if (prop_gtype == GLib.Type.INVALID) {
466 var ret = GLib.Value(prop_gtype);
471 ret.set_boolean(val.down() == "false" ? false : true);
474 ret.set_uint(int.parse(val));
478 ret.set_int(int.parse(val));
482 ret.set_float(long.parse(val));
491 var sval = GLib.Value(typeof(string));
492 sval.set_string(val);
494 if (!sval.transform(ref ret)) {