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));
144 this.afterChildren();
153 * called after the this.object has been created
154 * and it needs to be packed onto parent.
156 public void packParent()
158 var cls = this.node.fqn().replace(".", "");
160 var gtkbuilder = new global::Gtk.Builder();
161 var cls_gtype = gtkbuilder.get_type_from_name(cls);
163 if (this.parentObj == null) {
168 var parent = this.parentObj.wrapped_object;
172 if (parent == null) { // no parent.. can not pack.
175 // ------------- handle various special parents .. -----------
177 var par_type = this.parentObj.node.fqn().replace(".", "");
179 if (par_type == "GtkNotebook") {
180 // do not pack - it's done afterwards...
184 // ------------- handle various child types.. -----------
186 if (cls == "GtkMenu") {
191 if (cls == "GtkTreeStore") { // other stores?
192 // tree store is buildable??? ---
193 this.packTreeStore();
196 if (cls =="GtkTreeViewColumn") { // other stores?
197 //?? treeview column is actually buildable -- but we do not use the builder???
198 this.packTreeViewColumn();
201 if (cls_gtype.is_a(typeof(global::Gtk.CellRenderer))) { // other stores?
202 this.packCellRenderer();
208 // -- handle buildable add_child..
209 if ( cls_gtype.is_a(typeof(global::Gtk.Buildable))
211 parent.get_type().is_a(typeof(global::Gtk.Buildable))
214 ((global::Gtk.Buildable)parent).add_child(gtkbuilder,
215 this.wrapped_object, null);
224 public void packMenu()
228 var parent = this.parentObj.wrapped_object;
229 if (!parent.get_type().is_a(typeof(global::Gtk.Widget))) {
230 print("skip menu pack - parent is not a widget");
234 var p = (global::Gtk.Menu)this.wrapped_object;
235 ((global::Gtk.Widget)parent).button_press_event.connect((s, ev) => {
236 p.set_screen(Gdk.Screen.get_default());
238 p.popup(null, null, null, ev.button, ev.time);
243 public void packTreeStore()
245 var parent = this.parentObj.wrapped_object;
246 if (!parent.get_type().is_a(typeof(global::Gtk.TreeView))) {
247 print("skip treestore pack - parent is not a treeview");
250 ((global::Gtk.TreeView)parent).set_model((global::Gtk.TreeModel)this.wrapped_object);
253 public void packTreeViewColumn()
255 var parent = this.parentObj.wrapped_object;
256 if (!parent.get_type().is_a(typeof(global::Gtk.TreeView))) {
257 print("skip packGtkViewColumn pack - parent is not a treeview");
260 ((global::Gtk.TreeView)parent).append_column((global::Gtk.TreeViewColumn)this.wrapped_object);
261 // init contains the add_attribute for what to render...
266 public void packCellRenderer()
268 var parent = this.parentObj.wrapped_object;
269 if (!parent.get_type().is_a(typeof(global::Gtk.TreeViewColumn))) {
270 print("skip packGtkViewColumn pack - parent is not a treeview");
273 ((global::Gtk.TreeViewColumn)parent).pack_start((global::Gtk.CellRenderer)this.wrapped_object, false);
274 // init contains the add_attribute for what to render...
279 public void packContainerParams()
282 if (this.parentObj == null) {
285 // child must be a widget..
286 if (!this.wrapped_object.get_type().is_a(typeof(global::Gtk.Widget))) {
290 var parent_gir = Palete.Gir.factoryFqn(this.parentObj.node.fqn());
292 var parent = this.parentObj.wrapped_object;
294 if (parent_gir == null) {
298 // let's test just setting expand to false...
299 var cls_methods = parent_gir.methods;
300 if (cls_methods == null) {
304 if (!this.node.props.has_key("* pack")) {
308 var ns = this.parentObj.node.fqn().split(".")[0];
310 var pack = this.node.props.get("* pack").split(",");
313 if (cls_methods.has_key(pack[0])) {
314 var mparams = cls_methods.get(pack[0]).paramset.params;
315 for (var i = 1; i < mparams.size; i++ ) {
316 if (i > (pack.length -1)) {
320 var k = mparams.get(i).name;
324 var type = mparams.get(i).type;
325 type = Palete.Gir.fqtypeLookup(type, ns);
327 var val = this.toValue(pack[i].strip(), type);
329 print("skip (failed to transform value %s type = %s from %s\n",
330 this.parentObj.node.fqn() + "." + k, type, pack[i].strip());
333 print ("pack:set_property ( %s , %s / %s)\n", k, pack[i].strip(), val.strdup_contents());
335 ((global::Gtk.Container)parent).child_set_property(
336 (global::Gtk.Widget)this.wrapped_object , k, val);
348 public GLib.Value? toValue(string val, string type) {
350 var gtkbuilder = new global::Gtk.Builder();
352 if (type == "utf8") {
353 var qret = GLib.Value(typeof(string));
354 qret.set_string(val);
358 var prop_gtype = gtkbuilder.get_type_from_name(type);
361 if (prop_gtype == GLib.Type.INVALID) {
367 var ret = GLib.Value(prop_gtype);
372 ret.set_boolean(val.down() == "false" ? false : true);
375 ret.set_uint(int.parse(val));
379 ret.set_int(int.parse(val));
383 ret.set_float(long.parse(val));
392 var sval = GLib.Value(typeof(string));
393 sval.set_string(val);
395 if (!sval.transform(ref ret)) {