1 /*******************************************************************************
2 * Copyright (c) 2000, 2018 IBM Corporation and others.
4 * This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
9 * SPDX-License-Identifier: EPL-2.0
12 * IBM Corporation - initial API and implementation
13 *******************************************************************************/
14 package org.eclipse.swt.custom;
17 import org.eclipse.swt.*;
18 import org.eclipse.swt.graphics.*;
19 import org.eclipse.swt.widgets.*;
22 * This Layout stacks all the controls one on top of the other and resizes all controls
23 * to have the same size and location.
24 * The control specified in topControl is visible and all other controls are not visible.
25 * Users must set the topControl value to flip between the visible items and then call
26 * layout() on the composite which has the StackLayout.
28 * <p> Here is an example which places ten buttons in a stack layout and
32 * public static void main(String[] args) {
33 * Display display = new Display();
34 * Shell shell = new Shell(display);
35 * shell.setLayout(new GridLayout());
37 * final Composite parent = new Composite(shell, SWT.NONE);
38 * parent.setLayoutData(new GridData(GridData.FILL_BOTH));
39 * final StackLayout layout = new StackLayout();
40 * parent.setLayout(layout);
41 * final Button[] bArray = new Button[10];
42 * for (int i = 0; i < 10; i++) {
43 * bArray[i] = new Button(parent, SWT.PUSH);
44 * bArray[i].setText("Button "+i);
46 * layout.topControl = bArray[0];
48 * Button b = new Button(shell, SWT.PUSH);
49 * b.setText("Show Next Button");
50 * final int[] index = new int[1];
51 * b.addListener(SWT.Selection, new Listener(){
52 * public void handleEvent(Event e) {
53 * index[0] = (index[0] + 1) % 10;
54 * layout.topControl = bArray[index[0]];
60 * while (shell != null && !shell.isDisposed()) {
61 * if (!display.readAndDispatch())
67 * @see <a href="http://www.eclipse.org/swt/snippets/#stacklayout">StackLayout snippets</a>
68 * @see <a href="http://www.eclipse.org/swt/examples.php">SWT Example: LayoutExample</a>
69 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
72 public class StackLayout extends Layout {
75 * marginWidth specifies the number of points of horizontal margin
76 * that will be placed along the left and right edges of the layout.
78 * The default value is 0.
80 public int marginWidth = 0;
82 * marginHeight specifies the number of points of vertical margin
83 * that will be placed along the top and bottom edges of the layout.
85 * The default value is 0.
87 public int marginHeight = 0;
90 * topControl the Control that is displayed at the top of the stack.
91 * All other controls that are children of the parent composite will not be visible.
93 public Control topControl;
96 protected Point computeSize(Composite composite, int wHint, int hHint, boolean flushCache) {
97 Control children[] = composite.getChildren();
100 for (int i = 0; i < children.length; i++) {
101 Point size = children[i].computeSize(wHint, hHint, flushCache);
102 maxWidth = Math.max(size.x, maxWidth);
103 maxHeight = Math.max(size.y, maxHeight);
105 int width = maxWidth + 2 * marginWidth;
106 int height = maxHeight + 2 * marginHeight;
107 if (wHint != SWT.DEFAULT) width = wHint;
108 if (hHint != SWT.DEFAULT) height = hHint;
109 return new Point(width, height);
113 protected boolean flushCache(Control control) {
118 protected void layout(Composite composite, boolean flushCache) {
119 Control children[] = composite.getChildren();
120 Rectangle rect = composite.getClientArea();
121 rect.x += marginWidth;
122 rect.y += marginHeight;
123 rect.width -= 2 * marginWidth;
124 rect.height -= 2 * marginHeight;
125 for (int i = 0; i < children.length; i++) {
126 children[i].setBounds(rect);
127 children[i].setVisible(children[i] == topControl);
132 String string = getClass ().getName ();
133 int index = string.lastIndexOf ('.');
134 if (index == -1) return string;
135 return string.substring (index + 1, string.length ());
139 * Returns a string containing a concise, human-readable
140 * description of the receiver.
142 * @return a string representation of the layout
145 public String toString () {
146 String string = getName ()+" {";
147 if (marginWidth != 0) string += "marginWidth="+marginWidth+" ";
148 if (marginHeight != 0) string += "marginHeight="+marginHeight+" ";
149 if (topControl != null) string += "topControl="+topControl+" ";
150 string = string.trim();