Project: SWT
Bug 84012 StyledText with SWT.WRAP style does not adjust selection correctly
Reported: 2005-01-30 18:59:05 | Status: resolved fixed

When selecting text in the native Windows text widget, it is possible to extend the selection to the beginning (end) of a line by moving the mouse cursor beyond the left (right) edge of the widget's client area. In a StyledText widget with SWT.WRAP style, this does not work. When handling mouse move events, the StyledText discards all cases in which the horizontal mouse position lies outside its client area while the vertical coordinate translates to the same line the caret is currently on. This means that if the mouse movement is too quick (resulting in not receiving enough mouse move events to cover all the caret positions up to the left(right)most one), the selection will remain incomplete. See the attached screenshot for some visual clarification. Ignoring events outside the client area is apparently done in order not to confuse the (horizontal) auto-scroll mechanism that may also kick in on mouse movements. However, when the SWT.WRAP style is set, there is no horizontal auto-scrolling. Therefore, the fix is to introduce a check to see if the word-wrap option is turned on and allow extending the selection if it is. --- import org.eclipse.swt.*; import org.eclipse.swt.custom.*; import org.eclipse.swt.graphics.*; import org.eclipse.swt.layout.*; import org.eclipse.swt.widgets.*; public class StyledTextSelectionTest { public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); FillLayout layout = new FillLayout(SWT.VERTICAL); layout.marginWidth = 20; layout.marginHeight = 20; layout.spacing = 20; shell.setLayout(layout); Font font = new Font(display, "Arial", 12, SWT.NORMAL); StyledText text1 = new StyledText(shell, SWT.BORDER | SWT.MULTI | SWT.WRAP | SWT.V_SCROLL); text1.setFont(font); text1.setText("StyledText widget with SWT.WRAP style."); Text text2 = new Text(shell, SWT.BORDER | SWT.MULTI | SWT.WRAP | SWT.V_SCROLL); text2.setFont(font); text2.setText("Native text widget with SWT.WRAP style."); shell.setSize(400, 300); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } font.dispose(); display.dispose(); } }

Files changed in fix:
bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/StyledText.java