Fix #8000 - left tree and file open
[roobuilder] / src / Builder4 / WindowLeftTree.bjs
index 148220b..f2ea2be 100644 (file)
@@ -3,7 +3,7 @@
  "gen_extended" : false,
  "items" : [
   {
-   "# Xcls_MainWindow main_window" : "null",
+   "# Xcls_MainWindow? main_window" : "null",
    "$ xns" : "Gtk",
    "@ bool before_node_change" : "()",
    "@ void changed" : "()",
@@ -12,6 +12,7 @@
    "bool hexpand" : true,
    "bool vexpand" : true,
    "id" : "WindowLeftTree",
+   "int last_error_counter" : "-1",
    "items" : [
     {
      "$ xns" : "Gtk",
     },
     {
      "$ xns" : "Gtk",
-     "* init" : [
-      " this.el.set_policy (Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);",
-      " "
-     ],
+     "Gtk.PolicyType hscrollbar_policy" : "Gtk.PolicyType.AUTOMATIC",
+     "Gtk.PolicyType vscrollbar_policy" : "Gtk.PolicyType.AUTOMATIC",
      "bool has_frame" : true,
      "bool hexpand" : true,
      "bool vexpand" : true,
@@ -37,6 +36,7 @@
      "items" : [
       {
        "# bool blockChanges" : false,
+       "$ JsRender.Node? dragNode" : "null",
        "$ string lastEventSource" : "\"\"",
        "$ xns" : "Gtk",
        "* init" : [
         " border-top-style: solid;",
         " border-top-color: #88a3bc;",
         "}",
+        ".node-err  {",
+        " border-top-width: 5px;",
+        " border-top-style: solid;",
+        " border-top-color: red;",
+        " border-bottom-width: 5px; ",
+        " border-bottom-style: solid;",
+        " border-bottom-color: red;",
+        "}",
+        ".node-warn  {",
+        " border-top-width: 5px;",
+        " border-top-style: solid;",
+        " border-top-color: #ABF4EB;",
+        " border-bottom-width: 5px; ",
+        " border-bottom-style: solid;",
+        " border-bottom-color: #ABF4EB;",
+        "}",
+        ".node-depr  {",
+        " border-top-width: 5px;",
+        " border-top-style: solid;",
+        " border-top-color: #EEA9FF;",
+        " border-bottom-width: 5px; ",
+        " border-bottom-style: solid;",
+        " border-bottom-color: #EEA9FF;",
+        "}",
+        "",
         "#left-tree-view indent {",
         "-gtk-icon-size : 2px;",
         "}",
            "    \tGLib.warning(\"No node found at row %d\", row);",
            "    \treturn;",
            "\t}",
+           "\t",
+           "\t",
            "\t_this.model.selectNode(node);",
            "     ",
            "     ",
            "\tGLib.debug(\"SOURCE: drag-begin\");",
            "\t ",
            "    // find what is selected in our tree...",
-           "   var data = _this.selmodel.getSelectedNode();",
+           "    var data = _this.selmodel.getSelectedNode();",
            "\tif (data == null) {",
            "\t\treturn  ;",
            "\t}",
-           "\t ",
+           "\t_this.view.dragNode = data;",
            "    var xname = data.fqn();",
            "    GLib.debug (\"XNAME  IS %s\", xname);",
            "",
            " ",
            "}"
           ],
+          "drag_cancel" : [
+           "(drag, reason) => {",
+           "",
+           "\t_this.view.dragNode = null;",
+           "\treturn true;",
+           "}",
+           ""
+          ],
+          "drag_end" : [
+           "(drag, delete_data) => {",
+           "",
+           "_this.view.dragNode = null;",
+           "}",
+           ""
+          ],
           "prepare" : [
            "(x, y) => {",
            "",
          },
          "xtype" : "EventControllerKey"
         },
+        {
+         "$ xns" : "Gtk",
+         "id" : "keystate",
+         "int is_shift" : 0,
+         "listeners" : {
+          "key_pressed" : [
+           "(keyval, keycode, state) => {",
+           "",
+           " \tif (keyval == Gdk.Key.Shift_L || keyval == Gdk.Key.Shift_R) {",
+           " \t\tthis.is_shift = 1;",
+           "\t}",
+           "\treturn true;",
+           "}",
+           ""
+          ],
+          "key_released" : [
+           "(keyval, keycode, state) => {",
+           "\tGLib.debug(\"key release %d, %d, %d\" , (int) keyval, (int)  keycode, state);",
+           " \tif (keyval == Gdk.Key.Shift_L || keyval == Gdk.Key.Shift_R) {",
+           " \t\tthis.is_shift = 0;",
+           "\t}",
+           "\t//GLib.debug(\"set state %d , shift = %d\", (int)this.el.get_current_event_state(), Gdk.ModifierType.SHIFT_MASK);",
+           "",
+           "",
+           " ",
+           "}"
+          ]
+         },
+         "xtype" : "EventControllerKey"
+        },
         {
          "$ xns" : "Gtk",
          "* ctor" : [
            "\t",
            "\tthis.addHighlight(null,\"\");",
            " ",
-           " ",
+           " \tvar is_shift = _this.keystate.is_shift > 0;",
            " ",
            " \tvar pos = \"\";",
            " \t// -- get position..",
            "\t\t\t\tpos = \"over\";",
            " \t\t\t} else {",
            "\t\t\t\tGLib.debug(\"drop  contains %s - using %s\" , node.parent.fqn(), pos);",
+           "\t\t\t\tif (_this.view.dragNode  != null && is_shift) {",
+           "\t\t \t\t\tif (node.parent.oid == _this.view.dragNode.oid || node.parent.has_parent(_this.view.dragNode)) {",
+           "\t\t\t \t\t\tGLib.debug(\"shift drop not self not allowed\");",
+           "  \t\t\t\t\t\treturn false;\t",
+           "\t\t \t\t\t}",
+           "\t\t \t\t\t",
+           "\t\t \t\t}",
+           "\t\t\t\t",
+           "\t\t\t\t",
            "\t\t\t}",
            " \t\t}",
            " \t\t",
            "\t\t\treturn false;",
            "",
            "\t\t}",
+           "\t\tif (node.oid == _this.view.dragNode.oid || node.has_parent(_this.view.dragNode)) {",
+           " \t\t\tGLib.debug(\"shift drop not self not allowed\");",
+           "\t\t\treturn false;\t",
+           "\t\t}",
            "\t}",
            " \t",
            " \tswitch(pos) {",
            " \t\tcase \"over\":",
            "\t \t\tnode.appendChild(dropNode);",
+           "\t \t\tif (is_shift && _this.view.dragNode != null) {",
+           "\t\t \t\t_this.model.selectNode(null); ",
+           "\t\t \t\t_this.view.dragNode.remove();",
+           "\t \t\t}",
+           "\t \t\t\t",
+           "\t \t\t",
            " \t\t\t_this.model.selectNode(dropNode); ",
+           " \t\t\t",
            " \t\t\t_this.changed();\t\t\t\t \t\t",
            "\t \t\treturn true;",
            "\t \t\t",
            " \t\tcase \"above\":",
            " \t\t\tGLib.debug(\"Above - insertBefore\");",
            " \t\t",
-           " \t\t\tnode.parent.insertBefore(dropNode, node);",
+           "\t\t\tnode.parent.insertBefore(dropNode, node);",
+           "\t \t\tif (is_shift && _this.view.dragNode != null) {",
+           "\t\t \t\t_this.model.selectNode(null); \t \t\t",
+           "\t\t \t\t_this.view.dragNode.remove();",
+           "\t \t\t}",
+           "\t",
            " \t\t\t_this.model.selectNode(dropNode); \t\t\t",
            " \t\t\t_this.changed();",
            " \t\t\treturn true;",
            " \t\t\t",
            " \t\tcase \"below\":",
            " \t\t\tGLib.debug(\"Below - insertAfter\"); \t\t",
+           "\t \t\tif (is_shift && _this.view.dragNode != null) {",
+           "\t\t \t\t_this.model.selectNode(null); \t \t\t",
+           "\t\t \t\t_this.view.dragNode.remove();",
+           "\t \t\t}",
+           "\t",
+           " \t\t\t",
            " \t\t\tnode.parent.insertAfter(dropNode, node);",
            " \t\t\t_this.model.selectNode(dropNode);\t",
            " \t\t\t_this.changed();",
           "motion" : [
            "(  x, y) => {",
            " ",
+           "\tvar is_shift = _this.keystate.is_shift > 0;",
+           "\t",
+           "\tGLib.debug(\"shift is    %s\", _this.keystate.is_shift > 0 ? \"SHIFT\" : \"-\");",
            "\tstring pos; // over / before / after..",
            "",
-           "    GLib.debug(\"got drag motion\");",
+           "    //GLib.debug(\"got drag motion\");",
            "",
            "    GLib.Value v = GLib.Value(typeof(string));",
            "   \t//var str = drop.read_text( [ \"text/plain\" ] 0);",
            "\t\treturn Gdk.DragAction.COPY;\t ",
            "\t",
            "\t}",
-           "",
-           "\tGLib.debug(\"got %s\", v.get_string());",
+           " ",
+           "\t//GLib.debug(\"got %s\", v.get_string());",
            "\t  ",
            "\tif (this.lastDragString != v.get_string() || this.lastDragNode == null) {",
            "\t\t// still dragging same node",
            "\t \t\t\tpos = \"over\";",
            " \t\t\t} else {",
            "\t\t\t\tGLib.debug(\"drop  contains %s - using %s\" , node.parent.fqn(), pos);",
+           "\t\t\t\tif (_this.view.dragNode  != null && is_shift) {",
+           "\t\t \t\t\tif (node.parent.oid == _this.view.dragNode.oid || node.parent.has_parent(_this.view.dragNode)) {",
+           "\t\t\t \t\t\tGLib.debug(\"shift drop not self not allowed\");",
+           "\t\t \t\t\t\tthis.addHighlight(null, \"\");",
+           "\t\t \t\t\t\treturn Gdk.DragAction.COPY;\t",
+           "\t\t \t\t\t}",
+           "\t\t \t\t\t",
+           "\t\t \t\t}",
+           "\t\t\t\t",
            "\t\t\t}",
+           "\t\t\t",
+           "\t\t\t",
+           "\t\t\t",
            " \t\t}",
            " \t\t",
+           " \t\t",
            " \t}",
            " \tif (pos == \"over\") {",
            "\t \tif (!drop_on_to.contains(node.fqn())) {",
            "\t\t\tGLib.debug(\"drop on does not contain %s - try center\" , node.fqn());",
            "\t\t\tthis.addHighlight(null, \"\"); ",
-           "\t\t\treturn Gdk.DragAction.COPY;\t\t",
+           "\t\t\treturn is_shift ?  Gdk.DragAction.MOVE :  Gdk.DragAction.COPY;\t\t",
            "\t\t}",
+           "\t\tif (_this.view.dragNode  != null && is_shift) {",
+           " \t\t\tif (node.oid == _this.view.dragNode.oid || node.has_parent(_this.view.dragNode)) {",
+           "\t \t\t\tGLib.debug(\"shift drop not self not allowed\");",
+           " \t\t\t\tthis.addHighlight(null, \"\");",
+           " \t\t\t\treturn Gdk.DragAction.COPY;\t",
+           " \t\t\t}",
+           "\t\t}",
+           " \t\t\t",
            "\t}",
            " \t",
            " \t",
            " \t    // _this.view.highlightDropPath(\"\", (Gtk.TreeViewDropPosition)0);",
            "\tvar w = _this.view.getWidgetAt(x,y);",
            "\tthis.addHighlight(w, pos); ",
-           "    return Gdk.DragAction.COPY;\t\t\t",
+           "\treturn is_shift ?  Gdk.DragAction.MOVE :  Gdk.DragAction.COPY;\t\t",
            "}"
           ]
          },
             "\treturn this.el;",
             "}"
            ],
+           "| int nodeToRow" : [
+            "(JsRender.Node node) ",
+            "{",
+            "\tvar row = -1;",
+            "\tvar s = _this.view.el.model as Gtk.SingleSelection;",
+            "\tfor (var i = 0; i < s.n_items; i++) {",
+            "\t\t//GLib.debug(\"check node %s\", s.get_item(i).get_type().name());",
+            "\t\tvar lr = s.get_item(i) as Gtk.TreeListRow;",
+            "\t\tGLib.debug(\"check node %s\", lr.get_item().get_type().name());",
+            "\t\tif ((lr.get_item() as JsRender.Node).oid == node.oid) {",
+            "\t\t\treturn i;",
+            "\t\t\t",
+            "\t\t}",
+            "\t}",
+            "\treturn -1;\t\t\t",
+            "\t",
+            "",
+            "}"
+           ],
            "| void deleteSelected" : [
             "() {",
             "",
             "    _this.main_window.windowstate.leftTreeNodeSelected(null);",
             "    // needed???",
             "    _this.main_window.windowstate.file = f;",
-            "    ",
+            "    _this.last_error_counter = -1;",
             "   ",
             "    if (f.tree == null) {",
             "\t    try {",
             "    // if it's still null?",
             "    if (f.tree == null) {",
             "\t\t_this.main_window.windowstate.showAddObject(_this.view.el, null);",
-            "    ",
+            "    \t_this.updateErrors();",
             "        return;",
             "    }",
             "  \tm.append(f.tree);",
-            "  \t// expand???",
-            "",
-            "/*",
-            "    if (f.tree.readItems().size < 1) {",
-            "        // single item..",
-            "        ",
-            "        //this.get('/Window.leftvpaned').el.set_position(80);",
-            "        // select first...",
-            "        _this.view.el.set_cursor( ",
-            "            new  Gtk.TreePath.from_string(\"0\"), null, false);",
-            "        ",
-            "        ",
-            "    } else {",
-            "          //this.get('/Window.leftvpaned').el.set_position(200);",
-            "    }",
-            "  */  ",
-            "    ",
-            "    ",
-            "",
-            "    //_this.maincol.el.set_max_width(_this.viewwin.el.get_allocated_width() - 32);",
+            "\t_this.updateErrors();",
             " ",
             "    _this.selmodel.el.set_selected(Gtk.INVALID_LIST_POSITION);",
             "   ",
            "| void selectNode" : [
             "(JsRender.Node node) ",
             "{",
-            "\tvar row = -1;",
-            "\tvar s = (Gtk.SingleSelection)_this.view.el.model;",
-            "\tfor (var i = 0; i < s.n_items; i++) {",
-            "\t\t//GLib.debug(\"check node %s\", s.get_item(i).get_type().name());",
-            "\t\tvar lr = (Gtk.TreeListRow)s.get_item(i);",
-            "\t\tGLib.debug(\"check node %s\", lr.get_item().get_type().name());",
-            "\t\tif (((JsRender.Node)lr.get_item()).oid == node.oid) {",
-            "\t\t\trow  = i;",
-            "\t\t\tbreak;",
-            "\t\t}",
-            "\t}",
+            "\tvar row = this.nodeToRow(node);",
+            "\tvar s = _this.view.el.model as Gtk.SingleSelection;",
+            "\t ",
             "\tif (row < 0) {",
             "\t\t// select none?",
             "\t\tGLib.debug(\"Could not find node\");",
         "\t\t\t\theader_height =  h;",
         "\t\t\t\t",
         "\t\t\t\treading_header = false;",
-        "\t\t\t\t",
+        "\t\t\t\tcontinue;",
         "\t        }",
         "\t\t    line_no++;",
         "",
         "",
         "\t\t\tvar hh = child.get_height();",
         "\t\t\t//GLib.debug(\"got cell xy = %d,%d  w,h= %d,%d\", alloc.x, alloc.y, alloc.width, alloc.height);",
-        "",
+        "\t\t\tif (child.has_css_class(\"node-err\") || ",
+        "\t\t\t\tchild.has_css_class(\"node-warn\") || ",
+        "\t\t\t\tchild.has_css_class(\"node-depr\")) {",
+        "\t\t\t\thh += 10;",
+        "\t\t\t",
+        "\t\t\t}\t",
         "\t\t    if (y > curr_y && y <= header_height + hh + curr_y ) {",
         "\t\t\t    return (Gtk.Widget)child;",
         "\t\t    }",
         "\t\t\t ",
         "\t\t\t\t",
         "\t\t\t\treading_header = false;",
-        "\t\t\t\t ",
+        "\t\t\t\tcontinue;",
         "\t\t    }",
         "\t\t    ",
         "\t\t  ",
         "\t\t\t\t}",
         "\t\t\t\t// should be columnlistview",
         "\t\t\t\tchild = child.get_first_child(); ",
-        "\t\t\t    GLib.debug(\"header height=%d\", h);",
+        "\t\t\t    //GLib.debug(\"header height=%d\", h);",
         "\t\t\t\theader_height =  h;",
         "\t\t\t\t",
         "\t\t\t\treading_header = false;",
+        "\t\t\t\tcontinue;",
         "\t\t\t\t",
         "\t        }",
         "\t        ",
         "\t\t    ",
         "\t\t    line_no++;",
         "\t\t\tvar hh = child.get_height();",
+        "\t\t\t",
+        "\t\t\tif (child.has_css_class(\"node-err\") || ",
+        "\t\t\t\tchild.has_css_class(\"node-warn\") || ",
+        "\t\t\t\tchild.has_css_class(\"node-depr\")) {",
+        "\t\t\t\thh += 10;",
+        "\t\t\t",
+        "\t\t\t}",
         "\t\t\t//child.get_allocation(out alloc);",
         "\t\t\t//GLib.debug(\"got cell xy = %d,%d  w,h= %d,%d\", alloc.x, alloc.y, alloc.width, alloc.height);",
         "\t\t\t//GLib.debug(\"row %d y= %d %s\", line_no, (int) (header_height + alloc.y),",
     "}",
     "",
     ""
+   ],
+   "| void removeErrors" : [
+    "() {",
+    "\tvar  child = this.view.el.get_first_child(); ",
+    " ",
+    "\tvar reading_header = true;",
+    " ",
+    "\twhile (child != null) {",
+    "\t\t//GLib.debug(\"Got %s\", child.get_type().name());",
+    "\t   ",
+    "\t   if (reading_header) {",
+    "\t\t\t",
+    "",
+    "\t\t\tif (child.get_type().name() != \"GtkColumnListView\") {",
+    "\t\t\t   ",
+    "\t\t\t\tchild = child.get_next_sibling();",
+    "\t\t\t\tcontinue;",
+    "\t\t\t}",
+    "\t\t\t// should be columnlistview",
+    "\t\t\tchild = child.get_first_child(); ",
+    "\t\t ",
+    "\t\t ",
+    "\t\t\t",
+    "\t\t\treading_header = false;",
+    "\t\t\t ",
+    "\t    }",
+    "\t    ",
+    "\t  \tif (child.has_css_class(\"node-err\")) {",
+    "\t\t\tchild.remove_css_class(\"node-err\");",
+    "\t\t}",
+    "\t\tif (child.has_css_class(\"node-warn\")) {",
+    "\t\t\tchild.remove_css_class(\"node-warn\");",
+    "\t\t}",
+    "\t\t",
+    "\t\tif (child.has_css_class(\"node-depr\")) {",
+    "\t\t\tchild.remove_css_class(\"node-depr\");",
+    "\t\t}",
+    "\t\t",
+    "        child = child.get_next_sibling(); ",
+    "\t}",
+    "\t//GLib.debug(\"Rturning null\");",
+    "     ",
+    "}"
+   ],
+   "| void updateErrors" : [
+    "() {",
+    "\tvar file = this.getActiveFile();",
+    "\tvar ar = file.getErrors();",
+    "\t\tif (ar.size < 1) {",
+    "\t\tif (this.last_error_counter != file.error_counter) {",
+    "\t\t\tthis.removeErrors();",
+    "\t\t}",
+    "\t",
+    "\t\tthis.last_error_counter = file.error_counter ;",
+    "",
+    "\t\treturn;",
+    "\t}",
+    " \tif (this.last_error_counter == file.error_counter) {",
+    "\t\treturn;",
+    "\t}",
+    "\tthis.removeErrors();",
+    "\t",
+    "\tforeach(var diag in ar) { ",
+    "\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);",
+    "    \tif (w == null) {",
+    "    \t\treturn;",
+    "\t\t}",
+    "\t\t// always show errors.",
+    "\t\tvar ed = diag.category.down();",
+    "\t\tif (ed != \"err\" && w.has_css_class(\"node-err\")) {",
+    "\t\t\tcontinue;",
+    "\t\t}",
+    "\t\tif (ed == \"err\" && w.has_css_class(\"node-warn\")) {",
+    "\t\t\tw.remove_css_class(\"node-warn\");",
+    "\t\t}",
+    "\t\tif (ed == \"err\" && w.has_css_class(\"node-depr\")) {",
+    "\t\t\tw.remove_css_class(\"node-depr\");",
+    "\t\t}",
+    "\t\tif (!w.has_css_class(\"node-\"+ ed)) {",
+    "\t\t\tw.add_css_class(\"node-\" + ed);",
+    "\t\t}",
+    "\t\t",
+    "\t}",
+    "\t",
+    "}"
    ]
   }
  ],