src/Builder4/WindowRooView.bjs
[app.Builder.js] / src / Builder4 / WindowRooView.vala
index b148b76..6ede968 100644 (file)
@@ -22,6 +22,7 @@ public class Xcls_WindowRooView : Object
     public Xcls_view view;
     public Xcls_inspectorcontainer inspectorcontainer;
     public Xcls_sourceview sourceview;
+    public Xcls_buffer buffer;
 
         // my vars (def)
     public Gtk.Widget lastObj;
@@ -950,6 +951,9 @@ public class Xcls_WindowRooView : Object
             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
 
@@ -1053,6 +1057,10 @@ public class Xcls_WindowRooView : Object
                 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);
@@ -1072,18 +1080,18 @@ public class Xcls_WindowRooView : Object
                                var kv = prop.split(":");
                                if (kv[0] == "p") {
                                
-                                       var k = prop.get_key(kv[1]);
+                                       //var k = prop.get_key(kv[1]);
                                        // fixme -- need to determine if it's an editable property...
                                        this.propSelected = prop;
                                        
-                               } elseif (kv[0] == "l") {
+                               } else if (kv[0] == "l") {
                                         this.propSelected = prop;
                                        
                                }
                     }
-                    
-                   ltree.view.el.set_cursor(new Gtk.TreePath.from_string(tp), null, false); 
-                   
+                    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 , () => {
@@ -1094,7 +1102,7 @@ public class Xcls_WindowRooView : Object
                 
                 // highlight the node..
         }
-        public void nodeSelected (JsRender.Node? sel) {
+        public void nodeSelected (JsRender.Node? sel, bool scroll ) {
           
             
           
@@ -1136,28 +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) {
                         
                this.el.scroll_to_iter(iter,  0.1f, true, 0.0f, 0.5f);
                }
             
-            var start = sel.line_start;
-            var end = sel.line_end;
+            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;
-                       sel.getPropertyRange(this.propSelected, out nstart, out nend);
-                       if (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 < (start -1) || i > (end -1)) {
+                if (i < (start_line -1) || i > (end_line -1)) {
                    
                     sbuf.get_iter_at_line(out iter, i);
                     sbuf.create_source_mark(null, "grey", iter);
@@ -1179,7 +1207,18 @@ 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;
         
@@ -1210,9 +1249,18 @@ public class Xcls_WindowRooView : Object
             sbuf.get_bounds (out start, out end);
             sbuf.remove_source_marks (start, end, null); // remove all marks..
             
-            
-            
-            
+             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; 
         }
         public void highlightErrorsJson (string type, Json.Object obj) {
@@ -1289,6 +1337,156 @@ 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 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.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?
+                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..
+        }
+    }
+