Fix #8089 - phase 1 - code ast navigation
[roobuilder] / src / Builder4 / WindowLeftTree.vala
index 8b0a0b0..8a08fdc 100644 (file)
@@ -27,6 +27,7 @@ public class Xcls_WindowLeftTree : Object
        public int last_error_counter;
        public signal void changed ();
        public signal void node_selected (JsRender.Node? node);
+       public Gee.ArrayList<Gtk.Widget>? error_widgets;
 
        // ctor
        public Xcls_WindowLeftTree()
@@ -37,6 +38,7 @@ public class Xcls_WindowLeftTree : Object
                // my vars (dec)
                this.main_window = null;
                this.last_error_counter = -1;
+               this.error_widgets = null;
 
                // set gobject values
                this.el.hexpand = true;
@@ -69,7 +71,7 @@ public class Xcls_WindowLeftTree : Object
                        return;
                }
                this.removeErrors();
-               
+               this.error_widgets = new Gee.ArrayList<Gtk.Widget>();
                foreach(var diag in ar) { 
                
                         
@@ -86,6 +88,7 @@ public class Xcls_WindowLeftTree : Object
                if (w == null) {
                        return;
                        }
+                       this.error_widgets.add(w);
                        // always show errors.
                        var ed = diag.category.down();
                        if (ed != "err" && w.has_css_class("node-err")) {
@@ -111,6 +114,26 @@ public class Xcls_WindowLeftTree : Object
        //      _this.maincol.el.set_max_width( _this.viewwin.el.get_width()  - 32 );
        }
        public void removeErrors () {
+               if (this.error_widgets == null || this.error_widgets.size < 1) {
+                       return;
+               }
+               foreach(var child in this.error_widgets) {
+               
+                       if (child.has_css_class("node-err")) {
+                               child.remove_css_class("node-err");
+                       }
+                       if (child.has_css_class("node-warn")) {
+                               child.remove_css_class("node-warn");
+                       }
+                       
+                       if (child.has_css_class("node-depr")) {
+                               child.remove_css_class("node-depr");
+                       }
+               }
+               this.error_widgets  = null;
+               return;
+               
+               /*
                var  child = this.view.el.get_first_child(); 
         
                var reading_header = true;
@@ -149,6 +172,7 @@ public class Xcls_WindowLeftTree : Object
                child = child.get_next_sibling(); 
                }
                //GLib.debug("Rturning null");
+               */
             
        }
        public JsRender.Node? getActiveElement () { // return path to actie node.
@@ -160,6 +184,7 @@ public class Xcls_WindowLeftTree : Object
        }
        public JsRender.JsRender getActiveFile () {
            return this.main_window.windowstate.file;
+           
        }
        public class Xcls_ListView1 : Object
        {
@@ -295,7 +320,7 @@ public class Xcls_WindowLeftTree : Object
                        // init method
 
                        {
-                        
+                        /*
                          this.css = new Gtk.CssProvider();
                        //      try {
                                        this.css.load_from_string("
@@ -349,7 +374,7 @@ public class Xcls_WindowLeftTree : Object
                                        this.css,
                                        Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION
                                );
-                               
+                               */
                                  
                        }
                }
@@ -437,161 +462,66 @@ public class Xcls_WindowLeftTree : Object
                    return -1;
                
                 }
