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.FileChooserWidget" : [ "action"],
39 //"name":"Edit Module Details",
41 //"btype":"FORM", // was to be components...
43 //"module":"Pman.Tab.BuilderTop2"
45 cfg.parent = cfg.parent || '';
46 if (!cfg.name || !cfg.fullname ) {
48 // name is in theory filename without .bjs (or .js eventually...)
49 cfg.name = cfg.path.split('/').pop().replace(/\.(bjs|js)$/, '');
51 cfg.fullname = (cfg.parent.length ? (cfg.parent + '.') : '' ) + cfg.name;
57 var jstr = JSON.parse(cfg.json);
58 this.items = [ jstr ];
59 //console.log(cfg.items.length);
60 delete cfg.json; // not needed!
64 var p = cfg.items && cfg.items.length && cfg.items[0].parent ? cfg.items[0].parent : false;
66 // ensure real data is set...
77 Gtk.superclass.constructor.call(this, cfg);
81 this.id = 'file-gtk-' + gid++;
83 // various loader methods..
89 setNSID : function(id)
92 this.items[0]['*class'] = id;
100 loadItems : function(cb)
103 print("load Items!");
104 if (this.items !== false) {
109 name: "ArgumentError",
110 message : "no callback for loadItems"
113 Seed.print("load: " + this.path);
119 var src = File.read(this.path);
121 var cfg = JSON.parse(src);
122 print("loaded data");
125 //_this.name = cfg.name; -- this should not happen - name should always match filename!!
126 _this.parent = cfg.parent;
127 _this.title = cfg.title;
128 _this.items = cfg.items || [];
138 * convert xtype for munged output..
141 mungeXtype : function(xtype, els)
143 els.push('xtype: '+ xtype);
146 toSource : function()
149 if (!this.items[0]) {
152 var data = JSON.parse(JSON.stringify(this.items[0]));
153 // we should base this on the objects in the tree really..
154 var i = [ 'Gtk', 'Gdk', 'Pango', 'GLib', 'Gio', 'GObject',
155 'GtkSource', 'WebKit', 'Vte' ]; //, 'GtkClutter' , 'Gdl'];
157 i.forEach(function(e) {
158 src += e+" = imports.gi." + e +";\n";
161 src += "console = imports.console;\n"; // path?!!?
162 src += "XObject = imports.XObject.XObject;\n"; // path?!!?
165 src += this.name + '=new XObject('+ this.mungeToString(data) + ");\n";
166 src += this.name + '.init();\n';
167 // register it in the cache
168 src += "XObject.cache['/" + this.name + "'] = " + this.name + ";\n";
176 Base.prototype.save.call(this);
184 * save as a javascript file.
185 * why is this not save...???
192 var fn = GLib.path_get_dirname(this.path) + '/' + this.name + '.js';
193 print("WRITE : " + fn);
194 File.write(fn, this.toSource());
202 var fn = GLib.path_get_dirname(this.path) + '/' + this.name + '.vala';
203 print("WRITE : " + fn);
204 File.write(fn, this.toVala());
208 valaCompileCmd : function()
211 var fn = '/tmp/' + this.name + '.vala';
212 print("WRITE : " + fn);
213 File.write(fn, this.toVala(true));
220 "--pkg" , "gtk+-3.0",
221 "--pkg", "libnotify",
222 "--pkg", "gtksourceview-3.0",
223 "--pkg", "libwnck-3.0",
224 fn , "-o", "/tmp/" + this.name];
233 getTree : function( o ) {
239 getHelpUrl : function(cls)
241 return 'http://devel.akbkhome.com/seed/' + cls + '.html';
246 toVala: function(testcompile)
249 testcompile = testcompile || false;
252 //print(JSON.stringify(this.items[0],null,4));
253 //print(JSON.stringify(this.items[0],null,4));Seed.quit();
255 var item= XObject.xclone(this.items[0]);
261 print(JSON.stringify(item,null,4));
263 this.palete = new imports.Palete.Gtk.Gtk({});
266 this.toValaName(item);
267 // print(JSON.stringify(item,null,4));Seed.quit();
269 ret += "/* -- to compile\n";
270 ret += "valac --pkg gio-2.0 --pkg posix --pkg gtk+-3.0 --pkg libnotify --pkg gtksourceview-3.0 --pkg libwnck-3.0 \\\n";
271 //ret += " " + item.xvala_id + ".vala -o /tmp/" + item.xvala_id +"\n";
272 ret += " /tmp/" + this.name + ".vala -o /tmp/" + this.name +"\n";
276 ret += "/* -- to test class\n";
279 ret += "static int main (string[] args) {\n";
280 ret += " Gtk.init (ref args);\n";
281 ret += " new " + item.xvala_xcls +"();\n";
282 ret += " " + this.name +".show_all();\n";
283 ret += " Gtk.main ();\n";
284 ret += " return 0;\n";
292 // print(JSON.stringify(item,null,4));
293 this.toValaItem(item,0, function(s) {
301 toValaNS : function(item)
303 var ns = item['|xns'] ;
304 if (ns == 'GtkSource') {
311 toValaName : function(item) {
313 var cls = this.toValaNS(item) + item.xtype; // eg. Gtk.Window
314 var id = item.id ? item.id : (item.xtype + this.vcnt);
315 var props = this.palete.getPropertiesFor(cls, 'props');
319 item.xvala_cls = cls;
320 item.xvala_xcls = 'Xcls_' + id;
321 item.xvala_id = item.id ? item.id : false;
322 this.vitems.push(item);
324 if (typeof(item.items) == 'undefined') {
327 for(var i =0;i<item.items.length;i++) {
328 this.toValaName(item.items[i]);
334 toValaItem : function(item, depth, strbuilder)
336 // print(JSON.stringify(item,null,4));
338 var inpad = new Array( depth +1 ).join(" ");
340 var pad = new Array( depth +2 ).join(" ");
341 var ipad = new Array( depth +3 ).join(" ");
343 var cls = item.xvala_cls;
345 var xcls = item.xvala_xcls;
351 strbuilder(inpad + "public static " + xcls + " " + this.name + ";\n\n");
357 // class xxx { WrappedGtk el; }
358 strbuilder(inpad + "public class " + xcls + "\n" + inpad + "{\n");
359 strbuilder(pad + "public " + cls + " el;\n");
362 strbuilder(pad + "private static " + xcls + " _this;\n\n");
368 //public bool paused = false;
369 //public static StatusIconA statusicon;
371 //strbuilder(pad + "public static " + xcls + " _this;\n");
372 for(var i=1;i < this.vitems.length; i++) {
373 if (this.vitems[i].xvala_id !== false) {
374 strbuilder(pad + "public " + this.vitems[i].xvala_xcls + " " + this.vitems[i].xvala_id + ";\n");
380 strbuilder("\n" + ipad + "// my vars\n");
384 for (var k in item) {
393 var kk = k.substring(1);
395 var vv = kk.split(':');
396 if (vv[0] == 'signal') {
397 strbuilder(pad + "public " + vv[0] + " " + vv[1] + " " + vv[2] + item[k] + ";\n");
400 strbuilder(pad + "public " + vv[0] + " " + vv[1] + ";\n");
408 strbuilder("\n" + ipad + "// ctor \n");
409 strbuilder(pad + "public " + xcls + "()\n" + pad + "{\n");
412 // this may need to look up properties to fill in the arguments..
413 // introspection does not workk..... - as things like gtkmessagedialog
415 if (cls == 'Gtk.Table') {
417 var methods = this.palete.getPropertiesFor(cls, 'methods');
419 print(JSON.stringify(this.palete.proplist[cls], null,4));
425 if (typeof(ctors[cls]) !== 'undefined') {
427 for(var i =0;i< ctors[cls].length;i++) {
429 var nv = ctors[cls][i].split(':');
431 if (typeof(item[nv[0]]) != 'undefined' && typeof(item[nv[0]]) != 'object' ) {
432 citems[nv[0]] = true;
433 args.push(JSON.stringify(item[nv[0]]));
436 if (typeof(item['|' + nv[0]]) != 'undefined' && typeof(item['|' + nv[0]]) != 'object' ) {
437 citems[nv[0]] = true;
438 citems['|' + nv[0]] = true;
439 args.push(item['|' + nv[0]]);
442 args.push(nv.length > 1 ? nv[1] : 'null');
445 strbuilder(ipad + "this.el = new " + cls + "( "+ args.join(", ") + " );\n" );
448 strbuilder(ipad + "this.el = new " + cls + "();\n" );
451 //var meths = this.palete.getPropertiesFor(item['|xns'] + '.' + item.xtype, 'methods');
452 //print(JSON.stringify(meths,null,4));Seed.quit();
458 strbuilder(ipad + "_this = this;\n");
459 strbuilder(ipad + this.name + " = this;\n");
461 if (item.xvala_id !== false) {
462 strbuilder(ipad + "_this." + item.xvala_id + " = this;\n");
468 // initialize.. my vars..
469 strbuilder("\n" + ipad + "// my vars\n");
470 for (var k in item) {
474 var kk = k.substring(1);
476 var vv = kk.split(':');
477 if (v.length < 1 || vv[0] == "signal") {
480 strbuilder(ipad + "this." + vv[1] + " = " + v +";\n");
485 // what are the properties of this class???
486 strbuilder("\n" + ipad + "// set gobject values\n");
487 var props = this.palete.getPropertiesFor(item['|xns'] + '.' + item.xtype, 'props');
491 props.forEach(function(p) {
493 if (typeof(citems[p.name]) != 'undefined') {
497 if (typeof(item[p.name]) != 'undefined' && typeof(item[p.name]) != 'object' ) {
498 citems[p.name] = true;
500 var val = JSON.stringify(item[p.name]);
501 if (['xalign','yalign'].indexOf(p.name) > -1) {
504 strbuilder(ipad + "this.el." + p.name + " = " + val + ";\n");
507 if (typeof(item['|' + p.name]) != 'undefined' && typeof(item['|' + p.name]) != 'object' ) {
508 citems['|' + p.name] = true;
509 //if (p.ctor_only ) {
510 // strbuilder(ipad + "Object(" + p.name + " : " + item['|' + p.name] + ");\n");
512 strbuilder(ipad + "this.el." + p.name + " = " + item['|' + p.name] + ";\n");
521 // add all the child items..
522 if (typeof(item.items) != 'undefined') {
523 for(var i =0;i<item.items.length;i++) {
524 var ci = item.items[i];
526 var packing = ci.pack ? ci.pack.split(',') : [ 'add' ];
527 if (typeof(ci['|pack']) != 'undefined') {
528 packing =ci['|pack'].split(',');
530 var pack = packing.shift();
531 strbuilder(ipad + "var child_" + i + " = new " + ci.xvala_xcls + "();\n" )
533 strbuilder(ipad + "this.el." + pack + " ( child_" + i + ".el " +
534 (packing.length ? ", " + packing.join(",") : "") + " );\n"
540 if (typeof(item['|init']) != 'undefined') {
543 var v = item['|init'].split(/\/*--/);
545 strbuilder("\n" + ipad + "// init method \n");
546 var vv = v[1].replace('*/', "");
547 //print(JSON.stringify(vv));Seed.quit();
548 vv = vv.replace(/^\n+/,'');
549 vv = vv.replace(/\n+$/,'');
550 vv = vv.replace(/\n/g,"\n" + ipad);
551 strbuilder(ipad + vv + "\n");
555 citems['|pack'] = true;
556 citems['|items'] = true;
557 citems['|init'] = true;
559 if (item.listeners) {
560 // print(JSON.stringify(item.listeners));Seed.quit();
562 strbuilder("\n" + ipad + "// listeners \n");
563 // add all the signal handlers..
564 for (var k in item.listeners) {
567 var v = item.listeners[k] ;
569 var vv = v.replace(/\n/g,"\n" + ipad);
573 strbuilder(ipad + "this.el." + k + ".connect( " + vv + " );\n");
582 strbuilder(pad + "}\n");
585 strbuilder("\n" + pad + "// userdefined functions \n");
587 // user defined functions...
589 for (var k in item) {
590 if (typeof(citems[k]) != 'undefined') {
591 strbuilder("\n" + pad + "// skip " + k + " - already used \n");
595 strbuilder("\n" + pad + "// skip " + k + " - not pipe \n");
598 // function in the format of {type} (args) { .... }
601 //print(JSON.stringify(vv));Seed.quit();
602 vv = vv.replace(/^\n+/,'');
603 vv = vv.replace(/\n+$/,'');
604 vv = vv.replace(/\n/g,"\n" + ipad);
606 var vva = k.split(':');
607 if (vva.length < 2) {
608 strbuilder("\n" + pad + "// skip " + k + " - no return type\n");
611 var rtype = vva.shift().substring(1);
613 var fname = vva.shift() || '???';
615 strbuilder(pad + "public " + rtype + " " + fname + body + "\n");
624 strbuilder(inpad + "}\n");
628 // next loop throug children..
629 if (typeof(item.items) != 'undefined') {
630 for(var i =0;i<item.items.length;i++) {
631 this.toValaItem(item.items[i], 1, strbuilder);
635 strbuilder(inpad + "}\n");