m. vokhm
m. vokhm

Reputation: 699

How GridLayout computes Composite size

In an SWT application, I have a composite with a GridLayout that has two columns. The first contains labels, the second texts and checkboxes. I use TextLayouts for some of them and paint them manually (in a paint listener), so I have to manually compute their sizes. I set the computed sizes for the widgets with setSize() and set widthHint and heightHint in their respective GridDatas. GridData objects for the labels (1st column of the grid) are created with (SWT.RIGHT, SWT.CENTER, false, false) and for the widgets in the 2nd column with (SWT.FILL, SWT.CENTER, true, false). The computeSize() for the widgets returns the sizes I expect (eg. no more then 68 pixels of width for the 1st column and 128 for the 2nd).

But the computeSize() for the Composite returns a width that is much larger than I expect to see (or want), namely 253, while I expect 68 + 128 + 3 * 4 + 2 * 2 = 212 or something near to it. The pack() on the Composite, acccordingly, makes it grow much wider than I want. I can force the required width of the Composite with the setSize(), but subsequent actions on the shell containing my composite invoke the composite's pack() anyway, so its width gets spoiled. Setting of the widthHint of the composite's GridData won't work either.

The Composite itself is a child of a upper-level Composite with GridLayout and has a GridData with (SWT.LEFT, SWT.FILL, false, true).

Why the computeSize() decides that the width of the Composite should be 253, if the widths of the widgets in the columns are computed to be 68 and 128 pixels wide and their widthHints are set correspondingly? How does it work? What does it take into account, besides the computed sizes of the child widgets? Where can I find a description of its algorithm (the source of the GridLayout's layout (Composite composite, boolean move, int x, int y, int width, int height, boolean flushCache), which actually computes the size, is rather complicated and difficult to understand) ?

How can I make the layout managers keep the required width of the Composite while changing layout of the parent Composites, including the Shell (on resizing, font changes etc)?

Upvotes: 2

Views: 1029

Answers (1)

hiergiltdiestfu
hiergiltdiestfu

Reputation: 2357

GridLayout is full of surprises. If you are not dogmatically stuck to it, I recommend switching to FormLayout. This is much more powerful and gives you much much more control over the final result of your UI. It is a paradigm shift compared to GridLayout, but it is very much worth learning it.

With FormLayout, you can use the dimensions of the parent, or the position and dimensions of any sibling Controls (ie. Controls sharing the parent) for positioning and sizing controls. It's like declaring a couple of layouting constraints per control and the manager will do its best to satisfy your wishes.

Here is a tutorial for you to get you started. It introduces you to FormData, which declares four anchor points (top, bottom, right, left) and the FormAttachments which define the constraints in a FormData object.

For your problem, you can layout the first column as using eg. 40% of the width of the parent, with each row being placed below the label of the previous row. The controls of the second column are placed relative to their label (on their left) and the label or control of the previous row (on their top).

Now what seems to bother you is the width of some of the controls in the second column. In FormLayout, you can set an explicite constraint for the right anchor, either by declaring it to be eg. at 100% width of the parent (starting from the 40% right anchor of the first column) which will then rely on the layout of the parent's parent, or by setting the right anchor as an absolute margin in pixels counting from the right edge of the first column. This can be done by referencing the label in the first column, but switching the alignment. This is necessary because by default, a relation from the right anchor of Control A to a sibling B will automatically use the left side of B. In code, this second possibility will spell out like that:

FormData fd;
//row 1
Label lblC1R1;
Control ctrlC2R1;
//row 2
Label lblC1R2;
Control ctrlC2R2;

...

fd = new FormData(); //for ctrlC2R2;
fd.top = new FormAttachment(lblC1R1);  //top edge clings to (y-coord of) bottom of row 1 label (could reference row 1 control, too)
fd.left = new FormAttachment(lblC1R2); //left edge clings to right side of row 2 label
fd.right = new FormAttachment(lblC1R2, 200, SWT.RIGHT); //right edge is defined as a distance of +200 px from *right* side of row 2 label (by default, it would use the left side)
//we don't set fd.bottom, so the control may grow or shrink with ie. font size

ctrlC2R2.setLayoutData(fd);

This will fix the width of the second column control to 200px starting from the right side of the first column, no matter what the control says about its preferred size.

Upvotes: 1

Related Questions