fix line numbering issues with vala generator - hopefully fixes completion in node...
[roobuilder] / src / JsRender / NodeToVala.vala
1 /**
2  * 
3  * Code to convert node tree to Vala...
4  * 
5  * usage : x = (new JsRender.NodeToVala(node)).munge();
6  * 
7  * Fixmes?
8  *
9  *  pack - can we come up with a replacement?
10      - parent.child == child_widget -- actually uses getters and effectively does 'add'?
11        (works on most)?
12     
13      
14  * args  -- vala constructor args (should really only be used at top level - we did use it for clutter originally(
15  * ctor  -- different ctor argument
16  
17  * 
18  
19  * 
20  * 
21 */
22
23  
24 public class JsRender.NodeToVala : Object {
25
26         Node node;
27
28         int depth;
29         string inpad;
30         string pad;
31         string ipad;
32         string cls;  // node fqn()
33         string xcls;
34         
35         string ret;
36         
37         int cur_line;
38
39         Gee.ArrayList<string> ignoreList;
40         Gee.ArrayList<string> ignoreWrappedList; 
41         Gee.ArrayList<string> myvars;
42         Gee.ArrayList<Node> vitems; // top level items
43         NodeToVala top;
44         JsRender file;
45         int pane_number = 0;
46         
47         /* 
48          * ctor - just initializes things
49          * - wraps a render node 
50          */
51         public NodeToVala( JsRender file,  Node node,  int depth, NodeToVala? parent) 
52         {
53
54                 
55                 this.node = node;
56                 this.depth = depth;
57                 if (file.name.contains(".")) { // namespaced..
58                         this.inpad = string.nfill(depth > 0 ? 8 : 4, ' ');
59                 } else {
60                         this.inpad = string.nfill(depth > 0 ? 8 : 4, ' ');
61                 }
62                 this.pad = this.inpad + "    ";
63                 this.ipad = this.inpad + "        ";
64                 this.cls = node.xvala_cls;
65                 this.xcls = node.xvala_xcls;
66                 if (depth == 0 && this.xcls.contains(".")) {
67                         var ar = this.xcls.split(".");
68                         this.xcls = ar[ar.length-1];
69                 }
70                 
71                 
72                 this.ret = "";
73                 this.cur_line = parent == null ? 0 : parent.cur_line;
74                 
75                 
76                 this.top = parent == null ? this : parent.top;
77                 this.ignoreList = new Gee.ArrayList<string>();
78                 this.ignoreWrappedList  = new Gee.ArrayList<string>();
79                 this.myvars = new Gee.ArrayList<string>();
80                 this.vitems = new Gee.ArrayList<Node>();
81                 this.file = file;
82                 
83                 // initialize line data..
84                 node.line_start = this.cur_line;
85                 node.line_end  = this.cur_line;
86                 node.lines = new Gee.ArrayList<int>();
87                 node.line_map = new Gee.HashMap<int,string>();
88                 if (parent == null) {
89                         node.node_lines = new Gee.ArrayList<int>();
90                         node.node_lines_map = new Gee.HashMap<int,Node>();
91                  }
92                 
93         }
94
95         public int vcnt = 0;
96         string toValaNS(Node item)
97         {
98                 var ns = item.get("xns") ;
99                 //if (ns == "GtkSource") {  technically on Gtk3?
100                 //      return "Gtk.Source";
101                 //}
102                 return ns + ".";
103         }
104         public void  toValaName(Node item, int depth =0) 
105         {
106                 this.vcnt++;
107
108                 var ns =  this.toValaNS(item) ;
109                 var cls = ns + item.get("xtype");
110                 
111                 
112                 item.xvala_cls = cls;
113                 
114                 
115                 string id = item.get("id").length > 0 ?
116                         item.get("id") :  "%s%d".printf(item.get("xtype"), this.vcnt);
117
118                 
119                 
120                 
121                 if (id[0] == '*' || id[0] == '+') {
122                         item.xvala_xcls = "Xcls_" + id.substring(1);
123                 } else {
124                         item.xvala_xcls = "Xcls_" + id;
125                 }
126                         
127                 
128                 item.xvala_id =  id;
129                 if (depth > 0) {                        
130                         this.vitems.add(item);
131                         
132                 // setting id on top level class changes it classname..                 
133                 // oddly enough we havent really thought about namespacing here.
134                 
135                 } else if (!item.props.has_key("id")) { 
136                         // use the file name..
137                         item.xvala_xcls =  this.file.file_without_namespace;
138                         // is id used?
139                         item.xvala_id = this.file.file_without_namespace;
140
141                 }
142                 // loop children..
143                                                                                                                            
144                 if (item.readItems().size < 1) {
145                         return;
146                 }
147                 for(var i =0;i<item.readItems().size;i++) {
148                         this.toValaName(item.readItems().get(i), depth+1);
149                 }
150                                           
151         }
152         /**
153          *  Main entry point to convert a file into a string..
154          */
155         public static string mungeFile(JsRender file) 
156         {
157                 if (file.tree == null) {
158                         return "";
159                 }
160
161                 var n = new NodeToVala(file, file.tree, 0, null);
162                 n.file = file;
163                 n.vcnt = 0;
164                 
165                 n.toValaName(file.tree);
166                 
167                 
168                 GLib.debug("top cls %s / xlcs %s\n ",file.tree.xvala_cls,file.tree.xvala_cls); 
169                 n.cls = file.tree.xvala_cls;
170                 n.xcls = file.tree.xvala_xcls;
171                 return n.munge();
172                 
173
174         }
175         int child_count = 1; // used to number the children.
176         public string munge ( )
177         {
178                 //return this.mungeToString(this.node);
179                 this.child_count = 1;
180                 this.ignore("pack");
181                 this.ignore("init");
182                 this.ignore("xns");
183                 this.ignore("xtype");
184                 this.ignore("id");
185                 
186                 this.namespaceHeader();
187                 this.globalVars();
188                 this.classHeader();
189                 this.addSingleton();
190                 this.addTopProperties();
191                 this.addMyVars();
192                 this.addPlusProperties();
193                 this.addValaCtor();
194                 this.addUnderThis();
195                 this.addWrappedCtor();  // var this.el = new XXXXX()
196
197                 this.addInitMyVars();
198                 this.addWrappedProperties();
199                 this.addChildren();
200                 this.addAutoShow(); // autoshow menuitems
201                 
202                 this.addInit();
203                 this.addListeners();
204                 this.addEndCtor();
205                 this.addUserMethods();
206                 this.iterChildren();
207                 this.namespaceFooter();
208                 
209                 return this.ret;
210                  
211                          
212         } 
213         public string mungeChild(  Node cnode)
214         {
215                 var x = new  NodeToVala(this.file, cnode,  this.depth+1, this);
216                 return x.munge();
217         }
218         public void addLine(string str= "")
219         {
220                 this.cur_line++;
221                 //this.ret += "/*%d*/ ".printf(this.cur_line-1) + str + "\n";
222                 this.ret += str + "\n";
223         }
224         public void addMultiLine(string str= "")
225         {
226                  
227                 this.cur_line += str.split("\n").length;
228                 //this.ret +=  "/*%d*/ ".printf(l) + str + "\n";
229                 this.ret +=   str + "\n";
230         }
231          
232         public void namespaceHeader()
233         {
234                 if (this.depth > 0 || this.file.file_namespace == "") {
235                         return;
236                 }
237                 this.addLine("namespace " + this.file.file_namespace);
238                 this.addLine("{");
239         
240         }
241         public void namespaceFooter()
242         {
243                 if (this.depth > 0 || this.file.file_namespace == "") {
244                         return;
245                 }
246                 this.addLine("}");
247         
248         }
249         public void globalVars()
250         {
251                 if (this.depth > 0) {
252                         return;
253                 }
254                 // Global Vars..??? when did this get removed..?
255                 //this.ret += this.inpad + "public static " + this.xcls + "  " + this.node.xvala_id+ ";\n\n";
256
257                 this.addLine(this.inpad + "static " + this.xcls + "  _" + this.node.xvala_id+ ";");
258                 this.addLine();
259                    
260         }
261
262         void classHeader()
263         {
264                            
265                 // class header..
266                 // class xxx {   WrappedGtk  el; }
267                 this.node.line_start = this.cur_line;
268                 
269                 this.top.node.setNodeLine(this.cur_line, this.node);
270                 
271                 this.addLine(this.inpad + "public class " + this.xcls + " : Object");
272                 this.addLine(this.inpad + "{");
273                 
274                  
275                 this.addLine(this.pad + "public " + this.cls + " el;");
276  
277                 this.addLine(this.pad + "private " + this.top.xcls + "  _this;");
278                 this.addLine();
279                         
280                         
281                         
282                         // singleton
283         }
284         void addSingleton() 
285         {
286                 if (depth > 0) {
287                         return;
288                 }
289                 this.addLine(pad + "public static " + xcls + " singleton()");
290                 this.addLine(this.pad + "{");
291                 this.addLine(this.ipad +    "if (_" + this.node.xvala_id  + " == null) {");
292                 this.addLine(this.ipad +    "    _" + this.node.xvala_id + "= new "+ this.xcls + "();");  // what about args?
293                 this.addLine(this.ipad +    "}");
294                 this.addLine(this.ipad +    "return _" + this.node.xvala_id +";");
295                 this.addLine(this.pad + "}");
296         }
297                         
298         /**
299          * when ID is used... on an element, it registeres a property on the top level...
300          * so that _this.ID always works..
301          * 
302          */
303         void addTopProperties()
304         {
305                 if (this.depth > 0) {
306                         return;
307                 }
308                 // properties - global..??
309
310                 var iter = this.vitems.list_iterator();
311                 while(iter.next()) {
312                         var n = iter.get();
313
314                          
315                         if (!n.props.has_key("id") || n.xvala_id.length < 0) {
316                                 continue;
317                                 
318                         }
319                         if (n.xvala_id[0] == '*') {
320                                 continue;
321                         }
322                         if (n.xvala_id[0] == '+') {
323                                 continue;
324                         }
325                         this.addLine(this.pad + "public " + n.xvala_xcls + " " + n.xvala_id + ";");
326                         
327                 }
328                                 
329         }
330         /**
331          * create properties that are not 'part of the wrapped element.
332          * 
333          * 
334          */
335  
336         void addMyVars()
337         {
338                 GLib.debug("callinged addMhyVars");
339                 
340                 this.addLine();
341                 this.addLine(this.ipad + "// my vars (def)");
342                         
343
344  
345                 var cls = Palete.Gir.factoryFqn((Project.Gtk) this.file.project, this.node.fqn());
346                    
347                 if (cls == null) {
348                         GLib.debug("Gir factory failed to find class %s", this.node.fqn());
349                         
350                         //return;
351                 }
352           
353                 
354                         // Key = TYPE:name
355                 var iter = this.node.props.map_iterator();
356                 while (iter.next()) {
357                          
358                         var prop = iter.get_value();
359                         
360                         if (this.shouldIgnore(prop.name)) {
361                                 continue;
362                         }
363
364                         // user defined method
365                         if (prop.ptype == NodePropType.METHOD) {
366                                 continue;
367                         }
368                         if (prop.ptype == NodePropType.SPECIAL) {
369                                 continue;
370                         }
371                                 
372                         if (prop.ptype == NodePropType.SIGNAL) {
373                                 this.node.setLine(this.cur_line, "p", prop.name);
374                                 this.addLine(this.pad + "public signal " + prop.rtype + " " + prop.name  + " "  + prop.val + ";");
375                                 
376                                 this.ignore(prop.name);
377                                 continue;
378                         }
379                         
380                         GLib.debug("Got myvars: %s", prop.name.strip());
381                         
382                         if (prop.rtype.strip().length < 1) {
383                                 continue;
384                         }
385                         
386                         // is it a class property...
387                         if (cls != null && cls.props.has_key(prop.name) && prop.ptype != NodePropType.USER) {
388                                 continue;
389                         }
390                         
391                         this.myvars.add(prop.name);
392                         prop.start_line = this.cur_line;
393                         
394                         this.node.setLine(this.cur_line, "p", prop.name);
395                         
396                         this.addLine(this.pad + "public " + prop.rtype + " " + prop.name + ";"); // definer - does not include value.
397
398
399                         prop.end_line = this.cur_line;                          
400                         this.ignore(prop.name);
401                         
402                                 
403                 }
404         }
405         
406         // if id of child is '+' then it's a property of this..
407         void addPlusProperties()
408         {
409                 if (this.node.readItems().size < 1) {
410                         return;
411                 }
412                 var iter = this.node.readItems().list_iterator();
413                 while (iter.next()) {
414                         var ci = iter.get();
415                                 
416                         if (ci.xvala_id[0] != '+') {
417                                 continue; // skip generation of children?
418                                 
419                         }
420                          
421                         this.addLine(this.pad + "public " + ci.xvala_xcls + " " + ci.xvala_id.substring(1) + ";");
422                                            
423                         
424                 }
425         }
426         /**
427          * add the constructor definition..
428          */
429         void addValaCtor()
430         {
431                         
432                 
433                 // .vala props.. 
434                 
435  
436                 var cargs_str = "";
437                 // ctor..
438                 this.addLine();
439                 this.addLine(this.pad + "// ctor");
440                 
441                 if (this.node.has("* args")) {
442                         // not sure what this is supposed to be ding..
443                 
444                         cargs_str =  this.node.get("* args");
445                         //var ar = this.node.get("* args");.split(",");
446                         //for (var ari =0; ari < ar.length; ari++) {
447                                 //      cargs +=  (ar[ari].trim().split(" ").pop();
448                                   // }
449                         }
450         
451                 if (this.depth < 1) {
452                  
453                         // top level - does not pass the top level element..
454                         this.addLine(this.pad + "public " + this.xcls + "(" +  cargs_str +")");
455                         this.addLine(this.pad + "{");
456                 } else {
457                         if (cargs_str.length > 0) {
458                                 cargs_str = ", " + cargs_str;
459                         }
460                         // for sub classes = we passs the top level as _owner
461                         this.addLine(this.pad + "public " + this.xcls + "(" +  this.top.xcls + " _owner " + cargs_str + ")");
462                         this.addLine(this.pad + "{");
463                 }
464                 
465
466         }
467         /**
468          *  make sure _this is defined..
469          */
470         void addUnderThis() 
471         {
472                 // public static?
473                 if (depth < 1) {
474                         this.addLine( this.ipad + "_this = this;");
475                         return;
476                 }
477                 // for non top level = _this point to owner, and _this.ID is set
478                 
479                 this.addLine( this.ipad + "_this = _owner;");
480
481                 if (this.node.props.has_key("id")
482                         &&
483                         this.node.xvala_id != "" 
484                         && 
485                         this.node.xvala_id[0] != '*' 
486                         && 
487                         this.node.xvala_id[0] != '+' 
488                         ) {
489                                 this.addLine( this.ipad + "_this." + node.xvala_id  + " = this;");
490                    
491                 }
492                          
493         }
494          
495         /**
496          * Initialize this.el to point to the wrapped element.
497          * 
498          * 
499          */
500
501         void addWrappedCtor()
502         {
503                 // wrapped ctor..
504                 // this may need to look up properties to fill in the arguments..
505                 // introspection does not workk..... - as things like gtkmessagedialog
506                 /*
507                 if (cls == 'Gtk.Table') {
508
509                 var methods = this.palete.getPropertiesFor(cls, 'methods');
510
511                 print(JSON.stringify(this.palete.proplist[cls], null,4));
512                 Seed.quit();
513                 }
514                 */
515                 
516                 // ctor can still override.
517                 if (this.node.has("* ctor")) {
518                         this.node.setLine(this.cur_line, "p", "* ctor");
519                         this.addLine(this.ipad + "this.el = " + this.node.get("* ctor")+ ";");
520                         return;
521                 }
522                 
523                 this.node.setLine(this.cur_line, "p", "* xtype");;
524                 
525                 // is the wrapped element a struct?
526                 
527                 var ncls = Palete.Gir.factoryFqn((Project.Gtk) this.file.project, this.node.fqn());
528                 if (ncls != null && ncls.nodetype == "Struct") {
529                         // we can use regular setters to apply the values.
530                         this.addLine(this.ipad + "this.el = " + this.node.fqn() + "();");
531                         return;
532                 
533                 
534                 }
535
536                 var ctor = ".new";
537                 var args_str = "";
538                 switch(this.node.fqn()) {
539                 
540                 // FIXME -- these are all GTK3 - can be removed when I get rid of them..
541                         case "Gtk.ComboBox":
542                                 var is_entry = this.node.has("has_entry") && this.node.get_prop("has_entry").val.down() == "true";
543                                 if (!is_entry) { 
544                                         break; // regular ctor.
545                                 }
546                                 this.ignoreWrapped("has_entry");
547                                 ctor = ".with_entry";
548                                 break;
549                                 
550                 
551                         case "Gtk.ListStore":
552                         case "Gtk.TreeStore":
553
554                                 // not sure if this works.. otherwise we have to go with varargs and count + vals...
555                                 if (this.node.has("* types")) {
556                                         args_str = this.node.get_prop("* types").val;
557                                 }
558                                 if (this.node.has("n_columns") && this.node.has("columns")) { // old value?
559                                         args_str = " { " + this.node.get_prop("columns").val + " } ";
560                                         this.ignoreWrapped("columns");
561                                         this.ignoreWrapped("n_columns");
562                                 }
563                                 
564                                 this.addLine(this.ipad + "this.el = new " + this.node.fqn() + ".newv( " + args_str + " );");
565                                 return;
566  
567                                 
568                         case "Gtk.LinkButton": // args filled with values.
569                                 if (this.node.has("label")) {
570                                         ctor = ".with_label";    
571                                 }
572                                 break;
573                                 
574                         default:
575                                 break;
576                 }
577                 var default_ctor = Palete.Gir.factoryFqn((Project.Gtk) this.file.project, this.node.fqn() + ctor);              
578                  
579                 
580                 // use the default ctor - with arguments (from properties)
581                 
582                 if (default_ctor != null && default_ctor.paramset != null && default_ctor.paramset.params.size > 0) {
583                         string[] args  = {};
584                         foreach(var param in default_ctor.paramset.params) {
585                                  
586                                 var n = param.name;
587                             GLib.debug("building CTOR ARGS: %s, %s", n, param.is_varargs ? "VARARGS": "");
588                                 if (n == "___") { // for some reason our varargs are converted to '___' ...
589                                         continue;
590                                 }
591                                 
592                                 if (this.node.has(n)) {  // node does not have a value
593                                         
594                                         this.ignoreWrapped(n);
595                                         this.ignore(n);
596                                         
597                                         var v = this.node.get(n);
598
599                                         if (param.type == "string") {
600                                                 v = "\"" +  v.escape("") + "\"";
601                                         }
602                                         if (v == "TRUE" || v == "FALSE") {
603                                                 v = v.down();
604                                         }
605
606                                         
607                                         args += v;
608                                         continue;
609                                 }
610                                 var propnode = this.node.findProp(n);
611                                 if (propnode != null) {
612                                         // assume it's ok..
613                                         
614                                         var pname = this.addPropSet(propnode, propnode.has("id") ? propnode.get_prop("id").val : "");
615                                         args += (pname + ".el") ;
616                                         if (!propnode.has("id")) {
617                                                 this.addLine(this.ipad + pname +".ref();"); 
618                                         }
619                                         
620                                         
621                                         
622                                         this.ignoreWrapped(n);
623                                         
624                                         continue;
625                                 }
626                                         
627                                          
628                                         
629                                         
630                                  
631                                 if (param.type.contains("int")) {
632                                         args += "0";
633                                         continue;
634                                 }
635                                 if (param.type.contains("float")) {
636                                         args += "0f";
637                                         continue;
638                                 }
639                                 if (param.type.contains("bool")) {
640                                         args += "true"; // always default to true?
641                                         continue;
642                                 }
643                                 // any other types???
644                                 
645                                 
646                                 
647                                 
648                                 args += "null";
649                                  
650                                 
651
652                         }
653                         this.node.setLine(this.cur_line, "p", "* xtype");
654                         this.addLine(this.ipad + "this.el = new " + this.node.fqn() + "( "+ string.joinv(", ",args) + " );") ;
655                         return;
656                         
657                 }
658                 // default ctor with no params..
659                  if (default_ctor != null && ctor != ".new" ) {
660                         this.node.setLine(this.cur_line, "p", "* xtype");
661                         
662                         this.addLine(this.ipad + "this.el = new " + this.node.fqn() + ctor + "(  );") ;
663                         return;
664                  }
665                 
666                 
667                 this.addLine(this.ipad + "this.el = new " + this.node.fqn() + "(" + args_str + ");");
668                 
669                 
670
671                         
672         }
673         public static Gee.ArrayList<string> menuitem_children = null;
674         
675         void addAutoShow()
676         {
677                 if (menuitem_children == null) {
678                         menuitem_children = new Gee.ArrayList<string>();
679                         menuitem_children.add("Gtk.MenuItem");
680                         var gir = this.file.project.palete.getClass("Gtk.MenuItem");
681                         if (gir != null) {
682                             foreach(var impl in gir.implementations) {
683                                     menuitem_children.add(impl);
684                             }
685                     }
686                 }
687
688                 if (menuitem_children.contains(this.node.fqn())) {
689                         this.addLine(this.ipad + "this.el.show();");
690                 
691                 }
692         }
693
694         void addInitMyVars()
695         {
696                         //var meths = this.palete.getPropertiesFor(item['|xns'] + '.' + item.xtype, 'methods');
697                         //print(JSON.stringify(meths,null,4));Seed.quit();
698                         
699                         
700                         
701                         // initialize.. my vars..
702                 this.addLine();
703                 this.addLine( this.ipad + "// my vars (dec)");
704                 
705                 var iter = this.myvars.list_iterator();
706                 while(iter.next()) {
707                         
708                         var k = iter.get();
709                         
710                          
711                         var prop = this.node.props.get(k);
712                         
713                         var v = prop.val.strip();                       
714                         
715                         if (v.length < 1) {
716                                 continue; 
717                         }
718                         // at this point start using 
719
720                         if (v == "FALSE" || v == "TRUE") {
721                                 v= v.down();
722                         }
723                         //FIXME -- check for raw string.. "string XXXX"
724                         
725                         // if it's a string...
726                         
727                         prop.start_line = this.cur_line;
728                         this.addLine(this.ipad + "this." + prop.name + " = " +   v +";");
729                         prop.end_line = this.cur_line;
730                 }
731         }
732
733         
734
735
736         
737         void addWrappedProperties()
738         {
739                 var cls = Palete.Gir.factoryFqn((Project.Gtk) this.file.project, this.node.fqn());
740                 if (cls == null) {
741                         GLib.debug("Skipping wrapped properties - could not find class  %s" , this.node.fqn());
742                         return;
743                 }
744                         // what are the properties of this class???
745                 this.addLine();
746                 this.addLine(this.ipad + "// set gobject values");
747                 
748
749                 var iter = cls.props.map_iterator();
750                 while (iter.next()) {
751                         var p = iter.get_key();
752                         //print("Check Write %s\n", p);
753                         if (!this.node.has(p)) {
754                                 continue;
755                         }
756                         if (this.shouldIgnoreWrapped(p)) {
757                                 continue;
758                         }
759                         
760                         this.ignore(p);
761
762
763                         var prop = this.node.get_prop(p);
764                         var v = prop.val;
765                         
766                         // user defined properties.
767                         if (prop.ptype == NodePropType.USER) {
768                                 continue;
769                         }
770                                 
771
772                         
773                         var is_raw = prop.ptype == NodePropType.RAW;
774                         
775                         // what's the type.. - if it's a string.. then we quote it..
776                         if (iter.get_value().type == "string" && !is_raw) {
777                                  v = "\"" +  v.escape("") + "\"";
778                         }
779                         if (v == "TRUE" || v == "FALSE") {
780                                 v = v.down();
781                         }
782                         if (iter.get_value().type == "float" && v[v.length-1] != 'f') {
783                                 v += "f";
784                         }
785                         
786                         prop.start_line = this.cur_line;
787                         this.addLine("%sthis.el.%s = %s;".printf(ipad,p,v)); // // %s,  iter.get_value().type);
788                         prop.end_line = this.cur_line;          
789                            // got a property..
790                            
791
792                 }
793                 
794         }
795         /**
796          *  pack the children into the parent.
797          * 
798          * if the child's id starts with '*' then it is not packed...
799          * - this allows you to define children and add them manually..
800          */
801
802         void addChildren()
803         {
804                                 //code
805                 if (this.node.readItems().size < 1) {
806                         return;
807                 }
808                 this.pane_number = 0;
809                 var cols = this.node.has("* columns") ? int.max(1, int.parse(this.node.get_prop("* columns").val)) : 1;
810                 var colpos = 0;
811                 
812  
813                  
814                 foreach(var child in this.node.readItems()) {
815                         
816                         
817                          
818
819                         if (child.xvala_id[0] == '*') {
820                                 continue; // skip generation of children?
821                         }
822
823                         // probably added in ctor..                             
824                         if (child.has("* prop") && this.shouldIgnoreWrapped(child.get_prop("* prop").val)) {
825                                 continue;
826                         }
827                         // create the element..
828                         
829                         // this is only needed if it does not have an ID???
830                         var childname = this.addPropSet(child, child.has("id") ? child.get_prop("id").val : "") ; 
831                         
832                         if (child.has("* prop")) {
833                          
834                         
835                                 // fixme special packing!??!?!
836                                 if (child.get_prop("* prop").val.contains("[]")) {
837                                         // currently these 'child props
838                                         // used for label[]  on Notebook
839                                         // used for button[]  on Dialog?
840                                         // columns[] ?
841                                          
842                                         this.packChild(child, childname, 0, 0, child.get_prop("* prop").val);  /// fixme - this is a bit speciall...
843                                         continue;
844                                 }
845                                 
846         
847                                 
848                                 this.ignoreWrapped(child.get_prop("* prop").val);
849                                 
850                                 this.addLine(ipad + "this.el." + child.get_prop("* prop").val + " = " + childname + ".el;");
851                                 continue;
852                         } 
853                          if (!child.has("id")) {
854                                 this.addLine(this.ipad + childname +".ref();"); 
855                          } 
856                         this.packChild(child, childname, cols, colpos);
857                         
858                         if (child.has("colspan")) {
859                                 colpos += int.parse(child.get_prop("colspan").val);
860                         } else {
861                                 colpos += 1;
862                         }
863                                           
864                         
865                         // this.{id - without the '+'} = the element...
866                          
867                                   
868                 }
869         }
870         
871         string addPropSet(Node child, string child_name) 
872         {
873          
874                 
875                 var xargs = "";
876                 if (child.has("* args")) {
877                         
878                         var ar = child.get_prop("* args").val.split(",");
879                         for (var ari = 0 ; ari < ar.length; ari++ ) {
880                                 var arg = ar[ari].split(" ");
881                                 xargs += "," + arg[arg.length -1];
882                         }
883                 }
884                 
885                 var childname = "child_" + "%d".printf(this.child_count++);     
886                 var prefix = "";
887                 if (child_name == "") {
888                         prefix = "var " + childname + " = ";
889                 }
890                 
891                 this.addLine(this.ipad +  prefix + "new " + child.xvala_xcls + "( _this " + xargs + ");" );
892                  
893                 // add a ref... (if 'id' is not set... to a '+' ?? what does that mean? - fake ids?
894                 // remove '+' support as I cant remember what it does!!!
895                 //if (child.xvala_id.length < 1 ) {
896                 //      this.addLine(this.ipad + childname +".ref();"); // we need to reference increase unnamed children...
897                 //}                     
898             //if (child.xvala_id[0] == '+') {
899                 //      this.addLine(this.ipad + "this." + child.xvala_id.substring(1) + " = " + childname+  ";");
900                                         
901                 //}
902                 
903
904                 return child_name == "" ? childname : ("_this." + child_name);  
905         }               
906                         
907         
908
909         
910         void packChild(Node child, string childname, int cols, int colpos, string propname= "")
911         {
912                 
913                 GLib.debug("packChild %s=>%s", this.node.fqn(), child.fqn());
914                 // forcing no packing? - true or false? -should we just accept false?
915                 if (child.has("* pack") && child.get("* pack").down() == "false") {
916                         return; // force no packing
917                 }
918                 if (child.has("* pack") && child.get("* pack").down() == "true") {
919                         return; // force no packing
920                 }
921                 
922                 // BC really - don't want to support this anymore.
923                 if (child.has("* pack")) {
924                         
925                         string[]  packing =  { "add" };
926                         if (child.has("* pack")) {
927                                 packing = child.get("* pack").split(",");
928                         }
929                         
930                         var pack = packing[0];
931                         this.addLine(this.ipad + "this.el." + pack.strip() + " ( " + childname + ".el " +
932                                    (packing.length > 1 ? 
933                                                 (", " + string.joinv(",", packing).substring(pack.length+1))
934                                         :
935                                                         ""
936                                                 ) + " );");
937                         return;  
938                 }
939                 var childcls =  this.file.project.palete.getClass(child.fqn()); // very trusting..
940                 if (childcls == null) {
941                   return;
942                 }
943                 // GTK4
944                 var is_event = childcls.inherits.contains("Gtk.EventController") || childcls.implements.contains("Gtk.EventController");
945                 if (is_event) {
946                     this.addLine(this.ipad + "this.el.add_controller(  %s.el );".printf(childname) );
947                     return;
948                 }
949                 
950                 
951                 switch (this.node.fqn()) {
952                         
953                                 
954                 
955                         case "Gtk.Fixed":
956                         case "Gtk.Layout":
957                                 var x = child.has("x") ?  child.get_prop("x").val  : "0";
958                                 var y = child.has("y") ?  child.get_prop("y").val  : "0";
959                                 this.addLine(this.ipad + "this.el.put( %s.el, %s, %s );".printf(childname,x,y) );
960                                 return;
961                                 
962                         
963
964                         case "Gtk.Stack":
965                                 var named = child.has("stack_name") ?  child.get_prop("stack_name").val.escape() : "";
966                                 var title = child.has("stack_title") ?  child.get_prop("stack_title").val.escape()  : "";
967                                 if (title.length > 0) {
968                                         this.addLine(this.ipad + "this.el.add_titled( %s.el, \"%s\", \"%s\" );".printf(childname,named,title)); 
969                                 } else {
970                                         this.addLine(this.ipad + "this.el.add_named( %s.el, \"%s\" );".printf(childname,named));
971                                 }
972                                 return;
973                                 
974                         case "Gtk.Notebook": // use label
975                                 var label = child.has("notebook_label") ?  child.get_prop("notebook_label").val.escape() : "";
976                                 this.addLine(this.ipad + "this.el.append_page( %s.el, new Gtk.Label(\"%s\"));".printf(childname, label));       
977                                 return;
978                                 
979                          
980                         case "Gtk.TreeView": // adding TreeViewColumns
981                                 this.addLine(this.ipad + "this.el.append_column( " + childname + ".el );");
982                                 return;
983                         
984                         case "Gtk.TreeViewColumn": //adding Renderers - I think these are all proprerties of the renderer used...
985                                 if (child.has("markup_column") && int.parse(child.get_prop("markup_column").val) > -1) {
986                                         this.addLine(this.ipad + "this.el.add_attribute( %s.el, \"markup\", %s );".printf(childname, child.get_prop("markup_column").val));
987                                 }
988                                 if (child.has("text_column") && int.parse(child.get_prop("text_column").val) > -1) {
989                                         this.addLine(this.ipad + "this.el.add_attribute(  %s.el, \"text\", %s );".printf(childname, child.get_prop("text_column").val));
990                                 }
991                                 if (child.has("pixbuf_column") && int.parse(child.get_prop("pixbuf_column").val) > -1) {
992                                         this.addLine(this.ipad + "this.el.add_attribute(  %s.el, \"pixbuf\", %s );".printf(childname, child.get_prop("pixbuf_column").val));
993                                 }
994                                 if (child.has("pixbuf_column") && int.parse(child.get_prop("active_column").val) > -1) {
995                                         this.addLine(this.ipad + "this.el.add_attribute(  %s.el, \"active\", %s );".printf(childname, child.get_prop("active_column").val));
996                                 }
997                                 if (child.has("background_column") && int.parse(child.get_prop("background_column").val) > -1) {
998                                         this.addLine(this.ipad + "this.el.add_attribute(  %s.el, \"background-rgba\", %s );".printf(childname, child.get_prop("background_column").val));
999                                 }
1000                                 this.addLine(this.ipad + "this.el.add( " + childname + ".el );");
1001                                 // any more!?
1002                                 return;
1003                         
1004                         case "Gtk.Dialog":
1005                                 if (propname == "buttons[]") {
1006                                         var resp_id = int.parse(childname.replace("child_", ""));
1007                                         if (child.has("* response_id")) { 
1008                                                 resp_id = int.parse(child.get_prop("* response_id").val);
1009                                         }
1010                                         this.addLine(this.ipad + "this.el.add_action_widget( %s.el, %d);".printf(childname,resp_id) );
1011                                         return;
1012                                 }
1013                         
1014                                 
1015                                 this.addLine(this.ipad + "this.el.get_content_area().add( " + childname + ".el );");
1016                                 return;
1017
1018                 
1019                                 
1020                         
1021         
1022         
1023         // known working with GTK4 !
1024                         case "Gtk.HeaderBar": // it could be end... - not sure how to hanle that other than overriding the pack method?
1025                                 this.addLine(this.ipad + "this.el.pack_start( "+ childname + ".el );");
1026                                 return;
1027                         
1028                         case "GLib.Menu":
1029                                 this.addLine(this.ipad + "this.el.append_item( "+ childname + ".el );");
1030                                 return; 
1031                         
1032                         case "Gtk.Paned":
1033                                 this.pane_number++;
1034                                 switch(this.pane_number) {
1035                                         case 1:
1036                                                 this.addLine(this.ipad + "this.el.pack_start( %s.el );".printf(childname));
1037                                                 return;
1038                                         case 2:                                 
1039                                                 this.addLine(this.ipad + "this.el.pack_end( %s.el );".printf(childname));
1040                                                 return;
1041                                         default:
1042                                                 // do nothing
1043                                                 break;
1044                                 }
1045                                 return;
1046                         
1047                         case "Gtk.ColumnView":
1048                                 this.addLine(this.ipad + "this.el.append_column( "+ childname + ".el );");
1049                                 return;
1050                         
1051                         case "Gtk.Grid":
1052                                 var x = "%d".printf(colpos % cols);
1053                                 var y = "%d".printf(( colpos - (colpos % cols) ) / cols);
1054                                 var w = child.has("colspan") ? child.get_prop("colspan").val : "1";
1055                                 var h = "1";
1056                                 this.addLine(this.ipad + "this.el.attach( %s.el, %s, %s, %s, %s );".printf(childname ,x,y, w, h) );
1057                                 return;
1058                         
1059                         default:
1060                             // gtk4 uses append!!!! - gtk3 - uses add..
1061                                 this.addLine(this.ipad + "this.el.append( "+ childname + ".el );");
1062                                 return;
1063                 
1064                 
1065                 }
1066                 
1067                 
1068         }
1069         
1070         // fixme GtkDialog?!? buttons[]
1071         
1072         // fixme ... add case "Gtk.RadioButton":  // group_id ??
1073
1074                         
1075
1076         void addInit()
1077         {
1078
1079                 
1080                 if (!this.node.has("* init")) {
1081                                 return;
1082                 }
1083                 this.addLine();
1084                 this.addLine(ipad + "// init method");
1085                 this.addLine();
1086                 this.node.setLine(this.cur_line, "p", "init");
1087                 
1088                 var init =  this.node.get_prop("* init");
1089                 init.start_line = this.cur_line;
1090                 this.addMultiLine(ipad + this.padMultiline(ipad, init.val) );
1091                 init.end_line = this.cur_line;
1092          }
1093          void addListeners()
1094          {
1095                 if (this.node.listeners.size < 1) {
1096                         return;
1097                 }
1098                                 
1099                 this.addLine();
1100                 this.addLine(ipad + "//listeners");
1101                         
1102                          
1103
1104                 var iter = this.node.listeners.map_iterator();
1105                 while (iter.next()) {
1106                         var k = iter.get_key();
1107                         var prop = iter.get_value();
1108                         var v = prop.val;
1109                         
1110                         prop.start_line = this.cur_line;
1111                         this.node.setLine(this.cur_line, "l", k);
1112                         this.addMultiLine(this.ipad + "this.el." + k + ".connect( " + 
1113                                         this.padMultiline(this.ipad,v) +");"); 
1114                         prop.end_line = this.cur_line;
1115                 }
1116         }    
1117         void addEndCtor()
1118         {
1119                          
1120                         // end ctor..
1121                         this.addLine(this.pad + "}");
1122         }
1123
1124
1125         /*
1126  * Standardize this crap...
1127  * 
1128  * standard properties (use to set)
1129  *          If they are long values show the dialog..
1130  *
1131  * someprop : ....
1132  * bool is_xxx  :: can show a pulldown.. (true/false)
1133  * string html  
1134  * $ string html  = string with value interpolated eg. baseURL + ".." 
1135  *  Clutter.ActorAlign x_align  (typed)  -- shows pulldowns if type is ENUM? 
1136  * $ untypedvalue = javascript untyped value...  
1137  * _ string html ... = translatable..
1138
1139  * 
1140  * object properties (not part of the GOjbect being wrapped?
1141  * # Gee.ArrayList<Xcls_fileitem> fileitems
1142  * 
1143  * signals
1144  * @ void open 
1145  * 
1146  * methods -- always text editor..
1147  * | void clearFiles
1148  * | someJSmethod
1149  * 
1150  * specials
1151  * * prop -- string
1152  * * args  -- string
1153  * * ctor -- string
1154  * * init -- big string?
1155  * 
1156  * event handlers (listeners)
1157  *   just shown 
1158  * 
1159  * -----------------
1160  * special ID values
1161  *  +XXXX -- indicates it's a instance property / not glob...
1162  *  *XXXX -- skip writing glob property (used as classes that can be created...)
1163  * 
1164  * 
1165  */
1166          
1167         void addUserMethods()
1168         {
1169                 this.addLine();
1170                 this.addLine(this.pad + "// user defined functions");
1171                         
1172                         // user defined functions...
1173                 var iter = this.node.props.map_iterator();
1174                 while(iter.next()) {
1175                         var prop = iter.get_value();
1176                         if (this.shouldIgnore(prop.name)) {
1177                                 continue;
1178                         }
1179                         // HOW TO DETERIME if its a method?            
1180                         if (prop.ptype != NodePropType.METHOD) {
1181                                         //strbuilder("\n" + pad + "// skip " + k + " - not pipe \n"); 
1182                                         continue;
1183                         }
1184                         
1185                         // function in the format of {type} (args) { .... }
1186
1187
1188
1189                         prop.start_line = this.cur_line;
1190                         this.node.setLine(this.cur_line, "p", prop.name);
1191                         this.addMultiLine(this.pad + "public " + prop.rtype + " " +  prop.name + " " + this.padMultiline(this.pad, prop.val));;
1192                         prop.end_line = this.cur_line;
1193                                 
1194                 }
1195         }
1196
1197         void iterChildren()
1198         {
1199                 this.node.line_end = this.cur_line;
1200                 this.node.sortLines();
1201                 
1202                         
1203                 if (this.depth > 0) {
1204                         this.addLine(this.inpad + "}");
1205                 }
1206                 
1207                 var iter = this.node.readItems().list_iterator();
1208                  
1209                 while (iter.next()) {
1210                         this.addMultiLine(this.mungeChild(iter.get()));
1211                 }
1212                          
1213                 if (this.depth < 1) {
1214                         this.addLine(this.inpad + "}");
1215                 }
1216                         
1217         }
1218
1219         string padMultiline(string pad, string str)
1220         {
1221                 var ar = str.strip().split("\n");
1222                 return string.joinv("\n" + pad , ar);
1223         }
1224         
1225         void ignore(string i) {
1226                 this.ignoreList.add(i);
1227                 
1228         }
1229         void ignoreWrapped(string i) {
1230                 this.ignoreWrappedList.add(i);
1231                 
1232         }
1233         bool shouldIgnore(string i)
1234         {
1235                 return ignoreList.contains(i);
1236         }
1237         bool shouldIgnoreWrapped(string i)
1238         {
1239                 return ignoreWrappedList.contains(i);
1240         }
1241         
1242 }
1243         
1244          
1245         
1246