]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.eclipse.swt.win32.win32.x86_64/src/org/eclipse/swt/custom/StackLayout.java
Remove invalid SHA-256-Digests
[simantics/platform.git] / bundles / org.eclipse.swt.win32.win32.x86_64 / src / org / eclipse / swt / custom / StackLayout.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2018 IBM Corporation and others.
3  *
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/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.swt.custom;
15
16
17 import org.eclipse.swt.*;
18 import org.eclipse.swt.graphics.*;
19 import org.eclipse.swt.widgets.*;
20
21 /**
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.
27  *
28  * <p> Here is an example which places ten buttons in a stack layout and
29  * flips between them:
30  *
31  * <pre><code>
32  *      public static void main(String[] args) {
33  *              Display display = new Display();
34  *              Shell shell = new Shell(display);
35  *              shell.setLayout(new GridLayout());
36  *
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 &lt; 10; i++) {
43  *                      bArray[i] = new Button(parent, SWT.PUSH);
44  *                      bArray[i].setText("Button "+i);
45  *              }
46  *              layout.topControl = bArray[0];
47  *
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]];
55  *                              parent.layout();
56  *                      }
57  *              });
58  *
59  *              shell.open();
60  *              while (shell != null &amp;&amp; !shell.isDisposed()) {
61  *                      if (!display.readAndDispatch())
62  *                              display.sleep();
63  *              }
64  *      }
65  * </code></pre>
66  *
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>
70  */
71
72 public class StackLayout extends Layout {
73
74         /**
75          * marginWidth specifies the number of points of horizontal margin
76          * that will be placed along the left and right edges of the layout.
77          *
78          * The default value is 0.
79          */
80         public int marginWidth = 0;
81         /**
82          * marginHeight specifies the number of points of vertical margin
83          * that will be placed along the top and bottom edges of the layout.
84          *
85          * The default value is 0.
86          */
87         public int marginHeight = 0;
88
89         /**
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.
92          */
93         public Control topControl;
94
95 @Override
96 protected Point computeSize(Composite composite, int wHint, int hHint, boolean flushCache) {
97         Control children[] = composite.getChildren();
98         int maxWidth = 0;
99         int maxHeight = 0;
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);
104         }
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);
110 }
111
112 @Override
113 protected boolean flushCache(Control control) {
114         return true;
115 }
116
117 @Override
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);
128         }
129 }
130
131 String getName () {
132         String string = getClass ().getName ();
133         int index = string.lastIndexOf ('.');
134         if (index == -1) return string;
135         return string.substring (index + 1, string.length ());
136 }
137
138 /**
139  * Returns a string containing a concise, human-readable
140  * description of the receiver.
141  *
142  * @return a string representation of the layout
143  */
144 @Override
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();
151         string += "}";
152         return string;
153 }
154 }