sync
[app.Builder.js] / Builder / RightGtkView.js
index 4261065..609e079 100755 (executable)
@@ -3,7 +3,7 @@ Gio = imports.gi.Gio;
 Gtk = imports.gi.Gtk;
 Gdk = imports.gi.Gdk;
 GObject = imports.gi.GObject;
+ GLib= imports.gi.GLib;
 
 /**
 * we use a hidden window to render the created dialog...
@@ -68,22 +68,25 @@ RightGtkView = new XObject({
                     {
                         
                         id : 'view-vbox',
-                        xtype : Gtk.Viewport,
+                        xtype : Gtk.Fixed,
                         init : function () {
                             XObject.prototype.init.call(this); 
-                            this.el.set_hadjustment(this.parent.el.get_hadjustment());
-                            this.el.set_vadjustment(this.parent.el.get_vadjustment());
+                           // this.el.set_hadjustment(this.parent.el.get_hadjustment());
+                            //this.el.set_vadjustment(this.parent.el.get_vadjustment());
                                 
                         },
-                        packing : ['add' ],
+                        pack  : 'add_with_viewport' ,
                         items: [
                             {
                                 id : 'view',
+                                xtype : Gtk.VBox,
+                                /*
                                 xtype : function() {
                                     return new Gtk.Image.from_stock (Gtk.STOCK_HOME, 100) 
 
                                 },
-                                packing : ['add' ],
+                                */
+                                pack : 'put,10,10',
                                 ready : false,
                                 init : function() {
                                     XObject.prototype.init.call(this); 
@@ -237,36 +240,47 @@ RightGtkView = new XObject({
         
         showInWindow: function ()
         {
-            var src= this.lastSrc;
-            if (!this.lastSrc.length) {
-                return;
-            }
-            var x = new imports.sandbox.Context();
-            x.add_globals();
+            
+            
+            var src= this.buildJS(this.get('/LeftTree.model').toJS()[0], true);
+            
+            
+            this.get('/Terminal').feed("Running\n");
+            
+            //var x = new imports.sandbox.Context();
+            //x.add_globals();
             //print(src);
             try {
                 Seed.check_syntax('var e = ' + src);
-                x.eval(src);
+              //  x.eval(src);
             } catch( e) {
-                print(e.message || e.toString());
-                console.dump(e);
+                this.get('/Terminal').feed(e.message || e.toString() + "\n");
+                this.get('/Terminal').feed(console._dump(e)+"\n");
                 if (e.line) {
                     var lines = src.split("\n");
                     var start = Math.max(0, e.line - 10);
                     var end = Math.min(lines.length, e.line + 10);
                     for (var i =start ; i < end; i++) {
                         if (i == e.line) {
-                            print(">>>>>" + lines[i]);
+                            this.get('/Terminal').feed(">>>>>" + lines[i] + "\n");
+                            
+                          
                             continue;
                         }
-                        print(lines[i]);
+                        
+                        this.get('/Terminal').feed(lines[i] + "\n");
                     }
                     
                 }
                 
                 return;
             }
-             
+            this.get('/BottomPane').el.set_current_page(1);
+            this.get('/Terminal').el.fork_command( null , [], [], "/tmp", false,false,false); 
+            var cmd = "/usr/bin/seed /tmp/BuilderGtkView.js\n";
+            this.get('/Terminal').el.feed_child(cmd, cmd.length);
+            //'/usr/bin/seed',  [ '/tmp/BuilderGtkView.js'], [], "/tmp", false,false,false);
+            /*
             var _top = x.get_global_object()._top;
             
             _top.el.set_screen(Gdk.Screen.get_default()); // just in case..
@@ -274,23 +288,41 @@ RightGtkView = new XObject({
             if (_top.el.popup) {
                 _top.el.popup(null, null, null, null, 3, null);
             }
+            */
         },
         
-        buildJS: function(data,withDebug) {
-            var i = [ 'Gtk', 'Gdk', 'Pango', 'GLib', 'Gio', 'GObject', 'GtkSource', 'WebKit' ];
+        buildJS: function(data,withDebug) 
+        {
+            var i = [ 'Gtk', 'Gdk', 'Pango', 'GLib', 'Gio', 'GObject', 'GtkSource', 'WebKit', 'Vte' ];
             var src = "";
             i.forEach(function(e) {
                 src += e+" = imports.gi." + e +";\n";
             });
+            
+            if (withDebug) {
+               src+= "imports.searchPath.push(" + JSON.stringify(GLib.path_get_dirname(__script_path__)) + ");\n";
+            }
+            
             src += "console = imports.console;\n"; // path?!!?
             src += "XObject = imports.XObject.XObject;\n"; // path?!!?
+            src += "XObject.cache = {};\n"; // reset cache!
+            if (withDebug) {
+                
+                
+                
+                src += "Gtk.init(null,null);\n"; 
+            }
             if (withDebug) {
                 src += "XObject.debug=true;\n"; 
             }
             
-            
+            this.withDebug = withDebug;
             src += '_top=new XObject('+ this.mungeToString(data) + ')\n;';
             src += '_top.init();\n';
+            if (withDebug) {
+                src += "_top.el.show_all();\n"; 
+                src += "Gtk.main();\n"; 
+            }
             File.write('/tmp/BuilderGtkView.js', src);
             print("Test code  in /tmp/BuilderGtkView.js");
             this.lastSrc = src;
@@ -309,6 +341,26 @@ RightGtkView = new XObject({
             if (!data) {
                  return; 
             }
+            this.withDebug = false;
+            
+            if (this.renderedEl) {
+                this.get('view').el.remove(this.renderedEl);
+                this.renderedEl.destroy();
+                this.renderedEl = false;
+            }
+            
+            var tree =  this.get('/LeftTree.model').toJS()[0];
+            // in theory tree is actually window..
+            this.renderedEl = this.viewAdd(tree.items[0], this.get('view').el);
+            this.get('view').el.set_size_request(
+                tree.default_width * 1 || 400, tree.default_height * 1 || 400
+            ) ;
+            
+            this.get('view').el.show_all();
+            
+            return;
+            
+            
             var src = this.buildJS(data,withDebug);
             var x = new imports.sandbox.Context();
             x.add_globals();
@@ -318,9 +370,9 @@ RightGtkView = new XObject({
                 Seed.check_syntax('var e = ' + src);
                 x.eval(src);
             } catch( e) {
-                if (!withDebug) {
-                   return this.renderJS(data,true);
-                }
+                //if (!withDebug) {
+                //   return this.renderJS(data,true);
+               // }
                 print(e.message || e.toString());
                 console.dump(e);
                 return;
@@ -331,17 +383,20 @@ RightGtkView = new XObject({
             
             _top.el.set_screen(Gdk.Screen.get_default()); // just in case..
             _top.el.show_all();
+              
+            
             if (_top.el.popup) {
                 _top.el.popup(null, null, null, null, 3, null);
             }
             
             
             
-            var pb = _top.el.get_snapshot(r);
+            var pb = _top.items[0].el.get_snapshot(r);
+            _top.el.hide();
             if (!pb) {
                 return;
             }
-            _top.el.hide();
+            
             _top.el.destroy();
             x._top = false;
             var Window = imports.Builder.Window.Window;
@@ -366,11 +421,11 @@ RightGtkView = new XObject({
         },
         mungeToString:  function(obj, isListener, pad)
         {
-            pad = pad|| '';
+            pad = pad || '';
             var keys = [];
             var isArray = false;
             isListener = isListener || false;
-            
+             
             // am I munging a object or array...
             if (obj.constructor.toString() === Array.toString()) {
                 for (var i= 0; i < obj.length; i++) {
@@ -396,16 +451,27 @@ RightGtkView = new XObject({
             
             var _this = this;
             
+            
+            
             keys.forEach(function(i) {
                 var el = obj[i];
                 if (!isArray && skip.indexOf(i) > -1) {
                     return;
                 }
                 if (isListener) {
-                    if (obj[i].match(/Gtk.main_quit/)) { // we can not handle this very well..
+                    if (!_this.withDebug) {
+                        // do not write listeners unless we are debug mode.
                         return;
                     }
-                    els.push(JSON.stringify(i) + ":" + obj[i]);
+                    //if (obj[i].match(new RegExp("Gtk.main" + "_quit"))) { // we can not handle this very well..
+                    //    return;
+                   // }
+                    var str= ('' + obj[i]).replace(/^\s+|\s+$/g,"");
+                    var lines = str.split("\n");
+                    if (lines.length > 1) {
+                        str = lines.join("\n" + pad);
+                    }
+                    els.push(JSON.stringify(i) + ":" + str);
                     return;
                 }
                 if (i[0] == '|') {
@@ -413,11 +479,18 @@ RightGtkView = new XObject({
                     if (typeof(el) == 'string' && !obj[i].length) { //skip empty.
                         return;
                     }
+                    // this needs to go...
+                    //if (typeof(el) == 'string'  && obj[i].match(new RegExp("Gtk.main" + "_quit"))) { // we can not handle this very well..
+                    //    return;
+                    //}
                     
-                    if (typeof(el) == 'string'  && obj[i].match(/Gtk.main_quit/)) { // we can not handle this very well..
-                        return;
+                    var str= ('' + obj[i]).replace(/^\s+|\s+$/g,"");;
+                    var lines = str.split("\n");
+                    if (lines.length > 1) {
+                        str = lines.join("\n" + pad);
                     }
-                    els.push(JSON.stringify(i.substring(1)) + ":" + obj[i]);
+                    
+                    els.push(JSON.stringify(i.substring(1)) + ":" + str);
                     return;
                 }
                 var left = isArray ? '' : (JSON.stringify(i) + " : " )
@@ -427,14 +500,106 @@ RightGtkView = new XObject({
                 }
                 els.push(JSON.stringify(i) + ":" + JSON.stringify(obj[i]));
             });
+            var spad = pad.substring(0, pad.length-4);
             return (isArray ? '[' : '{') + "\n" +
-                pad  + els.join(",\n" + pad ) +
-                (isArray ? ']' : '}');
+                pad  + els.join(",\n" + pad ) + 
+                "\n" + spad + (isArray ? ']' : '}');
                
             
             
+        },
+        
+        buildView : function()
+        {
+            
+            
+        },
+        viewAdd : function(item, par)
+        {
+            // does something similar to xobject..
+            item.pack = (typeof(item.pack) == 'undefined') ?  'add' : item.pack;
+            
+            if (item.pack===false || item.pack === 'false') {  // no ;
+                return;
+            }
+            print("CREATE: " + item['|xns'] + '.' + item['xtype']);
+            var ns = imports.gi[item['|xns']];
+            var ctr = ns[item['xtype']];
+            var ctr_args = { };
+            for(var k in item) {
+                var kv = item[k];
+                if (typeof(kv) == 'object' || typeof(kv) == 'function') {
+                    continue;
+                }
+                if ( 
+                    k == 'pack' ||
+                    k == 'items' ||
+                    k == 'id' ||
+                    k == 'xtype' ||
+                    k == 'xdebug' ||
+                    k == 'xns' ||
+                    k == '|xns'
+                ) {
+                    continue;
+                }
+                ctr_args[k] = kv;
+                
+            } 
+            
+            
+            var el = new ctr(ctr_args);
+            
+            print("PACK");
+            console.dump(item.pack);
+            
+            
+            
+            
+            var args = [];
+            var pack_m  = false;
+            if (typeof(item.pack) == 'string') {
+                 
+                item.pack.split(',').forEach(function(e, i) {
+                    
+                    if (e == 'false') { args.push( false); return; }
+                    if (e == 'true') {  args.push( true);  return; }
+                    if (!isNaN(parseInt(e))) { args.push( parseInt(e)); return; }
+                    args.push(e);
+                });
+                //print(args.join(","));
+                
+                pack_m = args.shift();
+            } else {
+                pack_m = item.pack.shift();
+                args = item.pack;
+            }
+            
+            // handle error.
+            if (pack_m && typeof(par[pack_m]) == 'undefined') {
+                Seed.print('pack method not available : ' + item.xtype + '.' +  pack_m);
+                return;
+            }
+            
+            console.dump(args);
+            args.unshift(el);
+            //if (XObject.debug) print(pack_m + '[' + args.join(',') +']');
+            //Seed.print('args: ' + args.length);
+            if (pack_m) {
+                par[pack_m].apply(par, args);
+            }
+            
+            var _this = this;
+            item.items = item.items || [];
+            item.items.forEach(function(ch) {
+                _this.viewAdd(ch, el);
+            });
+            
+            return el;
+            
         }
         
+        
+        
     }