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

Bug 237675

Summary: Giving focus to other control during FocusIn doesn't reassign the Caret
Product: [Eclipse Project] Platform Reporter: Felipe Heidrich <eclipse.felipe>
Component: SWTAssignee: Felipe Heidrich <eclipse.felipe>
Status: RESOLVED FIXED QA Contact:
Severity: normal    
Priority: P3 CC: snorthov
Version: 3.4   
Target Milestone: 3.5 M1   
Hardware: PC   
OS: Windows XP   
Whiteboard:

Description Felipe Heidrich CLA 2008-06-18 14:14:40 EDT
Build 3.4 RC4 

Run the snippet and type something.
-> the chars are insert in the text control but the caret is over the canvas - and moves along as chars are insert/delete.
Comment 1 Felipe Heidrich CLA 2008-06-18 14:17:03 EDT
Snippet:
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

public class PR237675 {
public static void main(String[] args) {
	Display display = new Display ();
	Shell shell = new Shell(display);
	shell.setLayout(new GridLayout(1, false));

	Canvas canvas = new Canvas(shell, SWT.NONE);
	Caret caret = new Caret(canvas, SWT.NONE);
	caret.setSize(1, 11);
	canvas.setCaret(caret);
	final Text text = new Text(shell, SWT.BORDER | SWT.SINGLE);

	canvas.addListener(SWT.Paint, new Listener() {
		public void handleEvent(Event event) {
			event.gc.drawText("This is a FormText field with a Caret.", 0, 0, 0);
		}
	});
	canvas.addListener(SWT.KeyDown, new Listener() {
		public void handleEvent(Event event) {
			//allow the canvas take focus
		}
	});
	canvas.addListener(SWT.FocusIn, new Listener() {
		public void handleEvent(Event event) {
			text.setFocus();//causes the bug
		}
	});
	shell.setSize(250, 200);
	shell.open();
	shell.layout();
	while (!shell.isDisposed()) {
		if (!display.readAndDispatch())
			display.sleep();
	}
}
}
Comment 2 Felipe Heidrich CLA 2008-06-18 14:17:55 EDT
Problem originally detected on Bug 236735
Comment 3 Felipe Heidrich CLA 2008-06-18 15:14:49 EDT
Fixed in HEAD > 20080618 (post 3.4)

Steve, I only added the fix in WM_SETFOCUS

The fix in WM_KILLFOCUS would be:
if (caret != null && !caret.isFocusCaret ()) caret.killFocus();
But that is not necessary cause, even if you kill the current caret that belongs to another control, that will get fixed during the WM_SETFOCUS that follows.