-               public int getRowAt (double x,  double in_y, out string pos) {
-               
+               public int getRowAt (double x,  double  y, out string pos) {
                
+                       pos = "";
+                       var w = this.el.pick(x, y, Gtk.PickFlags.DEFAULT);
+                       //GLib.debug("got widget %s", w == null ? "nothing" : w.get_type().name());
+                       if (w == null) {
+                               return -1;
+                       }
+                       
+                       var row= w.get_ancestor(GLib.Type.from_name("GtkColumnViewRowWidget"));
+                       if (row == null) {
+                               return -1;
+                       }
+                       
+                       //GLib.debug("got colview %s", row == null ? "nothing" : row.get_type().name());
                         
-               
-               /*
-                       
-               from            https://discourse.gnome.org/t/gtk4-finding-a-row-data-on-gtkcolumnview/8465
-                       var colview = gesture.widget;
-                       var line_no = check_list_widget(colview, x,y);
-                        if (line_no > -1) {
-                               var item = colview.model.get_item(line_no);
-                                
-                       }
-                       */
-                                
-                               
-                               GLib.debug("get Widget At Row x = %d  y = %d", (int) x, (int) in_y);
-                       var y = in_y + _this.viewwin.el.vadjustment.value; 
-                       var  child = this.el.get_first_child(); 
-                       //Gtk.Allocation alloc = { 0, 0, 0, 0 };
-                       var line_no = -1; 
-                       var reading_header = true;
-                       var real_y = 0;
-                       var header_height  = 0;
-                       pos = "none";
-                       var h = 0;
-                       while (child != null) {
-                                       //GLib.debug("Got %s", child.get_type().name());
-                           if (reading_header) {
-                                               
-               
-                                               if (child.get_type().name() != "GtkColumnListView") {
-                                               h += child.get_height();
-                                                       child = child.get_next_sibling();
-                                                       continue;
-                                               }
-                                               // should be columnlistview
-                                               child = child.get_first_child(); 
-                                           //GLib.debug("header height=%d", h);
-                                               header_height =  h;
-                                               
-                                               reading_header = false;
-                                               continue;
-                                               
-                               }
-                               
-                                   if (child.get_type().name() != "GtkColumnViewRowWidget") {
-                                   child = child.get_next_sibling();
-                                   continue;
-                                   }
-                                   
-                                       if (y < header_height) {
-                                       return -1;
-                               }
-                                   
-                                   line_no++;
-                                       var hh = child.get_height();
-                                       
-                                       if (child.has_css_class("node-err") || 
-                                               child.has_css_class("node-warn") || 
-                                               child.has_css_class("node-depr")) {
-                                               hh += 10;
-                                       
-                                       }
-                                       //child.get_allocation(out alloc);
-                                       //GLib.debug("got cell xy = %d,%d  w,h= %d,%d", alloc.x, alloc.y, alloc.width, alloc.height);
-                                       //GLib.debug("row %d y= %d %s", line_no, (int) (header_height + alloc.y),
-                                       
-                                       //      child.visible ? "VIS" : "hidden");
-               
-                                   if (y >  (header_height + real_y) && y <= (header_height +  real_y + hh) ) {
-                                       if (y > ( header_height + real_y + (hh * 0.8))) {
-                                               pos = "below";
-                                       } else if (y > ( header_height + real_y + (hh * 0.2))) {
-                                               pos = "over";
-                                       } else {
-                                               pos = "above";
-                                               }
-                                        GLib.debug("getRowAt return : %d, %s", line_no, pos);
-                                           return line_no;
-                                   }
-                
-               
-                                   if (real_y + hh > y) {
-                                       return -1;
-                               }
-                               real_y += hh;
-                               child = child.get_next_sibling(); 
-                       }
-                       return -1;
-               
+                       var rn = 0;
+                       var cr = row;
+                        
+                       while (cr.get_prev_sibling() != null) {
+                               rn++;
+                               cr = cr.get_prev_sibling();
+                       }
+                       
+                       //GLib.debug("row number is %d", rn);
+                       //GLib.debug("click %d, %d", (int)x, (int)y);
+                       // above or belw
+                       Graphene.Rect  bounds;
+                       row.compute_bounds(this.el, out bounds);
+                       //GLib.debug("click x=%d, y=%d, w=%d, h=%d", 
+                       //      (int)bounds.get_x(), (int)bounds.get_y(),
+                       //      (int)bounds.get_width(), (int)bounds.get_height()
+                       //      );
+                       var ypos = y - bounds.get_y();
+                       //GLib.debug("rel ypos = %d", (int)ypos);       
+                       var rpos = 100.0 * (ypos / bounds.get_height());
+                       //GLib.debug("rel pos = %d %%", (int)rpos);
+                       pos = "over";
+                       
+                       if (rpos > 80) {
+                               pos = "below";
+                       } else if (rpos < 20) {
+                               pos = "above";
+                       } 
+                       return rn;
                 }
-               public Gtk.Widget? getWidgetAt (double x,  double in_y) {
-               /*
-                       
-               from            https://discourse.gnome.org/t/gtk4-finding-a-row-data-on-gtkcolumnview/8465
-                       var colview = gesture.widget;
-                       var line_no = check_list_widget(colview, x,y);
-                        if (line_no > -1) {
-                               var item = colview.model.get_item(line_no);
-                                
-                       }
-                       */
-                       var y = in_y + _this.viewwin.el.vadjustment.value; 
-                       var  child = this.el.get_first_child(); 
-                       //Gtk.Allocation alloc = { 0, 0, 0, 0 };
-                       var line_no = -1; 
-                       var reading_header = true;
-                       var curr_y = 0;
-                       var header_height  = 0;
-                       var h = 0;
-                       while (child != null) {
-                                       //GLib.debug("Got %s", child.get_type().name());
-                               if (reading_header) {
-                                               
+               public Gtk.Widget? getWidgetAt (double x,  double  y) {
                
-                                               if (child.get_type().name() != "GtkColumnListView") {
-                                               h += child.get_height();
-                                                       child = child.get_next_sibling();
-                                                       continue;
-                                               }
-                                               // should be columnlistview
-                                               child = child.get_first_child(); 
-                                           GLib.debug("header height=%d", h);
-                                               header_height =  h;
-                                               
-                                               reading_header = false;
-                                               continue;
-                               }
-                                   line_no++;
-               
-                                       if (y < header_height) {
-                                       return null;
-                               }
-               
-                                       var hh = child.get_height();
-                                       //GLib.debug("got cell xy = %d,%d  w,h= %d,%d", alloc.x, alloc.y, alloc.width, alloc.height);
-                                       if (child.has_css_class("node-err") || 
-                                               child.has_css_class("node-warn") || 
-                                               child.has_css_class("node-depr")) {
-                                               hh += 10;
-                                       
-                                       }       
-                                   if (y > curr_y && y <= header_height + hh + curr_y ) {
-                                           return (Gtk.Widget)child;
-                                   }
-                                   curr_y +=  hh ;
-               
-                                   if (curr_y > y) {
-                                       return null;
-                               }
-                               child = child.get_next_sibling(); 
-                       }
-                       return null;
+                       var w = this.el.pick(x, y, Gtk.PickFlags.DEFAULT);
+                       //GLib.debug("got widget %s", w == null ? "nothing" : w.get_type().name());
+                       if (w == null) {
+                               return null;
+                       }
+                       
+                       var row= w.get_ancestor(GLib.Type.from_name("GtkColumnViewRowWidget"));
+                       if (row == null) {
+                               return null;
+                       }
+                       return row;
+                
                
                 }
        }
@@ -944,7 +874,7 @@ public class Xcls_WindowLeftTree : Object
                        // my vars (dec)
                        this.highlightWidget = null;
                        this.lastDragNode = null;
-                       this.lastDragString = "";
+                       this.lastDragString = "\"\"";
 
                        // set gobject values
 
@@ -1138,7 +1068,7 @@ public class Xcls_WindowLeftTree : Object
                         
                                var is_shift = _this.keystate.is_shift > 0;
                                
-                               GLib.debug("shift is    %s", _this.keystate.is_shift > 0 ? "SHIFT" : "-");
+                               //GLib.debug("shift is    %s", _this.keystate.is_shift > 0 ? "SHIFT" : "-");
                                string pos; // over / before / after..
                        
                            //GLib.debug("got drag motion");
@@ -1149,7 +1079,7 @@ public class Xcls_WindowLeftTree : Object
                                try {
                                        cont.get_value(ref v);
                                } catch (GLib.Error e) {
-                                   GLib.debug("failed to get drag value");
+                                  // GLib.debug("failed to get drag value");
                                        return Gdk.DragAction.COPY;      
                                
                                }
@@ -1171,7 +1101,7 @@ public class Xcls_WindowLeftTree : Object
                             foreach(var dp in drop_on_to) {
                                str += dp;
                                }
-                               GLib.debug("droplist: %s", string.joinv(", ", str));
+                               //GLib.debug("droplist: %s", string.joinv(", ", str));
                             
                             
                            // if there are not items in the tree.. the we have to set isOver to true for anything..
@@ -1189,13 +1119,14 @@ public class Xcls_WindowLeftTree : Object
                            }
                            
                            
