1 //<Script type="text/javascript">
4 GLib = imports.gi.GLib;
6 XObject = imports.XObject.XObject;
7 File = imports.File.File;
9 //----------------------- our roo verison
10 Base = imports.JsRender.Base.Base;
14 // ctors needed for Constructing vala?? -
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"],
42 //"name":"Edit Module Details",
44 //"btype":"FORM", // was to be components...
46 //"module":"Pman.Tab.BuilderTop2"
48 cfg.parent = cfg.parent || '';
49 if (!cfg.name || !cfg.fullname ) {
51 // name is in theory filename without .bjs (or .js eventually...)
52 cfg.name = cfg.path.split('/').pop().replace(/\.(bjs|js)$/, '');
54 cfg.fullname = (cfg.parent.length ? (cfg.parent + '.') : '' ) + cfg.name;
60 var jstr = JSON.parse(cfg.json);
61 this.items = [ jstr ];
62 //console.log(cfg.items.length);
63 delete cfg.json; // not needed!
67 var p = cfg.items && cfg.items.length && cfg.items[0].parent ? cfg.items[0].parent : false;
69 // ensure real data is set...
80 Gtk.superclass.constructor.call(this, cfg);
84 this.id = 'file-gtk-' + gid++;
86 // various loader methods..
92 setNSID : function(id)
95 this.items[0]['*class'] = id;
103 loadItems : function(cb)
106 print("load Items!");
107 if (this.items !== false) {
112 name: "ArgumentError",
113 message : "no callback for loadItems"
116 Seed.print("load: " + this.path);
122 var src = File.read(this.path);
124 var cfg = JSON.parse(src);
125 print("loaded data");
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 || [];
141 * convert xtype for munged output..
144 mungeXtype : function(xtype, els)
146 els.push('xtype: '+ xtype);
149 toSource : function()
152 if (!this.items[0]) {
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'];
160 i.forEach(function(e) {
161 src += e+" = imports.gi." + e +";\n";
164 src += "console = imports.console;\n"; // path?!!?
165 src += "XObject = imports.XObject.XObject;\n"; // path?!!?
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";
179 Base.prototype.save.call(this);
187 * save as a javascript file.
188 * why is this not save...???
195 var fn = GLib.path_get_dirname(this.path) + '/' + this.name + '.js';
196 print("WRITE : " + fn);
197 File.write(fn, this.toSource());
205 var fn = GLib.path_get_dirname(this.path) + '/' + this.name + '.vala';
206 print("WRITE : " + fn);
207 File.write(fn, this.toVala());
211 valaCompileCmd : function()
214 var fn = '/tmp/' + this.name + '.vala';
215 print("WRITE : " + fn);
216 File.write(fn, this.toVala(true));
223 "--pkg" , "gtk+-3.0",
224 "--pkg", "libnotify",
225 "--pkg", "gtksourceview-3.0",
226 "--pkg", "libwnck-3.0",
227 fn , "-o", "/tmp/" + this.name];
236 getTree : function( o ) {
242 getHelpUrl : function(cls)
244 return 'http://devel.akbkhome.com/seed/' + cls + '.html';
249 toVala: function(testcompile)
252 testcompile = testcompile || false;
255 //print(JSON.stringify(this.items[0],null,4));
256 //print(JSON.stringify(this.items[0],null,4));Seed.quit();
258 var item= XObject.xclone(this.items[0]);
264 print(JSON.stringify(item,null,4));
266 this.palete = new imports.Palete.Gtk.Gtk({});
269 this.toValaName(item);
270 // print(JSON.stringify(item,null,4));Seed.quit();
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";
279 ret += "/* -- to test class\n";
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";
295 // print(JSON.stringify(item,null,4));
296 this.toValaItem(item,0, function(s) {
304 toValaNS : function(item)
306 var ns = item['|xns'] ;
307 if (ns == 'GtkSource') {
314 toValaName : function(item) {
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');
321 if (id[0] == '+' || id[0] == '*') {
322 id_clsname = id_clsname.substring(1);
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);
330 if (typeof(item.items) == 'undefined') {
333 for(var i =0;i<item.items.length;i++) {
334 this.toValaName(item.items[i]);
340 toValaItem : function(item, depth, strbuilder)
342 // print(JSON.stringify(item,null,4));
344 var inpad = new Array( depth +1 ).join(" ");
346 var pad = new Array( depth +2 ).join(" ");
347 var ipad = new Array( depth +3 ).join(" ");
349 var cls = item.xvala_cls;
351 var xcls = item.xvala_xcls;
357 strbuilder(inpad + "public static " + xcls + " " + this.name + ";\n\n");
363 // class xxx { WrappedGtk el; }
364 strbuilder(inpad + "public class " + xcls + " : Object \n" + inpad + "{\n");
365 strbuilder(pad + "public " + cls + " el;\n");
367 this.top_xcls = xcls;
369 strbuilder(pad + "private " + this.top_xcls + " _this;\n\n");
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");
387 // properties - global..??
389 //public bool paused = false;
390 //public static StatusIconA statusicon;
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) {
398 if (this.vitems[i].xvala_id[0] == '*') {
401 if (this.vitems[i].xvala_id[0] == '+') {
404 strbuilder(pad + "public " + this.vitems[i].xvala_xcls + " " + this.vitems[i].xvala_id + ";\n");
409 strbuilder("\n" + ipad + "// my vars\n");
413 for (var k in item) {
424 var kk = k.substring(1);
426 var vv = kk.split(':');
427 if (vv[0] == 'signal') {
428 strbuilder(pad + "public " + vv[0] + " " + vv[1] + " " + vv[2] + item[k] + ";\n");
431 strbuilder(pad + "public " + vv[0] + " " + vv[1] + ";\n");
436 // if id of child is '+' then it's a property of this..
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?
445 strbuilder(pad + "public " + ci.xvala_xcls + " " + ci.xvala_id.substring(1) + ";\n");
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());
469 strbuilder(pad + "public " + xcls + "(" + cargs_str.substring(1) +")\n" + pad + "{\n");
474 strbuilder(pad + "public " + xcls + "(" + this.top_xcls + " _owner " + cargs_str + ")\n" + pad + "{\n");
481 strbuilder(ipad + "_this = this;\n");
482 //strbuilder(ipad + this.name + " = this;\n");
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");
494 // this may need to look up properties to fill in the arguments..
495 // introspection does not workk..... - as things like gtkmessagedialog
497 if (cls == 'Gtk.Table') {
499 var methods = this.palete.getPropertiesFor(cls, 'methods');
501 print(JSON.stringify(this.palete.proplist[cls], null,4));
505 if (typeof(item['.ctor']) != 'undefined') {
506 strbuilder(ipad + "this.el = " + item['.ctor']+ ";\n" );
507 } else if (typeof(ctors[cls]) !== 'undefined') {
509 for(var i =0;i< ctors[cls].length;i++) {
511 var nv = ctors[cls][i].split(':');
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]]));
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]]);
524 args.push(nv.length > 1 ? nv[1] : 'null');
527 strbuilder(ipad + "this.el = new " + cls + "( "+ args.join(", ") + " );\n" );
530 strbuilder(ipad + "this.el = new " + cls + "();\n" );
533 //var meths = this.palete.getPropertiesFor(item['|xns'] + '.' + item.xtype, 'methods');
534 //print(JSON.stringify(meths,null,4));Seed.quit();
538 // initialize.. my vars..
539 strbuilder("\n" + ipad + "// my vars\n");
540 for (var k in item) {
548 var kk = k.substring(1);
550 var vv = kk.split(':');
551 if (v.length < 1 || vv[0] == "signal") {
554 strbuilder(ipad + "this." + vv[1] + " = " + v +";\n");
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');
565 props.forEach(function(p) {
567 if (typeof(citems[p.name]) != 'undefined') {
571 if (typeof(item[p.name]) != 'undefined' && typeof(item[p.name]) != 'object' ) {
572 citems[p.name] = true;
574 var val = JSON.stringify(item[p.name]);
575 if (['xalign','yalign'].indexOf(p.name) > -1) {
578 strbuilder(ipad + "this.el." + p.name + " = " + val + ";\n");
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");
586 strbuilder(ipad + "this.el." + p.name + " = " + item['|' + p.name] + ";\n");
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?
604 if (typeof(ci['*args']) != 'undefined') {
606 var ar = ci['*args'].split(',');
607 for (var ari = 0 ; ari < ar.length; ari++ ) {
608 xargs += "," + ar[ari].trim().split(" ").pop();
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...
615 if (typeof(ci['*prop']) != 'undefined') {
616 strbuilder(ipad + "this.el." + ci['*prop'] + " = child_" + i + ".el;\n" );
620 if (ci.pack === false || ci.pack == "false") {
624 var packing = ci.pack ? ci.pack.split(',') : [ 'add' ];
625 if (typeof(ci['|pack']) != 'undefined') {
626 packing =ci['|pack'].split(',');
628 var pack = packing.shift();
631 strbuilder(ipad + "this.el." + pack + " ( child_" + i + ".el " +
632 (packing.length ? ", " + packing.join(",") : "") + " );\n"
635 if (ci.xvala_id[0] != '+') {
636 continue; // skip generation of children?
639 strbuilder(ipad + "this." + ci.xvala_id.substring(1) + " = child_" + i + ";\n");
643 if (typeof(item['|init']) != 'undefined') {
646 var v = item['|init']
648 strbuilder("\n" + ipad + "// init method \n");
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");
658 citems['|pack'] = true;
659 citems['|items'] = true;
660 citems['|init'] = true;
662 if (item.listeners) {
663 // print(JSON.stringify(item.listeners));Seed.quit();
665 strbuilder("\n" + ipad + "// listeners \n");
666 // add all the signal handlers..
667 for (var k in item.listeners) {
670 var v = item.listeners[k] ;
672 var vv = v.replace(/\n/g,"\n" + ipad);
676 strbuilder(ipad + "this.el." + k + ".connect( " + vv + " );\n");
685 strbuilder(pad + "}\n");
688 strbuilder("\n" + pad + "// userdefined functions \n");
690 // user defined functions...
692 for (var k in item) {
693 if (typeof(citems[k]) != 'undefined') {
694 //strbuilder("\n" + pad + "// skip " + k + " - already used \n");
698 //strbuilder("\n" + pad + "// skip " + k + " - not pipe \n");
701 // function in the format of {type} (args) { .... }
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);
709 var vva = k.split(':');
710 if (vva.length < 2) {
711 strbuilder("\n" + pad + "// skip " + k + " - no return type\n");
714 var rtype = vva.shift().substring(1);
716 var fname = vva.shift() || '???';
718 strbuilder(pad + "public " + rtype + " " + fname + body + "\n");
727 strbuilder(inpad + "}\n");
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);
738 strbuilder(inpad + "}\n");