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