Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 379271

Summary: Combo#getCaretLocation() is slightly too far down/right
Product: [Eclipse Project] Platform Reporter: Carolyn MacLeod <carolynmacleod4>
Component: SWTAssignee: Xi Yan <xixiyan>
Status: VERIFIED FIXED QA Contact:
Severity: normal    
Priority: P3 CC: daniel_megert, ericwill, xixiyan
Version: 4.2Keywords: triaged
Target Milestone: 4.9 M2   
Hardware: PC   
OS: Linux-GTK   
See Also: https://git.eclipse.org/r/124696
https://git.eclipse.org/c/platform/eclipse.platform.swt.git/commit/?id=8be5e0838c490f3184968b6763b945ea0f99e8fa
Whiteboard:
Bug Depends on:    
Bug Blocks: 204599    

Description Carolyn MacLeod CLA 2012-05-11 11:33:56 EDT
Problem was noticed when fixing bug 44072.

Combo#getCaretLocation() is slightly too far down and to the right.
This is not just a mapping problem from the internal text to the combo...
there is something deeper going on here.

Not for 3.8/4.2.

Here's a test snippet (modified version of Snippet359):
/*
 * Combo getCaretLocation and getCaretPosition example
 *
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/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.internal.cocoa.OS;
//import org.eclipse.swt.internal.win32.OS;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.*;

public class Snippet359 {
	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);
		//final Text combo = new Text(shell, SWT.SINGLE);
		combo.addKeyListener(new KeyListener() {
			public void keyReleased(KeyEvent e) {
			   if (e.keyCode == SWT.CR) {
				   combo.add(combo.getText());
			   }
			}
			public void keyPressed(KeyEvent e) {
				System.out.println("caret position: " + combo.getCaretPosition());
				System.out.println("caret location: " + combo.getCaretLocation()); 
				
                display.asyncExec(new Runnable() {
                    public void run() {
						Point pt = combo.getCaretLocation();
						Rectangle rect  = display.map(combo, null, pt.x, pt.y, width, height);
						hint.setLocation(rect.x, rect.y);
						hint.moveAbove(null);
						hint.redraw();
                    }
                });
			}

		});
		combo.addListener(SWT.MouseMove, new Listener() {
			public void handleEvent(Event event) {
				System.out.println(event.x + " " + event.y);
			}
		});
		
//		int [] buffer = new int [1];
//		if (OS.SystemParametersInfo (OS.SPI_GETCARETWIDTH, 0, buffer, 0)) {
//			width = buffer [0];
//		}
		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.pack();
		shell.open ();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch ()) display.sleep ();
		}
		display.dispose ();
//		region.dispose();
	}
}
Comment 1 Eric Williams CLA 2018-05-14 15:20:02 EDT
This is still reproducible on GTK3.22, 4.8 M7, and Fedora 28.
Comment 2 Eclipse Genie CLA 2018-06-18 14:44:15 EDT
New Gerrit change created: https://git.eclipse.org/r/124696
Comment 4 Eric Williams CLA 2018-06-18 16:49:51 EDT
(In reply to Eclipse Genie from comment #3)
> Gerrit change https://git.eclipse.org/r/124696 was merged to [master].
> Commit:
> http://git.eclipse.org/c/platform/eclipse.platform.swt.git/commit/
> ?id=8be5e0838c490f3184968b6763b945ea0f99e8fa

In master now, thanks Xi.