diff options
| author | Xi Yan | 2018-06-18 18:43:12 +0000 |
|---|---|---|
| committer | Xi Yan | 2018-06-18 19:20:57 +0000 |
| commit | 8be5e0838c490f3184968b6763b945ea0f99e8fa (patch) | |
| tree | 284b7ea82864c0ba1172775764111408113d624b | |
| parent | ebc68f80d66ed176ef3bb5409964b9c502d19556 (diff) | |
| download | eclipse.platform.swt-8be5e0838c490f3184968b6763b945ea0f99e8fa.tar.gz eclipse.platform.swt-8be5e0838c490f3184968b6763b945ea0f99e8fa.tar.xz eclipse.platform.swt-8be5e0838c490f3184968b6763b945ea0f99e8fa.zip | |
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>
2 files changed, 91 insertions, 2 deletions
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 ff043982c2..9265206ba7 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 @@ Point getCaretLocationInPixels () { 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 0000000000..478aea8912 --- /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(); + + } +} |