-                               GLib.debug("check is over");
+                       
                                 
                            // if path of source and dest are inside each other..
                            // need to add source info to drag?
                            // the fail();
                                var row = _this.view.getRowAt(x,y, out pos);
-                               
+                               //GLib.debug("check is over %d, %d, %s", (int)x,(int)y, pos);
+                       
                                if (row < 0) {
                                        this.addHighlight(null, "");    
                                        return Gdk.DragAction.COPY;
@@ -1204,20 +1135,20 @@ public class Xcls_WindowLeftTree : Object
                                
                                var node =  (JsRender.Node)tr.get_item();
                                
-                               GLib.debug("Drop over node: %s", node.fqn());
+                               //GLib.debug("Drop over node: %s", node.fqn());
                                
                        
                                if (pos == "above" || pos == "below") {
                                        if (node.parent == null) {
-                                               GLib.debug("no parent try center");
+                                               //GLib.debug("no parent try center");
                                                pos = "over";
                                        } else {
                                                 
                                                if (!drop_on_to.contains(node.parent.fqn())) {
-                                                       GLib.debug("drop on does not contain %s - try center" , node.parent.fqn());
+                                                       //GLib.debug("drop on does not contain %s - try center" , node.parent.fqn());
                                                        pos = "over";
                                                } else {
-                                                       GLib.debug("drop  contains %s - using %s" , node.parent.fqn(), pos);
+                                                       //GLib.debug("drop  contains %s - using %s" , node.parent.fqn(), pos);
                                                        if (_this.view.dragNode  != null && is_shift) {
                                                                if (node.parent.oid == _this.view.dragNode.oid || node.parent.has_parent(_this.view.dragNode)) {
                                                                        GLib.debug("shift drop not self not allowed");
@@ -1237,13 +1168,13 @@ public class Xcls_WindowLeftTree : Object
                                }
                                if (pos == "over") {
                                        if (!drop_on_to.contains(node.fqn())) {
-                                               GLib.debug("drop on does not contain %s - try center" , node.fqn());
+                                               //GLib.debug("drop on does not contain %s - try center" , node.fqn());
                                                this.addHighlight(null, ""); 
                                                return is_shift ?  Gdk.DragAction.MOVE :  Gdk.DragAction.COPY;          
                                        }
                                        if (_this.view.dragNode  != null && is_shift) {
                                                if (node.oid == _this.view.dragNode.oid || node.has_parent(_this.view.dragNode)) {
-                                                       GLib.debug("shift drop not self not allowed");
+                                                       //GLib.debug("shift drop not self not allowed");
                                                        this.addHighlight(null, "");
                                                        return Gdk.DragAction.COPY;     
                                                }
@@ -1263,11 +1194,14 @@ public class Xcls_WindowLeftTree : Object
                        });
                        this.el.drop.connect( (v, x, y) => {
                                
+                               // must get the pos before we clear the hightlihg.
+                               var pos = "";
+                               var row = _this.view.getRowAt(x,y, out pos);
                                this.addHighlight(null,"");
                         
                                var is_shift = _this.keystate.is_shift > 0;
                         
-                               var pos = "";
+                       
                                // -- get position..
                                if (this.lastDragString != v.get_string() || this.lastDragNode == null) {
                                        // still dragging same node
@@ -1279,7 +1213,10 @@ public class Xcls_WindowLeftTree : Object
                                     
                               
                            var dropNode = new JsRender.Node(); 
-                               dropNode.loadFromJsonString(v.get_string(), 1);
+                               dropNode.loadFromJsonString(v.get_string(), 2);
+                               GLib.debug("dropped node %s", dropNode.toJsonString());
+                               
+                               
                                var drop_on_to = _this.main_window.windowstate.file.palete().getDropList(dropNode.fqn());
                           
                            // if there are not items in the tree.. the we have to set isOver to true for anything..
@@ -1291,7 +1228,7 @@ public class Xcls_WindowLeftTree : Object
                                                return false;   
                                        }
                                        // add new node to top..
-                                       
+                                       GLib.debug("adding to top");
                                        
                                         var m = (GLib.ListStore) _this.model.el.model;
                                _this.main_window.windowstate.file.tree = dropNode;  
@@ -1300,14 +1237,16 @@ public class Xcls_WindowLeftTree : Object
                                        m.append(dropNode);
                                        _this.model.selectNode(dropNode);       
                                        _this.changed();
+                                       _this.node_selected(dropNode);
                                        return true; // no need to highlight?
                             
                            }
                        
                        
                        
-                               var row = _this.view.getRowAt(x,y, out pos);
+                       
                                if (row < 0) {
+                                       GLib.debug("could not get row %d,%d, %s", (int)x,(int)y,pos);
                                        return   false; //Gdk.DragAction.COPY;
                                }
                                var tr = (Gtk.TreeListRow)_this.view.el.model.get_object(row);
@@ -1349,12 +1288,12 @@ public class Xcls_WindowLeftTree : Object
                                
                                switch(pos) {
                                        case "over":
-                                               node.appendChild(dropNode);
+                       
                                                if (is_shift && _this.view.dragNode != null) {
                                                        _this.model.selectNode(null); 
                                                        _this.view.dragNode.remove();
                                                }
-                                                       
+                                               node.appendChild(dropNode);                     
                                                dropNode.updated_count++;
                                                _this.model.selectNode(dropNode); 
                                                
@@ -1364,11 +1303,12 @@ public class Xcls_WindowLeftTree : Object
                                        case "above":
                                                GLib.debug("Above - insertBefore");
                                        
-                                               node.parent.insertBefore(dropNode, node);
+                       
                                                if (is_shift && _this.view.dragNode != null) {
                                                        _this.model.selectNode(null);                   
                                                        _this.view.dragNode.remove();
                                                }
+                                               node.parent.insertBefore(dropNode, node);                       
                                                dropNode.updated_count++;
                                                _this.model.selectNode(dropNode);                       
                                                _this.changed();
@@ -1406,7 +1346,7 @@ public class Xcls_WindowLeftTree : Object
                public void addHighlight (Gtk.Widget? w, string hl) {
                        if (this.highlightWidget != null) {
                                var ww  = this.highlightWidget;
-                               GLib.debug("clear drag from previous highlight");
+                               //GLib.debug("clear drag from previous highlight");
                                if (ww.has_css_class("drag-below")) {
                                         ww.remove_css_class("drag-below");
                                }
@@ -1418,7 +1358,7 @@ public class Xcls_WindowLeftTree : Object
                                }
                        }
                        if (w != null) {
-                               GLib.debug("add drag=%s to widget", hl);        
+                               //GLib.debug("add drag=%s to widget", hl);      
                                if (!w.has_css_class("drag-" + hl)) {
                                        w.add_css_class("drag-" + hl);
                                }
@@ -1446,6 +1386,7 @@ public class Xcls_WindowLeftTree : Object
                        // my vars (dec)
 
                        // set gobject values
+                       this.el.can_unselect = true;
 
                        //listeners
                        this.el.selection_changed.connect( (position, n_items) => {
@@ -1500,7 +1441,7 @@ public class Xcls_WindowLeftTree : Object
                                    // why dup_?
                                    
                        
-                                   GLib.debug ("calling left_tree.node_selected");
+                                   GLib.debug ("calling left_tree.node_selected %s", snode.toJsonString());
                                    _this.node_selected(snode);
                                   
                                     
@@ -1517,8 +1458,11 @@ public class Xcls_WindowLeftTree : Object
                public JsRender.Node? getSelectedNode () {
                  if (this.el.selected_item == null) {
                                return null;
-                 }                             
+                 }     
+                  
+                 
                   var tr = (Gtk.TreeListRow)this.el.selected_item;
+                 
                   return (JsRender.Node)tr.get_item();
                         
                }
@@ -1591,13 +1535,14 @@ public class Xcls_WindowLeftTree : Object
                }
                public int nodeToRow (JsRender.Node node) 
                {
-                       var row = -1;
+                
                        var s = _this.view.el.model as Gtk.SingleSelection;
                        for (var i = 0; i < s.n_items; i++) {
                                //GLib.debug("check node %s", s.get_item(i).get_type().name());
                                var lr = s.get_item(i) as Gtk.TreeListRow;
                                //GLib.debug("check node %s", lr.get_item().get_type().name());
-                               if ((lr.get_item() as JsRender.Node).oid == node.oid) {
+                               var nn = (lr.get_item() as JsRender.Node);
+                               if (nn != null && nn.oid == node.oid) {
                                        return i;
                                        
                                }
@@ -1618,10 +1563,16 @@ public class Xcls_WindowLeftTree : Object
                             return;
                     }
                    _this.selmodel.el.unselect_all();
-                   
-                   node.remove();
-                       GLib.debug("delete Selected - done");
-                   _this.changed();
+                   if (node.parent != null) {
+                               node.remove();
+                               GLib.debug("delete Selected - done");
+                               _this.changed();
+                               return;
+                       }
+                       this.updateModel(null);
+                       _this.main_window.windowstate.file.tree = null;
+                       _this.changed();
+                       _this.node_selected(null);
                /*    
                    print("DELETE SELECTED?");
                    //_this.view.blockChanges = true;
@@ -1688,14 +1639,20 @@ public class Xcls_WindowLeftTree : Object
                        _this.selmodel.el.set_model(this.el);
                        return this.el;
                }
-               public void selectNode (JsRender.Node node) 
+               public void selectNode (JsRender.Node node) 
                {
-                       var row = this.nodeToRow(node);
                        var s = _this.view.el.model as Gtk.SingleSelection;
+                       if (node == null) {
+                               s.selected=Gtk.INVALID_LIST_POSITION;
+                               return;
+                       }
+                       var row = this.nodeToRow(node);
+               
                         
                        if (row < 0) {
                                // select none?
                                GLib.debug("Could not find node");
+                               s.selected=Gtk.INVALID_LIST_POSITION;
                                return;
                        }
                        GLib.debug("Select %d", row);
@@ -1779,7 +1736,7 @@ public class Xcls_WindowLeftTree : Object
                                
                        });
                        this.el.bind.connect( (listitem) => {
-                                GLib.debug("listitme is is %s", ((Gtk.ListItem)listitem).get_type().name());
+                               // GLib.debug("listitme is is %s", ((Gtk.ListItem)listitem).get_type().name());
                                
                                //var expand = (Gtk.TreeExpander) ((Gtk.ListItem)listitem).get_child();
                                var expand = (Gtk.TreeExpander)  ((Gtk.ListItem)listitem).get_child();
@@ -1793,8 +1750,10 @@ public class Xcls_WindowLeftTree : Object
                                
                                var lr = (Gtk.TreeListRow)((Gtk.ListItem)listitem).get_item();
                                var node = (JsRender.Node) lr.get_item();
-                               
-                          GLib.debug("node is %s", node.get_type().name());
+                               if (node == null || node.fqn() == "") {
+                                       return;
+                               }
+                          //GLib.debug("node is %s", node.get_type().name());
                        // was item (1) in old layout
                        
                                
@@ -1942,6 +1901,7 @@ public class Xcls_WindowLeftTree : Object
 
                        // set gobject values
                        var child_1 = new Xcls_Box18( _this );
+                       child_1.ref();
                        this.el.child = child_1.el;
                }