2 this is the code to write the 'classic' node to vala output
6 * Code to convert node tree to Vala...
8 * usage : x = (new JsRender.NodeToVala(node)).munge();
12 * pack - can we come up with a replacement?
13 - parent.child == child_widget -- actually uses getters and effectively does 'add'?
17 * args -- vala constructor args (should really only be used at top level - we did use it for clutter originally(
18 * ctor -- different ctor argument
27 public class JsRender.NodeToValaWrapped : NodeToVala {
31 public NodeToValaWrapped( JsRender file, Node node, int depth, NodeToVala? parent)
33 base (file, node, depth, parent);
34 this.this_el = "this.el.";
38 * Main entry point to convert a file into a string..
40 public static string mungeFile(JsRender file)
42 if (file.tree == null) {
46 var n = new NodeToValaWrapped(file, file.tree, 0, null);
47 n.toValaName(file.tree);
49 GLib.debug("top cls %s / xlcs %s\n ",file.tree.xvala_cls,file.tree.xvala_cls);
55 public override string munge ( )
57 //return this.mungeToString(this.node);
58 if (this.node.as_source_version > 0 &&
59 this.node.as_source_version == this.node.updated_count
60 && this.node.as_source != ""
62 return this.node.as_source;
66 this.namespaceHeader();
70 this.addTopProperties();
72 this.addPlusProperties(); // (this is child properties whos 'id' starts with '+' ??? not sure..
75 this.addWrappedCtor(); // var this.el = new XXXXX()
78 this.addWrappedProperties();
80 //this.addAutoShow(); // not needed gtk4 autoshow menuitems
85 this.addUserMethods();
87 this.namespaceFooter();
90 this.node.as_source_version = this.node.updated_count;
91 this.node.as_source == this.ret;
96 public override string mungeChild( Node cnode)
98 var x = new NodeToValaWrapped(this.file, cnode, this.depth+1, this);
102 protected override void classHeader()
104 var top = this.top as NodeToVala;
110 // class xxx { WrappedGtk el; }
111 this.node.line_start = this.cur_line;
113 this.top.node.setNodeLine(this.cur_line, this.node);
115 this.addLine(this.inpad + "public class " + this.xcls + " : Object");
116 this.addLine(this.inpad + "{");
119 this.addLine(this.pad + "public " + this.cls + " el;");
121 this.addLine(this.pad + "private " + top.xcls + " _this;");
128 public void globalVars()
130 if (this.depth > 0) {
133 // Global Vars..??? when did this get removed..?
134 //this.ret += this.inpad + "public static " + this.xcls + " " + this.node.xvala_id+ ";\n\n";
136 this.addLine(this.inpad + "static " + this.xcls + " _" + this.node.xvala_id+ ";");
140 protected void addSingleton()
145 this.addLine(pad + "public static " + xcls + " singleton()");
146 this.addLine(this.pad + "{");
147 this.addLine(this.ipad + "if (_" + this.node.xvala_id + " == null) {");
148 this.addLine(this.ipad + " _" + this.node.xvala_id + "= new "+ this.xcls + "();"); // what about args?
149 this.addLine(this.ipad + "}");
150 this.addLine(this.ipad + "return _" + this.node.xvala_id +";");
151 this.addLine(this.pad + "}");
154 * add the constructor definition..
156 protected override void addValaCtor()
166 this.addLine(this.pad + "// ctor");
168 if (this.node.has("* args")) {
169 // not sure what this is supposed to be ding..
171 cargs_str = this.node.get("* args");
172 //var ar = this.node.get("* args");.split(",");
173 //for (var ari =0; ari < ar.length; ari++) {
174 // cargs += (ar[ari].trim().split(" ").pop();
178 if (this.depth < 1) {
180 // top level - does not pass the top level element..
181 this.addLine(this.pad + "public " + this.xcls + "(" + cargs_str +")");
182 this.addLine(this.pad + "{");
184 if (cargs_str.length > 0) {
185 cargs_str = ", " + cargs_str;
187 var top = this.top as NodeToVala;
188 var tcls = top == null ? "???" : top.xcls;
189 // for sub classes = we passs the top level as _owner
190 this.addLine(this.pad + "public " + this.xcls + "(" + tcls + " _owner " + cargs_str + ")");
191 this.addLine(this.pad + "{");
197 * Initialize this.el to point to the wrapped element.
202 void addWrappedCtor()
205 // this may need to look up properties to fill in the arguments..
206 // introspection does not workk..... - as things like gtkmessagedialog
208 if (cls == 'Gtk.Table') {
210 var methods = this.palete.getPropertiesFor(cls, 'methods');
212 print(JSON.stringify(this.palete.proplist[cls], null,4));
217 // ctor can still override.
218 if (this.node.has("* ctor")) {
219 this.node.setLine(this.cur_line, "p", "* ctor");
220 this.addLine(this.ipad + "this.el = " + this.node.get("* ctor")+ ";");
224 this.node.setLine(this.cur_line, "p", "* xtype");;
226 // is the wrapped element a struct?
228 var ncls = Palete.Gir.factoryFqn((Project.Gtk) this.file.project, this.node.fqn());
229 if (ncls != null && ncls.nodetype == "Struct") {
230 // we can use regular setters to apply the values.
231 this.addLine(this.ipad + "this.el = " + this.node.fqn() + "();");
239 switch(this.node.fqn()) {
241 // FIXME -- these are all GTK3 - can be removed when I get rid of them..
243 var is_entry = this.node.has("has_entry") && this.node.get_prop("has_entry").val.down() == "true";
245 break; // regular ctor.
247 this.ignoreWrapped("has_entry");
248 ctor = ".with_entry";
252 case "Gtk.ListStore":
253 case "Gtk.TreeStore":
255 // not sure if this works.. otherwise we have to go with varargs and count + vals...
256 if (this.node.has("* types")) {
257 args_str = this.node.get_prop("* types").val;
259 if (this.node.has("n_columns") && this.node.has("columns")) { // old value?
260 args_str = " { " + this.node.get_prop("columns").val + " } ";
261 this.ignoreWrapped("columns");
262 this.ignoreWrapped("n_columns");
265 this.addLine(this.ipad + "this.el = new " + this.node.fqn() + ".newv( " + args_str + " );");
269 case "Gtk.LinkButton": // args filled with values.
270 if (this.node.has("label")) {
271 ctor = ".with_label";
278 var default_ctor = Palete.Gir.factoryFqn((Project.Gtk) this.file.project, this.node.fqn() + ctor);
281 // use the default ctor - with arguments (from properties)
283 if (default_ctor != null && default_ctor.paramset != null && default_ctor.paramset.params.size > 0) {
285 foreach(var param in default_ctor.paramset.params) {
288 GLib.debug("building CTOR ARGS: %s, %s", n, param.is_varargs ? "VARARGS": "");
289 if (n == "___") { // for some reason our varargs are converted to '___' ...
293 if (this.node.has(n)) { // node does not have a value
295 this.ignoreWrapped(n);
298 var v = this.node.get(n);
300 if (param.type == "string") {
301 v = "\"" + v.escape("") + "\"";
303 if (v == "TRUE" || v == "FALSE") {
311 var propnode = this.node.findProp(n);
312 if (propnode != null) {
315 var pname = this.addPropSet(propnode, propnode.has("id") ? propnode.get_prop("id").val : "");
316 args += (pname + ".el") ;
317 if (!propnode.has("id")) {
318 this.addLine(this.ipad + pname +".ref();");
323 this.ignoreWrapped(n);
332 if (param.type.contains("int")) {
336 if (param.type.contains("float")) {
340 if (param.type.contains("bool")) {
341 args += "true"; // always default to true?
344 // any other types???
354 this.node.setLine(this.cur_line, "p", "* xtype");
355 this.addLine(this.ipad + "this.el = new " + this.node.fqn() + "( "+ string.joinv(", ",args) + " );") ;
359 // default ctor with no params..
360 if (default_ctor != null && ctor != ".new" ) {
361 this.node.setLine(this.cur_line, "p", "* xtype");
363 this.addLine(this.ipad + "this.el = new " + this.node.fqn() + ctor + "( );") ;
368 this.addLine(this.ipad + "this.el = new " + this.node.fqn() + "(" + args_str + ");");