Bug 552096: [GTK] Tree.showItem does not work reliably
Remove geometric calculations as they cannot be relied upon to be accurate.
Just call gtk_tree_view_scroll_to_cell() directly as Table does.
Tested on GTK3.24 and 3.10 using the snippet attached and a child Eclipse.
No AllNonBrowser JUnit tests fail.
Change-Id: I423943f349be63a637891e673ab4df34013c194c
Signed-off-by: Eric Williams <ericwill@redhat.com>
diff --git a/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/Tree.java b/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/Tree.java
index 411b8e0..a199f99 100644
--- a/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/Tree.java
+++ b/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/Tree.java
@@ -4121,22 +4121,7 @@
GTK.gtk_tree_path_free (tempPath);
}
if (scroll) {
- GdkRectangle cellRect = new GdkRectangle ();
- GTK.gtk_widget_realize (handle);
- GTK.gtk_tree_view_get_cell_area (handle, path, 0, cellRect);
- boolean isHidden = cellRect.y == 0 && cellRect.height == 0;
- int [] tx = new int [1], ty = new int [1];
- GTK.gtk_tree_view_convert_bin_window_to_tree_coords(handle, cellRect.x, cellRect.y, tx, ty);
- if (!isHidden) {
- GdkRectangle visibleRect = new GdkRectangle ();
- GTK.gtk_tree_view_get_visible_rect (handle, visibleRect);
- if (ty [0] < visibleRect.y || ty [0] + cellRect.height > visibleRect.y + visibleRect.height) {
- isHidden = true;
- }
- }
- if (isHidden) {
- GTK.gtk_tree_view_scroll_to_cell (handle, path, 0, depth != 1, 0.5f, 0.0f);
- }
+ GTK.gtk_tree_view_scroll_to_cell (handle, path, 0, depth != 1, 0.5f, 0.0f);
}
}
diff --git a/tests/org.eclipse.swt.tests.gtk/ManualTests/org/eclipse/swt/tests/gtk/snippets/Bug552096_TreeShowItemUnreliable.java b/tests/org.eclipse.swt.tests.gtk/ManualTests/org/eclipse/swt/tests/gtk/snippets/Bug552096_TreeShowItemUnreliable.java
new file mode 100644
index 0000000..a924e5f
--- /dev/null
+++ b/tests/org.eclipse.swt.tests.gtk/ManualTests/org/eclipse/swt/tests/gtk/snippets/Bug552096_TreeShowItemUnreliable.java
@@ -0,0 +1,63 @@
+/*******************************************************************************
+ * Copyright (c) 2019 Thomas Singer and others.
+ *
+ * This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License 2.0
+ * which accompanies this distribution, and is available at
+ * https://www.eclipse.org/legal/epl-2.0/
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ *
+ * Contributors:
+ * Thomas Singer - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.swt.tests.gtk.snippets;
+
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.layout.GridData;
+import org.eclipse.swt.layout.GridLayout;
+import org.eclipse.swt.widgets.Button;
+import org.eclipse.swt.widgets.Display;
+import org.eclipse.swt.widgets.Shell;
+import org.eclipse.swt.widgets.Tree;
+import org.eclipse.swt.widgets.TreeItem;
+
+public class Bug552096_TreeShowItemUnreliable {
+
+ public static void main(String[] args) {
+ final Display display = new Display();
+ final Shell shell = new Shell(display);
+ shell.setLayout(new GridLayout());
+
+ final Button button = new Button(shell, SWT.PUSH);
+ button.setText("Add");
+ button.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, false));
+
+ final Tree tree = new Tree(shell, SWT.BORDER);
+ tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
+
+ button.addListener(SWT.Selection, event -> {
+ final TreeItem rootNode = new TreeItem(tree, SWT.NONE, 0);
+ // Code below appends to the bottom of the Tree
+// final TreeItem rootNode = new TreeItem(tree, SWT.NONE, tree.getItemCount());
+ rootNode.setText("Root " + tree.getItemCount());
+
+ final TreeItem childItem = new TreeItem(rootNode, SWT.NONE);
+ childItem.setText("Child");
+ rootNode.setExpanded(true);
+
+ tree.showItem(rootNode);
+ });
+
+ shell.setSize(400, 300);
+ shell.open();
+
+ while (!shell.isDisposed()) {
+ if (!display.readAndDispatch()) {
+ display.sleep();
+ }
+ }
+
+ display.dispose();
+ }
+}