5 public class JsRender.NodeToGtk : Object {
11 Gee.ArrayList<string> els;
12 //Gee.ArrayList<string> skip;
13 Gee.HashMap<string,string> ar_props;
14 public static int vcnt = 0;
16 public NodeToGtk( Node node)
19 this.els = new Gee.ArrayList<string>();
20 //this.skip = new Gee.ArrayList<string>();
21 this.ar_props = new Gee.HashMap<string,string>();
22 this.parentObj = null;
25 public Object? munge ( )
28 return this.mungeNode ();
32 public Object? mungeChild( Node cnode)
34 var x = new NodeToGtk(cnode);
39 public Object? mungeNode()
42 var parent = this.parentObj != null ? this.parentObj.wrapped_object : null;
43 var cls = this.node.fqn().replace(".", "");
44 var ns = this.node.fqn().split(".")[0];
45 var gtkbuilder = new global::Gtk.Builder();
47 var cls_gtype = gtkbuilder.get_type_from_name(cls);
48 print("Type: %s ?= %s\n", this.node.fqn(), cls_gtype.name());
50 if (cls_gtype == GLib.Type.INVALID) {
51 print("SKIP - gtype is invalid\n");
54 // if it's a window...
56 if (cls_gtype.is_a(typeof(global::Gtk.Window))) {
57 // what if it has none...
58 if (this.node.items.size < 1) {
61 return this.mungeChild(this.node.items.get(0));
64 var ret = Object.new(cls_gtype);
65 ret.ref(); //??? problematic?
66 this.wrapped_object = ret;
71 //case "GtkTreeStore": // top level.. - named and referenced
72 case "GtkListStore": // top level.. - named and referenced
73 //case "GtkTreeViewColumn": // part of liststore?!?!
74 //case "GtkMenu": // top level..
75 //case "GtkCellRendererText":
76 case "GtkSourceBuffer":
77 case "GtkClutterActor"://fixme..
78 case "GtkClutterEmbed"://fixme.. -- we can not nest embedded.. need to solve..
89 if (parent != null && parent.get_type().is_a(typeof(global::Gtk.Container))) {
90 this.packContainerParams();
93 var cls_gir =Palete.Gir.factoryFqn(this.node.fqn());
94 if (cls_gir == null) {
97 //var id = this.node.uid();
98 //var ret = @"$pad<object class=\"$cls\" id=\"$id\">\n";
100 var props = cls_gir.props;
103 var pviter = props.map_iterator();
104 while (pviter.next()) {
106 // print("Check: " +cls + "::(" + pviter.get_value().propertyof + ")" + pviter.get_key() + " " );
107 var k = pviter.get_key();
108 // skip items we have already handled..
109 if (!this.node.has(k)) {
112 // find out the type of the property...
113 var type = pviter.get_value().type;
114 type = Palete.Gir.fqtypeLookup(type, ns);
116 var val = this.toValue(this.node.get(k).strip(), type);
118 print("skip (failed to transform value %s type = %s from %s\n",
119 cls + "." + k, type, this.node.get(k).strip());
122 print ("set_property ( %s , %s / %s)\n", k, this.node.get(k).strip(), val.strdup_contents());
125 ret.set_property(k, val);
130 // for now... - just try the builder style packing
134 if (this.node.items.size < 1) {
138 for (var i = 0; i < this.node.items.size; i++ ) {
140 this.mungeChild(this.node.items.get(i));
151 * called after the this.object has been created
152 * and it needs to be packed onto parent.
154 public void packParent()
156 var cls = this.node.fqn().replace(".", "");
158 var gtkbuilder = new global::Gtk.Builder();
159 var cls_gtype = gtkbuilder.get_type_from_name(cls);
161 if (this.parentObj == null) {
166 var parent = this.parentObj.wrapped_object;
170 if (parent == null) { // no parent.. can not pack.
173 // ------------- handle various special parents .. -----------
175 var par_type = this.parentObj.node.fqn().replace(".", "");
177 if (par_type == "GtkNotebook") {
178 this.packIntoNotebook();
182 // ------------- handle various child types.. -----------
184 if (cls == "GtkMenu") {
189 if (cls == "GtkTreeStore") { // other stores?
190 // tree store is buildable??? ---
191 this.packTreeStore();
194 if (cls =="GtkTreeViewColumn") { // other stores?
195 //?? treeview column is actually buildable -- but we do not use the builder???
196 this.packTreeViewColumn();
199 if (cls_gtype.is_a(typeof(global::Gtk.CellRenderer))) { // other stores?
200 this.packCellRenderer();
206 // -- handle buildable add_child..
207 if ( cls_gtype.is_a(typeof(global::Gtk.Buildable))
209 parent.get_type().is_a(typeof(global::Gtk.Buildable))
212 ((global::Gtk.Buildable)parent).add_child(gtkbuilder,
213 this.wrapped_object, null);
222 public void packMenu()
226 var parent = this.parentObj.wrapped_object;
227 if (!parent.get_type().is_a(typeof(global::Gtk.Widget))) {
228 print("skip menu pack - parent is not a widget");
232 var p = (global::Gtk.Menu)this.wrapped_object;
233 ((global::Gtk.Widget)parent).button_press_event.connect((s, ev) => {
234 p.set_screen(Gdk.Screen.get_default());
236 p.popup(null, null, null, ev.button, ev.time);
241 public void packTreeStore()
243 var parent = this.parentObj.wrapped_object;
244 if (!parent.get_type().is_a(typeof(global::Gtk.TreeView))) {
245 print("skip treestore pack - parent is not a treeview");
248 ((global::Gtk.TreeView)parent).set_model((global::Gtk.TreeModel)this.wrapped_object);
251 public void packTreeViewColumn()
253 var parent = this.parentObj.wrapped_object;
254 if (!parent.get_type().is_a(typeof(global::Gtk.TreeView))) {
255 print("skip packGtkViewColumn pack - parent is not a treeview");
258 ((global::Gtk.TreeView)parent).append_column((global::Gtk.TreeViewColumn)this.wrapped_object);
259 // init contains the add_attribute for what to render...
264 public void packCellRenderer()
266 var parent = this.parentObj.wrapped_object;
267 if (!parent.get_type().is_a(typeof(global::Gtk.TreeViewColumn))) {
268 print("skip packGtkViewColumn pack - parent is not a treeview");
271 ((global::Gtk.TreeViewColumn)parent).pack_start((global::Gtk.CellRenderer)this.wrapped_object, false);
272 // init contains the add_attribute for what to render...
277 public void packContainerParams()
280 if (this.parentObj == null) {
283 // child must be a widget..
284 if (!this.wrapped_object.get_type().is_a(typeof(global::Gtk.Widget))) {
288 var parent_gir = Palete.Gir.factoryFqn(this.parentObj.node.fqn());
290 var parent = this.parentObj.wrapped_object;
292 if (parent_gir == null) {
296 // let's test just setting expand to false...
297 var cls_methods = parent_gir.methods;
298 if (cls_methods == null) {
302 if (!this.node.props.has_key("* pack")) {
306 var ns = this.parentObj.node.fqn().split(".")[0];
308 var pack = this.node.props.get("* pack").split(",");
311 if (cls_methods.has_key(pack[0])) {
312 var mparams = cls_methods.get(pack[0]).paramset.params;
313 for (var i = 1; i < mparams.size; i++ ) {
314 if (i > (pack.length -1)) {
318 var k = mparams.get(i).name;
322 var type = mparams.get(i).type;
323 type = Palete.Gir.fqtypeLookup(type, ns);
325 var val = this.toValue(pack[i].strip(), type);
327 print("skip (failed to transform value %s type = %s from %s\n",
328 this.parentObj.node.fqn() + "." + k, type, pack[i].strip());
331 print ("pack:set_property ( %s , %s / %s)\n", k, pack[i].strip(), val.strdup_contents());
333 ((global::Gtk.Container)parent).child_set_property(
334 (global::Gtk.Widget)this.wrapped_object , k, val);
346 public GLib.Value? toValue(string val, string type) {
348 var gtkbuilder = new global::Gtk.Builder();
350 if (type == "utf8") {
351 var qret = GLib.Value(typeof(string));
352 qret.set_string(val);
356 var prop_gtype = gtkbuilder.get_type_from_name(type);
359 if (prop_gtype == GLib.Type.INVALID) {
365 var ret = GLib.Value(prop_gtype);
370 ret.set_boolean(val.down() == "false" ? false : true);
373 ret.set_uint(int.parse(val));
377 ret.set_int(int.parse(val));
381 ret.set_float(long.parse(val));
390 var sval = GLib.Value(typeof(string));
391 sval.set_string(val);
393 if (!sval.transform(ref ret)) {