Fix #8099 - convert left tree to use get_data<> methods
authorAlan <alan@roojs.com>
Wed, 20 Mar 2024 05:38:46 +0000 (13:38 +0800)
committerAlan <alan@roojs.com>
Wed, 20 Mar 2024 05:38:46 +0000 (13:38 +0800)
src/Builder4/WindowLeftTree.bjs
src/Builder4/WindowLeftTree.vala

index 9bf6162..5c883b8 100644 (file)
            "\t    return ;",
            "    }",
            "    string pos;",
-           "    var row = _this.view.getRowAt(x,y, out pos );",
-           "    if (row < 0) {",
+           "    var row_widget = _this.view.getRowWidgetAt(x,y, out pos );",
+           "    if (row_widget == null) {",
            "\t    GLib.debug(\"no row selected items\");",
            "\t    return;",
            "    }",
            "    ",
-           "    var node =   _this.selmodel.getNodeAt(row);",
+           "    var node =   row_widget.get_data<JsRender.Node>(\"node\");",
            "    if (node == null) {",
-           "    \tGLib.warning(\"No node found at row %d\", row);",
+           "    \tGLib.warning(\"No node found bound to widget\");",
            "    \treturn;",
            "\t}",
            "",
            "\t    return ;",
            "    }",
            "    string pos;",
-           "    var row = _this.view.getRowAt(x,y, out pos );",
-           "    if (row < 0) {",
+           "    var row_widget = _this.view.getRowWidgetAt(x,y, out pos );",
+           "    if (row_widget == null) {",
            "\t    GLib.debug(\"no row selected items\");",
            "\t    return;",
            "    }",
            "    ",
-           "    var node =   _this.selmodel.getNodeAt(row);",
+           "    var node =  row_widget.get_data<JsRender.Node>(\"node\");",
            "    if (node == null) {",
-           "    \tGLib.warning(\"No node found at row %d\", row);",
+           "    \tGLib.warning(\"No node found from widget\");",
            "    \treturn;",
            "\t}",
            "\t",
            "    var xname = data.fqn();",
            "    GLib.debug (\"XNAME  IS %s\", xname);",
            "",
-           " \tvar widget = _this.view.getWidgetAtRow(_this.selmodel.el.selected);",
+           " \tvar widget = data.get_data<Gtk.Widget>(\"tree-row\");",
            " \t",
            " \t",
            "    var paintable = new Gtk.WidgetPaintable(widget);",
            "\t",
            "\t// must get the pos before we clear the hightlihg.",
            " \tvar pos = \"\";",
-           " \tvar row = _this.view.getRowAt(x,y, out pos);",
+           " \tvar row_widget = _this.view.getRowWidgetAt(x,y, out pos);",
            "\tthis.addHighlight(null,\"\");",
            " ",
            " \tvar is_shift = _this.keystate.is_shift > 0;",
            "",
            "",
            "",
-           "\tif (row < 0) {",
+           "\tif (row_widget == null) {",
            "\t\tGLib.debug(\"could not get row %d,%d, %s\", (int)x,(int)y,pos);",
            "\t\treturn   false; //Gdk.DragAction.COPY;",
            "\t}",
-           "\tvar tr = (Gtk.TreeListRow)_this.view.el.model.get_object(row);",
-           "\t",
-           "\tvar node =  (JsRender.Node)tr.get_item();",
+           " \t",
+           "\tvar node =  row_widget.get_data<JsRender.Node>(\"node\");",
            "",
            " \tif (pos == \"above\" || pos == \"below\") {",
            "\t\tif (node.parent == null) {",
            "    // if path of source and dest are inside each other..",
            "    // need to add source info to drag?",
            "    // the fail();",
-           " \tvar row = _this.view.getRowAt(x,y, out pos);",
+           " \t var row_widget = _this.view.getRowWidgetAt( x,y, out pos);    ",
+           "// \tvar row = _this.view.getRowAt(x,y, out pos);",
            " \t//GLib.debug(\"check is over %d, %d, %s\", (int)x,(int)y, pos);",
            "",
-           " \tif (row < 0) {",
+           " \tif (row_widget == null) {",
            "\t\tthis.addHighlight(null, \"\");\t",
            "\t \treturn Gdk.DragAction.COPY;",
            " \t}",
