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 ( )
31 return this.mungeNode().wrapped_object;
35 public NodeToGtk mungeChild( Node cnode)
37 var x = new NodeToGtk(cnode);
43 public NodeToGtk mungeNode()
46 var parent = this.parentObj != null ? this.parentObj.wrapped_object : null;
47 var cls = this.node.fqn().replace(".", "");
48 var ns = this.node.fqn().split(".")[0];
49 var gtkbuilder = new global::Gtk.Builder();
51 var cls_gtype = gtkbuilder.get_type_from_name(cls);
52 print("Type: %s ?= %s\n", this.node.fqn(), cls_gtype.name());
54 if (cls_gtype == GLib.Type.INVALID) {
55 print("SKIP - gtype is invalid\n");
58 // if it's a window...
60 if (cls_gtype.is_a(typeof(global::Gtk.Window))) {
61 // what if it has none...
62 if (this.node.items.size < 1) {
65 return this.mungeChild(this.node.items.get(0));
68 var ret = Object.new(cls_gtype);
69 ret.ref(); //??? problematic?
70 this.wrapped_object = ret;
75 //case "GtkTreeStore": // top level.. - named and referenced
76 case "GtkListStore": // top level.. - named and referenced
77 //case "GtkTreeViewColumn": // part of liststore?!?!
78 //case "GtkMenu": // top level..
79 //case "GtkCellRendererText":
80 case "GtkSourceBuffer":
81 case "GtkClutterActor"://fixme..
82 case "GtkClutterEmbed"://fixme.. -- we can not nest embedded.. need to solve..
93 if (parent != null && parent.get_type().is_a(typeof(global::Gtk.Container))) {
94 this.packContainerParams();
97 var cls_gir =Palete.Gir.factoryFqn(this.node.fqn());
98 if (cls_gir == null) {
101 //var id = this.node.uid();
102 //var ret = @"$pad<object class=\"$cls\" id=\"$id\">\n";
104 var props = cls_gir.props;
107 var pviter = props.map_iterator();
108 while (pviter.next()) {
110 // print("Check: " +cls + "::(" + pviter.get_value().propertyof + ")" + pviter.get_key() + " " );
111 var k = pviter.get_key();
112 // skip items we have already handled..
113 if (!this.node.has(k)) {
116 // find out the type of the property...
117 var type = pviter.get_value().type;
118 type = Palete.Gir.fqtypeLookup(type, ns);
120 var val = this.toValue(this.node.get(k).strip(), type);
122 print("skip (failed to transform value %s type = %s from %s\n",
123 cls + "." + k, type, this.node.get(k).strip());
126 print ("set_property ( %s , %s / %s)\n", k, this.node.get(k).strip(), val.strdup_contents());
129 ret.set_property(k, val);
134 // for now... - just try the builder style packing
138 if (this.node.items.size < 1) {
142 for (var i = 0; i < this.node.items.size; i++ ) {
144 this.mungeChild(this.node.items.get(i));
148 this.afterChildren();
157 * called after the this.object has been created
158 * and it needs to be packed onto parent.
160 public void packParent()
162 var cls = this.node.fqn().replace(".", "");
164 var gtkbuilder = new global::Gtk.Builder();
165 var cls_gtype = gtkbuilder.get_type_from_name(cls);
167 if (this.parentObj == null) {
172 var parent = this.parentObj.wrapped_object;
176 if (parent == null) { // no parent.. can not pack.
179 // ------------- handle various special parents .. -----------
181 var par_type = this.parentObj.node.fqn().replace(".", "");
183 if (par_type == "GtkNotebook") {
184 // do not pack - it's done afterwards...
188 // ------------- handle various child types.. -----------
190 if (cls == "GtkMenu") {
195 if (cls == "GtkTreeStore") { // other stores?
196 // tree store is buildable??? ---
197 this.packTreeStore();
200 if (cls =="GtkTreeViewColumn") { // other stores?
201 //?? treeview column is actually buildable -- but we do not use the builder???
202 this.packTreeViewColumn();
205 if (cls_gtype.is_a(typeof(global::Gtk.CellRenderer))) { // other stores?
206 this.packCellRenderer();
212 // -- handle buildable add_child..
213 if ( cls_gtype.is_a(typeof(global::Gtk.Buildable))
215 parent.get_type().is_a(typeof(global::Gtk.Buildable))
218 ((global::Gtk.Buildable)parent).add_child(gtkbuilder,
219 this.wrapped_object, null);
228 public void packMenu()
232 var parent = this.parentObj.wrapped_object;
233 if (!parent.get_type().is_a(typeof(global::Gtk.Widget))) {
234 print("skip menu pack - parent is not a widget");
238 var p = (global::Gtk.Menu)this.wrapped_object;
239 ((global::Gtk.Widget)parent).button_press_event.connect((s, ev) => {
240 p.set_screen(Gdk.Screen.get_default());
242 p.popup(null, null, null, ev.button, ev.time);
247 public void packTreeStore()
249 var parent = this.parentObj.wrapped_object;
250 if (!parent.get_type().is_a(typeof(global::Gtk.TreeView))) {
251 print("skip treestore pack - parent is not a treeview");
254 ((global::Gtk.TreeView)parent).set_model((global::Gtk.TreeModel)this.wrapped_object);
257 public void packTreeViewColumn()
259 var parent = this.parentObj.wrapped_object;
260 if (!parent.get_type().is_a(typeof(global::Gtk.TreeView))) {
261 print("skip packGtkViewColumn pack - parent is not a treeview");
264 ((global::Gtk.TreeView)parent).append_column((global::Gtk.TreeViewColumn)this.wrapped_object);
265 // init contains the add_attribute for what to render...
270 public void packCellRenderer()
272 var parent = this.parentObj.wrapped_object;
273 if (!parent.get_type().is_a(typeof(global::Gtk.TreeViewColumn))) {
274 print("skip packGtkViewColumn pack - parent is not a treeview");
277 ((global::Gtk.TreeViewColumn)parent).pack_start((global::Gtk.CellRenderer)this.wrapped_object, false);
278 // init contains the add_attribute for what to render...
283 public void packContainerParams()
286 if (this.parentObj == null) {
289 // child must be a widget..
290 if (!this.wrapped_object.get_type().is_a(typeof(global::Gtk.Widget))) {
294 var parent_gir = Palete.Gir.factoryFqn(this.parentObj.node.fqn());
296 var parent = this.parentObj.wrapped_object;
298 if (parent_gir == null) {
302 // let's test just setting expand to false...
303 var cls_methods = parent_gir.methods;
304 if (cls_methods == null) {
308 if (!this.node.props.has_key("* pack")) {
312 var ns = this.parentObj.node.fqn().split(".")[0];
314 var pack = this.node.props.get("* pack").split(",");
317 if (cls_methods.has_key(pack[0])) {
318 var mparams = cls_methods.get(pack[0]).paramset.params;
319 for (var i = 1; i < mparams.size; i++ ) {
320 if (i > (pack.length -1)) {
324 var k = mparams.get(i).name;
328 var type = mparams.get(i).type;
329 type = Palete.Gir.fqtypeLookup(type, ns);
331 var val = this.toValue(pack[i].strip(), type);
333 print("skip (failed to transform value %s type = %s from %s\n",
334 this.parentObj.node.fqn() + "." + k, type, pack[i].strip());
337 print ("pack:set_property ( %s , %s / %s)\n", k, pack[i].strip(), val.strdup_contents());
339 ((global::Gtk.Container)parent).child_set_property(
340 (global::Gtk.Widget)this.wrapped_object , k, val);
352 public GLib.Value? toValue(string val, string type) {
354 var gtkbuilder = new global::Gtk.Builder();
356 if (type == "utf8") {
357 var qret = GLib.Value(typeof(string));
358 qret.set_string(val);
362 var prop_gtype = gtkbuilder.get_type_from_name(type);
365 if (prop_gtype == GLib.Type.INVALID) {
371 var ret = GLib.Value(prop_gtype);
376 ret.set_boolean(val.down() == "false" ? false : true);
379 ret.set_uint(int.parse(val));
383 ret.set_int(int.parse(val));
387 ret.set_float(long.parse(val));
396 var sval = GLib.Value(typeof(string));
397 sval.set_string(val);
399 if (!sval.transform(ref ret)) {