001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: * Darrell Meyer <darrell@mygwt.net> - derived implementation
011: *******************************************************************************/package net.mygwt.ui.client.widget.layout;
012:
013: import net.mygwt.ui.client.MyDOM;
014: import net.mygwt.ui.client.Style;
015: import net.mygwt.ui.client.util.Rectangle;
016: import net.mygwt.ui.client.widget.Layout;
017: import net.mygwt.ui.client.widget.WidgetContainer;
018:
019: import com.google.gwt.user.client.Element;
020: import com.google.gwt.user.client.ui.Widget;
021:
022: /**
023: * <code>FillLayout</code> lays out widgets in a single row or column, forcing
024: * them to be the same size.
025: */
026: public class FillLayout extends Layout {
027:
028: int margin = 0;
029: int spacing = 0;
030: int type = Style.HORIZONTAL;
031:
032: /**
033: * Creates a new layout instance.
034: */
035: public FillLayout() {
036:
037: }
038:
039: /**
040: * Creates a new instance with the given margin.
041: *
042: * @param margin the margin in pixels
043: */
044: public FillLayout(int margin) {
045: this .margin = margin;
046: }
047:
048: /**
049: * Returns the margin.
050: *
051: * @return the margin
052: */
053: public int getMargin() {
054: return margin;
055: }
056:
057: /**
058: * Returns the spacing.
059: *
060: * @return the spacing in pixels
061: */
062: public int getSpacing() {
063: return spacing;
064: }
065:
066: /**
067: * Returns the type.
068: *
069: * @return the type
070: */
071: public int getType() {
072: return type;
073: }
074:
075: /**
076: * Sets the number of pixels of margin that will be placed along the edges of
077: * the layout. The default value is 0.
078: *
079: * @param margin the margin in pixels
080: */
081: public void setMargin(int margin) {
082: this .margin = margin;
083: }
084:
085: /**
086: * Sets the number of pixels between the edge of one cell and the edge of its
087: * neighbouring cell. The default value is 0.
088: *
089: * @param spacing the spacing in pixels.
090: */
091: public void setSpacing(int spacing) {
092: this .spacing = spacing;
093: }
094:
095: /**
096: * Sets how widgets will be positioned within the layout. The default value is
097: * HORIZONTAL.
098: *
099: * @param type the type
100: */
101: public void setType(int type) {
102: this .type = type;
103: }
104:
105: protected void onLayout(WidgetContainer container, Element target) {
106: super .onLayout(container, target);
107:
108: int count = container.getWidgetCount();
109: if (count < 1) {
110: return;
111: }
112:
113: for (int i = 0; i < count; i++) {
114: Widget w = container.getWidget(i);
115: MyDOM.makePositionable(w.getElement(), count != 1);
116: }
117:
118: Element ct = container.getLayoutTarget();
119:
120: Rectangle rect = MyDOM.getBounds(ct, true);
121:
122: int width = rect.width - margin * 2;
123: int height = rect.height - margin * 2;
124:
125: if (type == Style.HORIZONTAL) {
126: width -= (count - 1) * spacing;
127: int x = rect.x + margin, extra = width % count;
128: int y = rect.y + margin, cellWidth = width / count;
129: x -= MyDOM.getScrollLeft(ct);
130: y -= MyDOM.getScrollTop(ct);
131: for (int i = 0; i < count; i++) {
132: Widget child = container.getWidget(i);
133: int childWidth = cellWidth;
134: if (i == 0) {
135: childWidth += extra / 2;
136: } else {
137: if (i == count - 1)
138: childWidth += (extra + 1) / 2;
139: }
140: setBounds(child, x, y, childWidth, height);
141: x += childWidth + spacing;
142: }
143: } else {
144: height -= (count - 1) * spacing;
145: int x = rect.x + margin, cellHeight = height / count;
146: int y = rect.y + margin, extra = height % count;
147: x -= MyDOM.getScrollLeft(ct);
148: y -= MyDOM.getScrollTop(ct);
149: for (int i = 0; i < count; i++) {
150: Widget child = container.getWidget(i);
151: int childHeight = cellHeight;
152: if (i == 0) {
153: childHeight += extra / 2;
154: } else {
155: if (i == count - 1)
156: childHeight += (extra + 1) / 2;
157: }
158: setBounds(child, x, y, width, childHeight);
159: y += childHeight + spacing;
160: }
161: }
162: }
163:
164: }
|