1

I am trying to write a custom widget:

package amarsoft.rcp.base.widgets;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Layout;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.ResourceManager;

public class SearchBox extends Composite {

    private Text text;

    private Label icon;

    public SearchBox(Composite parent) {
        super(parent, SWT.BORDER);
        setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
        setBackgroundMode(SWT.INHERIT_FORCE);

        icon = new Label(this, SWT.NONE);
        icon.setImage(ResourceManager.getImage("icons/search.png"));

        text = new Text(this, SWT.NONE);
        addControlListener(new ControlAdapter() {
            @Override
            public void controlResized(ControlEvent e) {
                resize();
            }

        });
    }

    void resize() {
        Point iExtent = icon.computeSize(SWT.DEFAULT, SWT.DEFAULT, false);
        Point tExtent = text.computeSize(SWT.DEFAULT, SWT.DEFAULT, false);
        icon.setBounds(tExtent.x, 1, iExtent.x, iExtent.y);
        text.setBounds(1, 1, tExtent.x, tExtent.y);
    }

    @Override
    public Point computeSize(int wHint, int hHint, boolean changed) {
        Point iExtent = icon.computeSize(SWT.DEFAULT, SWT.DEFAULT, false);
        Point tExtent = text.computeSize(SWT.DEFAULT, SWT.DEFAULT, false);
        System.out.println(iExtent);
        System.out.println(tExtent);
        int width = iExtent.x + tExtent.x;
        int height = Math.max(iExtent.y, tExtent.y);
        if (wHint != SWT.DEFAULT)
            width = wHint;
        if (hHint != SWT.DEFAULT)
            height = hHint;
        System.out.println(new Point(width + 2, height + 2));
        return new Point(width + 5, height + 5);
    }

}

if I try to use it in a parent composite which has a RowLayout, it looks like this:
enter image description here

but if I try to use it in a parent composite which has a FillLayout, it looks like : enter image description here

how can I make it always behavior like this: always has a fixed height, a minimal width, can be resized at horizontal direction if there is more space for it.

CaiNiaoCoder
  • 3,269
  • 9
  • 52
  • 82

1 Answers1

0

The parent composite can give its children any size it wants. With a single child, FillLayout will resize the child to consume all available space. That's just how it works and how it's supposed to work.

It's the responsibility of the parent composite to layout its children in a suitable way. In other words, FillLayout is just the wrong layout to use here. Try it with a regular Text control. The FillLayout will make it much higher than it needs to be for its single line of text.

Frettman
  • 2,251
  • 1
  • 13
  • 9