Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Wrapped SWT Text or StyledText with fix number of characters per row
Wrapped SWT Text or StyledText with fix number of characters per row [message #643706] Tue, 07 December 2010 22:55 Go to next message
thomasr Missing name is currently offline thomasr Missing nameFriend
Messages: 6
Registered: December 2010
Junior Member
Hi together,
is there a way to use SWT Text or SWT StyleText with a wordwrap that starts at a certain number of characters in a row. This means, a row should never have more than lets say 80 chars, but can have less, depending on wordwrap.

In fact, I'd like to use this component in front of a system which doesnt take more than these 80 chars per row, so I need to make sure that the text doesnt exceed this (no matter on how big the text component is on the screen). I cannot accept the wrapping of the component at some different position (depending on its size) and calculate the wrap by myself, cause this way I cannot ensure that the text the users submit to the system was excactly the same they saw on the screen. Both need to match.

Is that possible and how ?

[Updated on: Tue, 07 December 2010 22:59]

Report message to a moderator

Re: Wrapped SWT Text or StyledText with fix number of characters per row [message #643756 is a reply to message #643706] Wed, 08 December 2010 07:46 Go to previous messageGo to next message
Daniel Krügler is currently offline Daniel KrüglerFriend
Messages: 853
Registered: July 2009
Senior Member
On 12/7/2010 23:55, thomasr wrote:
> Hi together,
> is there a way to use SWT Text or SWT StyleText with a wordwrap that
> starts at a certain number of characters in a row. This means, a row
> should never have more than lets say 80 chars, but can have less,
> depending on wordwrap.
>
> Is that possible and how ?

Depending on the context where you are trying to create the text field
this is possible by taking advantage of specific functions that provide
information about the width and height of letters given a font. We
compute the width/height limits in our GridData via the helper functions
from org.eclipse.jface.dialogs:

public static int convertHeightInCharsToPixels(FontMetrics fontMetrics,
int chars);
public static int convertHorizontalDLUsToPixels(FontMetrics fontMetrics,
int dlus);
public static int convertVerticalDLUsToPixels(FontMetrics fontMetrics,
int dlus);
public static int convertWidthInCharsToPixels(FontMetrics fontMetrics,
int chars);

or directly use indirect usages of above functions via

protected int convertHeightInCharsToPixels(int chars);
protected int convertHorizontalDLUsToPixels(int dlus);
protected int convertVerticalDLUsToPixels(int dlus);
protected int convertWidthInCharsToPixels(int chars);

HTH & Greetings from Bremen,

Daniel Krügler
Re: Wrapped SWT Text or StyledText with fix number of characters per row [message #643885 is a reply to message #643706] Wed, 08 December 2010 15:29 Go to previous messageGo to next message
Grant Gayed is currently offline Grant GayedFriend
Messages: 2150
Registered: July 2009
Senior Member
There isn't a way to specify the number of characters to wrap at, you can
only give the Text a width that should fit ~ 80 characters. The snippet
below demonstrates this using only swt.

public static void main(String[] args) {
final int NUM_CHARS = 80;
final Display display = new Display();
Shell shell = new Shell(display);
Text text = new Text(shell, SWT.MULTI | SWT.WRAP);
GC gc = new GC(text);
int textWidth = gc.getFontMetrics().getAverageCharWidth() * NUM_CHARS;
gc.dispose();
text.setSize(textWidth, 99 /* arbitrarily-chosen height */);
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) display.sleep();
}
display.dispose();
}

If you have an absolute requirement of no line having > 80 characters then
this approach will not be adequate (unless all characters in the font share
a common width), since a user could enter a line with all thin characters
like 'i'. To account for this possibility you would have to choose a
smaller width initially than the average character width * character count,
which would result in most of your lines wrapping much earlier than
80-character increments.

Grant


"thomasr" <reinecke.fox@gmx.net> wrote in message
news:idmdoo$ejl$1@news.eclipse.org...
> Hi together,
> is there a way to use SWT Text or SWT StyleText with a wordwrap that
> starts at a certain number of characters in a row. This means, a row
> should never have more than lets say 80 chars, but can have less,
> depending on wordwrap.
>
> Is that possible and how ?
Re: Wrapped SWT Text or StyledText with fix number of characters per row [message #644954 is a reply to message #643885] Tue, 14 December 2010 23:30 Go to previous messageGo to next message
thomasr Missing name is currently offline thomasr Missing nameFriend
Messages: 6
Registered: December 2010
Junior Member
Hi Grand,
so far so good.... I can statically set the size of the SWT text component (combined with a fixed with font) to wrap at 80 chars.... BUT...

SWT Text does not allow me to get the data out of the model. I can get the line count (getLineCount()), but there's no way to get the Line (like getLine(index)). That doesnt make sense, does it ?
I've tried everything, even to implement an own wordwrapping mechanism which comes close to the SWT Text visual wrap, but that again doesnt guarantees that the visual presentation of the text is the same than the data.

I've used this code so far.

public StaticWrapTextComponent(Composite parent, int style) {
super(parent, style);
this.parent = parent;

Text atText = new Text(this, SWT.MULTI | SWT.WRAP);

// add a change listener which does get triggered for the wordwrap
modifyListener = new ModifyListener() {
public void modifyText(ModifyEvent e) {
// get the wrapped data in the text component
extractRows(atText);
}
};
atText.addModifyListener(modifyListener);

// set the fixed sized font
Font font = new Font(Display.getDefault(), "Courier New", 10, SWT.NORMAL);
atText.setFont(font);
atText.setBackground(new Color(Display.getDefault(), 225, 255, 255));

// align size of the component
GC gc = new GC(atText);
int textWidth = gc.getFontMetrics().getAverageCharWidth() * StringUtil.RETAIN_MAX_CHARS_IN_A_ROW;
gc.dispose();
atText.setSize(textWidth, 200);
}


private void extractRows(Text atText) {
int count = atText.getLineCount();
System.out.println(count);

for(int i=0; i < count; i++) {
//System.out.println("line-"+i+" : "+getLine(i));
}
}


I've also tries StyledText. Here we have the getLine(index) methode, but when I do the same trick like above and let StyledText wrap via the boundaries of the component, the getLineCount() methode doesnt give me the correct number of lines. I've seen some comments of SWT developers that StyledText apparently only returns the number of 'logical lines' whatever this is and however this makes sense...
(https://bugs.eclipse.org/bugs/show_bug.cgi?id=154225#c1)
So either way, SWT Text and StyledText are not sufficient for this little task.

I'm also happy to use any other mechanism, I was now playing around with this 'little nice requirement' since a couple days and I just cant find an appropriate solution.
Even if using the component boundary approach works, I still need to embed it in a bigger component. ok I can make the background white aswell, but what about the H_ and V_SCOLL.

any other ideas are highly appreciated. Thank you
Re: Wrapped SWT Text or StyledText with fix number of characters per row [message #1719987 is a reply to message #644954] Wed, 13 January 2016 15:45 Go to previous message
Markus Krug is currently offline Markus KrugFriend
Messages: 2
Registered: January 2016
Junior Member
Even though the topic is pretty old, google still found this topic to a recent query. And even though i cannot give a solution to the question that was asked i can nevertheless give a way to calculate the true line of the words in a StyledText widget.


// the location of the first character
Rectangle textBoundsFirst = widget.getTextBounds(0, 1);

//the location of the last charatcer
Rectangle textBoundsLast = widget.getTextBounds(widget.getText().length() - 2, widget.getText().length() - 1);

//height of the whole StyledText widget
int widgetHeight = Math.abs(textBounds.y) + Math.abs(textBounds2.y);

//divide this by the lineHeight of a single line + the linespacing to derive the number of lines.


Previous Topic:ExpandItem Header invisible
Next Topic:SWT Browser and javascript in nested frames
Goto Forum:
  


Current Time: Tue Apr 23 15:36:44 GMT 2024

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

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

Back to the top