Fix #8003 - undo code
[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 != ""
61                 ) {
62                         return this.node.as_source;
63                 }
64         
65                 
66                 this.namespaceHeader();
67                 this.globalVars();
68                 this.classHeader();
69                 this.addSingleton();
70                 this.addTopProperties();
71                 this.addMyVars();
72                 this.addPlusProperties(); // (this is child properties whos 'id' starts with '+' ??? not sure..
73                 this.addValaCtor();
74                 this.addUnderThis();
75                 this.addWrappedCtor();  // var this.el = new XXXXX()
76
77                 this.addInitMyVars();
78                 this.addWrappedProperties();
79                 this.addChildren();
80                 //this.addAutoShow(); // not needed gtk4 autoshow menuitems
81                 
82                 this.addInit();
83                 this.addListeners();
84                 this.addEndCtor();
85                 this.addUserMethods();
86                 this.iterChildren();
87                 this.namespaceFooter();
88                 
89                 
90                 this.node.as_source_version = this.node.updated_count;
91                 this.node.as_source == this.ret;
92                 return this.ret;
93                  
94                          
95         }
96         public override string mungeChild(  Node cnode)
97         {
98                 var x = new  NodeToValaWrapped(this.file, cnode,  this.depth+1, this);
99                 return x.munge();
100         }
101         
102         protected override void classHeader()
103         {
104                 var top = this.top as NodeToVala;
105                 if (top == null) {
106                          
107                         return;
108                 }
109                 // class header..
110                 // class xxx {   WrappedGtk  el; }
111                 this.node.line_start = this.cur_line;
112                 
113                 this.top.node.setNodeLine(this.cur_line, this.node);
114                 
115                 this.addLine(this.inpad + "public class " + this.xcls + " : Object");
116                 this.addLine(this.inpad + "{");
117                 
118                  
119                 this.addLine(this.pad + "public " + this.cls + " el;");
120  
121                 this.addLine(this.pad + "private " + top.xcls + "  _this;");
122                 this.addLine();
123                         
124                         
125                         
126                         // singleton
127         }
128         public void globalVars()
129         {
130                 if (this.depth > 0) {
131                         return;
132                 }
133                 // Global Vars..??? when did this get removed..?
134                 //this.ret += this.inpad + "public static " + this.xcls + "  " + this.node.xvala_id+ ";\n\n";
135
136                 this.addLine(this.inpad + "static " + this.xcls + "  _" + this.node.xvala_id+ ";");
137                 this.addLine();
138                    
139         }
140         protected void addSingleton() 
141         {
142                 if (depth > 0) {
143                         return;
144                 }
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 + "}");
152         }
153         /**
154          * add the constructor definition..
155          */
156         protected override void addValaCtor()
157         {
158                         
159                 
160                 // .vala props.. 
161                 
162  
163                 var cargs_str = "";
164                 // ctor..
165                 this.addLine();
166                 this.addLine(this.pad + "// ctor");
167                 
168                 if (this.node.has("* args")) {
169                         // not sure what this is supposed to be ding..
170                 
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();
175                                   // }
176                         }
177         
178                 if (this.depth < 1) {
179                  
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 + "{");
183                 } else {
184                         if (cargs_str.length > 0) {
185                                 cargs_str = ", " + cargs_str;
186                         }
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 + "{");
192                 }
193                 
194
195         }
196         /**
197          * Initialize this.el to point to the wrapped element.
198          * 
199          * 
200          */
201
202         void addWrappedCtor()
203         {
204                 // wrapped ctor..
205                 // this may need to look up properties to fill in the arguments..
206                 // introspection does not workk..... - as things like gtkmessagedialog
207                 /*
208                 if (cls == 'Gtk.Table') {
209
210                 var methods = this.palete.getPropertiesFor(cls, 'methods');
211
212                 print(JSON.stringify(this.palete.proplist[cls], null,4));
213                 Seed.quit();
214                 }
215                 */
216                 
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")+ ";");
221                         return;
222                 }
223                 
224                 this.node.setLine(this.cur_line, "p", "* xtype");;
225                 
226                 // is the wrapped element a struct?
227                 
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() + "();");
232                         return;
233                 
234                 
235                 }
236
237                 var ctor = ".new";
238                 var args_str = "";
239                 switch(this.node.fqn()) {
240                 
241                 // FIXME -- these are all GTK3 - can be removed when I get rid of them..
242                         case "Gtk.ComboBox":
243                                 var is_entry = this.node.has("has_entry") && this.node.get_prop("has_entry").val.down() == "true";
244                                 if (!is_entry) { 
245                                         break; // regular ctor.
246                                 }
247                                 this.ignoreWrapped("has_entry");
248                                 ctor = ".with_entry";
249                                 break;
250                                 
251                 
252                         case "Gtk.ListStore":
253                         case "Gtk.TreeStore":
254
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;
258                                 }
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");
263                                 }
264                                 
265                                 this.addLine(this.ipad + "this.el = new " + this.node.fqn() + ".newv( " + args_str + " );");
266                                 return;
267  
268                                 
269                         case "Gtk.LinkButton": // args filled with values.
270                                 if (this.node.has("label")) {
271                                         ctor = ".with_label";    
272                                 }
273                                 break;
274                                 
275                         default:
276                                 break;
277                 }
278                 var default_ctor = Palete.Gir.factoryFqn((Project.Gtk) this.file.project, this.node.fqn() + ctor);              
279                  
280                 
281                 // use the default ctor - with arguments (from properties)
282                 
283                 if (default_ctor != null && default_ctor.paramset != null && default_ctor.paramset.params.size > 0) {
284                         string[] args  = {};
285                         foreach(var param in default_ctor.paramset.params) {
286                                  
287                                 var n = param.name;
288                             GLib.debug("building CTOR ARGS: %s, %s", n, param.is_varargs ? "VARARGS": "");
289                                 if (n == "___") { // for some reason our varargs are converted to '___' ...
290                                         continue;
291                                 }
292                                 
293                                 if (this.node.has(n)) {  // node does not have a value
294                                         
295                                         this.ignoreWrapped(n);
296                                         this.ignore(n);
297                                         
298                                         var v = this.node.get(n);
299
300                                         if (param.type == "string") {
301                                                 v = "\"" +  v.escape("") + "\"";
302                                         }
303                                         if (v == "TRUE" || v == "FALSE") {
304                                                 v = v.down();
305                                         }
306
307                                         
308                                         args += v;
309                                         continue;
310                                 }
311                                 var propnode = this.node.findProp(n);
312                                 if (propnode != null) {
313                                         // assume it's ok..
314                                         
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();"); 
319                                         }
320                                         
321                                         
322                                         
323                                         this.ignoreWrapped(n);
324                                         
325                                         continue;
326                                 }
327                                         
328                                          
329                                         
330                                         
331                                  
332                                 if (param.type.contains("int")) {
333                                         args += "0";
334                                         continue;
335                                 }
336                                 if (param.type.contains("float")) {
337                                         args += "0f";
338                                         continue;
339                                 }
340                                 if (param.type.contains("bool")) {
341                                         args += "true"; // always default to true?
342                                         continue;
343                                 }
344                                 // any other types???
345                                 
346                                 
347                                 
348                                 
349                                 args += "null";
350                                  
351                                 
352
353                         }
354                         this.node.setLine(this.cur_line, "p", "* xtype");
355                         this.addLine(this.ipad + "this.el = new " + this.node.fqn() + "( "+ string.joinv(", ",args) + " );") ;
356                         return;
357                         
358                 }
359                 // default ctor with no params..
360                  if (default_ctor != null && ctor != ".new" ) {
361                         this.node.setLine(this.cur_line, "p", "* xtype");
362                         
363                         this.addLine(this.ipad + "this.el = new " + this.node.fqn() + ctor + "(  );") ;
364                         return;
365                  }
366                 
367                 
368                 this.addLine(this.ipad + "this.el = new " + this.node.fqn() + "(" + args_str + ");");
369                         
370         }
371          
372                  
373
374 }