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 {
32 public NodeToValaWrapped( JsRender file, Node node, int depth, NodeToVala? parent)
34 base (file, node, depth, parent);
35 this.this_el = "this.el.";
39 * Main entry point to convert a file into a string..
41 public static string mungeFile(JsRender file)
43 if (file.tree == null) {
47 var n = new NodeToValaWrapped(file, file.tree, 0, null);
48 n.toValaName(file.tree);
50 GLib.debug("top cls %s / xlcs %s\n ",file.tree.xvala_cls,file.tree.xvala_cls);
56 public override string munge ( )
58 //return this.mungeToString(this.node);
62 this.namespaceHeader();
66 this.addTopProperties();
68 this.addPlusProperties(); // (this is child properties whos 'id' starts with '+' ??? not sure..
71 this.addWrappedCtor(); // var this.el = new XXXXX()
74 this.addWrappedProperties();
76 //this.addAutoShow(); // not needed gtk4 autoshow menuitems
81 this.addUserMethods();
83 this.namespaceFooter();
89 public override string mungeChild( Node cnode)
91 var x = new NodeToValaWrapped(this.file, cnode, this.depth+1, this);
95 protected override void classHeader()
97 var top = this.top as NodeToVala;
103 // class xxx { WrappedGtk el; }
104 this.node.line_start = this.cur_line;
106 this.top.node.setNodeLine(this.cur_line, this.node);
108 this.addLine(this.inpad + "public class " + this.xcls + " : Object");
109 this.addLine(this.inpad + "{");
112 this.addLine(this.pad + "public " + this.cls + " el;");
114 this.addLine(this.pad + "private " + top.xcls + " _this;");
121 public void globalVars()
123 if (this.depth > 0) {
126 // Global Vars..??? when did this get removed..?
127 //this.ret += this.inpad + "public static " + this.xcls + " " + this.node.xvala_id+ ";\n\n";
129 this.addLine(this.inpad + "static " + this.xcls + " _" + this.node.xvala_id+ ";");
133 protected void addSingleton()
138 this.addLine(pad + "public static " + xcls + " singleton()");
139 this.addLine(this.pad + "{");
140 this.addLine(this.ipad + "if (_" + this.node.xvala_id + " == null) {");
141 this.addLine(this.ipad + " _" + this.node.xvala_id + "= new "+ this.xcls + "();"); // what about args?
142 this.addLine(this.ipad + "}");
143 this.addLine(this.ipad + "return _" + this.node.xvala_id +";");
144 this.addLine(this.pad + "}");
147 * add the constructor definition..
149 protected override void addValaCtor()
159 this.addLine(this.pad + "// ctor");
161 if (this.node.has("* args")) {
162 // not sure what this is supposed to be ding..
164 cargs_str = this.node.get("* args");
165 //var ar = this.node.get("* args");.split(",");
166 //for (var ari =0; ari < ar.length; ari++) {
167 // cargs += (ar[ari].trim().split(" ").pop();
171 if (this.depth < 1) {
173 // top level - does not pass the top level element..
174 this.addLine(this.pad + "public " + this.xcls + "(" + cargs_str +")");
175 this.addLine(this.pad + "{");
177 if (cargs_str.length > 0) {
178 cargs_str = ", " + cargs_str;
180 var top = this.top as NodeToVala;
181 var tcls = top == null ? "???" : top.xcls;
182 // for sub classes = we passs the top level as _owner
183 this.addLine(this.pad + "public " + this.xcls + "(" + tcls + " _owner " + cargs_str + ")");
184 this.addLine(this.pad + "{");
190 * Initialize this.el to point to the wrapped element.
195 void addWrappedCtor()
198 // this may need to look up properties to fill in the arguments..
199 // introspection does not workk..... - as things like gtkmessagedialog
201 if (cls == 'Gtk.Table') {
203 var methods = this.palete.getPropertiesFor(cls, 'methods');
205 print(JSON.stringify(this.palete.proplist[cls], null,4));
210 // ctor can still override.
211 if (this.node.has("* ctor")) {
212 this.node.setLine(this.cur_line, "p", "* ctor");
213 this.addLine(this.ipad + "this.el = " + this.node.get("* ctor")+ ";");
217 this.node.setLine(this.cur_line, "p", "* xtype");;
219 // is the wrapped element a struct?
221 var ncls = Palete.Gir.factoryFqn((Project.Gtk) this.file.project, this.node.fqn());
222 if (ncls != null && ncls.nodetype == "Struct") {
223 // we can use regular setters to apply the values.
224 this.addLine(this.ipad + "this.el = " + this.node.fqn() + "();");
232 switch(this.node.fqn()) {
234 // FIXME -- these are all GTK3 - can be removed when I get rid of them..
236 var is_entry = this.node.has("has_entry") && this.node.get_prop("has_entry").val.down() == "true";
238 break; // regular ctor.
240 this.ignoreWrapped("has_entry");
241 ctor = ".with_entry";
245 case "Gtk.ListStore":
246 case "Gtk.TreeStore":
248 // not sure if this works.. otherwise we have to go with varargs and count + vals...
249 if (this.node.has("* types")) {
250 args_str = this.node.get_prop("* types").val;
252 if (this.node.has("n_columns") && this.node.has("columns")) { // old value?
253 args_str = " { " + this.node.get_prop("columns").val + " } ";
254 this.ignoreWrapped("columns");
255 this.ignoreWrapped("n_columns");
258 this.addLine(this.ipad + "this.el = new " + this.node.fqn() + ".newv( " + args_str + " );");
262 case "Gtk.LinkButton": // args filled with values.
263 if (this.node.has("label")) {
264 ctor = ".with_label";
271 var default_ctor = Palete.Gir.factoryFqn((Project.Gtk) this.file.project, this.node.fqn() + ctor);
274 // use the default ctor - with arguments (from properties)
276 if (default_ctor != null && default_ctor.paramset != null && default_ctor.paramset.params.size > 0) {
278 foreach(var param in default_ctor.paramset.params) {
281 GLib.debug("building CTOR ARGS: %s, %s", n, param.is_varargs ? "VARARGS": "");
282 if (n == "___") { // for some reason our varargs are converted to '___' ...
286 if (this.node.has(n)) { // node does not have a value
288 this.ignoreWrapped(n);
291 var v = this.node.get(n);
293 if (param.type == "string") {
294 v = "\"" + v.escape("") + "\"";
296 if (v == "TRUE" || v == "FALSE") {
304 var propnode = this.node.findProp(n);
305 if (propnode != null) {
308 var pname = this.addPropSet(propnode, propnode.has("id") ? propnode.get_prop("id").val : "");
309 args += (pname + ".el") ;
310 if (!propnode.has("id")) {
311 this.addLine(this.ipad + pname +".ref();");
316 this.ignoreWrapped(n);
325 if (param.type.contains("int")) {
329 if (param.type.contains("float")) {
333 if (param.type.contains("bool")) {
334 args += "true"; // always default to true?
337 // any other types???
347 this.node.setLine(this.cur_line, "p", "* xtype");
348 this.addLine(this.ipad + "this.el = new " + this.node.fqn() + "( "+ string.joinv(", ",args) + " );") ;
352 // default ctor with no params..
353 if (default_ctor != null && ctor != ".new" ) {
354 this.node.setLine(this.cur_line, "p", "* xtype");
356 this.addLine(this.ipad + "this.el = new " + this.node.fqn() + ctor + "( );") ;
361 this.addLine(this.ipad + "this.el = new " + this.node.fqn() + "(" + args_str + ");");