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

Bug 368417

Summary: When you try to move cursor up/down with arrow keys in a multiline StyledText with word wrapping and nonzero margins, cursor may "freeze" and pressing up/down keys has no effect.
Product: [Eclipse Project] Platform Reporter: Alexander Zakusylo <zakgof>
Component: SWTAssignee: Felipe Heidrich <eclipse.felipe>
Status: RESOLVED FIXED QA Contact:
Severity: normal    
Priority: P3 CC: alex.evseenko, eclipse.felipe, Silenio_Quarti
Version: 4.2   
Target Milestone: 3.8 M5   
Hardware: PC   
OS: Windows 7   
Whiteboard:

Description Alexander Zakusylo CLA 2012-01-12 03:42:46 EST
Build Identifier: Version: Indigo Service Release 1 Build id: 20110916-0149

Looks like the reason of the bug is trivial: the top margin in StyledText#getVisualLineIndex is added twice:


int getVisualLineIndex(TextLayout layout, int offsetInLine) {
	int lineIndex = layout.getLineIndex(offsetInLine);
	int[] offsets = layout.getLineOffsets();
	if (lineIndex != 0 && offsetInLine == offsets[lineIndex]) {
		int lineY = layout.getLineBounds(lineIndex).y;
		int caretY = getCaret().getLocation().y - topMargin - getLinePixel(getCaretLine());
		if (lineY > caretY) lineIndex--;
		caretAlignment = OFFSET_LEADING;
 	}
	return lineIndex;
}


topMargin is explicitly subtracted, but it is already included in value calculated by getLinePixel. The line should look like:

int caretY = getCaret().getLocation().y - getLinePixel(getCaretLine());

Reproducible: Always

Steps to Reproduce:
1. Run the following snippet:

public class Snippet {

  static String text =
    "Line One. Long Long Long Long Long Long Long Long Long Long Long Long Long Long Long Long Long Long\n" +
    "Line Two.\n" +
    "Line Three";

	public static void main(final String [] args) {
		final Display display = new Display();
		final Shell shell = new Shell(display);
		shell.setLayout(new FillLayout());

    final StyledText styledText = new StyledText(shell, SWT.MULTI | SWT.WRAP | SWT.BORDER);

    styledText.setMargins(5, 5, 5, 5);
    styledText.setWordWrap(true);
    styledText.setText(text);
		shell.setSize(300, 400);
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
	}
}

2. Press ARROW DOWN several times
3. Notice that UP/DOWN keys are no more working
Comment 1 Felipe Heidrich CLA 2012-01-16 11:29:55 EST
Thank you very much for the bug report and patch
http://git.eclipse.org/c/platform/eclipse.platform.swt.git/commit/?id=663ad5c7f54209a8b60ed32a953bece16acebea0
Comment 2 Alexander Zakusylo CLA 2012-01-25 05:45:53 EST
Can I ask to backport the fix to 3.7.2 ?
Comment 3 Felipe Heidrich CLA 2012-01-25 13:39:21 EST
Silenio: 3.7.2 backport requested, is there still time ?
Comment 4 Silenio Quarti CLA 2012-01-25 14:13:27 EST
Sorry no, 3.7.2 SR3 is Monday.
Comment 5 Felipe Heidrich CLA 2012-01-30 12:12:36 EST
*** Bug 319131 has been marked as a duplicate of this bug. ***