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