} else {
layoutData = new BoxLayoutData();
widget.setLayoutData(layoutData);
}
Margins cm = layoutData.getMargins();
if (cm == null) {
cm = new Margins(0);
layoutData.setMargins(cm);
}
}
if (findWidth || findHeight) {
for (int i = 0, len = getWidgetCount(); i < len; i++) {
Widget widget = getWidget(i);
if (!widget.isVisible()) {
continue;
}
BoxLayoutData layoutData = (BoxLayoutData) widget.getLayoutData();
Margins cm = layoutData.getMargins();
calculateWidth += widget.getOffsetWidth();
maxWidgetHeight = Math.max(maxWidgetHeight, widget.getOffsetHeight());
calculateWidth += (cm.getLeft() + cm.getRight());
maxMarginTop = Math.max(maxMarginTop, cm.getTop());
maxMarginBottom = Math.max(maxMarginBottom, cm.getBottom());
}
maxWidgetHeight += (maxMarginTop + maxMarginBottom);
if (findWidth) {
w = calculateWidth;
}
if (findHeight) {
h = maxWidgetHeight;
}
}
int pl = 0;
int pt = 0;
int pb = 0;
int pr = 0;
if (getPadding() != null) {
pl = getPadding().getLeft();
pt = getPadding().getTop();
pb = getPadding().getBottom();
pr = getPadding().getRight();
}
if (findHeight) {
h += pt + pb;
}
if (findWidth) {
w += pl + pr;
}
int stretchHeight = h - pt - pb;
int totalFlex = 0;
int totalWidth = 0;
int maxHeight = 0;
for (int i = 0, len = getWidgetCount(); i < len; i++) {
Widget widget = getWidget(i);
widget.addStyleName(CommonStyles.get().positionable());
// very strange behavior where clearing the margins in causing
// widget.getOffsetWidth to return an invalid result (for buttons -5 px
// actual width)
// which is corrected by the time enable overflow code runs
if (!GXT.isIE7()) {
widget.getElement().getStyle().setMargin(0, Unit.PX);
}
if (!widget.isVisible()) {
continue;
}
if (widget == more) {
triggerWidth = widget.getOffsetWidth() + 10;
}
BoxLayoutData layoutData = (BoxLayoutData) widget.getLayoutData();
Margins cm = layoutData.getMargins();
// TODO strange issue where getOffsetWidth call in 2nd loop is returning smaller number than acutal offset
// when packing CENTER or END so we cache the offsetWidth for use in 2nd loop
int ww = widget.getOffsetWidth();
loopWidthMap.put(widget, ww);
totalFlex += layoutData.getFlex();
totalWidth += (ww + cm.getLeft() + cm.getRight());
maxHeight = Math.max(maxHeight, widget.getOffsetHeight() + cm.getTop() + cm.getBottom());
}
int innerCtHeight = maxHeight + pt + pb;
if (hBoxLayoutAlign.equals(HBoxLayoutAlign.STRETCH)) {
getContainerTarget().setSize(w, h, true);
} else if (hBoxLayoutAlign.equals(HBoxLayoutAlign.MIDDLE) || hBoxLayoutAlign.equals(HBoxLayoutAlign.BOTTOM)) {
getContainerTarget().setSize(w, h = Math.max(h, innerCtHeight), true);
} else {
getContainerTarget().setSize(w, innerCtHeight, true);
}
int extraWidth = w - totalWidth - pl - pr;
int allocated = 0;
int cw, ch, ct;
int availableHeight = h - pt - pb;
if (getPack().equals(BoxLayoutPack.CENTER)) {
pl += extraWidth / 2;
} else if (getPack().equals(BoxLayoutPack.END)) {
pl += extraWidth;
}
for (int i = 0, len = getWidgetCount(); i < len; i++) {
Widget widget = getWidget(i);
if (!widget.isVisible()) {
continue;
}
BoxLayoutData layoutData = (BoxLayoutData) widget.getLayoutData();
Margins cm = layoutData.getMargins();
cw = loopWidthMap.remove(widget);
ch = widget.getOffsetHeight();
pl += cm.getLeft();
pl = Math.max(0, pl);
if (hBoxLayoutAlign.equals(HBoxLayoutAlign.MIDDLE)) {
int diff = availableHeight - (ch + cm.getTop() + cm.getBottom());
if (diff == 0) {
ct = pt + cm.getTop();
} else {
ct = pt + cm.getTop() + (diff / 2);
}
} else {
if (hBoxLayoutAlign.equals(HBoxLayoutAlign.BOTTOM)) {
ct = h - (pb + cm.getBottom() + ch);
} else {
ct = pt + cm.getTop();
}
}
boolean component = widget instanceof Component;
Component c = null;
if (component) {
c = (Component) widget;
}
int width = -1;
if (component) {
c.setPosition(pl, ct);
} else {
XElement.as(widget.getElement()).setLeftTop(pl, ct);
}
if (getPack().equals(BoxLayoutPack.START) && layoutData.getFlex() > 0) {
int add = (int) Math.floor(extraWidth * (layoutData.getFlex() / totalFlex));
allocated += add;
if (isAdjustForFlexRemainder() && i == getWidgetCount() - 1) {
add += (extraWidth - allocated);
}
cw += add;
width = cw;
}
if (hBoxLayoutAlign.equals(HBoxLayoutAlign.STRETCH)) {
applyLayout(
widget,
width,
Util.constrain(stretchHeight - cm.getTop() - cm.getBottom(), layoutData.getMinSize(),
layoutData.getMaxSize()));
} else if (hBoxLayoutAlign.equals(HBoxLayoutAlign.STRETCHMAX)) {
applyLayout(widget, width,
Util.constrain(maxHeight - cm.getTop() - cm.getBottom(), layoutData.getMinSize(), layoutData.getMaxSize()));
} else if (width > 0) {
applyLayout(widget, width, -1);
}
pl += cw + cm.getRight();
}
// do we need overflow
if (enableOverflow) {
int runningWidth = 0;
for (int i = 0, len = getWidgetCount(); i < len; i++) {
Widget widget = getWidget(i);
if (widget == more) {
continue;
}
BoxLayoutData layoutData = null;
Object d = widget.getLayoutData();
if (d instanceof BoxLayoutData) {
layoutData = (BoxLayoutData) d;
} else {
layoutData = new BoxLayoutData();
}
Margins cm = layoutData.getMargins();
if (cm == null) {
cm = new Margins(0);
}
runningWidth += getWidgetWidth(widget);
runningWidth += cm.getLeft();
runningWidth += cm.getRight();
}
if (runningWidth > w) {
hasOverflow = true;
onOverflow();