resources/RooUsage.txt
[app.Builder.js] / src / Builder4 / GtkView.vala
index ccabdbb..9851abb 100644 (file)
@@ -2,7 +2,7 @@ static Xcls_GtkView  _GtkView;
 
 public class Xcls_GtkView : Object
 {
-    public Gtk.VBox el;
+    public Gtk.Box el;
     private Xcls_GtkView  _this;
 
     public static Xcls_GtkView singleton()
@@ -12,6 +12,7 @@ public class Xcls_GtkView : Object
         }
         return _GtkView;
     }
+    public Xcls_notebook notebook;
     public Xcls_label_preview label_preview;
     public Xcls_label_code label_code;
     public Xcls_view_layout view_layout;
@@ -21,6 +22,8 @@ public class Xcls_GtkView : Object
         // my vars (def)
     public Gtk.Widget lastObj;
     public int width;
+    public int last_search_end;
+    public Gtk.SourceSearchContext searchcontext;
     public JsRender.JsRender file;
     public int height;
     public Xcls_MainWindow main_window;
@@ -29,49 +32,80 @@ public class Xcls_GtkView : Object
     public Xcls_GtkView()
     {
         _this = this;
-        this.el = new Gtk.VBox( true, 0 );
+        this.el = new Gtk.Box( Gtk.Orientation.VERTICAL, 0 );
 
         // my vars (dec)
         this.lastObj = null;
         this.width = 0;
+        this.last_search_end = 0;
         this.file = null;
         this.height = 0;
 
         // set gobject values
-        var child_0 = new Xcls_Notebook2( _this );
+        this.el.hexpand = true;
+        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
+    public void scroll_to_line (int line) {
+       this.notebook.el.page = 1;// code preview...
+       
+       GLib.Timeout.add(500, () => {
+       
+       
+          
+          
+                 var buf = this.sourceview.el.get_buffer();
+        
+               var sbuf = (Gtk.SourceBuffer) buf;
+    
+    
+               Gtk.TextIter iter;   
+               sbuf.get_iter_at_line(out iter,  line);
+               this.sourceview.el.scroll_to_iter(iter,  0.1f, true, 0.0f, 0.5f);
+               return false;
+       });   
+    
+       
+    }
     public void createThumb () {
         
         
         if (this.file == null) {
             return;
         }
+        // only screenshot the gtk preview..
+        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, height); // this.el.position?
+             screenshot.save(filename,"png");
+        } catch (Error e) {
+            
+        }
     
-        Gdk.Pixbuf screenshot = Gdk.pixbuf_get_from_window(win, 0, 0, width, height); // this.el.position?
-    
-        screenshot.save(filename,"png");
+       
         return;
         
         
-        
-        
-        
+         
          
         
         // should we hold until it's printed...
@@ -92,9 +126,13 @@ public class Xcls_GtkView : Object
             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) {
@@ -136,7 +174,49 @@ public class Xcls_GtkView : Object
              
             
     }
