Bug 379271 - Combo#getCaretLocation() is slightly too far down/right
Subtracted thickness from offset when calculating caret location for
Combo. Works on GTK3.
Change-Id: I0b781d3065f6ff65dae6d0ed1c894d0526a22f4f
Signed-off-by: Xi Yan <xixiyan@redhat.com>
diff --git a/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/Combo.java b/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/Combo.java
index ff04398..9265206 100644
--- a/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/Combo.java
+++ b/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/Combo.java
@@ -984,8 +984,9 @@
long /*int*/ layout = GTK.gtk_entry_get_layout (entryHandle);
PangoRectangle pos = new PangoRectangle ();
OS.pango_layout_index_to_pos (layout, index, pos);
- int x = offset_x [0] + OS.PANGO_PIXELS (pos.x) - getBorderWidthInPixels ();
- int y = offset_y [0] + OS.PANGO_PIXELS (pos.y);
+ Point thickness = getThickness (entryHandle);
+ int x = offset_x [0] + OS.PANGO_PIXELS (pos.x) - getBorderWidthInPixels () - thickness.x;
+ int y = offset_y [0] + OS.PANGO_PIXELS (pos.y) - thickness.y;
return new Point (x, y);
}
diff --git a/tests/org.eclipse.swt.tests.gtk/ManualTests/org/eclipse/swt/tests/gtk/snippets/Bug379271_ComboCaretLocationOffset.java b/tests/org.eclipse.swt.tests.gtk/ManualTests/org/eclipse/swt/tests/gtk/snippets/Bug379271_ComboCaretLocationOffset.java
new file mode 100644
index 0000000..478aea8
--- /dev/null
+++ b/tests/org.eclipse.swt.tests.gtk/ManualTests/org/eclipse/swt/tests/gtk/snippets/Bug379271_ComboCaretLocationOffset.java
@@ -0,0 +1,88 @@
+/*******************************************************************************
+ * Copyright (c) 2018 Red Hat and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Red Hat - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.swt.tests.gtk.snippets;
+
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.events.KeyEvent;
+import org.eclipse.swt.events.KeyListener;
+import org.eclipse.swt.graphics.Point;
+import org.eclipse.swt.graphics.Rectangle;
+import org.eclipse.swt.layout.FillLayout;
+import org.eclipse.swt.widgets.Combo;
+import org.eclipse.swt.widgets.Display;
+import org.eclipse.swt.widgets.Shell;
+
+public class Bug379271_ComboCaretLocationOffset {
+
+ static int width = 1; // default width on Mac and GTK
+ static int height = 0;
+
+ public static void main(String[] args) {
+ final Display display = new Display();
+ final Shell shell = new Shell(display);
+ final Shell hint = new Shell(shell, SWT.NO_TRIM);
+
+ shell.setLayout(new FillLayout());
+
+ final Combo combo = new Combo(shell, SWT.DROP_DOWN);
+ combo.addKeyListener(new KeyListener() {
+ @Override
+ public void keyReleased(KeyEvent e) {
+ if (e.keyCode == SWT.CR) {
+ combo.add(combo.getText());
+ }
+ }
+
+ @Override
+ public void keyPressed(KeyEvent e) {
+ System.out.println("caret position: " + combo.getCaretPosition());
+ System.out.println("caret location: " + combo.getCaretLocation());
+
+ display.asyncExec(() -> {
+ Point pt = combo.getCaretLocation();
+
+ /* Mapping to combo box, wrong location */
+ Rectangle comboRect = display.map(combo, null, pt.x, pt.y, width, height);
+
+ hint.setLocation(comboRect.x, comboRect.y);
+
+ hint.moveAbove(null);
+ hint.redraw();
+ });
+
+ }
+ });
+
+ combo.addListener(SWT.MouseMove, e -> {
+ System.out.println(e.x + " " + e.y);
+ });
+
+ height = combo.getFont().getFontData()[0].getHeight();
+ System.out.println("caret width: " + width + ", combo font height: " + height);
+
+ hint.setSize(width, height);
+ hint.setBackground(display.getSystemColor(SWT.COLOR_RED));
+ hint.open();
+
+ shell.setBounds(800, 800, 200, 50);
+ shell.pack();
+ shell.open();
+
+ System.err.println("Map (0, 0) to shell: " + display.map(shell, null, 0, 0, width, height));
+ System.err.println("Map (0, 0) to combo: " + display.map(combo, null, 0, 0, width, height));
+
+ while (!shell.isDisposed()) {
+ if (!display.readAndDispatch()) display.sleep();
+ }
+ display.dispose();
+
+ }
+}