5 public class JsRender.NodeToGtk : Object {
11 Gee.ArrayList<NodeToGtk> children;
13 Gee.ArrayList<string> els;
14 //Gee.ArrayList<string> skip;
15 Gee.HashMap<string,string> ar_props;
16 public static int vcnt = 0;
18 public NodeToGtk( Node node)
21 this.els = new Gee.ArrayList<string>();
22 this.children = new Gee.ArrayList<NodeToGtk>();
23 //this.skip = new Gee.ArrayList<string>();
24 this.ar_props = new Gee.HashMap<string,string>();
25 this.parentObj = null;
28 public Object? munge ( )
30 var ret = this.mungeNode();
35 return ret.wrapped_object;
38 public NodeToGtk? mungeChild( Node cnode)
40 var x = new NodeToGtk(cnode);
46 public NodeToGtk? mungeNode()
49 var parent = this.parentObj != null ? this.parentObj.wrapped_object : null;
50 var cls = this.node.fqn().replace(".", "");
51 var ns = this.node.fqn().split(".")[0];
52 var gtkbuilder = new global::Gtk.Builder();
54 var cls_gtype = gtkbuilder.get_type_from_name(cls);
55 print("Type: %s ?= %s\n", this.node.fqn(), cls_gtype.name());
57 if (cls_gtype == GLib.Type.INVALID) {
58 print("SKIP - gtype is invalid\n");
61 // if it's a window...
63 if (cls_gtype.is_a(typeof(global::Gtk.Window))) {
64 // what if it has none...
65 if (this.node.items.size < 1) {
68 return this.mungeChild(this.node.items.get(0));
71 var ret = Object.new(cls_gtype);
72 ret.ref(); //??? problematic?
73 this.wrapped_object = ret;
78 //case "GtkTreeStore": // top level.. - named and referenced
79 case "GtkListStore": // top level.. - named and referenced
80 //case "GtkTreeViewColumn": // part of liststore?!?!
81 //case "GtkMenu": // top level..
82 //case "GtkCellRendererText":
83 case "GtkSourceBuffer":
84 case "GtkClutterActor"://fixme..
85 case "GtkClutterEmbed"://fixme.. -- we can not nest embedded.. need to solve..
96 if (parent != null && parent.get_type().is_a(typeof(global::Gtk.Container))) {
97 this.packContainerParams();
100 var cls_gir =Palete.Gir.factoryFqn(this.node.fqn());
101 if (cls_gir == null) {
104 //var id = this.node.uid();
105 //var ret = @"$pad<object class=\"$cls\" id=\"$id\">\n";
107 var props = cls_gir.props;
110 var pviter = props.map_iterator();
111 while (pviter.next()) {
113 // print("Check: " +cls + "::(" + pviter.get_value().propertyof + ")" + pviter.get_key() + " " );
114 var k = pviter.get_key();
115 // skip items we have already handled..
116 if (!this.node.has(k)) {
119 // find out the type of the property...
120 var type = pviter.get_value().type;
121 type = Palete.Gir.fqtypeLookup(type, ns);
123 var val = this.toValue(this.node.get(k).strip(), type);
125 print("skip (failed to transform value %s type = %s from %s\n",
126 cls + "." + k, type, this.node.get(k).strip());
129 print ("set_property ( %s , %s / %s)\n", k, this.node.get(k).strip(), val.strdup_contents());
132 ret.set_property(k, val);
137 // for now... - just try the builder style packing
141 if (this.node.items.size < 1) {
145 for (var i = 0; i < this.node.items.size; i++ ) {
147 var ch = this.mungeChild(this.node.items.get(i));
149 this.children.add(ch);
154 this.afterChildren();
163 public void afterChildren()
165 // things like GtkNotebook - we have to pack children after they have been created..
166 var cls = this.node.fqn().replace(".", "");
168 if (cls == "GtkNotebook") {
169 this.afterChildrenGtkNotebook();
176 public void afterChildrenGtkNotebook()
178 // we have a number of children..
179 // some are labels - this might need to be more complex...
180 // perhaps labels should be a special property labels[] of the notebook..
181 var labels = new Gee.ArrayList<NodeToGtk>();
182 var bodies = new Gee.ArrayList<NodeToGtk>();
183 for (var i = 0; i < this.children.size; i++) {
184 var cn = this.children.get(i).node.fqn().replace(".", "");
185 if (cn != "GtkLabel") {
186 bodies.add(this.children.get(i));
189 labels.add(this.children.get(i));
191 for (var i = 0; i < bodies.size; i++) {
192 ((global::Gtk.Notebook)this.wrapped_object).append_page(
210 * called after the this.object has been created
211 * and it needs to be packed onto parent.
213 public void packParent()
215 var cls = this.node.fqn().replace(".", "");
217 var gtkbuilder = new global::Gtk.Builder();
218 var cls_gtype = gtkbuilder.get_type_from_name(cls);
220 if (this.parentObj == null) {
225 var parent = this.parentObj.wrapped_object;
229 if (parent == null) { // no parent.. can not pack.
232 // ------------- handle various special parents .. -----------
234 var par_type = this.parentObj.node.fqn().replace(".", "");
236 if (par_type == "GtkNotebook") {
237 // do not pack - it's done afterwards...
241 // ------------- handle various child types.. -----------
243 if (cls == "GtkMenu") {
248 if (cls == "GtkTreeStore") { // other stores?
249 // tree store is buildable??? ---
250 this.packTreeStore();
253 if (cls =="GtkTreeViewColumn") { // other stores?
254 //?? treeview column is actually buildable -- but we do not use the builder???
255 this.packTreeViewColumn();
258 if (cls_gtype.is_a(typeof(global::Gtk.CellRenderer))) { // other stores?
259 this.packCellRenderer();
265 // -- handle buildable add_child..
266 if ( cls_gtype.is_a(typeof(global::Gtk.Buildable))
268 parent.get_type().is_a(typeof(global::Gtk.Buildable))
271 ((global::Gtk.Buildable)parent).add_child(gtkbuilder,
272 this.wrapped_object, null);
281 public void packMenu()
285 var parent = this.parentObj.wrapped_object;
286 if (!parent.get_type().is_a(typeof(global::Gtk.Widget))) {
287 print("skip menu pack - parent is not a widget");
291 var p = (global::Gtk.Menu)this.wrapped_object;
292 ((global::Gtk.Widget)parent).button_press_event.connect((s, ev) => {
293 p.set_screen(Gdk.Screen.get_default());
295 p.popup(null, null, null, ev.button, ev.time);
300 public void packTreeStore()
302 var parent = this.parentObj.wrapped_object;
303 if (!parent.get_type().is_a(typeof(global::Gtk.TreeView))) {
304 print("skip treestore pack - parent is not a treeview");
307 ((global::Gtk.TreeView)parent).set_model((global::Gtk.TreeModel)this.wrapped_object);
310 public void packTreeViewColumn()
312 var parent = this.parentObj.wrapped_object;
313 if (!parent.get_type().is_a(typeof(global::Gtk.TreeView))) {
314 print("skip packGtkViewColumn pack - parent is not a treeview");
317 ((global::Gtk.TreeView)parent).append_column((global::Gtk.TreeViewColumn)this.wrapped_object);
318 // init contains the add_attribute for what to render...
323 public void packCellRenderer()
325 var parent = this.parentObj.wrapped_object;
326 if (!parent.get_type().is_a(typeof(global::Gtk.TreeViewColumn))) {
327 print("skip packGtkViewColumn pack - parent is not a treeview");
330 ((global::Gtk.TreeViewColumn)parent).pack_start((global::Gtk.CellRenderer)this.wrapped_object, false);
331 // init contains the add_attribute for what to render...
336 public void packContainerParams()
339 if (this.parentObj == null) {
342 // child must be a widget..
343 if (!this.wrapped_object.get_type().is_a(typeof(global::Gtk.Widget))) {
347 var parent_gir = Palete.Gir.factoryFqn(this.parentObj.node.fqn());
349 var parent = this.parentObj.wrapped_object;
351 if (parent_gir == null) {
355 // let's test just setting expand to false...
356 var cls_methods = parent_gir.methods;
357 if (cls_methods == null) {
361 if (!this.node.props.has_key("* pack")) {
365 var ns = this.parentObj.node.fqn().split(".")[0];
367 var pack = this.node.props.get("* pack").split(",");
370 if (cls_methods.has_key(pack[0])) {
371 var mparams = cls_methods.get(pack[0]).paramset.params;
372 for (var i = 1; i < mparams.size; i++ ) {
373 if (i > (pack.length -1)) {
377 var k = mparams.get(i).name;
381 var type = mparams.get(i).type;
382 type = Palete.Gir.fqtypeLookup(type, ns);
384 var val = this.toValue(pack[i].strip(), type);
386 print("skip (failed to transform value %s type = %s from %s\n",
387 this.parentObj.node.fqn() + "." + k, type, pack[i].strip());
390 print ("pack:set_property ( %s , %s / %s)\n", k, pack[i].strip(), val.strdup_contents());
392 ((global::Gtk.Container)parent).child_set_property(
393 (global::Gtk.Widget)this.wrapped_object , k, val);
405 public GLib.Value? toValue(string val, string type) {
407 var gtkbuilder = new global::Gtk.Builder();
409 if (type == "utf8") {
410 var qret = GLib.Value(typeof(string));
411 qret.set_string(val);
415 var prop_gtype = gtkbuilder.get_type_from_name(type);
418 if (prop_gtype == GLib.Type.INVALID) {
424 var ret = GLib.Value(prop_gtype);
429 ret.set_boolean(val.down() == "false" ? false : true);
432 ret.set_uint(int.parse(val));
436 ret.set_int(int.parse(val));
440 ret.set_float(long.parse(val));
449 var sval = GLib.Value(typeof(string));
450 sval.set_string(val);
452 if (!sval.transform(ref ret)) {