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 about box, and turn that into load of real widgets..
18 public class JsRender.NodeToGtk : Object {
21 Object wrapped_object;
24 Gee.ArrayList<NodeToGtk> children;
26 Gee.ArrayList<string> els;
27 //Gee.ArrayList<string> skip;
28 Gee.HashMap<string,string> ar_props;
29 public static int vcnt = 0;
31 public NodeToGtk( Node node)
34 this.els = new Gee.ArrayList<string>();
35 this.children = new Gee.ArrayList<NodeToGtk>();
36 //this.skip = new Gee.ArrayList<string>();
37 this.ar_props = new Gee.HashMap<string,string>();
38 this.parentObj = null;
41 public Object? munge ( )
43 var ret = this.mungeNode();
48 return ret.wrapped_object;
51 public NodeToGtk? mungeChild( Node cnode)
53 var x = new NodeToGtk(cnode);
59 public NodeToGtk? mungeNode()
62 var parent = this.parentObj != null ? this.parentObj.wrapped_object : null;
63 var cls = this.node.fqn().replace(".", "");
64 var ns = this.node.fqn().split(".")[0];
65 var gtkbuilder = new global::Gtk.Builder();
67 var cls_gtype = gtkbuilder.get_type_from_name(cls);
68 print("Type: %s ?= %s\n", this.node.fqn(), cls_gtype.name());
70 if (cls_gtype == GLib.Type.INVALID) {
71 print("SKIP - gtype is invalid\n");
74 // if it's a window...
76 if (cls_gtype.is_a(typeof(global::Gtk.Window))) {
77 // what if it has none...
78 if (this.node.items.size < 1) {
81 return this.mungeChild(this.node.items.get(0));
84 var ret = Object.new(cls_gtype);
85 ret.ref(); //??? problematic?
86 this.wrapped_object = ret;
91 //case "GtkTreeStore": // top level.. - named and referenced
92 case "GtkListStore": // top level.. - named and referenced
93 //case "GtkTreeViewColumn": // part of liststore?!?!
94 //case "GtkMenu": // top level..
95 //case "GtkCellRendererText":
96 case "GtkSourceBuffer":
97 case "GtkClutterActor"://fixme..
98 case "GtkClutterEmbed"://fixme.. -- we can not nest embedded.. need to solve..
109 if (parent != null && parent.get_type().is_a(typeof(global::Gtk.Container))) {
110 this.packContainerParams();
113 var cls_gir =Palete.Gir.factoryFqn(this.node.fqn());
114 if (cls_gir == null) {
117 //var id = this.node.uid();
118 //var ret = @"$pad<object class=\"$cls\" id=\"$id\">\n";
120 var props = cls_gir.props;
123 var pviter = props.map_iterator();
124 while (pviter.next()) {
126 // print("Check: " +cls + "::(" + pviter.get_value().propertyof + ")" + pviter.get_key() + " " );
127 var k = pviter.get_key();
128 // skip items we have already handled..
129 if (!this.node.has(k)) {
132 // find out the type of the property...
133 var type = pviter.get_value().type;
134 type = Palete.Gir.fqtypeLookup(type, ns);
136 var val = this.toValue(this.node.get(k).strip(), type);
138 print("skip (failed to transform value %s type = %s from %s\n",
139 cls + "." + k, type, this.node.get(k).strip());
142 print ("set_property ( %s , %s / %s)\n", k, this.node.get(k).strip(), val.strdup_contents());
145 ret.set_property(k, val);
150 // for now... - just try the builder style packing
154 if (this.node.items.size < 1) {
158 for (var i = 0; i < this.node.items.size; i++ ) {
160 var ch = this.mungeChild(this.node.items.get(i));
162 this.children.add(ch);
167 this.afterChildren();
176 public void afterChildren()
178 // things like GtkNotebook - we have to pack children after they have been created..
179 var cls = this.node.fqn().replace(".", "");
181 if (cls == "GtkNotebook") {
182 this.afterChildrenGtkNotebook();
189 public void afterChildrenGtkNotebook()
191 // we have a number of children..
192 // some are labels - this might need to be more complex...
193 // perhaps labels should be a special property labels[] of the notebook..
194 var labels = new Gee.ArrayList<NodeToGtk>();
195 var bodies = new Gee.ArrayList<NodeToGtk>();
196 for (var i = 0; i < this.children.size; i++) {
197 var cn = this.children.get(i).node.fqn().replace(".", "");
198 if (cn != "GtkLabel") {
199 bodies.add(this.children.get(i));
202 labels.add(this.children.get(i));
204 for (var i = 0; i < bodies.size; i++) {
205 ((global::Gtk.Notebook)this.wrapped_object).append_page(
206 (global::Gtk.Notebook) bodies.get(i).wrapped_object,
207 (labels.size > i - 1) ?
208 (global::Gtk.Notebook) labels.get(i).wrapped_object :
221 * called after the this.object has been created
222 * and it needs to be packed onto parent.
224 public void packParent()
226 var cls = this.node.fqn().replace(".", "");
228 var gtkbuilder = new global::Gtk.Builder();
229 var cls_gtype = gtkbuilder.get_type_from_name(cls);
231 if (this.parentObj == null) {
236 var parent = this.parentObj.wrapped_object;
240 if (parent == null) { // no parent.. can not pack.
243 // ------------- handle various special parents .. -----------
245 var par_type = this.parentObj.node.fqn().replace(".", "");
247 if (par_type == "GtkNotebook") {
248 // do not pack - it's done afterwards...
252 // ------------- handle various child types.. -----------
254 if (cls == "GtkMenu") {
259 if (cls == "GtkTreeStore") { // other stores?
260 // tree store is buildable??? ---
261 this.packTreeStore();
264 if (cls =="GtkTreeViewColumn") { // other stores?
265 //?? treeview column is actually buildable -- but we do not use the builder???
266 this.packTreeViewColumn();
269 if (cls_gtype.is_a(typeof(global::Gtk.CellRenderer))) { // other stores?
270 this.packCellRenderer();
276 // -- handle buildable add_child..
277 if ( cls_gtype.is_a(typeof(global::Gtk.Buildable))
279 parent.get_type().is_a(typeof(global::Gtk.Buildable))
282 ((global::Gtk.Buildable)parent).add_child(gtkbuilder,
283 this.wrapped_object, null);
292 public void packMenu()
296 var parent = this.parentObj.wrapped_object;
297 if (!parent.get_type().is_a(typeof(global::Gtk.Widget))) {
298 print("skip menu pack - parent is not a widget");
302 var p = (global::Gtk.Menu)this.wrapped_object;
303 ((global::Gtk.Widget)parent).button_press_event.connect((s, ev) => {
304 p.set_screen(Gdk.Screen.get_default());
306 p.popup(null, null, null, ev.button, ev.time);
311 public void packTreeStore()
313 var parent = this.parentObj.wrapped_object;
314 if (!parent.get_type().is_a(typeof(global::Gtk.TreeView))) {
315 print("skip treestore pack - parent is not a treeview");
318 ((global::Gtk.TreeView)parent).set_model((global::Gtk.TreeModel)this.wrapped_object);
321 public void packTreeViewColumn()
323 var parent = this.parentObj.wrapped_object;
324 if (!parent.get_type().is_a(typeof(global::Gtk.TreeView))) {
325 print("skip packGtkViewColumn pack - parent is not a treeview");
328 ((global::Gtk.TreeView)parent).append_column((global::Gtk.TreeViewColumn)this.wrapped_object);
329 // init contains the add_attribute for what to render...
334 public void packCellRenderer()
336 var parent = this.parentObj.wrapped_object;
337 if (!parent.get_type().is_a(typeof(global::Gtk.TreeViewColumn))) {
338 print("skip packGtkViewColumn pack - parent is not a treeview");
341 ((global::Gtk.TreeViewColumn)parent).pack_start((global::Gtk.CellRenderer)this.wrapped_object, false);
342 // init contains the add_attribute for what to render...
347 public void packContainerParams()
350 if (this.parentObj == null) {
353 // child must be a widget..
354 if (!this.wrapped_object.get_type().is_a(typeof(global::Gtk.Widget))) {
358 var parent_gir = Palete.Gir.factoryFqn(this.parentObj.node.fqn());
360 var parent = this.parentObj.wrapped_object;
362 if (parent_gir == null) {
366 // let's test just setting expand to false...
367 var cls_methods = parent_gir.methods;
368 if (cls_methods == null) {
372 if (!this.node.props.has_key("* pack") ||
373 this.node.props.get("* pack").length < 1) {
377 var ns = this.parentObj.node.fqn().split(".")[0];
379 var pack = this.node.props.get("* pack").split(",");
382 if (cls_methods.has_key(pack[0])) {
383 var mparams = cls_methods.get(pack[0]).paramset.params;
384 for (var i = 1; i < mparams.size; i++ ) {
385 if (i > (pack.length -1)) {
389 var k = mparams.get(i).name;
393 var type = mparams.get(i).type;
394 type = Palete.Gir.fqtypeLookup(type, ns);
396 var val = this.toValue(pack[i].strip(), type);
398 print("skip (failed to transform value %s type = %s from %s\n",
399 this.parentObj.node.fqn() + "." + k, type, pack[i].strip());
402 print ("pack:set_property ( %s , %s / %s)\n", k, pack[i].strip(), val.strdup_contents());
404 ((global::Gtk.Container)parent).child_set_property(
405 (global::Gtk.Widget)this.wrapped_object , k, val);
417 public GLib.Value? toValue(string val, string type) {
419 var gtkbuilder = new global::Gtk.Builder();
421 if (type == "utf8") {
422 var qret = GLib.Value(typeof(string));
423 qret.set_string(val);
427 var prop_gtype = gtkbuilder.get_type_from_name(type);
430 if (prop_gtype == GLib.Type.INVALID) {
436 var ret = GLib.Value(prop_gtype);
441 ret.set_boolean(val.down() == "false" ? false : true);
444 ret.set_uint(int.parse(val));
448 ret.set_int(int.parse(val));
452 ret.set_float(long.parse(val));
461 var sval = GLib.Value(typeof(string));
462 sval.set_string(val);
464 if (!sval.transform(ref ret)) {