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