Fix #8004 - try and fix start line / end line cached node output
[roobuilder] / src / JsRender / NodeToValaWrapped.vala
1 /**
2         this is the code to write the 'classic' node to vala output
3         */
4         /**
5  * 
6  * Code to convert node tree to Vala...
7  * 
8  * usage : x = (new JsRender.NodeToVala(node)).munge();
9  * 
10  * Fixmes?
11  *
12  *  pack - can we come up with a replacement?
13      - parent.child == child_widget -- actually uses getters and effectively does 'add'?
14        (works on most)?
15     
16      
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
19  
20  * 
21  
22  * 
23  * 
24 */
25
26  
27 public class JsRender.NodeToValaWrapped : NodeToVala {
28
29
30          
31         public NodeToValaWrapped( JsRender file,  Node node,  int depth, NodeToVala? parent) 
32         {
33                 base (file, node, depth, parent);
34                 this.this_el = "this.el.";
35         }
36         
37         /**
38          *  Main entry point to convert a file into a string..
39          */
40         public static string mungeFile(JsRender file) 
41         {
42                 if (file.tree == null) {
43                         return "";
44                 }
45
46                 var n = new NodeToValaWrapped(file, file.tree, 0, null);
47                 n.toValaName(file.tree);
48                  
49                 GLib.debug("top cls %s / xlcs %s\n ",file.tree.xvala_cls,file.tree.xvala_cls); 
50                 n.initCls();
51                 return n.munge();
52                 
53
54         }
55         public override string munge ( )
56         {
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_start_line == cur_line &&
61                         this.node.as_source != ""
62                 ) {
63                         return this.node.as_source;
64                 }
65                 this.node.as_source_start_line = cur_line;
66                 
67                 this.namespaceHeader();
68                 this.globalVars();
69                 this.classHeader();
70                 this.addSingleton();
71                 this.addTopProperties();
72                 this.addMyVars();
73                 this.addPlusProperties(); // (this is child properties whos 'id' starts with '+' ??? not sure..
74                 this.addValaCtor();
75                 this.addUnderThis();
76                 this.addWrappedCtor();  // var this.el = new XXXXX()
77
78                 this.addInitMyVars();
79                 this.addWrappedProperties();
80                 this.addChildren();
81                 //this.addAutoShow(); // not needed gtk4 autoshow menuitems
82                 
83                 this.addInit();
84                 this.addListeners();
85                 this.addEndCtor();
86                 this.addUserMethods();
87                 this.iterChildren();
88                 this.namespaceFooter();
89                 
90  
91                 this.node.as_source_version = this.node.updated_count;
92                 this.node.as_source == this.ret;
93                 return this.ret;
94                  
95                          
96         }
97         public override string mungeChild(  Node cnode)
98         {
99                 var x = new  NodeToValaWrapped(this.file, cnode,  this.depth+1, this);
100                 return x.munge();
101         }
102         
103         protected override void classHeader()
104         {
105                 var top = this.top as NodeToVala;
106                 if (top == null) {
107                          
108                         return;
109                 }
110                 // class header..
111                 // class xxx {   WrappedGtk  el; }
112                 this.node.line_start = this.cur_line;
113                 
114                 this.top.node.setNodeLine(this.cur_line, this.node);
115                 
116                 this.addLine(this.inpad + "public class " + this.xcls + " : Object");
117                 this.addLine(this.inpad + "{");
118                 
119                  
120                 this.addLine(this.pad + "public " + this.cls + " el;");
121  
122                 this.addLine(this.pad + "private " + top.xcls + "  _this;");
123                 this.addLine();
124                         
125                         
126                         
127                         // singleton
128         }
129         public void globalVars()
130         {
131                 if (this.depth > 0) {
132                         return;
133                 }
134                 // Global Vars..??? when did this get removed..?
135                 //this.ret += this.inpad + "public static " + this.xcls + "  " + this.node.xvala_id+ ";\n\n";
136
137                 this.addLine(this.inpad + "static " + this.xcls + "  _" + this.node.xvala_id+ ";");
138                 this.addLine();
139                    
140         }
141         protected void addSingleton() 
142         {
143                 if (depth > 0) {
144                         return;
145                 }
146                 this.addLine(pad + "public static " + xcls + " singleton()");
147                 this.addLine(this.pad + "{");
148                 this.addLine(this.ipad +    "if (_" + this.node.xvala_id  + " == null) {");
149                 this.addLine(this.ipad +    "    _" + this.node.xvala_id + "= new "+ this.xcls + "();");  // what about args?
150                 this.addLine(this.ipad +    "}");
151                 this.addLine(this.ipad +    "return _" + this.node.xvala_id +";");
152                 this.addLine(this.pad + "}");
153         }
154         /**
155          * add the constructor definition..
156          */
157         protected override void addValaCtor()
158         {
159                         
160                 
161                 // .vala props.. 
162                 
163  
164                 var cargs_str = "";
165                 // ctor..
166                 this.addLine();
167                 this.addLine(this.pad + "// ctor");
168                 
169                 if (this.node.has("* args")) {
170                         // not sure what this is supposed to be ding..
171                 
172                         cargs_str =  this.node.get("* args");
173                         //var ar = this.node.get("* args");.split(",");
174                         //for (var ari =0; ari < ar.length; ari++) {
175                                 //      cargs +=  (ar[ari].trim().split(" ").pop();
176                                   // }
177                         }
178         
179                 if (this.depth < 1) {
180                  
181                         // top level - does not pass the top level element..
182                         this.addLine(this.pad + "public " + this.xcls + "(" +  cargs_str +")");
183                         this.addLine(this.pad + "{");
184                 } else {
185                         if (cargs_str.length > 0) {
186                                 cargs_str = ", " + cargs_str;
187                         }
188                         var top = this.top as NodeToVala;
189                         var tcls = top == null ? "???" : top.xcls;
190                         // for sub classes = we passs the top level as _owner
191                         this.addLine(this.pad + "public " + this.xcls + "(" +  tcls + " _owner " + cargs_str + ")");
192                         this.addLine(this.pad + "{");
193                 }
194                 
195
196         }
197         /**
198          * Initialize this.el to point to the wrapped element.
199          * 
200          * 
201          */
202
203         void addWrappedCtor()
204         {
205                 // wrapped ctor..
206                 // this may need to look up properties to fill in the arguments..
207                 // introspection does not workk..... - as things like gtkmessagedialog
208                 /*
209                 if (cls == 'Gtk.Table') {
210
211                 var methods = this.palete.getPropertiesFor(cls, 'methods');
212
213                 print(JSON.stringify(this.palete.proplist[cls], null,4));
214                 Seed.quit();
215                 }
216                 */
217                 
218                 // ctor can still override.
219                 if (this.node.has("* ctor")) {
220                         this.node.setLine(this.cur_line, "p", "* ctor");
221                         this.addLine(this.ipad + "this.el = " + this.node.get("* ctor")+ ";");
222                         return;
223                 }
224                 
225                 this.node.setLine(this.cur_line, "p", "* xtype");;
226                 
227                 // is the wrapped element a struct?
228                 
229                 var ncls = Palete.Gir.factoryFqn((Project.Gtk) this.file.project, this.node.fqn());
230                 if (ncls != null && ncls.nodetype == "Struct") {
231                         // we can use regular setters to apply the values.
232                         this.addLine(this.ipad + "this.el = " + this.node.fqn() + "();");
233                         return;
234                 
235                 
236                 }
237
238                 var ctor = ".new";
239                 var args_str = "";
240                 switch(this.node.fqn()) {
241                 
242                 // FIXME -- these are all GTK3 - can be removed when I get rid of them..
243                         case "Gtk.ComboBox":
244                                 var is_entry = this.node.has("has_entry") && this.node.get_prop("has_entry").val.down() == "true";
245                                 if (!is_entry) { 
246                                         break; // regular ctor.
247                                 }
248                                 this.ignoreWrapped("has_entry");
249                                 ctor = ".with_entry";
250                                 break;
251                                 
252                 
253                         case "Gtk.ListStore":
254                         case "Gtk.TreeStore":
255
256                                 // not sure if this works.. otherwise we have to go with varargs and count + vals...
257                                 if (this.node.has("* types")) {
258                                         args_str = this.node.get_prop("* types").val;
259                                 }
260                                 if (this.node.has("n_columns") && this.node.has("columns")) { // old value?
261                                         args_str = " { " + this.node.get_prop("columns").val + " } ";
262                                         this.ignoreWrapped("columns");
263                                         this.ignoreWrapped("n_columns");
264                                 }
265                                 
266                                 this.addLine(this.ipad + "this.el = new " + this.node.fqn() + ".newv( " + args_str + " );");
267                                 return;
268  
269                                 
270                         case "Gtk.LinkButton": // args filled with values.
271                                 if (this.node.has("label")) {
272                                         ctor = ".with_label";    
273                                 }
274                                 break;
275                                 
276                         default:
277                                 break;
278                 }
279                 var default_ctor = Palete.Gir.factoryFqn((Project.Gtk) this.file.project, this.node.fqn() + ctor);              
280                  
281                 
282                 // use the default ctor - with arguments (from properties)
283                 
284                 if (default_ctor != null && default_ctor.paramset != null && default_ctor.paramset.params.size > 0) {
285                         string[] args  = {};
286                         foreach(var param in default_ctor.paramset.params) {
287                                  
288                                 var n = param.name;
289                             GLib.debug("building CTOR ARGS: %s, %s", n, param.is_varargs ? "VARARGS": "");
290                                 if (n == "___") { // for some reason our varargs are converted to '___' ...
291                                         continue;
292                                 }
293                                 
294                                 if (this.node.has(n)) {  // node does not have a value
295                                         
296                                         this.ignoreWrapped(n);
297                                         this.ignore(n);
298                                         
299                                         var v = this.node.get(n);
300
301                                         if (param.type == "string") {
302                                                 v = "\"" +  v.escape("") + "\"";
303                                         }
304                                         if (v == "TRUE" || v == "FALSE") {
305                                                 v = v.down();
306                                         }
307
308                                         
309                                         args += v;
310                                         continue;
311                                 }
312                                 var propnode = this.node.findProp(n);
313                                 if (propnode != null) {
314                                         // assume it's ok..
315                                         
316                                         var pname = this.addPropSet(propnode, propnode.has("id") ? propnode.get_prop("id").val : "");
317                                         args += (pname + ".el") ;
318                                         if (!propnode.has("id")) {
319                                                 this.addLine(this.ipad + pname +".ref();"); 
320                                         }
321                                         
322                                         
323                                         
324                                         this.ignoreWrapped(n);
325                                         
326                                         continue;
327                                 }
328                                         
329                                          
330                                         
331                                         
332                                  
333                                 if (param.type.contains("int")) {
334                                         args += "0";
335                                         continue;
336                                 }
337                                 if (param.type.contains("float")) {
338                                         args += "0f";
339                                         continue;
340                                 }
341                                 if (param.type.contains("bool")) {
342                                         args += "true"; // always default to true?
343                                         continue;
344                                 }
345                                 // any other types???
346                                 
347                                 
348                                 
349                                 
350                                 args += "null";
351                                  
352                                 
353
354                         }
355                         this.node.setLine(this.cur_line, "p", "* xtype");
356                         this.addLine(this.ipad + "this.el = new " + this.node.fqn() + "( "+ string.joinv(", ",args) + " );") ;
357                         return;
358                         
359                 }
360                 // default ctor with no params..
361                  if (default_ctor != null && ctor != ".new" ) {
362                         this.node.setLine(this.cur_line, "p", "* xtype");
363                         
364                         this.addLine(this.ipad + "this.el = new " + this.node.fqn() + ctor + "(  );") ;
365                         return;
366                  }
367                 
368                 
369                 this.addLine(this.ipad + "this.el = new " + this.node.fqn() + "(" + args_str + ");");
370                         
371         }
372          
373                  
374
375 }