src/Builder4/WindowRooView.bjs
[app.Builder.js] / src / Builder4 / WindowRooView.vala
index 615213b..49a0d20 100644 (file)
@@ -15,13 +15,14 @@ public class Xcls_WindowRooView : Object
     public Xcls_notebook notebook;
     public Xcls_label_preview label_preview;
     public Xcls_label_code label_code;
-    public Xcls_WindowRooView WindowRooView;
+    public Xcls_paned paned;
     public Xcls_viewbox viewbox;
     public Xcls_AutoRedraw AutoRedraw;
     public Xcls_viewcontainer viewcontainer;
     public Xcls_view view;
     public Xcls_inspectorcontainer inspectorcontainer;
     public Xcls_sourceview sourceview;
+    public Xcls_buffer buffer;
 
         // my vars (def)
     public Gtk.Widget lastObj;
@@ -50,13 +51,6 @@ public class Xcls_WindowRooView : Object
         var child_0 = new Xcls_notebook( _this );
         child_0.ref();
         this.el.pack_start (  child_0.el , true,true,0 );
-
-        //listeners
-        this.el.size_allocate.connect( (aloc) => {
-        
-            this.width = aloc.width;
-            this.height =aloc.height;
-        });
     }
 
     // user defined functions
@@ -87,13 +81,17 @@ public class Xcls_WindowRooView : Object
         if (this.file == null) {
             return;
         }
+       if (this.notebook.el.page > 0 ) {
+            return;
+        }
+        
         var filename = this.file.getIconFileName(false);
         
         var  win = this.el.get_parent_window();
         var width = win.get_width();
       //  var height = win.get_height();
         try { 
-            Gdk.Pixbuf screenshot = Gdk.pixbuf_get_from_window(win, 0, 0, width, this.el.position);
+            Gdk.Pixbuf screenshot = Gdk.pixbuf_get_from_window(win, 0, 0, width, this.paned.el.position);
             screenshot.save(filename,"png");
         } catch(Error e) {
             //noop
@@ -104,60 +102,13 @@ public class Xcls_WindowRooView : Object
         
          
     }
-    public void loadFile (JsRender.JsRender file) 
+    public void loadFile (JsRender.JsRender file)
     {
-            this.file = null;
-            
-            if (file.tree == null) {
-                return;
-            }
-            this.notebook.el.page = 0;// gtk preview 
-       
-      
-            
-           this.file = file;     
-            this.sourceview.loadFile();
-            this.searchcontext = null;
-            
-    
-            if (this.lastObj != null) {
-                this.container.el.remove(this.lastObj);
-            }
-            
-            // hide the compile view at present..
-              
-            
-            var w = this.width;
-            var h = this.height;
-            
-            print("ALLOC SET SIZES %d, %d\n", w,h); 
-            
-            // set the container size min to 500/500 or 20 px less than max..
-            w = int.max (w-20, 500);
-            h = int.max (h-20, 500); 
-            
-            print("SET SIZES %d, %d\n", w,h);       
-            _this.container.el.set_size_request(w,h);
-            
-            _this.view_layout.el.set_size(w,h); // should be baded on calc.. -- see update_scrolled.
-            var rgba = Gdk.RGBA ();
-            rgba.parse ("#ccc");
-            _this.view_layout.el.override_background_color(Gtk.StateFlags.NORMAL, rgba);
-            
-            
-       var x = new JsRender.NodeToGtk(file.tree);
-            var obj = x.munge() as Gtk.Widget;
-            this.lastObj = null;
-       if (obj == null) {
-               return;
-       }
-       this.lastObj = obj;
-            
-            this.container.el.add(obj);
-            obj.show_all();
-            
-             
-            
+        this.file = file;
+        this.view.renderJS(true);
+        this.notebook.el.page = 0;// gtk preview 
+        this.sourceview.loadFile();   
+        
     }
     public int search (string txt) {
        this.notebook.el.page = 1;
@@ -176,7 +127,11 @@ public class Xcls_WindowRooView : Object
     
        
     }