-           "\tvar tr = (Gtk.TreeListRow)_this.view.el.model.get_object(row);",
-           "\t",
-           "\tvar node =  (JsRender.Node)tr.get_item();",
+           " \tvar node = row_widget.get_data<JsRender.Node>(\"node\");",
            "\t",
            "\t//GLib.debug(\"Drop over node: %s\", node.fqn());",
            "\t",
            " \t",
            " \t",
            " \t    // _this.view.highlightDropPath(\"\", (Gtk.TreeViewDropPosition)0);",
-           "\tvar w = _this.view.getWidgetAt(x,y);",
-           "\tthis.addHighlight(w, pos); ",
+           "",
+           "\tthis.addHighlight(row_widget, pos); ",
            "\treturn is_shift ?  Gdk.DragAction.MOVE :  Gdk.DragAction.COPY;\t\t",
            "}"
           ]
              "\tif (node == null || node.fqn() == \"\") {",
              "\t\treturn;",
              "\t}",
+             "\t",
+             "\tnode.set_data<Gtk.Widget>(\"tree-row\", expand.get_parent().get_parent());",
+             "\texpand.get_parent().get_parent().set_data<JsRender.Node>(\"node\", node);",
+             "\t",
              "   //GLib.debug(\"node is %s\", node.get_type().name());",
              "// was item (1) in old layout",
              "",
        ],
        "string name" : "left-tree-view",
        "xtype" : "ColumnView",
