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)
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 = null;
49 public Object? munge ( )
51 var ret = this.mungeNode();
56 return ret.wrapped_object;
59 public NodeToGtk? mungeChild( Node cnode)
61 var x = new NodeToGtk(cnode);
67 public NodeToGtk? mungeNode()
70 var parent = this.parentObj != null ? this.parentObj.wrapped_object : null;
71 var cls = this.node.fqn().replace(".", "");
72 var ns = this.node.fqn().split(".")[0];
73 var gtkbuilder = new global::Gtk.Builder();
75 var cls_gtype = gtkbuilder.get_type_from_name(cls);
76 print("Type: %s ?= %s\n", this.node.fqn(), cls_gtype.name());
78 if (cls_gtype == GLib.Type.INVALID) {
79 print("SKIP - gtype is invalid\n");
82 // if it's a window...
84 if (cls_gtype.is_a(typeof(global::Gtk.Window))) {
85 // what if it has none...
86 if (this.node.items.size < 1) {
89 return this.mungeChild(this.node.items.get(0));
92 var ret = Object.new(cls_gtype);
93 ret.ref(); //??? problematic?
94 this.wrapped_object = ret;
99 //case "GtkTreeStore": // top level.. - named and referenced
100 case "GtkListStore": // top level.. - named and referenced
101 //case "GtkTreeViewColumn": // part of liststore?!?!
102 //case "GtkMenu": // top level..
103 //case "GtkCellRendererText":
104 case "GtkSourceBuffer":
105 case "GtkClutterActor"://fixme..
106 case "GtkClutterEmbed"://fixme.. -- we can not nest embedded.. need to solve..
117 if (parent != null && parent.get_type().is_a(typeof(global::Gtk.Container))) {
118 this.packContainerParams();
121 var cls_gir =Palete.Gir.factoryFqn(this.node.fqn());
122 if (cls_gir == null) {
125 //var id = this.node.uid();
126 //var ret = @"$pad<object class=\"$cls\" id=\"$id\">\n";
128 var props = cls_gir.props;
131 var pviter = props.map_iterator();
132 while (pviter.next()) {
134 // print("Check: " +cls + "::(" + pviter.get_value().propertyof + ")" + pviter.get_key() + " " );
135 var k = pviter.get_key();
136 // skip items we have already handled..
137 if (!this.node.has(k)) {
140 // find out the type of the property...
141 var type = pviter.get_value().type;
142 type = Palete.Gir.fqtypeLookup(type, ns);
144 var val = this.toValue(this.node.get(k).strip(), type);
146 print("skip (failed to transform value %s type = %s from %s\n",
147 cls + "." + k, type, this.node.get(k).strip());
150 print ("set_property ( %s , %s / %s)\n", k, this.node.get(k).strip(), val.strdup_contents());
153 ret.set_property(k, val);
158 // for now... - just try the builder style packing
162 if (this.node.items.size < 1) {
166 for (var i = 0; i < this.node.items.size; i++ ) {
168 var ch = this.mungeChild(this.node.items.get(i));
170 this.children.add(ch);
175 this.afterChildren();
184 public void afterChildren()
186 // things like GtkNotebook - we have to pack children after they have been created..
187 var cls = this.node.fqn().replace(".", "");
189 if (cls == "GtkNotebook") {
190 this.afterChildrenGtkNotebook();
197 public void afterChildrenGtkNotebook()
199 // we have a number of children..
200 // some are labels - this might need to be more complex...
201 // perhaps labels should be a special property labels[] of the notebook..
202 var labels = new Gee.ArrayList<NodeToGtk>();
203 var bodies = new Gee.ArrayList<NodeToGtk>();
204 for (var i = 0; i < this.children.size; i++) {
205 var cn = this.children.get(i).node.fqn().replace(".", "");
206 if (cn != "GtkLabel") {
207 bodies.add(this.children.get(i));
210 labels.add(this.children.get(i));
212 for (var i = 0; i < bodies.size; i++) {
213 ((global::Gtk.Notebook)this.wrapped_object).append_page(
214 (global::Gtk.Notebook) bodies.get(i).wrapped_object,
215 (labels.size > i - 1) ?
216 (global::Gtk.Notebook) labels.get(i).wrapped_object :
229 * called after the this.object has been created
230 * and it needs to be packed onto parent.
232 public void packParent()
234 var cls = this.node.fqn().replace(".", "");
236 var gtkbuilder = new global::Gtk.Builder();
237 var cls_gtype = gtkbuilder.get_type_from_name(cls);
239 if (this.parentObj == null) {
244 var parent = this.parentObj.wrapped_object;
248 if (parent == null) { // no parent.. can not pack.
251 // ------------- handle various special parents .. -----------
253 var par_type = this.parentObj.node.fqn().replace(".", "");
255 if (par_type == "GtkNotebook") {
256 // do not pack - it's done afterwards...
260 // ------------- handle various child types.. -----------
262 if (cls == "GtkMenu") {
267 if (cls == "GtkTreeStore") { // other stores?
268 // tree store is buildable??? ---
269 this.packTreeStore();
272 if (cls =="GtkTreeViewColumn") { // other stores?
273 //?? treeview column is actually buildable -- but we do not use the builder???
274 this.packTreeViewColumn();
277 if (cls_gtype.is_a(typeof(global::Gtk.CellRenderer))) { // other stores?
278 this.packCellRenderer();
284 // -- handle buildable add_child..
285 if ( cls_gtype.is_a(typeof(global::Gtk.Buildable))
287 parent.get_type().is_a(typeof(global::Gtk.Buildable))
290 ((global::Gtk.Buildable)parent).add_child(gtkbuilder,
291 this.wrapped_object, null);
300 public void packMenu()
304 var parent = this.parentObj.wrapped_object;
305 if (!parent.get_type().is_a(typeof(global::Gtk.Widget))) {
306 print("skip menu pack - parent is not a widget");
310 var p = (global::Gtk.Menu)this.wrapped_object;
311 ((global::Gtk.Widget)parent).button_press_event.connect((s, ev) => {
312 p.set_screen(Gdk.Screen.get_default());
314 p.popup(null, null, null, ev.button, ev.time);
319 public void packTreeStore()
321 var parent = this.parentObj.wrapped_object;
322 if (!parent.get_type().is_a(typeof(global::Gtk.TreeView))) {
323 print("skip treestore pack - parent is not a treeview");
326 ((global::Gtk.TreeView)parent).set_model((global::Gtk.TreeModel)this.wrapped_object);
329 public void packTreeViewColumn()
331 var parent = this.parentObj.wrapped_object;
332 if (!parent.get_type().is_a(typeof(global::Gtk.TreeView))) {
333 print("skip packGtkViewColumn pack - parent is not a treeview");
336 ((global::Gtk.TreeView)parent).append_column((global::Gtk.TreeViewColumn)this.wrapped_object);
337 // init contains the add_attribute for what to render...
342 public void packCellRenderer()
344 var parent = this.parentObj.wrapped_object;
345 if (!parent.get_type().is_a(typeof(global::Gtk.TreeViewColumn))) {
346 print("skip packGtkViewColumn pack - parent is not a treeview");
349 ((global::Gtk.TreeViewColumn)parent).pack_start((global::Gtk.CellRenderer)this.wrapped_object, false);
350 // init contains the add_attribute for what to render...
355 public void packContainerParams()
358 if (this.parentObj == null) {
361 // child must be a widget..
362 if (!this.wrapped_object.get_type().is_a(typeof(global::Gtk.Widget))) {
366 var parent_gir = Palete.Gir.factoryFqn(this.parentObj.node.fqn());
368 var parent = this.parentObj.wrapped_object;
370 if (parent_gir == null) {
374 // let's test just setting expand to false...
375 var cls_methods = parent_gir.methods;
376 if (cls_methods == null) {
380 if (!this.node.props.has_key("* pack") ||
381 this.node.props.get("* pack").length < 1) {
385 var ns = this.parentObj.node.fqn().split(".")[0];
387 var pack = this.node.props.get("* pack").split(",");
390 if (cls_methods.has_key(pack[0])) {
391 var mparams = cls_methods.get(pack[0]).paramset.params;
392 for (var i = 1; i < mparams.size; i++ ) {
393 if (i > (pack.length -1)) {
397 var k = mparams.get(i).name;
401 var type = mparams.get(i).type;
402 type = Palete.Gir.fqtypeLookup(type, ns);
404 var val = this.toValue(pack[i].strip(), type);
406 print("skip (failed to transform value %s type = %s from %s\n",
407 this.parentObj.node.fqn() + "." + k, type, pack[i].strip());
410 print ("pack:set_property ( %s , %s / %s)\n", k, pack[i].strip(), val.strdup_contents());
412 ((global::Gtk.Container)parent).child_set_property(
413 (global::Gtk.Widget)this.wrapped_object , k, val);
425 public GLib.Value? toValue(string val, string type) {
427 var gtkbuilder = new global::Gtk.Builder();
429 if (type == "utf8") {
430 var qret = GLib.Value(typeof(string));
431 qret.set_string(val);
435 var prop_gtype = gtkbuilder.get_type_from_name(type);
438 if (prop_gtype == GLib.Type.INVALID) {
444 var ret = GLib.Value(prop_gtype);
449 ret.set_boolean(val.down() == "false" ? false : true);
452 ret.set_uint(int.parse(val));
456 ret.set_int(int.parse(val));
460 ret.set_float(long.parse(val));
469 var sval = GLib.Value(typeof(string));
470 sval.set_string(val);
472 if (!sval.transform(ref ret)) {