-    public class Xcls_Notebook2 : Object
+    public int search (string txt) {
+       this.notebook.el.page = 1;
+       var s = new Gtk.SourceSearchSettings();
+       var buf = (Gtk.SourceBuffer) this.sourceview.el.get_buffer();
+       this.searchcontext = new Gtk.SourceSearchContext(buf,s);
+       this.searchcontext.set_highlight(true);
+       s.set_search_text(txt);
+       
+       Gtk.TextIter beg, st,en;
+        
+       buf.get_start_iter(out beg);
+       this.searchcontext.forward(beg, out st, out en);
+       this.last_search_end  = 0;
+       return this.searchcontext.get_occurrences_count();
+    
+       
+    }
+    public void forwardSearch (bool change_focus) {
+    
+       if (this.searchcontext == null) {
+               return;
+       }
+       this.notebook.el.page = 1;
+       Gtk.TextIter beg, st,en, stl;
+       
+       var buf = this.sourceview.el.get_buffer();
+       buf.get_iter_at_offset(out beg, this.last_search_end);
+       if (!this.searchcontext.forward(beg, out st, out en)) {
+               this.last_search_end = 0;
+       } else { 
+               this.last_search_end = en.get_offset();
+               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);
+                
+               this.sourceview.el.scroll_to_iter(stl,  0.0f, true, 0.0f, 0.5f);
+       }
+    
+    }
+    public class Xcls_notebook : Object
     {
         public Gtk.Notebook el;
         private Xcls_GtkView  _this;
@@ -145,9 +225,10 @@ public class Xcls_GtkView : Object
             // my vars (def)
 
         // ctor
-        public Xcls_Notebook2(Xcls_GtkView _owner )
+        public Xcls_notebook(Xcls_GtkView _owner )
         {
             _this = _owner;
+            _this.notebook = this;
             this.el = new Gtk.Notebook();
 
             // my vars (dec)
@@ -264,7 +345,7 @@ public class Xcls_GtkView : Object
     }
     public class Xcls_container : Object
     {
-        public Gtk.HBox el;
+        public Gtk.Box el;
         private Xcls_GtkView  _this;
 
 
@@ -275,7 +356,7 @@ public class Xcls_GtkView : Object
         {
             _this = _owner;
             _this.container = this;
-            this.el = new Gtk.HBox( true, 0 );
+            this.el = new Gtk.Box( Gtk.Orientation.HORIZONTAL, 0 );
 
             // my vars (dec)
 
@@ -319,6 +400,7 @@ public class Xcls_GtkView : Object
 
             // my vars (def)
         public bool loading;
+        public bool allow_node_scroll;
 
         // ctor
         public Xcls_sourceview(Xcls_GtkView _owner )
@@ -329,6 +411,7 @@ public class Xcls_GtkView : Object
 
             // my vars (dec)
             this.loading = true;
+            this.allow_node_scroll = true;
 
             // set gobject values
             this.el.editable = false;
@@ -357,8 +440,9 @@ public class Xcls_GtkView : Object
                     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;
@@ -367,7 +451,14 @@ public class Xcls_GtkView : Object
                     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..
@@ -437,7 +528,7 @@ public class Xcls_GtkView : Object
             
           
             // this is connected in widnowstate
-            print("node selected");
+            print("Roo-view - node selected\n");
             var buf = this.el.get_buffer();
          
             var sbuf = (Gtk.SourceBuffer) buf;
@@ -462,7 +553,21 @@ public class Xcls_GtkView : Object
             }
             Gtk.TextIter iter;   
             sbuf.get_iter_at_line(out iter,  sel.line_start);
-            this.el.scroll_to_iter(iter,  0.1f, true, 0.0f, 0.0f);
+            
+            
+            Gtk.TextIter cur_iter;
+            sbuf.get_iter_at_offset(out cur_iter, sbuf.cursor_position);
+            
+            //var cur_line = cur_iter.get_line();
+            //if (cur_line > sel.line_start && cur_line < sel.line_end) {
+            
+            //} else {
+            if (this.allow_node_scroll) {
+                        
+               this.el.scroll_to_iter(iter,  0.1f, true, 0.0f, 0.5f);
+               }
+            
+             
             
             for (var i = 0; i < buf.get_line_count();i++) {
                 if (i < sel.line_start || i > sel.line_end) {
@@ -475,6 +580,15 @@ public class Xcls_GtkView : Object
             }
             
         
+        }
+        public string toString () {
+           Gtk.TextIter s;
+            Gtk.TextIter e;
+            this.el.get_buffer().get_start_iter(out s);
+            this.el.get_buffer().get_end_iter(out e);
+            var ret = this.el.get_buffer().get_text(s,e,true);
+            //print("TO STRING? " + ret);
+            return ret;
         }
         public void loadFile ( ) {
             this.loading = true;
@@ -500,6 +614,7 @@ public class Xcls_GtkView : Object
                      this.loading = false;
                     return;
                 }   
+            
         
            if (!FileUtils.test(valafn,FileTest.IS_REGULAR) ) {
                 print("File path has no errors\n");
@@ -508,7 +623,13 @@ public class Xcls_GtkView : Object
             }
             
             string str;
-            GLib.FileUtils.get_contents (valafn, out str);
+            try {
+            
+                GLib.FileUtils.get_contents (valafn, out str);
+            } catch (Error e) {
+                this.loading = false;
+                return  ;
+            }
         
         //    print("setting str %d\n", str.length);
             buf.set_text(str, str.length);
@@ -557,7 +678,10 @@ public class Xcls_GtkView : Object
             }
             var err = obj.get_object_member(type);
             
-             
+            if (_this.file == null) { 
+                return; // just in case the file has not loaded yet?
+            }
+         
         
             var valafn = "";
               try {