Project: SWT
Bug 123384 Embedded control in StyledText has some problem with scrolling
Reported: 2006-01-11 01:23:20 | Status: resolved fixed

Run below snippet. Try to drag scroll bar up and down, will find the button's location sometimes goes wrong. But the image is ok when scrolling. My system is Windows XP Chinese version. public class Snippet { class StyleObject { public static final int IMAGE = 0; public static final int CONTROL = 1; public int objectType; public Object object; public StyleObject(int ot, Object obj) { objectType = ot; object = obj; } public Rectangle getBounds() { switch(objectType) { case IMAGE: return ((Image)object).getBounds(); case CONTROL: return ((Control)object).getBounds(); default: return new Rectangle(0, 0, 0, 0); } } public boolean isDisposed() { switch(objectType) { case IMAGE: return ((Image)object).isDisposed(); case CONTROL: return ((Control)object).isDisposed(); default: return false; } } public void dispose() { switch(objectType) { case IMAGE: ((Image)object).dispose(); break; case CONTROL: ((Control)object).dispose(); break; } } } StyledText styledText; String text = "Embedded images:\u0014 works fine when scrolling\n" + "Embeded control:\u0014 has some problem with scrolling\n" + "new line\n" + "new line\n" + "new line\n" + "new line\n" + "new line\n" + "new line\n" + "new line\n" + "new line\n" + "new line\n" + "new line\n" + "new line\n" + "new line\n" + "new line\n" + "new line\n" + "new line\n" + "new line\n" + "new line\n" + "new line\n"; int[] offsets; private StyleObject[] objects; void addObject(StyleObject so, int offset) { StyleRange style = new StyleRange(); style.start = offset; style.length = 1; Rectangle rect = so.getBounds(); style.metrics = new GlyphMetrics(rect.height, 0, rect.width); styledText.setStyleRange(style); } public static void main(String[] args) { Snippet t = new Snippet(); t.run(); } public void run() { final Display display = new Display(); final Shell shell = new Shell(display); shell.setLayout(new GridLayout()); styledText = new StyledText(shell, SWT.WRAP | SWT.BORDER | SWT.V_SCROLL); styledText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); styledText.setText(text); Button button = new Button(styledText, SWT.PUSH); button.setText("Button"); button.setCursor(display.getSystemCursor(SWT.CURSOR_ARROW)); button.setSize(button.computeSize(SWT.DEFAULT, SWT.DEFAULT)); objects = new StyleObject[] { new StyleObject(StyleObject.IMAGE, display.getSystemImage(SWT.ICON_QUESTION)), new StyleObject(StyleObject.CONTROL, button) }; offsets = new int[objects.length]; int lastOffset = 0; for(int i = 0; i < objects.length; i++) { int offset = text.indexOf("\u0014", lastOffset); offsets[i] = offset; addObject(objects[i], offset); lastOffset = offset + 1; } styledText.addPaintObjectListener(new PaintObjectListener() { public void paintObject(PaintObjectEvent event) { GC gc = event.gc; StyleRange style = event.style; int start = style.start; for(int i = 0; i < offsets.length; i++) { int offset = offsets[i]; if(start == offset) { switch(objects[i].objectType) { case StyleObject.IMAGE: Image image = (Image)objects[i].object; int x = event.x; int y = event.y + event.ascent - style.metrics.ascent; gc.drawImage(image, x, y); break; case StyleObject.CONTROL: Control control = (Control)objects[i].object; control.setLocation(event.x, event.y + event.ascent - objects[i].getBounds().height); break; } } } } }); shell.setSize(400, 200); shell.open(); while(!shell.isDisposed()) { if(!display.readAndDispatch()) display.sleep(); } for(int i = 0; i < objects.length; i++) { if(objects[i] != null && !objects[i].isDisposed()) { objects[i].dispose(); } } display.dispose(); } }

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