Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 145997 - [Forms] FormText::computeTextSize returns incorrect date for horizontal size
Summary: [Forms] FormText::computeTextSize returns incorrect date for horizontal size
Status: RESOLVED FIXED
Alias: None
Product: Platform
Classification: Eclipse Project
Component: User Assistance (show other bugs)
Version: 3.2   Edit
Hardware: PC Windows XP
: P3 enhancement (vote)
Target Milestone: 3.5 M1   Edit
Assignee: Adam Archer CLA
QA Contact:
URL:
Whiteboard:
Keywords:
: 234998 264091 (view as bug list)
Depends on:
Blocks:
 
Reported: 2006-06-08 08:58 EDT by Martin Donnelly CLA
Modified: 2009-02-17 15:56 EST (History)
3 users (show)

See Also:


Attachments
Small change to FormText.computeTextSize() method that modifies logic for computing horizontal size (1.15 KB, patch)
2006-06-08 09:04 EDT, Martin Donnelly CLA
agarcher: iplog+
Details | Diff
modified patch (6.98 KB, patch)
2008-07-04 13:40 EDT, Adam Archer CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Martin Donnelly CLA 2006-06-08 08:58:25 EDT
I notice this problem when I put a FormText control on a ScrolledComposite and then call its computeSize() method to set the minimum size for vertical and horizontal scrollbars.  The vertical sizing is correct but the horizontal sizing is far too large for a couple of reasons, e.g. doesn't account for new lines in the formtext markup - if the XML contains line breaks then the paragraph width continues to accumulate regardless. 

I am enclosing some snippet code here which will demonstrate the problem:

package org.eclipse.swt.snippets;

import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.forms.FormColors;
import org.eclipse.ui.forms.widgets.FormText;
import org.eclipse.ui.forms.widgets.FormToolkit;

public class FormTextComputeSize {

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

        // create scrolled composite
        ScrolledComposite myComposite = new ScrolledComposite(shell, SWT.H_SCROLL | SWT.V_SCROLL);
        myComposite.setExpandHorizontal(true);
        myComposite.setExpandVertical(true);

        // add FormText
        FormToolkit formToolkit = new FormToolkit(display);
        FormText formText = formToolkit.createFormText(myComposite, true);

        // add 5 buttons across top
        for (int i = 0; i < 5; i++) {
            Button myButton = new Button(formText, SWT.PUSH);
            myButton.setText("MyButton" + i);
            formText.setControl("MyButton" + i, myButton);
        }

        // add 15 labels vertically
        for (int i = 0; i < 15; i++) {
            Label myLabel = new Label(formText, SWT.NONE);
            myLabel.setText("myLabel" + i);
            formText.setControl("MyLabel" + i, myLabel);
        }

        formText.setColor("header", formToolkit.getColors().getColor(FormColors.TITLE));
        formText.setFont("header", JFaceResources.getHeaderFont());
        formText.setText(getText(), true, false);

        myComposite.setContent(formText);
        Point point = formText.computeSize(SWT.DEFAULT, SWT.DEFAULT);
        myComposite.setMinSize(point.x, point.y);

        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) display.sleep ();
        }
        display.dispose ();
    }

    static String getText() {
        StringBuffer buf = new StringBuffer();
        buf.append("<form><p>");
        for (int i = 0; i < 5; i++) {
            buf.append("<control fill=\"false\" href=\"" + "MyButton" + i + "\"/>");
        }
        buf.append("<br/>");
        for (int i = 0; i < 15; i++) {
            buf.append("<control fill=\"false\" href=\"" + "MyLabel" + i + "\"/>");
            buf.append("<br/>");
        }
        buf.append("</p></form>");

        System.out.println(buf.toString());
        return buf.toString();
    }

}

This effectively results in the following markup:
- <form>
- <p>
  <control fill="false" href="MyButton0" /> 
  <control fill="false" href="MyButton1" /> 
  <control fill="false" href="MyButton2" /> 
  <control fill="false" href="MyButton3" /> 
  <control fill="false" href="MyButton4" /> 
  <br /> 
  <control fill="false" href="MyLabel0" /> 
  <br /> 
  <control fill="false" href="MyLabel1" /> 
  <br /> 
  <control fill="false" href="MyLabel2" /> 
  <br /> 
  <control fill="false" href="MyLabel3" /> 
  <br /> 
  <control fill="false" href="MyLabel4" /> 
  <br /> 
  <control fill="false" href="MyLabel5" /> 
  <br /> 
  <control fill="false" href="MyLabel6" /> 
  <br /> 
  <control fill="false" href="MyLabel7" /> 
  <br /> 
  <control fill="false" href="MyLabel8" /> 
  <br /> 
  <control fill="false" href="MyLabel9" /> 
  <br /> 
  <control fill="false" href="MyLabel10" /> 
  <br /> 
  <control fill="false" href="MyLabel11" /> 
  <br /> 
  <control fill="false" href="MyLabel12" /> 
  <br /> 
  <control fill="false" href="MyLabel13" /> 
  <br /> 
  <control fill="false" href="MyLabel14" /> 
  <br /> 
  </p>
  </form>

I have a patch which addresses the problem shown here which I would like to contribute for evaluation.
Comment 1 Martin Donnelly CLA 2006-06-08 09:04:21 EDT
Created attachment 43857 [details]
Small change to FormText.computeTextSize() method that modifies logic for computing horizontal size

This is a change to the computeTextSize() method that I would like to propose in order to better support horizontal scroll bar sizing for FormText controls. The current computation tends to return horizontal dimensions well in excess of the actual size.  The amendment is quite small - just a few lines of code with no API changes.
Comment 2 Adam Archer CLA 2008-07-04 13:40:43 EDT
Created attachment 106605 [details]
modified patch

I reviewed the provided patch. It looks good, though I moved part of the fix out of FormText and into BreakSegment.

While testing I discovered that the horizontal margin on FormText was not being interpreted correctly. When computing size, the left margin was being taken into account twice for all paragraphs. When rendering, the right margin was being ignored by any inline controls.

Also I discovered an inconsistency with how the Locator.width field was being used by different subclasses of ParagraphSegment. This caused incorrect size calculations for FormText is some very rare cases.

This modified patch fixes all listed problems.
Comment 3 Adam Archer CLA 2008-07-04 13:43:09 EDT
Patch applied to HEAD.
Comment 4 Adam Archer CLA 2008-07-07 12:05:28 EDT
*** Bug 234998 has been marked as a duplicate of this bug. ***
Comment 5 Gerald Rosenberg CLA 2009-02-17 15:56:24 EST
*** Bug 264091 has been marked as a duplicate of this bug. ***