Test.GtkWriter.vala.c
[app.Builder.js] / JsRender / Gtk.js
1 //<Script type="text/javascript">
2  
3 Gio = imports.gi.Gio;
4 GLib = imports.gi.GLib;
5
6 XObject = imports.XObject.XObject;
7 File = imports.File.File;
8   
9 //----------------------- our roo verison
10 Base = imports.JsRender.Base.Base;
11
12 var gid = 1;
13
14 // ctors needed for Constructing vala?? - 
15
16 var ctors = {
17     "Gtk.Label": [ "label" ],
18     "Gtk.Box": [ "orientation", "spacing:0" ],
19     "Gtk.MessageDialog" : [ "parent:null", "flags:Gtk.DialogFlags.MODAL", "message_type",  "buttons", "text" ],
20     "Gtk.ScrolledWindow": [ "hadjustment:null", "vadjustment:null" ],
21     "Gtk.SourceBuffer": [ "table:null" ],
22     "Gtk.Table": [ "n_rows", "n_columns" , "homogeneous" ],
23     "Gtk.ToolButton": [ "icon_widget:null", "label:null" ],
24     "Gtk.HBox": [ "homogeneous:true", "spacing:0" ],
25     "Gtk.VBox": [ "homogeneous:true", "spacing:0" ],
26     "Gtk.ListStore": [ "n_columns", "columns" ],
27     "Gtk.FileChooserWidget" : [ "action"],
28     //"Gtk.Entry": [  ],
29 };
30
31
32
33 Gtk = XObject.define( 
34     
35     
36     function(cfg) {
37         
38         // id ,
39         //"name":"Edit Module Details",
40         // items : 
41         //"btype":"FORM", // was to be components...
42         //"app":"Builder",
43         //"module":"Pman.Tab.BuilderTop2"
44         //console.dump(cfg);
45         cfg.parent = cfg.parent || '';
46         if (!cfg.name || !cfg.fullname ) {
47             
48             // name is in theory filename without .bjs (or .js eventually...)
49             cfg.name = cfg.path.split('/').pop().replace(/\.(bjs|js)$/, '');
50             
51             cfg.fullname = (cfg.parent.length ? (cfg.parent + '.') : '' ) + cfg.name;
52         }
53         
54         
55         this.items = false;
56         if (cfg.json) {
57             var jstr =  JSON.parse(cfg.json);
58             this.items = [ jstr ];
59             //console.log(cfg.items.length);
60             delete cfg.json; // not needed!
61         }
62         this.cn = [];
63          /*
64         var p = cfg.items && cfg.items.length && cfg.items[0].parent ? cfg.items[0].parent : false;
65         
66         // ensure real data is set...
67         Roo.apply(this, {
68             name : cfg.module,
69             parent : p,
70             title : cfg.name,
71             project : cfg.app
72             
73         });
74         
75         this.cn = [];
76         */
77         Gtk.superclass.constructor.call(this, cfg);
78
79         
80         // super?!?!
81         this.id = 'file-gtk-' + gid++;
82         //console.dump(this);
83         // various loader methods..
84        
85     },
86     Base,   
87     {
88         xtype : 'Gtk',
89         setNSID : function(id)
90         {
91             
92             this.items[0]['*class'] = id;
93             
94             
95         },
96         getType: function() {
97             return 'Gtk';
98         },
99         
100         loadItems : function(cb)
101         {
102           
103             print("load Items!");
104             if (this.items !== false) {
105                 return false;
106             }
107             if (!cb) {  
108                 throw {
109                     name: "ArgumentError", 
110                     message : "no callback for loadItems"
111                 };
112             }
113             Seed.print("load: " + this.path);
114             
115             
116
117             
118             var _this = this;     
119             var src = File.read(this.path);
120             
121             var cfg = JSON.parse(src);
122             print("loaded data");
123             //console.dump(cfg);
124             
125             //_this.name = cfg.name; -- this should not happen - name should always match filename!!
126             _this.parent =  cfg.parent;
127             _this.title =  cfg.title;
128             _this.items = cfg.items || []; 
129            
130              cb();
131              
132              
133             
134             
135             
136         },
137         /**
138          * convert xtype for munged output..
139          * 
140          */
141         mungeXtype : function(xtype, els)
142         {
143             els.push('xtype: '+ xtype);
144         },
145         
146         toSource : function()
147         {
148             
149             if (!this.items[0]) {
150                 return false;
151             }
152             var data = JSON.parse(JSON.stringify(this.items[0]));
153             // we should base this on the objects in the tree really..
154             var i = [ 'Gtk', 'Gdk', 'Pango', 'GLib', 'Gio', 'GObject', 
155                 'GtkSource', 'WebKit', 'Vte' ]; //, 'GtkClutter' , 'Gdl'];
156             var src = "";
157             i.forEach(function(e) {
158                 src += e+" = imports.gi." + e +";\n";
159             });
160             
161             src += "console = imports.console;\n"; // path?!!?
162             src += "XObject = imports.XObject.XObject;\n"; // path?!!?
163             
164             
165             src += this.name + '=new XObject('+ this.mungeToString(data) + ");\n";
166             src += this.name + '.init();\n';
167             // register it in the cache
168             src += "XObject.cache['/" + this.name + "'] = " + this.name + ";\n";
169             
170             
171             return src;
172             
173             
174         },
175         save : function() {
176             Base.prototype.save.call(this);
177             this.saveJS();
178             this.saveVala();
179         },
180         
181         /** 
182          *  saveJS
183          * 
184          * save as a javascript file.
185          * why is this not save...???
186          * 
187          * 
188          */
189         saveJS: function()
190         {
191              
192             var fn = GLib.path_get_dirname(this.path) + '/' + this.name + '.js';
193             print("WRITE : " + fn);
194             File.write(fn, this.toSource());
195             
196             return fn;
197         },
198         
199         saveVala: function()
200         {
201              
202             var fn = GLib.path_get_dirname(this.path) + '/' + this.name + '.vala';
203             print("WRITE : " + fn);
204             File.write(fn, this.toVala());
205             
206             return fn;
207         },
208         valaCompileCmd : function()
209         {
210             
211             var fn = '/tmp/' + this.name + '.vala';
212             print("WRITE : " + fn);
213             File.write(fn, this.toVala(true));
214             
215             
216             
217             return ["valac",
218                    "--pkg",  "gio-2.0",
219                    "--pkg" , "posix" ,
220                    "--pkg" , "gtk+-3.0",
221                    "--pkg",  "libnotify",
222                    "--pkg",  "gtksourceview-3.0",
223                    "--pkg", "libwnck-3.0",
224                    fn ,   "-o", "/tmp/" + this.name];
225             
226            
227              
228             
229         },
230         
231         
232         /*
233         getTree : function( o ) {
234             
235             
236             
237         }
238         */
239         getHelpUrl : function(cls)
240         {
241             return 'http://devel.akbkhome.com/seed/' + cls + '.html';
242         },
243         
244         vcnt : false,
245         
246         toVala: function(testcompile)
247         {
248             var ret = '';
249             testcompile = testcompile || false;
250             
251             this.vcnt = 0;
252             //print(JSON.stringify(this.items[0],null,4));
253             //print(JSON.stringify(this.items[0],null,4));Seed.quit();
254
255             var item=  XObject.xclone(this.items[0]);
256             if (!item.id) {
257                 item.id = this.name;
258                 
259             }
260             
261             print(JSON.stringify(item,null,4));
262             
263             this.palete  = new imports.Palete.Gtk.Gtk({});
264             
265             this.vitems = [];
266             this.toValaName(item);
267            // print(JSON.stringify(item,null,4));Seed.quit();
268             
269             ret += "/* -- to compile\n";
270             ret += "valac  --pkg gio-2.0  --pkg posix  --pkg gtk+-3.0 --pkg libnotify --pkg gtksourceview-3.0  --pkg  libwnck-3.0 \\\n";
271             //ret += "    " + item.xvala_id + ".vala  -o /tmp/" + item.xvala_id +"\n";
272             ret += "    /tmp/" + this.name + ".vala  -o /tmp/" + this.name +"\n";
273             ret += "*/\n";
274             ret += "\n\n";
275             if (!testcompile) {
276                 ret += "/* -- to test class\n";  
277             }
278             //
279             ret += "static int main (string[] args) {\n";
280             ret += "    Gtk.init (ref args);\n";
281             ret += "    new " + item.xvala_xcls +"();\n";
282             ret += "    " + this.name +".show_all();\n";
283             ret += "     Gtk.main ();\n";
284             ret += "    return 0;\n";
285             ret += "}\n";
286             if (!testcompile) {
287                 ret += "*/\n";
288             }
289             ret += "\n\n";
290             
291             
292             // print(JSON.stringify(item,null,4));
293             this.toValaItem(item,0, function(s) {
294                 ret+= s;
295             });
296             
297             return ret;
298             
299         },
300         
301         toValaNS : function(item)
302         {
303             var ns = item['|xns'] ;
304             if (ns == 'GtkSource') {
305                 return 'Gtk.Source'
306                
307             }
308             return ns + '.';
309         },
310         
311         toValaName : function(item) {
312             this.vcnt++;
313             var cls = this.toValaNS(item) + item.xtype;  // eg. Gtk.Window
314             var id = item.id ? item.id : (item.xtype + this.vcnt);
315             var props = this.palete.getPropertiesFor(cls, 'props');
316             
317             
318             
319             item.xvala_cls = cls;
320             item.xvala_xcls = 'Xcls_' + id;
321             item.xvala_id = item.id ? item.id : false;
322             this.vitems.push(item);  
323             // loop children..
324             if (typeof(item.items) == 'undefined') {
325                 return;
326             }
327             for(var i =0;i<item.items.length;i++) {
328                 this.toValaName(item.items[i]);
329             }
330           
331         },
332         
333         
334         toValaItem : function(item, depth, strbuilder)
335         {
336         // print(JSON.stringify(item,null,4));
337             
338             var inpad = new Array( depth +1 ).join("    ");
339             
340             var pad = new Array( depth +2 ).join("    ");
341             var ipad = new Array( depth +3 ).join("    ");
342             
343             var cls = item.xvala_cls;
344             
345             var xcls = item.xvala_xcls;
346             
347             var citems = {};
348             
349             if (!depth) {
350                 // Global Vars..
351                 strbuilder(inpad + "public static " + xcls + "  " + this.name + ";\n\n");
352                  
353                 
354             }
355             
356             // class header..
357             // class xxx {   WrappedGtk  el; }
358             strbuilder(inpad + "public class " + xcls + "\n" + inpad + "{\n");
359             strbuilder(pad + "public " + cls + " el;\n");
360              if (!depth) {
361                 
362                 strbuilder(pad + "private static " + xcls + "  _this;\n\n");
363             }
364             
365             
366             // properties??
367                 
368                 //public bool paused = false;
369                 //public static StatusIconA statusicon;
370             if (!depth) {
371                 //strbuilder(pad + "public static " + xcls + "  _this;\n");
372                 for(var i=1;i < this.vitems.length; i++) {
373                     if (this.vitems[i].xvala_id  !== false) {
374                         strbuilder(pad + "public " + this.vitems[i].xvala_xcls + " " + this.vitems[i].xvala_id + ";\n");
375                     }
376                 }
377                 
378             }
379             
380             strbuilder("\n" + ipad + "// my vars\n");
381             
382             
383             // Key = TYPE:name
384             for (var k in item) {
385                 if (k[0] != '.') {
386                    
387                     continue;
388                 }
389                 if (k == '.ctor') {
390                     continue; 
391                 }
392                 
393                 var kk = k.substring(1);
394                 
395                 var vv = kk.split(':');
396                 if (vv[0] == 'signal') {
397                     strbuilder(pad + "public " + vv[0] + " " + vv[1] + " " + vv[2] + item[k] + ";\n");
398                 } else {
399                 
400                     strbuilder(pad + "public " + vv[0] + " " + vv[1] + ";\n");
401                 }
402                 citems[k] = true; 
403                 
404             }
405             // .vala props.. 
406              
407             // ctor..
408             strbuilder("\n" + ipad + "// ctor \n");
409             strbuilder(pad + "public " + xcls + "()\n" + pad + "{\n");
410             
411             // wrapped ctor..
412             // this may need to look up properties to fill in the arguments..
413             // introspection does not workk..... - as things like gtkmessagedialog
414             /*
415             if (cls == 'Gtk.Table') {
416                 
417                 var methods = this.palete.getPropertiesFor(cls, 'methods');
418                 
419                 print(JSON.stringify(this.palete.proplist[cls], null,4));
420                 Seed.quit();
421             }
422             */
423             
424             
425             if (typeof(ctors[cls]) !== 'undefined') {
426                 var args = [];
427                 for(var i =0;i< ctors[cls].length;i++) {
428                     
429                     var nv = ctors[cls][i].split(':');
430                     
431                     if (typeof(item[nv[0]]) != 'undefined' && typeof(item[nv[0]]) != 'object' ) {
432                         citems[nv[0]] = true;
433                         args.push(JSON.stringify(item[nv[0]]));
434                         continue;
435                     }
436                     if (typeof(item['|' + nv[0]]) != 'undefined' && typeof(item['|' + nv[0]]) != 'object' ) {
437                         citems[nv[0]] = true;
438                         citems['|' + nv[0]] = true;
439                         args.push(item['|' + nv[0]]);
440                         continue;
441                     }
442                     args.push(nv.length > 1 ? nv[1] : 'null'); 
443                     
444                 }
445                 strbuilder(ipad + "this.el = new " + cls + "( "+ args.join(", ") + " );\n" );
446
447             } else {
448                 strbuilder(ipad + "this.el = new " + cls + "();\n" );
449
450             }
451             //var meths = this.palete.getPropertiesFor(item['|xns'] + '.' + item.xtype, 'methods');
452             //print(JSON.stringify(meths,null,4));Seed.quit();
453             
454              
455             
456             // public static?
457             if (!depth) {
458                 strbuilder(ipad + "_this = this;\n");
459                 strbuilder(ipad + this.name  + " = this;\n");
460             } else {
461                 if (item.xvala_id !== false) {
462                     strbuilder(ipad + "_this." + item.xvala_id  + " = this;\n");
463                    
464                 }
465                 
466                 
467             }
468             // initialize.. my vars..
469             strbuilder("\n" + ipad + "// my vars\n");
470             for (var k in item) {
471                 if (k[0] != '.') {
472                     continue;
473                 }
474                 var kk = k.substring(1);
475                 var v = item[k];
476                 var vv = kk.split(':');
477                 if (v.length < 1 || vv[0] == "signal") {
478                     continue;
479                 }
480                 strbuilder(ipad + "this." + vv[1] + " = " +   v +";\n");
481                 
482             }
483            
484            
485             // what are the properties of this class???
486             strbuilder("\n" + ipad + "// set gobject values\n");
487             var props = this.palete.getPropertiesFor(item['|xns'] + '.' + item.xtype, 'props');
488             
489             
490             
491             props.forEach(function(p) {
492                
493                 if (typeof(citems[p.name]) != 'undefined') {
494                     return;
495                 }
496                      
497                 if (typeof(item[p.name]) != 'undefined' && typeof(item[p.name]) != 'object' ) {
498                     citems[p.name] = true;
499                     
500                     var val = JSON.stringify(item[p.name]);
501                     if (['xalign','yalign'].indexOf(p.name) > -1) {
502                         val +='f';
503                     }
504                     strbuilder(ipad + "this.el." + p.name + " = " + val + ";\n");
505                     return;
506                 }
507                 if (typeof(item['|' + p.name]) != 'undefined' && typeof(item['|' + p.name]) != 'object' ) {
508                     citems['|' + p.name] = true;
509                     //if (p.ctor_only ) {
510                     //    strbuilder(ipad + "Object(" + p.name + " : " +  item['|' + p.name] + ");\n");
511                     //} else {
512                         strbuilder(ipad + "this.el." + p.name + " = " +  item['|' + p.name] + ";\n");
513                     //}
514                     return;
515                 }
516                // got a property..
517                
518                
519             });
520                 //code
521             // add all the child items..
522             if (typeof(item.items) != 'undefined') {
523                 for(var i =0;i<item.items.length;i++) {
524                     var ci = item.items[i];
525                     
526                     var packing = ci.pack ? ci.pack.split(',') : [ 'add' ];
527                     if (typeof(ci['|pack']) != 'undefined') {
528                         packing =ci['|pack'].split(',');
529                     }
530                     var pack = packing.shift();
531                     strbuilder(ipad + "var child_" + i + " = new " + ci.xvala_xcls + "();\n" )
532                     
533                     strbuilder(ipad + "this.el." + pack + " (  child_" + i + ".el " +
534                                (packing.length ? ", " + packing.join(",") : "") + " );\n"
535                             );
536                                
537                     
538                 }
539             }
540             if (typeof(item['|init']) != 'undefined') {
541                 
542                 
543                     var v = item['|init'].split(/\/*--/);
544                     if (v.length > 1) {
545                         strbuilder("\n" + ipad + "// init method \n");            
546                          var vv = v[1].replace('*/', "");
547                          //print(JSON.stringify(vv));Seed.quit();
548                          vv = vv.replace(/^\n+/,'');
549                          vv = vv.replace(/\n+$/,'');
550                          vv = vv.replace(/\n/g,"\n" + ipad);
551                          strbuilder(ipad + vv  + "\n");
552                     }
553             }
554             
555             citems['|pack'] = true;
556             citems['|items'] = true;
557              citems['|init'] = true;
558             
559             if (item.listeners) {
560             //    print(JSON.stringify(item.listeners));Seed.quit();
561             
562                 strbuilder("\n" + ipad + "// listeners \n");  
563                 // add all the signal handlers..
564                 for (var k in item.listeners) {
565                     
566                     
567                     var v = item.listeners[k] ;
568                     
569                     var vv = v.replace(/\n/g,"\n" + ipad);
570                         
571                         
572                     
573                     strbuilder(ipad + "this.el." + k + ".connect( " + vv  + " );\n");
574                     
575                 }
576             }    
577                 
578             
579             
580             
581             // end ctor..
582             strbuilder(pad + "}\n");
583             
584             
585             strbuilder("\n" + pad + "// userdefined functions \n");  
586             
587             // user defined functions...
588             
589             for (var k in item) {
590                 if (typeof(citems[k]) != 'undefined') {
591                     strbuilder("\n" + pad + "// skip " + k + " - already used \n"); 
592                     continue;
593                 }
594                 if (k[0] != '|') {
595                       strbuilder("\n" + pad + "// skip " + k + " - not pipe \n"); 
596                     continue;
597                 }
598                 // function in the format of {type} (args) { .... }
599                  
600                 var vv = item[k];
601                 //print(JSON.stringify(vv));Seed.quit();
602                 vv = vv.replace(/^\n+/,'');
603                 vv = vv.replace(/\n+$/,'');
604                 vv = vv.replace(/\n/g,"\n" + ipad);
605                 
606                 var vva = k.split(':');
607                 if (vva.length  < 2) {
608                     strbuilder("\n" + pad + "// skip " + k + " - no return type\n"); 
609                     continue;
610                 }
611                 var rtype = vva.shift().substring(1);
612                 var body = vv;
613                 var fname = vva.shift() || '???';
614                 
615                 strbuilder(pad + "public " + rtype + " " + fname + body + "\n");
616                 
617                 
618                 
619             }
620             
621             
622             
623             if (depth > 0) {
624                 strbuilder(inpad + "}\n");
625             }
626             
627             
628             // next loop throug children..
629             if (typeof(item.items) != 'undefined') {
630                 for(var i =0;i<item.items.length;i++) {
631                     this.toValaItem(item.items[i], 1, strbuilder);
632                 }
633             }
634             if (depth < 1) {
635                 strbuilder(inpad + "}\n");
636             }
637             
638         }
639         
640         
641         
642     });