SpoutDev/Spout — Issue #1342: Strange Buttons sizes with GenericContainer in SpoutPluginAPI
Author: blaise64
Created: 2011-12-25 14:21:54+00:00
State: open

I am experimenting some strange trouble with autosizing in GenericContainer.
Unless I calculate all the size and margins when I don't use the property isFixed() on all the widget I have issues.
I take a look a the source code at https://github.com/SpoutDev/SpoutPluginAPI/blob/master/src/org/getspout/spoutapi/gui/GenericContainer.java
and I think the problem is that in some cases the margin are counted twice

I am only talking about not Fixed widgets and I am going to show the problem with 1 widget but it's the same with multiple

In the method ```updateLayout()``` the used space (including margin but not including widget's size if not fixed) is calculate by
```int horiz = widget.getMarginLeft() + widget.getMarginRight(); //line 272
int vert = widget.getMarginTop() + widget.getMarginBottom(); //line 273```

depending on the type the total space occupied in the container is
```totalheight += vert;//type == ContainerType.VERTICAL line 279
totalwidth += horiz;//type == ContainerType.HORIZONTAL line 284```

then the new space by widget is calculate
```newwidth = (availableWidth - totalwidth) / Math.max(1, hcount); //line 291
newheight = (availableHeight - totalheight) / Math.max(1, vcount); //line 292```

finally children size is calculate
```realheight = Math.max(widget.getMinHeight(), Math.min(newheight - widget.getMarginTop() - widget.getMarginBottom(), widget.getMaxHeight())); //line 326
realwidth = Math.max(widget.getMinWidth(), Math.min(newwidth - widget.getMarginLeft() - widget.getMarginRight(), widget.getMaxWidth())); //line 327```

In the case of min and max not used (for example min=0 max>maxsize)
when ContainerType.VERTICAL
```realheight=newheight - widget.getMarginTop() - widget.getMarginBottom()
=(availableHeight - totalheight) - widget.getMarginTop() - widget.getMarginBottom()
=(availableHeight - widget.getMarginTop() - widget.getMarginBottom()) - widget.getMarginTop() - widget.getMarginBottom()```
when ContainerType.HORIZONTAL
```realwidth = newwidth - widget.getMarginLeft() - widget.getMarginRight()
= (availableWidth - totalwidth) - widget.getMarginLeft() - widget.getMarginRight()
= (availableWidth - widget.getMarginLeft() - widget.getMarginRight() - widget.getMarginLeft() - widget.getMarginRight()```

so the margin are counted twice.
I think a solution is to separate the two cases when calculating ```real width``` and ```real height```