Bug 125969 - [ScrolledComposite] SWT Layouts in ScrolledComposite in FormToolkit broken
Summary: [ScrolledComposite] SWT Layouts in ScrolledComposite in FormToolkit broken
Status: NEW
Alias: None
Product: Platform
Classification: Eclipse Project
Component: SWT (show other bugs)
Version: 3.1.1   Edit
Hardware: PC Windows XP
: P3 normal with 1 vote (vote)
Target Milestone: ---   Edit
Assignee: Duong Nguyen CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2006-01-31 22:34 EST by Paul E. Keyser CLA
Modified: 2019-09-06 16:17 EDT (History)
1 user (show)

See Also:


Attachments
screen shot of layout bug (22.60 KB, image/png)
2006-01-31 22:35 EST, Paul E. Keyser CLA
no flags Details
zip of a one-class example with running code showing the problem (1.65 KB, text/zip)
2006-03-31 15:28 EST, Paul E. Keyser CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Paul E. Keyser CLA 2006-01-31 22:34:46 EST
I have a widget containment hierachy as below; after two weeks of fussing and trying to get help on the newsgroup, I cannot get reasonable behavior from the layout: I will attach a cleaned-up screenshot. I do NOT want the widgets to expand infinitely to the right; I want them to act like the Group and expand to fill their parent! 

In the widget hierarchy, of course I create using FormToolkit, or when I cannot (as for Group), I call adapt(). 

ViewPart with Eclipse-supplied parent Composite 
   FormToolkit 
   Form form(parent) -- form.getBody() has GridLayout 
      Group g(body) -- has GridLayout, and GridData -- behaves correctly 
         Button b1, b2, b3(g) -- each has GridData 
      ScrolledComposite sc(body) -- used because of bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=103420 -- has GridData 
         Composite c(sc) -- sc.setContent(c) -- has GridLayout, and GridData 
            Composite item<001...100>(c) -- i.e., up to around 100 kids -- each has GridLayout, and GridData 
               Hyperlink (item) -- has GridData 
               FormText (item) -- has GridData 

FormText is where the problem shows up, but the problem is the same if I use Text or Label instead of FormText.
Comment 1 Paul E. Keyser CLA 2006-01-31 22:35:41 EST
Created attachment 33920 [details]
screen shot of layout bug
Comment 2 Steve Northover CLA 2006-02-02 17:53:33 EST
Sign.  No code, just a screen shot.  Veronika, do you want to have a crack at figuring out what he wants?
Comment 3 Veronika Irvine CLA 2006-02-03 09:45:16 EST
Paul you need to give code showing what GridData and GridLayout info you are setting.
Comment 4 Paul E. Keyser CLA 2006-02-17 11:06:38 EST
Sorry for delay; here is code (I'm in a ViewPart, and the View gets told to "append items" which results in the call to "appendHit()"; the View can also be refreshed by receiving a new input, in which case "refresh()" is called): 

	/**
	 * @see org.eclipse.ui.IWorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
	 */
	public void createPartControl(final Composite parent) {
		// Eclipse-supplied "parent" seems to have FillLayout 
		// but changing to GridLayout and setting a GridData on the Form does not help 
		_formToolkit = new FormToolkit(parent.getDisplay());
		_theForm = toolkit().createForm(parent); 

		toolkit().paintBordersFor(parent());

		final GridLayout layout = new GridLayout();
		layout.marginHeight = 0; 
		layout.marginWidth = 0; 
		layout.verticalSpacing = 0; 
		parent().setLayout(layout); 

		_theViewer = new HitListViewer(toolkit(), form());
	}

	public HitListViewer(final FormToolkit toolkit, final Form form) {
		_formToolkit = toolkit; 
		_theForm = form; 

		createControl(); 
		refresh(); 
		form().layout(); 
	}

	/**
	 * Create the "List" Control of this HitListViewer 
	 */
	protected void createControl() {
		// adding a Group around the SC does not help the "infinite horiz expansion" problem; 
		// adding one inside the Group prevents any display  

		_sc = new ScrolledComposite(parent(), SWT.V_SCROLL | SWT.H_SCROLL);
		// TODO keyser: *** HitList: H_SCROLL required since contained widgets grow infinitely wide 

		_sc.setBackground(parent().getBackground()); 
		// the "horizontalAlignment" must be SWT.FILL, or else the SC will not fill the parent 
		_sc.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); 

		_itemParent = toolkit().createComposite(_sc); 
		// the SWT.LEFT/FILL and true/false on the horiz make no difference 
	    _itemParent.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); 
	    _sc.setContent(_itemParent);

	    // using RowLayout(SWT.VERTICAL) here does not help the "infinite horiz expansion" problem 
	    final GridLayout listLayout = new GridLayout();
	    // these values need to be "1" in order to see the borders 
	    listLayout.marginWidth = listLayout.marginHeight = 1; 
	    listLayout.verticalSpacing = 3; // needs to be at least "3" 
		_itemParent.setLayout(listLayout);

	    // this extra call to paintBordersFor() is required 
		toolkit().paintBordersFor(_itemParent);
	}

	public void refresh() { 
		final Composite itemParent = getItemParent(); 

		for (final Control control : itemParent.getChildren()) {
			control.dispose(); 
		}

		for (final IHit hit : _cp.getItems()) {
			new HitItem(toolkit(), itemParent, hit); 
		}

		int width = SWT.DEFAULT; 
		// width = _sc.getClientArea().width; // changes nothing 
		// whether or not omitting the H_SCROLL, 
		// passing "itemParent.getSize().x" as width hint fails utterly; 
		// whereas passing "itemParent.getParent().getBounds().width" 
		// fails utterly with H_SCROLL and clips without it 
		itemParent.setSize(itemParent.computeSize(width, SWT.DEFAULT)); 
	}

	public void appendHit(final IHit hit) { 
		final Composite itemParent = getItemParent(); 
		final Control[] kids = itemParent.getChildren(); 

		if (kids.length == 1) {
			refresh(); 

		} else {
			new HitItem(toolkit(), itemParent, hit);

			int width = SWT.DEFAULT;
			// width = _sc.getClientArea().width; // changes nothing
			// whether or not omitting the H_SCROLL,
			// passing "itemParent.getSize().x" as width hint fails 
			// utterly; but "itemParent.getParent().getBounds().width"
			// fails utterly with H_SCROLL and clips without it
			itemParent.setSize(itemParent.computeSize(width, SWT.DEFAULT));
		}
	}
