3 * Copyright(c) 2006-2010 Sencha Inc.
5 * http://www.sencha.com/license
8 * @class Ext.layout.HBoxLayout
9 * @extends Ext.layout.BoxLayout
10 * <p>A layout that arranges items horizontally across a Container. This layout optionally divides available horizontal
11 * space between child items containing a numeric <code>flex</code> configuration.</p>
12 * This layout may also be used to set the heights of child items by configuring it with the {@link #align} option.
14 Ext.layout.HBoxLayout = Ext.extend(Ext.layout.BoxLayout, {
17 * Controls how the child items of the container are aligned. Acceptable configuration values for this
19 * <div class="mdetail-params"><ul>
20 * <li><b><tt>top</tt></b> : <b>Default</b><div class="sub-desc">child items are aligned vertically
21 * at the <b>top</b> of the container</div></li>
22 * <li><b><tt>middle</tt></b> : <div class="sub-desc">child items are aligned vertically in the
23 * <b>middle</b> of the container</div></li>
24 * <li><b><tt>stretch</tt></b> : <div class="sub-desc">child items are stretched vertically to fill
25 * the height of the container</div></li>
26 * <li><b><tt>stretchmax</tt></b> : <div class="sub-desc">child items are stretched vertically to
27 * the height of the largest item.</div></li>
29 align: 'top', // top, middle, stretch, strechmax
35 * Controls how the child items of the container are packed together. Acceptable configuration values
36 * for this property are:
37 * <div class="mdetail-params"><ul>
38 * <li><b><tt>start</tt></b> : <b>Default</b><div class="sub-desc">child items are packed together at
39 * <b>left</b> side of container</div></li>
40 * <li><b><tt>center</tt></b> : <div class="sub-desc">child items are packed together at
41 * <b>mid-width</b> of container</div></li>
42 * <li><b><tt>end</tt></b> : <div class="sub-desc">child items are packed together at <b>right</b>
43 * side of container</div></li>
48 * This configuation option is to be applied to <b>child <tt>items</tt></b> of the container managed
49 * by this layout. Each child item with a <tt>flex</tt> property will be flexed <b>horizontally</b>
50 * according to each item's <b>relative</b> <tt>flex</tt> value compared to the sum of all items with
51 * a <tt>flex</tt> value specified. Any child items that have either a <tt>flex = 0</tt> or
52 * <tt>flex = undefined</tt> will not be 'flexed' (the initial size will not be changed).
57 * Calculates the size and positioning of each item in the HBox. This iterates over all of the rendered,
58 * visible items and returns a height, width, top and left for each, as well as a reference to each. Also
59 * returns meta data such as maxHeight which are useful when resizing layout wrappers such as this.innerCt.
60 * @param {Array} visibleItems The array of all rendered, visible items to be calculated for
61 * @param {Object} targetSize Object containing target size and height
62 * @return {Object} Object containing box measurements for each child, plus meta data
64 calculateChildBoxes: function(visibleItems, targetSize) {
65 var visibleCount = visibleItems.length,
67 padding = this.padding,
68 topOffset = padding.top,
69 leftOffset = padding.left,
70 paddingVert = topOffset + padding.bottom,
71 paddingHoriz = leftOffset + padding.right,
73 width = targetSize.width - this.scrollOffset,
74 height = targetSize.height,
75 availHeight = Math.max(0, height - paddingVert),
77 isStart = this.pack == 'start',
78 isCenter = this.pack == 'center',
79 isEnd = this.pack == 'end',
87 //used to cache the calculated size and position values for each child item
90 //used in the for loops below, just declared here for brevity
91 child, childWidth, childHeight, childSize, childMargins, canLayout, i, calcs, flexedWidth,
92 horizMargins, vertMargins, stretchHeight;
94 //gather the total flex of all flexed items and the width taken up by fixed width items
95 for (i = 0; i < visibleCount; i++) {
96 child = visibleItems[i];
97 childHeight = child.height;
98 childWidth = child.width;
99 canLayout = !child.hasLayout && typeof child.doLayout == 'function';
101 // Static width (numeric) requires no calcs
102 if (typeof childWidth != 'number') {
104 // flex and not 'auto' width
105 if (child.flex && !childWidth) {
106 totalFlex += child.flex;
108 // Not flexed or 'auto' width or undefined width
110 //Render and layout sub-containers without a flex or width defined, as otherwise we
111 //don't know how wide the sub-container should be and cannot calculate flexed widths
112 if (!childWidth && canLayout) {
116 childSize = child.getSize();
117 childWidth = childSize.width;
118 childHeight = childSize.height;
122 childMargins = child.margins;
123 horizMargins = childMargins.left + childMargins.right;
125 nonFlexWidth += horizMargins + (childWidth || 0);
126 desiredWidth += horizMargins + (child.flex ? child.minWidth || 0 : childWidth);
127 minimumWidth += horizMargins + (child.minWidth || childWidth || 0);
129 // Max height for align - force layout of non-laid out subcontainers without a numeric height
130 if (typeof childHeight != 'number') {
134 childHeight = child.getHeight();
137 maxHeight = Math.max(maxHeight, childHeight + childMargins.top + childMargins.bottom);
139 //cache the size of each child component. Don't set height or width to 0, keep undefined instead
142 height : childHeight || undefined,
143 width : childWidth || undefined
147 var shortfall = desiredWidth - width,
148 tooNarrow = minimumWidth > width;
150 //the width available to the flexed items
151 var availableWidth = Math.max(0, width - nonFlexWidth - paddingHoriz);
154 for (i = 0; i < visibleCount; i++) {
155 boxes[i].width = visibleItems[i].minWidth || visibleItems[i].width || boxes[i].width;
158 //all flexed items should be sized to their minimum width, other items should be shrunk down until
159 //the shortfall has been accounted for
164 * When we have a shortfall but are not tooNarrow, we need to shrink the width of each non-flexed item.
165 * Flexed items are immediately reduced to their minWidth and anything already at minWidth is ignored.
166 * The remaining items are collected into the minWidths array, which is later used to distribute the shortfall.
168 for (var index = 0, length = visibleCount; index < length; index++) {
169 var item = visibleItems[index],
170 minWidth = item.minWidth || 0;
172 //shrink each non-flex tab by an equal amount to make them all fit. Flexed items are all
173 //shrunk to their minWidth because they're flexible and should be the first to lose width
175 boxes[index].width = minWidth;
179 available: boxes[index].width - minWidth,
185 //sort by descending amount of width remaining before minWidth is reached
186 minWidths.sort(function(a, b) {
187 return a.available > b.available ? 1 : -1;
191 * Distribute the shortfall (difference between total desired with of all items and actual width available)
192 * between the non-flexed items. We try to distribute the shortfall evenly, but apply it to items with the
193 * smallest difference between their width and minWidth first, so that if reducing the width by the average
194 * amount would make that item less than its minWidth, we carry the remainder over to the next item.
196 for (var i = 0, length = minWidths.length; i < length; i++) {
197 var itemIndex = minWidths[i].index;
199 if (itemIndex == undefined) {
203 var item = visibleItems[itemIndex],
204 box = boxes[itemIndex],
205 oldWidth = box.width,
206 minWidth = item.minWidth,
207 newWidth = Math.max(minWidth, oldWidth - Math.ceil(shortfall / (length - i))),
208 reduction = oldWidth - newWidth;
210 boxes[itemIndex].width = newWidth;
211 shortfall -= reduction;
214 //temporary variables used in the flex width calculations below
215 var remainingWidth = availableWidth,
216 remainingFlex = totalFlex;
218 //calculate the widths of each flexed item
219 for (i = 0; i < visibleCount; i++) {
220 child = visibleItems[i];
223 childMargins = child.margins;
224 vertMargins = childMargins.top + childMargins.bottom;
226 if (isStart && child.flex && !child.width) {
227 flexedWidth = Math.ceil((child.flex / remainingFlex) * remainingWidth);
228 remainingWidth -= flexedWidth;
229 remainingFlex -= child.flex;
231 calcs.width = flexedWidth;
232 calcs.dirtySize = true;
239 leftOffset += availableWidth / 2;
241 leftOffset += availableWidth;
244 //finally, calculate the left and top position of each item
245 for (i = 0; i < visibleCount; i++) {
246 child = visibleItems[i];
249 childMargins = child.margins;
250 leftOffset += childMargins.left;
251 vertMargins = childMargins.top + childMargins.bottom;
253 calcs.left = leftOffset;
254 calcs.top = topOffset + childMargins.top;
256 switch (this.align) {
258 stretchHeight = availHeight - vertMargins;
259 calcs.height = stretchHeight.constrain(child.minHeight || 0, child.maxHeight || 1000000);
260 calcs.dirtySize = true;
263 stretchHeight = maxHeight - vertMargins;
264 calcs.height = stretchHeight.constrain(child.minHeight || 0, child.maxHeight || 1000000);
265 calcs.dirtySize = true;
268 var diff = availHeight - calcs.height - vertMargins;
270 calcs.top = topOffset + vertMargins + (diff / 2);
274 leftOffset += calcs.width + childMargins.right;
280 maxHeight : maxHeight,
281 nonFlexWidth: nonFlexWidth,
282 desiredWidth: desiredWidth,
283 minimumWidth: minimumWidth,
284 shortfall : desiredWidth - width,
285 tooNarrow : tooNarrow
291 Ext.Container.LAYOUTS.hbox = Ext.layout.HBoxLayout;