-       "| Gtk.Widget? getWidgetAt" : [
-        "(double x,  double  y) {",
+       "| Gtk.Widget? getRowWidgetAt" : [
+        "(double x,  double  y, out string pos) {",
         "",
+        "\tpos = \"\";",
         "\tvar w = this.el.pick(x, y, Gtk.PickFlags.DEFAULT);",
         "\t//GLib.debug(\"got widget %s\", w == null ? \"nothing\" : w.get_type().name());",
         "\tif (w == null) {",
         "\t\treturn null;",
         "\t}",
         "\t",
-        "\tvar row= w.get_ancestor(GLib.Type.from_name(\"GtkColumnViewRowWidget\"));",
+        "\tvar row = w.get_ancestor(GLib.Type.from_name(\"GtkColumnViewRowWidget\"));",
         "\tif (row == null) {",
         "\t\treturn null;",
         "\t}",
+        "\t",
+        "\t//GLib.debug(\"got colview %s\", row == null ? \"nothing\" : row.get_type().name());",
+        "\t ",
+        "\t",
+        "\t",
+        "\t//GLib.debug(\"row number is %d\", rn);",
+        "\t//GLib.debug(\"click %d, %d\", (int)x, (int)y);",
+        "\t// above or belw",
+        "\tGraphene.Rect  bounds;",
+        "\trow.compute_bounds(this.el, out bounds);",
+        "\t//GLib.debug(\"click x=%d, y=%d, w=%d, h=%d\", ",
+        "\t//\t(int)bounds.get_x(), (int)bounds.get_y(),",
+        "\t//\t(int)bounds.get_width(), (int)bounds.get_height()",
+        "\t//\t);",
+        "\tvar ypos = y - bounds.get_y();",
+        "\t//GLib.debug(\"rel ypos = %d\", (int)ypos);\t",
+        "\tvar rpos = 100.0 * (ypos / bounds.get_height());",
+        "\t//GLib.debug(\"rel pos = %d %%\", (int)rpos);",
+        "\tpos = \"over\";",
+        "\t",
+        "\tif (rpos > 80) {",
+        "\t\tpos = \"below\";",
+        "\t} else if (rpos < 20) {",
+        "\t\tpos = \"above\";",
+        "\t} ",
         "\treturn row;",
-        " ",
-        "",
         " }"
        ],
-       "| Gtk.Widget? getWidgetAtRow" : [
+       "| Gtk.Widget? getWidgetAtRowBROKE" : [
         "(uint row) {",
         "/*",
         "    \t",
         "",
         " }"
        ],
-       "| int getRowAt" : [
+       "| int getRowAtOLD" : [
         "(double x,  double  y, out string pos) {",
         "",
         "\tpos = \"\";",
     "\tthis.removeErrors();",
     "\tthis.error_widgets = new Gee.ArrayList<Gtk.Widget>();",
     "\tforeach(var diag in ar) { ",
-    "\t",
-    "\t\t ",
+    "\t ",
     "//        print(\"get inter\\n\");",
     "\t    var node= file.lineToNode( (int)diag.range.start.line) ;",
     "\t    if (node == null) {",
     "\t    \tcontinue;",
     "    \t}",
-    "    \tvar row = _this.model.nodeToRow(node);",
-    "    \tif (row < 0) {",
-    "    \t\tcontinue;",
-    "\t\t}",
-    "    \tvar w = this.view.getWidgetAtRow(row);",
+    "    \tvar w = node.get_data<Gtk.Widget>(\"tree-row\");",
     "    \tif (w == null) {",
     "    \t\treturn;",
     "\t\t}",
index 8a08fdc..28ba407 100644 (file)
@@ -73,18 +73,13 @@ public class Xcls_WindowLeftTree : Object
                this.removeErrors();
                this.error_widgets = new Gee.ArrayList<Gtk.Widget>();
                foreach(var diag in ar) { 
-               
-                        
+                
        //        print("get inter\n");
                    var node= file.lineToNode( (int)diag.range.start.line) ;
                    if (node == null) {
                        continue;
                }
-               var row = _this.model.nodeToRow(node);
-               if (row < 0) {
-                       continue;
-                       }
-               var w = this.view.getWidgetAtRow(row);
+               var w = node.get_data<Gtk.Widget>("tree-row");
                if (w == null) {
                        return;
                        }
@@ -380,54 +375,45 @@ public class Xcls_WindowLeftTree : Object
                }
 
                // user defined functions
-               public Gtk.Widget? getWidgetAtRow (uint row) {
-               /*
-                       
-               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 %d", (int)row);
-                       var  child = this.el.get_first_child(); 
-                       var line_no = -1; 
-                       var reading_header = true;
-                        
-                       while (child != null) {
-                                       //GLib.debug("Got %s", child.get_type().name());
-                          
-                          if (reading_header) {
-                                               
-               
-                                               if (child.get_type().name() != "GtkColumnListView") {
-                                                  
-                                                       child = child.get_next_sibling();
-                                                       continue;
-                                               }
-                                               // should be columnlistview
-                                               child = child.get_first_child(); 
-                                        
-                                        
-                                               
-                                               reading_header = false;
-                                               continue;
-                                   }
-                                   
-                                 
-                           
-                                   line_no++;
-                                       if (line_no == row) {
-                                               //GLib.debug("Returning widget %s", child.get_type().name());
-                                           return (Gtk.Widget)child;
-                                   }
-                               child = child.get_next_sibling(); 
-                       }
-                               //GLib.debug("Rturning null");
-                       return null;
+               public Gtk.Widget? getRowWidgetAt (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 null;
+                       }
+                       
+                       var row = w.get_ancestor(GLib.Type.from_name("GtkColumnViewRowWidget"));
+                       if (row == null) {
+                               return null;
+                       }
+                       
+                       //GLib.debug("got colview %s", row == null ? "nothing" : row.get_type().name());
+                        
+                       
+                       
+                       //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 row;
                 }
                public int getColAt (double x,  double y) {
                        /*
@@ -462,7 +448,7 @@ public class Xcls_WindowLeftTree : Object
                    return -1;
                
                 }
-               public int getRowAt (double x,  double  y, out string pos) {
+               public int getRowAtOLD (double x,  double  y, out string pos) {
                
                        pos = "";
                        var w = this.el.pick(x, y, Gtk.PickFlags.DEFAULT);
@@ -508,20 +494,53 @@ public class Xcls_WindowLeftTree : Object
                        } 
                        return rn;
                 }
-               public Gtk.Widget? getWidgetAt (double x,  double  y) {
+               public Gtk.Widget? getWidgetAtRowBROKE (uint row) {
+               /*
+                       
+               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 %d", (int)row);
+                       var  child = this.el.get_first_child(); 
+                       var line_no = -1; 
+                       var reading_header = true;
+                        
+                       while (child != null) {
+                                       //GLib.debug("Got %s", child.get_type().name());
+                          
+                          if (reading_header) {
+                                               
                
-                       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;
-                
+                                               if (child.get_type().name() != "GtkColumnListView") {
+                                                  
+                                                       child = child.get_next_sibling();
+                                                       continue;
+                                               }
+                                               // should be columnlistview
+                                               child = child.get_first_child(); 
+                                        
+                                        
+                                               
+                                               reading_header = false;
+                                               continue;
+                                   }
+                                   
+                                 
+                           
+                                   line_no++;
+                                       if (line_no == row) {
+                                               //GLib.debug("Returning widget %s", child.get_type().name());
+                                           return (Gtk.Widget)child;
+                                   }
+                               child = child.get_next_sibling(); 
+                       }
+                               //GLib.debug("Rturning null");
+                       return null;
                
                 }
        }
@@ -573,15 +592,15 @@ public class Xcls_WindowLeftTree : Object
                                    return ;
                            }
                            string pos;
-                           var row = _this.view.getRowAt(x,y, out pos );
-                           if (row < 0) {
+                           var row_widget = _this.view.getRowWidgetAt(x,y, out pos );
+                           if (row_widget == null) {
                                    GLib.debug("no row selected items");
                                    return;
                            }
                            
-                           var node =   _this.selmodel.getNodeAt(row);
+                           var node =   row_widget.get_data<JsRender.Node>("node");
                            if (node == null) {
-                               GLib.warning("No node found at row %d", row);
+                               GLib.warning("No node found bound to widget");
                                return;
                                }
                        
@@ -642,15 +661,15 @@ public class Xcls_WindowLeftTree : Object
                                    return ;
                            }
                            string pos;
-                           var row = _this.view.getRowAt(x,y, out pos );
-                           if (row < 0) {
+                           var row_widget = _this.view.getRowWidgetAt(x,y, out pos );
+                           if (row_widget == null) {
                                    GLib.debug("no row selected items");
                                    return;
                            }
                            
-                           var node =   _this.selmodel.getNodeAt(row);
+                           var node =  row_widget.get_data<JsRender.Node>("node");
                            if (node == null) {
-                               GLib.warning("No node found at row %d", row);
+                               GLib.warning("No node found from widget");
                                return;
                                }
                                
@@ -755,7 +774,7 @@ public class Xcls_WindowLeftTree : Object
                            var xname = data.fqn();
                            GLib.debug ("XNAME  IS %s", xname);
                        
-                               var widget = _this.view.getWidgetAtRow(_this.selmodel.el.selected);
+                               var widget = data.get_data<Gtk.Widget>("tree-row");
                                
                                
                            var paintable = new Gtk.WidgetPaintable(widget);
@@ -1124,16 +1143,15 @@ public class Xcls_WindowLeftTree : Object
                            // 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);
+                                var row_widget = _this.view.getRowWidgetAt( x,y, out pos);    
+                       //      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) {
+                               if (row_widget == null) {
                                        this.addHighlight(null, "");    
                                        return Gdk.DragAction.COPY;
                                }
-                               var tr = (Gtk.TreeListRow)_this.view.el.model.get_object(row);
-                               
-                               var node =  (JsRender.Node)tr.get_item();
+                               var node = row_widget.get_data<JsRender.Node>("node");
                                
                                //GLib.debug("Drop over node: %s", node.fqn());
                                
@@ -1184,8 +1202,8 @@ public class Xcls_WindowLeftTree : Object
                                
                                
                                    // _this.view.highlightDropPath("", (Gtk.TreeViewDropPosition)0);
-                               var w = _this.view.getWidgetAt(x,y);
-                               this.addHighlight(w, pos); 
+                       
+                               this.addHighlight(row_widget, pos); 
                                return is_shift ?  Gdk.DragAction.MOVE :  Gdk.DragAction.COPY;          
                        });
                        this.el.leave.connect( ( ) => {
@@ -1196,7 +1214,7 @@ public class Xcls_WindowLeftTree : Object
                                
                                // must get the pos before we clear the hightlihg.
                                var pos = "";
-                               var row = _this.view.getRowAt(x,y, out pos);
+                               var row_widget = _this.view.getRowWidgetAt(x,y, out pos);
                                this.addHighlight(null,"");
                         
                                var is_shift = _this.keystate.is_shift > 0;
@@ -1245,13 +1263,12 @@ public class Xcls_WindowLeftTree : Object
                        
                        
                        
-                               if (row < 0) {
+                               if (row_widget == null) {
                                        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);
-                               
-                               var node =  (JsRender.Node)tr.get_item();
+                               
+                               var node =  row_widget.get_data<JsRender.Node>("node");
                        
                                if (pos == "above" || pos == "below") {
                                        if (node.parent == null) {
@@ -1753,6 +1770,10 @@ public class Xcls_WindowLeftTree : Object
                                if (node == null || node.fqn() == "") {
                                        return;
                                }
+                               
+                               node.set_data<Gtk.Widget>("tree-row", expand.get_parent().get_parent());
+                               expand.get_parent().get_parent().set_data<JsRender.Node>("node", node);
+                               
                           //GLib.debug("node is %s", node.get_type().name());
                        // was item (1) in old layout