Comment 5 Paul E. Keyser CLA 2006-02-17 11:07:48 EST
Sorry about the messy line-wrapping. 

Also, note that I have eliminated the radio-button group (in favor of using proper View-toolbar buttons). 

thanks
Comment 6 Paul E. Keyser CLA 2006-02-17 14:11:10 EST
I see that I omitted to supply the code for HitItem (an IHit is just a thing that has a String Title and some other data): 

	public HitItem(final FormToolkit toolkit, 
		final Composite parent, final IHit hit) {
		_formToolkit = toolkit; 
		_hit = hit; 
		_linker = createLinker(); // suitable IHyperlinkListener 

		createControls(parent); 
	}

	protected void createControls(final Composite parent) {
		final Composite theItem = toolkit().createComposite(parent); 
		// setting horiz to (SWT.LEFT, false) makes right-hand edge 
		// ragged, 
		// but does not affect the "infinite horizontal expansion" 
		// problem 
		theItem.setLayoutData(
			new GridData(SWT.FILL, SWT.TOP, true, false)); 
		theItem.setLayout(new GridLayout(2, false)); 

		// this ensures that borders are actually drawn: 
		theItem.setData(FormToolkit.KEY_DRAW_BORDER, 
			FormToolkit.TEXT_BORDER); 

		createLinks(theItem); 
		createTextDisplay(theItem); 
	}

	protected void createLinks(final Composite theItem) {
		final ImageHyperlink imageLink = 
			toolkit().createImageHyperlink(theItem, SWT.LEFT); 
		final Image image = MyPlugin.getImage("icons/jumpTo.png"); 
		imageLink.setActiveImage(image); 
		imageLink.setImage(image); 
		imageLink.setHoverImage(image); 
		imageLink.setLayoutData(
			new GridData(SWT.LEFT, SWT.TOP, false, false));
		final String title = _hit.title();
		final Hyperlink titleLink = 
			toolkit().createHyperlink(theItem, title, SWT.LEFT); 
		titleLink.setLayoutData(
			new GridData(SWT.LEFT, SWT.TOP, false, false)); 
		imageLink.addHyperlinkListener(_linker); 
		titleLink.addHyperlinkListener(_linker); 
	}

	protected void createTextDisplay(final Composite theItem) {
		final FormText textArea = 
			toolkit().createFormText(theItem, false); 
		// w/out the H_SCROLL in the ScrolledComposite, this gets 
		// infinitely wide; 
		// but so do a Text or a Label, 
		// even when they too are created with SWT.WRAP 

		textArea.setText("SOME TEXT OR OTHER", true, false);

		final GridData data = 
			new GridData(SWT.LEFT, SWT.TOP, false, true); 
		data.horizontalSpan = 2; 
		textArea.setLayoutData(data);
	}

Comment 7 Paul E. Keyser CLA 2006-03-06 10:53:36 EST
two more small omissions, in comment 4, that I just noticed: 

_cp.getItems() simply returns the current IHit[] 

parent() simply returns _theForm.getBody() 
Comment 8 Paul E. Keyser CLA 2006-03-31 15:28:42 EST
Created attachment 37433 [details]
zip of a one-class example with running code showing the problem
Comment 9 Eclipse Webmaster CLA 2019-09-06 16:17:39 EDT
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.