-    public void forwardSearch () {
+    public void requestRedraw () {
+        this.view.renderJS(false);
+        this.sourceview.loadFile();   
+    }
+    public void forwardSearch (bool change_focus) {
     
        if (this.searchcontext == null) {
                return;
@@ -190,9 +145,9 @@ public class Xcls_WindowRooView : Object
                this.last_search_end = 0;
        } else { 
                this.last_search_end = en.get_offset();
-       
-               this.sourceview.el.grab_focus();
-        
+               if (change_focus) {
+                       this.sourceview.el.grab_focus();
+               }
                buf.place_cursor(st);
                var ln = st.get_line();
                buf.get_iter_at_line(out stl,ln);
@@ -223,7 +178,7 @@ public class Xcls_WindowRooView : Object
             child_0.ref();
             var child_1 = new Xcls_label_code( _this );
             child_1.ref();
-            var child_2 = new Xcls_WindowRooView( _this );
+            var child_2 = new Xcls_paned( _this );
             child_2.ref();
             this.el.add (  child_2.el  );
             var child_3 = new Xcls_ScrolledWindow14( _this );
@@ -279,20 +234,19 @@ public class Xcls_WindowRooView : Object
         // user defined functions
     }
 
-    public class Xcls_WindowRooView : Object
+    public class Xcls_paned : Object
     {
         public Gtk.Paned el;
         private Xcls_WindowRooView  _this;
 
 
             // my vars (def)
-        public JsRender.JsRender file;
 
         // ctor
-        public Xcls_WindowRooView(Xcls_WindowRooView _owner )
+        public Xcls_paned(Xcls_WindowRooView _owner )
         {
             _this = _owner;
-            _this.WindowRooView = this;
+            _this.paned = this;
             this.el = new Gtk.Paned( Gtk.Orientation.VERTICAL );
 
             // my vars (dec)
@@ -307,37 +261,6 @@ public class Xcls_WindowRooView : Object
         }
 
         // user defined functions
-        public void createThumb () {
-            
-            
-            if (this.file == null) {
-                return;
-            }
-            var filename = this.file.getIconFileName(false);
-            
-            var  win = this.el.get_parent_window();
-            var width = win.get_width();
-          //  var height = win.get_height();
-            try { 
-                Gdk.Pixbuf screenshot = Gdk.pixbuf_get_from_window(win, 0, 0, width, this.el.position);
-                screenshot.save(filename,"png");
-            } catch(Error e) {
-                //noop
-            }
-        
-            
-             
-            
-             
-        }
-        public void loadFile (JsRender.JsRender file)
-        {
-            this.file = file;
-            this.view.renderJS(true);
-        }
-        public void requestRedraw () {
-            this.view.renderJS(false);
-        }
     }
     public class Xcls_viewbox : Object
     {
@@ -688,7 +611,7 @@ public class Xcls_WindowRooView : Object
                  
                  
             _this.viewbox.el.remove(_this.viewcontainer.el);
-            _this.el.remove(_this.inspectorcontainer.el);        
+            _this.paned.el.remove(_this.inspectorcontainer.el);        
                  
                  // destory seems to cause problems.
                  //this.el.destroy();
@@ -696,7 +619,7 @@ public class Xcls_WindowRooView : Object
                  //_this.inspectorcontainer.el.destroy();
              var  inv =new Xcls_inspectorcontainer(_this);
               inv.ref();
-              _this.el.pack2(inv.el,true,true);
+              _this.paned.el.pack2(inv.el,true,true);
               
               
              this.el = null;         
@@ -1010,6 +933,7 @@ public class Xcls_WindowRooView : Object
             // my vars (def)
         public bool loading;
         public bool allow_node_scroll;
+        public string propSelected;
 
         // ctor
         public Xcls_sourceview(Xcls_WindowRooView _owner )
@@ -1021,11 +945,15 @@ public class Xcls_WindowRooView : Object
             // my vars (dec)
             this.loading = true;
             this.allow_node_scroll = true;
+            this.propSelected = "";
 
             // set gobject values
             this.el.editable = false;
             this.el.show_line_marks = true;
             this.el.show_line_numbers = true;
+            var child_0 = new Xcls_buffer( _this );
+            child_0.ref();
+            this.el.set_buffer (  child_0.el  );
 
             // init method
 
@@ -1036,43 +964,8 @@ public class Xcls_WindowRooView : Object
                 this.el.override_font(description);
             
                 this.loading = true;
-                var buf = this.el.get_buffer();
-                buf.notify.connect((ps) => {
-                    if (this.loading) {
-                        return;
-                    }
-                    if (ps.name != "cursor-position") {
-                        return;
-                    }
-                    print("cursor changed : %d\n", buf.cursor_position);
-                    Gtk.TextIter cpos;
-                    buf.get_iter_at_offset(out cpos, buf.cursor_position);
-                    
-                    var ln = cpos.get_line();
-             
-                    var node = _this.file.lineToNode(ln);
-             
-                    if (node == null) {
-                        print("can not find node\n");
-                        return;
-                    }
-                    var ltree = _this.main_window.windowstate.left_tree;
-                    var tp = ltree.model.treePathFromNode(node);
-                    print("got tree path %s\n", tp);
-                    if (tp != "") {
-                      this.allow_node_scroll = false;        
-                      print("changing cursor on tree..\n");
-                        ltree.view.el.set_cursor(new Gtk.TreePath.from_string(tp), null, false);
-                        // scrolling is disabled... as node selection calls scroll 10ms after it changes.
-                        GLib.Timeout.add_full(GLib.Priority.DEFAULT,100 , () => {
-                           this.allow_node_scroll = true;
-                           return false;
-                        });
-                    }
-                    
-                    // highlight the node..
-                    
-                });
+                //var buf = this.el.get_buffer();
+                //buf.notify.connect(this.onCursorChanged);
               
               
               
@@ -1129,10 +1022,87 @@ public class Xcls_WindowRooView : Object
                 
                 
             }
+
+            //listeners
+            this.el.button_release_event.connect( () => {
+               this.onCursorChanged();
+            
+               return false;
+            });
         }
 
         // user defined functions
-        public void nodeSelected (JsRender.Node? sel) {
+        public void onCursorChanged (/*ParamSpec ps*/) {
+                 if (this.loading) {
+                    return;
+                }
+               // if (ps.name != "cursor-position") {
+               //     return;
+               // }
+        
+                var buf = this.el.get_buffer();
+                print("cursor changed : %d\n", buf.cursor_position);
+                Gtk.TextIter cpos;
+                buf.get_iter_at_offset(out cpos, buf.cursor_position);
+                
+                var ln = cpos.get_line();
+                       print("cursor changed line : %d\n", ln);
+                var node = _this.file.lineToNode(ln+1);
+         
+                if (node == null) {
+                    print("can not find node\n");
+                    return;
+                }
+                var prop = node.lineToProp(ln+1);
+                print("prop : %s", prop == null ? "???" : prop);
+                
+                
+                
+                
+                
+                
+                var ltree = _this.main_window.windowstate.left_tree;
+                var tp = ltree.model.treePathFromNode(node);
+                print("got tree path %s\n", tp);
+                if (tp != "") {
+                      this.allow_node_scroll = false; /// block node scrolling..
+                      
+                      
+                       //print("changing cursor on tree..\n");
+                      
+         
+                    
+                    // let's try allowing editing on the methods.
+                    // a little klunky at present..
+                    this.propSelected = "";
+                    if (prop != null) {
+                               //see if we can find it..
+                               var kv = prop.split(":");
+                               if (kv[0] == "p") {
+                               
+                                       //var k = prop.get_key(kv[1]);
+                                       // fixme -- need to determine if it's an editable property...
+                                       this.propSelected = prop;
+                                       
+                               } else if (kv[0] == "l") {
+                                        this.propSelected = prop;
+                                       
+                               }
+                    }
+                    ltree.view.setCursor(tp, "editor");
+                   // ltree.view.el.set_cursor(new Gtk.TreePath.from_string(tp), null, false); 
+                   this.nodeSelected(node,false);
+                    
+                    // scrolling is disabled... as node selection calls scroll 10ms after it changes.
+                    GLib.Timeout.add_full(GLib.Priority.DEFAULT,100 , () => {
+                           this.allow_node_scroll = true;
+                           return false;
+                    });
+                }
+                
+                // highlight the node..
+        }
+        public void nodeSelected (JsRender.Node? sel, bool scroll ) {
           
             
           
@@ -1149,7 +1119,7 @@ public class Xcls_WindowRooView : Object
             
            
             // clear all the marks..
-             Gtk.TextIter start;
+            Gtk.TextIter start;
             Gtk.TextIter end;     
                 
             sbuf.get_bounds (out start, out end);
@@ -1157,9 +1127,12 @@ public class Xcls_WindowRooView : Object
             
             
              if (sel == null) {
+                    print("no selected node\n");
                 // no highlighting..
                 return;
             }
+            
+            print("highlight region %d to %d\n", sel.line_start,sel.line_end);
             Gtk.TextIter iter;   
             sbuf.get_iter_at_line(out iter,  sel.line_start);
             
@@ -1171,15 +1144,48 @@ public class Xcls_WindowRooView : Object
             //if (cur_line > sel.line_start && cur_line < sel.line_end) {
             
             //} else {
-            if (this.allow_node_scroll) {
-                        
+            if (scroll) {
+                       print("scrolling to node -- should occur on node picking.\n");
                this.el.scroll_to_iter(iter,  0.1f, true, 0.0f, 0.5f);
                }
             
-             
+            var start_line = sel.line_start;
+            var end_line = sel.line_end;
+            
+            
+            this.el.editable = false;
+            // now if we have selected a property...
+            if (this.propSelected.length> 0 ) {
+        
+                       int nstart, nend;
+                       if (sel.getPropertyRange(this.propSelected, out nstart, out nend) && nend > nstart) {
+                               start_line = nstart;
+                               end_line = nend;
+                               this.el.editable = true;
+                       }
+                       print("propSelected = %s range  %d -> %d\n", this.propSelected, start_line, end_line);          
+                       
+                       
+            }
+            
+            // check selection - if it's out of 'bounds'
+            if (this.el.editable && sbuf.get_has_selection()) {
+                       Gtk.TextIter sel_start_iter, sel_end_iter;
+                       sbuf.get_selection_bounds(out sel_start_iter, out sel_end_iter);
+                       
+                       if (sel_start_iter.get_line() < start_line || sel_end_iter.get_line() > end_line ||
+                               sel_start_iter.get_line() > end_line   || sel_end_iter.get_line() < start_line                  ) {
+                               // save?
+                               this.el.editable = false;
+                       }
+            
+            }
+            
+            
+            
             
             for (var i = 0; i < buf.get_line_count();i++) {
-                if (i < sel.line_start || i > sel.line_end) {
+                if (i < (start_line -1) || i > (end_line -1)) {
                    
                     sbuf.get_iter_at_line(out iter, i);
                     sbuf.create_source_mark(null, "grey", iter);
@@ -1201,45 +1207,33 @@ public class Xcls_WindowRooView : Object
         }
         public void loadFile ( ) {
             this.loading = true;
+            
+            
+            // get the cursor and scroll position....
             var buf = this.el.get_buffer();
+               var cpos = buf.cursor_position;
+            
+           print("BEFORE LOAD cursor = %d\n", cpos);
+           
+            var vadj_pos = this.el.get_vadjustment().get_value();
+           
+            
+         
             buf.set_text("",0);
             var sbuf = (Gtk.SourceBuffer) buf;
         
             
         
-            if (_this.file == null || _this.file.xtype != "Gtk") {
-                print("xtype != Gtk");
+            if (_this.file == null || _this.file.xtype != "Roo") {
+                print("xtype != Roo");
                 this.loading = false;
                 return;
             }
             
-            var valafn = "";
-              try {             
-                   var  regex = new Regex("\\.bjs$");
-                
-                 
-                    valafn = regex.replace(_this.file.path,_this.file.path.length , 0 , ".vala");
-                 } catch (GLib.RegexError e) {
-                     this.loading = false;
-                    return;
-                }   
-            
-        
-           if (!FileUtils.test(valafn,FileTest.IS_REGULAR) ) {
-                print("File path has no errors\n");
-                this.loading = false;
-                return  ;
-            }
-            
-            string str;
-            try {
-            
-                GLib.FileUtils.get_contents (valafn, out str);
-            } catch (Error e) {
-                this.loading = false;
-                return  ;
-            }
-        
+            // get the string from the rendered tree...
+             
+             var str = _this.file.toSource();
+             
         //    print("setting str %d\n", str.length);
             buf.set_text(str, str.length);
             var lm = Gtk.SourceLanguageManager.get_default();
@@ -1255,18 +1249,20 @@ public class Xcls_WindowRooView : Object
             sbuf.get_bounds (out start, out end);
             sbuf.remove_source_marks (start, end, null); // remove all marks..
             
-            
-            if (_this.main_window.windowstate.last_compile_result != null) {
-                var obj = _this.main_window.windowstate.last_compile_result;
-                this.highlightErrorsJson("ERR", obj);
-                this.highlightErrorsJson("WARN", obj);
-                this.highlightErrorsJson("DEPR", obj);                 
-            }
-            //while (Gtk.events_pending()) {
-             //   Gtk.main_iteration();
-           // }
-            
+             GLib.Timeout.add(500, () => {
+               
+               print("RESORTING cursor to = %d\n", cpos);
+                       Gtk.TextIter cpos_iter;
+                       buf.get_iter_at_offset(out cpos_iter, cpos);
+                       buf.place_cursor(cpos_iter); 
+                       
+                       this.el.get_vadjustment().set_value(vadj_pos);;
+                       this.onCursorChanged();
+                       return false;
+               });
+                       
             this.loading = false; 
+            _this.buffer.dirty = false;
         }
         public void highlightErrorsJson (string type, Json.Object obj) {
               Gtk.TextIter start;
@@ -1342,6 +1338,162 @@ public class Xcls_WindowRooView : Object
         
         }
     }
+    public class Xcls_buffer : Object
+    {
+        public Gtk.SourceBuffer el;
+        private Xcls_WindowRooView  _this;
+
+
+            // my vars (def)
+        public bool check_queued;
+        public bool dirty;
+        public int error_line;
+        public bool check_running;
+
+        // ctor
+        public Xcls_buffer(Xcls_WindowRooView _owner )
+        {
+            _this = _owner;
+            _this.buffer = this;
+            this.el = new Gtk.SourceBuffer( null );
+
+            // my vars (dec)
+            this.check_queued = false;
+            this.dirty = false;
+            this.error_line = -1;
+            this.check_running = false;
+
+            // set gobject values
+
+            //listeners
+            this.el.changed.connect( () => {
+                // check syntax??
+                // ??needed..??
+               // _this.save_button.el.sensitive = true;
+                ///?? has changed occured during loading?
+                if (_this.sourceview.loading) {
+                       return;
+               }
+               
+                print("- PREVIEW EDITOR CHANGED--");
+                
+                
+                //this.checkSyntax();
+               
+                this.dirty = true;
+            
+            
+                return ;
+            });
+        }
+
+        // user defined functions
+        public   string toString () {
+            
+            Gtk.TextIter s;
+            Gtk.TextIter e;
+            this.el.get_start_iter(out s);
+            this.el.get_end_iter(out e);
+            var ret = this.el.get_text(s,e,true);
+            //print("TO STRING? " + ret);
+            return ret;
+        }
+        public   bool checkSyntax () {
+         
+            if (this.check_running) {
+                print("Check is running\n");
+                if (this.check_queued) { 
+                    print("Check is already queued");
+                    return true;
+                }
+                this.check_queued = true;
+                print("Adding queued Check ");
+                GLib.Timeout.add_seconds(1, () => {
+                    this.check_queued = false;
+                    
+                    this.checkSyntax();
+                    return false;
+                });
+            
+        
+                return true;
+            }
+            var str = this.toString();
+            
+            // needed???
+            if (this.error_line > 0) {
+                 Gtk.TextIter start;
+                 Gtk.TextIter end;     
+                this.el.get_bounds (out start, out end);
+        
+                this.el.remove_source_marks (start, end, null);
+            }
+            if (str.length < 1) {
+                print("checkSyntax - empty string?\n");
+                return true;
+            }
+            
+            if (_this.file.xtype == "PlainFile") {
+            
+                // assume it's gtk...
+                   this.check_running = true;
+         /*
+                 if (!_this.window.windowstate.valasource.checkPlainFileSpawn(
+                  _this.file,
+                   str
+                )) {
+                    this.check_running = false;
+                }
+               */
+                return true;
+            
+            }
+           if (_this.file == null) {
+               return true;
+           }
+            var p = Palete.factory(_this.file.xtype);   
+            
+        
+             
+            this.check_running = true;
+            
+            
+            if (_this.file.language == "js") {
+                this.check_running = false;
+                print("calling validate javascript\n"); 
+              /*  return this.highlightErrors(p.validateJavascript(
+                    str, 
+                     _this.key, 
+                    _this.ptype,
+                    _this.file,
+                    _this.node
+                ));    
+                */
+            }
+                
+                
+            print("calling validate vala\n");    
+            // clear the buttons.
+         
+            /*
+           if (! _this.window.windowstate.valasource.checkFileWithNodePropChange(
+                _this.file,
+                _this.node,
+                 _this.key,        
+                 _this.ptype,
+                    str
+                )) {
+                this.check_running = false;
+            } 
+             
+            */
+            
+            //print("done mark line\n");
+             
+            return true; // at present allow saving - even if it's invalid..
+        }
+    }
+