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