5 public class JsRender.NodeToGtk : Object {
11 Gee.ArrayList<Object> 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.skip = new Gee.ArrayList<string>();
23 this.ar_props = new Gee.HashMap<string,string>();
24 this.parentObj = null;
27 public Object? munge ( )
30 return this.mungeNode ();
34 public Object? mungeChild( Node cnode)
36 var x = new NodeToGtk(cnode);
41 public Object? mungeNode()
44 var parent = this.parentObj != null ? this.parentObj.wrapped_object : null;
45 var cls = this.node.fqn().replace(".", "");
46 var ns = this.node.fqn().split(".")[0];
47 var gtkbuilder = new global::Gtk.Builder();
49 var cls_gtype = gtkbuilder.get_type_from_name(cls);
50 print("Type: %s ?= %s\n", this.node.fqn(), cls_gtype.name());
52 if (cls_gtype == GLib.Type.INVALID) {
53 print("SKIP - gtype is invalid\n");
56 // if it's a window...
58 if (cls_gtype.is_a(typeof(global::Gtk.Window))) {
59 // what if it has none...
60 if (this.node.items.size < 1) {
63 return this.mungeChild(this.node.items.get(0));
66 var ret = Object.new(cls_gtype);
67 ret.ref(); //??? problematic?
68 this.wrapped_object = ret;
73 //case "GtkTreeStore": // top level.. - named and referenced
74 case "GtkListStore": // top level.. - named and referenced
75 //case "GtkTreeViewColumn": // part of liststore?!?!
76 //case "GtkMenu": // top level..
77 //case "GtkCellRendererText":
78 case "GtkSourceBuffer":
79 case "GtkClutterActor"://fixme..
80 case "GtkClutterEmbed"://fixme.. -- we can not nest embedded.. need to solve..
91 if (parent != null && parent.get_type().is_a(typeof(global::Gtk.Container))) {
92 this.packContainerParams();
95 var cls_gir =Palete.Gir.factoryFqn(this.node.fqn());
96 if (cls_gir == null) {
99 //var id = this.node.uid();
100 //var ret = @"$pad<object class=\"$cls\" id=\"$id\">\n";
102 var props = cls_gir.props;
105 var pviter = props.map_iterator();
106 while (pviter.next()) {
108 // print("Check: " +cls + "::(" + pviter.get_value().propertyof + ")" + pviter.get_key() + " " );
109 var k = pviter.get_key();
110 // skip items we have already handled..
111 if (!this.node.has(k)) {
114 // find out the type of the property...
115 var type = pviter.get_value().type;
116 type = Palete.Gir.fqtypeLookup(type, ns);
118 var val = this.toValue(this.node.get(k).strip(), type);
120 print("skip (failed to transform value %s type = %s from %s\n",
121 cls + "." + k, type, this.node.get(k).strip());
124 print ("set_property ( %s , %s / %s)\n", k, this.node.get(k).strip(), val.strdup_contents());
127 ret.set_property(k, val);
132 // for now... - just try the builder style packing
136 if (this.node.items.size < 1) {
140 for (var i = 0; i < this.node.items.size; i++ ) {
142 this.mungeChild(this.node.items.get(i));
146 this.afterChildren();
155 * called after the this.object has been created
156 * and it needs to be packed onto parent.
158 public void packParent()
160 var cls = this.node.fqn().replace(".", "");
162 var gtkbuilder = new global::Gtk.Builder();
163 var cls_gtype = gtkbuilder.get_type_from_name(cls);
165 if (this.parentObj == null) {
170 var parent = this.parentObj.wrapped_object;
174 if (parent == null) { // no parent.. can not pack.
177 // ------------- handle various special parents .. -----------
179 var par_type = this.parentObj.node.fqn().replace(".", "");
181 if (par_type == "GtkNotebook") {
182 // do not pack - it's done afterwards...
186 // ------------- handle various child types.. -----------
188 if (cls == "GtkMenu") {
193 if (cls == "GtkTreeStore") { // other stores?
194 // tree store is buildable??? ---
195 this.packTreeStore();
198 if (cls =="GtkTreeViewColumn") { // other stores?
199 //?? treeview column is actually buildable -- but we do not use the builder???
200 this.packTreeViewColumn();
203 if (cls_gtype.is_a(typeof(global::Gtk.CellRenderer))) { // other stores?
204 this.packCellRenderer();
210 // -- handle buildable add_child..
211 if ( cls_gtype.is_a(typeof(global::Gtk.Buildable))
213 parent.get_type().is_a(typeof(global::Gtk.Buildable))
216 ((global::Gtk.Buildable)parent).add_child(gtkbuilder,
217 this.wrapped_object, null);
226 public void packMenu()
230 var parent = this.parentObj.wrapped_object;
231 if (!parent.get_type().is_a(typeof(global::Gtk.Widget))) {
232 print("skip menu pack - parent is not a widget");
236 var p = (global::Gtk.Menu)this.wrapped_object;
237 ((global::Gtk.Widget)parent).button_press_event.connect((s, ev) => {
238 p.set_screen(Gdk.Screen.get_default());
240 p.popup(null, null, null, ev.button, ev.time);
245 public void packTreeStore()
247 var parent = this.parentObj.wrapped_object;
248 if (!parent.get_type().is_a(typeof(global::Gtk.TreeView))) {
249 print("skip treestore pack - parent is not a treeview");
252 ((global::Gtk.TreeView)parent).set_model((global::Gtk.TreeModel)this.wrapped_object);
255 public void packTreeViewColumn()
257 var parent = this.parentObj.wrapped_object;
258 if (!parent.get_type().is_a(typeof(global::Gtk.TreeView))) {
259 print("skip packGtkViewColumn pack - parent is not a treeview");
262 ((global::Gtk.TreeView)parent).append_column((global::Gtk.TreeViewColumn)this.wrapped_object);
263 // init contains the add_attribute for what to render...
268 public void packCellRenderer()
270 var parent = this.parentObj.wrapped_object;
271 if (!parent.get_type().is_a(typeof(global::Gtk.TreeViewColumn))) {
272 print("skip packGtkViewColumn pack - parent is not a treeview");
275 ((global::Gtk.TreeViewColumn)parent).pack_start((global::Gtk.CellRenderer)this.wrapped_object, false);
276 // init contains the add_attribute for what to render...
281 public void packContainerParams()
284 if (this.parentObj == null) {
287 // child must be a widget..
288 if (!this.wrapped_object.get_type().is_a(typeof(global::Gtk.Widget))) {
292 var parent_gir = Palete.Gir.factoryFqn(this.parentObj.node.fqn());
294 var parent = this.parentObj.wrapped_object;
296 if (parent_gir == null) {
300 // let's test just setting expand to false...
301 var cls_methods = parent_gir.methods;
302 if (cls_methods == null) {
306 if (!this.node.props.has_key("* pack")) {
310 var ns = this.parentObj.node.fqn().split(".")[0];
312 var pack = this.node.props.get("* pack").split(",");
315 if (cls_methods.has_key(pack[0])) {
316 var mparams = cls_methods.get(pack[0]).paramset.params;
317 for (var i = 1; i < mparams.size; i++ ) {
318 if (i > (pack.length -1)) {
322 var k = mparams.get(i).name;
326 var type = mparams.get(i).type;
327 type = Palete.Gir.fqtypeLookup(type, ns);
329 var val = this.toValue(pack[i].strip(), type);
331 print("skip (failed to transform value %s type = %s from %s\n",
332 this.parentObj.node.fqn() + "." + k, type, pack[i].strip());
335 print ("pack:set_property ( %s , %s / %s)\n", k, pack[i].strip(), val.strdup_contents());
337 ((global::Gtk.Container)parent).child_set_property(
338 (global::Gtk.Widget)this.wrapped_object , k, val);
350 public GLib.Value? toValue(string val, string type) {
352 var gtkbuilder = new global::Gtk.Builder();
354 if (type == "utf8") {
355 var qret = GLib.Value(typeof(string));
356 qret.set_string(val);
360 var prop_gtype = gtkbuilder.get_type_from_name(type);
363 if (prop_gtype == GLib.Type.INVALID) {
369 var ret = GLib.Value(prop_gtype);
374 ret.set_boolean(val.down() == "false" ? false : true);
377 ret.set_uint(int.parse(val));
381 ret.set_int(int.parse(val));
385 ret.set_float(long.parse(val));
394 var sval = GLib.Value(typeof(string));
395 sval.set_string(val);
397 if (!sval.transform(ref ret)) {