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             var fn = GLib.path_get_dirname(this.path) + '/' + this.name + '.vala';
192             return ["valac",
193                    "--pkg",  "gio-2.0",
194                    "--pkg" , "posix" ,
195                    "--pkg" , "gtk+-3.0",
196                    "--pkg",  "libnotify",
197                    "--pkg",  "gtksourceview-3.0",
198                    "--pkg", "libwnck-3.0",
199                    fn ,   "-o", "/tmp/" + this.name];
200             
201            
202              
203             
204         },
205         
206         
207         /*
208         getTree : function( o ) {
209             
210             
211             
212         }
213         */
214         getHelpUrl : function(cls)
215         {
216             return 'http://devel.akbkhome.com/seed/' + cls + '.html';
217         },
218         
219         vcnt : false,
220         
221         toVala: function(testcompile)
222         {
223             var ret = '';
224             testcompile = testcompile || false;
225             
226             this.vcnt = 0;
227             //print(JSON.stringify(this.items[0],null,4));
228             //print(JSON.stringify(this.items[0],null,4));Seed.quit();
229
230             var item=  XObject.xclone(this.items[0]);
231             
232             print(JSON.stringify(item,null,4));
233             
234             this.palete  = new imports.Palete.Gtk.Gtk({});
235             
236             this.vitems = [];
237             this.toValaName(item);
238            // print(JSON.stringify(item,null,4));Seed.quit();
239             
240             ret += "/* -- to compile\n";
241             ret += "valac  --pkg gio-2.0  --pkg posix  --pkg gtk+-3.0 --pkg libnotify --pkg gtksourceview-3.0  --pkg  libwnck-3.0 \\\n";
242             //ret += "    " + item.xvala_id + ".vala  -o /tmp/" + item.xvala_id +"\n";
243             ret += "    /tmp/test.vala  -o /tmp/" + item.xvala_id +"\n";
244             ret += "*/\n";
245             ret += "\n\n";
246             if (!testcompile) {
247                 ret += "/* -- to test class\n";  
248             }
249             //
250             ret += "static int main (string[] args) {\n";
251             ret += "    Gtk.init (ref args);\n";
252             ret += "    new " + item.xvala_xcls +"();\n";
253             ret += "    " + item.xvala_id +".show_all();\n";
254             ret += "     Gtk.main ();\n";
255             ret += "    return 0;\n";
256             ret += "}\n";
257             //ret += "*/\n";
258             ret += "\n\n";
259             // print(JSON.stringify(item,null,4));
260             this.toValaItem(item,0, function(s) {
261                 ret+= s;
262             });
263             
264             return ret;
265             
266         },
267         
268         toValaNS : function(item)
269         {
270             var ns = item['|xns'] ;
271             if (ns == 'GtkSource') {
272                 return 'Gtk.Source'
273                 ns = 'Gtk.Source';
274             }
275             return ns + '.';
276         },
277         
278         toValaName : function(item) {
279             this.vcnt++;
280             var cls = this.toValaNS(item) + item.xtype;
281             var id = item.id ? item.id : (item.xtype + this.vcnt);
282             var props = this.palete.getPropertiesFor(cls, 'props');
283             
284             
285             
286             item.xvala_cls = cls;
287             item.xvala_xcls = 'Xcls_' + id;
288             item.xvala_id = item.id ? item.id : false;
289             this.vitems.push(item);  
290             // loop children..
291             if (typeof(item.items) == 'undefined') {
292                 return;
293             }
294             for(var i =0;i<item.items.length;i++) {
295                 this.toValaName(item.items[i]);
296             }
297           
298         },
299         
300         
301         toValaItem : function(item, depth, strbuilder)
302         {
303         // print(JSON.stringify(item,null,4));
304             
305             var inpad = new Array( depth +1 ).join("    ");
306             
307             var pad = new Array( depth +2 ).join("    ");
308             var ipad = new Array( depth +3 ).join("    ");
309             
310             var cls = item.xvala_cls;
311             
312             var xcls = item.xvala_xcls;
313             
314             var citems = {};
315             
316             if (!depth) {
317                 strbuilder(inpad + "public static " + xcls + "  " + item.id + ";\n\n");
318                  strbuilder(inpad + "private static " + xcls + "  _this;\n\n");
319                  
320                 
321             }
322             
323             // class header..
324             strbuilder(inpad + "public class " + xcls + " : " + cls + "\n" + inpad + "{\n");
325             
326             // properties??
327                 
328                 //public bool paused = false;
329                 //public static StatusIconA statusicon;
330             if (!depth) {
331                 //strbuilder(pad + "public static " + xcls + "  _this;\n");
332                 for(var i=1;i < this.vitems.length; i++) {
333                     if (this.vitems[i].xvala_id  !== false) {
334                         strbuilder(pad + "public " + this.vitems[i].xvala_xcls + " " + this.vitems[i].xvala_id + ";\n");
335                     }
336                 }
337                 
338             }
339             
340             strbuilder("\n" + ipad + "// my vars\n");
341             
342             
343             for (var k in item) {
344                 if (k[0] != '.') {
345                    
346                     continue;
347                 }
348                 var kk = k.substring(1);
349                 var v = item[k];
350                 var vv = v.split(':');
351                 strbuilder(pad + "public " + vv[0] + " " + kk + ";\n");
352                  citems[k] = true; 
353                 
354             }
355             // .vala props.. 
356              
357             // ctor..
358             strbuilder("\n" + ipad + "// ctor \n");
359             strbuilder(pad + "public " + xcls + "()\n" + pad + "{\n");
360             
361             // public static?
362             if (!depth) {
363                 strbuilder(ipad + "_this = this;\n");
364                 strbuilder(ipad +  item.id  + " = this;\n");
365             } else {
366                 if (item.xvala_id !== false) {
367                     strbuilder(ipad + "_this." + item.xvala_id  + " = this;\n");
368                    
369                 }
370                 
371                 
372             }
373             // initialize.. my vars..
374             strbuilder("\n" + ipad + "// my vars\n");
375             for (var k in item) {
376                 if (k[0] != '.') {
377                     continue;
378                 }
379                 var kk = k.substring(1);
380                 var v = item[k];
381                 var vv = v.split(':');
382                 if (vv.length < 2) {
383                     continue;
384                 }
385                 strbuilder(ipad + "this" + k + " = " +   vv[1] +";\n");
386                 
387             }
388            
389            
390             // what are the properties of this class???
391             strbuilder("\n" + ipad + "// set gobject values\n");
392             var props = this.palete.getPropertiesFor(item['|xns'] + '.' + item.xtype, 'props');
393             
394             
395             
396             props.forEach(function(p) {
397                 if (typeof(item[p.name]) != 'undefined' && typeof(item[p.name]) != 'object' ) {
398                     citems[p.name] = true; 
399                     strbuilder(ipad + "this." + p.name + " = " + JSON.stringify(item[p.name]) + ";\n");
400                     return;
401                 }
402                 if (typeof(item['|' + p.name]) != 'undefined' && typeof(item['|' + p.name]) != 'object' ) {
403                     citems['|' + p.name] = true;
404                     strbuilder(ipad + "this." + p.name + " = " +  item['|' + p.name] + ";\n");
405                     return;
406                 }
407                // got a property..
408                
409                
410             });
411                 //code
412             // add all the child items..
413             if (typeof(item.items) != 'undefined') {
414                 for(var i =0;i<item.items.length;i++) {
415                     var ci = item.items[i];
416                     var packing = ci.pack ? ci.pack.split(',') : [ 'add' ];
417                     var pack = packing.shift();
418                     strbuilder(ipad + "this." + pack + " (  new " + ci.xvala_xcls + "()" +
419                                (packing.length ? ", " + packing.join(",") : "") + " );\n"
420                             );
421                                
422                     
423                 }
424             }
425             if (typeof(item['|init']) != 'undefined') {
426                 
427                 
428                     var v = item['|init'].split(/\/*--/);
429                     if (v.length > 1) {
430                         strbuilder("\n" + ipad + "// init method \n");            
431                          var vv = v[1].replace('*/', "");
432                          //print(JSON.stringify(vv));Seed.quit();
433                          vv = vv.replace(/^\n+/,'');
434                          vv = vv.replace(/\n+$/,'');
435                          vv = vv.replace(/\n/g,"\n" + ipad);
436                          strbuilder(ipad + vv  + "\n");
437                     }
438             }
439             
440             citems['|pack'] = true;
441             citems['|items'] = true;
442              citems['|init'] = true;
443             
444             if (item.listeners) {
445             //    print(JSON.stringify(item.listeners));Seed.quit();
446             
447                 strbuilder("\n" + ipad + "// listeners \n");  
448                 // add all the signal handlers..
449                 for (var k in item.listeners) {
450                     
451                     
452                     var v = item.listeners[k].split(/\/*--/);
453                     if (v.length < 2) {
454                         continue;
455                     }
456                     var vv = v[1].replace('*/', "");
457                     //print(JSON.stringify(vv));Seed.quit();
458                     vv = vv.replace(/^\n+/,'');
459                     vv = vv.replace(/\n+$/,'');
460                     vv = vv.replace(/\n/g,"\n" + ipad);
461                     strbuilder(ipad + "this." + k + ".connect( " + vv  + " );\n");
462                     
463                 }
464             }    
465                 
466             
467             
468             
469             // end ctor..
470             strbuilder(pad + "}\n");
471             
472             
473             strbuilder("\n" + pad + "// userdefined functions \n");  
474             
475             // user defined functions...
476             
477             for (var k in item) {
478                 if (typeof(citems[k]) != 'undefined') {
479                     //strbuilder("\n" + pad + "// skip " + k + " - already used \n"); 
480                     continue;
481                 }
482                 if (k[0] != '|') {
483                     // strbuilder("\n" + pad + "// skip " + k + " - not pipe \n"); 
484                     continue;
485                 }
486                 // function in the format of {type} (args) { .... }
487                 
488                 var v = item[k].split(/\/*--/);
489                 if (v.length < 2) {
490                     continue;
491                 }
492                 var vv = v[1].replace('*/', "");
493                 //print(JSON.stringify(vv));Seed.quit();
494                 vv = vv.replace(/^\n+/,'');
495                 vv = vv.replace(/\n+$/,'');
496                 vv = vv.replace(/\n/g,"\n" + ipad);
497                 
498                 vva = vv.split(' ');
499                 var rtype = vva.shift();
500                 var body = vva.join(' ');
501                 
502                 
503                 strbuilder(pad + "public " + rtype + " " + k.substring(1) +body + "\n");
504                 
505                 
506                 
507             }
508             
509             
510             
511             if (depth > 0) {
512                 strbuilder(inpad + "}\n");
513             }
514             
515             
516             // next loop throug children..
517             if (typeof(item.items) != 'undefined') {
518                 for(var i =0;i<item.items.length;i++) {
519                     this.toValaItem(item.items[i], 1, strbuilder);
520                 }
521             }
522             if (depth < 1) {
523                 strbuilder(inpad + "}\n");
524             }
525             
526         }
527         
528         
529         
530     });