Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Text preferred size ignores trailing line feed(Text.computeSize(SWT.DEFAULT, SWT.DEFAULT, true) ignores trailing linefeeds in the text content)  () 1 Vote
Text preferred size ignores trailing line feed [message #546005] Fri, 09 July 2010 20:52 Go to next message
Roy Paterson is currently offline Roy PatersonFriend
Messages: 5
Registered: July 2009
Junior Member
I have a Shell that contains a single Text control. I want to resize the shell when I type in the Text control, so that it grows and shrinks as necessary to show the text content.

Here is my code:

package com.smartbear.test;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.*;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.*;

public class TextResizeTest {

	public static void main (String [] args) {
		Display display = new Display ();
		final Shell shell = new Shell (display);
		shell.setLayout(new FillLayout());
		
		final Text text = new Text (shell, SWT.WRAP | SWT.MULTI);
	
		text.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				shell.pack();
			}
		});
		
		shell.pack ();
		shell.open ();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch ()) display.sleep ();
		}
		display.dispose ();
	}
} 


This works if you type characters in the Text. If you insert a line feed in to the middle of some Text it grows vertically as appropriate.

However - if you append a line feed to the *end* of the Text, the Shell doesn't grow - instead the top line of Text is scrolled out of view.

It seems like Text.computeSize(SWT.DEFAULT, SWT.DEFAULT, true) ignores trailing line feeds? How can I get this to work the way I want?

Note I am running on Windows XP, Eclipse 3.6.

Thanks,
Roy
Re: Text preferred size ignores trailing line feed [message #546608 is a reply to message #546005] Tue, 13 July 2010 14:26 Go to previous messageGo to next message
Lakshmi ShanmugamFriend
Messages: 274
Registered: July 2009
Location: India
Senior Member
Hi,

You are seeing a bug in the Text Control with SWT.WRAP style and happens on windows only. It works as expected if WRAP style is not set.
Please see - ttps://bugs.eclipse.org/bugs/show_bug.cgi?id=86529.


Lakshmi P Shanmugam
Re: Text preferred size ignores trailing line feed [message #546618 is a reply to message #546608] Tue, 13 July 2010 14:50 Go to previous messageGo to next message
Roy Paterson is currently offline Roy PatersonFriend
Messages: 5
Registered: July 2009
Junior Member
Thanks for the pointer - I can't believe a bug like this could sit in the code for over 5 years! I'll pursue this in bugzilla.

Regards,
Roy
Re: Text preferred size ignores trailing line feed [message #546622 is a reply to message #546618] Tue, 13 July 2010 14:59 Go to previous messageGo to next message
Lakshmi ShanmugamFriend
Messages: 274
Registered: July 2009
Location: India
Senior Member
You can try out this workaround to see if it works for you. It uses the line count and line height and sets the size accordingly.

public class TextResizeTest {
public static void main (String [] args) {
Display display = new Display ();
final Shell shell = new Shell (display);
shell.setLayout(new GridLayout());
final Text text = new Text (shell, SWT.WRAP|SWT.MULTI);
final GridData gd = new GridData(SWT.FILL, SWT.FILL, true, false);
text.setLayoutData(gd);

final int lineHeight = text.getLineHeight();
text.addModifyListener(new ModifyListener() {
public void modifyText(ModifyEvent e) {
int ht = text.getLineCount() * lineHeight;
gd.heightHint = ht;
shell.pack();
}
});
shell.pack();
shell.open ();
while (!shell.isDisposed()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
}



Lakshmi P Shanmugam
Re: Text preferred size ignores trailing line feed [message #546627 is a reply to message #546622] Tue, 13 July 2010 15:04 Go to previous message
Roy Paterson is currently offline Roy PatersonFriend
Messages: 5
Registered: July 2009
Junior Member
Yeah, that was the workaround I was thinking of too. Don't we also have to account for the Text's trimmings (border, etc) when accounting for height?
Previous Topic:How are PaintEvents supposed to work?
Next Topic:Issues with SWT 3.5 and Solaris (Sparc)
Goto Forum:
  


Current Time: Sat Nov 22 09:08:35 GMT 2014

Powered by FUDForum. Page generated in 0.02941 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software